1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace nystudio107\seomatic\helpers; |
4
|
|
|
|
5
|
|
|
use benf\neo\elements\Block as NeoBlock; |
6
|
|
|
use Craft; |
7
|
|
|
use craft\base\Element; |
8
|
|
|
use craft\base\Event; |
9
|
|
|
use craft\console\Application as ConsoleApplication; |
10
|
|
|
use craft\db\Paginator; |
11
|
|
|
use craft\elements\Asset; |
12
|
|
|
use craft\elements\Entry; |
13
|
|
|
use craft\errors\SiteNotFoundException; |
14
|
|
|
use craft\fields\Assets as AssetsField; |
15
|
|
|
use DateTime; |
16
|
|
|
use nystudio107\seomatic\base\SeoElementInterface; |
17
|
|
|
use nystudio107\seomatic\events\IncludeSitemapEntryEvent; |
18
|
|
|
use nystudio107\seomatic\fields\SeoSettings; |
19
|
|
|
use nystudio107\seomatic\helpers\Field as FieldHelper; |
20
|
|
|
use nystudio107\seomatic\models\MetaBundle; |
21
|
|
|
use nystudio107\seomatic\models\SitemapTemplate; |
22
|
|
|
use nystudio107\seomatic\Seomatic; |
23
|
|
|
use Throwable; |
24
|
|
|
use yii\base\Exception; |
25
|
|
|
use yii\helpers\Html; |
26
|
|
|
use function array_intersect_key; |
27
|
|
|
use function count; |
28
|
|
|
use function in_array; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @author nystudio107 |
32
|
|
|
* @package Seomatic |
33
|
|
|
* @since 3.4.18 |
34
|
|
|
*/ |
35
|
|
|
class Sitemap |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* @event IncludeSitemapEntryEvent The event that is triggered when an entry is |
39
|
|
|
* about to be included in a sitemap |
40
|
|
|
* |
41
|
|
|
* --- |
42
|
|
|
* ```php |
43
|
|
|
* use nystudio107\seomatic\events\IncludeSitemapEntryEvent; |
44
|
|
|
* use nystudio107\seomatic\helpers\Sitemap; |
45
|
|
|
* use yii\base\Event; |
46
|
|
|
* Event::on(Sitemap::class, Sitemap::EVENT_INCLUDE_SITEMAP_ENTRY, function(IncludeSitemapEntryEvent $e) { |
47
|
|
|
* $e->include = false; |
48
|
|
|
* }); |
49
|
|
|
* ``` |
50
|
|
|
*/ |
51
|
|
|
public const EVENT_INCLUDE_SITEMAP_ENTRY = 'includeSitemapEntry'; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @const The number of assets to return in a single paginated query |
55
|
|
|
*/ |
56
|
|
|
public const SITEMAP_QUERY_PAGE_SIZE = 100; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Generate a sitemap with the passed in $params |
60
|
|
|
* |
61
|
|
|
* @param array $params |
62
|
|
|
* @return string |
63
|
|
|
* @throws SiteNotFoundException |
64
|
|
|
*/ |
65
|
|
|
public static function generateSitemap(array $params): ?string |
66
|
|
|
{ |
67
|
|
|
$groupId = $params['groupId']; |
68
|
|
|
$type = $params['type']; |
69
|
|
|
$handle = $params['handle']; |
70
|
|
|
$siteId = $params['siteId']; |
71
|
|
|
$page = $params['page']; |
72
|
|
|
|
73
|
|
|
// Get an array of site ids for this site group |
74
|
|
|
$groupSiteIds = []; |
75
|
|
|
|
76
|
|
|
if (Seomatic::$settings->siteGroupsSeparate) { |
77
|
|
|
if (empty($groupId)) { |
78
|
|
|
try { |
79
|
|
|
$thisSite = Craft::$app->getSites()->getSiteById($siteId); |
80
|
|
|
if ($thisSite !== null) { |
81
|
|
|
$group = $thisSite->getGroup(); |
82
|
|
|
$groupId = $group->id; |
83
|
|
|
} |
84
|
|
|
} catch (Throwable $e) { |
85
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
$siteGroup = Craft::$app->getSites()->getGroupById($groupId); |
89
|
|
|
if ($siteGroup !== null) { |
90
|
|
|
$groupSiteIds = $siteGroup->getSiteIds(); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if (empty($groupSiteIds)) { |
95
|
|
|
$groupSiteIds = Craft::$app->getSites()->allSiteIds; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$lines = []; |
99
|
|
|
// Sitemap index XML header and opening tag |
100
|
|
|
$lines[] = '<?xml version="1.0" encoding="UTF-8"?>'; |
101
|
|
|
$lines[] = '<?xml-stylesheet type="text/xsl" href="sitemap.xsl"?>'; |
102
|
|
|
// One sitemap entry for each element |
103
|
|
|
$metaBundle = Seomatic::$plugin->metaBundles->getMetaBundleBySourceHandle( |
104
|
|
|
$type, |
105
|
|
|
$handle, |
106
|
|
|
$siteId |
107
|
|
|
); |
108
|
|
|
// If it doesn't exist, exit |
109
|
|
|
if ($metaBundle === null) { |
110
|
|
|
return null; |
111
|
|
|
} |
112
|
|
|
$multiSite = count($metaBundle->sourceAltSiteSettings) > 1; |
113
|
|
|
$totalElements = null; |
114
|
|
|
$urlsetLine = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"'; |
115
|
|
|
if ($metaBundle->metaSitemapVars->sitemapAssets) { |
116
|
|
|
$urlsetLine .= ' xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"'; |
117
|
|
|
$urlsetLine .= ' xmlns:video="http://www.google.com/schemas/sitemap-video/1.1"'; |
118
|
|
|
} |
119
|
|
|
if ($multiSite) { |
120
|
|
|
$urlsetLine .= ' xmlns:xhtml="http://www.w3.org/1999/xhtml"'; |
121
|
|
|
} |
122
|
|
|
if ((bool)$metaBundle->metaSitemapVars->newsSitemap) { |
123
|
|
|
$urlsetLine .= ' xmlns:news="http://www.google.com/schemas/sitemap-news/0.9"'; |
124
|
|
|
} |
125
|
|
|
$urlsetLine .= '>'; |
126
|
|
|
$lines[] = $urlsetLine; |
127
|
|
|
|
128
|
|
|
// Get all of the elements for this meta bundle type |
129
|
|
|
$seoElement = Seomatic::$plugin->seoElements->getSeoElementByMetaBundleType($metaBundle->sourceBundleType); |
130
|
|
|
|
131
|
|
|
if ($seoElement !== null) { |
132
|
|
|
// Ensure `null` so that the resulting element query is correct |
133
|
|
|
if (empty($metaBundle->metaSitemapVars->sitemapLimit)) { |
134
|
|
|
$metaBundle->metaSitemapVars->sitemapLimit = null; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$totalElements = $seoElement::sitemapElementsQuery($metaBundle)->count(); |
138
|
|
|
if ($metaBundle->metaSitemapVars->sitemapLimit && ($totalElements > $metaBundle->metaSitemapVars->sitemapLimit)) { |
|
|
|
|
139
|
|
|
$totalElements = $metaBundle->metaSitemapVars->sitemapLimit; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// If no elements exist, just exit |
144
|
|
|
if (!$totalElements) { |
145
|
|
|
return null; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
// Stash the sitemap attributes so they can be modified on a per-entry basis |
149
|
|
|
$stashedSitemapAttrs = $metaBundle->metaSitemapVars->getAttributes(); |
|
|
|
|
150
|
|
|
$stashedGlobalVarsAttrs = $metaBundle->metaGlobalVars->getAttributes(); |
|
|
|
|
151
|
|
|
// Use craft\db\Paginator to paginate the results so we don't exceed any memory limits |
152
|
|
|
// See batch() and each() discussion here: https://github.com/yiisoft/yii2/issues/8420 |
153
|
|
|
// and here: https://github.com/craftcms/cms/issues/7338 |
154
|
|
|
|
155
|
|
|
$elementQuery = $seoElement::sitemapElementsQuery($metaBundle); |
156
|
|
|
$sitemapPageSize = $metaBundle->metaSitemapVars->sitemapPageSize; |
157
|
|
|
$elementQuery->limit($metaBundle->metaSitemapVars->sitemapLimit ?? null); |
158
|
|
|
|
159
|
|
|
// If this is not a paged sitemap, go through full results |
160
|
|
|
if (empty($sitemapPageSize)) { |
161
|
|
|
$pagedSitemap = false; |
162
|
|
|
$paginator = new Paginator($elementQuery, [ |
163
|
|
|
'pageSize' => self::SITEMAP_QUERY_PAGE_SIZE, |
164
|
|
|
]); |
165
|
|
|
$elements = $paginator->getPageResults(); |
166
|
|
|
} else { |
167
|
|
|
$sitemapPage = empty($page) ? 1 : $page; |
168
|
|
|
$pagedSitemap = true; |
169
|
|
|
$elementQuery->limit($sitemapPageSize); |
170
|
|
|
$elementQuery->offset(($sitemapPage - 1) * $sitemapPageSize); |
171
|
|
|
$elements = $elementQuery->all(); |
172
|
|
|
$totalElements = $sitemapPageSize; |
173
|
|
|
$paginator = new Paginator($elementQuery, [ |
174
|
|
|
'pageSize' => $sitemapPageSize, |
175
|
|
|
]); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$currentElement = 1; |
179
|
|
|
|
180
|
|
|
do { |
181
|
|
|
if (Craft::$app instanceof ConsoleApplication) { |
182
|
|
|
if ($pagedSitemap) { |
183
|
|
|
$message = sprintf('Query %d elements', $sitemapPageSize); |
184
|
|
|
} else { |
185
|
|
|
$message = sprintf('Query %d / %d - elements: %d', |
186
|
|
|
$paginator->getCurrentPage(), |
187
|
|
|
$paginator->getTotalPages(), |
188
|
|
|
$paginator->getTotalResults()); |
189
|
|
|
} |
190
|
|
|
echo $message . PHP_EOL; |
191
|
|
|
} |
192
|
|
|
/** @var Element $element */ |
193
|
|
|
foreach ($elements as $element) { |
194
|
|
|
// Output some info if this is a console app |
195
|
|
|
if (Craft::$app instanceof ConsoleApplication) { |
196
|
|
|
echo "Processing element {$currentElement}/{$totalElements} - {$element->title}" . PHP_EOL; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$metaBundle->metaSitemapVars->setAttributes($stashedSitemapAttrs, false); |
200
|
|
|
$metaBundle->metaGlobalVars->setAttributes($stashedGlobalVarsAttrs, false); |
201
|
|
|
// Combine in any per-entry type settings |
202
|
|
|
self::combineEntryTypeSettings($seoElement, $element, $metaBundle); |
203
|
|
|
// Make sure this entry isn't disabled |
204
|
|
|
self::combineFieldSettings($element, $metaBundle); |
205
|
|
|
// Special case for the __home__ URI |
206
|
|
|
$path = ($element->uri === '__home__') ? '' : $element->uri; |
207
|
|
|
// Check to see if robots is `none` or `no index` |
208
|
|
|
$robotsEnabled = true; |
209
|
|
|
if (!empty($metaBundle->metaGlobalVars->robots)) { |
210
|
|
|
$robotsEnabled = $metaBundle->metaGlobalVars->robots !== 'none' && |
211
|
|
|
$metaBundle->metaGlobalVars->robots !== 'noindex'; |
212
|
|
|
} |
213
|
|
|
$enabled = $element->getEnabledForSite($metaBundle->sourceSiteId); |
214
|
|
|
$enabled = $enabled && $path !== null && $metaBundle->metaSitemapVars->sitemapUrls && $robotsEnabled; |
215
|
|
|
$event = new IncludeSitemapEntryEvent([ |
216
|
|
|
'include' => $enabled, |
217
|
|
|
'element' => $element, |
218
|
|
|
'metaBundle' => $metaBundle, |
219
|
|
|
]); |
220
|
|
|
Event::trigger(self::class, self::EVENT_INCLUDE_SITEMAP_ENTRY, $event); |
221
|
|
|
// Only add in a sitemap entry if it meets our criteria |
222
|
|
|
if ($event->include) { |
223
|
|
|
// Get the url and canonicalUrl |
224
|
|
|
try { |
225
|
|
|
$url = UrlHelper::siteUrl($path, null, null, $metaBundle->sourceSiteId); |
226
|
|
|
} catch (Exception $e) { |
227
|
|
|
$url = ''; |
228
|
|
|
} |
229
|
|
|
$url = UrlHelper::absoluteUrlWithProtocol($url); |
230
|
|
|
if (Seomatic::$settings->excludeNonCanonicalUrls) { |
231
|
|
|
Seomatic::$matchedElement = $element; |
232
|
|
|
MetaValue::cache(); |
233
|
|
|
$path = $metaBundle->metaGlobalVars->parsedValue('canonicalUrl'); |
234
|
|
|
try { |
235
|
|
|
$canonicalUrl = UrlHelper::siteUrl($path, null, null, $metaBundle->sourceSiteId); |
236
|
|
|
} catch (Exception $e) { |
237
|
|
|
$canonicalUrl = ''; |
238
|
|
|
} |
239
|
|
|
$canonicalUrl = UrlHelper::absoluteUrlWithProtocol($canonicalUrl); |
240
|
|
|
if ($url !== $canonicalUrl) { |
241
|
|
|
Craft::info("Excluding URL: {$url} from the sitemap because it does not match the Canonical URL: {$canonicalUrl} - " . $metaBundle->metaGlobalVars->canonicalUrl . " - " . $element->uri); |
|
|
|
|
242
|
|
|
continue; |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
$dateUpdated = $element->dateUpdated ?? $element->dateCreated ?? new DateTime(); |
246
|
|
|
$lines[] = '<url>'; |
247
|
|
|
// Standard sitemap key/values |
248
|
|
|
$lines[] = '<loc>'; |
249
|
|
|
$lines[] = Html::encode($url); |
250
|
|
|
$lines[] = '</loc>'; |
251
|
|
|
$lines[] = '<lastmod>'; |
252
|
|
|
$lines[] = $dateUpdated->format(DateTime::W3C); |
253
|
|
|
$lines[] = '</lastmod>'; |
254
|
|
|
$lines[] = '<changefreq>'; |
255
|
|
|
$lines[] = $metaBundle->metaSitemapVars->sitemapChangeFreq; |
256
|
|
|
$lines[] = '</changefreq>'; |
257
|
|
|
$lines[] = '<priority>'; |
258
|
|
|
$lines[] = $metaBundle->metaSitemapVars->sitemapPriority; |
259
|
|
|
$lines[] = '</priority>'; |
260
|
|
|
// Handle alternate URLs if this is multi-site |
261
|
|
|
if ($multiSite && $metaBundle->metaSitemapVars->sitemapAltLinks) { |
262
|
|
|
$primarySiteId = Craft::$app->getSites()->getPrimarySite()->id; |
263
|
|
|
foreach ($metaBundle->sourceAltSiteSettings as $altSiteSettings) { |
264
|
|
|
if (in_array($altSiteSettings['siteId'], $groupSiteIds, false) && SiteHelper::siteEnabledWithUrls($altSiteSettings['siteId'])) { |
265
|
|
|
$altElement = null; |
266
|
|
|
if ($seoElement !== null) { |
267
|
|
|
/** @var Element $altElement */ |
268
|
|
|
$altElement = $seoElement::sitemapAltElement( |
269
|
|
|
$metaBundle, |
270
|
|
|
$element->id, |
271
|
|
|
$altSiteSettings['siteId'] |
272
|
|
|
); |
273
|
|
|
} |
274
|
|
|
// Make sure to only include the `hreflang` if the element exists, |
275
|
|
|
// and sitemaps are on for it |
276
|
|
|
if (Seomatic::$settings->addHrefLang && $altElement && $altElement->url) { |
277
|
|
|
list($altSourceId, $altSourceBundleType, $altSourceHandle, $altSourceSiteId, $altTypeId) |
278
|
|
|
= Seomatic::$plugin->metaBundles->getMetaSourceFromElement($altElement); |
279
|
|
|
$altMetaBundle = Seomatic::$plugin->metaBundles->getMetaBundleBySourceId( |
280
|
|
|
$altSourceBundleType, |
281
|
|
|
$altSourceId, |
282
|
|
|
$altSourceSiteId |
283
|
|
|
); |
284
|
|
|
if ($altMetaBundle) { |
285
|
|
|
$altEnabled = $altElement->getEnabledForSite($altMetaBundle->sourceSiteId); |
286
|
|
|
// Make sure this entry isn't disabled |
287
|
|
|
self::combineFieldSettings($altElement, $altMetaBundle); |
288
|
|
|
if ($altEnabled && $altMetaBundle->metaSitemapVars->sitemapUrls) { |
289
|
|
|
try { |
290
|
|
|
$altUrl = UrlHelper::siteUrl($altElement->url, null, null, $altSourceId); |
291
|
|
|
} catch (Exception $e) { |
292
|
|
|
$altUrl = $altElement->url; |
293
|
|
|
} |
294
|
|
|
$altUrl = UrlHelper::absoluteUrlWithProtocol($altUrl); |
295
|
|
|
// If this is the primary site, add it as x-default, too |
296
|
|
|
if ($primarySiteId === $altSourceSiteId && Seomatic::$settings->addXDefaultHrefLang) { |
297
|
|
|
$lines[] = '<xhtml:link rel="alternate"' |
298
|
|
|
. ' hreflang="x-default"' |
299
|
|
|
. ' href="' . Html::encode($altUrl) . '"' |
300
|
|
|
. ' />'; |
301
|
|
|
} |
302
|
|
|
$lines[] = '<xhtml:link rel="alternate"' |
303
|
|
|
. ' hreflang="' . $altSiteSettings['language'] . '"' |
304
|
|
|
. ' href="' . Html::encode($altUrl) . '"' |
305
|
|
|
. ' />'; |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
// Handle any Assets |
313
|
|
|
if ($metaBundle->metaSitemapVars->sitemapAssets) { |
314
|
|
|
// Regular Assets fields |
315
|
|
|
$assetFields = FieldHelper::fieldsOfTypeFromElement( |
316
|
|
|
$element, |
317
|
|
|
FieldHelper::ASSET_FIELD_CLASS_KEY, |
318
|
|
|
true |
319
|
|
|
); |
320
|
|
|
foreach ($assetFields as $assetField) { |
321
|
|
|
$assets = $element[$assetField]->all(); |
322
|
|
|
/** @var Asset[] $assets */ |
323
|
|
|
foreach ($assets as $asset) { |
324
|
|
|
self::assetSitemapItem($asset, $metaBundle, $lines); |
325
|
|
|
} |
326
|
|
|
} |
327
|
|
|
// Assets embeded in Block fields |
328
|
|
|
$blockFields = FieldHelper::fieldsOfTypeFromElement( |
329
|
|
|
$element, |
330
|
|
|
FieldHelper::BLOCK_FIELD_CLASS_KEY, |
331
|
|
|
true |
332
|
|
|
); |
333
|
|
|
foreach ($blockFields as $blockField) { |
334
|
|
|
$blocks = $element[$blockField]->all(); |
335
|
|
|
/** @var Entry[]|NeoBlock[]|object[] $blocks */ |
336
|
|
|
foreach ($blocks as $block) { |
337
|
|
|
$assetFields = []; |
338
|
|
|
if ($block instanceof Entry) { |
339
|
|
|
$assetFields = FieldHelper::matrixFieldsOfType($block, AssetsField::class); |
340
|
|
|
} |
341
|
|
|
if ($block instanceof NeoBlock) { |
342
|
|
|
$assetFields = FieldHelper::neoFieldsOfType($block, AssetsField::class); |
343
|
|
|
} |
344
|
|
|
foreach ($assetFields as $assetField) { |
345
|
|
|
foreach ($block[$assetField]->all() as $asset) { |
346
|
|
|
self::assetSitemapItem($asset, $metaBundle, $lines); |
347
|
|
|
} |
348
|
|
|
} |
349
|
|
|
} |
350
|
|
|
} |
351
|
|
|
} |
352
|
|
|
$lines[] = '</url>'; |
353
|
|
|
} |
354
|
|
|
// Include links to any known file types in the assets fields |
355
|
|
|
if ($metaBundle->metaSitemapVars->sitemapFiles) { |
356
|
|
|
// Regular Assets fields |
357
|
|
|
$assetFields = FieldHelper::fieldsOfTypeFromElement( |
358
|
|
|
$element, |
359
|
|
|
FieldHelper::ASSET_FIELD_CLASS_KEY, |
360
|
|
|
true |
361
|
|
|
); |
362
|
|
|
foreach ($assetFields as $assetField) { |
363
|
|
|
$assets = $element[$assetField]->all(); |
364
|
|
|
foreach ($assets as $asset) { |
365
|
|
|
self::assetFilesSitemapLink($asset, $metaBundle, $lines); |
366
|
|
|
} |
367
|
|
|
} |
368
|
|
|
// Assets embeded in Block fields |
369
|
|
|
$blockFields = FieldHelper::fieldsOfTypeFromElement( |
370
|
|
|
$element, |
371
|
|
|
FieldHelper::BLOCK_FIELD_CLASS_KEY, |
372
|
|
|
true |
373
|
|
|
); |
374
|
|
|
foreach ($blockFields as $blockField) { |
375
|
|
|
$blocks = $element[$blockField]->all(); |
376
|
|
|
/** @var Entry[]|NeoBlock[]|object[] $blocks */ |
377
|
|
|
foreach ($blocks as $block) { |
378
|
|
|
$assetFields = []; |
379
|
|
|
if ($block instanceof Entry) { |
380
|
|
|
$assetFields = FieldHelper::matrixFieldsOfType($block, AssetsField::class); |
381
|
|
|
} |
382
|
|
|
if ($block instanceof NeoBlock) { |
383
|
|
|
$assetFields = FieldHelper::neoFieldsOfType($block, AssetsField::class); |
384
|
|
|
} |
385
|
|
|
foreach ($assetFields as $assetField) { |
386
|
|
|
foreach ($block[$assetField]->all() as $asset) { |
387
|
|
|
self::assetFilesSitemapLink($asset, $metaBundle, $lines); |
388
|
|
|
} |
389
|
|
|
} |
390
|
|
|
} |
391
|
|
|
} |
392
|
|
|
} |
393
|
|
|
$currentElement++; |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
if ($pagedSitemap) { |
397
|
|
|
break; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
if ($paginator->getCurrentPage() == $paginator->getTotalPages()) { |
401
|
|
|
break; |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
$paginator->currentPage++; |
405
|
|
|
$elements = $paginator->getPageResults(); |
406
|
|
|
} while (!empty($elements)); |
407
|
|
|
|
408
|
|
|
// Sitemap closing tag |
409
|
|
|
$lines[] = '</urlset>'; |
410
|
|
|
|
411
|
|
|
return implode('', $lines); |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* Return the total number of elements in a sitemap, respecting metabundle settings. |
416
|
|
|
* |
417
|
|
|
* @param class-string<SeoElementInterface> $seoElement |
|
|
|
|
418
|
|
|
* @param MetaBundle $metaBundle |
419
|
|
|
* @return int|null |
420
|
|
|
*/ |
421
|
|
|
public static function getTotalElementsInSitemap(string $seoElement, MetaBundle $metaBundle): ?int |
422
|
|
|
{ |
423
|
|
|
$totalElements = $seoElement::sitemapElementsQuery($metaBundle)->count(); |
424
|
|
|
|
425
|
|
|
if ($metaBundle->metaSitemapVars->sitemapLimit && ($totalElements > $metaBundle->metaSitemapVars->sitemapLimit)) { |
|
|
|
|
426
|
|
|
$totalElements = $metaBundle->metaSitemapVars->sitemapLimit; |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
return $totalElements; |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
/** |
433
|
|
|
* Combine any per-entry type field settings from $element with the passed in |
434
|
|
|
* $metaBundle |
435
|
|
|
* |
436
|
|
|
* @param SeoElementInterface|string $seoElement |
437
|
|
|
* @param Element $element |
438
|
|
|
* @param MetaBundle $metaBundle |
439
|
|
|
*/ |
440
|
|
|
protected static function combineEntryTypeSettings($seoElement, Element $element, MetaBundle $metaBundle) |
441
|
|
|
{ |
442
|
|
|
if (!empty($seoElement::typeMenuFromHandle($metaBundle->sourceHandle))) { |
443
|
|
|
list($sourceId, $sourceBundleType, $sourceHandle, $sourceSiteId, $typeId) |
444
|
|
|
= Seomatic::$plugin->metaBundles->getMetaSourceFromElement($element); |
445
|
|
|
$entryTypeBundle = Seomatic::$plugin->metaBundles->getMetaBundleBySourceId( |
446
|
|
|
$sourceBundleType, |
447
|
|
|
$sourceId, |
448
|
|
|
$sourceSiteId, |
449
|
|
|
$typeId |
450
|
|
|
); |
451
|
|
|
// Combine in any settings for this entry type |
452
|
|
|
if ($entryTypeBundle) { |
453
|
|
|
// Combine the meta sitemap vars |
454
|
|
|
$attributes = $entryTypeBundle->metaSitemapVars->getAttributes(); |
|
|
|
|
455
|
|
|
$attributes = array_filter( |
456
|
|
|
$attributes, |
457
|
|
|
[ArrayHelper::class, 'preserveBools'] |
458
|
|
|
); |
459
|
|
|
$metaBundle->metaSitemapVars->setAttributes($attributes, false); |
460
|
|
|
|
461
|
|
|
// Combine the meta global vars |
462
|
|
|
$attributes = $entryTypeBundle->metaGlobalVars->getAttributes(); |
|
|
|
|
463
|
|
|
$attributes = array_filter( |
464
|
|
|
$attributes, |
465
|
|
|
[ArrayHelper::class, 'preserveBools'] |
466
|
|
|
); |
467
|
|
|
$metaBundle->metaGlobalVars->setAttributes($attributes, false); |
468
|
|
|
} |
469
|
|
|
} |
470
|
|
|
} |
471
|
|
|
|
472
|
|
|
/** |
473
|
|
|
* Combine any SEO Settings field settings from $element with the passed in |
474
|
|
|
* $metaBundle |
475
|
|
|
* |
476
|
|
|
* @param Element $element |
477
|
|
|
* @param MetaBundle $metaBundle |
478
|
|
|
*/ |
479
|
|
|
protected static function combineFieldSettings(Element $element, MetaBundle $metaBundle) |
480
|
|
|
{ |
481
|
|
|
$fieldHandles = FieldHelper::fieldsOfTypeFromElement( |
482
|
|
|
$element, |
483
|
|
|
FieldHelper::SEO_SETTINGS_CLASS_KEY, |
484
|
|
|
true |
485
|
|
|
); |
486
|
|
|
foreach ($fieldHandles as $fieldHandle) { |
487
|
|
|
if (!empty($element->$fieldHandle)) { |
488
|
|
|
/** @var SeoSettings $seoSettingsField */ |
489
|
|
|
$seoSettingsField = Craft::$app->getFields()->getFieldByHandle($fieldHandle); |
490
|
|
|
/** @var MetaBundle $metaBundle */ |
491
|
|
|
$fieldMetaBundle = $element->$fieldHandle; |
492
|
|
|
if ($seoSettingsField !== null) { |
493
|
|
|
if ($seoSettingsField->sitemapTabEnabled) { |
494
|
|
|
Seomatic::$plugin->metaBundles->pruneFieldMetaBundleSettings($fieldMetaBundle, $fieldHandle); |
495
|
|
|
// Combine the meta sitemap vars |
496
|
|
|
$attributes = $fieldMetaBundle->metaSitemapVars->getAttributes(); |
497
|
|
|
|
498
|
|
|
// Get the explicitly inherited attributes |
499
|
|
|
$inherited = array_keys(ArrayHelper::remove($attributes, 'inherited', [])); |
|
|
|
|
500
|
|
|
|
501
|
|
|
$attributes = array_intersect_key( |
502
|
|
|
$attributes, |
503
|
|
|
array_flip((array)$seoSettingsField->sitemapEnabledFields) |
504
|
|
|
); |
505
|
|
|
$attributes = array_filter( |
506
|
|
|
$attributes, |
507
|
|
|
[ArrayHelper::class, 'preserveBools'] |
508
|
|
|
); |
509
|
|
|
|
510
|
|
|
foreach ($inherited as $inheritedAttribute) { |
511
|
|
|
unset($attributes[$inheritedAttribute]); |
512
|
|
|
} |
513
|
|
|
|
514
|
|
|
$metaBundle->metaSitemapVars->setAttributes($attributes, false); |
515
|
|
|
} |
516
|
|
|
// Combine the meta global vars |
517
|
|
|
$attributes = $fieldMetaBundle->metaGlobalVars->getAttributes(); |
518
|
|
|
$attributes = array_filter( |
519
|
|
|
$attributes, |
520
|
|
|
[ArrayHelper::class, 'preserveBools'] |
521
|
|
|
); |
522
|
|
|
$metaBundle->metaGlobalVars->setAttributes($attributes, false); |
523
|
|
|
} |
524
|
|
|
} |
525
|
|
|
} |
526
|
|
|
} |
527
|
|
|
|
528
|
|
|
/** |
529
|
|
|
* @param Asset $asset |
530
|
|
|
* @param MetaBundle $metaBundle |
531
|
|
|
* @param array $lines |
532
|
|
|
*/ |
533
|
|
|
protected static function assetSitemapItem(Asset $asset, MetaBundle $metaBundle, array &$lines) |
534
|
|
|
{ |
535
|
|
|
if ((bool)$asset->enabledForSite && $asset->getUrl() !== null) { |
536
|
|
|
switch ($asset->kind) { |
537
|
|
|
case 'image': |
538
|
|
|
$transform = Craft::$app->getImageTransforms()->getTransformByHandle($metaBundle->metaSitemapVars->sitemapAssetTransform ?? ''); |
539
|
|
|
$lines[] = '<image:image>'; |
540
|
|
|
$lines[] = '<image:loc>'; |
541
|
|
|
$lines[] = Html::encode(UrlHelper::absoluteUrlWithProtocol($asset->getUrl($transform, true))); |
542
|
|
|
$lines[] = '</image:loc>'; |
543
|
|
|
// Handle the dynamic field => property mappings |
544
|
|
|
foreach ($metaBundle->metaSitemapVars->sitemapImageFieldMap as $row) { |
545
|
|
|
$fieldName = $row['field'] ?? ''; |
546
|
|
|
$propName = $row['property'] ?? ''; |
547
|
|
|
if (!empty($fieldName) && !empty($asset[$fieldName]) && !empty($propName)) { |
548
|
|
|
$lines[] = '<image:' . $propName . '>'; |
549
|
|
|
$lines[] = Html::encode($asset[$fieldName]); |
550
|
|
|
$lines[] = '</image:' . $propName . '>'; |
551
|
|
|
} |
552
|
|
|
} |
553
|
|
|
$lines[] = '</image:image>'; |
554
|
|
|
break; |
555
|
|
|
|
556
|
|
|
case 'video': |
557
|
|
|
$lines[] = '<video:video>'; |
558
|
|
|
$lines[] = '<video:content_loc>'; |
559
|
|
|
$lines[] = Html::encode(UrlHelper::absoluteUrlWithProtocol($asset->getUrl())); |
560
|
|
|
$lines[] = '</video:content_loc>'; |
561
|
|
|
// Handle the dynamic field => property mappings |
562
|
|
|
foreach ($metaBundle->metaSitemapVars->sitemapVideoFieldMap as $row) { |
563
|
|
|
$fieldName = $row['field'] ?? ''; |
564
|
|
|
$propName = $row['property'] ?? ''; |
565
|
|
|
if (!empty($fieldName) && !empty($asset[$fieldName]) && !empty($propName)) { |
566
|
|
|
$lines[] = '<video:' . $propName . '>'; |
567
|
|
|
$lines[] = Html::encode($asset[$fieldName]); |
568
|
|
|
$lines[] = '</video:' . $propName . '>'; |
569
|
|
|
} |
570
|
|
|
} |
571
|
|
|
$lines[] = '</video:video>'; |
572
|
|
|
break; |
573
|
|
|
} |
574
|
|
|
} |
575
|
|
|
} |
576
|
|
|
|
577
|
|
|
/** |
578
|
|
|
* @param Asset $asset |
579
|
|
|
* @param MetaBundle $metaBundle |
580
|
|
|
* @param array $lines |
581
|
|
|
*/ |
582
|
|
|
protected static function assetFilesSitemapLink(Asset $asset, MetaBundle $metaBundle, array &$lines) |
583
|
|
|
{ |
584
|
|
|
if ((bool)$asset->enabledForSite && $asset->getUrl() !== null) { |
585
|
|
|
if (in_array($asset->kind, SitemapTemplate::FILE_TYPES, false)) { |
586
|
|
|
$dateUpdated = $asset->dateUpdated ?? $asset->dateCreated ?? new DateTime(); |
587
|
|
|
$lines[] = '<url>'; |
588
|
|
|
$lines[] = '<loc>'; |
589
|
|
|
$lines[] = Html::encode(UrlHelper::absoluteUrlWithProtocol($asset->getUrl())); |
590
|
|
|
$lines[] = '</loc>'; |
591
|
|
|
$lines[] = '<lastmod>'; |
592
|
|
|
$lines[] = $dateUpdated->format(DateTime::W3C); |
593
|
|
|
$lines[] = '</lastmod>'; |
594
|
|
|
$lines[] = '<changefreq>'; |
595
|
|
|
$lines[] = $metaBundle->metaSitemapVars->sitemapChangeFreq; |
596
|
|
|
$lines[] = '</changefreq>'; |
597
|
|
|
$lines[] = '<priority>'; |
598
|
|
|
$lines[] = $metaBundle->metaSitemapVars->sitemapPriority; |
599
|
|
|
$lines[] = '</priority>'; |
600
|
|
|
$lines[] = '</url>'; |
601
|
|
|
} |
602
|
|
|
} |
603
|
|
|
} |
604
|
|
|
} |
605
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: