diff --git a/src/Search/Searchables/Assets.php b/src/Search/Searchables/Assets.php index a4848314db9..7ae8a8b23a7 100644 --- a/src/Search/Searchables/Assets.php +++ b/src/Search/Searchables/Assets.php @@ -28,7 +28,11 @@ public function provide(): Collection // TODO: query scope support? - return $assets->filter($this->filter())->values()->map->reference(); + if ($filter = $this->filter()) { + $assets = $assets->filter($filter); + } + + return $assets->values()->map->reference(); } public function contains($searchable): bool @@ -41,7 +45,9 @@ public function contains($searchable): bool return false; } - return $this->filter()($searchable); + return ($filter = $this->filter()) + ? $filter($searchable) + : true; } public function find(array $keys): Collection diff --git a/src/Search/Searchables/Entries.php b/src/Search/Searchables/Entries.php index f9a1a808051..e3a14a622db 100644 --- a/src/Search/Searchables/Entries.php +++ b/src/Search/Searchables/Entries.php @@ -33,15 +33,15 @@ public function provide(): Collection|LazyCollection $this->applyQueryScope($query); - if ($this->hasFilter()) { + if ($filter = $this->filter()) { return $query ->lazy(config('statamic.search.chunk_size')) - ->filter($this->filter()) + ->filter($filter) ->values() ->map->reference(); } - $query->where('status', 'published'); + $query->whereStatus('published'); return $query->pluck('reference'); } @@ -60,12 +60,12 @@ public function contains($searchable): bool return false; } - if ($this->hasFilter()) { - return $this->filter()($searchable); + if ($filter = $this->filter()) { + return $filter($searchable); } $query = Entry::query() - ->where('status', 'published') + ->whereStatus('published') ->where('id', $searchable->id()); $this->applyQueryScope($query); @@ -77,9 +77,4 @@ public function find(array $ids): Collection { return Entry::query()->whereIn('id', $ids)->get(); } - - protected function defaultFilter() - { - return fn ($item) => $item->status() === 'published'; - } } diff --git a/src/Search/Searchables/Provider.php b/src/Search/Searchables/Provider.php index 93252c2c346..cbfd747cad6 100644 --- a/src/Search/Searchables/Provider.php +++ b/src/Search/Searchables/Provider.php @@ -6,7 +6,6 @@ use Statamic\Facades\Search; use Statamic\Search\Index; use Statamic\Search\ProvidesSearchables; -use Statamic\Support\Arr; abstract class Provider implements ProvidesSearchables { @@ -66,11 +65,6 @@ protected function applyQueryScope($query) Scope::find($scope)->apply($query, []); } - protected function hasFilter() - { - return Arr::has($this->index->config(), 'filter'); - } - protected function filter() { $filter = $this->index->config()['filter'] ?? null; @@ -84,6 +78,6 @@ protected function filter() protected function defaultFilter() { - return fn () => true; + return null; } } diff --git a/src/Search/Searchables/Terms.php b/src/Search/Searchables/Terms.php index 601a0ba65bd..60c32681e07 100644 --- a/src/Search/Searchables/Terms.php +++ b/src/Search/Searchables/Terms.php @@ -34,10 +34,10 @@ public function provide(): Collection|LazyCollection $this->applyQueryScope($query); - if ($this->hasFilter()) { + if ($filter = $this->filter()) { return $query ->lazy(config('statamic.search.chunk_size')) - ->filter($this->filter()) + ->filter($filter) ->values() ->map->reference(); } @@ -59,8 +59,8 @@ public function contains($searchable): bool return false; } - if ($this->hasFilter()) { - return $this->filter()($searchable); + if ($filter = $this->filter()) { + return $filter($searchable); } $query = Term::query()->where('reference', $searchable->reference()); diff --git a/src/Search/Searchables/Users.php b/src/Search/Searchables/Users.php index e14cf1659b4..c6a98f053da 100644 --- a/src/Search/Searchables/Users.php +++ b/src/Search/Searchables/Users.php @@ -25,10 +25,10 @@ public function provide(): Collection|LazyCollection $this->applyQueryScope($query); - if ($this->hasFilter()) { + if ($filter = $this->filter()) { return $query ->lazy(config('statamic.search.chunk_size')) - ->filter($this->filter()) + ->filter($filter) ->values() ->map->reference(); } @@ -42,8 +42,8 @@ public function contains($searchable): bool return false; } - if ($this->hasFilter()) { - return $this->filter()($searchable); + if ($filter = $this->filter()) { + return $filter($searchable); } $query = User::query()->where('id', $searchable->id());