Ensures a REST response is a response object (for consistency).
Description
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.
678functionrest_ensure_response($response){679if(is_wp_error($response)){680return$response;681}682683if($responseinstanceofWP_REST_Response){684return$response;685}686687/*688 * While WP_HTTP_Response is the base class of WP_REST_Response, it doesn't provide689 * all the required methods used in WP_REST_Server::dispatch().690 */691if($responseinstanceofWP_HTTP_Response){692returnnewWP_REST_Response(693$response->get_data(),694$response->get_status(),695$response->get_headers()696);697}698699returnnewWP_REST_Response($response);700}
History
Introduced in 4.4.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-includes/rest-api.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.