1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace XoopsModules\Publisher; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
class Jsonld |
10
|
|
|
{ |
11
|
|
|
public $website = []; |
12
|
|
|
|
13
|
|
|
public static function getAuthor(\XoopsUser $xoopsUser) |
14
|
|
|
{ |
15
|
|
|
$author = [ |
16
|
|
|
'@type' => 'Person', |
17
|
|
|
'name' => $xoopsUser->getVar('name') ?? $xoopsUser->getVar('uname'), |
18
|
|
|
'email' => $xoopsUser->getVar('email') ?? '', |
19
|
|
|
'telephone' => $xoopsUser->getVar('phone') ?? '', |
20
|
|
|
"sameAs" => [ |
21
|
|
|
$xoopsUser->getVar('facebook') ?? '',//"https://www.facebook.com/your-organization-url", |
22
|
|
|
$xoopsUser->getVar('instagram') ?? '',//"https://www.instagram.com/your-organization-url/", |
23
|
|
|
], |
24
|
|
|
]; |
25
|
|
|
return $author; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public static function getAuthoritem($xoopsUser, array $xoopsConfig, string $xoops_url) |
29
|
|
|
{ |
30
|
|
|
$ret = ''; |
31
|
|
|
$helper = Helper::getInstance(); |
32
|
|
|
if ($helper->getConfig('generate_jsonld')) { |
33
|
|
|
$schema['@context'] = 'https://schema.org/'; |
|
|
|
|
34
|
|
|
$schema['@type'] = 'Article'; |
35
|
|
|
if ($xoopsUser instanceof \XoopsUser) { |
36
|
|
|
$schema['author'] = self::getAuthor($xoopsUser); |
37
|
|
|
} |
38
|
|
|
$schema['publisher'] = self::getOrganization($xoopsConfig, $xoops_url); |
39
|
|
|
|
40
|
|
|
$ret = '<script type="application/ld+json">' . json_encode($schema, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES) . '</script>'; |
41
|
|
|
} |
42
|
|
|
return $ret; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public static function getCategory(Category $categoryObj) |
46
|
|
|
{ |
47
|
|
|
$ret = ''; |
48
|
|
|
$helper = Helper::getInstance(); |
49
|
|
|
if ($helper->getConfig('generate_jsonld')) { |
50
|
|
|
global $xoopsConfig, $xoopsUser, $xoops_url; |
51
|
|
|
$schema = []; |
52
|
|
|
$schema['@context'] = 'https://schema.org/'; |
53
|
|
|
$schema['@type'] = 'Article'; |
54
|
|
|
$schema['articleSection'] = $categoryObj->getVar('name'); |
55
|
|
|
$schema['publisher'] = self::getOrganization($xoopsConfig, $xoops_url); |
56
|
|
|
|
57
|
|
|
$ret = '<script type="application/ld+json">' . json_encode($schema, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES) . '</script>'; |
58
|
|
|
} |
59
|
|
|
return $ret; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public static function getArticle(Item $itemObj, Category $categoryObj, \XoopsUser $xoopsUser, Helper $helper) |
63
|
|
|
{ |
64
|
|
|
$itemImage = $itemObj->getVar('image'); |
65
|
|
|
if (isset($itemImage)) { |
66
|
|
|
$imageHandler = xoops_getHandler('image'); |
67
|
|
|
$criteria = new \Criteria('image_id', $itemObj->getVar('image')); |
|
|
|
|
68
|
|
|
$image = $imageHandler->getObjects($criteria)[0]??null; |
|
|
|
|
69
|
|
|
$imageUrl = ''; |
70
|
|
|
if (null!==$image) { |
71
|
|
|
$imageUrl = XOOPS_URL . '/images/' . $image->getVar('image_name'); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
if ($xoopsUser instanceof \XoopsUser) { |
|
|
|
|
75
|
|
|
$authorName = $xoopsUser->getVar('name') ?? $xoopsUser->getVar('uname'); |
76
|
|
|
} |
77
|
|
|
$item = [ |
78
|
|
|
"@context" => "https://schema.org", |
79
|
|
|
'@type' => 'Article', |
80
|
|
|
'articleSection' => $categoryObj->getVar('name'), |
81
|
|
|
'url' => $helper->url('item.php?itemid=') . $itemObj->getVar('itemid'), |
82
|
|
|
'headline' => $itemObj->getVar('title'), |
83
|
|
|
'text' => $itemObj->getVar('body'), |
84
|
|
|
'datePublished' => $itemObj->getVar('datesub'), |
85
|
|
|
'dateModified' => $itemObj->getVar('datesub'), |
86
|
|
|
'name' => $authorName, |
|
|
|
|
87
|
|
|
'aggregateRating' => [ |
88
|
|
|
'@type' => 'AggregateRating', |
89
|
|
|
'ratingValue' => $itemObj->getVar('rating'), |
90
|
|
|
'ratingCount' => $itemObj->getVar('votes'), |
91
|
|
|
], |
92
|
|
|
'commentCount' => $itemObj->getVar('comments'), |
93
|
|
|
'image' => [ |
94
|
|
|
'@type' => 'ImageObject', |
95
|
|
|
'url' => $imageUrl, |
|
|
|
|
96
|
|
|
'width' => '300', |
97
|
|
|
'height' => '60', |
98
|
|
|
], |
99
|
|
|
'description' => $itemObj->getVar('summary'), |
100
|
|
|
]; |
101
|
|
|
return $item; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public static function getOrganization(array $xoopsConfig, string $xoops_url) |
105
|
|
|
{ |
106
|
|
|
$organization = [ |
107
|
|
|
"@context" => "https://schema.org", |
108
|
|
|
"@type" => "Organization", |
109
|
|
|
"name" => $xoopsConfig['sitename'], |
110
|
|
|
'slogan' => $xoopsConfig['slogan'], |
111
|
|
|
"url" => $xoops_url, |
112
|
|
|
'logo' => [ |
113
|
|
|
'@type' => 'ImageObject', |
114
|
|
|
'url' => $xoops_url . '/images/logo.png', |
115
|
|
|
'width' => '300', |
116
|
|
|
'height' => '60', |
117
|
|
|
], |
118
|
|
|
// "sameAs" => [ |
119
|
|
|
// "https://www.facebook.com/your-organization-url", |
120
|
|
|
// "https://www.instagram.com/your-organization-url/", |
121
|
|
|
// ], |
122
|
|
|
]; |
123
|
|
|
return $organization; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public static function getWebsite($xoopsConfig, $xoops_url) |
127
|
|
|
{ |
128
|
|
|
$website = [ |
129
|
|
|
"@context" => "https://schema.org", |
130
|
|
|
"@type" => "WebSite", |
131
|
|
|
"name" => $xoopsConfig['sitename'], |
132
|
|
|
'slogan' => $xoopsConfig['slogan'], |
133
|
|
|
"url" => $xoops_url, |
134
|
|
|
]; |
135
|
|
|
return $website; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public static function getIndex($xoopsConfig, $xoopsUser, $xoops_url) |
139
|
|
|
{ |
140
|
|
|
$ret = ''; |
141
|
|
|
$helper = Helper::getInstance(); |
142
|
|
|
|
143
|
|
|
if ($helper->getConfig('generate_jsonld')) { |
144
|
|
|
$schema = []; |
145
|
|
|
|
146
|
|
|
// $website = self::getWebsite($xoopsConfig, $xoops_url); |
147
|
|
|
|
148
|
|
|
$schema['@context'] = 'https://schema.org/'; |
149
|
|
|
$schema['@type'] = 'Article'; |
150
|
|
|
if ($xoopsUser instanceof \XoopsUser) { |
151
|
|
|
$schema['author'] = self::getAuthor($xoopsUser); |
152
|
|
|
} |
153
|
|
|
$schema['publisher'] = self::getOrganization($xoopsConfig, $xoops_url); |
154
|
|
|
$ret = '<script type="application/ld+json">' . json_encode($schema, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES) . '</script>'; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $ret; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public static function getBreadcrumbs(?array $data, $settings = null) |
|
|
|
|
161
|
|
|
{ |
162
|
|
|
// |
163
|
|
|
// $breadcrumbs = [ |
164
|
|
|
// "@context" => "https://schema.org", |
165
|
|
|
// "@type" => "BreadcrumbList", |
166
|
|
|
// "itemListElement" => $listItems, |
167
|
|
|
// ]; |
168
|
|
|
// } |
169
|
|
|
// return $breadcrumbs; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public static function getItem(Item $itemObj, Category $categoryObj): string |
173
|
|
|
{ |
174
|
|
|
$ret = ''; |
175
|
|
|
$helper = Helper::getInstance(); |
176
|
|
|
if ($helper->getConfig('generate_jsonld')) { |
177
|
|
|
global $xoopsConfig, $xoopsUser, $xoops_url; |
178
|
|
|
$schema = []; |
179
|
|
|
// $website = self::getWebsite($xoopsConfig, $xoops_url); |
180
|
|
|
// $category = self::getCategory($categoryObj); |
181
|
|
|
|
182
|
|
|
if ($xoopsUser instanceof \XoopsUser) { |
183
|
|
|
$schema['article'] = self::getArticle($itemObj, $categoryObj, $xoopsUser, $helper); |
184
|
|
|
$schema['author'] = self::getAuthor($xoopsUser); |
185
|
|
|
} |
186
|
|
|
$schema['publisher'] = self::getOrganization($xoopsConfig, $xoops_url); |
187
|
|
|
|
188
|
|
|
$ret = '<script type="application/ld+json">' . json_encode($schema, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES) . '</script>'; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return $ret; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
|