Skip to content

Commit 0e66ab1

Browse files
Add sites hook to Sitemap class (#438)
1 parent 9032ef3 commit 0e66ab1

File tree

2 files changed

+12
-22
lines changed

2 files changed

+12
-22
lines changed

src/Http/Controllers/SitemapController.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
use Carbon\Carbon;
66
use Illuminate\Routing\Controller;
77
use Illuminate\Support\Facades\Cache;
8-
use Illuminate\Support\Str;
9-
use Statamic\Facades;
108
use Statamic\SeoPro\Sitemap\Sitemap;
11-
use Statamic\Sites\Site;
129

1310
class SitemapController extends Controller
1411
{
@@ -18,20 +15,19 @@ public function index()
1815

1916
$key = request()->getHttpHost();
2017
$cacheUntil = Carbon::now()->addMinutes(config('statamic.seo-pro.sitemap.expire'));
21-
$sites = Facades\Site::all()->filter(fn (Site $site) => Str::of($site->absoluteUrl())->startsWith(request()->schemeAndHttpHost()));
2218

2319
if (config('statamic.seo-pro.sitemap.pagination.enabled', false)) {
24-
$content = Cache::remember(Sitemap::CACHE_KEY.'_'.$key.'_index', $cacheUntil, function () use ($sites) {
20+
$content = Cache::remember(Sitemap::CACHE_KEY.'_'.$key.'_index', $cacheUntil, function () {
2521
return view('seo-pro::sitemap_index', [
2622
'xml_header' => '<?xml version="1.0" encoding="UTF-8"?>',
27-
'sitemaps' => app(Sitemap::class)->forSites($sites)->paginatedSitemaps(),
23+
'sitemaps' => app(Sitemap::class)->paginatedSitemaps(),
2824
])->render();
2925
});
3026
} else {
31-
$content = Cache::remember(Sitemap::CACHE_KEY.'_'.$key, $cacheUntil, function () use ($sites) {
27+
$content = Cache::remember(Sitemap::CACHE_KEY.'_'.$key, $cacheUntil, function () {
3228
return view('seo-pro::sitemap', [
3329
'xml_header' => '<?xml version="1.0" encoding="UTF-8"?>',
34-
'pages' => app(Sitemap::class)->forSites($sites)->pages(),
30+
'pages' => app(Sitemap::class)->pages(),
3531
])->render();
3632
});
3733
}

src/Sitemap/Sitemap.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Support\Collection as IlluminateCollection;
66
use Illuminate\Support\LazyCollection;
7+
use Illuminate\Support\Str;
78
use Statamic\Contracts\Entries\Entry;
89
use Statamic\Contracts\Query\Builder;
910
use Statamic\Contracts\Taxonomies\Term;
@@ -24,13 +25,6 @@ class Sitemap
2425

2526
const CACHE_KEY = 'seo-pro.sitemap';
2627

27-
private IlluminateCollection $sites;
28-
29-
public function __construct()
30-
{
31-
$this->sites = collect();
32-
}
33-
3428
public function pages(): array
3529
{
3630
return collect()
@@ -103,11 +97,11 @@ public function paginatedSitemaps(): array
10397
->all();
10498
}
10599

106-
public function forSites(IlluminateCollection $sites): self
100+
private function sites(): IlluminateCollection
107101
{
108-
$this->sites = $sites;
102+
$sites = SiteFacade::all()->filter(fn ($site) => Str::of($site->absoluteUrl())->startsWith(request()->schemeAndHttpHost()));
109103

110-
return $this;
104+
return $this->runHooks('sites', $sites);
111105
}
112106

113107
protected function getPages($items)
@@ -153,8 +147,8 @@ protected function publishedEntriesQuery()
153147

154148
return EntryFacade::query()
155149
->when(
156-
$this->sites->isNotEmpty(),
157-
fn (Builder $query) => $query->whereIn('site', $this->sites->map->handle()->all())
150+
$this->sites()->isNotEmpty(),
151+
fn (Builder $query) => $query->whereIn('site', $this->sites()->map->handle()->all())
158152
)
159153
->whereIn('collection', $collections)
160154
->whereNotNull('uri')
@@ -190,7 +184,7 @@ protected function publishedTerms()
190184
return $taxonomy->cascade('seo') !== false
191185
? $taxonomy
192186
->queryTerms()
193-
->when($this->sites->isNotEmpty(), fn (Builder $query) => $query->whereIn('site', $this->sites->map->handle()->all()))
187+
->when($this->sites()->isNotEmpty(), fn (Builder $query) => $query->whereIn('site', $this->sites()->map->handle()->all()))
194188
->get()
195189
: collect();
196190
})
@@ -213,7 +207,7 @@ protected function publishedCollectionTerms()
213207
return $taxonomy->cascade('seo') !== false
214208
? $taxonomy
215209
->queryTerms()
216-
->when($this->sites->isNotEmpty(), fn (Builder $query) => $query->whereIn('site', $this->sites->map->handle()->all()))
210+
->when($this->sites()->isNotEmpty(), fn (Builder $query) => $query->whereIn('site', $this->sites()->map->handle()->all()))
217211
->get()->map->collection($taxonomy->collection())
218212
: collect();
219213
})

0 commit comments

Comments
 (0)