rest_ensure_response

The timeline below displays how wordpress function rest_ensure_response has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * Ensures a REST response is a response object (for consistency).
 *
 * 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_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_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);
}

WordPress Version: 5.5

/**
 * Ensures a REST response is a response object (for consistency).
 *
 * 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_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_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);
}

WordPress Version: 5.4

/**
 * Ensures a REST response is a response object (for consistency).
 *
 * This implements WP_HTTP_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.
 */
function rest_ensure_response($response)
{
    if (is_wp_error($response)) {
        return $response;
    }
    if ($response instanceof WP_HTTP_Response) {
        return $response;
    }
    return new WP_REST_Response($response);
}

WordPress Version: 4.7

/**
 * Ensures a REST response is a response object (for consistency).
 *
 * This implements WP_HTTP_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_Error|WP_HTTP_Response|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.
 */
function rest_ensure_response($response)
{
    if (is_wp_error($response)) {
        return $response;
    }
    if ($response instanceof WP_HTTP_Response) {
        return $response;
    }
    return new WP_REST_Response($response);
}

WordPress Version: 4.4

/**
 * Ensures a REST response is a response object (for consistency).
 *
 * This implements WP_HTTP_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_Error|WP_HTTP_Response|mixed $response Response to check.
 * @return mixed WP_Error if response generated an error, WP_HTTP_Response if response
 *               is a already an instance, 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) {
        return $response;
    }
    return new WP_REST_Response($response);
}