From aaeab2f3f5a06b266b6a83a3776b514c77a1d475 Mon Sep 17 00:00:00 2001 From: Dominik Schilling Date: Sat, 23 May 2020 14:36:09 +0000 Subject: [PATCH] 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 --- wp-includes/rest-api.php | 22 ++++++++++++++----- wp-includes/rest-api/class-wp-rest-server.php | 12 +++++----- wp-includes/version.php | 2 +- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/wp-includes/rest-api.php b/wp-includes/rest-api.php index 11eb2b71ad..e408522ab6 100644 --- a/wp-includes/rest-api.php +++ b/wp-includes/rest-api.php @@ -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 ); } diff --git a/wp-includes/rest-api/class-wp-rest-server.php b/wp-includes/rest-api/class-wp-rest-server.php index 771c3b9fbe..0ab94bbbde 100644 --- a/wp-includes/rest-api/class-wp-rest-server.php +++ b/wp-includes/rest-api/class-wp-rest-server.php @@ -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 ); diff --git a/wp-includes/version.php b/wp-includes/version.php index 1d5be769f0..ae6b843524 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.