process gallery media in the correct order

This commit is contained in:
Andrew Dolgov 2021-12-23 17:33:04 +03:00
parent 1818fc11a5
commit 97baf3e8b9
1 changed files with 18 additions and 1 deletions

View File

@ -158,7 +158,24 @@ class Af_RedditImgur extends Plugin {
private function process_post_media(array $data, DOMDocument $doc, DOMXPath $xpath, DOMElement $anchor) : bool {
$found = 0;
if (isset($data["media_metadata"])) {
// process galleries in the right order
if (isset($data["gallery_data"]) && isset($data["media_metadata"])) {
foreach ($data["gallery_data"]["items"] as $gal_item) {
$media_id = $gal_item["media_id"] ?? null;
if ($media_id) {
$media_url = htmlspecialchars_decode($data["media_metadata"][$media_id]["s"]["u"] ?? "");
if ($media_url) {
Debug::log("found gallery item: $media_id, url: $media_url", Debug::LOG_EXTENDED);
$this->handle_as_image($doc, $anchor, $media_url);
$found = 1;
}
}
}
// i'm not sure if this is a thing, but if there's no gallery just process any possible attaches in the random order...
} else if (isset($data["media_metadata"])) {
foreach ($data["media_metadata"] as $media) {
if (!empty($media["s"]["u"])) {
$media_url = htmlspecialchars_decode($media["s"]["u"]);