Make WordPress Core


Ignore:
Timestamp:
01/19/2024 05:37:05 PM (8 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.