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/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/**
Note: See TracChangeset for help on using the changeset viewer.