[wp-trac] [WordPress Trac] #42061: Add new utility function for checking if the current request is a REST API request.

WordPress Trac noreply at wordpress.org
Fri Jan 19 17:37:21 UTC 2024


#42061: Add new utility function for checking if the current request is a REST API
request.
-------------------------------------------------+-------------------------
 Reporter:  lots.0.logs                          |       Owner:
                                                 |  TimothyBlynJacobs
     Type:  enhancement                          |      Status:  closed
 Priority:  normal                               |   Milestone:  6.5
Component:  Bootstrap/Load                       |     Version:  4.9
 Severity:  normal                               |  Resolution:  fixed
 Keywords:  has-patch dev-feedback commit has-   |     Focuses:  rest-api,
  unit-tests                                     |  performance
-------------------------------------------------+-------------------------
Changes (by flixos90):

 * status:  assigned => closed
 * resolution:   => fixed


Comment:

 In [changeset:"57312" 57312]:
 {{{
 #!CommitTicketReference repository="" revision="57312"
 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.
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/42061#comment:39>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list