Skip to content

Commit

Permalink
fix: use rest_url instead of site_url to generate REST url (#4pmk26)
Browse files Browse the repository at this point in the history
  • Loading branch information
matzeeable committed Apr 16, 2020
1 parent e25ca4a commit 7b476d8
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
7 changes: 2 additions & 5 deletions packages/utils/src/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,8 @@ public function getCore() {
* @return string Example: https://example.com/wp-json
*/
public static function getUrl($instance, $namespace = null, $endpoint = '') {
return site_url(rest_get_url_prefix()) .
'/' .
($namespace === null ? Service::getNamespace($instance) : $namespace) .
'/' .
$endpoint;
$path = ($namespace === null ? Service::getNamespace($instance) : $namespace) . '/' . $endpoint;
return rest_url($path);
}

/**
Expand Down
9 changes: 3 additions & 6 deletions packages/utils/test/phpunit/ServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ public function testGetUrl() {
$mockCore = Mockery::mock(CoreImpl::class);
$mockCore->shouldReceive('getPluginConstant')->andReturnUsing('mockGetPluginConstant');

WP_Mock::userFunction('site_url', ['return' => 'http://localhost/wp-json']);
WP_Mock::userFunction('rest_get_url_prefix');
WP_Mock::userFunction('rest_url', ['return' => $should, 'args' => ['test/v1/']]);

redefine(Service::class . '::getNamespace', always('test/v1'));

Expand All @@ -71,8 +70,7 @@ public function testGetUrlWithCustomNamespace() {
$mockCore = Mockery::mock(CoreImpl::class);
$mockCore->shouldReceive('getPluginConstant')->andReturnUsing('mockGetPluginConstant');

WP_Mock::userFunction('site_url', ['return' => 'http://localhost/wp-json']);
WP_Mock::userFunction('rest_get_url_prefix');
WP_Mock::userFunction('rest_url', ['return' => $should, 'args' => ['another/v2/']]);

$actual = Service::getUrl($mockCore, 'another/v2');

Expand All @@ -85,8 +83,7 @@ public function testGetUrlWithEndpoint() {
$mockCore = Mockery::mock(CoreImpl::class);
$mockCore->shouldReceive('getPluginConstant')->andReturnUsing('mockGetPluginConstant');

WP_Mock::userFunction('site_url', ['return' => 'http://localhost/wp-json']);
WP_Mock::userFunction('rest_get_url_prefix');
WP_Mock::userFunction('rest_url', ['return' => $should, 'args' => ['test/v1/user/1']]);

redefine(Service::class . '::getNamespace', always('test/v1'));

Expand Down

0 comments on commit 7b476d8

Please sign in to comment.