Skip to content

Commit

Permalink
feat: disallow search engine indexing for vanity domains
Browse files Browse the repository at this point in the history
  • Loading branch information
carlalexander committed Aug 21, 2021
1 parent 7793484 commit 94cc727
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Configuration/EventManagementConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function modify(Container $container)
$container['subscribers'] = $container->service(function (Container $container) {
$subscribers = [
new Subscriber\AssetsSubscriber($container['site_url'], $container['assets_url'], $container['ymir_project_type']),
new Subscriber\DisallowIndexingSubscriber($container['site_url']),
new Subscriber\ImageEditorSubscriber($container['console_client'], $container['file_manager']),
new Subscriber\PluploadSubscriber($container['plugin_relative_path'], $container['rest_namespace'], $container['assets_url'], $container['plupload_error_messages']),
new Subscriber\RedirectSubscriber($container['ymir_primary_domain_name'], $container['is_multisite']),
Expand Down
80 changes: 80 additions & 0 deletions src/Subscriber/DisallowIndexingSubscriber.php
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');
}
}
33 changes: 33 additions & 0 deletions tests/Unit/Subscriber/DisallowIndexingSubscriberTest.php
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'));
}
}

0 comments on commit 94cc727

Please sign in to comment.