Skip to content

Commit 4565e0b

Browse files
authored
Multisite improvements (#193)
* Pass `site` instead of `locale`, so that we have access to both long and short locale strings. * Rework `alternate_locales` config to accept `excluded_sites`. * Pass down augmented `site` with each alternate locale, while respecting configured excludes. * Use passed `site` instead of old `locale` var. * Update tests. * Add `assertArraySubset` assertion. * Assert array subset, instead of exact array. * Setup localized site fixture. * Add `Localized/TestCase` to run on localized site fixture. * Pass tests again. * Add test coverage for localized meta generation.
1 parent 8c066e0 commit 4565e0b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+776
-39
lines changed

config/seo-pro.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
'card' => 'summary_large_image',
3535
],
3636

37-
'alternate_locales' => true,
37+
'alternate_locales' => [
38+
'enabled' => true,
39+
'excluded_sites' => [],
40+
],
3841

3942
];

resources/views/meta.antlers.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626
<meta property="og:site_name" content="{{ site_name | striptags | entities | trim }}" />
2727
{{ /if }}
2828

29-
<meta property="og:locale" content="{{ locale }}" />
29+
<meta property="og:locale" content="{{ site:locale }}" />
3030

3131
{{ if alternate_locales }}
3232
{{ alternate_locales }}
33-
<meta property="og:locale:alternate" content="{{ locale }}" />
33+
<meta property="og:locale:alternate" content="{{ site:locale }}" />
3434
{{ /alternate_locales }}
3535
{{ /if }}
3636

@@ -88,9 +88,9 @@
8888
{{ /if }}
8989

9090
{{ if alternate_locales }}
91-
<link rel="alternate" href="{{ canonical_url }}" hreflang="{{ locale }}" />
91+
<link rel="alternate" href="{{ canonical_url }}" hreflang="{{ site:short_locale }}" />
9292
{{ alternate_locales }}
93-
<link rel="alternate" href="{{ url }}" hreflang="{{ locale }}" />
93+
<link rel="alternate" href="{{ url }}" hreflang="{{ site:short_locale }}" />
9494
{{ /alternate_locales }}
9595
{{ /if }}
9696

src/Cascade.php

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function get()
8080
'next_url' => $this->nextUrl(),
8181
'home_url' => URL::makeAbsolute('/'),
8282
'humans_txt' => $this->humans(),
83-
'locale' => $this->locale(),
83+
'site' => $this->site(),
8484
'alternate_locales' => $this->alternateLocales(),
8585
'last_modified' => $this->lastModified(),
8686
'twitter_card' => config('statamic.seo-pro.twitter.card'),
@@ -259,37 +259,39 @@ protected function lastModified()
259259
: null;
260260
}
261261

262-
protected function locale()
262+
protected function site()
263263
{
264-
return method_exists($this->model, 'locale')
265-
? Config::getShortLocale($this->model->locale())
266-
: Site::default()->handle();
264+
$site = method_exists($this->model, 'site')
265+
? $this->model->site()
266+
: Site::default();
267+
268+
return $site->toAugmentedArray();
267269
}
268270

269271
protected function alternateLocales()
270272
{
271-
if (! config('statamic.seo-pro.alternate_locales')) {
273+
if (config('statamic.seo-pro.alternate_locales') === false) {
272274
return [];
273-
}
274-
275-
if (! $this->model) {
275+
} elseif (config('statamic.seo-pro.alternate_locales.enabled') === false) {
276+
return [];
277+
} elseif (! $this->model) {
276278
return [];
277279
}
278280

279-
if (! method_exists($this->model, 'locales')) {
280-
return collect(Config::getOtherLocales())->map(function ($locale) {
281-
return ['locale' => $locale, 'url' => $this->model->absoluteUrl()];
282-
})->all();
283-
}
284-
285-
$alternates = array_values(array_diff($this->model->locales(), [$this->model->locale()]));
286-
287-
return collect($alternates)->map(function ($locale) {
288-
return [
289-
'locale' => Config::getShortLocale($locale),
290-
'url' => $this->model->in($locale)->absoluteUrl(),
291-
];
292-
})->all();
281+
return collect(Config::getOtherLocales())
282+
->filter(function ($locale) {
283+
return $this->model->in($locale);
284+
})
285+
->reject(function ($locale) {
286+
return collect(config('statamic.seo-pro.alternate_locales.excluded_sites'))->contains($locale);
287+
})
288+
->map(function ($locale) {
289+
return [
290+
'site' => Config::getSite($locale)->toAugmentedArray(),
291+
'url' => $this->model->in($locale)->absoluteUrl(),
292+
];
293+
})
294+
->all();
293295
}
294296

295297
protected function parseDescriptionField($value)

tests/CascadeTest.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,19 @@ public function it_generates_seo_cascade_from_site_defaults_and_home_entry()
3030
'next_url' => null,
3131
'home_url' => 'http://cool-runnings.com/',
3232
'humans_txt' => 'http://cool-runnings.com/humans.txt',
33-
'locale' => 'default',
33+
'site' => [
34+
'handle' => 'default',
35+
'name' => 'English',
36+
'locale' => 'en_US',
37+
'short_locale' => 'en',
38+
'url' => '/',
39+
],
3440
'alternate_locales' => [],
3541
'last_modified' => null,
3642
'twitter_card' => 'summary_large_image',
3743
];
3844

39-
$this->assertEquals($expected, $data);
45+
$this->assertArraySubset($expected, $data);
4046
}
4147

4248
/** @test */
@@ -68,13 +74,19 @@ public function it_overwrites_data_in_cascade()
6874
'next_url' => null,
6975
'home_url' => 'http://cool-runnings.com/',
7076
'humans_txt' => 'http://cool-runnings.com/humans.txt',
71-
'locale' => 'default',
77+
'site' => [
78+
'handle' => 'default',
79+
'name' => 'English',
80+
'locale' => 'en_US',
81+
'short_locale' => 'en',
82+
'url' => '/',
83+
],
7284
'alternate_locales' => [],
7385
'last_modified' => null,
7486
'twitter_card' => 'summary_large_image',
7587
];
7688

77-
$this->assertEquals($expected, $data);
89+
$this->assertArraySubset($expected, $data);
7890
}
7991

8092
/** @test */
@@ -138,12 +150,18 @@ public function it_generates_seo_cascade_without_exception_when_no_home_entry_ex
138150
'next_url' => null,
139151
'home_url' => 'http://cool-runnings.com/',
140152
'humans_txt' => 'http://cool-runnings.com/humans.txt',
141-
'locale' => 'default',
153+
'site' => [
154+
'handle' => 'default',
155+
'name' => 'English',
156+
'locale' => 'en_US',
157+
'short_locale' => 'en',
158+
'url' => '/',
159+
],
142160
'alternate_locales' => [],
143161
'last_modified' => null,
144162
'twitter_card' => 'summary_large_image',
145163
];
146164

147-
$this->assertEquals($expected, $data);
165+
$this->assertArraySubset($expected, $data);
148166
}
149167
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
return [
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| Sites
8+
|--------------------------------------------------------------------------
9+
|
10+
| Each site should have root URL that is either relative or absolute. Sites
11+
| are typically used for localization (eg. English/French) but may also
12+
| be used for related content (eg. different franchise locations).
13+
|
14+
*/
15+
16+
'sites' => [
17+
18+
'default' => [
19+
'name' => 'English',
20+
'locale' => 'en_US',
21+
'url' => '/',
22+
],
23+
24+
'french' => [
25+
'name' => 'French',
26+
'locale' => 'fr_FR',
27+
'url' => '/fr/',
28+
],
29+
30+
'italian' => [
31+
'name' => 'Italian',
32+
'locale' => 'it_IT',
33+
'url' => '/it/',
34+
],
35+
36+
],
37+
];
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)