Make WordPress Core

Changeset 57312


Ignore:
Timestamp:
01/19/2024 05:37:05 PM (13 months ago)
Author:
flixos90
Message:

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.

Location:
trunk
Files:
1 added
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-query.php

    r57195 r57312  
    10281028
    10291029        if ( ! ( $this->is_singular || $this->is_archive || $this->is_search || $this->is_feed
    1030                 || ( defined( 'REST_REQUEST' ) && REST_REQUEST && $this->is_main_query() )
     1030                || ( wp_is_serving_rest_request() && $this->is_main_query() )
    10311031                || $this->is_trackback || $this->is_404 || $this->is_admin || $this->is_robots || $this->is_favicon ) ) {
    10321032            $this->is_home = true;
  • trunk/src/wp-includes/deprecated.php

    r57196 r57312  
    54375437
    54385438        // If in the editor, add webfonts defined in variations.
    5439         if ( is_admin() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) {
     5439        if ( is_admin() || wp_is_rest_endpoint() ) {
    54405440            $variations = WP_Theme_JSON_Resolver::get_style_variations();
    54415441            foreach ( $variations as $variation ) {
  • trunk/src/wp-includes/functions.php

    r57279 r57312  
    37193719         */
    37203720        $callback = apply_filters( 'wp_die_json_handler', '_json_wp_die_handler' );
    3721     } elseif ( defined( 'REST_REQUEST' ) && REST_REQUEST && wp_is_jsonp_request() ) {
     3721    } elseif ( wp_is_serving_rest_request() && wp_is_jsonp_request() ) {
    37223722        /**
    37233723         * Filters the callback for killing WordPress execution for JSONP REST requests.
     
    44424442 */
    44434443function wp_send_json( $response, $status_code = null, $flags = 0 ) {
    4444     if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
     4444    if ( wp_is_serving_rest_request() ) {
    44454445        _doing_it_wrong(
    44464446            __FUNCTION__,
     
    46984698}
    46994699
     4700/**
     4701 * Determines whether WordPress is currently serving a REST API request.
     4702 *
     4703 * The function relies on the 'REST_REQUEST' global. As such, it only returns true when an actual REST _request_ is
     4704 * being made. It does not return true when a REST endpoint is hit as part of another request, e.g. for preloading a
     4705 * REST response. See {@see wp_is_rest_endpoint()} for that purpose.
     4706 *
     4707 * This function should not be called until the {@see 'parse_request'} action, as the constant is only defined then,
     4708 * even for an actual REST request.
     4709 *
     4710 * @since 6.5.0
     4711 *
     4712 * @return bool True if it's a WordPress REST API request, false otherwise.
     4713 */
     4714function wp_is_serving_rest_request() {
     4715    return defined( 'REST_REQUEST' ) && REST_REQUEST;
     4716}
    47004717
    47014718/**
  • trunk/src/wp-includes/load.php

    r56804 r57312  
    599599    }
    600600
     601    /*
     602     * The 'REST_REQUEST' check here is optimistic as the constant is most
     603     * likely not set at this point even if it is in fact a REST request.
     604     */
    601605    if ( defined( 'XMLRPC_REQUEST' ) || defined( 'REST_REQUEST' ) || defined( 'MS_FILES_REQUEST' )
    602606        || ( defined( 'WP_INSTALLING' ) && WP_INSTALLING )
  • trunk/src/wp-includes/rest-api.php

    r56834 r57312  
    210210 */
    211211function rest_api_default_filters() {
    212     if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
     212    if ( wp_is_serving_rest_request() ) {
    213213        // Deprecated reporting.
    214214        add_action( 'deprecated_function_run', 'rest_handle_deprecated_function', 10, 3 );
     
    33903390    return new WP_REST_Response( $data, $status );
    33913391}
     3392
     3393/**
     3394 * Checks whether a REST API endpoint request is currently being handled.
     3395 *
     3396 * This may be a standalone REST API request, or an internal request dispatched from within a regular page load.
     3397 *
     3398 * @since 6.5.0
     3399 *
     3400 * @global WP_REST_Server $wp_rest_server REST server instance.
     3401 *
     3402 * @return bool True if a REST endpoint request is currently being handled, false otherwise.
     3403 */
     3404function wp_is_rest_endpoint() {
     3405    /* @var WP_REST_Server $wp_rest_server */
     3406    global $wp_rest_server;
     3407
     3408    // Check whether this is a standalone REST request.
     3409    $is_rest_endpoint = wp_is_serving_rest_request();
     3410    if ( ! $is_rest_endpoint ) {
     3411        // Otherwise, check whether an internal REST request is currently being handled.
     3412        $is_rest_endpoint = isset( $wp_rest_server )
     3413            && $wp_rest_server->is_dispatching();
     3414    }
     3415
     3416    /**
     3417     * Filters whether a REST endpoint request is currently being handled.
     3418     *
     3419     * This may be a standalone REST API request, or an internal request dispatched from within a regular page load.
     3420     *
     3421     * @since 6.5.0
     3422     *
     3423     * @param bool $is_request_endpoint Whether a REST endpoint request is currently being handled.
     3424     */
     3425    return (bool) apply_filters( 'wp_is_rest_endpoint', $is_rest_endpoint );
     3426}
  • trunk/src/wp-includes/rest-api/class-wp-rest-server.php

    r57147 r57312  
    8787     */
    8888    protected $embed_cache = array();
     89
     90    /**
     91     * Stores request objects that are currently being handled.
     92     *
     93     * @since 6.5.0
     94     * @var array
     95     */
     96    protected $dispatching_requests = array();
    8997
    9098    /**
     
    984992     */
    985993    public function dispatch( $request ) {
     994        $this->dispatching_requests[] = $request;
     995
    986996        /**
    987997         * Filters the pre-calculated result of a REST API dispatch request.
     
    10091019            }
    10101020
     1021            array_pop( $this->dispatching_requests );
    10111022            return $result;
    10121023        }
     
    10161027
    10171028        if ( is_wp_error( $matched ) ) {
    1018             return $this->error_to_response( $matched );
     1029            $response = $this->error_to_response( $matched );
     1030            array_pop( $this->dispatching_requests );
     1031            return $response;
    10191032        }
    10201033
     
    10411054        }
    10421055
    1043         return $this->respond_to_request( $request, $route, $handler, $error );
     1056        $response = $this->respond_to_request( $request, $route, $handler, $error );
     1057        array_pop( $this->dispatching_requests );
     1058        return $response;
     1059    }
     1060
     1061    /**
     1062     * Returns whether the REST server is currently dispatching / responding to a request.
     1063     *
     1064     * This may be a standalone REST API request, or an internal request dispatched from within a regular page load.
     1065     *
     1066     * @since 6.5.0
     1067     *
     1068     * @return bool Whether the REST server is currently handling a request.
     1069     */
     1070    public function is_dispatching() {
     1071        return (bool) $this->dispatching_requests;
    10441072    }
    10451073
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

    r56586 r57312  
    202202        wp_after_insert_post( $attachment, false, null );
    203203
    204         if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
     204        if ( wp_is_serving_rest_request() ) {
    205205            /*
    206206             * Set a custom header with the attachment_id.
     
    631631        }
    632632
    633         if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
     633        if ( wp_is_serving_rest_request() ) {
    634634            /*
    635635             * Set a custom header with the attachment_id.
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-url-details-controller.php

    r53455 r57312  
    129129     * @since 5.9.0
    130130     *
    131      * @param WP_REST_REQUEST $request Full details about the request.
     131     * @param WP_REST_Request $request Full details about the request.
    132132     * @return WP_REST_Response|WP_Error The parsed details as a response object. WP_Error if there are errors.
    133133     */
  • trunk/src/wp-includes/script-loader.php

    r57204 r57312  
    25872587 */
    25882588function wp_should_load_separate_core_block_assets() {
    2589     if ( is_admin() || is_feed() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) {
     2589    if ( is_admin() || is_feed() || wp_is_rest_endpoint() ) {
    25902590        return false;
    25912591    }
  • trunk/src/wp-includes/user.php

    r57226 r57312  
    334334    }
    335335
     336    // The 'REST_REQUEST' check here may happen too early for the constant to be available.
    336337    $is_api_request = ( ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) );
    337338
Note: See TracChangeset for help on using the changeset viewer.