Skip to content

Commit 94cc727

Browse files
committed
feat: disallow search engine indexing for vanity domains
1 parent 7793484 commit 94cc727

3 files changed

Lines changed: 114 additions & 0 deletions

File tree

src/Configuration/EventManagementConfiguration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function modify(Container $container)
4444
$container['subscribers'] = $container->service(function (Container $container) {
4545
$subscribers = [
4646
new Subscriber\AssetsSubscriber($container['site_url'], $container['assets_url'], $container['ymir_project_type']),
47+
new Subscriber\DisallowIndexingSubscriber($container['site_url']),
4748
new Subscriber\ImageEditorSubscriber($container['console_client'], $container['file_manager']),
4849
new Subscriber\PluploadSubscriber($container['plugin_relative_path'], $container['rest_namespace'], $container['assets_url'], $container['plupload_error_messages']),
4950
new Subscriber\RedirectSubscriber($container['ymir_primary_domain_name'], $container['is_multisite']),
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Ymir WordPress plugin.
7+
*
8+
* (c) Carl Alexander <support@ymirapp.com>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Ymir\Plugin\Subscriber;
15+
16+
use Ymir\Plugin\EventManagement\SubscriberInterface;
17+
18+
/**
19+
* Subscriber for managing whether we allow indexing or not.
20+
*/
21+
class DisallowIndexingSubscriber implements SubscriberInterface
22+
{
23+
/**
24+
* WordPress site URL.
25+
*
26+
* @var string
27+
*/
28+
private $siteUrl;
29+
30+
/**
31+
* Constructor.
32+
*/
33+
public function __construct(string $siteUrl)
34+
{
35+
$this->siteUrl = rtrim($siteUrl, '/');
36+
}
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
public static function getSubscribedEvents(): array
42+
{
43+
return [
44+
'admin_notices' => 'displayAdminNotice',
45+
'pre_option_blog_public' => 'filterBlogPublic',
46+
];
47+
}
48+
49+
/**
50+
* Display admin notice about search indexing being disabled.
51+
*/
52+
public function displayAdminNotice()
53+
{
54+
if ($this->isIndexingAllowed()) {
55+
return;
56+
}
57+
58+
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>';
59+
}
60+
61+
/**
62+
* Filter the "blog_public" option value.
63+
*/
64+
public function filterBlogPublic($value)
65+
{
66+
if (!$this->isIndexingAllowed()) {
67+
$value = 0;
68+
}
69+
70+
return $value;
71+
}
72+
73+
/**
74+
* Determine whether if search engine indexing should be allowed or not.
75+
*/
76+
private function isIndexingAllowed(): bool
77+
{
78+
return false === stripos($this->siteUrl, '.ymirsites.com');
79+
}
80+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Ymir WordPress plugin.
7+
*
8+
* (c) Carl Alexander <support@ymirapp.com>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Ymir\Plugin\Tests\Unit\Subscriber;
15+
16+
use Ymir\Plugin\Subscriber\DisallowIndexingSubscriber;
17+
use Ymir\Plugin\Tests\Unit\TestCase;
18+
19+
/**
20+
* @covers \Ymir\Plugin\Subscriber\DisallowIndexingSubscriber
21+
*/
22+
class DisallowIndexingSubscriberTest extends TestCase
23+
{
24+
public function testFilterBlogPublicReturnsSameValueIfIndexingAllowed()
25+
{
26+
$this->assertSame('value', (new DisallowIndexingSubscriber('https://foo.com'))->filterBlogPublic('value'));
27+
}
28+
29+
public function testFilterBlogPublicReturnsZeroValueIfSiteUrlUsesVanityDomain()
30+
{
31+
$this->assertSame(0, (new DisallowIndexingSubscriber('https://subdomain.ymirsites.com'))->filterBlogPublic('value'));
32+
}
33+
}

0 commit comments

Comments
 (0)