Bootstrap/Load: Introduce functions to check whether WordPress is serving 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
This commit is contained in:
Felix Arntz
2024-01-19 17:37:05 +00:00
parent 3287684830
commit d17afcc9dd
11 changed files with 162 additions and 11 deletions

View File

@@ -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() );
}
}