Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix searchable item checking #2643

Merged
merged 6 commits into from
Oct 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 93 additions & 3 deletions src/Search/Searchables.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
namespace Statamic\Search;

use Closure;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Statamic\Contracts\Assets\Asset as AssetContract;
use Statamic\Contracts\Auth\User as UserContract;
use Statamic\Contracts\Entries\Entry as EntryContract;
use Statamic\Contracts\Taxonomies\Term as TermContract;
use Statamic\Facades\Asset;
use Statamic\Facades\Entry;
use Statamic\Facades\Term;
use Statamic\Facades\User;
use Statamic\Support\Arr;
use Statamic\Support\Str;

class Searchables
{
Expand All @@ -21,7 +26,7 @@ public function __construct(Index $index)

public function all(): Collection
{
$searchables = collect(Arr::wrap($this->index->config()['searchables']));
$searchables = $this->getConfiguredSearchables();

if ($searchables->contains('all')) {
return collect()
Expand Down Expand Up @@ -60,7 +65,92 @@ public function all(): Collection

public function contains($searchable)
{
return $this->all()->has($searchable->id());
$searchables = $this->getConfiguredSearchables();

if (! $this->isSearchable($searchable)) {
return false;
}

if ($searchables->contains('all')) {
return true;
}

if ($searchable instanceof EntryContract) {
$collections = $this->searchableCollections();

return $collections->isNotEmpty()
&& ($collections->contains('*') || $collections->contains($searchable->collectionHandle()));
}

if ($searchable instanceof TermContract) {
$taxonomies = $this->searchableTaxonomies();

return $taxonomies->isNotEmpty()
&& ($taxonomies->contains('*') || $taxonomies->contains($searchable->taxonomyHandle()));
}

if ($searchable instanceof AssetContract) {
$containers = $this->searchableAssetContainers();

return $containers->isNotEmpty()
&& ($containers->contains('*') || $containers->contains($searchable->containerHandle()));
}

if ($searchable instanceof UserContract) {
return $searchables->contains('users');
}

return false;
}

private function getConfiguredSearchables()
{
return collect(Arr::wrap($this->index->config()['searchables']));
}

private function searchableCollections()
{
return $this->getConfiguredSearchables()->filter(function ($item) {
return Str::startsWith($item, 'collection:');
})->map(function ($item) {
return Str::after($item, 'collection:');
});
}

private function searchableTaxonomies()
{
return $this->getConfiguredSearchables()->filter(function ($item) {
return Str::startsWith($item, 'taxonomy:');
})->map(function ($item) {
return Str::after($item, 'taxonomy:');
});
}

private function searchableAssetContainers()
{
return $this->getConfiguredSearchables()->filter(function ($item) {
return Str::startsWith($item, 'assets:');
})->map(function ($item) {
return Str::after($item, 'assets:');
});
}

private function isSearchable($searchable)
{
$contracts = [
EntryContract::class,
TermContract::class,
AssetContract::class,
UserContract::class,
];

foreach ($contracts as $contract) {
if ($searchable instanceof $contract) {
return true;
}
}

return false;
}

public function fields($searchable): array
Expand Down
2 changes: 1 addition & 1 deletion src/Taxonomies/Term.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function taxonomy($taxonomy = null)

public function taxonomyHandle()
{
return $this->taxonomy()->handle();
return $this->taxonomy;
}

public function path()
Expand Down
Loading