<?php
/**
 * Blog post schema.org implementation
 */

use Magento\Framework\Escaper;
use Mageplaza\Blog\Block\Post\View;

/** @var Escaper $escaper */
/** @var View $block */
$helper = $block->getBlogHelper();
$_post = $block->getPost();

// Get blog post data for schema
$postTitle = $_post->getName();
$postDescription = $_post->getShortDescription() ?: '';
$postUrl = $_post->getUrl();
$postPublishDate = $_post->getPublishDate() ?: $_post->getCreatedAt();
$postModifiedDate = $_post->getUpdatedAt() ?: $postPublishDate;

// Get blog post image
$postImage = '';
if ($_post->getImage()) {
    $postImage = $block->getImageUrl($_post->getImage());
}

// Get logo URL for publisher
$baseUrl = $block->getUrl('');
$storeUrl = rtrim($baseUrl, '/');
$logoUrl = $storeUrl . '/media/logo/stores/3/logobanyo_1.png';

// Get main blog page URL
$mainBlogUrl = $storeUrl . '/blog';
?>
<?php
// Create blog post schema data array
$blogPostSchema = [
    "@context" => "http://schema.org",
    "@type" => "BlogPosting",
    "@id" => $postUrl,
    "headline" => $postTitle,
    "description" => $postDescription,
    "datePublished" => $postPublishDate,
    "dateModified" => $postModifiedDate,
    "publisher" => [
        "@type" => "Organization",
        "name" => "Banyo",
        "logo" => [
            "@type" => "ImageObject",
            "url" => $logoUrl
        ]
    ],
    "mainEntityOfPage" => $mainBlogUrl
];

// Add image if available
if ($postImage) {
    $blogPostSchema["image"] = [
        "@type" => "ImageObject",
        "url" => $postImage,
        "width" => 720,
        "height" => 720
    ];
}
?>
<script type="application/ld+json">
<?= json_encode($blogPostSchema) ?>
</script>
