From 8d17a752b20f237d0cfee442834ba151f503fed5 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Wed, 7 Jun 2023 18:21:01 -0400 Subject: [PATCH 1/2] wip --- src/Stache/Indexes/Entries/Status.php | 89 ++++++++++++++++++++ src/Stache/Stores/CollectionEntriesStore.php | 3 +- 2 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 src/Stache/Indexes/Entries/Status.php diff --git a/src/Stache/Indexes/Entries/Status.php b/src/Stache/Indexes/Entries/Status.php new file mode 100644 index 00000000000..41cc73f2ff3 --- /dev/null +++ b/src/Stache/Indexes/Entries/Status.php @@ -0,0 +1,89 @@ +status(); + + return $status.'::'.$this->getExpiration($entry, $status); + } + + private function getExpiration($entry, $status) + { + if (! $this->isDated()) { + return; + } + + // Past date behavior is private (eg. events that disappear after they happen) + if ($status === 'published' && $this->collection()->pastDateBehavior() === 'private') { + return $entry->date()->timestamp; + } + + // When future date behavior is private (eg. scheduled blog posts) + if ($status === 'scheduled' && $this->collection()->futureDateBehavior() === 'private') { + return $entry->date()->timestamp; + } + } + + public function load() + { + if ($this->loaded) { + return $this; + } + + parent::load(); + + $this->items = collect($this->items)->map(function ($value, $id) { + [$status, $expiration] = explode('::', $value); + + if ($newStatus = $this->updateItemIfNecessary($expiration, $id)) { + $status = $newStatus; + } + + return $status; + })->all(); + + return $this; + } + + public function update() + { + parent::update(); + + $this->recentlyUpdated = true; + + return $this; + } + + private function collection() + { + return $this->store->collection(); + } + + private function isDated() + { + return $this->collection()->dated(); + } + + private function updateItemIfNecessary($expiration, $id) + { + if (! $this->isDated() || $this->recentlyUpdated || ! $expiration) { + return; + } + + if (Carbon::createFromTimestamp($expiration)->isPast()) { + $this->updateItem($entry = Entry::find($id)); + + return $entry->status(); + } + } +} diff --git a/src/Stache/Stores/CollectionEntriesStore.php b/src/Stache/Stores/CollectionEntriesStore.php index 1182491c4c5..5278ec17f1f 100644 --- a/src/Stache/Stores/CollectionEntriesStore.php +++ b/src/Stache/Stores/CollectionEntriesStore.php @@ -20,7 +20,7 @@ class CollectionEntriesStore extends ChildStore { protected $collection; - protected function collection() + public function collection() { return $this->collection ?? Collection::findByHandle($this->childKey); } @@ -157,6 +157,7 @@ protected function storeIndexes() 'site' => Indexes\Site::class, 'origin' => Indexes\Origin::class, 'parent' => Indexes\Parents::class, + 'status' => Indexes\Entries\Status::class, ]); if (! $collection = Collection::findByHandle($this->childKey())) { From d9eaad4612fcb693a0d454ba2a3e8d6670dcffbc Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Thu, 22 Jun 2023 14:09:06 -0400 Subject: [PATCH 2/2] Take the bit before the separator --- src/Stache/Indexes/Entries/Status.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Stache/Indexes/Entries/Status.php b/src/Stache/Indexes/Entries/Status.php index 41cc73f2ff3..43e52af2852 100644 --- a/src/Stache/Indexes/Entries/Status.php +++ b/src/Stache/Indexes/Entries/Status.php @@ -86,4 +86,9 @@ private function updateItemIfNecessary($expiration, $id) return $entry->status(); } } + + public function items() + { + return parent::items()->map(fn ($item) => explode('::', $item)[0]); + } }