From 589f9f39bdcc17549fa38229f35fe47b3b05e815 Mon Sep 17 00:00:00 2001 From: Eric Rosas Date: Tue, 22 Oct 2024 15:39:44 -0700 Subject: [PATCH] Added support for callout-homepage revalidation Added support for callout-homepage revalidation --- src/NextBuilds.php | 30 +++++++++++++++++++++--------- src/services/Request.php | 10 ++++------ 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/NextBuilds.php b/src/NextBuilds.php index b1b5366..bbb0773 100755 --- a/src/NextBuilds.php +++ b/src/NextBuilds.php @@ -63,6 +63,8 @@ class NextBuilds extends Plugin */ public string $schemaVersion = '1.0.0'; + private string $homeUri = "__home__"; + /** * @var bool */ @@ -98,8 +100,14 @@ function(StatusChangeEvent $event) { !($entry->duplicateOf && $entry->getIsCanonical() && !$entry->updatingFromDerivative) && !ElementHelper::rootElement($entry)->isProvisionalDraft && $newStatus == Entry::STATUS_LIVE) { + + if($entry->type->handle == "callout") { + $this->request->buildPagesFromEntry($this->homeUri, false); + } else if ($entry->uri != null) { $revalidateMenu = ($entry->type->handle == "pages"); - $this->request->buildPagesFromEntry($entry, $revalidateMenu); + $this->request->buildPagesFromEntry($entry->uri, $revalidateMenu); + } + } } ); @@ -138,11 +146,12 @@ function (ModelEvent $event) { !ElementHelper::isDraftOrRevision($entry) && !($entry->duplicateOf && $entry->getIsCanonical() && !$entry->updatingFromDerivative) && !ElementHelper::rootElement($entry)->isProvisionalDraft && - !$entry->resaving + !$entry->resaving && + $entry->uri != null ) { $revalidateMenu = ($entry->type->handle == "pages"); Craft::$app->onAfterRequest(function() use ($entry, $revalidateMenu) { - $this->request->buildPagesFromEntry($entry, $revalidateMenu); + $this->request->buildPagesFromEntry($entry->uri, $revalidateMenu); }); } } @@ -157,11 +166,12 @@ function (Event $event) { $this->settings->activeSections[$entry->section->handle] && !ElementHelper::isDraftOrRevision($entry) && !($entry->duplicateOf && $entry->getIsCanonical() && !$entry->updatingFromDerivative) && - !ElementHelper::rootElement($entry)->isProvisionalDraft + !ElementHelper::rootElement($entry)->isProvisionalDraft && + $entry->uri != null ) { $revalidateMenu = ($entry->type->handle == "pages"); Craft::$app->onAfterRequest(function() use ($entry, $revalidateMenu) { - $this->request->buildPagesFromEntry($entry, $revalidateMenu); + $this->request->buildPagesFromEntry($entry->uri, $revalidateMenu); }); } } @@ -188,11 +198,12 @@ function (MoveElementEvent $event) { $this->settings->activeSections[$handle] && !ElementHelper::isDraftOrRevision($entry) && !($entry->duplicateOf && $entry->getIsCanonical() && !$entry->updatingFromDerivative) && - !ElementHelper::rootElement($entry)->isProvisionalDraft + !ElementHelper::rootElement($entry)->isProvisionalDraft && + $entry->uri != null ) { $revalidateMenu = ($handle == "pages"); Craft::$app->onAfterRequest(function() use ($entry, $revalidateMenu) { - $this->request->buildPagesFromEntry($entry, $revalidateMenu); + $this->request->buildPagesFromEntry($entry->uri, $revalidateMenu); }); } } @@ -207,11 +218,12 @@ function (Event $event) { $this->settings->activeSections[$entry->section->handle] && !ElementHelper::isDraftOrRevision($entry) && !($entry->duplicateOf && $entry->getIsCanonical() && !$entry->updatingFromDerivative) && - !ElementHelper::rootElement($entry)->isProvisionalDraft + !ElementHelper::rootElement($entry)->isProvisionalDraft && + $entry->uri != null ) { $revalidateMenu = ($entry->type->handle == "pages"); Craft::$app->onAfterRequest(function() use ($entry, $revalidateMenu) { - $this->request->buildPagesFromEntry($entry, $revalidateMenu); + $this->request->buildPagesFromEntry($entry->uri, $revalidateMenu); }); } } diff --git a/src/services/Request.php b/src/services/Request.php index 33322ee..0a84147 100755 --- a/src/services/Request.php +++ b/src/services/Request.php @@ -10,8 +10,6 @@ use Craft; use lsst\nextbuilds\NextBuilds; use craft\base\Component; -use craft\elements\Entry; -use craft\helpers\Session; use GuzzleHttp\Client; /** @@ -27,18 +25,18 @@ class Request extends Component // ========================================================================= /** - * @param Entry $entry + * @param String uri * @return void * @throws \GuzzleHttp\Exception\GuzzleException */ - public function buildPagesFromEntry(Entry $entry, string $revalidateMenu) + public function buildPagesFromEntry(string $uri, bool $revalidateMenu) { $settings = NextBuilds::getInstance()->getSettings(); $client = new Client(); $endpoint = $this->getSettingsData($settings->nextApiBaseUrl) . self::NEXT_ENDPOINT_REVALIDATE; $params = [ - 'uri' => $entry->uri, + 'uri' => $uri, 'secret' => $this->getSettingsData($settings->nextSecretToken) ]; if ($revalidateMenu) { @@ -51,7 +49,7 @@ public function buildPagesFromEntry(Entry $entry, string $revalidateMenu) $client->request('GET', $requestUrl, []); $isConsoleRequest = Craft::$app->getRequest()->getIsConsoleRequest(); if (!$isConsoleRequest) { - Craft::$app->session->setNotice('Requesting revalidation: ' . $entry->uri); + Craft::$app->session->setNotice('Requesting revalidation: ' . $uri); } } catch (\Exception $exception) { Craft::error($exception->getMessage(), "REVALIDATE_STATUS");