<?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->getMetaDescription() ?: '';
$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
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get(\Magento\Store\Model\StoreManagerInterface::class);
$logoUrl = $storeManager
    ->getStore()
    ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA)
    . 'logo/stores/1/BanyoStoreLogo.png';

// Get main blog page URL
$baseUrl = $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);
$mainBlogUrl = rtrim($baseUrl, '/') . '/article';
?>
<?php
// Create blog post schema data array
$blogPostSchema = [
    "@context" => "http://schema.org",
    "@type" => "BlogPosting",
    "@id" => $postUrl,
    "headline" => $postTitle,
    "description" => $postDescription,
    "datePublished" => $postPublishDate,
    "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" => 1024,
        "height" => 520
    ];
}
?>
<script type="application/ld+json">
<?= json_encode($blogPostSchema) ?>
</script>
