Rest API: Ensure `rest_ensure_response()` upgrades `WP_HTTP_Response` to `WP_REST_Response`.

An instance of `WP_HTTP_Response` doesn't ensure that the required methods used in `WP_REST_Server::dispatch()` exist, currently causing a fatal error.

Props ali11007, TimothyBlynJacobs, ocean90.
Fixes #49495.
Built from https://develop.svn.wordpress.org/trunk@47849


git-svn-id: http://core.svn.wordpress.org/trunk@47625 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Dominik Schilling 2020-05-23 14:36:09 +00:00
parent b9751d4efe
commit aaeab2f3f5
3 changed files with 23 additions and 13 deletions

View File

@ -506,26 +506,36 @@ function rest_ensure_request( $request ) {
/**
* Ensures a REST response is a response object (for consistency).
*
* This implements WP_HTTP_Response, allowing usage of `set_status`/`header`/etc
* This implements WP_REST_Response, allowing usage of `set_status`/`header`/etc
* without needing to double-check the object. Will also allow WP_Error to indicate error
* responses, so users should immediately check for this value.
*
* @since 4.4.0
*
* @param WP_HTTP_Response|WP_Error|mixed $response Response to check.
* @return WP_REST_Response|mixed If response generated an error, WP_Error, if response
* is already an instance, WP_HTTP_Response, otherwise
* returns a new WP_REST_Response instance.
* @param WP_REST_Response|WP_Error|WP_HTTP_Response|mixed $response Response to check.
* @return WP_REST_Response|WP_Error If response generated an error, WP_Error, if response
* is already an instance, WP_REST_Response, otherwise
* returns a new WP_REST_Response instance.
*/
function rest_ensure_response( $response ) {
if ( is_wp_error( $response ) ) {
return $response;
}
if ( $response instanceof WP_HTTP_Response ) {
if ( $response instanceof WP_REST_Response ) {
return $response;
}
// While WP_HTTP_Response is the base class of WP_REST_Response, it doesn't provide
// all the required methods used in WP_REST_Server::dispatch().
if ( $response instanceof WP_HTTP_Response ) {
return new WP_REST_Response(
$response->get_data(),
$response->get_status(),
$response->get_headers()
);
}
return new WP_REST_Response( $response );
}

View File

@ -969,9 +969,9 @@ class WP_REST_Server {
*
* @since 4.7.0
*
* @param WP_HTTP_Response|WP_Error $response Result to send to the client. Usually a WP_REST_Response or WP_Error.
* @param array $handler Route handler used for the request.
* @param WP_REST_Request $request Request used to generate the response.
* @param WP_REST_Response|WP_HTTP_Response|WP_Error|mixed $response Result to send to the client. Usually a WP_REST_Response or WP_Error.
* @param array $handler Route handler used for the request.
* @param WP_REST_Request $request Request used to generate the response.
*/
$response = apply_filters( 'rest_request_before_callbacks', $response, $handler, $request );
@ -1032,9 +1032,9 @@ class WP_REST_Server {
*
* @since 4.7.0
*
* @param WP_HTTP_Response|WP_Error $response Result to send to the client. Usually a WP_REST_Response or WP_Error.
* @param array $handler Route handler used for the request.
* @param WP_REST_Request $request Request used to generate the response.
* @param WP_REST_Response|WP_HTTP_Response|WP_Error|mixed $response Result to send to the client. Usually a WP_REST_Response or WP_Error.
* @param array $handler Route handler used for the request.
* @param WP_REST_Request $request Request used to generate the response.
*/
$response = apply_filters( 'rest_request_after_callbacks', $response, $handler, $request );

View File

@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.5-alpha-47848';
$wp_version = '5.5-alpha-47849';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.