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

[5.x] When augmenting terms, entries_count should only consider published entries #10727

Merged
merged 3 commits into from
Sep 11, 2024
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
11 changes: 10 additions & 1 deletion src/Stache/Repositories/TermRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Statamic\Exceptions\TaxonomyNotFoundException;
use Statamic\Exceptions\TermNotFoundException;
use Statamic\Facades\Collection;
use Statamic\Facades\Entry;
use Statamic\Facades\Taxonomy;
use Statamic\Stache\Query\TermQueryBuilder;
use Statamic\Stache\Stache;
Expand Down Expand Up @@ -138,7 +139,7 @@ public function make(?string $slug = null): Term
return app(Term::class)->slug($slug);
}

public function entriesCount(Term $term): int
public function entriesCount(Term $term, ?string $status = null): int
{
$items = $this->store->store($term->taxonomyHandle())
->index('associations')
Expand All @@ -153,6 +154,14 @@ public function entriesCount(Term $term): int
$items = $items->where('collection', $collection->handle());
}

if ($status) {
return Entry::query()
->whereIn('id', $items->pluck('entry')->all())
->when($collection, fn ($query) => $query->where('collection', $collection->handle()))
->whereStatus($status)
->count();
}

return $items->count();
}

Expand Down
14 changes: 14 additions & 0 deletions src/Taxonomies/AugmentedTerm.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Statamic\Taxonomies;

use Statamic\Data\AbstractAugmented;
use Statamic\Facades\Blink;
use Statamic\Facades\Term;
use Statamic\Query\StatusQueryBuilder;
use Statamic\Statamic;

Expand Down Expand Up @@ -80,4 +82,16 @@ public function title()

return $this->wrapValue($title, 'title');
}

public function entriesCount()
{
$key = vsprintf('term-published-entries-count-%s-%s', [
$this->data->id(),
optional($this->data->collection())->handle(),
]);

return Blink::once($key, function () {
return Term::entriesCount($this->data, 'published');
});
}
}
Loading