Skip to content

Commit

Permalink
Do not show SEO Pro tab on publish forms where disabled for section.
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseleite committed Jul 31, 2020
1 parent b9914d3 commit 8207672
Showing 1 changed file with 47 additions and 2 deletions.
49 changes: 47 additions & 2 deletions src/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Statamic\SeoPro;

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Request;
use Statamic\Events;
use Statamic\Facades\Data;
use Statamic\SeoPro\Sitemap\Sitemap;

class Subscriber
Expand Down Expand Up @@ -35,13 +37,15 @@ public function subscribe($events)
}

/**
* Add SEO section and fields to blueprint.
* Add SEO section and fields to entry blueprint.
*
* @param mixed $event
*/
public function addSeoFields($event)
{
Blueprint::on($event)->addSeoFields();
if ($this->isAllowedPublishForm() && $this->seoIsEnabledForSection($event)) {
Blueprint::on($event)->addSeoFields();
}
}

/**
Expand All @@ -51,4 +55,45 @@ 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.
*
* @param mixed $event
* @return bool
*/
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);

// 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;
}

// And then grab the setting right off the collection/taxonomy.
return Data::find($section)->cascade('seo') !== false;
}
}

0 comments on commit 8207672

Please sign in to comment.