meraproject/module/core/site/structure/SchemaOrg.php

136 lines
3.7 KiB
PHP
Raw Permalink Normal View History

<?php
namespace core\site\structure;
use common\faq\question;
use core\site\structure\HTMLPage as Page;
use blog\news;
class SchemaOrg {
var $faqLink = '#faq';
var $newsLink = '#news';
var $faq = [];
var $news = [];
var $mainID;
public static function formatTimeSeo($x){
return date('Y-m-d\TH:i:sP', $x);
}
function addFaqArray($questions) {
foreach ($questions as $q) $this->addFaq($q);
}
function addFaq($question) {
$this->faq[] = [
'@type' => 'Question',
'name' => $question[Question::$QUESTION],
'acceptedAnswer' => [
'@type' => 'Answer',
'text' => $question[Question::$ANSWER]
]
];
}
function addNewsArray($news) {
foreach ($news as $n) $this->addNews($n);
}
function addNews($news) {
$id = \Site::sectionUrl(substr($news['link'],1),true);
if($news['schemaorg_main']) $this->mainID = $id;
$el = [
'@type' => 'NewsArticle',
'mainEntityOfPage' => [
'@type' => 'WebPage',
'@id' => \Site::sectionUrl(substr($news['link'],1),true)
],
'headline' => News::getName($news),
'datePublished' => self::formatTimeSeo($news[News::$CREATED]),
'dateModified' => self::formatTimeSeo($news[News::$UPDATED]),
'author' => $news['author'],
'publisher' => $news['publisher'],
'description' => $news[News::$ANNOTATION]
];
if($news['preview']) {
$el['image'] = \Site::sectionUrl(substr($news['preview'],1),true);
}
$this->news[] = $el;
}
function toJson():array
{
$url = Page::$canonical;
$faq = null;
if($this->faq){
$a = [];
foreach ($this->faq as $q){
$a[] = $q;
}
$faq = [
"@type"=> "FAQPage",
"@id"=> $url.$this->faqLink,
'mainEntity'=>$a
];
}
$listItems = [];
if($this->news) {
$listItems[] = [
'title'=>'Новости',
'id'=>$url.$this->newsLink,
'list'=>$this->news,
];
}
$graphListItems = null;
if($listItems){
$graphListItems = [];
foreach ($listItems as $v) {
$list = [];
foreach($v['list'] as $el){
$list[] = [
'@type' => 'ListItem',
'item'=>$el,
];
}
$graphListItems[] = [
'@type' => 'ItemList',
'@id' => $v['id'],
'name' => $v['title'],
'numberOfItems' => count($v['list']),
'itemListOrder' => 'ItemListUnordered',
'itemListElement'=>$list
];
}
}
$main =
[
'@type' => 'WebPage',
'@id' => $url,
'name' => Page::getTitleText(),
'description' => Page::$description,
'url' => $url,
//'mainEntity' => [ '@id' => 'https://site.com/games/main-game-page/#main-product' ]
];
if($this->mainID) $main['mainEntity'] = ['@id'=>$this->mainID];
return [
"@context"=> "https://schema.org",
"@graph"=>
array_diff(
array_merge(
[$main,$faq,]
,$graphListItems
)
, [null]),
];
}
}