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