diff options
Diffstat (limited to '')
-rw-r--r-- | wp-includes/rest-api/class-wp-rest-server.php | 51 |
1 files changed, 44 insertions, 7 deletions
diff --git a/wp-includes/rest-api/class-wp-rest-server.php b/wp-includes/rest-api/class-wp-rest-server.php index a1bc4b9..b85c020 100644 --- a/wp-includes/rest-api/class-wp-rest-server.php +++ b/wp-includes/rest-api/class-wp-rest-server.php @@ -88,6 +88,14 @@ class WP_REST_Server { protected $embed_cache = array(); /** + * Stores request objects that are currently being handled. + * + * @since 6.5.0 + * @var array + */ + protected $dispatching_requests = array(); + + /** * Instantiates the REST server. * * @since 4.4.0 @@ -467,18 +475,20 @@ class WP_REST_Server { $this->set_status( $code ); /** - * Filters whether to send nocache headers on a REST API request. + * Filters whether to send no-cache headers on a REST API request. * * @since 4.4.0 - * @since 6.3.2 Moved the block to catch the filter added on rest_cookie_check_errors() from rest-api.php + * @since 6.3.2 Moved the block to catch the filter added on rest_cookie_check_errors() from wp-includes/rest-api.php. * * @param bool $rest_send_nocache_headers Whether to send no-cache headers. */ $send_no_cache_headers = apply_filters( 'rest_send_nocache_headers', is_user_logged_in() ); - // send no cache headers if the $send_no_cache_headers is true - // OR if the HTTP_X_HTTP_METHOD_OVERRIDE is used but resulted a 4x response code. - if ( $send_no_cache_headers || ( true === $method_overridden && strpos( $code, '4' ) === 0 ) ) { + /* + * Send no-cache headers if $send_no_cache_headers is true, + * OR if the HTTP_X_HTTP_METHOD_OVERRIDE is used but resulted a 4xx response code. + */ + if ( $send_no_cache_headers || ( true === $method_overridden && str_starts_with( $code, '4' ) ) ) { foreach ( wp_get_nocache_headers() as $header => $header_value ) { if ( empty( $header_value ) ) { $this->remove_header( $header ); @@ -738,6 +748,13 @@ class WP_REST_Server { $request['context'] = 'embed'; } + if ( empty( $request['per_page'] ) ) { + $matched = $this->match_request_to_handler( $request ); + if ( ! is_wp_error( $matched ) && isset( $matched[1]['args']['per_page']['maximum'] ) ) { + $request['per_page'] = (int) $matched[1]['args']['per_page']['maximum']; + } + } + $response = $this->dispatch( $request ); /** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */ @@ -981,6 +998,8 @@ class WP_REST_Server { * @return WP_REST_Response Response returned by the callback. */ public function dispatch( $request ) { + $this->dispatching_requests[] = $request; + /** * Filters the pre-calculated result of a REST API dispatch request. * @@ -1006,6 +1025,7 @@ class WP_REST_Server { $result = $this->error_to_response( $result ); } + array_pop( $this->dispatching_requests ); return $result; } @@ -1013,7 +1033,9 @@ class WP_REST_Server { $matched = $this->match_request_to_handler( $request ); if ( is_wp_error( $matched ) ) { - return $this->error_to_response( $matched ); + $response = $this->error_to_response( $matched ); + array_pop( $this->dispatching_requests ); + return $response; } list( $route, $handler ) = $matched; @@ -1038,7 +1060,22 @@ class WP_REST_Server { } } - return $this->respond_to_request( $request, $route, $handler, $error ); + $response = $this->respond_to_request( $request, $route, $handler, $error ); + array_pop( $this->dispatching_requests ); + return $response; + } + + /** + * 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; } /** |