![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/app/code/Soon/Faq/Block/Question/ |
<?php /** * Copyright (c) 2020 Kaliop Digital Commerce (https://digitalcommerce.kaliop.com) All Rights Reserved. * https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) * * Hugo Herblot <[email protected]> <[email protected]> */ namespace Soon\Faq\Block\Question; use Magento\Framework\View\Element\Text; use Magento\Framework\View\Element\Context; use Magento\Framework\Serialize\Serializer\Json; class Richsnippets extends Text { /** * @var Json */ private $json; /** * @param array */ private $richSnippet; /** * @var ListQuestion */ private $listQuestion; /** * Richsnippets constructor. * @param Context $context * @param Json $json * @param ListQuestion $listQuestion * @param array $data */ public function __construct( Context $context, Json $json, ListQuestion $listQuestion, array $data = [] ) { parent::__construct($context, $data); $this->json = $json; $this->listQuestion = $listQuestion; } /** * Retrieve snipet params * * @return array */ public function getRichSnippetData() { $listQuestions = $this->listQuestion->getCategoriesWithQuestions(); if ($listQuestions) { $mainEntity = []; foreach ($listQuestions as $listQuestion) { foreach ($listQuestion->getData('category_questions') as $question) { $mainEntity[] = [ "@type" => "Question", 'name' => $this->stripTags($question->getData('question')), 'acceptedAnswer' => [ "@type" => "Answer", "text" => $this->stripTags($this->listQuestion->getAnswerForQuestion($question)) ] ]; } } $this->richSnippet = [ '@context' => 'http://schema.org', "@type" => "FAQPage", 'mainEntity' => $mainEntity ]; } return $this->richSnippet; } /** * Render html output * * @return string */ protected function _toHtml() { if ($richSnippetData = $this->getRichSnippetData()) { $data = '<script type="application/ld+json">' . $this->json->serialize($richSnippetData) . '</script>'; $this->setText($data); } return parent::_toHtml(); } }