<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[57312] trunk: Bootstrap/Load: Introduce functions to check whether WordPress is serving a REST API request.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/57312">57312</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/57312","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>flixos90</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2024-01-19 17:37:05 +0000 (Fri, 19 Jan 2024)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="https://core.trac.wordpress.org/ticket/42061">#42061</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswpqueryphp">trunk/src/wp-includes/class-wp-query.php</a></li>
<li><a href="#trunksrcwpincludesdeprecatedphp">trunk/src/wp-includes/deprecated.php</a></li>
<li><a href="#trunksrcwpincludesfunctionsphp">trunk/src/wp-includes/functions.php</a></li>
<li><a href="#trunksrcwpincludesloadphp">trunk/src/wp-includes/load.php</a></li>
<li><a href="#trunksrcwpincludesrestapiclasswprestserverphp">trunk/src/wp-includes/rest-api/class-wp-rest-server.php</a></li>
<li><a href="#trunksrcwpincludesrestapiendpointsclasswprestattachmentscontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php</a></li>
<li><a href="#trunksrcwpincludesrestapiendpointsclasswpresturldetailscontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-url-details-controller.php</a></li>
<li><a href="#trunksrcwpincludesrestapiphp">trunk/src/wp-includes/rest-api.php</a></li>
<li><a href="#trunksrcwpincludesscriptloaderphp">trunk/src/wp-includes/script-loader.php</a></li>
<li><a href="#trunksrcwpincludesuserphp">trunk/src/wp-includes/user.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsrestapiwpIsRestEndpointphp">trunk/tests/phpunit/tests/rest-api/wpIsRestEndpoint.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswpqueryphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-query.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-query.php 2024-01-19 17:36:18 UTC (rev 57311)
+++ trunk/src/wp-includes/class-wp-query.php 2024-01-19 17:37:05 UTC (rev 57312)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1027,7 +1027,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! ( $this->is_singular || $this->is_archive || $this->is_search || $this->is_feed
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- || ( defined( 'REST_REQUEST' ) && REST_REQUEST && $this->is_main_query() )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ || ( wp_is_serving_rest_request() && $this->is_main_query() )
</ins><span class="cx" style="display: block; padding: 0 10px"> || $this->is_trackback || $this->is_404 || $this->is_admin || $this->is_robots || $this->is_favicon ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $this->is_home = true;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunksrcwpincludesdeprecatedphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/deprecated.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/deprecated.php 2024-01-19 17:36:18 UTC (rev 57311)
+++ trunk/src/wp-includes/deprecated.php 2024-01-19 17:37:05 UTC (rev 57312)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -5436,7 +5436,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $settings = WP_Theme_JSON_Resolver::get_merged_data()->get_settings();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // If in the editor, add webfonts defined in variations.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( is_admin() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( is_admin() || wp_is_rest_endpoint() ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $variations = WP_Theme_JSON_Resolver::get_style_variations();
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $variations as $variation ) {
</span><span class="cx" style="display: block; padding: 0 10px"> // Skip if fontFamilies are not defined in the variation.
</span></span></pre></div>
<a id="trunksrcwpincludesfunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/functions.php 2024-01-19 17:36:18 UTC (rev 57311)
+++ trunk/src/wp-includes/functions.php 2024-01-19 17:37:05 UTC (rev 57312)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3718,7 +3718,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @param callable $callback Callback function name.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> $callback = apply_filters( 'wp_die_json_handler', '_json_wp_die_handler' );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- } elseif ( defined( 'REST_REQUEST' ) && REST_REQUEST && wp_is_jsonp_request() ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ } elseif ( wp_is_serving_rest_request() && wp_is_jsonp_request() ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Filters the callback for killing WordPress execution for JSONP REST requests.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -4441,7 +4441,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @param int $flags Optional. Options to be passed to json_encode(). Default 0.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function wp_send_json( $response, $status_code = null, $flags = 0 ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( wp_is_serving_rest_request() ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> _doing_it_wrong(
</span><span class="cx" style="display: block; padding: 0 10px"> __FUNCTION__,
</span><span class="cx" style="display: block; padding: 0 10px"> sprintf(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -4697,6 +4697,23 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return $mce_init;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/**
+ * Determines whether WordPress is currently serving a REST API request.
+ *
+ * The function relies on the 'REST_REQUEST' global. As such, it only returns true when an actual REST _request_ is
+ * being made. It does not return true when a REST endpoint is hit as part of another request, e.g. for preloading a
+ * REST response. See {@see wp_is_rest_endpoint()} for that purpose.
+ *
+ * This function should not be called until the {@see 'parse_request'} action, as the constant is only defined then,
+ * even for an actual REST request.
+ *
+ * @since 6.5.0
+ *
+ * @return bool True if it's a WordPress REST API request, false otherwise.
+ */
+function wp_is_serving_rest_request() {
+ return defined( 'REST_REQUEST' ) && REST_REQUEST;
+}
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Converts smiley code to the icon graphic file equivalent.
</span></span></pre></div>
<a id="trunksrcwpincludesloadphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/load.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/load.php 2024-01-19 17:36:18 UTC (rev 57311)
+++ trunk/src/wp-includes/load.php 2024-01-19 17:37:05 UTC (rev 57312)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -598,6 +598,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> error_reporting( E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_ERROR | E_WARNING | E_PARSE | E_USER_ERROR | E_USER_WARNING | E_RECOVERABLE_ERROR );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /*
+ * The 'REST_REQUEST' check here is optimistic as the constant is most
+ * likely not set at this point even if it is in fact a REST request.
+ */
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( defined( 'XMLRPC_REQUEST' ) || defined( 'REST_REQUEST' ) || defined( 'MS_FILES_REQUEST' )
</span><span class="cx" style="display: block; padding: 0 10px"> || ( defined( 'WP_INSTALLING' ) && WP_INSTALLING )
</span><span class="cx" style="display: block; padding: 0 10px"> || wp_doing_ajax() || wp_is_json_request()
</span></span></pre></div>
<a id="trunksrcwpincludesrestapiclasswprestserverphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/rest-api/class-wp-rest-server.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/class-wp-rest-server.php 2024-01-19 17:36:18 UTC (rev 57311)
+++ trunk/src/wp-includes/rest-api/class-wp-rest-server.php 2024-01-19 17:37:05 UTC (rev 57312)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -88,6 +88,14 @@
</span><span class="cx" style="display: block; padding: 0 10px"> protected $embed_cache = array();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Stores request objects that are currently being handled.
+ *
+ * @since 6.5.0
+ * @var array
+ */
+ protected $dispatching_requests = array();
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Instantiates the REST server.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.4.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -983,6 +991,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @return WP_REST_Response Response returned by the callback.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function dispatch( $request ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->dispatching_requests[] = $request;
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Filters the pre-calculated result of a REST API dispatch request.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1008,6 +1018,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $result = $this->error_to_response( $result );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ array_pop( $this->dispatching_requests );
</ins><span class="cx" style="display: block; padding: 0 10px"> return $result;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1015,7 +1026,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $matched = $this->match_request_to_handler( $request );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( is_wp_error( $matched ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return $this->error_to_response( $matched );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $response = $this->error_to_response( $matched );
+ array_pop( $this->dispatching_requests );
+ return $response;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> list( $route, $handler ) = $matched;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1040,10 +1053,25 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return $this->respond_to_request( $request, $route, $handler, $error );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $response = $this->respond_to_request( $request, $route, $handler, $error );
+ array_pop( $this->dispatching_requests );
+ return $response;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Returns whether the REST server is currently dispatching / responding to a request.
+ *
+ * This may be a standalone REST API request, or an internal request dispatched from within a regular page load.
+ *
+ * @since 6.5.0
+ *
+ * @return bool Whether the REST server is currently handling a request.
+ */
+ public function is_dispatching() {
+ return (bool) $this->dispatching_requests;
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Matches a request object to its handler.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @access private
</span></span></pre></div>
<a id="trunksrcwpincludesrestapiendpointsclasswprestattachmentscontrollerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php 2024-01-19 17:36:18 UTC (rev 57311)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php 2024-01-19 17:37:05 UTC (rev 57312)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -201,7 +201,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> wp_after_insert_post( $attachment, false, null );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( wp_is_serving_rest_request() ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> /*
</span><span class="cx" style="display: block; padding: 0 10px"> * Set a custom header with the attachment_id.
</span><span class="cx" style="display: block; padding: 0 10px"> * Used by the browser/client to resume creating image sub-sizes after a PHP fatal error.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -630,7 +630,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> update_post_meta( $new_attachment_id, '_wp_attachment_image_alt', wp_slash( $image_alt ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( wp_is_serving_rest_request() ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> /*
</span><span class="cx" style="display: block; padding: 0 10px"> * Set a custom header with the attachment_id.
</span><span class="cx" style="display: block; padding: 0 10px"> * Used by the browser/client to resume creating image sub-sizes after a PHP fatal error.
</span></span></pre></div>
<a id="trunksrcwpincludesrestapiendpointsclasswpresturldetailscontrollerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-url-details-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-url-details-controller.php 2024-01-19 17:36:18 UTC (rev 57311)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-url-details-controller.php 2024-01-19 17:37:05 UTC (rev 57312)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -128,7 +128,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 5.9.0
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param WP_REST_REQUEST $request Full details about the request.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param WP_REST_Request $request Full details about the request.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @return WP_REST_Response|WP_Error The parsed details as a response object. WP_Error if there are errors.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function parse_url_details( $request ) {
</span></span></pre></div>
<a id="trunksrcwpincludesrestapiphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/rest-api.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api.php 2024-01-19 17:36:18 UTC (rev 57311)
+++ trunk/src/wp-includes/rest-api.php 2024-01-19 17:37:05 UTC (rev 57312)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -209,7 +209,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.4.0
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function rest_api_default_filters() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( wp_is_serving_rest_request() ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> // Deprecated reporting.
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'deprecated_function_run', 'rest_handle_deprecated_function', 10, 3 );
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'deprecated_function_trigger_error', '__return_false' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3389,3 +3389,38 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_REST_Response( $data, $status );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/**
+ * Checks whether a REST API endpoint request is currently being handled.
+ *
+ * This may be a standalone REST API request, or an internal request dispatched from within a regular page load.
+ *
+ * @since 6.5.0
+ *
+ * @global WP_REST_Server $wp_rest_server REST server instance.
+ *
+ * @return bool True if a REST endpoint request is currently being handled, false otherwise.
+ */
+function wp_is_rest_endpoint() {
+ /* @var WP_REST_Server $wp_rest_server */
+ global $wp_rest_server;
+
+ // Check whether this is a standalone REST request.
+ $is_rest_endpoint = wp_is_serving_rest_request();
+ if ( ! $is_rest_endpoint ) {
+ // Otherwise, check whether an internal REST request is currently being handled.
+ $is_rest_endpoint = isset( $wp_rest_server )
+ && $wp_rest_server->is_dispatching();
+ }
+
+ /**
+ * Filters whether a REST endpoint request is currently being handled.
+ *
+ * This may be a standalone REST API request, or an internal request dispatched from within a regular page load.
+ *
+ * @since 6.5.0
+ *
+ * @param bool $is_request_endpoint Whether a REST endpoint request is currently being handled.
+ */
+ return (bool) apply_filters( 'wp_is_rest_endpoint', $is_rest_endpoint );
+}
</ins></span></pre></div>
<a id="trunksrcwpincludesscriptloaderphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/script-loader.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/script-loader.php 2024-01-19 17:36:18 UTC (rev 57311)
+++ trunk/src/wp-includes/script-loader.php 2024-01-19 17:37:05 UTC (rev 57312)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2586,7 +2586,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @return bool Whether separate assets will be loaded.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function wp_should_load_separate_core_block_assets() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( is_admin() || is_feed() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( is_admin() || is_feed() || wp_is_rest_endpoint() ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return false;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunksrcwpincludesuserphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/user.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/user.php 2024-01-19 17:36:18 UTC (rev 57311)
+++ trunk/src/wp-includes/user.php 2024-01-19 17:37:05 UTC (rev 57312)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -333,6 +333,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return $input_user;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // The 'REST_REQUEST' check here may happen too early for the constant to be available.
</ins><span class="cx" style="display: block; padding: 0 10px"> $is_api_request = ( ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapiwpIsRestEndpointphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/rest-api/wpIsRestEndpoint.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/wpIsRestEndpoint.php (rev 0)
+++ trunk/tests/phpunit/tests/rest-api/wpIsRestEndpoint.php 2024-01-19 17:37:05 UTC (rev 57312)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,66 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * Tests for the `wp_is_rest_endpoint()` function.
+ *
+ * @group rest-api
+ * @covers ::wp_is_rest_endpoint
+ */
+class Tests_Media_Wp_Is_Rest_Endpoint extends WP_UnitTestCase {
+
+ /**
+ * Tests that `wp_is_rest_endpoint()` returns false by default.
+ *
+ * @ticket 42061
+ */
+ public function test_wp_is_rest_endpoint_default() {
+ $this->assertFalse( wp_is_rest_endpoint() );
+ }
+
+ /**
+ * Tests that `wp_is_rest_endpoint()` relies on whether the global REST server is dispatching.
+ *
+ * @ticket 42061
+ */
+ public function test_wp_is_rest_endpoint_via_global() {
+ global $wp_rest_server;
+
+ $wp_rest_server = new Spy_REST_Server();
+ do_action( 'rest_api_init', $wp_rest_server );
+
+ // The presence of a REST server itself won't set this to true.
+ $this->assertFalse( wp_is_rest_endpoint() );
+
+ // Set up filter to record value during dispatching.
+ $result_within_request = null;
+ add_filter(
+ 'rest_pre_dispatch',
+ function ( $result ) use ( &$result_within_request ) {
+ $result_within_request = wp_is_rest_endpoint();
+ return $result;
+ }
+ );
+
+ /*
+ * Dispatch a request (doesn't matter that it's invalid).
+ * This already is completed after this method call.
+ */
+ $wp_rest_server->dispatch( new WP_REST_Request() );
+
+ // Within that request, the function should have returned true.
+ $this->assertTrue( $result_within_request );
+
+ // After the dispatching, the function should return false again.
+ $this->assertFalse( wp_is_rest_endpoint() );
+ }
+
+ /**
+ * Tests that `wp_is_rest_endpoint()` returns a result enforced via filter.
+ *
+ * @ticket 42061
+ */
+ public function test_wp_is_rest_endpoint_via_filter() {
+ add_filter( 'wp_is_rest_endpoint', '__return_true' );
+ $this->assertTrue( wp_is_rest_endpoint() );
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/rest-api/wpIsRestEndpoint.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span></div>
</body>
</html>