Skip to content

Commit 15ec444

Browse files
committed
filter out incorrect domains
1 parent fd9e6ca commit 15ec444

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

src/Http/Controllers/SitemapController.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@
33
namespace Statamic\SeoPro\Http\Controllers;
44

55
use Carbon\Carbon;
6+
use Illuminate\Http\Request;
67
use Illuminate\Routing\Controller;
78
use Illuminate\Support\Facades\Cache;
89
use Statamic\SeoPro\Sitemap\Sitemap;
910

1011
class SitemapController extends Controller
1112
{
12-
public function index()
13+
public function index(Request $request)
1314
{
1415
abort_unless(config('statamic.seo-pro.sitemap.enabled'), 404);
1516

1617
$cacheUntil = Carbon::now()->addMinutes(config('statamic.seo-pro.sitemap.expire'));
18+
$domain = $request->schemeAndHttpHost();
1719

1820
if (config('statamic.seo-pro.sitemap.pagination.enabled', false)) {
1921
$content = Cache::remember(Sitemap::CACHE_KEY.'_index', $cacheUntil, function () {
@@ -23,10 +25,10 @@ public function index()
2325
])->render();
2426
});
2527
} else {
26-
$content = Cache::remember(Sitemap::CACHE_KEY, $cacheUntil, function () {
28+
$content = Cache::remember(Sitemap::CACHE_KEY.'_'.$domain, $cacheUntil, function () use ($domain) {
2729
return view('seo-pro::sitemap', [
2830
'xml_header' => '<?xml version="1.0" encoding="UTF-8"?>',
29-
'pages' => app(Sitemap::class)->pages(),
31+
'pages' => app(Sitemap::class)->forDomain($domain)->pages(),
3032
])->render();
3133
});
3234
}

src/Sitemap/Sitemap.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
use Illuminate\Support\Collection as IlluminateCollection;
66
use Illuminate\Support\LazyCollection;
7+
use Statamic\Entries\Entry;
78
use Statamic\Facades\Blink;
89
use Statamic\Facades\Collection;
9-
use Statamic\Facades\Entry;
10+
use Statamic\Facades\Entry as EntryFacade;
1011
use Statamic\Facades\Taxonomy;
1112
use Statamic\SeoPro\Cascade;
1213
use Statamic\SeoPro\GetsSectionDefaults;
@@ -18,6 +19,8 @@ class Sitemap
1819

1920
const CACHE_KEY = 'seo-pro.sitemap';
2021

22+
private ?string $domain = null;
23+
2124
public function pages(): array
2225
{
2326
return collect()
@@ -88,6 +91,13 @@ public function paginatedSitemaps(): array
8891
->all();
8992
}
9093

94+
public function forDomain(string $domain): self
95+
{
96+
$this->domain = $domain;
97+
98+
return $this;
99+
}
100+
91101
protected function getPages($items)
92102
{
93103
return $items
@@ -123,7 +133,7 @@ protected function publishedEntriesQuery()
123133
->values()
124134
->all();
125135

126-
return Entry::query()
136+
return EntryFacade::query()
127137
->whereIn('collection', $collections)
128138
->whereNotNull('uri')
129139
->whereStatus('published')
@@ -132,7 +142,15 @@ protected function publishedEntriesQuery()
132142

133143
protected function publishedEntries(): LazyCollection
134144
{
135-
return $this->publishedEntriesQuery()->lazy();
145+
return $this
146+
->publishedEntriesQuery()
147+
->lazy()
148+
->unless(
149+
is_null($this->domain),
150+
fn (LazyCollection $c) => $c->filter(
151+
fn (Entry $entry) => str($entry->permalink)->startsWith($this->domain)
152+
)
153+
);
136154
}
137155

138156
protected function publishedEntriesForPage(int $page, int $perPage): IlluminateCollection

0 commit comments

Comments
 (0)