From ab454e787ae822f80152f27eee79c337d7a8cc53 Mon Sep 17 00:00:00 2001 From: Michael Aerni Date: Mon, 24 Jun 2024 12:07:11 -0400 Subject: [PATCH] Add `@seo` Blade directive (#152) --- src/Data/DefaultsData.php | 3 +- src/ServiceProvider.php | 39 +++++++++---------- src/Sitemap/CollectionSitemap.php | 4 +- src/Sitemap/CollectionSitemapUrl.php | 4 +- src/Sitemap/CollectionTaxonomySitemapUrl.php | 4 +- src/Sitemap/CollectionTermSitemapUrl.php | 4 +- src/Sitemap/CustomSitemapUrl.php | 4 +- src/Sitemap/TaxonomySitemap.php | 4 +- src/Sitemap/TermSitemapUrl.php | 4 +- src/Tags/AdvancedSeoDirective.php | 16 ++++++++ src/Tags/AdvancedSeoTags.php | 12 +++++- .../CascadeComposer.php} | 38 ++++++++++++++++-- 12 files changed, 88 insertions(+), 48 deletions(-) create mode 100644 src/Tags/AdvancedSeoDirective.php rename src/{Actions/ShouldProcessViewCascade.php => View/CascadeComposer.php} (55%) diff --git a/src/Data/DefaultsData.php b/src/Data/DefaultsData.php index dacaaf05..9b0c3387 100644 --- a/src/Data/DefaultsData.php +++ b/src/Data/DefaultsData.php @@ -11,8 +11,7 @@ public function __construct( public string $handle, public ?string $locale = null, public ?Collection $sites = null, - ) { - } + ) {} public function id(): string { diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 726330cc..b7d17593 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -2,7 +2,6 @@ namespace Aerni\AdvancedSeo; -use Aerni\AdvancedSeo\Actions\ShouldProcessViewCascade; use Aerni\AdvancedSeo\Data\SeoVariables; use Aerni\AdvancedSeo\GraphQL\Fields\SeoField; use Aerni\AdvancedSeo\GraphQL\Queries\SeoDefaultsQuery; @@ -27,8 +26,9 @@ use Aerni\AdvancedSeo\GraphQL\Types\SocialMediaDefaultsType; use Aerni\AdvancedSeo\Models\Defaults; use Aerni\AdvancedSeo\Stache\SeoStore; -use Aerni\AdvancedSeo\View\ViewCascade; +use Aerni\AdvancedSeo\View\CascadeComposer; use Illuminate\Support\Arr; +use Illuminate\Support\Facades\Blade; use Illuminate\Support\Facades\View; use Statamic\Facades\CP\Nav; use Statamic\Facades\Git; @@ -41,7 +41,6 @@ use Statamic\Providers\AddonServiceProvider; use Statamic\Stache\Stache; use Statamic\Statamic; -use Statamic\Tags\Context; class ServiceProvider extends AddonServiceProvider { @@ -110,16 +109,17 @@ class ServiceProvider extends AddonServiceProvider public function bootAddon(): void { $this - ->bootAddonStores() - ->bootAddonNav() - ->bootAddonPermissions() + ->bootStores() + ->bootNav() + ->bootPermissions() ->bootGit() - ->bootCascade() + ->bootViewCascade() + ->bootBladeDirective() ->bootGraphQL() ->autoPublishConfig(); } - protected function bootAddonStores(): self + protected function bootStores(): self { $seoStore = app(SeoStore::class)->directory(config('advanced-seo.directory')); @@ -128,7 +128,7 @@ protected function bootAddonStores(): self return $this; } - protected function bootAddonNav(): self + protected function bootNav(): self { Nav::extend(function ($nav) { Defaults::enabled() @@ -154,7 +154,7 @@ protected function bootAddonNav(): self return $this; } - protected function bootAddonPermissions(): self + protected function bootPermissions(): self { Permission::extend(function () { Permission::group('advanced-seo', 'Advanced SEO', function () { @@ -195,23 +195,22 @@ protected function bootGit(): self return $this; } - protected function bootCascade(): self + protected function bootViewCascade(): self { - $views = [ + View::composer([ ...Arr::wrap(config('advanced-seo.view_composer', '*')), 'advanced-seo::head', 'advanced-seo::body', 'social_images.*', - ]; + ], CascadeComposer::class); - View::composer($views, function ($view) { - $data = new Context($view->getData()); - - if (! ShouldProcessViewCascade::handle($data)) { - return; - } + return $this; + } - $view->with('seo', ViewCascade::from($data)->toAugmentedArray()); + protected function bootBladeDirective(): self + { + Blade::directive('seo', function ($tag) { + return ""; }); return $this; diff --git a/src/Sitemap/CollectionSitemap.php b/src/Sitemap/CollectionSitemap.php index 6adede40..d9c1b39a 100644 --- a/src/Sitemap/CollectionSitemap.php +++ b/src/Sitemap/CollectionSitemap.php @@ -8,9 +8,7 @@ class CollectionSitemap extends BaseSitemap { - public function __construct(protected EntriesCollection $model) - { - } + public function __construct(protected EntriesCollection $model) {} public function urls(): Collection { diff --git a/src/Sitemap/CollectionSitemapUrl.php b/src/Sitemap/CollectionSitemapUrl.php index 7db29308..b36854b1 100644 --- a/src/Sitemap/CollectionSitemapUrl.php +++ b/src/Sitemap/CollectionSitemapUrl.php @@ -10,9 +10,7 @@ class CollectionSitemapUrl extends BaseSitemapUrl { - public function __construct(protected Entry $entry, protected CollectionSitemap $sitemap) - { - } + public function __construct(protected Entry $entry, protected CollectionSitemap $sitemap) {} public function loc(): string { diff --git a/src/Sitemap/CollectionTaxonomySitemapUrl.php b/src/Sitemap/CollectionTaxonomySitemapUrl.php index a071a1e5..5c534d46 100644 --- a/src/Sitemap/CollectionTaxonomySitemapUrl.php +++ b/src/Sitemap/CollectionTaxonomySitemapUrl.php @@ -12,9 +12,7 @@ class CollectionTaxonomySitemapUrl extends BaseSitemapUrl { - public function __construct(protected Taxonomy $taxonomy, protected string $site, protected TaxonomySitemap $sitemap) - { - } + public function __construct(protected Taxonomy $taxonomy, protected string $site, protected TaxonomySitemap $sitemap) {} public function loc(): string { diff --git a/src/Sitemap/CollectionTermSitemapUrl.php b/src/Sitemap/CollectionTermSitemapUrl.php index b58a09f2..eac44888 100644 --- a/src/Sitemap/CollectionTermSitemapUrl.php +++ b/src/Sitemap/CollectionTermSitemapUrl.php @@ -9,9 +9,7 @@ class CollectionTermSitemapUrl extends BaseSitemapUrl { - public function __construct(protected Term $term, protected TaxonomySitemap $sitemap) - { - } + public function __construct(protected Term $term, protected TaxonomySitemap $sitemap) {} public function loc(): string { diff --git a/src/Sitemap/CustomSitemapUrl.php b/src/Sitemap/CustomSitemapUrl.php index d60d1b7f..cf380a38 100644 --- a/src/Sitemap/CustomSitemapUrl.php +++ b/src/Sitemap/CustomSitemapUrl.php @@ -11,9 +11,7 @@ class CustomSitemapUrl extends BaseSitemapUrl { use FluentlyGetsAndSets; - public function __construct(protected string $loc) - { - } + public function __construct(protected string $loc) {} public function loc(?string $loc = null): string|self { diff --git a/src/Sitemap/TaxonomySitemap.php b/src/Sitemap/TaxonomySitemap.php index f0a3f27d..18f2b84c 100644 --- a/src/Sitemap/TaxonomySitemap.php +++ b/src/Sitemap/TaxonomySitemap.php @@ -8,9 +8,7 @@ class TaxonomySitemap extends BaseSitemap { - public function __construct(protected Taxonomy $model) - { - } + public function __construct(protected Taxonomy $model) {} public function urls(): Collection { diff --git a/src/Sitemap/TermSitemapUrl.php b/src/Sitemap/TermSitemapUrl.php index 06b00308..d211a901 100644 --- a/src/Sitemap/TermSitemapUrl.php +++ b/src/Sitemap/TermSitemapUrl.php @@ -9,9 +9,7 @@ class TermSitemapUrl extends BaseSitemapUrl { - public function __construct(protected Term $term, protected TaxonomySitemap $sitemap) - { - } + public function __construct(protected Term $term, protected TaxonomySitemap $sitemap) {} public function loc(): string { diff --git a/src/Tags/AdvancedSeoDirective.php b/src/Tags/AdvancedSeoDirective.php new file mode 100644 index 00000000..ad5284ba --- /dev/null +++ b/src/Tags/AdvancedSeoDirective.php @@ -0,0 +1,16 @@ +tags->setContext($context)->$tag(); + } +} diff --git a/src/Tags/AdvancedSeoTags.php b/src/Tags/AdvancedSeoTags.php index 2cbc20d7..f2a3c976 100755 --- a/src/Tags/AdvancedSeoTags.php +++ b/src/Tags/AdvancedSeoTags.php @@ -23,16 +23,24 @@ public function wildcard() /** * Renders the head view. */ - public function head(): View + public function head(): ?View { + if (! $this->context->has('seo')) { + return null; + } + return view('advanced-seo::head', $this->context->all()); } /** * Renders the body view. */ - public function body(): View + public function body(): ?View { + if (! $this->context->has('seo')) { + return null; + } + return view('advanced-seo::body', $this->context->all()); } diff --git a/src/Actions/ShouldProcessViewCascade.php b/src/View/CascadeComposer.php similarity index 55% rename from src/Actions/ShouldProcessViewCascade.php rename to src/View/CascadeComposer.php index f39500c0..9fdadedd 100644 --- a/src/Actions/ShouldProcessViewCascade.php +++ b/src/View/CascadeComposer.php @@ -1,13 +1,45 @@ getData()); + + if (! $context->has('current_template')) { + $context = $this->getContextFromCascade($context); + } + + if (! $this->shouldProcessCascade($context)) { + return; + } + + $view->with('seo', ViewCascade::from($context)); + } + + protected function getContextFromCascade(Context $context): Context + { + $cascade = Cascade::instance(); + + /** + * If the cascade has not yet been hydrated, ensure it is hydrated. + * This is important for people using custom route/controller/view implementations. + */ + if (empty($cascade->toArray())) { + $cascade->hydrate(); + } + + return $context->merge($cascade->toArray()); + } + + protected function shouldProcessCascade(Context $context): bool { // Don't process the cascade if it has been processed before. if ($context->has('seo')) {