Skip to content

Commit

Permalink
Clean up SEO handling when disabled for section.
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseleite committed Aug 4, 2020
1 parent 8207672 commit e9eb8ef
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
43 changes: 12 additions & 31 deletions src/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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();
}
}
Expand All @@ -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.
*
Expand All @@ -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;
}
}

0 comments on commit e9eb8ef

Please sign in to comment.