Total Complexity | 156 |
Total Lines | 792 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 2 | Features | 0 |
Complex classes like DynamicMeta 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 DynamicMeta, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
55 | class DynamicMeta |
||
56 | { |
||
57 | // Constants |
||
58 | // ========================================================================= |
||
59 | |||
60 | /** |
||
61 | * @event AddDynamicMetaEvent The event that is triggered when SEOmatic has |
||
62 | * included the standard meta containers, and gives your plugin/module |
||
63 | * the chance to add whatever custom dynamic meta items you like |
||
64 | * |
||
65 | * --- |
||
66 | * ```php |
||
67 | * use nystudio107\seomatic\events\AddDynamicMetaEvent; |
||
68 | * use nystudio107\seomatic\helpers\DynamicMeta; |
||
69 | * use yii\base\Event; |
||
70 | * Event::on(DynamicMeta::class, DynamicMeta::EVENT_ADD_DYNAMIC_META, function(AddDynamicMetaEvent $e) { |
||
71 | * // Add whatever dynamic meta items to the containers as you like |
||
72 | * }); |
||
73 | * ``` |
||
74 | */ |
||
75 | public const EVENT_ADD_DYNAMIC_META = 'addDynamicMeta'; |
||
76 | |||
77 | // Static Methods |
||
78 | // ========================================================================= |
||
79 | |||
80 | /** |
||
81 | * Paginate based on the passed in Paginate variable as returned from the |
||
82 | * Twig {% paginate %} tag: |
||
83 | * https://docs.craftcms.com/v3/templating/tags/paginate.html#the-pageInfo-variable |
||
84 | * |
||
85 | * @param ?Paginate $pageInfo |
||
86 | */ |
||
87 | public static function paginate(?Paginate $pageInfo) |
||
88 | { |
||
89 | if ($pageInfo !== null && $pageInfo->currentPage !== null) { |
||
90 | // Let the meta containers know that this page is paginated |
||
91 | Seomatic::$plugin->metaContainers->paginationPage = (string)$pageInfo->currentPage; |
||
92 | // See if we should strip the query params |
||
93 | $stripQueryParams = true; |
||
94 | $pageTrigger = Craft::$app->getConfig()->getGeneral()->pageTrigger; |
||
95 | // Is this query string-based pagination? |
||
96 | if ($pageTrigger[0] === '?') { |
||
97 | $stripQueryParams = false; |
||
98 | } |
||
99 | // Set the canonical URL to be the paginated URL |
||
100 | // see: https://github.com/nystudio107/craft-seomatic/issues/375#issuecomment-488369209 |
||
101 | $url = $pageInfo->getPageUrl($pageInfo->currentPage); |
||
102 | if ($stripQueryParams) { |
||
103 | $url = preg_replace('/\?.*/', '', $url); |
||
104 | } |
||
105 | if (!empty($url)) { |
||
106 | $url = UrlHelper::absoluteUrlWithProtocol($url); |
||
107 | Seomatic::$seomaticVariable->meta->canonicalUrl = $url; |
||
108 | $canonical = Seomatic::$seomaticVariable->link->get('canonical'); |
||
109 | if ($canonical !== null) { |
||
110 | $canonical->href = $url; |
||
111 | } |
||
112 | } |
||
113 | // Set the previous URL |
||
114 | $url = $pageInfo->getPrevUrl(); |
||
115 | if ($stripQueryParams) { |
||
116 | $url = preg_replace('/\?.*/', '', $url); |
||
117 | } |
||
118 | if (!empty($url)) { |
||
119 | $url = UrlHelper::absoluteUrlWithProtocol($url); |
||
120 | $metaTag = Seomatic::$plugin->link->create([ |
||
|
|||
121 | 'rel' => 'prev', |
||
122 | 'href' => $url, |
||
123 | ]); |
||
124 | } |
||
125 | // Set the next URL |
||
126 | $url = $pageInfo->getNextUrl(); |
||
127 | if ($stripQueryParams) { |
||
128 | $url = preg_replace('/\?.*/', '', $url); |
||
129 | } |
||
130 | if (!empty($url)) { |
||
131 | $url = UrlHelper::absoluteUrlWithProtocol($url); |
||
132 | $metaTag = Seomatic::$plugin->link->create([ |
||
133 | 'rel' => 'next', |
||
134 | 'href' => $url, |
||
135 | ]); |
||
136 | } |
||
137 | // If this page is paginated, we need to factor that into the cache key |
||
138 | // We also need to re-add the hreflangs |
||
139 | if (Seomatic::$plugin->metaContainers->paginationPage !== '1') { |
||
140 | if (Seomatic::$settings->addHrefLang && Seomatic::$settings->addPaginatedHreflang) { |
||
141 | self::addMetaLinkHrefLang(); |
||
142 | } |
||
143 | } |
||
144 | } |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Include any headers for this request |
||
149 | */ |
||
150 | public static function includeHttpHeaders() |
||
151 | { |
||
152 | self::addCspHeaders(); |
||
153 | // Don't include headers for any response code >= 400 |
||
154 | $request = Craft::$app->getRequest(); |
||
155 | if (!$request->isConsoleRequest) { |
||
156 | $response = Craft::$app->getResponse(); |
||
157 | if ($response->statusCode >= 400 || SeomaticHelper::isPreview()) { |
||
158 | return; |
||
159 | } |
||
160 | } |
||
161 | // Assuming they have headersEnabled, add the response code to the headers |
||
162 | if (Seomatic::$settings->headersEnabled) { |
||
163 | $response = Craft::$app->getResponse(); |
||
164 | // X-Robots-Tag header |
||
165 | $robots = Seomatic::$seomaticVariable->tag->get('robots'); |
||
166 | if ($robots !== null && $robots->include) { |
||
167 | $robotsArray = $robots->renderAttributes(); |
||
168 | $content = $robotsArray['content'] ?? ''; |
||
169 | if (!empty($content)) { |
||
170 | // The content property can be a string or an array |
||
171 | if (is_array($content)) { |
||
172 | $headerValue = ''; |
||
173 | foreach ($content as $contentVal) { |
||
174 | $headerValue .= ($contentVal . ','); |
||
175 | } |
||
176 | $headerValue = rtrim($headerValue, ','); |
||
177 | } else { |
||
178 | $headerValue = $content; |
||
179 | } |
||
180 | $response->headers->set('X-Robots-Tag', $headerValue); |
||
181 | } |
||
182 | } |
||
183 | // Link canonical header |
||
184 | $canonical = Seomatic::$seomaticVariable->link->get('canonical'); |
||
185 | if ($canonical !== null && $canonical->include) { |
||
186 | $canonicalArray = $canonical->renderAttributes(); |
||
187 | $href = $canonicalArray['href'] ?? ''; |
||
188 | if (!empty($href)) { |
||
189 | // The href property can be a string or an array |
||
190 | if (is_array($href)) { |
||
191 | $headerValue = ''; |
||
192 | foreach ($href as $hrefVal) { |
||
193 | $hrefVal = UrlHelper::encodeUrl($hrefVal); |
||
194 | $headerValue .= ('<' . $hrefVal . '>' . ','); |
||
195 | } |
||
196 | $headerValue = rtrim($headerValue, ','); |
||
197 | } else { |
||
198 | $href = UrlHelper::encodeUrl($href); |
||
199 | $headerValue = '<' . $href . '>'; |
||
200 | } |
||
201 | $headerValue .= "; rel='canonical'"; |
||
202 | $response->headers->add('Link', $headerValue); |
||
203 | } |
||
204 | } |
||
205 | // Referrer-Policy header |
||
206 | $referrer = Seomatic::$seomaticVariable->tag->get('referrer'); |
||
207 | if ($referrer !== null && $referrer->include) { |
||
208 | $referrerArray = $referrer->renderAttributes(); |
||
209 | $content = $referrerArray['content'] ?? ''; |
||
210 | if (!empty($content)) { |
||
211 | // The content property can be a string or an array |
||
212 | if (is_array($content)) { |
||
213 | $headerValue = ''; |
||
214 | foreach ($content as $contentVal) { |
||
215 | $headerValue .= ($contentVal . ','); |
||
216 | } |
||
217 | $headerValue = rtrim($headerValue, ','); |
||
218 | } else { |
||
219 | $headerValue = $content; |
||
220 | } |
||
221 | $response->headers->add('Referrer-Policy', $headerValue); |
||
222 | } |
||
223 | } |
||
224 | // The X-Powered-By tag |
||
225 | if (Seomatic::$settings->generatorEnabled) { |
||
226 | $response->headers->add('X-Powered-By', 'SEOmatic'); |
||
227 | } |
||
228 | } |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * Add the Content-Security-Policy script-src headers |
||
233 | */ |
||
234 | public static function addCspHeaders() |
||
235 | { |
||
236 | $cspNonces = self::getCspNonces(); |
||
237 | $container = Seomatic::$plugin->script->container(); |
||
238 | if ($container !== null) { |
||
239 | $container->addNonceHeaders($cspNonces); |
||
240 | } |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Get all of the CSP Nonces from containers that can have them |
||
245 | * |
||
246 | * @return array |
||
247 | */ |
||
248 | public static function getCspNonces(): array |
||
249 | { |
||
250 | $cspNonces = []; |
||
251 | // Add in any fixed policies from Settings |
||
252 | if (!empty(Seomatic::$settings->cspScriptSrcPolicies)) { |
||
253 | $fixedCsps = Seomatic::$settings->cspScriptSrcPolicies; |
||
254 | $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($fixedCsps)); |
||
255 | foreach ($iterator as $value) { |
||
256 | $cspNonces[] = $value; |
||
257 | } |
||
258 | } |
||
259 | // Add in any CSP nonce headers |
||
260 | $container = Seomatic::$plugin->jsonLd->container(); |
||
261 | if ($container !== null) { |
||
262 | $cspNonces = array_merge($cspNonces, $container->getCspNonces()); |
||
263 | } |
||
264 | $container = Seomatic::$plugin->script->container(); |
||
265 | if ($container !== null) { |
||
266 | $cspNonces = array_merge($cspNonces, $container->getCspNonces()); |
||
267 | } |
||
268 | |||
269 | return $cspNonces; |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * Add the Content-Security-Policy script-src tags |
||
274 | */ |
||
275 | public static function addCspTags() |
||
276 | { |
||
277 | $cspNonces = self::getCspNonces(); |
||
278 | $container = Seomatic::$plugin->script->container(); |
||
279 | if ($container !== null) { |
||
280 | $container->addNonceTags($cspNonces); |
||
281 | } |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * Add any custom/dynamic meta to the containers |
||
286 | * |
||
287 | * @param string|null $uri The URI of the route to add dynamic metadata for |
||
288 | * @param int|null $siteId The siteId of the current site |
||
289 | */ |
||
290 | public static function addDynamicMetaToContainers(string $uri = null, int $siteId = null) |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * If this is the homepage, and the MainEntityOfPage is WebPage or a WebSite, set the name |
||
328 | * and alternateName so it shows up in SERP as per: |
||
329 | * https://developers.google.com/search/docs/appearance/site-names |
||
330 | * |
||
331 | * @return void |
||
332 | */ |
||
333 | public static function handleHomepage(): void |
||
334 | { |
||
335 | if (Seomatic::$matchedElement && Seomatic::$matchedElement->uri === '__home__') { |
||
336 | $mainEntity = Seomatic::$plugin->jsonLd->get('mainEntityOfPage'); |
||
337 | if ($mainEntity instanceof WebPage || $mainEntity instanceof WebSite) { |
||
338 | /** WebPage $mainEntity */ |
||
339 | $mainEntity->name = "{{ seomatic.site.siteName }}"; |
||
340 | $mainEntity->alternateName = "{{ seomatic.site.siteAlternateName }}"; |
||
341 | } |
||
342 | } |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * Add breadcrumbs to the MetaJsonLdContainer |
||
347 | * |
||
348 | * @param int|null $siteId |
||
349 | */ |
||
350 | public static function addMetaJsonLdBreadCrumbs(int $siteId = null) |
||
458 | } |
||
459 | |||
460 | /** |
||
461 | * Add meta hreflang tags if there is more than one site |
||
462 | * |
||
463 | * @param string|null $uri |
||
464 | * @param int|null $siteId |
||
465 | */ |
||
466 | public static function addMetaLinkHrefLang(string $uri = null, int $siteId = null) |
||
467 | { |
||
468 | Craft::beginProfile('DynamicMeta::addMetaLinkHrefLang', __METHOD__); |
||
469 | $siteLocalizedUrls = self::getLocalizedUrls($uri, $siteId); |
||
470 | $currentPaginationUrl = null; |
||
471 | if (Seomatic::$plugin->metaContainers->paginationPage !== '1') { |
||
472 | $currentPaginationUrl = Seomatic::$seomaticVariable->meta->canonicalUrl ?? null; |
||
473 | } |
||
474 | if (!empty($siteLocalizedUrls)) { |
||
475 | // Add the rel=alternate tag |
||
476 | /** @var MetaLink $metaTag */ |
||
477 | $metaTag = Seomatic::$plugin->link->create([ |
||
478 | 'rel' => 'alternate', |
||
479 | 'hreflang' => [], |
||
480 | 'href' => [], |
||
481 | ]); |
||
482 | // Add the alternate language link rel's |
||
483 | if (count($siteLocalizedUrls) > 1) { |
||
484 | foreach ($siteLocalizedUrls as $siteLocalizedUrl) { |
||
485 | $url = $siteLocalizedUrl['url']; |
||
486 | if ($siteLocalizedUrl['current']) { |
||
487 | $url = $currentPaginationUrl ?? $siteLocalizedUrl['url']; |
||
488 | } |
||
489 | $metaTag->hreflang[] = $siteLocalizedUrl['hreflangLanguage']; |
||
490 | $metaTag->href[] = $url; |
||
491 | // Add the x-default hreflang |
||
492 | if ($siteLocalizedUrl['primary'] && Seomatic::$settings->addXDefaultHrefLang) { |
||
493 | $metaTag->hreflang[] = 'x-default'; |
||
494 | $metaTag->href[] = $siteLocalizedUrl['url']; |
||
495 | } |
||
496 | } |
||
497 | Seomatic::$plugin->link->add($metaTag); |
||
498 | } |
||
499 | // Add in the og:locale:alternate tags |
||
500 | $ogLocaleAlternate = Seomatic::$plugin->tag->get('og:locale:alternate'); |
||
501 | if (count($siteLocalizedUrls) > 1 && $ogLocaleAlternate) { |
||
502 | $ogContentArray = []; |
||
503 | foreach ($siteLocalizedUrls as $siteLocalizedUrl) { |
||
504 | if (!in_array($siteLocalizedUrl['ogLanguage'], $ogContentArray, true) && |
||
505 | Craft::$app->language !== $siteLocalizedUrl['language']) { |
||
506 | $ogContentArray[] = $siteLocalizedUrl['ogLanguage']; |
||
507 | } |
||
508 | } |
||
509 | $ogLocaleAlternate->content = $ogContentArray; |
||
510 | } |
||
511 | } |
||
512 | Craft::endProfile('DynamicMeta::addMetaLinkHrefLang', __METHOD__); |
||
513 | } |
||
514 | |||
515 | /** |
||
516 | * Return a list of localized URLs that are in the current site's group |
||
517 | * The current URI is used if $uri is null. Similarly, the current site is |
||
518 | * used if $siteId is null. |
||
519 | * The resulting array of arrays has `id`, `language`, `ogLanguage`, |
||
520 | * `hreflangLanguage`, and `url` as keys. |
||
521 | * |
||
522 | * @param string|null $uri |
||
523 | * @param int|null $siteId |
||
524 | * |
||
525 | * @return array |
||
526 | */ |
||
527 | public static function getLocalizedUrls(string $uri = null, int $siteId = null): array |
||
528 | { |
||
529 | Craft::beginProfile('DynamicMeta::getLocalizedUrls', __METHOD__); |
||
530 | $localizedUrls = []; |
||
531 | // No pagination params for URLs |
||
532 | $urlParams = null; |
||
533 | // Get the request URI |
||
534 | if ($uri === null) { |
||
535 | $requestUri = Craft::$app->getRequest()->pathInfo; |
||
536 | } else { |
||
537 | $requestUri = $uri; |
||
538 | } |
||
539 | // Get the site to use |
||
540 | if ($siteId === null) { |
||
541 | try { |
||
542 | $thisSite = Craft::$app->getSites()->getCurrentSite(); |
||
543 | } catch (SiteNotFoundException $e) { |
||
544 | $thisSite = null; |
||
545 | Craft::error($e->getMessage(), __METHOD__); |
||
546 | } |
||
547 | } else { |
||
548 | $thisSite = Craft::$app->getSites()->getSiteById($siteId); |
||
549 | } |
||
550 | // Bail if we can't get a site |
||
551 | if ($thisSite === null) { |
||
552 | return $localizedUrls; |
||
553 | } |
||
554 | if (Seomatic::$settings->siteGroupsSeparate) { |
||
555 | // Get only the sites that are in the current site's group |
||
556 | try { |
||
557 | $siteGroup = $thisSite->getGroup(); |
||
558 | } catch (InvalidConfigException $e) { |
||
559 | $siteGroup = null; |
||
560 | Craft::error($e->getMessage(), __METHOD__); |
||
561 | } |
||
562 | // Bail if we can't get a site group |
||
563 | if ($siteGroup === null) { |
||
564 | return $localizedUrls; |
||
565 | } |
||
566 | $sites = $siteGroup->getSites(); |
||
567 | } else { |
||
568 | $sites = Craft::$app->getSites()->getAllSites(); |
||
569 | } |
||
570 | $elements = Craft::$app->getElements(); |
||
571 | foreach ($sites as $site) { |
||
572 | $includeUrl = true; |
||
573 | $matchedElement = $elements->getElementByUri($requestUri, $thisSite->id, true); |
||
574 | if ($matchedElement) { |
||
575 | $url = $elements->getElementUriForSite($matchedElement->getId(), $site->id); |
||
576 | // See if they have disabled sitemaps or robots for this entry, |
||
577 | // and if so, don't include it in the hreflang |
||
578 | $element = null; |
||
579 | if ($url) { |
||
580 | /** @var Element $element */ |
||
581 | $element = $elements->getElementByUri($url, $site->id, false); |
||
582 | } |
||
583 | if ($element !== null) { |
||
584 | /** @var MetaBundle $metaBundle */ |
||
585 | list($sourceId, $sourceBundleType, $sourceHandle, $sourceSiteId, $typeId) |
||
586 | = Seomatic::$plugin->metaBundles->getMetaSourceFromElement($element); |
||
587 | $metaBundle = Seomatic::$plugin->metaBundles->getMetaBundleBySourceId( |
||
588 | $sourceBundleType, |
||
589 | $sourceId, |
||
590 | $sourceSiteId |
||
591 | ); |
||
592 | if ($metaBundle !== null) { |
||
593 | // If robots contains 'none' or 'noindex' don't include the URL |
||
594 | $robotsArray = explode(',', $metaBundle->metaGlobalVars->robots); |
||
595 | if (in_array('noindex', $robotsArray, true) || in_array('none', $robotsArray, true)) { |
||
596 | $includeUrl = false; |
||
597 | } |
||
598 | } |
||
599 | $fieldHandles = FieldHelper::fieldsOfTypeFromElement( |
||
600 | $element, |
||
601 | FieldHelper::SEO_SETTINGS_CLASS_KEY, |
||
602 | true |
||
603 | ); |
||
604 | foreach ($fieldHandles as $fieldHandle) { |
||
605 | if (!empty($element->$fieldHandle)) { |
||
606 | /** @var MetaBundle $fieldMetaBundle */ |
||
607 | $fieldMetaBundle = $element->$fieldHandle; |
||
608 | /** @var SeoSettings $seoSettingsField */ |
||
609 | $seoSettingsField = Craft::$app->getFields()->getFieldByHandle($fieldHandle); |
||
610 | if ($seoSettingsField !== null) { |
||
611 | // If robots is set to 'none' don't include the URL |
||
612 | if ($seoSettingsField->generalTabEnabled |
||
613 | && in_array('robots', $seoSettingsField->generalEnabledFields, false) |
||
614 | && !Seomatic::$plugin->helper->isInherited($fieldMetaBundle->metaGlobalVars, 'robots') |
||
615 | ) { |
||
616 | // If robots contains 'none' or 'noindex' don't include the URL |
||
617 | $robotsArray = explode(',', $fieldMetaBundle->metaGlobalVars->robots); |
||
618 | if (in_array('noindex', $robotsArray, true) || in_array('none', $robotsArray, true)) { |
||
619 | $includeUrl = false; |
||
620 | } else { |
||
621 | // Otherwise, include the URL |
||
622 | $includeUrl = true; |
||
623 | } |
||
624 | } |
||
625 | } |
||
626 | } |
||
627 | } |
||
628 | // Never include the URL if the element isn't enabled for the site |
||
629 | if (isset($element->enabledForSite) && !(bool)$element->enabledForSite) { |
||
630 | $includeUrl = false; |
||
631 | } |
||
632 | } else { |
||
633 | $includeUrl = false; |
||
634 | } |
||
635 | $url = ($url === '__home__') ? '' : $url; |
||
636 | } else { |
||
637 | try { |
||
638 | $url = SiteHelper::siteEnabledWithUrls($site->id) ? UrlHelper::siteUrl($requestUri, $urlParams, null, $site->id) |
||
639 | : Craft::$app->getSites()->getPrimarySite()->baseUrl; |
||
640 | } catch (SiteNotFoundException $e) { |
||
641 | $url = ''; |
||
642 | Craft::error($e->getMessage(), __METHOD__); |
||
643 | } catch (Exception $e) { |
||
644 | $url = ''; |
||
645 | Craft::error($e->getMessage(), __METHOD__); |
||
646 | } |
||
647 | } |
||
648 | $url = $url ?? ''; |
||
649 | if (!UrlHelper::isAbsoluteUrl($url)) { |
||
650 | try { |
||
651 | $url = UrlHelper::siteUrl($url, $urlParams, null, $site->id); |
||
652 | } catch (Exception $e) { |
||
653 | $url = ''; |
||
654 | Craft::error($e->getMessage(), __METHOD__); |
||
655 | } |
||
656 | } |
||
657 | // Strip any query string params, and make sure we have an absolute URL with protocol |
||
658 | if ($urlParams === null) { |
||
659 | $url = UrlHelper::stripQueryString($url); |
||
660 | } |
||
661 | $url = UrlHelper::absoluteUrlWithProtocol($url); |
||
662 | |||
663 | $url = self::sanitizeUrl($url); |
||
664 | $language = $site->language; |
||
665 | $ogLanguage = LocalizationHelper::normalizeOgLocaleLanguage($language); |
||
666 | $hreflangLanguage = $language; |
||
667 | $hreflangLanguage = strtolower($hreflangLanguage); |
||
668 | $hreflangLanguage = str_replace('_', '-', $hreflangLanguage); |
||
669 | $primary = Seomatic::$settings->xDefaultSite == 0 ? $site->primary : Seomatic::$settings->xDefaultSite == $site->id; |
||
670 | if ($includeUrl) { |
||
671 | $localizedUrls[] = [ |
||
672 | 'id' => $site->id, |
||
673 | 'language' => $language, |
||
674 | 'ogLanguage' => $ogLanguage, |
||
675 | 'hreflangLanguage' => $hreflangLanguage, |
||
676 | 'url' => $url, |
||
677 | 'primary' => $primary, |
||
678 | 'current' => $thisSite->id === $site->id, |
||
679 | ]; |
||
680 | } |
||
681 | } |
||
682 | Craft::endProfile('DynamicMeta::getLocalizedUrls', __METHOD__); |
||
683 | |||
684 | return $localizedUrls; |
||
685 | } |
||
686 | |||
687 | /** |
||
688 | * Return a sanitized URL with the query string stripped |
||
689 | * |
||
690 | * @param string $url |
||
691 | * @param bool $checkStatus |
||
692 | * |
||
693 | * @return string |
||
694 | */ |
||
695 | public static function sanitizeUrl(string $url, bool $checkStatus = true, bool $stripQueryString = true): string |
||
696 | { |
||
697 | // Remove the query string |
||
698 | if ($stripQueryString) { |
||
699 | $url = UrlHelper::stripQueryString($url); |
||
700 | } |
||
701 | $url = UrlHelper::encodeUrlQueryParams(TextHelper::sanitizeUserInput($url)); |
||
702 | |||
703 | // If this is a >= 400 status code, set the canonical URL to nothing |
||
704 | if ($checkStatus && !Craft::$app->getRequest()->getIsConsoleRequest() && Craft::$app->getResponse()->statusCode >= 400) { |
||
705 | $url = ''; |
||
706 | } |
||
707 | |||
708 | return $url; |
||
709 | } |
||
710 | |||
711 | /** |
||
712 | * Add the Same As meta tags and JSON-LD |
||
713 | */ |
||
714 | public static function addSameAsMeta() |
||
735 | } |
||
736 | |||
737 | /** |
||
738 | * Add the OpeningHoursSpecific to the $jsonLd based on the Entity settings |
||
739 | * |
||
740 | * @param MetaJsonLd $jsonLd |
||
741 | * @param ?Entity $entity |
||
742 | */ |
||
743 | public static function addOpeningHours(MetaJsonLd $jsonLd, ?Entity $entity) |
||
744 | { |
||
745 | Craft::beginProfile('DynamicMeta::addOpeningHours', __METHOD__); |
||
746 | if ($jsonLd instanceof LocalBusiness && $entity !== null) { |
||
747 | /** @var LocalBusiness $jsonLd */ |
||
748 | $openingHours = []; |
||
749 | $days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; |
||
750 | $times = $entity->localBusinessOpeningHours; |
||
751 | $index = 0; |
||
752 | foreach ($times as $hours) { |
||
753 | $openTime = ''; |
||
754 | $closeTime = ''; |
||
755 | if (!empty($hours['open'])) { |
||
756 | /** @var DateTime $dateTime */ |
||
757 | try { |
||
758 | $dateTime = DateTimeHelper::toDateTime($hours['open']['date'], false, false); |
||
759 | } catch (\Exception $e) { |
||
760 | $dateTime = false; |
||
761 | } |
||
762 | if ($dateTime !== false) { |
||
763 | $openTime = $dateTime->format('H:i:s'); |
||
764 | } |
||
765 | } |
||
766 | if (!empty($hours['close'])) { |
||
767 | /** @var DateTime $dateTime */ |
||
768 | try { |
||
769 | $dateTime = DateTimeHelper::toDateTime($hours['close']['date'], false, false); |
||
770 | } catch (\Exception $e) { |
||
771 | $dateTime = false; |
||
772 | } |
||
773 | if ($dateTime !== false) { |
||
774 | $closeTime = $dateTime->format('H:i:s'); |
||
775 | } |
||
776 | } |
||
777 | if ($openTime && $closeTime) { |
||
778 | /** @var OpeningHoursSpecification $hours */ |
||
779 | $hours = Seomatic::$plugin->jsonLd->create([ |
||
780 | 'type' => 'OpeningHoursSpecification', |
||
781 | 'opens' => $openTime, |
||
782 | 'closes' => $closeTime, |
||
783 | 'dayOfWeek' => [$days[$index]], |
||
784 | ], false); |
||
785 | $openingHours[] = $hours; |
||
786 | } |
||
787 | $index++; |
||
788 | } |
||
789 | $jsonLd->openingHoursSpecification = $openingHours; |
||
790 | } |
||
791 | Craft::endProfile('DynamicMeta::addOpeningHours', __METHOD__); |
||
792 | } |
||
793 | |||
794 | /** |
||
795 | * Add the ContactPoint to the $jsonLd based on the Entity settings |
||
796 | * |
||
797 | * @param MetaJsonLd $jsonLd |
||
798 | * @param Entity|null $entity |
||
799 | */ |
||
800 | public static function addContactPoints(MetaJsonLd $jsonLd, ?Entity $entity) |
||
801 | { |
||
802 | Craft::beginProfile('DynamicMeta::addContactPoints', __METHOD__); |
||
803 | if ($jsonLd instanceof Organization && $entity !== null) { |
||
804 | /** @var Organization $jsonLd */ |
||
805 | $contactPoints = []; |
||
806 | if (is_array($entity->organizationContactPoints)) { |
||
807 | foreach ($entity->organizationContactPoints as $contacts) { |
||
808 | /** @var ContactPoint $contact */ |
||
809 | $contact = Seomatic::$plugin->jsonLd->create([ |
||
810 | 'type' => 'ContactPoint', |
||
811 | 'telephone' => $contacts['telephone'], |
||
812 | 'contactType' => $contacts['contactType'], |
||
813 | ], false); |
||
814 | $contactPoints[] = $contact; |
||
815 | } |
||
816 | } |
||
817 | $jsonLd->contactPoint = $contactPoints; |
||
818 | } |
||
819 | Craft::endProfile('DynamicMeta::addContactPoints', __METHOD__); |
||
820 | } |
||
821 | |||
822 | /** |
||
823 | * Normalize the array of opening hours passed in |
||
824 | * |
||
825 | * @param $value |
||
826 | */ |
||
827 | public static function normalizeTimes(&$value) |
||
847 | } |
||
848 | } |
||
849 |