mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-06-28 22:30:04 +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:
@@ -3718,7 +3718,7 @@ function wp_die( $message = '', $title = '', $args = array() ) {
|
||||
* @param callable $callback Callback function name.
|
||||
*/
|
||||
$callback = apply_filters( 'wp_die_json_handler', '_json_wp_die_handler' );
|
||||
} elseif ( defined( 'REST_REQUEST' ) && REST_REQUEST && wp_is_jsonp_request() ) {
|
||||
} elseif ( wp_is_serving_rest_request() && wp_is_jsonp_request() ) {
|
||||
/**
|
||||
* Filters the callback for killing WordPress execution for JSONP REST requests.
|
||||
*
|
||||
@@ -4441,7 +4441,7 @@ function _wp_json_prepare_data( $value ) {
|
||||
* @param int $flags Optional. Options to be passed to json_encode(). Default 0.
|
||||
*/
|
||||
function wp_send_json( $response, $status_code = null, $flags = 0 ) {
|
||||
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
|
||||
if ( wp_is_serving_rest_request() ) {
|
||||
_doing_it_wrong(
|
||||
__FUNCTION__,
|
||||
sprintf(
|
||||
@@ -4697,6 +4697,23 @@ function _mce_set_direction( $mce_init ) {
|
||||
return $mce_init;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether WordPress is currently serving a REST API request.
|
||||
*
|
||||
* The function relies on the 'REST_REQUEST' global. As such, it only returns true when an actual REST _request_ is
|
||||
* being made. It does not return true when a REST endpoint is hit as part of another request, e.g. for preloading a
|
||||
* REST response. See {@see wp_is_rest_endpoint()} for that purpose.
|
||||
*
|
||||
* This function should not be called until the {@see 'parse_request'} action, as the constant is only defined then,
|
||||
* even for an actual REST request.
|
||||
*
|
||||
* @since 6.5.0
|
||||
*
|
||||
* @return bool True if it's a WordPress REST API request, false otherwise.
|
||||
*/
|
||||
function wp_is_serving_rest_request() {
|
||||
return defined( 'REST_REQUEST' ) && REST_REQUEST;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts smiley code to the icon graphic file equivalent.
|
||||
|
||||
Reference in New Issue
Block a user