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