Skip to content

Commit

Permalink
[5.x] When augmenting terms, entries_count should only consider pub…
Browse files Browse the repository at this point in the history
…lished entries (#10727)

Co-authored-by: duncanmcclean <[email protected]>
Co-authored-by: Jason Varga <[email protected]>
  • Loading branch information
3 people authored Sep 11, 2024
1 parent ee380ae commit 5ca20e2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
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');
});
}
}

0 comments on commit 5ca20e2

Please sign in to comment.