From 346d50ec76e72097d27cab3ef421fa01171ea1ef Mon Sep 17 00:00:00 2001 From: Alba2020 Date: Wed, 29 Jun 2022 18:12:34 +0300 Subject: [PATCH] add SitemapGeneratedEvent --- src/console/controllers/SitemapController.php | 3 + src/events/SitemapGeneratedEvent.php | 56 +++++++++++++++++++ src/helpers/Sitemap.php | 29 +++++++++- 3 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 src/events/SitemapGeneratedEvent.php diff --git a/src/console/controllers/SitemapController.php b/src/console/controllers/SitemapController.php index 89414f065..4a32416fc 100644 --- a/src/console/controllers/SitemapController.php +++ b/src/console/controllers/SitemapController.php @@ -9,6 +9,8 @@ namespace nystudio107\seomatic\console\controllers; +use nystudio107\seomatic\events\SitemapGeneratedEvent; +use nystudio107\seomatic\helpers\Sitemap; use nystudio107\seomatic\models\MetaBundle; use nystudio107\seomatic\models\SitemapTemplate; use nystudio107\seomatic\Seomatic; @@ -16,6 +18,7 @@ use Craft; use craft\helpers\App; +use yii\base\Event; use yii\console\Controller; /** diff --git a/src/events/SitemapGeneratedEvent.php b/src/events/SitemapGeneratedEvent.php new file mode 100644 index 000000000..880801090 --- /dev/null +++ b/src/events/SitemapGeneratedEvent.php @@ -0,0 +1,56 @@ +url) { - list($altSourceId, $altSourceBundleType, $altSourceHandle, $altSourceSiteId, $altTypeId) + [$altSourceId, $altSourceBundleType, $altSourceHandle, $altSourceSiteId, $altTypeId] = Seomatic::$plugin->metaBundles->getMetaSourceFromElement($altElement); $altMetaBundle = Seomatic::$plugin->metaBundles->getMetaBundleBySourceId( $altSourceBundleType, @@ -355,6 +367,7 @@ public static function generateSitemap(array $params) ], ]); $lines = implode('', $lines); + // Cache sitemap cache; we use this instead of Seomatic::$cacheDuration because for // Control Panel requests, we set Seomatic::$cacheDuration = 1 so that they are never // cached @@ -374,8 +387,18 @@ public static function generateSitemap(array $params) if ($plugin !== null) { FastcgiCacheBust::$plugin->cache->clearAll(); } - } + // Fire event + $event = new SitemapGeneratedEvent([ + 'siteId' => $siteId, + 'groupId' => $groupId, + 'handle' => $handle, + 'type' => $type, + 'cacheKey' => $cacheKey, + 'cacheDependency' => $dependency, + ]); + Event::trigger(Sitemap::class, Sitemap::EVENT_SITEMAP_GENERATED, $event); + } /** * Combine any per-entry type field settings from $element with the passed in @@ -388,7 +411,7 @@ public static function generateSitemap(array $params) protected static function combineEntryTypeSettings($seoElement, Element $element, MetaBundle $metaBundle) { if (!empty($seoElement::typeMenuFromHandle($metaBundle->sourceHandle))) { - list($sourceId, $sourceBundleType, $sourceHandle, $sourceSiteId, $typeId) + [$sourceId, $sourceBundleType, $sourceHandle, $sourceSiteId, $typeId] = Seomatic::$plugin->metaBundles->getMetaSourceFromElement($element); $entryTypeBundle = Seomatic::$plugin->metaBundles->getMetaBundleBySourceId( $sourceBundleType,