+ switch (JsonLD::fetchElement($attachment, '@type')) {
+ case 'as:Page':
+ $pageUrl = null;
+ $pageImage = null;
+
+ $urls = JsonLD::fetchElementArray($attachment, 'as:url');
+ foreach ($urls as $url) {
+ // Single scalar URL case
+ if (is_string($url)) {
+ $pageUrl = $url;
+ continue;
+ }
+
+ $href = JsonLD::fetchElement($url, 'as:href', '@id');
+ $mediaType = JsonLD::fetchElement($url, 'as:mediaType', '@value');
+ if (Strings::startsWith($mediaType, 'image')) {
+ $pageImage = $href;
+ } else {
+ $pageUrl = $href;
+ }
+ }
+
+ $attachlist[] = [
+ 'type' => 'link',
+ 'title' => JsonLD::fetchElement($attachment, 'as:name', '@value'),
+ 'desc' => JsonLD::fetchElement($attachment, 'as:summary', '@value'),
+ 'url' => $pageUrl,
+ 'image' => $pageImage,
+ ];
+ break;
+ case 'as:Link':
+ $attachlist[] = [
+ 'type' => str_replace('as:', '', JsonLD::fetchElement($attachment, '@type')),
+ 'mediaType' => JsonLD::fetchElement($attachment, 'as:mediaType', '@value'),
+ 'name' => JsonLD::fetchElement($attachment, 'as:name', '@value'),
+ 'url' => JsonLD::fetchElement($attachment, 'as:href', '@id')
+ ];
+ break;
+ case 'as:Image':
+ $mediaType = JsonLD::fetchElement($attachment, 'as:mediaType', '@value');
+ $imageFullUrl = JsonLD::fetchElement($attachment, 'as:url', '@id');
+ $imagePreviewUrl = null;
+ // Multiple URLs?
+ if (!$imageFullUrl && ($urls = JsonLD::fetchElementArray($attachment, 'as:url'))) {
+ $imageVariants = [];
+ $previewVariants = [];
+ foreach ($urls as $url) {
+ // Scalar URL, no discrimination possible
+ if (is_string($url)) {
+ $imageFullUrl = $url;
+ continue;
+ }
+
+ // Not sure what to do with a different Link media type than the base Image, we skip
+ if ($mediaType != JsonLD::fetchElement($url, 'as:mediaType', '@value')) {
+ continue;
+ }
+
+ $href = JsonLD::fetchElement($url, 'as:href', '@id');
+
+ // Default URL choice if no discriminating width is provided
+ $imageFullUrl = $href ?? $imageFullUrl;
+
+ $width = intval(JsonLD::fetchElement($url, 'as:width', '@value') ?? 1);
+
+ if ($href && $width) {
+ $imageVariants[$width] = $href;
+ // 632 is the ideal width for full screen frio posts, we compute the absolute distance to it
+ $previewVariants[abs(632 - $width)] = $href;
+ }
+ }