From 5ca20e2443462fbc937e39a8c634fe2fc3276ed9 Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Wed, 11 Sep 2024 16:11:08 +0100 Subject: [PATCH] [5.x] When augmenting terms, `entries_count` should only consider published entries (#10727) Co-authored-by: duncanmcclean Co-authored-by: Jason Varga --- src/Stache/Repositories/TermRepository.php | 11 ++++++++++- src/Taxonomies/AugmentedTerm.php | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Stache/Repositories/TermRepository.php b/src/Stache/Repositories/TermRepository.php index 2c0ec7e227..be09c040ce 100644 --- a/src/Stache/Repositories/TermRepository.php +++ b/src/Stache/Repositories/TermRepository.php @@ -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; @@ -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') @@ -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(); } diff --git a/src/Taxonomies/AugmentedTerm.php b/src/Taxonomies/AugmentedTerm.php index 4c9c06e958..dd82c49a27 100644 --- a/src/Taxonomies/AugmentedTerm.php +++ b/src/Taxonomies/AugmentedTerm.php @@ -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; @@ -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'); + }); + } }