Make WordPress Core


Ignore:
Timestamp:
01/19/2024 05:37:05 PM (11 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.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}
Note: See TracChangeset for help on using the changeset viewer.