-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: disallow search engine indexing for vanity domains
- Loading branch information
1 parent
7793484
commit 94cc727
Showing
3 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Ymir WordPress plugin. | ||
* | ||
* (c) Carl Alexander <support@ymirapp.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Ymir\Plugin\Subscriber; | ||
|
||
use Ymir\Plugin\EventManagement\SubscriberInterface; | ||
|
||
/** | ||
* Subscriber for managing whether we allow indexing or not. | ||
*/ | ||
class DisallowIndexingSubscriber implements SubscriberInterface | ||
{ | ||
/** | ||
* WordPress site URL. | ||
* | ||
* @var string | ||
*/ | ||
private $siteUrl; | ||
|
||
/** | ||
* Constructor. | ||
*/ | ||
public function __construct(string $siteUrl) | ||
{ | ||
$this->siteUrl = rtrim($siteUrl, '/'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
'admin_notices' => 'displayAdminNotice', | ||
'pre_option_blog_public' => 'filterBlogPublic', | ||
]; | ||
} | ||
|
||
/** | ||
* Display admin notice about search indexing being disabled. | ||
*/ | ||
public function displayAdminNotice() | ||
{ | ||
if ($this->isIndexingAllowed()) { | ||
return; | ||
} | ||
|
||
echo '<div class="notice notice-warning"><p><strong>Ymir:</strong> Search engine indexing is disallowed when using a vanity domain. To learn how to map a domain to your environment, check out <a href="https://docs.ymirapp.com/guides/domain-mapping.html">this guide</a>.</p></div>'; | ||
} | ||
|
||
/** | ||
* Filter the "blog_public" option value. | ||
*/ | ||
public function filterBlogPublic($value) | ||
{ | ||
if (!$this->isIndexingAllowed()) { | ||
$value = 0; | ||
} | ||
|
||
return $value; | ||
} | ||
|
||
/** | ||
* Determine whether if search engine indexing should be allowed or not. | ||
*/ | ||
private function isIndexingAllowed(): bool | ||
{ | ||
return false === stripos($this->siteUrl, '.ymirsites.com'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Ymir WordPress plugin. | ||
* | ||
* (c) Carl Alexander <support@ymirapp.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Ymir\Plugin\Tests\Unit\Subscriber; | ||
|
||
use Ymir\Plugin\Subscriber\DisallowIndexingSubscriber; | ||
use Ymir\Plugin\Tests\Unit\TestCase; | ||
|
||
/** | ||
* @covers \Ymir\Plugin\Subscriber\DisallowIndexingSubscriber | ||
*/ | ||
class DisallowIndexingSubscriberTest extends TestCase | ||
{ | ||
public function testFilterBlogPublicReturnsSameValueIfIndexingAllowed() | ||
{ | ||
$this->assertSame('value', (new DisallowIndexingSubscriber('https://foo.com'))->filterBlogPublic('value')); | ||
} | ||
|
||
public function testFilterBlogPublicReturnsZeroValueIfSiteUrlUsesVanityDomain() | ||
{ | ||
$this->assertSame(0, (new DisallowIndexingSubscriber('https://subdomain.ymirsites.com'))->filterBlogPublic('value')); | ||
} | ||
} |