Make WordPress Core

Changeset 57312

Timestamp:
01/19/2024 05:37:05 PM (7 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                || ( && $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() || ) ) {
    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_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 ( ) {
    44454445        _doing_it_wrong(
    44464446            __FUNCTION__,
     
    46984698}
    46994699
     4700
     4701
    ��4702
     4703
     4704
     4705
     4706
     4707
     4708
     4709
     4710
     4711
     4712
     4713
     4714
     4715
     4716
    47004717
    47014718/**
  • trunk/src/wp-includes/load.php

    r56804 r57312  
    599599    }
    600600
     601
     602
     603
     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 ( ) {
    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
     3395
     3396
     3397
     3398
     3399
     3400
     3401
     3402
     3403
     3404
     3405
     3406
     3407
     3408
     3409
     3410
     3411
     3412
     3413
     3414
     3415
     3416
     3417
     3418
     3419
     3420
     3421
     3422
     3423
     3424
     3425
     3426
  • trunk/src/wp-includes/rest-api/class-wp-rest-server.php

    r57147 r57312  
    8787     */
    8888    protected $embed_cache = array();
     89
     90
     91
     92
     93
     94
     95
     96
    8997
    9098    /**
     
    984992     */
    985993    public function dispatch( $request ) {
     994
     995
    986996        /**
    987997         * Filters the pre-calculated result of a REST API dispatch request.
     
    10091019            }
    10101020
     1021
    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 ( ) {
    205205            /*
    206206             * Set a custom header with the attachment_id.
     
    631631        }
    632632
    633         if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
     633        if ( ) {
    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_R $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() || ) ) {
    25902590        return false;
    25912591    }
  • trunk/src/wp-includes/user.php

    r57226 r57312  
    334334    }
    335335
     336
    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.