Skip to content

Commit 36b4412

Browse files
committed
Improve early detection of rest and login contexts
1 parent b95a5b4 commit 36b4412

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

src/WpContext.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,19 @@ private static function isRestRequest(): bool
103103
return true;
104104
}
105105

106+
if (!get_option('permalink_structure')) {
107+
return !empty($_GET['rest_route']); // phpcs:ignore
108+
}
109+
106110
// This is needed because, if called early, global $wp_rewrite is not defined but required
107111
// by get_rest_url(). WP will reuse what we set here, or in worst case will replace, but no
108112
// consequences for us in any case.
109-
if (get_option('permalink_structure') && empty($GLOBALS['wp_rewrite'])) {
113+
if (empty($GLOBALS['wp_rewrite'])) {
110114
$GLOBALS['wp_rewrite'] = new \WP_Rewrite();
111115
}
112116

113-
$currentUrl = set_url_scheme(add_query_arg([]));
114-
$restUrl = set_url_scheme(get_rest_url());
115-
$currentPath = trim((string)parse_url((string)$currentUrl, PHP_URL_PATH), '/') . '/';
116-
$restPath = trim((string)parse_url((string)$restUrl, PHP_URL_PATH), '/') . '/';
117+
$currentPath = trim((string)parse_url((string)add_query_arg([]), PHP_URL_PATH), '/') . '/';
118+
$restPath = trim((string)parse_url((string)get_rest_url(), PHP_URL_PATH), '/') . '/';
117119

118120
return strpos($currentPath, $restPath) === 0;
119121
}
@@ -132,9 +134,10 @@ private static function isLoginRequest(): bool
132134
return true;
133135
}
134136

135-
$url = home_url((string)parse_url(add_query_arg([]), PHP_URL_PATH));
137+
$currentPath = (string)parse_url(add_query_arg([]), PHP_URL_PATH);
138+
$loginPath = (string)parse_url(wp_login_url(), PHP_URL_PATH);
136139

137-
return rtrim($url, '/') === rtrim(wp_login_url(), '/');
140+
return rtrim($currentPath, '/') === rtrim($loginPath, '/');
138141
}
139142

140143
/**

tests/WpContextTest.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ class WpContextTest extends TestCase
1616
{
1717
use MockeryPHPUnitIntegration;
1818

19+
/**
20+
* @var string
21+
*/
1922
private $currentPath = '/';
2023

2124
/**
@@ -25,7 +28,7 @@ protected function setUp(): void
2528
{
2629
parent::setUp();
2730
Monkey\setUp();
28-
Monkey\Functions\expect('add_query_arg')->with([])->andReturnUsing(function () {
31+
Monkey\Functions\expect('add_query_arg')->with([])->andReturnUsing(function (): string {
2932
return $this->currentPath;
3033
});
3134
}
@@ -100,7 +103,6 @@ public function testIsLoginLate(): void
100103
$this->mockIsRestRequest(false);
101104
$this->mockIsLoginRequest(false);
102105

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

112114
static::assertTrue($context->isCore());
113115
static::assertFalse($context->isLogin());
116+
/** @var callable $onLoginInit */
114117
$onLoginInit();
115118
static::assertTrue($context->isLogin());
116119
}
@@ -156,7 +159,6 @@ public function testIsRestLate(): void
156159
$this->mockIsRestRequest(false);
157160
$this->mockIsLoginRequest(false);
158161

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

168170
static::assertTrue($context->isCore());
169171
static::assertFalse($context->isRest());
172+
/** @var callable $onRestInit */
170173
$onRestInit();
171174
static::assertTrue($context->isRest());
172175
}
@@ -331,7 +334,7 @@ public function testJsonSerialize(): void
331334
$this->mockIsLoginRequest(true);
332335

333336
$context = WpContext::determine();
334-
$decoded = json_decode(json_encode($context), true);
337+
$decoded = (array)json_decode((string)json_encode($context), true);
335338

336339
static::assertTrue($decoded[WpContext::CORE]);
337340
static::assertTrue($decoded[WpContext::LOGIN]);
@@ -348,8 +351,8 @@ public function testJsonSerialize(): void
348351
*/
349352
private function mockIsRestRequest(bool $is): void
350353
{
351-
Monkey\Functions\expect('get_option')->with('permalink_structure')->andReturn(false);
352-
Monkey\Functions\stubs(['set_url_scheme']);
354+
Monkey\Functions\expect('get_option')->with('permalink_structure')->andReturn(true);
355+
$GLOBALS['wp_rewrite'] = \Mockery::mock('WP_Rewrite');
353356
Monkey\Functions\when('get_rest_url')->justReturn('https://example.com/wp-json');
354357
$is and $this->currentPath = '/wp-json/foo';
355358
}
@@ -361,7 +364,7 @@ private function mockIsLoginRequest(bool $is): void
361364
{
362365
$is and $this->currentPath = '/wp-login.php';
363366
Monkey\Functions\when('wp_login_url')->justReturn('https://example.com/wp-login.php');
364-
Monkey\Functions\when('home_url')->alias(static function ($path = '') {
367+
Monkey\Functions\when('home_url')->alias(static function (string $path = ''): string {
365368
return 'https://example.com/' . ltrim($path, '/');
366369
});
367370
}

0 commit comments

Comments
 (0)