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