Skip to content

Commit 663b999

Browse files
Remove external domains from sitemap (#399)
Co-authored-by: Duncan McClean <duncan@duncanmcclean.com>
1 parent 913dbe9 commit 663b999

File tree

8 files changed

+137
-54
lines changed

8 files changed

+137
-54
lines changed

src/Http/Controllers/SitemapController.php

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

1013
class SitemapController extends Controller
1114
{
1215
public function index()
1316
{
1417
abort_unless(config('statamic.seo-pro.sitemap.enabled'), 404);
1518

19+
$key = request()->getHttpHost();
1620
$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()));
1722

1823
if (config('statamic.seo-pro.sitemap.pagination.enabled', false)) {
19-
$content = Cache::remember(Sitemap::CACHE_KEY.'_index', $cacheUntil, function () {
24+
$content = Cache::remember(Sitemap::CACHE_KEY.'_'.$key.'_index', $cacheUntil, function () use ($sites) {
2025
return view('seo-pro::sitemap_index', [
2126
'xml_header' => '<?xml version="1.0" encoding="UTF-8"?>',
22-
'sitemaps' => app(Sitemap::class)->paginatedSitemaps(),
27+
'sitemaps' => app(Sitemap::class)->forSites($sites)->paginatedSitemaps(),
2328
])->render();
2429
});
2530
} else {
26-
$content = Cache::remember(Sitemap::CACHE_KEY, $cacheUntil, function () {
31+
$content = Cache::remember(Sitemap::CACHE_KEY.'_'.$key, $cacheUntil, function () use ($sites) {
2732
return view('seo-pro::sitemap', [
2833
'xml_header' => '<?xml version="1.0" encoding="UTF-8"?>',
29-
'pages' => app(Sitemap::class)->pages(),
34+
'pages' => app(Sitemap::class)->forSites($sites)->pages(),
3035
])->render();
3136
});
3237
}

src/Sitemap/Sitemap.php

Lines changed: 21 additions & 2 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\Contracts\Entries\QueryBuilder;
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;
@@ -19,6 +20,13 @@ class Sitemap
1920

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

23+
private IlluminateCollection $sites;
24+
25+
public function __construct()
26+
{
27+
$this->sites = collect();
28+
}
29+
2230
public function pages(): array
2331
{
2432
return collect()
@@ -91,6 +99,13 @@ public function paginatedSitemaps(): array
9199
->all();
92100
}
93101

102+
public function forSites(IlluminateCollection $sites): self
103+
{
104+
$this->sites = $sites;
105+
106+
return $this;
107+
}
108+
94109
protected function getPages($items)
95110
{
96111
return $items
@@ -130,7 +145,11 @@ protected function publishedEntriesQuery()
130145
->values()
131146
->all();
132147

133-
return Entry::query()
148+
return EntryFacade::query()
149+
->when(
150+
$this->sites->isNotEmpty(),
151+
fn (QueryBuilder $query) => $query->whereIn('site', $this->sites->map->handle()->all())
152+
)
134153
->whereIn('collection', $collections)
135154
->whereNotNull('uri')
136155
->whereStatus('published')
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
default:
22
name: English
33
locale: en_US
4-
url: /
4+
url: http://cool-runnings.com
55
french:
66
name: French
77
locale: fr_FR
8-
url: /fr/
8+
url: http://cool-runnings.com/fr/
99
italian:
1010
name: Italian
1111
locale: it_IT
12-
url: /it/
12+
url: http://corse-fantastiche.it
1313
british:
1414
name: British
1515
locale: en_GB
16-
url: /en-gb/
16+
url: http://cool-runnings.com/en-gb/

tests/Localized/CascadeTest.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,9 @@
66
use Statamic\Facades\Entry;
77
use Statamic\SeoPro\Cascade;
88
use Statamic\SeoPro\SiteDefaults;
9-
use Tests\TestCase;
109

11-
class CascadeTest extends TestCase
10+
class CascadeTest extends LocalizedTestCase
1211
{
13-
protected $siteFixturePath = __DIR__.'/../Fixtures/site-localized';
14-
15-
protected function getEnvironmentSetUp($app)
16-
{
17-
parent::getEnvironmentSetUp($app);
18-
19-
$app['config']->set('statamic.editions.pro', true);
20-
$app['config']->set('statamic.system.multisite', true);
21-
}
22-
2312
#[Test]
2413
public function it_generates_seo_cascade_for_canonical_url_and_alternate_locales()
2514
{
@@ -30,7 +19,7 @@ public function it_generates_seo_cascade_for_canonical_url_and_alternate_locales
3019
->withCurrent($entry)
3120
->get();
3221

33-
$this->assertEquals('http://cool-runnings.com/it/about', $data['canonical_url']);
22+
$this->assertEquals('http://corse-fantastiche.it/about', $data['canonical_url']);
3423
$this->assertEquals('it', $data['current_hreflang']);
3524

3625
$this->assertEquals([
@@ -49,7 +38,7 @@ public function it_generates_seo_cascade_for_canonical_url_and_handles_duplicate
4938
->withCurrent($entry)
5039
->get();
5140

52-
$this->assertEquals('http://cool-runnings.com/it', $data['canonical_url']);
41+
$this->assertEquals('http://corse-fantastiche.it', $data['canonical_url']);
5342
$this->assertEquals('it', $data['current_hreflang']);
5443

5544
$this->assertEquals([
@@ -75,7 +64,7 @@ public function it_generates_seo_cascade_for_canonical_url_and_handles_duplicate
7564
$this->assertEquals([
7665
'en-gb' => 'http://cool-runnings.com/en-gb',
7766
'fr' => 'http://cool-runnings.com/fr',
78-
'it' => 'http://cool-runnings.com/it',
67+
'it' => 'http://corse-fantastiche.it',
7968
], collect($data['alternate_locales'])->pluck('url', 'hreflang')->all());
8069
}
8170
}

tests/Localized/GraphQLTest.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,13 @@
33
namespace Tests\Localized;
44

55
use PHPUnit\Framework\Attributes\Test;
6-
use Tests\TestCase;
76

8-
class GraphQLTest extends TestCase
7+
class GraphQLTest extends LocalizedTestCase
98
{
10-
protected $siteFixturePath = __DIR__.'/../Fixtures/site-localized';
11-
129
protected function getEnvironmentSetup($app)
1310
{
1411
parent::getEnvironmentSetUp($app);
1512

16-
$app['config']->set('statamic.editions.pro', true);
17-
$app['config']->set('statamic.system.multisite', true);
1813
$app['config']->set('statamic.graphql.enabled', true);
1914
$app['config']->set('statamic.graphql.resources.collections', true);
2015
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Tests\Localized;
4+
5+
use Tests\TestCase;
6+
7+
abstract class LocalizedTestCase extends TestCase
8+
{
9+
protected $siteFixturePath = __DIR__.'/../Fixtures/site-localized';
10+
11+
protected function getEnvironmentSetUp($app)
12+
{
13+
parent::getEnvironmentSetUp($app);
14+
15+
$app['config']->set('statamic.editions.pro', true);
16+
$app['config']->set('statamic.system.multisite', true);
17+
}
18+
}

tests/Localized/MetaTagTest.php

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,17 @@
77
use Statamic\Facades\Config;
88
use Statamic\Facades\Entry;
99
use Statamic\Facades\Site;
10-
use Tests\TestCase;
1110
use Tests\ViewScenarios;
1211

13-
class MetaTagTest extends TestCase
12+
class MetaTagTest extends LocalizedTestCase
1413
{
1514
use ViewScenarios;
1615

17-
protected $siteFixturePath = __DIR__.'/../Fixtures/site-localized';
18-
1916
protected function getEnvironmentSetUp($app)
2017
{
2118
parent::getEnvironmentSetUp($app);
2219

2320
$app['config']->set('view.paths', [$this->viewsPath()]);
24-
$app['config']->set('statamic.editions.pro', true);
25-
$app['config']->set('statamic.system.multisite', true);
2621
}
2722

2823
protected function tearDown(): void
@@ -48,7 +43,7 @@ public function it_generates_multisite_meta($viewType)
4843
$expectedAlternateHreflangMeta = <<<'EOT'
4944
<link rel="alternate" href="http://cool-runnings.com" hreflang="en-us" />
5045
<link rel="alternate" href="http://cool-runnings.com/fr" hreflang="fr" />
51-
<link rel="alternate" href="http://cool-runnings.com/it" hreflang="it" />
46+
<link rel="alternate" href="http://corse-fantastiche.it" hreflang="it" />
5247
<link rel="alternate" href="http://cool-runnings.com/en-gb" hreflang="en-gb" />
5348
EOT;
5449

@@ -74,7 +69,7 @@ public function it_generates_multisite_meta_for_non_home_page_route($viewType)
7469
$expectedAlternateHreflangMeta = <<<'EOT'
7570
<link rel="alternate" href="http://cool-runnings.com/about" hreflang="en" />
7671
<link rel="alternate" href="http://cool-runnings.com/fr/about" hreflang="fr" />
77-
<link rel="alternate" href="http://cool-runnings.com/it/about" hreflang="it" />
72+
<link rel="alternate" href="http://corse-fantastiche.it/about" hreflang="it" />
7873
EOT;
7974

8075
$content = $this->get('/about')->content();
@@ -104,24 +99,24 @@ public function it_generates_multisite_meta_for_canonical_url_and_alternate_loca
10499
$this->prepareViews($viewType);
105100

106101
$expectedOgLocaleMeta = <<<'EOT'
107-
<meta property="og:locale" content="it_IT" />
102+
<meta property="og:locale" content="fr_FR" />
108103
<meta property="og:locale:alternate" content="en_US" />
109-
<meta property="og:locale:alternate" content="fr_FR" />
104+
<meta property="og:locale:alternate" content="it_IT" />
110105
EOT;
111106

112107
$expectedAlternateHreflangMeta = <<<'EOT'
113-
<link href="http://cool-runnings.com/it/about" rel="canonical" />
114-
<link rel="alternate" href="http://cool-runnings.com/it/about" hreflang="it" />
115-
<link rel="alternate" href="http://cool-runnings.com/about" hreflang="en" />
108+
<link href="http://cool-runnings.com/fr/about" rel="canonical" />
116109
<link rel="alternate" href="http://cool-runnings.com/fr/about" hreflang="fr" />
110+
<link rel="alternate" href="http://cool-runnings.com/about" hreflang="en" />
111+
<link rel="alternate" href="http://corse-fantastiche.it/about" hreflang="it" />
117112
EOT;
118113

119114
// Though hitting a route will automatically set the current site,
120115
// we want to test that the alternate locales are generated off
121116
// the entry's model, not from the current site in the cp.
122117
Site::setCurrent('default');
123118

124-
$content = $this->get('/it/about')->content();
119+
$content = $this->get('/fr/about')->content();
125120

126121
$this->assertStringContainsStringIgnoringLineEndings("<h1>{$viewType}</h1>", $content);
127122
$this->assertStringContainsStringIgnoringLineEndings($expectedOgLocaleMeta, $content);
@@ -135,10 +130,10 @@ public function it_handles_duplicate_alternate_hreflangs($viewType)
135130
$this->prepareViews($viewType);
136131

137132
$expectedAlternateHreflangMeta = <<<'EOT'
138-
<link href="http://cool-runnings.com/it" rel="canonical" />
139-
<link rel="alternate" href="http://cool-runnings.com/it" hreflang="it" />
140-
<link rel="alternate" href="http://cool-runnings.com" hreflang="en-us" />
133+
<link href="http://cool-runnings.com/fr" rel="canonical" />
141134
<link rel="alternate" href="http://cool-runnings.com/fr" hreflang="fr" />
135+
<link rel="alternate" href="http://cool-runnings.com" hreflang="en-us" />
136+
<link rel="alternate" href="http://corse-fantastiche.it" hreflang="it" />
142137
<link rel="alternate" href="http://cool-runnings.com/en-gb" hreflang="en-gb" />
143138
EOT;
144139

@@ -147,7 +142,7 @@ public function it_handles_duplicate_alternate_hreflangs($viewType)
147142
// the entry's model, not from the current site in the cp.
148143
Site::setCurrent('default');
149144

150-
$content = $this->get('/it')->content();
145+
$content = $this->get('/fr')->content();
151146

152147
$this->assertStringContainsStringIgnoringLineEndings("<h1>{$viewType}</h1>", $content);
153148
$this->assertStringContainsStringIgnoringLineEndings($expectedAlternateHreflangMeta, $content);
@@ -164,7 +159,7 @@ public function it_handles_duplicate_current_hreflang($viewType)
164159
<link rel="alternate" href="http://cool-runnings.com/en-gb" hreflang="en-gb" />
165160
<link rel="alternate" href="http://cool-runnings.com" hreflang="en-us" />
166161
<link rel="alternate" href="http://cool-runnings.com/fr" hreflang="fr" />
167-
<link rel="alternate" href="http://cool-runnings.com/it" hreflang="it" />
162+
<link rel="alternate" href="http://corse-fantastiche.it" hreflang="it" />
168163
EOT;
169164

170165
// Though hitting a route will automatically set the current site,
@@ -208,7 +203,7 @@ public function it_doesnt_generate_multisite_meta_for_excluded_sites($viewType)
208203

209204
$expectedAlternateHreflangMeta = <<<'EOT'
210205
<link rel="alternate" href="http://cool-runnings.com/about" hreflang="en" />
211-
<link rel="alternate" href="http://cool-runnings.com/it/about" hreflang="it" />
206+
<link rel="alternate" href="http://corse-fantastiche.it/about" hreflang="it" />
212207
EOT;
213208

214209
$content = $this->get('/about')->content();
@@ -235,7 +230,7 @@ public function it_doesnt_generate_multisite_meta_for_unpublished_content($viewT
235230

236231
$expectedAlternateHreflangMeta = <<<'EOT'
237232
<link rel="alternate" href="http://cool-runnings.com/about" hreflang="en" />
238-
<link rel="alternate" href="http://cool-runnings.com/it/about" hreflang="it" />
233+
<link rel="alternate" href="http://corse-fantastiche.it/about" hreflang="it" />
239234
EOT;
240235

241236
$content = $this->get('/about')->content();
@@ -266,7 +261,7 @@ public function it_doesnt_generate_multisite_meta_for_scheduled_content($viewTyp
266261

267262
$expectedAlternateHreflangMeta = <<<'EOT'
268263
<link rel="alternate" href="http://cool-runnings.com/about" hreflang="en" />
269-
<link rel="alternate" href="http://cool-runnings.com/it/about" hreflang="it" />
264+
<link rel="alternate" href="http://corse-fantastiche.it/about" hreflang="it" />
270265
EOT;
271266

272267
$content = $this->get('/about')->content();
@@ -297,7 +292,7 @@ public function it_doesnt_generate_multisite_meta_for_expired_content($viewType)
297292

298293
$expectedAlternateHreflangMeta = <<<'EOT'
299294
<link rel="alternate" href="http://cool-runnings.com/about" hreflang="en" />
300-
<link rel="alternate" href="http://cool-runnings.com/it/about" hreflang="it" />
295+
<link rel="alternate" href="http://corse-fantastiche.it/about" hreflang="it" />
301296
EOT;
302297

303298
$content = $this->get('/about')->content();

0 commit comments

Comments
 (0)