From 381784b89dfa9481b5841a2cc0fc1fb249418458 Mon Sep 17 00:00:00 2001 From: Michael Aerni Date: Fri, 23 Feb 2024 18:16:21 -0500 Subject: [PATCH 1/8] Add site tag --- src/Providers/ExtensionServiceProvider.php | 1 + src/Tags/Site.php | 25 ++++++++++ tests/Tags/SiteTagTest.php | 58 ++++++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 src/Tags/Site.php create mode 100644 tests/Tags/SiteTagTest.php diff --git a/src/Providers/ExtensionServiceProvider.php b/src/Providers/ExtensionServiceProvider.php index 15636477c4..97c91dbb5b 100644 --- a/src/Providers/ExtensionServiceProvider.php +++ b/src/Providers/ExtensionServiceProvider.php @@ -188,6 +188,7 @@ class ExtensionServiceProvider extends ServiceProvider Tags\Set::class, Tags\Section::class, Tags\Session::class, + Tags\Site::class, Tags\Structure::class, Tags\Svg::class, Tags\Taxonomy\Taxonomy::class, diff --git a/src/Tags/Site.php b/src/Tags/Site.php new file mode 100644 index 0000000000..9939d5254a --- /dev/null +++ b/src/Tags/Site.php @@ -0,0 +1,25 @@ +context->get('sites')->firstWhere('handle', $handle); + + if (! $site) { + return null; + } + + $data = $site->toAugmentedCollection(); + + return Str::contains($key, ':') + ? $data->get(Str::after($key, ':')) + : $data; + } +} diff --git a/tests/Tags/SiteTagTest.php b/tests/Tags/SiteTagTest.php new file mode 100644 index 0000000000..8966f8eaaa --- /dev/null +++ b/tests/Tags/SiteTagTest.php @@ -0,0 +1,58 @@ + [ + 'english' => ['name' => 'English', 'locale' => 'en_US', 'url' => '/en'], + 'french' => ['name' => 'French', 'locale' => 'fr_FR', 'url' => '/fr'], + ]]); + + $context = [ + 'site' => Site::get('english'), + 'sites' => Site::all()->values(), + ]; + + $this->assertEquals( + 'English', + Antlers::parse('{{ site:english:name }}', $context) + ); + + $this->assertEquals( + 'fr_FR', + Antlers::parse('{{ site:french }}{{ locale }}{{ /site:french }}', $context) + ); + + $this->assertEquals( + '', + Antlers::parse('{{ site:nonexistend_site:name }}', $context) + ); + } + + /** @test */ + public function it_wont_conflict_with_site_context() + { + Site::setConfig(['sites' => [ + 'english' => ['name' => 'English', 'locale' => 'en_US', 'url' => '/en'], + 'french' => ['name' => 'French', 'locale' => 'fr_FR', 'url' => '/fr'], + ]]); + + $context = [ + 'site' => Site::get('french'), + 'sites' => Site::all()->values(), + ]; + + $this->assertEquals( + 'French', + Antlers::parse('{{ site:name }}', $context) + ); + } +} From 0d0b575d650994e32b0fa74944f1c41538cb7b8e Mon Sep 17 00:00:00 2001 From: Michael Aerni Date: Sat, 24 Feb 2024 13:16:46 -0500 Subject: [PATCH 2/8] Rename and refactor tag --- src/Providers/ExtensionServiceProvider.php | 2 +- src/Tags/GetSite.php | 39 ++++++++++++++ src/Tags/Site.php | 25 --------- tests/Tags/GetSiteTagTest.php | 59 ++++++++++++++++++++++ tests/Tags/SiteTagTest.php | 58 --------------------- 5 files changed, 99 insertions(+), 84 deletions(-) create mode 100644 src/Tags/GetSite.php delete mode 100644 src/Tags/Site.php create mode 100644 tests/Tags/GetSiteTagTest.php delete mode 100644 tests/Tags/SiteTagTest.php diff --git a/src/Providers/ExtensionServiceProvider.php b/src/Providers/ExtensionServiceProvider.php index 97c91dbb5b..808d6276dc 100644 --- a/src/Providers/ExtensionServiceProvider.php +++ b/src/Providers/ExtensionServiceProvider.php @@ -160,6 +160,7 @@ class ExtensionServiceProvider extends ServiceProvider Tags\GetError::class, Tags\GetErrors::class, Tags\GetFiles::class, + Tags\GetSite::class, Tags\Glide::class, Tags\In::class, Tags\Increment::class, @@ -188,7 +189,6 @@ class ExtensionServiceProvider extends ServiceProvider Tags\Set::class, Tags\Section::class, Tags\Session::class, - Tags\Site::class, Tags\Structure::class, Tags\Svg::class, Tags\Taxonomy\Taxonomy::class, diff --git a/src/Tags/GetSite.php b/src/Tags/GetSite.php new file mode 100644 index 0000000000..0a1c50427b --- /dev/null +++ b/src/Tags/GetSite.php @@ -0,0 +1,39 @@ +toAugmentedCollection(); + + return Str::contains($tag, ':') + ? $data->get(Str::after($tag, ':')) + : $data; + } + + /** + * {{ get_site handle="" }} ... {{ /get_site }}. + */ + public function index() + { + if (! $handle = $this->params->get('handle')) { + throw new \Exception("Please set the handle of a site"); + } + + return $this->wildcard($handle); + } +} diff --git a/src/Tags/Site.php b/src/Tags/Site.php deleted file mode 100644 index 9939d5254a..0000000000 --- a/src/Tags/Site.php +++ /dev/null @@ -1,25 +0,0 @@ -context->get('sites')->firstWhere('handle', $handle); - - if (! $site) { - return null; - } - - $data = $site->toAugmentedCollection(); - - return Str::contains($key, ':') - ? $data->get(Str::after($key, ':')) - : $data; - } -} diff --git a/tests/Tags/GetSiteTagTest.php b/tests/Tags/GetSiteTagTest.php new file mode 100644 index 0000000000..4878ef4614 --- /dev/null +++ b/tests/Tags/GetSiteTagTest.php @@ -0,0 +1,59 @@ + [ + 'english' => ['name' => 'English', 'locale' => 'en_US', 'url' => '/en'], + 'french' => ['name' => 'French', 'locale' => 'fr_FR', 'url' => '/fr'], + ]]); + } + + /** @test */ + public function it_gets_site_by_handle() + { + $this->assertEquals( + 'English', + Antlers::parse('{{ get_site handle="english" }}{{ name }}{{ /get_site }}') + ); + + $this->assertEquals( + 'French', + Antlers::parse('{{ get_site:french }}{{ name }}{{ /get_site:french }}') + ); + } + + /** @test */ + public function it_can_be_used_as_single_tag() + { + $this->assertEquals( + 'en_US', + Antlers::parse('{{ get_site:english:locale }}') + ); + } + + /** @test */ + public function it_throws_exception_if_handle_is_missing() + { + $this->expectExceptionMessage("Please set the handle of a site"); + + Antlers::parse('{{ get_site }}{{ name }}{{ /get_site }}'); + } + + /** @test */ + public function it_throws_exception_if_site_doesnt_exist() + { + $this->expectExceptionMessage("Site [nonexistent] does not exist."); + + Antlers::parse('{{ get_site handle="nonexistent" }}{{ name }}{{ /get_site }}'); + } +} diff --git a/tests/Tags/SiteTagTest.php b/tests/Tags/SiteTagTest.php deleted file mode 100644 index 8966f8eaaa..0000000000 --- a/tests/Tags/SiteTagTest.php +++ /dev/null @@ -1,58 +0,0 @@ - [ - 'english' => ['name' => 'English', 'locale' => 'en_US', 'url' => '/en'], - 'french' => ['name' => 'French', 'locale' => 'fr_FR', 'url' => '/fr'], - ]]); - - $context = [ - 'site' => Site::get('english'), - 'sites' => Site::all()->values(), - ]; - - $this->assertEquals( - 'English', - Antlers::parse('{{ site:english:name }}', $context) - ); - - $this->assertEquals( - 'fr_FR', - Antlers::parse('{{ site:french }}{{ locale }}{{ /site:french }}', $context) - ); - - $this->assertEquals( - '', - Antlers::parse('{{ site:nonexistend_site:name }}', $context) - ); - } - - /** @test */ - public function it_wont_conflict_with_site_context() - { - Site::setConfig(['sites' => [ - 'english' => ['name' => 'English', 'locale' => 'en_US', 'url' => '/en'], - 'french' => ['name' => 'French', 'locale' => 'fr_FR', 'url' => '/fr'], - ]]); - - $context = [ - 'site' => Site::get('french'), - 'sites' => Site::all()->values(), - ]; - - $this->assertEquals( - 'French', - Antlers::parse('{{ site:name }}', $context) - ); - } -} From 1ab91463d8dadf49629686be04d74563d6c356bf Mon Sep 17 00:00:00 2001 From: Michael Aerni Date: Sat, 24 Feb 2024 13:23:46 -0500 Subject: [PATCH 3/8] Fix linting issues --- tests/Tags/GetSiteTagTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Tags/GetSiteTagTest.php b/tests/Tags/GetSiteTagTest.php index 4878ef4614..8e40b80264 100644 --- a/tests/Tags/GetSiteTagTest.php +++ b/tests/Tags/GetSiteTagTest.php @@ -44,7 +44,7 @@ public function it_can_be_used_as_single_tag() /** @test */ public function it_throws_exception_if_handle_is_missing() { - $this->expectExceptionMessage("Please set the handle of a site"); + $this->expectExceptionMessage('Please set the handle of a site'); Antlers::parse('{{ get_site }}{{ name }}{{ /get_site }}'); } @@ -52,7 +52,7 @@ public function it_throws_exception_if_handle_is_missing() /** @test */ public function it_throws_exception_if_site_doesnt_exist() { - $this->expectExceptionMessage("Site [nonexistent] does not exist."); + $this->expectExceptionMessage('Site [nonexistent] does not exist.'); Antlers::parse('{{ get_site handle="nonexistent" }}{{ name }}{{ /get_site }}'); } From e9bfb4096de9e85398cf627676dbc6d31a9ffdb1 Mon Sep 17 00:00:00 2001 From: Michael Aerni Date: Sat, 24 Feb 2024 13:26:51 -0500 Subject: [PATCH 4/8] Fix more linting issues --- src/Tags/GetSite.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Tags/GetSite.php b/src/Tags/GetSite.php index 0a1c50427b..c591b8e58b 100644 --- a/src/Tags/GetSite.php +++ b/src/Tags/GetSite.php @@ -15,7 +15,7 @@ public function wildcard($tag) $handle = Str::before($tag, ':'); if (! $site = Site::get($handle)) { - throw new \Exception("Site [$handle] does not exist."); + throw new \Exception('Site [$handle] does not exist.'); } $data = $site->toAugmentedCollection(); @@ -31,7 +31,7 @@ public function wildcard($tag) public function index() { if (! $handle = $this->params->get('handle')) { - throw new \Exception("Please set the handle of a site"); + throw new \Exception('Please set the handle of a site'); } return $this->wildcard($handle); From 07f1f89c6202275942416ada182d7542f8404eaf Mon Sep 17 00:00:00 2001 From: Michael Aerni Date: Sat, 24 Feb 2024 13:32:27 -0500 Subject: [PATCH 5/8] Figured out how to run Pint locally --- src/Tags/GetSite.php | 4 ++-- tests/Tags/GetSiteTagTest.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Tags/GetSite.php b/src/Tags/GetSite.php index c591b8e58b..30dd0e68b5 100644 --- a/src/Tags/GetSite.php +++ b/src/Tags/GetSite.php @@ -2,8 +2,8 @@ namespace Statamic\Tags; -use Statamic\Facades\Site; use Illuminate\Support\Str; +use Statamic\Facades\Site; class GetSite extends Tags { @@ -15,7 +15,7 @@ public function wildcard($tag) $handle = Str::before($tag, ':'); if (! $site = Site::get($handle)) { - throw new \Exception('Site [$handle] does not exist.'); + throw new \Exception("Site [$handle] does not exist."); } $data = $site->toAugmentedCollection(); diff --git a/tests/Tags/GetSiteTagTest.php b/tests/Tags/GetSiteTagTest.php index 8e40b80264..9cdc9c5cd6 100644 --- a/tests/Tags/GetSiteTagTest.php +++ b/tests/Tags/GetSiteTagTest.php @@ -2,9 +2,9 @@ namespace Tests\Tags; -use Tests\TestCase; -use Statamic\Facades\Site; use Statamic\Facades\Antlers; +use Statamic\Facades\Site; +use Tests\TestCase; class GetSiteTagTest extends TestCase { From 40db70ea045bd6c3218b9b2641075db604217478 Mon Sep 17 00:00:00 2001 From: Michael Aerni Date: Sat, 24 Feb 2024 14:02:04 -0500 Subject: [PATCH 6/8] Fix edge case MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ensure that we can’t access a variable of a site through the handle parameter like `{{ get_site handle="german:permalink" }}` --- src/Tags/GetSite.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Tags/GetSite.php b/src/Tags/GetSite.php index 30dd0e68b5..298164d462 100644 --- a/src/Tags/GetSite.php +++ b/src/Tags/GetSite.php @@ -34,6 +34,10 @@ public function index() throw new \Exception('Please set the handle of a site'); } - return $this->wildcard($handle); + if (! $site = Site::get($handle)) { + throw new \Exception("Site [$handle] does not exist."); + } + + return $site->toAugmentedCollection(); } } From 9f0636b6961a15ccf6da221d1ed77c31aba4302d Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Mon, 20 May 2024 15:46:05 -0400 Subject: [PATCH 7/8] wording --- src/Tags/GetSite.php | 2 +- tests/Tags/GetSiteTagTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Tags/GetSite.php b/src/Tags/GetSite.php index 298164d462..7897ae7435 100644 --- a/src/Tags/GetSite.php +++ b/src/Tags/GetSite.php @@ -31,7 +31,7 @@ public function wildcard($tag) public function index() { if (! $handle = $this->params->get('handle')) { - throw new \Exception('Please set the handle of a site'); + throw new \Exception('A site handle is required.'); } if (! $site = Site::get($handle)) { diff --git a/tests/Tags/GetSiteTagTest.php b/tests/Tags/GetSiteTagTest.php index 9cdc9c5cd6..7cc1864dcd 100644 --- a/tests/Tags/GetSiteTagTest.php +++ b/tests/Tags/GetSiteTagTest.php @@ -44,7 +44,7 @@ public function it_can_be_used_as_single_tag() /** @test */ public function it_throws_exception_if_handle_is_missing() { - $this->expectExceptionMessage('Please set the handle of a site'); + $this->expectExceptionMessage('A site handle is required.'); Antlers::parse('{{ get_site }}{{ name }}{{ /get_site }}'); } From 60fff5993203d2398be9058fba13a12890ba709e Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Mon, 20 May 2024 16:06:10 -0400 Subject: [PATCH 8/8] nitpick --- src/Tags/GetSite.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Tags/GetSite.php b/src/Tags/GetSite.php index 7897ae7435..82ac0bb72c 100644 --- a/src/Tags/GetSite.php +++ b/src/Tags/GetSite.php @@ -18,11 +18,9 @@ public function wildcard($tag) throw new \Exception("Site [$handle] does not exist."); } - $data = $site->toAugmentedCollection(); - - return Str::contains($tag, ':') - ? $data->get(Str::after($tag, ':')) - : $data; + return (Str::contains($tag, ':') && ($var = Str::after($tag, ':'))) + ? $site->$var + : $site; } /** @@ -38,6 +36,6 @@ public function index() throw new \Exception("Site [$handle] does not exist."); } - return $site->toAugmentedCollection(); + return $site; } }