Add blog post link preview

This commit is contained in:
grandeljay 2022-06-08 15:54:13 +02:00
parent c0abf38189
commit 90f4de5885
3 changed files with 31 additions and 2 deletions

View file

@ -89,6 +89,28 @@ class Blog
return $htmlPicture;
}
public static function getMediaPreviewURL(int $mediaID): string
{
$url = '';
$media = self::getMedia($mediaID);
$mediaSizes = (array) $media->media_details->sizes;
uasort(
$mediaSizes,
function ($a, $b) {
$sizeA = $a->width + $a->height;
$sizeB = $b->width + $b->height;
return $sizeA <=> $sizeB;
}
);
$mediaSmallest = (object) reset($mediaSizes);
$url = $mediaSmallest->source_url;
return $url;
}
public static function getCategory(int $categoryID): \stdClass
{
$category = self::get(sprintf(self::ENDPOINT_CATEGORIES, $categoryID));

View file

@ -99,8 +99,9 @@ class Page
/**
* Non-Static
*/
public string $language = DEFAULT_LOCALE;
public array $messages = array();
public string $language = DEFAULT_LOCALE;
public array $messages = array();
public string $link_preview;
/**
* __construct

View file

@ -11,8 +11,14 @@ namespace wishthis;
$postSlug = $_SESSION['_GET']['slug'];
$post = Blog::getPostBySlug($postSlug);
$postMediaHTML = isset($post->featured_media) ? Blog::getMediaHTML($post->featured_media) : '';
$postMediaURL = isset($post->featured_media) ? Blog::getMediaPreviewURL($post->featured_media) : '';
$page = new Page(__FILE__, $post->title->rendered);
if ('' !== $postMediaURL) {
$page->link_preview = $postMediaURL;
}
$page->header();
$page->bodyStart();
$page->navigation();