rest_ensure_response()

最后更新于:2021-11-27 23:34:39

rest_ensure_response( GC_REST_Response|GC_Error|GC_|mixed$response)

Ensures a REST response is a response object (for consistency).

参数

$response

(GC_REST_Response|GC_Error|GC_HTTP_Response|mixed) (Required) Response to check.

响应

(GC_REST_Response|GC_Error) If response generated an error, GC_Error, if response is already an instance, GC_REST_Response, otherwise returns a new GC_REST_Response instance.

源文件

文件: gc-includes/rest-api.php

function rest_ensure_response( $response ) {
	if ( is_gc_error( $response ) ) {
		return $response;
	}

	if ( $response instanceof GC_REST_Response ) {
		return $response;
	}

	// While GC_HTTP_Response is the base class of GC_REST_Response, it doesn't provide
	// all the required methods used in GC_REST_Server::dispatch().
	if ( $response instanceof GC_HTTP_Response ) {
		return new GC_REST_Response(
			$response->get_data(),
			$response->get_status(),
			$response->get_headers()
		);
	}

	return new GC_REST_Response( $response );
}