get_status example

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 );
}

/** * Handles _deprecated_function() errors. * * @since 4.4.0 * * @param string $function_name The function that was called. * @param string $replacement The function that should have been called. * @param string $version Version. */
// Wrap the response in an envelope if asked for.         if ( isset( $_GET['_envelope'] ) ) {
            $embed  = isset( $_GET['_embed'] ) ? rest_parse_embed_param( $_GET['_embed'] ) : false;
            $result = $this->envelope_response( $result$embed );
        }

        // Send extra data from response objects.         $headers = $result->get_headers();
        $this->send_headers( $headers );

        $code = $result->get_status();
        $this->set_status( $code );

        /** * Filters whether the REST API request has already been served. * * Allow sending the request manually - by returning true, the API result * will not be sent to the client. * * @since 4.4.0 * * @param bool $served Whether the request has already been served. * Default false. * @param WP_HTTP_Response $result Result to send to the client. Usually a `WP_REST_Response`. * @param WP_REST_Request $request Request used to generate the response. * @param WP_REST_Server $server Server instance. */
$this->matched_handler = $handler;
    }

    /** * Checks if the response is an error, i.e. >= 400 response code. * * @since 4.4.0 * * @return bool Whether the response is an error. */
    public function is_error() {
        return $this->get_status() >= 400;
    }

    /** * Retrieves a WP_Error object from the response. * * @since 4.4.0 * * @return WP_Error|null WP_Error or null on not an errored response. */
    public function as_error() {
        if ( ! $this->is_error() ) {
            

    public function to_array() {
        return array(
            'headers'  => $this->get_headers(),
            'body'     => $this->get_data(),
            'response' => array(
                'code'    => $this->get_status(),
                'message' => get_status_header_desc( $this->get_status() ),
            ),
            'cookies'  => $this->get_cookies(),
            'filename' => $this->filename,
        );
    }
}
Home | Imprint | This part of the site doesn't use cookies.