From 7d395c169f364d9ec5232f6834d6b198bed3ac0f Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Mon, 20 May 2024 16:09:46 -0400 Subject: [PATCH 01/10] refactor: Fix PHPStan issues --- src/Seomatic.php | 2 -- src/helpers/Sitemap.php | 2 -- 2 files changed, 4 deletions(-) diff --git a/src/Seomatic.php b/src/Seomatic.php index 3fc6fcdd9..7e7aa9f80 100644 --- a/src/Seomatic.php +++ b/src/Seomatic.php @@ -317,8 +317,6 @@ public function clearAllCaches(): void /** @var ?FastcgiCacheBust $plugin */ $plugin = Craft::$app->getPlugins()->getPlugin('fastcgi-cache-bust'); if ($plugin !== null) { - // FastcgiCacheBust has an error in its PHPdoc - /** @phpstan-ignore-next-line */ $plugin->cache->clearAll(); } } diff --git a/src/helpers/Sitemap.php b/src/helpers/Sitemap.php index a8ec2ca19..8e28abcdf 100644 --- a/src/helpers/Sitemap.php +++ b/src/helpers/Sitemap.php @@ -440,8 +440,6 @@ public static function generateSitemap(array $params) /** @var ?FastcgiCacheBust $plugin */ $plugin = Craft::$app->getPlugins()->getPlugin('fastcgi-cache-bust'); if ($plugin !== null) { - // FastcgiCacheBust has an error in its PHPdoc - /** @phpstan-ignore-next-line */ $plugin->cache->clearAll(); } } From 8f0f8bcc51ae35c8558cddf77a2043453051a31b Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Sun, 2 Jun 2024 22:03:10 -0400 Subject: [PATCH 02/10] fix: Fixed an issue that could cause an exception to be thrown if the selected asset for the Creator or Identity brand image was deleted ([#1472](https://github.com/nystudio107/craft-seomatic/issues/1472)) --- src/helpers/ImageTransform.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helpers/ImageTransform.php b/src/helpers/ImageTransform.php index d9e86171c..dd903a690 100644 --- a/src/helpers/ImageTransform.php +++ b/src/helpers/ImageTransform.php @@ -261,7 +261,7 @@ public static function assetElementsFromIds($assetIds, $siteId = null): array } } - return $assets; + return array_filter($assets); } // Protected Static Methods From 02af9d0b805d0bba4504ba9d254df3a7a915186c Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Sun, 2 Jun 2024 22:03:25 -0400 Subject: [PATCH 03/10] chore: Version 4.0.50 --- CHANGELOG.md | 4 ++++ composer.json | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 62d1be3f1..4d18e9b7f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # SEOmatic Changelog +## 4.0.50 - UNRELEASED +### Fixed +* Fixed an issue that could cause an exception to be thrown if the selected asset for the Creator or Identity brand image was deleted ([#1472](https://github.com/nystudio107/craft-seomatic/issues/1472)) + ## 4.0.49 - 2024.05.20 ### Fixed * Fixed an issue where the down and up arrows were reversed for sorting purposes diff --git a/composer.json b/composer.json index 0df3ebf71..c92c43a48 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "nystudio107/craft-seomatic", "description": "SEOmatic facilitates modern SEO best practices & implementation for Craft CMS 4. It is a turnkey SEO system that is comprehensive, powerful, and flexible.", "type": "craft-plugin", - "version": "4.0.49", + "version": "4.0.50", "keywords": [ "craft", "cms", From 39275ddfd31e73b435b16c30352e8743439d9798 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Wed, 5 Jun 2024 22:21:19 -0700 Subject: [PATCH 04/10] fix: Fixed an issue where the SEO preview for SEO Settings fields and the sidebar wouldn't be displayed correctly for drafts ([#1449](https://github.com/nystudio107/craft-seomatic/issues/1449)) --- src/fields/SeoSettings.php | 4 ++-- src/seoelements/SeoEntry.php | 5 ++--- src/services/MetaContainers.php | 20 ++++++++++++++------ 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/fields/SeoSettings.php b/src/fields/SeoSettings.php index bb4c99bf1..17526bbfb 100644 --- a/src/fields/SeoSettings.php +++ b/src/fields/SeoSettings.php @@ -352,7 +352,7 @@ public function getInputHtml(mixed $value, ?ElementInterface $element = null): s $variables['parentBundles'] = []; // Preview the containers so the preview is correct in the field if ($element !== null && $element->uri !== null) { - Seomatic::$plugin->metaContainers->previewMetaContainers($element->uri, $element->siteId, true); + Seomatic::$plugin->metaContainers->previewMetaContainers($element->uri, $element->siteId, true, true, $element); $contentMeta = Seomatic::$plugin->metaBundles->getContentMetaBundleForElement($element); $globalMeta = Seomatic::$plugin->metaBundles->getGlobalMetaBundle($element->siteId); $variables['parentBundles'] = [$contentMeta, $globalMeta]; @@ -397,7 +397,7 @@ public function getTableAttributeHtml(mixed $value, ElementInterface $element): $html = $cache->getOrSet( self::CACHE_KEY . $cacheKey, function() use ($uri, $siteId, $element) { - Seomatic::$plugin->metaContainers->previewMetaContainers($uri, $siteId, true); + Seomatic::$plugin->metaContainers->previewMetaContainers($uri, $siteId, true, true, $element); $variables = [ 'previewTypes' => [ $this->elementDisplayPreviewType, diff --git a/src/seoelements/SeoEntry.php b/src/seoelements/SeoEntry.php index cf3c94411..0d0c51ff4 100644 --- a/src/seoelements/SeoEntry.php +++ b/src/seoelements/SeoEntry.php @@ -192,7 +192,7 @@ static function(DefineHtmlEvent $event) { /** @var Entry $entry */ $entry = $event->sender ?? null; if ($entry !== null && $entry->uri !== null) { - Seomatic::$plugin->metaContainers->previewMetaContainers($entry->uri, $entry->siteId, true); + Seomatic::$plugin->metaContainers->previewMetaContainers($entry->uri, $entry->siteId, true, true, $entry); // Render our preview sidebar template if (Seomatic::$settings->displayPreviewSidebar && Seomatic::$matchedElement) { $html .= PluginTemplate::renderPluginTemplate('_sidebars/entry-preview.twig'); @@ -243,8 +243,7 @@ public static function sitemapAltElement( MetaBundle $metaBundle, int $elementId, int $siteId, - ) - { + ) { return Entry::find() ->section($metaBundle->sourceHandle) ->id($elementId) diff --git a/src/services/MetaContainers.php b/src/services/MetaContainers.php index 86e24fb2d..3cbe4dc71 100644 --- a/src/services/MetaContainers.php +++ b/src/services/MetaContainers.php @@ -14,9 +14,11 @@ use Craft; use craft\base\Component; use craft\base\Element; +use craft\base\ElementInterface; use craft\commerce\Plugin as CommercePlugin; use craft\console\Application as ConsoleApplication; use craft\elements\GlobalSet; +use craft\helpers\ElementHelper; use craft\web\UrlManager; use nystudio107\seomatic\base\MetaContainer; use nystudio107\seomatic\base\MetaItem; @@ -287,10 +289,11 @@ function() use ($uniqueKey) { * should be factored into the preview */ public function previewMetaContainers( - string $uri = '', - int $siteId = null, - bool $parseVariables = false, - bool $includeElement = true, + string $uri = '', + int $siteId = null, + bool $parseVariables = false, + bool $includeElement = true, + ?ElementInterface $element = null ) { // If we've already previewed the containers for this request, there's no need to do it again if (Seomatic::$previewingMetaContainers && !Seomatic::$headlessRequest) { @@ -303,7 +306,7 @@ public function previewMetaContainers( } Seomatic::$previewingMetaContainers = true; $this->includeMatchedElement = $includeElement; - $this->loadMetaContainers($uri, $siteId); + $this->loadMetaContainers($uri, $siteId, $element); // Load in the right globals $twig = Craft::$app->getView()->getTwig(); $globalSets = GlobalSet::findAll([ @@ -355,14 +358,19 @@ public function previewMetaContainers( * * @param string|null $uri * @param int|null $siteId + * @param ElementInterface|null $element */ - public function loadMetaContainers(?string $uri = '', int $siteId = null) + public function loadMetaContainers(?string $uri = '', int $siteId = null, ?ElementInterface $element = null) { Craft::beginProfile('MetaContainers::loadMetaContainers', __METHOD__); // Avoid recursion if (!Seomatic::$loadingMetaContainers) { Seomatic::$loadingMetaContainers = true; $this->setMatchedElement($uri, $siteId); + // If this is a draft or revision we're previewing, swap it in so they see the draft preview image & data + if ($element && ElementHelper::isDraftOrRevision($element)) { + Seomatic::setMatchedElement($element); + } // Get the cache tag for the matched meta bundle $metaBundle = $this->getMatchedMetaBundle(); $metaBundleSourceId = ''; From ead0e222c6ced34e6fd080f8a44797b6481e1d99 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Wed, 5 Jun 2024 22:21:58 -0700 Subject: [PATCH 05/10] chore: Version 4.0.50 --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d18e9b7f..75f60e09b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## 4.0.50 - UNRELEASED ### Fixed * Fixed an issue that could cause an exception to be thrown if the selected asset for the Creator or Identity brand image was deleted ([#1472](https://github.com/nystudio107/craft-seomatic/issues/1472)) +* Fixed an issue where the SEO preview for SEO Settings fields and the sidebar wouldn't be displayed correctly for drafts ([#1449](https://github.com/nystudio107/craft-seomatic/issues/1449)) ## 4.0.49 - 2024.05.20 ### Fixed From 54074e213aeed344878d4467f8e5daf0318a30f4 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Thu, 6 Jun 2024 18:24:13 -0700 Subject: [PATCH 06/10] refactor: Moved where paginated `hreflang` tags are added for paginated pages, so that they can be overriden via Twig templating code. They are now added inside of `seomatic.helper.paginate()` --- src/helpers/DynamicMeta.php | 7 +++++++ src/services/MetaContainers.php | 7 ------- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/helpers/DynamicMeta.php b/src/helpers/DynamicMeta.php index 623ac4e36..f82e1896d 100644 --- a/src/helpers/DynamicMeta.php +++ b/src/helpers/DynamicMeta.php @@ -132,6 +132,13 @@ public static function paginate(?Paginate $pageInfo) 'href' => $url, ]); } + // If this page is paginated, we need to factor that into the cache key + // We also need to re-add the hreflangs + if (Seomatic::$plugin->metaContainers->paginationPage !== '1') { + if (Seomatic::$settings->addHrefLang && Seomatic::$settings->addPaginatedHreflang) { + self::addMetaLinkHrefLang(); + } + } } } diff --git a/src/services/MetaContainers.php b/src/services/MetaContainers.php index 3cbe4dc71..e4e3dd0c3 100644 --- a/src/services/MetaContainers.php +++ b/src/services/MetaContainers.php @@ -211,13 +211,6 @@ public function getContainersOfType(string $type): array public function includeMetaContainers() { Craft::beginProfile('MetaContainers::includeMetaContainers', __METHOD__); - // If this page is paginated, we need to factor that into the cache key - // We also need to re-add the hreflangs - if ($this->paginationPage !== '1') { - if (Seomatic::$settings->addHrefLang && Seomatic::$settings->addPaginatedHreflang) { - DynamicMetaHelper::addMetaLinkHrefLang(); - } - } // Fire an 'metaBundleDebugData' event if ($this->hasEventHandlers(self::EVENT_METABUNDLE_DEBUG_DATA)) { $metaBundle = new MetaBundle([ From 94e5aacec3e60db5322eb03b87dcf016d960b17e Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Thu, 6 Jun 2024 18:24:50 -0700 Subject: [PATCH 07/10] chore: Version 4.0.50 --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 75f60e09b..72fb90dbb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ # SEOmatic Changelog ## 4.0.50 - UNRELEASED +### Changed +* Moved where paginated `hreflang` tags are added for paginated pages, so that they can be overriden via Twig templating code. They are now added inside of `seomatic.helper.paginate()` + ### Fixed * Fixed an issue that could cause an exception to be thrown if the selected asset for the Creator or Identity brand image was deleted ([#1472](https://github.com/nystudio107/craft-seomatic/issues/1472)) * Fixed an issue where the SEO preview for SEO Settings fields and the sidebar wouldn't be displayed correctly for drafts ([#1449](https://github.com/nystudio107/craft-seomatic/issues/1449)) From 52139d8662b4551da6bdf55ebea6edfd49666784 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Mon, 10 Jun 2024 17:43:27 -0700 Subject: [PATCH 08/10] feat: Added a setting in Plugin Settings -> Tags to specify which site should be used as the `x-default` for `hreflang` tags ([1162](https://github.com/nystudio107/craft-seomatic/issues/1162)) --- src/config.php | 3 ++ src/controllers/SettingsController.php | 2 ++ src/helpers/DynamicMeta.php | 3 +- src/models/Settings.php | 7 ++++ .../settings/plugin/_includes/tags.twig | 34 ++++++++++++++----- src/translations/en/seomatic.php | 4 ++- 6 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/config.php b/src/config.php index 563b15840..70fb01bd4 100644 --- a/src/config.php +++ b/src/config.php @@ -109,6 +109,9 @@ // Whether to dynamically include the `x-default` hreflang tags 'addXDefaultHrefLang' => true, + // The site to use for the `x-default` hreflang tag (0 defaults to the Primary site) + 'xDefaultSite' => 0, + // Whether to dynamically include hreflang tags on paginated pages 'addPaginatedHreflang' => true, diff --git a/src/controllers/SettingsController.php b/src/controllers/SettingsController.php index 68aa213c9..c0f1153be 100644 --- a/src/controllers/SettingsController.php +++ b/src/controllers/SettingsController.php @@ -879,6 +879,8 @@ public function actionPlugin(): Response ]; $variables['selectedSubnavItem'] = 'plugin'; $variables['settings'] = Seomatic::$settings; + $sites = ArrayHelper::map(Craft::$app->getSites()->getAllSites(), 'id', 'name'); + $variables['sites'] = $sites; // Render the template return $this->renderTemplate('seomatic/settings/plugin/_edit', $variables); diff --git a/src/helpers/DynamicMeta.php b/src/helpers/DynamicMeta.php index f82e1896d..e3230dd7d 100644 --- a/src/helpers/DynamicMeta.php +++ b/src/helpers/DynamicMeta.php @@ -642,6 +642,7 @@ public static function getLocalizedUrls(string $uri = null, int $siteId = null): $hreflangLanguage = $language; $hreflangLanguage = strtolower($hreflangLanguage); $hreflangLanguage = str_replace('_', '-', $hreflangLanguage); + $primary = Seomatic::$settings->xDefaultSite == 0 ? $site->primary : Seomatic::$settings->xDefaultSite == $site->id; if ($includeUrl) { $localizedUrls[] = [ 'id' => $site->id, @@ -649,7 +650,7 @@ public static function getLocalizedUrls(string $uri = null, int $siteId = null): 'ogLanguage' => $ogLanguage, 'hreflangLanguage' => $hreflangLanguage, 'url' => $url, - 'primary' => $site->primary, + 'primary' => $primary, 'current' => $thisSite->id === $site->id, ]; } diff --git a/src/models/Settings.php b/src/models/Settings.php index e48ee3c9e..bafff0293 100644 --- a/src/models/Settings.php +++ b/src/models/Settings.php @@ -157,6 +157,11 @@ class Settings extends VarsModel */ public bool $addXDefaultHrefLang = true; + /** + * @var int The site to use for the `x-default` hreflang tag (0 defaults to the Primary site) + */ + public int $xDefaultSite = 0; + /** * @var bool Whether to dynamically include hreflang tags on paginated pages */ @@ -292,6 +297,8 @@ public function rules(): array ], 'boolean', ], + ['xDefaultSite', 'integer'], + ['xDefaultSite', 'default', 'value' => 0], ['cspNonce', 'string'], ['cspNonce', 'in', 'range' => [ '', diff --git a/src/templates/settings/plugin/_includes/tags.twig b/src/templates/settings/plugin/_includes/tags.twig index e697a1b27..21904a39c 100644 --- a/src/templates/settings/plugin/_includes/tags.twig +++ b/src/templates/settings/plugin/_includes/tags.twig @@ -20,19 +20,35 @@ id: "addXDefaultHrefLang", name: "addXDefaultHrefLang", on: settings.addXDefaultHrefLang, + toggle: ".x-default-wrapper", warning: configWarning("addXDefaultHrefLang", "seomatic"), errors: settings.getErrors("addXDefaultHrefLang"), }) }} - {{ forms.lightswitchField({ - label: "Include Paginated `hreflang` Tags"|t("seomatic")|md, - instructions: "Controls whether SEOmatic will automatically include `hreflang` tags on paginated pages. [Learn More](https://moz.com/community/q/hreflang-alternate-pagination)"|t("seomatic"), - id: "addPaginatedHreflang", - name: "addPaginatedHreflang", - on: settings.addPaginatedHreflang, - warning: configWarning("addPaginatedHreflang", "seomatic"), - errors: settings.getErrors("addPaginatedHreflang"), - }) }} +
+ {{ forms.selectField({ + label: "`x-default` site"|md|t("seomatic"), + instructions: "Which site should be used as the `x-default` for the `hreflang` tag (default is the Primary site in Craft)."|md|t("seomatic"), + id: "xDefaultSite", + name: "xDefaultSite", + value: settings.xDefaultSite, + options: { + 0: "Primary"|t("seomatic"), + } | merge(sites), + warning: configWarning("xDefaultSite", "seomatic"), + errors: settings.getErrors("xDefaultSite"), + }) }} + + {{ forms.lightswitchField({ + label: "Include Paginated `hreflang` Tags"|t("seomatic")|md, + instructions: "Controls whether SEOmatic will use `hreflang` tags that point to the paginated page rather than the root page on paginated pages. [Learn More](https://moz.com/community/q/hreflang-alternate-pagination)"|t("seomatic"), + id: "addPaginatedHreflang", + name: "addPaginatedHreflang", + on: settings.addPaginatedHreflang, + warning: configWarning("addPaginatedHreflang", "seomatic"), + errors: settings.getErrors("addPaginatedHreflang"), + }) }} +
{{ forms.lightswitchField({ label: "Generator Enabled"|t("seomatic"), diff --git a/src/translations/en/seomatic.php b/src/translations/en/seomatic.php index 509572327..a12445fea 100644 --- a/src/translations/en/seomatic.php +++ b/src/translations/en/seomatic.php @@ -522,5 +522,7 @@ 'Body Begin - after the tag ' => 'Body Begin - after the tag ', 'The Asset Transform that should be applied to images before they are added to the sitemap.' => 'The Asset Transform that should be applied to images before they are added to the sitemap.', 'Asset Transform for Images' => 'Asset Transform for Images', - 'Sitemap Asset Transform' => 'Sitemap Asset Transform' + 'Sitemap Asset Transform' => 'Sitemap Asset Transform', + 'Controls whether SEOmatic will use `hreflang` tags that point to the paginated page rather than the root page on paginated pages. [Learn More](https://moz.com/community/q/hreflang-alternate-pagination)' => 'Controls whether SEOmatic will use `hreflang` tags that point to the paginated page rather than the root page on paginated pages. [Learn More](https://moz.com/community/q/hreflang-alternate-pagination)', + 'Primary' => 'Primary' ]; From 21512379635870df1d8bd4eb0fa0c44a1ff83825 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Mon, 10 Jun 2024 17:44:10 -0700 Subject: [PATCH 09/10] chore: Version 4.0.50 --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 72fb90dbb..925d4dedc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ # SEOmatic Changelog ## 4.0.50 - UNRELEASED +### Added +* Added a setting in Plugin Settings -> Tags to specify which site should be used as the `x-default` for `hreflang` tags ([1162](https://github.com/nystudio107/craft-seomatic/issues/1162)) + ### Changed * Moved where paginated `hreflang` tags are added for paginated pages, so that they can be overriden via Twig templating code. They are now added inside of `seomatic.helper.paginate()` From daa84f961e553c2ecc003ff6e281d3883998d977 Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Thu, 13 Jun 2024 15:57:03 -0700 Subject: [PATCH 10/10] chore: Version 4.0.50 --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 925d4dedc..45521230e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # SEOmatic Changelog -## 4.0.50 - UNRELEASED +## 4.0.50 - 2024.05.13 ### Added * Added a setting in Plugin Settings -> Tags to specify which site should be used as the `x-default` for `hreflang` tags ([1162](https://github.com/nystudio107/craft-seomatic/issues/1162))