确保REST响应是响应对象(为了保持一致性)。
原型
rest_ensure_response( WP_Error|WP_HTTP_Response|mixed $response )
描述
这实现了WP_HTTP_Response,允许使用set_status / header / etc而无需仔细检查对象。还将允许WP_Error指示错误响应,因此用户应立即检查此值。
参数
$response
(WP_Error|WP_HTTP_Response|mixed)
(Required)
回复检查。
返回值
(WP_REST_Response|mixed)
如果响应产生错误,
源文件
路径:wp-includes/rest-api.php
<?php
...
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 );
}
...
?>
其他
英文文档:https://developer.wordpress.org/reference/functions/rest_ensure_response/