Skip to content

Commit

Permalink
Improve early detection of rest and login contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
gmazzap committed Feb 19, 2021
1 parent b95a5b4 commit 36b4412
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
17 changes: 10 additions & 7 deletions src/WpContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,19 @@ private static function isRestRequest(): bool
return true;
}

if (!get_option('permalink_structure')) {
return !empty($_GET['rest_route']); // phpcs:ignore
}

// This is needed because, if called early, global $wp_rewrite is not defined but required
// by get_rest_url(). WP will reuse what we set here, or in worst case will replace, but no
// consequences for us in any case.
if (get_option('permalink_structure') && empty($GLOBALS['wp_rewrite'])) {
if (empty($GLOBALS['wp_rewrite'])) {
$GLOBALS['wp_rewrite'] = new \WP_Rewrite();
}

$currentUrl = set_url_scheme(add_query_arg([]));
$restUrl = set_url_scheme(get_rest_url());
$currentPath = trim((string)parse_url((string)$currentUrl, PHP_URL_PATH), '/') . '/';
$restPath = trim((string)parse_url((string)$restUrl, PHP_URL_PATH), '/') . '/';
$currentPath = trim((string)parse_url((string)add_query_arg([]), PHP_URL_PATH), '/') . '/';
$restPath = trim((string)parse_url((string)get_rest_url(), PHP_URL_PATH), '/') . '/';

return strpos($currentPath, $restPath) === 0;
}
Expand All @@ -132,9 +134,10 @@ private static function isLoginRequest(): bool
return true;
}

$url = home_url((string)parse_url(add_query_arg([]), PHP_URL_PATH));
$currentPath = (string)parse_url(add_query_arg([]), PHP_URL_PATH);
$loginPath = (string)parse_url(wp_login_url(), PHP_URL_PATH);

return rtrim($url, '/') === rtrim(wp_login_url(), '/');
return rtrim($currentPath, '/') === rtrim($loginPath, '/');
}

/**
Expand Down
17 changes: 10 additions & 7 deletions tests/WpContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class WpContextTest extends TestCase
{
use MockeryPHPUnitIntegration;

/**
* @var string
*/
private $currentPath = '/';

/**
Expand All @@ -25,7 +28,7 @@ protected function setUp(): void
{
parent::setUp();
Monkey\setUp();
Monkey\Functions\expect('add_query_arg')->with([])->andReturnUsing(function () {
Monkey\Functions\expect('add_query_arg')->with([])->andReturnUsing(function (): string {
return $this->currentPath;
});
}
Expand Down Expand Up @@ -100,7 +103,6 @@ public function testIsLoginLate(): void
$this->mockIsRestRequest(false);
$this->mockIsLoginRequest(false);

/** @var callable|null $onLoginInit */
$onLoginInit = null;
Monkey\Actions\expectAdded('login_init')
->whenHappen(function (callable $callback) use (&$onLoginInit) {
Expand All @@ -111,6 +113,7 @@ public function testIsLoginLate(): void

static::assertTrue($context->isCore());
static::assertFalse($context->isLogin());
/** @var callable $onLoginInit */
$onLoginInit();
static::assertTrue($context->isLogin());
}
Expand Down Expand Up @@ -156,7 +159,6 @@ public function testIsRestLate(): void
$this->mockIsRestRequest(false);
$this->mockIsLoginRequest(false);

/** @var callable|null $onRestInit */
$onRestInit = null;
Monkey\Actions\expectAdded('rest_api_init')
->whenHappen(function (callable $callback) use (&$onRestInit) {
Expand All @@ -167,6 +169,7 @@ public function testIsRestLate(): void

static::assertTrue($context->isCore());
static::assertFalse($context->isRest());
/** @var callable $onRestInit */
$onRestInit();
static::assertTrue($context->isRest());
}
Expand Down Expand Up @@ -331,7 +334,7 @@ public function testJsonSerialize(): void
$this->mockIsLoginRequest(true);

$context = WpContext::determine();
$decoded = json_decode(json_encode($context), true);
$decoded = (array)json_decode((string)json_encode($context), true);

static::assertTrue($decoded[WpContext::CORE]);
static::assertTrue($decoded[WpContext::LOGIN]);
Expand All @@ -348,8 +351,8 @@ public function testJsonSerialize(): void
*/
private function mockIsRestRequest(bool $is): void
{
Monkey\Functions\expect('get_option')->with('permalink_structure')->andReturn(false);
Monkey\Functions\stubs(['set_url_scheme']);
Monkey\Functions\expect('get_option')->with('permalink_structure')->andReturn(true);
$GLOBALS['wp_rewrite'] = \Mockery::mock('WP_Rewrite');
Monkey\Functions\when('get_rest_url')->justReturn('https://example.com/wp-json');
$is and $this->currentPath = '/wp-json/foo';
}
Expand All @@ -361,7 +364,7 @@ private function mockIsLoginRequest(bool $is): void
{
$is and $this->currentPath = '/wp-login.php';
Monkey\Functions\when('wp_login_url')->justReturn('https://example.com/wp-login.php');
Monkey\Functions\when('home_url')->alias(static function ($path = '') {
Monkey\Functions\when('home_url')->alias(static function (string $path = ''): string {
return 'https://example.com/' . ltrim($path, '/');
});
}
Expand Down

0 comments on commit 36b4412

Please sign in to comment.