diff --git a/.gitignore b/.gitignore index 723ef36..090a1f0 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.idea \ No newline at end of file +.idea +.DS_Store diff --git a/src/jobs/RevalidateJob.php b/src/jobs/RevalidateJob.php index 6c42833..7095fba 100644 --- a/src/jobs/RevalidateJob.php +++ b/src/jobs/RevalidateJob.php @@ -3,11 +3,13 @@ namespace ether\utilitybelt\jobs; use Craft; +use craft\elements\Entry; use craft\queue\BaseJob; class RevalidateJob extends BaseJob { + public $sectionUid = null; public $uris = []; protected function defaultDescription (): string @@ -18,8 +20,8 @@ protected function defaultDescription (): string public function execute ($queue): bool { // Skip if localhost - if (str_contains(getenv('FRONTEND_URL'), 'local')) - return true; +// if (str_contains(getenv('FRONTEND_URL'), 'local')) +// return true; $this->uris = array_unique($this->uris); @@ -27,14 +29,41 @@ public function execute ($queue): bool $url = getenv('FRONTEND_URL') . '/api/revalidate?token=' . getenv('REVALIDATE_TOKEN') . '&path=/'; $total = count($this->uris); $i = 0; + $section = null; + + if (!empty($this->sectionUid)) + { + $section = Craft::$app->getSections()->getSectionByUid($this->sectionUid); + $entriesInSection = Entry::find()->sectionId($section->id)->count(); + + $urisWithTemplates = 0; + foreach ($this->uris as $uri) + if (str_contains($uri, '{')) + $urisWithTemplates++; + + $total = ($total - $urisWithTemplates) + ($entriesInSection * $urisWithTemplates); + } foreach ($this->uris as $uri) { - $client->get($url . ltrim($uri, '/')); - $queue->setProgress(++$i / $total * 100, $uri); + if (empty($this->sectionUid) || !str_contains($uri, '{')) { + $client->get($url . ltrim($uri, '/')); + $queue->setProgress(++$i / $total * 100, $uri); + } else { + foreach (Entry::find()->sectionId($section->id)->all() as $entry) + { + $parsedUrl = Craft::$app->getView()->renderObjectTemplate( + $uri, + $entry + ); + + $client->get($url . ltrim($parsedUrl, '/')); + $queue->setProgress(++$i / $total * 100, $uri); + } + } } return true; } -} \ No newline at end of file +} diff --git a/src/services/Revalidator.php b/src/services/Revalidator.php index f128a5b..51d74b6 100644 --- a/src/services/Revalidator.php +++ b/src/services/Revalidator.php @@ -200,6 +200,7 @@ public function push (Element $element) if ($element instanceof Entry) { $sectionUid = Craft::$app->getSections()->getSectionById($element->sectionId)->uid; + $job->sectionUid = $sectionUid; foreach ($this->getAdditionalURIs($sectionUid) as $uri) $job->uris[] = $uri; @@ -272,4 +273,4 @@ private function updateJob (int $id, RevalidateJob $job) ->execute(); } -} \ No newline at end of file +}