1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* SEOmatic plugin for Craft CMS 3.x |
4
|
|
|
* |
5
|
|
|
* A turnkey SEO implementation for Craft CMS that is comprehensive, powerful, |
6
|
|
|
* and flexible |
7
|
|
|
* |
8
|
|
|
* @link https://nystudio107.com |
9
|
|
|
* @copyright Copyright (c) 2017 nystudio107 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace nystudio107\seomatic\services; |
13
|
|
|
|
14
|
|
|
use Craft; |
15
|
|
|
use craft\base\Component; |
16
|
|
|
use craft\base\Element; |
17
|
|
|
use craft\commerce\Plugin as CommercePlugin; |
|
|
|
|
18
|
|
|
use craft\console\Application as ConsoleApplication; |
19
|
|
|
use craft\elements\GlobalSet; |
20
|
|
|
use nystudio107\seomatic\base\MetaContainer; |
21
|
|
|
use nystudio107\seomatic\base\MetaItem; |
22
|
|
|
use nystudio107\seomatic\events\InvalidateContainerCachesEvent; |
23
|
|
|
use nystudio107\seomatic\events\MetaBundleDebugDataEvent; |
24
|
|
|
use nystudio107\seomatic\helpers\ArrayHelper; |
25
|
|
|
use nystudio107\seomatic\helpers\DynamicMeta as DynamicMetaHelper; |
26
|
|
|
use nystudio107\seomatic\helpers\Field as FieldHelper; |
27
|
|
|
use nystudio107\seomatic\helpers\Json; |
28
|
|
|
use nystudio107\seomatic\helpers\Localization as LocalizationHelper; |
29
|
|
|
use nystudio107\seomatic\helpers\MetaValue as MetaValueHelper; |
30
|
|
|
use nystudio107\seomatic\helpers\UrlHelper; |
31
|
|
|
use nystudio107\seomatic\models\FrontendTemplateContainer; |
32
|
|
|
use nystudio107\seomatic\models\MetaBundle; |
33
|
|
|
use nystudio107\seomatic\models\MetaGlobalVars; |
34
|
|
|
use nystudio107\seomatic\models\MetaJsonLd; |
35
|
|
|
use nystudio107\seomatic\models\MetaJsonLdContainer; |
36
|
|
|
use nystudio107\seomatic\models\MetaLinkContainer; |
37
|
|
|
use nystudio107\seomatic\models\MetaScript; |
38
|
|
|
use nystudio107\seomatic\models\MetaScriptContainer; |
39
|
|
|
use nystudio107\seomatic\models\MetaSitemapVars; |
40
|
|
|
use nystudio107\seomatic\models\MetaSiteVars; |
41
|
|
|
use nystudio107\seomatic\models\MetaTagContainer; |
42
|
|
|
use nystudio107\seomatic\models\MetaTitleContainer; |
43
|
|
|
use nystudio107\seomatic\seoelements\SeoProduct; |
44
|
|
|
use nystudio107\seomatic\Seomatic; |
45
|
|
|
use nystudio107\seomatic\services\JsonLd as JsonLdService; |
46
|
|
|
use nystudio107\seomatic\variables\SeomaticVariable; |
47
|
|
|
use yii\base\Exception; |
48
|
|
|
use yii\base\InvalidConfigException; |
49
|
|
|
use yii\caching\TagDependency; |
50
|
|
|
use function is_array; |
51
|
|
|
use function is_object; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Meta container functions for SEOmatic |
55
|
|
|
* An instance of the service is available via [[`Seomatic::$plugin->metaContainers`|`seomatic.containers`]] |
56
|
|
|
* |
57
|
|
|
* @author nystudio107 |
58
|
|
|
* @package Seomatic |
59
|
|
|
* @since 3.0.0 |
60
|
|
|
*/ |
61
|
|
|
class MetaContainers extends Component |
62
|
|
|
{ |
63
|
|
|
// Constants |
64
|
|
|
// ========================================================================= |
65
|
|
|
|
66
|
|
|
const GLOBAL_METACONTAINER_CACHE_TAG = 'seomatic_metacontainer'; |
67
|
|
|
const METACONTAINER_CACHE_TAG = 'seomatic_metacontainer_'; |
68
|
|
|
|
69
|
|
|
const CACHE_KEY = 'seomatic_metacontainer_'; |
70
|
|
|
const INVALID_RESPONSE_CACHE_KEY = 'seomatic_invalid_response'; |
71
|
|
|
const GLOBALS_CACHE_KEY = 'parsed_globals_'; |
72
|
|
|
const SCRIPTS_CACHE_KEY = 'body_scripts_'; |
73
|
|
|
|
74
|
|
|
/** @var array Rules for replacement values on arbitrary empty values */ |
75
|
|
|
const COMPOSITE_SETTING_LOOKUP = [ |
76
|
|
|
'ogImage' => [ |
77
|
|
|
'metaBundleSettings.ogImageSource' => 'sameAsSeo.seoImage', |
78
|
|
|
], |
79
|
|
|
'twitterImage' => [ |
80
|
|
|
'metaBundleSettings.twitterImageSource' => 'sameAsSeo.seoImage', |
81
|
|
|
], |
82
|
|
|
]; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @event InvalidateContainerCachesEvent The event that is triggered when SEOmatic |
86
|
|
|
* is about to clear its meta container caches |
87
|
|
|
* |
88
|
|
|
* --- |
89
|
|
|
* ```php |
90
|
|
|
* use nystudio107\seomatic\events\InvalidateContainerCachesEvent; |
91
|
|
|
* use nystudio107\seomatic\services\MetaContainers; |
92
|
|
|
* use yii\base\Event; |
93
|
|
|
* Event::on(MetaContainers::class, MetaContainers::EVENT_INVALIDATE_CONTAINER_CACHES, function(InvalidateContainerCachesEvent $e) { |
94
|
|
|
* // Container caches are about to be cleared |
95
|
|
|
* }); |
96
|
|
|
* ``` |
97
|
|
|
*/ |
98
|
|
|
const EVENT_INVALIDATE_CONTAINER_CACHES = 'invalidateContainerCaches'; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @event MetaBundleDebugDataEvent The event that is triggered to record MetaBundle |
102
|
|
|
* debug data |
103
|
|
|
* |
104
|
|
|
* --- |
105
|
|
|
* ```php |
106
|
|
|
* use nystudio107\seomatic\events\MetaBundleDebugDataEvent; |
107
|
|
|
* use nystudio107\seomatic\services\MetaContainers; |
108
|
|
|
* use yii\base\Event; |
109
|
|
|
* Event::on(MetaContainers::class, MetaContainers::EVENT_METABUNDLE_DEBUG_DATA, function(MetaBundleDebugDataEvent $e) { |
110
|
|
|
* // Do something with the MetaBundle debug data |
111
|
|
|
* }); |
112
|
|
|
* ``` |
113
|
|
|
*/ |
114
|
|
|
const EVENT_METABUNDLE_DEBUG_DATA = 'metaBundleDebugData'; |
115
|
|
|
|
116
|
|
|
// Public Properties |
117
|
|
|
// ========================================================================= |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @var MetaGlobalVars |
121
|
|
|
*/ |
122
|
|
|
public $metaGlobalVars; |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @var MetaSiteVars |
126
|
|
|
*/ |
127
|
|
|
public $metaSiteVars; |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @var MetaSitemapVars |
131
|
|
|
*/ |
132
|
|
|
public $metaSitemapVars; |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @var string The current page number of paginated pages |
136
|
|
|
*/ |
137
|
|
|
public $paginationPage = '1'; |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @var null|string Cached nonce to be shared by all JSON-LD entities |
141
|
|
|
*/ |
142
|
|
|
public $cachedJsonLdNonce; |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @var MetaContainer |
146
|
|
|
*/ |
147
|
|
|
public $metaContainers = []; |
148
|
|
|
|
149
|
|
|
// Protected Properties |
150
|
|
|
// ========================================================================= |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @var null|MetaBundle |
154
|
|
|
*/ |
155
|
|
|
protected $matchedMetaBundle; |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @var null|TagDependency |
159
|
|
|
*/ |
160
|
|
|
protected $containerDependency; |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @var bool Whether or not the matched element should be included in the |
164
|
|
|
* meta containers |
165
|
|
|
*/ |
166
|
|
|
protected $includeMatchedElement = true; |
167
|
|
|
|
168
|
|
|
// Public Methods |
169
|
|
|
// ========================================================================= |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @inheritdoc |
173
|
|
|
*/ |
174
|
1 |
|
public function init(): void |
175
|
|
|
{ |
176
|
1 |
|
parent::init(); |
177
|
|
|
// Get the page number of this request |
178
|
1 |
|
$request = Craft::$app->getRequest(); |
179
|
1 |
|
if (!$request->isConsoleRequest) { |
180
|
|
|
$this->paginationPage = (string)$request->pageNum; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Return the containers of a specific type |
186
|
|
|
* |
187
|
|
|
* @param string $type |
188
|
|
|
* |
189
|
|
|
* @return array |
190
|
|
|
*/ |
191
|
|
|
public function getContainersOfType(string $type): array |
192
|
|
|
{ |
193
|
|
|
$containers = []; |
194
|
|
|
/** @var $metaContainer MetaContainer */ |
195
|
|
|
foreach ($this->metaContainers as $metaContainer) { |
196
|
|
|
if ($metaContainer::CONTAINER_TYPE === $type) { |
197
|
|
|
$containers[] = $metaContainer; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return $containers; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Include the meta containers |
206
|
|
|
*/ |
207
|
|
|
public function includeMetaContainers() |
208
|
|
|
{ |
209
|
|
|
Craft::beginProfile('MetaContainers::includeMetaContainers', __METHOD__); |
210
|
|
|
// If this page is paginated, we need to factor that into the cache key |
211
|
|
|
// We also need to re-add the hreflangs |
212
|
|
|
if ($this->paginationPage !== '1') { |
213
|
|
|
if (Seomatic::$settings->addHrefLang && Seomatic::$settings->addPaginatedHreflang) { |
214
|
|
|
DynamicMetaHelper::addMetaLinkHrefLang(); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
// Fire an 'metaBundleDebugData' event |
218
|
|
|
if ($this->hasEventHandlers(self::EVENT_METABUNDLE_DEBUG_DATA)) { |
219
|
|
|
$metaBundle = new MetaBundle([ |
220
|
|
|
'metaGlobalVars' => clone $this->metaGlobalVars, |
221
|
|
|
'metaSiteVars' => clone $this->metaSiteVars, |
222
|
|
|
'metaSitemapVars' => clone $this->metaSitemapVars, |
223
|
|
|
'metaContainers' => $this->metaContainers, |
224
|
|
|
]); |
225
|
|
|
$event = new MetaBundleDebugDataEvent([ |
226
|
|
|
'metaBundleCategory' => MetaBundleDebugDataEvent::COMBINED_META_BUNDLE, |
227
|
|
|
'metaBundle' => $metaBundle, |
228
|
|
|
]); |
229
|
|
|
$this->trigger(self::EVENT_METABUNDLE_DEBUG_DATA, $event); |
230
|
|
|
} |
231
|
|
|
// Add in our http headers |
232
|
|
|
DynamicMetaHelper::includeHttpHeaders(); |
233
|
|
|
DynamicMetaHelper::addCspTags(); |
234
|
|
|
$this->parseGlobalVars(); |
235
|
|
|
foreach ($this->metaContainers as $metaContainer) { |
236
|
|
|
/** @var $metaContainer MetaContainer */ |
237
|
|
|
if ($metaContainer->include) { |
238
|
|
|
// Don't cache the rendered result if we're previewing meta containers |
239
|
|
|
if (Seomatic::$previewingMetaContainers) { |
240
|
|
|
$metaContainer->clearCache = true; |
241
|
|
|
} |
242
|
|
|
$metaContainer->includeMetaData($this->containerDependency); |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
Craft::endProfile('MetaContainers::includeMetaContainers', __METHOD__); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Parse the global variables |
250
|
|
|
*/ |
251
|
|
|
public function parseGlobalVars() |
252
|
|
|
{ |
253
|
|
|
$dependency = $this->containerDependency; |
254
|
|
|
$uniqueKey = $dependency->tags[3] ?? self::GLOBALS_CACHE_KEY; |
255
|
|
|
list($this->metaGlobalVars, $this->metaSiteVars) = Craft::$app->getCache()->getOrSet( |
256
|
|
|
self::GLOBALS_CACHE_KEY . $uniqueKey, |
257
|
|
|
function () use ($uniqueKey) { |
258
|
|
|
Craft::info( |
259
|
|
|
self::GLOBALS_CACHE_KEY . ' cache miss: ' . $uniqueKey, |
260
|
|
|
__METHOD__ |
261
|
|
|
); |
262
|
|
|
|
263
|
|
|
if ($this->metaGlobalVars) { |
264
|
|
|
$this->metaGlobalVars->parseProperties(); |
265
|
|
|
} |
266
|
|
|
if ($this->metaSiteVars) { |
267
|
|
|
$this->metaSiteVars->parseProperties(); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
return [$this->metaGlobalVars, $this->metaSiteVars]; |
271
|
|
|
}, |
272
|
|
|
Seomatic::$cacheDuration, |
273
|
|
|
$dependency |
274
|
|
|
); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Prep all of the meta for preview purposes |
279
|
|
|
* |
280
|
|
|
* @param string $uri |
281
|
|
|
* @param int|null $siteId |
282
|
|
|
* @param bool $parseVariables Whether or not the variables should be |
283
|
|
|
* parsed as Twig |
284
|
|
|
* @param bool $includeElement Whether or not the matched element |
285
|
|
|
* should be factored into the preview |
286
|
|
|
*/ |
287
|
|
|
public function previewMetaContainers( |
288
|
|
|
string $uri = '', |
289
|
|
|
int $siteId = null, |
290
|
|
|
bool $parseVariables = false, |
291
|
|
|
bool $includeElement = true |
292
|
|
|
) |
293
|
|
|
{ |
294
|
|
|
// If we've already previewed the containers for this request, there's no need to do it again |
295
|
|
|
if (Seomatic::$previewingMetaContainers && !Seomatic::$headlessRequest) { |
296
|
|
|
return; |
297
|
|
|
} |
298
|
|
|
// It's possible this won't exist at this point |
299
|
|
|
if (!Seomatic::$seomaticVariable) { |
300
|
|
|
// Create our variable and stash it in the plugin for global access |
301
|
|
|
Seomatic::$seomaticVariable = new SeomaticVariable(); |
302
|
|
|
} |
303
|
|
|
Seomatic::$previewingMetaContainers = true; |
304
|
|
|
$this->includeMatchedElement = $includeElement; |
305
|
|
|
$this->loadMetaContainers($uri, $siteId); |
306
|
|
|
// Load in the right globals |
307
|
|
|
$twig = Craft::$app->getView()->getTwig(); |
|
|
|
|
308
|
|
|
$globalSets = GlobalSet::findAll([ |
309
|
|
|
'siteId' => $siteId, |
310
|
|
|
]); |
311
|
|
|
foreach ($globalSets as $globalSet) { |
312
|
|
|
MetaValueHelper::$templatePreviewVars[$globalSet->handle] = $globalSet; |
313
|
|
|
} |
314
|
|
|
// Parse the global vars |
315
|
|
|
if ($parseVariables) { |
316
|
|
|
$this->parseGlobalVars(); |
317
|
|
|
} |
318
|
|
|
// Get the homeUrl and canonicalUrl |
319
|
|
|
$homeUrl = '/'; |
320
|
|
|
$canonicalUrl = $this->metaGlobalVars->parsedValue('canonicalUrl'); |
321
|
|
|
$canonicalUrl = DynamicMetaHelper::sanitizeUrl($canonicalUrl, false); |
322
|
|
|
// Special-case the global bundle |
323
|
|
|
if ($uri === MetaBundles::GLOBAL_META_BUNDLE || $uri === '__home__') { |
324
|
|
|
$canonicalUrl = '/'; |
325
|
|
|
} |
326
|
|
|
try { |
327
|
|
|
$homeUrl = UrlHelper::siteUrl($homeUrl, null, null, $siteId); |
328
|
|
|
$canonicalUrl = UrlHelper::siteUrl($canonicalUrl, null, null, $siteId); |
329
|
|
|
} catch (Exception $e) { |
330
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
331
|
|
|
} |
332
|
|
|
$canonical = Seomatic::$seomaticVariable->link->get('canonical'); |
333
|
|
|
if ($canonical !== null) { |
334
|
|
|
$canonical->href = $canonicalUrl; |
335
|
|
|
} |
336
|
|
|
$home = Seomatic::$seomaticVariable->link->get('home'); |
337
|
|
|
if ($home !== null) { |
338
|
|
|
$home->href = $homeUrl; |
339
|
|
|
} |
340
|
|
|
// The current language may _not_ match the current site, if we're headless |
341
|
|
|
$ogLocale = Seomatic::$seomaticVariable->tag->get('og:locale'); |
342
|
|
|
if ($ogLocale !== null && $siteId !== null) { |
343
|
|
|
$site = Craft::$app->getSites()->getSiteById($siteId); |
344
|
|
|
if ($site !== null) { |
345
|
|
|
$ogLocale->content = LocalizationHelper::normalizeOgLocaleLanguage($site->language); |
346
|
|
|
} |
347
|
|
|
} |
348
|
|
|
// Update seomatic.meta.canonicalUrl when previewing meta containers |
349
|
|
|
$this->metaGlobalVars->canonicalUrl = $canonicalUrl; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Load the meta containers |
354
|
|
|
* |
355
|
|
|
* @param string|null $uri |
356
|
|
|
* @param int|null $siteId |
357
|
|
|
*/ |
358
|
|
|
public function loadMetaContainers(string $uri = '', int $siteId = null) |
359
|
|
|
{ |
360
|
|
|
Craft::beginProfile('MetaContainers::loadMetaContainers', __METHOD__); |
361
|
|
|
// Avoid recursion |
362
|
|
|
if (!Seomatic::$loadingMetaContainers) { |
363
|
|
|
Seomatic::$loadingMetaContainers = true; |
364
|
|
|
$this->setMatchedElement($uri, $siteId); |
365
|
|
|
// Get the cache tag for the matched meta bundle |
366
|
|
|
$metaBundle = $this->getMatchedMetaBundle(); |
367
|
|
|
$metaBundleSourceId = ''; |
368
|
|
|
$metaBundleSourceType = ''; |
369
|
|
|
if ($metaBundle) { |
370
|
|
|
$metaBundleSourceId = $metaBundle->sourceId; |
371
|
|
|
$metaBundleSourceType = $metaBundle->sourceBundleType; |
372
|
|
|
} |
373
|
|
|
// We need an actual $siteId here for the cache key |
374
|
|
|
if ($siteId === null) { |
375
|
|
|
$siteId = Craft::$app->getSites()->currentSite->id |
376
|
|
|
?? Craft::$app->getSites()->primarySite->id |
377
|
|
|
?? 1; |
378
|
|
|
} |
379
|
|
|
// Handle pagination |
380
|
|
|
$paginationPage = 'page' . $this->paginationPage; |
381
|
|
|
// Get the path for the current request |
382
|
|
|
$request = Craft::$app->getRequest(); |
383
|
|
|
$requestPath = '/'; |
384
|
|
|
if (!$request->getIsConsoleRequest()) { |
385
|
|
|
try { |
386
|
|
|
$requestPath = $request->getPathInfo(); |
387
|
|
|
} catch (InvalidConfigException $e) { |
388
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
389
|
|
|
} |
390
|
|
|
// If this is any type of a preview, ensure that it's not cached |
391
|
|
|
if (Seomatic::$plugin->helper::isPreview()) { |
392
|
|
|
Seomatic::$previewingMetaContainers = true; |
393
|
|
|
} |
394
|
|
|
} |
395
|
|
|
// Get our cache key |
396
|
|
|
$cacheKey = $uri . $siteId . $paginationPage . $requestPath . $this->getAllowedUrlParams(); |
397
|
|
|
// For requests with a status code of >= 400, use one cache key |
398
|
|
|
if (!$request->isConsoleRequest) { |
399
|
|
|
$response = Craft::$app->getResponse(); |
400
|
|
|
if ($response->statusCode >= 400) { |
401
|
|
|
$cacheKey = $siteId . self::INVALID_RESPONSE_CACHE_KEY . $response->statusCode; |
402
|
|
|
} |
403
|
|
|
} |
404
|
|
|
// Load the meta containers |
405
|
|
|
$dependency = new TagDependency([ |
406
|
|
|
'tags' => [ |
407
|
|
|
self::GLOBAL_METACONTAINER_CACHE_TAG, |
408
|
|
|
self::METACONTAINER_CACHE_TAG . $metaBundleSourceId . $metaBundleSourceType . $siteId, |
409
|
|
|
self::METACONTAINER_CACHE_TAG . $uri . $siteId, |
410
|
|
|
self::METACONTAINER_CACHE_TAG . $cacheKey, |
411
|
|
|
], |
412
|
|
|
]); |
413
|
|
|
$this->containerDependency = $dependency; |
414
|
|
|
$debugModule = Craft::$app->getModule('debug'); |
415
|
|
|
if (Seomatic::$previewingMetaContainers || $debugModule) { |
416
|
|
|
Seomatic::$plugin->frontendTemplates->loadFrontendTemplateContainers($siteId); |
417
|
|
|
$this->loadGlobalMetaContainers($siteId); |
418
|
|
|
$this->loadContentMetaContainers(); |
419
|
|
|
$this->loadFieldMetaContainers(); |
420
|
|
|
// We only need the dynamic data for headless requests |
421
|
|
|
if (Seomatic::$headlessRequest || Seomatic::$plugin->helper::isPreview() || $debugModule) { |
422
|
|
|
DynamicMetaHelper::addDynamicMetaToContainers($uri, $siteId); |
423
|
|
|
} |
424
|
|
|
} else { |
425
|
|
|
$cache = Craft::$app->getCache(); |
426
|
|
|
list($this->metaGlobalVars, $this->metaSiteVars, $this->metaSitemapVars, $this->metaContainers) = $cache->getOrSet( |
427
|
|
|
self::CACHE_KEY . $cacheKey, |
428
|
|
|
function () use ($uri, $siteId) { |
429
|
|
|
Craft::info( |
430
|
|
|
'Meta container cache miss: ' . $uri . '/' . $siteId, |
431
|
|
|
__METHOD__ |
432
|
|
|
); |
433
|
|
|
$this->loadGlobalMetaContainers($siteId); |
434
|
|
|
$this->loadContentMetaContainers(); |
435
|
|
|
$this->loadFieldMetaContainers(); |
436
|
|
|
DynamicMetaHelper::addDynamicMetaToContainers($uri, $siteId); |
437
|
|
|
|
438
|
|
|
return [$this->metaGlobalVars, $this->metaSiteVars, $this->metaSitemapVars, $this->metaContainers]; |
439
|
|
|
}, |
440
|
|
|
Seomatic::$cacheDuration, |
441
|
|
|
$dependency |
442
|
|
|
); |
443
|
|
|
} |
444
|
|
|
Seomatic::$seomaticVariable->init(); |
|
|
|
|
445
|
|
|
MetaValueHelper::cache(); |
446
|
|
|
Seomatic::$loadingMetaContainers = false; |
447
|
|
|
} |
448
|
|
|
Craft::endProfile('MetaContainers::loadMetaContainers', __METHOD__); |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
/** |
452
|
|
|
* Return the MetaBundle that corresponds with the Seomatic::$matchedElement |
453
|
|
|
* |
454
|
|
|
* @return null|MetaBundle |
455
|
|
|
*/ |
456
|
|
|
public function getMatchedMetaBundle() |
457
|
|
|
{ |
458
|
|
|
$metaBundle = null; |
459
|
|
|
/** @var Element $element */ |
460
|
|
|
$element = Seomatic::$matchedElement; |
461
|
|
|
if ($element) { |
|
|
|
|
462
|
|
|
$sourceType = Seomatic::$plugin->seoElements->getMetaBundleTypeFromElement($element); |
463
|
|
|
if ($sourceType) { |
464
|
|
|
list($sourceId, $sourceBundleType, $sourceHandle, $sourceSiteId, $typeId) |
465
|
|
|
= Seomatic::$plugin->metaBundles->getMetaSourceFromElement($element); |
466
|
|
|
$metaBundle = Seomatic::$plugin->metaBundles->getMetaBundleBySourceId( |
467
|
|
|
$sourceType, |
468
|
|
|
$sourceId, |
469
|
|
|
$sourceSiteId, |
470
|
|
|
$typeId |
471
|
|
|
); |
472
|
|
|
} |
473
|
|
|
} |
474
|
|
|
$this->matchedMetaBundle = $metaBundle; |
475
|
|
|
|
476
|
|
|
return $metaBundle; |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
/** |
480
|
|
|
* Load the global site meta containers |
481
|
|
|
* |
482
|
|
|
* @param int|null $siteId |
483
|
|
|
*/ |
484
|
|
|
public function loadGlobalMetaContainers(int $siteId = null) |
485
|
|
|
{ |
486
|
|
|
Craft::beginProfile('MetaContainers::loadGlobalMetaContainers', __METHOD__); |
487
|
|
|
if ($siteId === null) { |
488
|
|
|
$siteId = Craft::$app->getSites()->currentSite->id ?? 1; |
489
|
|
|
} |
490
|
|
|
$metaBundle = Seomatic::$plugin->metaBundles->getGlobalMetaBundle($siteId); |
491
|
|
|
if ($metaBundle) { |
492
|
|
|
// Fire an 'metaBundleDebugData' event |
493
|
|
|
if ($this->hasEventHandlers(self::EVENT_METABUNDLE_DEBUG_DATA)) { |
494
|
|
|
$event = new MetaBundleDebugDataEvent([ |
495
|
|
|
'metaBundleCategory' => MetaBundleDebugDataEvent::GLOBAL_META_BUNDLE, |
496
|
|
|
'metaBundle' => $metaBundle, |
497
|
|
|
]); |
498
|
|
|
$this->trigger(self::EVENT_METABUNDLE_DEBUG_DATA, $event); |
499
|
|
|
} |
500
|
|
|
// Meta global vars |
501
|
|
|
$this->metaGlobalVars = clone $metaBundle->metaGlobalVars; |
502
|
|
|
// Meta site vars |
503
|
|
|
$this->metaSiteVars = clone $metaBundle->metaSiteVars; |
504
|
|
|
// Meta sitemap vars |
505
|
|
|
$this->metaSitemapVars = clone $metaBundle->metaSitemapVars; |
506
|
|
|
// Language |
507
|
|
|
$this->metaGlobalVars->language = Seomatic::$language; |
508
|
|
|
// Meta containers |
509
|
|
|
foreach ($metaBundle->metaContainers as $key => $metaContainer) { |
510
|
|
|
$this->metaContainers[$key] = clone $metaContainer; |
511
|
|
|
} |
512
|
|
|
} |
513
|
|
|
Craft::endProfile('MetaContainers::loadGlobalMetaContainers', __METHOD__); |
514
|
|
|
} |
515
|
|
|
|
516
|
|
|
/** |
517
|
|
|
* Add the meta bundle to our existing meta containers, overwriting meta |
518
|
|
|
* items with the same key |
519
|
|
|
* |
520
|
|
|
* @param MetaBundle $metaBundle |
521
|
|
|
*/ |
522
|
|
|
public function addMetaBundleToContainers(MetaBundle $metaBundle) |
523
|
|
|
{ |
524
|
|
|
// Ensure the variable is synced properly first |
525
|
|
|
Seomatic::$seomaticVariable->init(); |
526
|
|
|
// Meta global vars |
527
|
|
|
$attributes = $metaBundle->metaGlobalVars->getAttributes(); |
528
|
|
|
// Parse the meta values so we can filter out any blank or empty attributes |
529
|
|
|
// So that they can fall back on the parent container |
530
|
|
|
$parsedAttributes = $attributes; |
531
|
|
|
MetaValueHelper::parseArray($parsedAttributes); |
532
|
|
|
$parsedAttributes = array_filter( |
533
|
|
|
$parsedAttributes, |
534
|
|
|
[ArrayHelper::class, 'preserveBools'] |
535
|
|
|
); |
536
|
|
|
$attributes = array_intersect_key($attributes, $parsedAttributes); |
537
|
|
|
// Add the attributes in |
538
|
|
|
$attributes = array_filter( |
539
|
|
|
$attributes, |
540
|
|
|
[ArrayHelper::class, 'preserveBools'] |
541
|
|
|
); |
542
|
|
|
$this->metaGlobalVars->setAttributes($attributes, false); |
543
|
|
|
// Meta site vars |
544
|
|
|
/* |
545
|
|
|
* Don't merge in the Site vars, since they are only editable on |
546
|
|
|
* a global basis. Otherwise stale data will be unable to be edited |
547
|
|
|
$attributes = $metaBundle->metaSiteVars->getAttributes(); |
548
|
|
|
$attributes = array_filter($attributes); |
549
|
|
|
$this->metaSiteVars->setAttributes($attributes, false); |
550
|
|
|
*/ |
551
|
|
|
// Meta sitemap vars |
552
|
|
|
$attributes = $metaBundle->metaSitemapVars->getAttributes(); |
553
|
|
|
$attributes = array_filter( |
554
|
|
|
$attributes, |
555
|
|
|
[ArrayHelper::class, 'preserveBools'] |
556
|
|
|
); |
557
|
|
|
$this->metaSitemapVars->setAttributes($attributes, false); |
558
|
|
|
// Language |
559
|
|
|
$this->metaGlobalVars->language = Seomatic::$language; |
560
|
|
|
// Meta containers |
561
|
|
|
foreach ($metaBundle->metaContainers as $key => $metaContainer) { |
562
|
|
|
foreach ($metaContainer->data as $metaTag) { |
563
|
|
|
$this->addToMetaContainer($metaTag, $key); |
564
|
|
|
} |
565
|
|
|
} |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
/** |
569
|
|
|
* Add the passed in MetaItem to the MetaContainer indexed as $key |
570
|
|
|
* |
571
|
|
|
* @param $data MetaItem The MetaItem to add to the container |
572
|
|
|
* @param $key string The key to the container to add the data to |
573
|
|
|
*/ |
574
|
1 |
|
public function addToMetaContainer(MetaItem $data, string $key) |
575
|
|
|
{ |
576
|
|
|
/** @var $container MetaContainer */ |
577
|
1 |
|
$container = $this->getMetaContainer($key); |
578
|
|
|
|
579
|
1 |
|
if ($container !== null) { |
580
|
|
|
$container->addData($data, $data->key); |
581
|
|
|
} |
582
|
|
|
} |
583
|
|
|
|
584
|
|
|
/** |
585
|
|
|
* @param string $key |
586
|
|
|
* |
587
|
|
|
* @return mixed|null |
588
|
|
|
*/ |
589
|
1 |
|
public function getMetaContainer(string $key) |
590
|
|
|
{ |
591
|
1 |
|
if (!$key || empty($this->metaContainers[$key])) { |
592
|
1 |
|
$error = Craft::t( |
593
|
1 |
|
'seomatic', |
594
|
1 |
|
'Meta container with key `{key}` does not exist.', |
595
|
1 |
|
['key' => $key] |
596
|
1 |
|
); |
597
|
1 |
|
Craft::error($error, __METHOD__); |
598
|
|
|
|
599
|
1 |
|
return null; |
600
|
|
|
} |
601
|
|
|
|
602
|
|
|
return $this->metaContainers[$key]; |
603
|
|
|
} |
604
|
|
|
|
605
|
|
|
/** |
606
|
|
|
* Create a MetaContainer of the given $type with the $key |
607
|
|
|
* |
608
|
|
|
* @param string $type |
609
|
|
|
* @param string $key |
610
|
|
|
* |
611
|
|
|
* @return null|MetaContainer |
612
|
|
|
*/ |
613
|
|
|
public function createMetaContainer(string $type, string $key): MetaContainer |
614
|
|
|
{ |
615
|
|
|
/** @var MetaContainer $container */ |
616
|
|
|
$container = null; |
617
|
|
|
if (empty($this->metaContainers[$key])) { |
618
|
|
|
/** @var MetaContainer $className */ |
619
|
|
|
$className = null; |
620
|
|
|
// Create a new container based on the type passed in |
621
|
|
|
switch ($type) { |
622
|
|
|
case MetaTagContainer::CONTAINER_TYPE: |
623
|
|
|
$className = MetaTagContainer::class; |
624
|
|
|
break; |
625
|
|
|
case MetaLinkContainer::CONTAINER_TYPE: |
626
|
|
|
$className = MetaLinkContainer::class; |
627
|
|
|
break; |
628
|
|
|
case MetaScriptContainer::CONTAINER_TYPE: |
629
|
|
|
$className = MetaScriptContainer::class; |
630
|
|
|
break; |
631
|
|
|
case MetaJsonLdContainer::CONTAINER_TYPE: |
632
|
|
|
$className = MetaJsonLdContainer::class; |
633
|
|
|
break; |
634
|
|
|
case MetaTitleContainer::CONTAINER_TYPE: |
635
|
|
|
$className = MetaTitleContainer::class; |
636
|
|
|
break; |
637
|
|
|
} |
638
|
|
|
if ($className) { |
639
|
|
|
$container = $className::create(); |
640
|
|
|
if ($container) { |
|
|
|
|
641
|
|
|
$this->metaContainers[$key] = $container; |
642
|
|
|
} |
643
|
|
|
} |
644
|
|
|
} |
645
|
|
|
|
646
|
|
|
/** @var MetaContainer $className */ |
647
|
|
|
return $container; |
648
|
|
|
} |
649
|
|
|
|
650
|
|
|
// Protected Methods |
651
|
|
|
// ========================================================================= |
652
|
|
|
|
653
|
|
|
/** |
654
|
|
|
* Render the HTML of all MetaContainers of a specific $type |
655
|
|
|
* |
656
|
|
|
* @param string $type |
657
|
|
|
* |
658
|
|
|
* @return string |
659
|
|
|
*/ |
660
|
|
|
public function renderContainersByType(string $type): string |
661
|
|
|
{ |
662
|
|
|
$html = ''; |
663
|
|
|
// Special-case for requests for the FrontendTemplateContainer "container" |
664
|
|
|
if ($type === FrontendTemplateContainer::CONTAINER_TYPE) { |
665
|
|
|
$renderedTemplates = []; |
666
|
|
|
if (Seomatic::$plugin->frontendTemplates->frontendTemplateContainer['data'] ?? false) { |
667
|
|
|
$frontendTemplateContainers = Seomatic::$plugin->frontendTemplates->frontendTemplateContainer['data']; |
668
|
|
|
foreach ($frontendTemplateContainers as $name => $frontendTemplateContainer) { |
669
|
|
|
if ($frontendTemplateContainer->include) { |
670
|
|
|
$result = $frontendTemplateContainer->render([ |
671
|
|
|
]); |
672
|
|
|
$renderedTemplates[] = [$name => $result]; |
673
|
|
|
} |
674
|
|
|
} |
675
|
|
|
} |
676
|
|
|
$html .= Json::encode($renderedTemplates); |
677
|
|
|
|
678
|
|
|
return $html; |
679
|
|
|
} |
680
|
|
|
/** @var $metaContainer MetaContainer */ |
681
|
|
|
foreach ($this->metaContainers as $metaContainer) { |
682
|
|
|
if ($metaContainer::CONTAINER_TYPE === $type && $metaContainer->include) { |
683
|
|
|
$result = $metaContainer->render([ |
684
|
|
|
'renderRaw' => true, |
685
|
|
|
'renderScriptTags' => true, |
686
|
|
|
'array' => true, |
687
|
|
|
]); |
688
|
|
|
// Special case for script containers, because they can have body scripts too |
689
|
|
|
if ($metaContainer::CONTAINER_TYPE === MetaScriptContainer::CONTAINER_TYPE) { |
690
|
|
|
$bodyScript = ''; |
691
|
|
|
/** @var MetaScriptContainer $metaContainer */ |
692
|
|
|
if ($metaContainer->prepForInclusion()) { |
693
|
|
|
foreach ($metaContainer->data as $metaScript) { |
694
|
|
|
/** @var MetaScript $metaScript */ |
695
|
|
|
if (!empty($metaScript->bodyTemplatePath)) { |
696
|
|
|
$bodyScript .= $metaScript->renderBodyHtml(); |
697
|
|
|
} |
698
|
|
|
} |
699
|
|
|
} |
700
|
|
|
|
701
|
|
|
$result = Json::encode([ |
702
|
|
|
'script' => $result, |
703
|
|
|
'bodyScript' => $bodyScript, |
704
|
|
|
]); |
705
|
|
|
} |
706
|
|
|
|
707
|
|
|
$html .= $result; |
708
|
|
|
} |
709
|
|
|
} |
710
|
|
|
// Special-case for requests for the MetaSiteVars "container" |
711
|
|
|
if ($type === MetaSiteVars::CONTAINER_TYPE) { |
712
|
|
|
$result = Json::encode($this->metaSiteVars->toArray()); |
713
|
|
|
$html .= $result; |
714
|
|
|
} |
715
|
|
|
|
716
|
|
|
return $html; |
717
|
|
|
} |
718
|
|
|
|
719
|
|
|
/** |
720
|
|
|
* Render the HTML of all MetaContainers of a specific $type as an array |
721
|
|
|
* |
722
|
|
|
* @param string $type |
723
|
|
|
* |
724
|
|
|
* @return array |
725
|
|
|
*/ |
726
|
|
|
public function renderContainersArrayByType(string $type): array |
727
|
|
|
{ |
728
|
|
|
$htmlArray = []; |
729
|
|
|
// Special-case for requests for the FrontendTemplateContainer "container" |
730
|
|
|
if ($type === FrontendTemplateContainer::CONTAINER_TYPE) { |
731
|
|
|
$renderedTemplates = []; |
732
|
|
|
if (Seomatic::$plugin->frontendTemplates->frontendTemplateContainer['data'] ?? false) { |
733
|
|
|
$frontendTemplateContainers = Seomatic::$plugin->frontendTemplates->frontendTemplateContainer['data']; |
734
|
|
|
foreach ($frontendTemplateContainers as $name => $frontendTemplateContainer) { |
735
|
|
|
if ($frontendTemplateContainer->include) { |
736
|
|
|
$result = $frontendTemplateContainer->render([ |
737
|
|
|
]); |
738
|
|
|
$renderedTemplates[] = [$name => $result]; |
739
|
|
|
} |
740
|
|
|
} |
741
|
|
|
} |
742
|
|
|
|
743
|
|
|
return $renderedTemplates; |
744
|
|
|
} |
745
|
|
|
/** @var $metaContainer MetaContainer */ |
746
|
|
|
foreach ($this->metaContainers as $metaContainer) { |
747
|
|
|
if ($metaContainer::CONTAINER_TYPE === $type && $metaContainer->include) { |
748
|
|
|
/** @noinspection SlowArrayOperationsInLoopInspection */ |
749
|
|
|
$htmlArray = array_merge($htmlArray, $metaContainer->renderArray()); |
750
|
|
|
} |
751
|
|
|
} |
752
|
|
|
// Special-case for requests for the MetaSiteVars "container" |
753
|
|
|
if ($type === MetaSiteVars::CONTAINER_TYPE) { |
754
|
|
|
$result = Json::encode($this->metaSiteVars->toArray()); |
|
|
|
|
755
|
|
|
$htmlArray = array_merge($htmlArray, $this->metaSiteVars->toArray()); |
756
|
|
|
} |
757
|
|
|
|
758
|
|
|
return $htmlArray; |
759
|
|
|
} |
760
|
|
|
|
761
|
|
|
/** |
762
|
|
|
* Return a MetaItem object by $key from container $type |
763
|
|
|
* |
764
|
|
|
* @param string $key |
765
|
|
|
* @param string $type |
766
|
|
|
* |
767
|
|
|
* @return null|MetaItem |
768
|
|
|
*/ |
769
|
|
|
public function getMetaItemByKey(string $key, string $type = '') |
770
|
|
|
{ |
771
|
|
|
$metaItem = null; |
|
|
|
|
772
|
|
|
/** @var $metaContainer MetaContainer */ |
773
|
|
|
foreach ($this->metaContainers as $metaContainer) { |
774
|
|
|
if (($metaContainer::CONTAINER_TYPE === $type) || empty($type)) { |
775
|
|
|
/** @var $metaTag MetaItem */ |
776
|
|
|
foreach ($metaContainer->data as $metaItem) { |
777
|
|
|
if ($key === $metaItem->key) { |
778
|
|
|
return $metaItem; |
779
|
|
|
} |
780
|
|
|
} |
781
|
|
|
} |
782
|
|
|
} |
783
|
|
|
|
784
|
|
|
return null; |
785
|
|
|
} |
786
|
|
|
|
787
|
|
|
/** |
788
|
|
|
* Invalidate all of the meta container caches |
789
|
|
|
*/ |
790
|
|
|
public function invalidateCaches() |
791
|
|
|
{ |
792
|
|
|
$cache = Craft::$app->getCache(); |
793
|
|
|
TagDependency::invalidate($cache, self::GLOBAL_METACONTAINER_CACHE_TAG); |
794
|
|
|
Craft::info( |
795
|
|
|
'All meta container caches cleared', |
796
|
|
|
__METHOD__ |
797
|
|
|
); |
798
|
|
|
// Trigger an event to let other plugins/modules know we've cleared our caches |
799
|
|
|
$event = new InvalidateContainerCachesEvent([ |
800
|
|
|
'uri' => null, |
801
|
|
|
'siteId' => null, |
802
|
|
|
'sourceId' => null, |
803
|
|
|
'sourceType' => null, |
804
|
|
|
]); |
805
|
|
|
if (!Craft::$app instanceof ConsoleApplication) { |
806
|
|
|
$this->trigger(self::EVENT_INVALIDATE_CONTAINER_CACHES, $event); |
807
|
|
|
} |
808
|
|
|
} |
809
|
|
|
|
810
|
|
|
/** |
811
|
|
|
* Invalidate a meta bundle cache |
812
|
|
|
* |
813
|
|
|
* @param int $sourceId |
814
|
|
|
* @param null|string $sourceType |
815
|
|
|
* @param null|int $siteId |
816
|
|
|
*/ |
817
|
|
|
public function invalidateContainerCacheById(int $sourceId, $sourceType = null, $siteId = null) |
818
|
|
|
{ |
819
|
|
|
$metaBundleSourceId = ''; |
820
|
|
|
if ($sourceId) { |
821
|
|
|
$metaBundleSourceId = $sourceId; |
822
|
|
|
} |
823
|
|
|
$metaBundleSourceType = ''; |
824
|
|
|
if ($sourceType) { |
825
|
|
|
$metaBundleSourceType = $sourceType; |
826
|
|
|
} |
827
|
|
|
if ($siteId === null) { |
828
|
|
|
$siteId = Craft::$app->getSites()->currentSite->id ?? 1; |
829
|
|
|
} |
830
|
|
|
$cache = Craft::$app->getCache(); |
831
|
|
|
TagDependency::invalidate( |
832
|
|
|
$cache, |
833
|
|
|
self::METACONTAINER_CACHE_TAG . $metaBundleSourceId . $metaBundleSourceType . $siteId |
834
|
|
|
); |
835
|
|
|
Craft::info( |
836
|
|
|
'Meta bundle cache cleared: ' . $metaBundleSourceId . ' / ' . $metaBundleSourceType . ' / ' . $siteId, |
837
|
|
|
__METHOD__ |
838
|
|
|
); |
839
|
|
|
// Trigger an event to let other plugins/modules know we've cleared our caches |
840
|
|
|
$event = new InvalidateContainerCachesEvent([ |
841
|
|
|
'uri' => null, |
842
|
|
|
'siteId' => $siteId, |
843
|
|
|
'sourceId' => $sourceId, |
844
|
|
|
'sourceType' => $metaBundleSourceType, |
845
|
|
|
]); |
846
|
|
|
if (!Craft::$app instanceof ConsoleApplication) { |
847
|
|
|
$this->trigger(self::EVENT_INVALIDATE_CONTAINER_CACHES, $event); |
848
|
|
|
} |
849
|
|
|
} |
850
|
|
|
|
851
|
|
|
/** |
852
|
|
|
* Invalidate a meta bundle cache |
853
|
|
|
* |
854
|
|
|
* @param string $uri |
855
|
|
|
* @param null|int $siteId |
856
|
|
|
*/ |
857
|
|
|
public function invalidateContainerCacheByPath(string $uri, $siteId = null) |
858
|
|
|
{ |
859
|
|
|
$cache = Craft::$app->getCache(); |
860
|
|
|
if ($siteId === null) { |
861
|
|
|
$siteId = Craft::$app->getSites()->currentSite->id ?? 1; |
862
|
|
|
} |
863
|
|
|
TagDependency::invalidate($cache, self::METACONTAINER_CACHE_TAG . $uri . $siteId); |
864
|
|
|
Craft::info( |
865
|
|
|
'Meta container cache cleared: ' . $uri . ' / ' . $siteId, |
866
|
|
|
__METHOD__ |
867
|
|
|
); |
868
|
|
|
// Trigger an event to let other plugins/modules know we've cleared our caches |
869
|
|
|
$event = new InvalidateContainerCachesEvent([ |
870
|
|
|
'uri' => $uri, |
871
|
|
|
'siteId' => $siteId, |
872
|
|
|
'sourceId' => null, |
873
|
|
|
'sourceType' => null, |
874
|
|
|
]); |
875
|
|
|
if (!Craft::$app instanceof ConsoleApplication) { |
876
|
|
|
$this->trigger(self::EVENT_INVALIDATE_CONTAINER_CACHES, $event); |
877
|
|
|
} |
878
|
|
|
} |
879
|
|
|
|
880
|
|
|
// Protected Methods |
881
|
|
|
// ========================================================================= |
882
|
|
|
|
883
|
|
|
/** |
884
|
|
|
* Set the element that matches the $uri |
885
|
|
|
* |
886
|
|
|
* @param string $uri |
887
|
|
|
* @param int|null $siteId |
888
|
|
|
*/ |
889
|
|
|
protected function setMatchedElement(string $uri, int $siteId = null) |
890
|
|
|
{ |
891
|
|
|
if ($siteId === null) { |
892
|
|
|
$siteId = Craft::$app->getSites()->currentSite->id |
893
|
|
|
?? Craft::$app->getSites()->primarySite->id |
894
|
|
|
?? 1; |
895
|
|
|
} |
896
|
|
|
$element = null; |
897
|
|
|
$uri = trim($uri, '/'); |
898
|
|
|
/** @var Element $element */ |
899
|
|
|
$enabledOnly = !Seomatic::$previewingMetaContainers; |
900
|
|
|
// Try to use Craft's matched element if looking for an enabled element, the current `siteId` is being used and |
901
|
|
|
// the current `uri` matches what was in the request |
902
|
|
|
$request = Craft::$app->getRequest(); |
903
|
|
|
if ($enabledOnly && !$request->getIsConsoleRequest()) { |
904
|
|
|
try { |
905
|
|
|
if ($siteId === Craft::$app->getSites()->currentSite->id |
906
|
|
|
&& $request->getPathInfo() === $uri) { |
907
|
|
|
$element = Craft::$app->getUrlManager()->getMatchedElement(); |
908
|
|
|
} |
909
|
|
|
} catch (\Throwable $e) { |
910
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
911
|
|
|
} |
912
|
|
|
} |
913
|
|
|
if (!$element) { |
914
|
|
|
$element = Craft::$app->getElements()->getElementByUri($uri, $siteId, $enabledOnly); |
915
|
|
|
} |
916
|
|
|
if ($element && ($element->uri !== null)) { |
917
|
|
|
Seomatic::setMatchedElement($element); |
918
|
|
|
} |
919
|
|
|
} |
920
|
|
|
|
921
|
|
|
/** |
922
|
|
|
* Return as key/value pairs any allowed parameters in the request |
923
|
|
|
* |
924
|
|
|
* @return string |
925
|
|
|
*/ |
926
|
|
|
protected function getAllowedUrlParams(): string |
927
|
|
|
{ |
928
|
|
|
$result = ''; |
929
|
|
|
$allowedParams = Seomatic::$settings->allowedUrlParams; |
930
|
|
|
if (Craft::$app->getPlugins()->getPlugin(SeoProduct::REQUIRED_PLUGIN_HANDLE)) { |
931
|
|
|
$commerce = CommercePlugin::getInstance(); |
932
|
|
|
if ($commerce !== null) { |
933
|
|
|
$allowedParams[] = 'variant'; |
934
|
|
|
} |
935
|
|
|
} |
936
|
|
|
// Iterate through the allowed parameters, adding the key/value pair to the $result string as found |
937
|
|
|
$request = Craft::$app->getRequest(); |
938
|
|
|
if (!$request->isConsoleRequest) { |
939
|
|
|
foreach ($allowedParams as $allowedParam) { |
940
|
|
|
$value = $request->getParam($allowedParam); |
941
|
|
|
if ($value !== null) { |
942
|
|
|
$result .= "{$allowedParam}={$value}"; |
943
|
|
|
} |
944
|
|
|
} |
945
|
|
|
} |
946
|
|
|
|
947
|
|
|
return $result; |
948
|
|
|
} |
949
|
|
|
|
950
|
|
|
/** |
951
|
|
|
* Load the meta containers specific to the matched meta bundle |
952
|
|
|
*/ |
953
|
|
|
protected function loadContentMetaContainers() |
954
|
|
|
{ |
955
|
|
|
Craft::beginProfile('MetaContainers::loadContentMetaContainers', __METHOD__); |
956
|
|
|
$metaBundle = $this->getMatchedMetaBundle(); |
957
|
|
|
if ($metaBundle) { |
958
|
|
|
// Fire an 'metaBundleDebugData' event |
959
|
|
|
if ($this->hasEventHandlers(self::EVENT_METABUNDLE_DEBUG_DATA)) { |
960
|
|
|
$event = new MetaBundleDebugDataEvent([ |
961
|
|
|
'metaBundleCategory' => MetaBundleDebugDataEvent::CONTENT_META_BUNDLE, |
962
|
|
|
'metaBundle' => $metaBundle, |
963
|
|
|
]); |
964
|
|
|
$this->trigger(self::EVENT_METABUNDLE_DEBUG_DATA, $event); |
965
|
|
|
} |
966
|
|
|
$this->addMetaBundleToContainers($metaBundle); |
967
|
|
|
} |
968
|
|
|
Craft::endProfile('MetaContainers::loadContentMetaContainers', __METHOD__); |
969
|
|
|
} |
970
|
|
|
|
971
|
|
|
/** |
972
|
|
|
* Load any meta containers in the current element |
973
|
|
|
*/ |
974
|
|
|
protected function loadFieldMetaContainers() |
975
|
|
|
{ |
976
|
|
|
Craft::beginProfile('MetaContainers::loadFieldMetaContainers', __METHOD__); |
977
|
|
|
$element = Seomatic::$matchedElement; |
978
|
|
|
if ($element && $this->includeMatchedElement) { |
979
|
|
|
/** @var Element $element */ |
980
|
|
|
$fieldHandles = FieldHelper::fieldsOfTypeFromElement($element, FieldHelper::SEO_SETTINGS_CLASS_KEY, true); |
981
|
|
|
foreach ($fieldHandles as $fieldHandle) { |
982
|
|
|
if (!empty($element->$fieldHandle)) { |
983
|
|
|
/** @var MetaBundle $metaBundle */ |
984
|
|
|
$metaBundle = $element->$fieldHandle; |
985
|
|
|
Seomatic::$plugin->metaBundles->pruneFieldMetaBundleSettings($metaBundle, $fieldHandle); |
986
|
|
|
|
987
|
|
|
// See which properties have to be overridden, because the parent bundle says so. |
988
|
|
|
foreach (self::COMPOSITE_SETTING_LOOKUP as $settingName => $rules) { |
989
|
|
|
if (empty($metaBundle->metaGlobalVars->{$settingName})) { |
990
|
|
|
$parentBundle = Seomatic::$plugin->metaBundles->getContentMetaBundleForElement($element); |
991
|
|
|
|
992
|
|
|
foreach ($rules as $settingPath => $action) { |
993
|
|
|
list ($container, $property) = explode('.', $settingPath); |
994
|
|
|
list ($testValue, $sourceSetting) = explode('.', $action); |
995
|
|
|
|
996
|
|
|
$bundleProp = $parentBundle->{$container}->{$property} ?? null; |
997
|
|
|
if ($bundleProp == $testValue) { |
998
|
|
|
$metaBundle->metaGlobalVars->{$settingName} = $metaBundle->metaGlobalVars->{$sourceSetting}; |
999
|
|
|
} |
1000
|
|
|
} |
1001
|
|
|
} |
1002
|
|
|
} |
1003
|
|
|
|
1004
|
|
|
// Handle re-creating the `mainEntityOfPage` so that the model injected into the |
1005
|
|
|
// templates has the appropriate attributes |
1006
|
|
|
$generalContainerKey = MetaJsonLdContainer::CONTAINER_TYPE . JsonLdService::GENERAL_HANDLE; |
1007
|
|
|
$generalContainer = $this->metaContainers[$generalContainerKey]; |
1008
|
|
|
if (($generalContainer !== null) && !empty($generalContainer->data['mainEntityOfPage'])) { |
1009
|
|
|
/** @var MetaJsonLd $jsonLdModel */ |
1010
|
|
|
$jsonLdModel = $generalContainer->data['mainEntityOfPage']; |
1011
|
|
|
$config = $jsonLdModel->getAttributes(); |
1012
|
|
|
$schemaType = $metaBundle->metaGlobalVars->mainEntityOfPage ?? $config['type'] ?? null; |
1013
|
|
|
// If the schemaType is '' we should fall back on whatever the mainEntityOfPage already is |
1014
|
|
|
if (empty($schemaType)) { |
1015
|
|
|
$schemaType = null; |
1016
|
|
|
} |
1017
|
|
|
if ($schemaType !== null) { |
1018
|
|
|
$config['key'] = 'mainEntityOfPage'; |
1019
|
|
|
$schemaType = MetaValueHelper::parseString($schemaType); |
1020
|
|
|
$generalContainer->data['mainEntityOfPage'] = MetaJsonLd::create($schemaType, $config); |
1021
|
|
|
} |
1022
|
|
|
} |
1023
|
|
|
// Fire an 'metaBundleDebugData' event |
1024
|
|
|
if ($this->hasEventHandlers(self::EVENT_METABUNDLE_DEBUG_DATA)) { |
1025
|
|
|
$event = new MetaBundleDebugDataEvent([ |
1026
|
|
|
'metaBundleCategory' => MetaBundleDebugDataEvent::FIELD_META_BUNDLE, |
1027
|
|
|
'metaBundle' => $metaBundle, |
1028
|
|
|
]); |
1029
|
|
|
$this->trigger(self::EVENT_METABUNDLE_DEBUG_DATA, $event); |
1030
|
|
|
} |
1031
|
|
|
$this->addMetaBundleToContainers($metaBundle); |
1032
|
|
|
} |
1033
|
|
|
} |
1034
|
|
|
} |
1035
|
|
|
Craft::endProfile('MetaContainers::loadFieldMetaContainers', __METHOD__); |
1036
|
|
|
} |
1037
|
|
|
|
1038
|
|
|
/** |
1039
|
|
|
* Generate an md5 hash from an object or array |
1040
|
|
|
* |
1041
|
|
|
* @param string|array|MetaItem $data |
1042
|
|
|
* |
1043
|
|
|
* @return string |
1044
|
|
|
*/ |
1045
|
|
|
protected function getHash($data): string |
1046
|
|
|
{ |
1047
|
|
|
if (is_object($data)) { |
1048
|
|
|
$data = $data->toArray(); |
1049
|
|
|
} |
1050
|
|
|
if (is_array($data)) { |
1051
|
|
|
$data = serialize($data); |
1052
|
|
|
} |
1053
|
|
|
|
1054
|
|
|
return md5($data); |
1055
|
|
|
} |
1056
|
|
|
|
1057
|
|
|
// Private Methods |
1058
|
|
|
// ========================================================================= |
1059
|
|
|
} |
1060
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths