diff --git a/src/Stache/Indexes/Entries/Status.php b/src/Stache/Indexes/Entries/Status.php new file mode 100644 index 00000000000..43e52af2852 --- /dev/null +++ b/src/Stache/Indexes/Entries/Status.php @@ -0,0 +1,94 @@ +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(); + } + } + + public function items() + { + return parent::items()->map(fn ($item) => explode('::', $item)[0]); + } +} 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())) {