From e9eb8efae969e0cd93ccbfb3bb024657b6c09cbd Mon Sep 17 00:00:00 2001 From: Jesse Leite Date: Tue, 4 Aug 2020 15:02:01 -0400 Subject: [PATCH] Clean up SEO handling when disabled for section. --- src/Blueprint.php | 2 +- src/Subscriber.php | 43 ++++++++++++------------------------------- 2 files changed, 13 insertions(+), 32 deletions(-) diff --git a/src/Blueprint.php b/src/Blueprint.php index e112b7a..e0dfcdf 100644 --- a/src/Blueprint.php +++ b/src/Blueprint.php @@ -49,7 +49,7 @@ public function addSeoFields() static::$addingField = true; - $this->blueprint->ensureFieldInSection('seo', ['type' => 'seo_pro'], __('SEO')); + $this->blueprint->ensureFieldInSection('seo', ['type' => 'seo_pro', 'listable' => false], __('SEO')); static::$addingField = false; } diff --git a/src/Subscriber.php b/src/Subscriber.php index cce19fd..c85a2e5 100644 --- a/src/Subscriber.php +++ b/src/Subscriber.php @@ -3,10 +3,11 @@ namespace Statamic\SeoPro; use Illuminate\Support\Facades\Cache; -use Illuminate\Support\Facades\Request; use Statamic\Events; -use Statamic\Facades\Data; +use Statamic\Facades\Collection; +use Statamic\Facades\Taxonomy; use Statamic\SeoPro\Sitemap\Sitemap; +use Statamic\Support\Str; class Subscriber { @@ -43,7 +44,7 @@ public function subscribe($events) */ public function addSeoFields($event) { - if ($this->isAllowedPublishForm() && $this->seoIsEnabledForSection($event)) { + if ($this->seoIsEnabledForSection($event)) { Blueprint::on($event)->addSeoFields(); } } @@ -56,23 +57,6 @@ public function clearSitemapCache() Cache::forget(Sitemap::CACHE_KEY); } - /** - * Check if on an allowed published form. - * - * @return bool - */ - protected function isAllowedPublishForm() - { - $allowedRoutes = [ - 'statamic.cp.collections.entries.create', - 'statamic.cp.collections.entries.edit', - 'statamic.cp.taxonomies.terms.create', - 'statamic.cp.taxonomies.terms.edit', - ]; - - return in_array(Request::route()->getName(), $allowedRoutes); - } - /** * Check if SEO is enabled for section. * @@ -81,19 +65,16 @@ protected function isAllowedPublishForm() */ protected function seoIsEnabledForSection($event) { - // We can't check `value('seo')` because when creating an entry/term, we don't have an instance yet. - $section = $event->blueprint->namespace(); - - // So we convert the blueprint's namespace to a proper collection/taxonomy id to find the section, - $section = str_replace('collections.', 'collection::', $section); - $section = str_replace('taxonomies.', 'taxonomy::', $section); + $namespace = $event->blueprint->namespace(); - // Temporary fix for `Data::('collection::handle')` until beta 43, - if (\Statamic\Support\Str::startsWith('collection::', $section)) { - return \Statamic\Facades\Collection::find(str_replace('collection::', '', $section))->cascade('seo') !== false; + if (Str::startsWith($namespace, 'collections.')) { + $section = Collection::findByHandle(Str::after($namespace, 'collections.')); + } elseif (Str::startsWith($namespace, 'taxonomies.')) { + $section = Taxonomy::findByHandle(Str::after($namespace, 'taxonomies.')); + } else { + throw new \Exception('Unknown section type.'); } - // And then grab the setting right off the collection/taxonomy. - return Data::find($section)->cascade('seo') !== false; + return $section->cascade('seo') !== false; } }