mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-10-05 15:16:59 +00:00
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:
@@ -87,6 +87,14 @@ class WP_REST_Server {
|
||||
*/
|
||||
protected $embed_cache = array();
|
||||
|
||||
/**
|
||||
* Stores request objects that are currently being handled.
|
||||
*
|
||||
* @since 6.5.0
|
||||
* @var array
|
||||
*/
|
||||
protected $dispatching_requests = array();
|
||||
|
||||
/**
|
||||
* Instantiates the REST server.
|
||||
*
|
||||
@@ -983,6 +991,8 @@ class WP_REST_Server {
|
||||
* @return WP_REST_Response Response returned by the callback.
|
||||
*/
|
||||
public function dispatch( $request ) {
|
||||
$this->dispatching_requests[] = $request;
|
||||
|
||||
/**
|
||||
* Filters the pre-calculated result of a REST API dispatch request.
|
||||
*
|
||||
@@ -1008,6 +1018,7 @@ class WP_REST_Server {
|
||||
$result = $this->error_to_response( $result );
|
||||
}
|
||||
|
||||
array_pop( $this->dispatching_requests );
|
||||
return $result;
|
||||
}
|
||||
|
||||
@@ -1015,7 +1026,9 @@ class WP_REST_Server {
|
||||
$matched = $this->match_request_to_handler( $request );
|
||||
|
||||
if ( is_wp_error( $matched ) ) {
|
||||
return $this->error_to_response( $matched );
|
||||
$response = $this->error_to_response( $matched );
|
||||
array_pop( $this->dispatching_requests );
|
||||
return $response;
|
||||
}
|
||||
|
||||
list( $route, $handler ) = $matched;
|
||||
@@ -1040,7 +1053,22 @@ class WP_REST_Server {
|
||||
}
|
||||
}
|
||||
|
||||
return $this->respond_to_request( $request, $route, $handler, $error );
|
||||
$response = $this->respond_to_request( $request, $route, $handler, $error );
|
||||
array_pop( $this->dispatching_requests );
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the REST server is currently dispatching / responding to a request.
|
||||
*
|
||||
* This may be a standalone REST API request, or an internal request dispatched from within a regular page load.
|
||||
*
|
||||
* @since 6.5.0
|
||||
*
|
||||
* @return bool Whether the REST server is currently handling a request.
|
||||
*/
|
||||
public function is_dispatching() {
|
||||
return (bool) $this->dispatching_requests;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -201,7 +201,7 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller {
|
||||
|
||||
wp_after_insert_post( $attachment, false, null );
|
||||
|
||||
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
|
||||
if ( wp_is_serving_rest_request() ) {
|
||||
/*
|
||||
* Set a custom header with the attachment_id.
|
||||
* Used by the browser/client to resume creating image sub-sizes after a PHP fatal error.
|
||||
@@ -630,7 +630,7 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller {
|
||||
update_post_meta( $new_attachment_id, '_wp_attachment_image_alt', wp_slash( $image_alt ) );
|
||||
}
|
||||
|
||||
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
|
||||
if ( wp_is_serving_rest_request() ) {
|
||||
/*
|
||||
* Set a custom header with the attachment_id.
|
||||
* Used by the browser/client to resume creating image sub-sizes after a PHP fatal error.
|
||||
|
||||
@@ -128,7 +128,7 @@ class WP_REST_URL_Details_Controller extends WP_REST_Controller {
|
||||
*
|
||||
* @since 5.9.0
|
||||
*
|
||||
* @param WP_REST_REQUEST $request Full details about the request.
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_REST_Response|WP_Error The parsed details as a response object. WP_Error if there are errors.
|
||||
*/
|
||||
public function parse_url_details( $request ) {
|
||||
|
||||
Reference in New Issue
Block a user