forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bootstrap/Load: Introduce functions to check whether WordPress is ser…
…ving a REST API request. This changeset introduces two functions: * `wp_is_serving_rest_request()` returns a boolean for whether WordPress is serving an actual REST API request. * `wp_is_rest_endpoint()` returns a boolean for whether a WordPress REST API endpoint is currently being used. While this is always the case if `wp_is_serving_rest_request()` returns `true`, the function additionally covers the scenario of internal REST API requests, i.e. where WordPress calls a REST API endpoint within the same request. Both functions should only be used after the `parse_request` action. All relevant manual checks have been adjusted to use one of the new functions, depending on the use-case. They were all using the same constant check so far, while in fact some of them were intending to check for an actual REST API request while others were intending to check for REST endpoint usage. A new filter `wp_is_rest_endpoint` can be used to alter the return value of the `wp_is_rest_endpoint()` function. Props lots.0.logs, TimothyBlynJacobs, flixos90, joehoyle, peterwilsoncc, swissspidy, SergeyBiryukov, pento, mikejolley, iandunn, hellofromTonya, Cybr, petitphp. Fixes #42061. git-svn-id: https://develop.svn.wordpress.org/trunk@57312 602fd350-edb4-49c9-b593-d223f7449a82
- Loading branch information
1 parent
3287684
commit d17afcc
Showing
11 changed files
with
162 additions
and
11 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
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
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,66 @@ | ||
<?php | ||
|
||
/** | ||
* Tests for the `wp_is_rest_endpoint()` function. | ||
* | ||
* @group rest-api | ||
* @covers ::wp_is_rest_endpoint | ||
*/ | ||
class Tests_Media_Wp_Is_Rest_Endpoint extends WP_UnitTestCase { | ||
|
||
/** | ||
* Tests that `wp_is_rest_endpoint()` returns false by default. | ||
* | ||
* @ticket 42061 | ||
*/ | ||
public function test_wp_is_rest_endpoint_default() { | ||
$this->assertFalse( wp_is_rest_endpoint() ); | ||
} | ||
|
||
/** | ||
* Tests that `wp_is_rest_endpoint()` relies on whether the global REST server is dispatching. | ||
* | ||
* @ticket 42061 | ||
*/ | ||
public function test_wp_is_rest_endpoint_via_global() { | ||
global $wp_rest_server; | ||
|
||
$wp_rest_server = new Spy_REST_Server(); | ||
do_action( 'rest_api_init', $wp_rest_server ); | ||
|
||
// The presence of a REST server itself won't set this to true. | ||
$this->assertFalse( wp_is_rest_endpoint() ); | ||
|
||
// Set up filter to record value during dispatching. | ||
$result_within_request = null; | ||
add_filter( | ||
'rest_pre_dispatch', | ||
function ( $result ) use ( &$result_within_request ) { | ||
$result_within_request = wp_is_rest_endpoint(); | ||
return $result; | ||
} | ||
); | ||
|
||
/* | ||
* Dispatch a request (doesn't matter that it's invalid). | ||
* This already is completed after this method call. | ||
*/ | ||
$wp_rest_server->dispatch( new WP_REST_Request() ); | ||
|
||
// Within that request, the function should have returned true. | ||
$this->assertTrue( $result_within_request ); | ||
|
||
// After the dispatching, the function should return false again. | ||
$this->assertFalse( wp_is_rest_endpoint() ); | ||
} | ||
|
||
/** | ||
* Tests that `wp_is_rest_endpoint()` returns a result enforced via filter. | ||
* | ||
* @ticket 42061 | ||
*/ | ||
public function test_wp_is_rest_endpoint_via_filter() { | ||
add_filter( 'wp_is_rest_endpoint', '__return_true' ); | ||
$this->assertTrue( wp_is_rest_endpoint() ); | ||
} | ||
} |