-
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: redirect to primary domain name if host is different
- Loading branch information
1 parent
0dae3cc
commit 8c75763
Showing
5 changed files
with
171 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
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,75 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Ymir WordPress plugin. | ||
* | ||
* (c) Carl Alexander <[email protected]> | ||
* | ||
* 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 that manages redirects that would have been handled by the web server. | ||
*/ | ||
class RedirectSubscriber implements SubscriberInterface | ||
{ | ||
/** | ||
* The primary domain name that we want to redirect requests to. | ||
* | ||
* @var string | ||
*/ | ||
private $domainName; | ||
|
||
/** | ||
* Flag whether this is a multisite installation or not. | ||
* | ||
* @var bool | ||
*/ | ||
private $isMultisite; | ||
|
||
/** | ||
* Constructor. | ||
*/ | ||
public function __construct(string $domainName, bool $isMultisite) | ||
{ | ||
$this->domainName = $domainName; | ||
$this->isMultisite = $isMultisite; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
'init' => ['redirectToDomainName', 1], | ||
]; | ||
} | ||
|
||
/** | ||
* Redirect to the primary domain name if necessary. | ||
*/ | ||
public function redirectToDomainName() | ||
{ | ||
if ($this->isMultisite || empty($_SERVER['HTTP_HOST']) || $_SERVER['HTTP_HOST'] === $this->domainName) { | ||
return; | ||
} | ||
|
||
$url = 'https://'.$this->domainName; | ||
|
||
if (!empty($_SERVER['REQUEST_URI'])) { | ||
$url .= $_SERVER['REQUEST_URI']; | ||
} | ||
|
||
if (wp_redirect($url, 301)) { | ||
exit; | ||
} | ||
} | ||
} |
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,93 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Ymir WordPress plugin. | ||
* | ||
* (c) Carl Alexander <[email protected]> | ||
* | ||
* 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\RedirectSubscriber; | ||
use Ymir\Plugin\Tests\Mock\FunctionMockTrait; | ||
use Ymir\Plugin\Tests\Unit\TestCase; | ||
|
||
/** | ||
* @covers \Ymir\Plugin\Subscriber\RedirectSubscriber | ||
*/ | ||
class RedirectSubscriberTest extends TestCase | ||
{ | ||
use FunctionMockTrait; | ||
|
||
public function testGetSubscribedEvents() | ||
{ | ||
$callbacks = RedirectSubscriber::getSubscribedEvents(); | ||
|
||
foreach ($callbacks as $callback) { | ||
$this->assertTrue(method_exists(RedirectSubscriber::class, is_array($callback) ? $callback[0] : $callback)); | ||
} | ||
|
||
$subscribedEvents = [ | ||
'init' => ['redirectToDomainName', 1], | ||
]; | ||
|
||
$this->assertSame($subscribedEvents, $callbacks); | ||
} | ||
|
||
/** | ||
* @backupGlobals enabled | ||
*/ | ||
public function testRedirectToDomainNameWithHttpHostDifferentThanDomainName() | ||
{ | ||
$_SERVER['HTTP_HOST'] = 'another_domain_name'; | ||
|
||
$wp_redirect = $this->getFunctionMock($this->getNamespace(RedirectSubscriber::class), 'wp_redirect'); | ||
$wp_redirect->expects($this->once()) | ||
->with($this->identicalTo('https://domain_name'), $this->identicalTo(301)) | ||
->willReturn(false); | ||
|
||
(new RedirectSubscriber('domain_name', false))->redirectToDomainName(); | ||
} | ||
|
||
/** | ||
* @backupGlobals enabled | ||
*/ | ||
public function testRedirectToDomainNameWithHttpHostDifferentThanDomainNameAddsRequestUri() | ||
{ | ||
$_SERVER['HTTP_HOST'] = 'another_domain_name'; | ||
$_SERVER['REQUEST_URI'] = '/uri'; | ||
|
||
$wp_redirect = $this->getFunctionMock($this->getNamespace(RedirectSubscriber::class), 'wp_redirect'); | ||
$wp_redirect->expects($this->once()) | ||
->with($this->identicalTo('https://domain_name/uri'), $this->identicalTo(301)) | ||
->willReturn(false); | ||
|
||
(new RedirectSubscriber('domain_name', false))->redirectToDomainName(); | ||
} | ||
|
||
/** | ||
* @backupGlobals enabled | ||
*/ | ||
public function testRedirectToDomainNameWithHttpHostSameAsDomainName() | ||
{ | ||
$_SERVER['HTTP_HOST'] = 'domain_name'; | ||
|
||
$wp_redirect = $this->getFunctionMock($this->getNamespace(RedirectSubscriber::class), 'wp_redirect'); | ||
$wp_redirect->expects($this->never()); | ||
|
||
(new RedirectSubscriber('domain_name', false))->redirectToDomainName(); | ||
} | ||
|
||
public function testRedirectToDomainNameWithMultisiteEnabled() | ||
{ | ||
$wp_redirect = $this->getFunctionMock($this->getNamespace(RedirectSubscriber::class), 'wp_redirect'); | ||
$wp_redirect->expects($this->never()); | ||
|
||
(new RedirectSubscriber('domain_name', true))->redirectToDomainName(); | ||
} | ||
} |