REST API: Refactor `WP_REST_Server::dispatch()` to make internal logic reusable.
#50244 aims to introduce batch processing in the REST API. An important feature is the ability to enforce that all requests have valid data before executing the route callbacks in "pre-validate" mode. This necessitates splitting `WP_REST_Server::dispatch()` into two methods so the batch controller can determine the request handler to perform pre-validation and then respond to the requests. The two new methods, `match_request_to_handler` and `respond_to_request`, have a public visibility, but are marked as `@access private`. This is to allow for iteration on the batch controller to happen in the Gutenberg repository. Developers should not rely upon these methods, their visibility may change in the future. See #50244. Props andraganescu, zieladam, TimothyBlynJacobs. Built from https://develop.svn.wordpress.org/trunk@48947 git-svn-id: http://core.svn.wordpress.org/trunk@48709 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
93b8183327
commit
a63708debf
|
@ -911,6 +911,48 @@ class WP_REST_Server {
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$error = null;
|
||||||
|
$matched = $this->match_request_to_handler( $request );
|
||||||
|
|
||||||
|
if ( is_wp_error( $matched ) ) {
|
||||||
|
return $this->error_to_response( $matched );
|
||||||
|
}
|
||||||
|
|
||||||
|
list( $route, $handler ) = $matched;
|
||||||
|
|
||||||
|
if ( ! is_callable( $handler['callback'] ) ) {
|
||||||
|
$error = new WP_Error(
|
||||||
|
'rest_invalid_handler',
|
||||||
|
__( 'The handler for the route is invalid' ),
|
||||||
|
array( 'status' => 500 )
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( ! is_wp_error( $error ) ) {
|
||||||
|
$check_required = $request->has_valid_params();
|
||||||
|
if ( is_wp_error( $check_required ) ) {
|
||||||
|
$error = $check_required;
|
||||||
|
} else {
|
||||||
|
$check_sanitized = $request->sanitize_params();
|
||||||
|
if ( is_wp_error( $check_sanitized ) ) {
|
||||||
|
$error = $check_sanitized;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->respond_to_request( $request, $route, $handler, $error );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Matches a request object to it's handler.
|
||||||
|
*
|
||||||
|
* @access private
|
||||||
|
* @since 5.6.0
|
||||||
|
*
|
||||||
|
* @param WP_REST_Request $request The request object.
|
||||||
|
* @return array|WP_Error The route and request handler on success or a WP_Error instance if no handler was found.
|
||||||
|
*/
|
||||||
|
public function match_request_to_handler( $request ) {
|
||||||
$method = $request->get_method();
|
$method = $request->get_method();
|
||||||
$path = $request->get_route();
|
$path = $request->get_route();
|
||||||
|
|
||||||
|
@ -957,144 +999,138 @@ class WP_REST_Server {
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ! is_callable( $callback ) ) {
|
if ( ! is_callable( $callback ) ) {
|
||||||
$response = new WP_Error(
|
return array( $route, $handler );
|
||||||
'rest_invalid_handler',
|
|
||||||
__( 'The handler for the route is invalid' ),
|
|
||||||
array( 'status' => 500 )
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ! is_wp_error( $response ) ) {
|
$request->set_url_params( $args );
|
||||||
// Remove the redundant preg_match argument.
|
$request->set_attributes( $handler );
|
||||||
unset( $args[0] );
|
|
||||||
|
|
||||||
$request->set_url_params( $args );
|
$defaults = array();
|
||||||
$request->set_attributes( $handler );
|
|
||||||
|
|
||||||
$defaults = array();
|
foreach ( $handler['args'] as $arg => $options ) {
|
||||||
|
if ( isset( $options['default'] ) ) {
|
||||||
foreach ( $handler['args'] as $arg => $options ) {
|
$defaults[ $arg ] = $options['default'];
|
||||||
if ( isset( $options['default'] ) ) {
|
|
||||||
$defaults[ $arg ] = $options['default'];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$request->set_default_params( $defaults );
|
|
||||||
|
|
||||||
$check_required = $request->has_valid_params();
|
|
||||||
if ( is_wp_error( $check_required ) ) {
|
|
||||||
$response = $check_required;
|
|
||||||
} else {
|
|
||||||
$check_sanitized = $request->sanitize_params();
|
|
||||||
if ( is_wp_error( $check_sanitized ) ) {
|
|
||||||
$response = $check_sanitized;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
$request->set_default_params( $defaults );
|
||||||
* Filters the response before executing any REST API callbacks.
|
|
||||||
*
|
|
||||||
* Allows plugins to perform additional validation after a
|
|
||||||
* request is initialized and matched to a registered route,
|
|
||||||
* but before it is executed.
|
|
||||||
*
|
|
||||||
* Note that this filter will not be called for requests that
|
|
||||||
* fail to authenticate or match to a registered route.
|
|
||||||
*
|
|
||||||
* @since 4.7.0
|
|
||||||
*
|
|
||||||
* @param WP_REST_Response|WP_HTTP_Response|WP_Error|mixed $response Result to send to the client. Usually a WP_REST_Response or WP_Error.
|
|
||||||
* @param array $handler Route handler used for the request.
|
|
||||||
* @param WP_REST_Request $request Request used to generate the response.
|
|
||||||
*/
|
|
||||||
$response = apply_filters( 'rest_request_before_callbacks', $response, $handler, $request );
|
|
||||||
|
|
||||||
if ( ! is_wp_error( $response ) ) {
|
return array( $route, $handler );
|
||||||
// Check permission specified on the route.
|
|
||||||
if ( ! empty( $handler['permission_callback'] ) ) {
|
|
||||||
$permission = call_user_func( $handler['permission_callback'], $request );
|
|
||||||
|
|
||||||
if ( is_wp_error( $permission ) ) {
|
|
||||||
$response = $permission;
|
|
||||||
} elseif ( false === $permission || null === $permission ) {
|
|
||||||
$response = new WP_Error(
|
|
||||||
'rest_forbidden',
|
|
||||||
__( 'Sorry, you are not allowed to do that.' ),
|
|
||||||
array( 'status' => rest_authorization_required_code() )
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( ! is_wp_error( $response ) ) {
|
|
||||||
/**
|
|
||||||
* Filters the REST dispatch request result.
|
|
||||||
*
|
|
||||||
* Allow plugins to override dispatching the request.
|
|
||||||
*
|
|
||||||
* @since 4.4.0
|
|
||||||
* @since 4.5.0 Added `$route` and `$handler` parameters.
|
|
||||||
*
|
|
||||||
* @param mixed $dispatch_result Dispatch result, will be used if not empty.
|
|
||||||
* @param WP_REST_Request $request Request used to generate the response.
|
|
||||||
* @param string $route Route matched for the request.
|
|
||||||
* @param array $handler Route handler used for the request.
|
|
||||||
*/
|
|
||||||
$dispatch_result = apply_filters( 'rest_dispatch_request', null, $request, $route, $handler );
|
|
||||||
|
|
||||||
// Allow plugins to halt the request via this filter.
|
|
||||||
if ( null !== $dispatch_result ) {
|
|
||||||
$response = $dispatch_result;
|
|
||||||
} else {
|
|
||||||
$response = call_user_func( $callback, $request );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Filters the response immediately after executing any REST API
|
|
||||||
* callbacks.
|
|
||||||
*
|
|
||||||
* Allows plugins to perform any needed cleanup, for example,
|
|
||||||
* to undo changes made during the {@see 'rest_request_before_callbacks'}
|
|
||||||
* filter.
|
|
||||||
*
|
|
||||||
* Note that this filter will not be called for requests that
|
|
||||||
* fail to authenticate or match to a registered route.
|
|
||||||
*
|
|
||||||
* Note that an endpoint's `permission_callback` can still be
|
|
||||||
* called after this filter - see `rest_send_allow_header()`.
|
|
||||||
*
|
|
||||||
* @since 4.7.0
|
|
||||||
*
|
|
||||||
* @param WP_REST_Response|WP_HTTP_Response|WP_Error|mixed $response Result to send to the client. Usually a WP_REST_Response or WP_Error.
|
|
||||||
* @param array $handler Route handler used for the request.
|
|
||||||
* @param WP_REST_Request $request Request used to generate the response.
|
|
||||||
*/
|
|
||||||
$response = apply_filters( 'rest_request_after_callbacks', $response, $handler, $request );
|
|
||||||
|
|
||||||
if ( is_wp_error( $response ) ) {
|
|
||||||
$response = $this->error_to_response( $response );
|
|
||||||
} else {
|
|
||||||
$response = rest_ensure_response( $response );
|
|
||||||
}
|
|
||||||
|
|
||||||
$response->set_matched_route( $route );
|
|
||||||
$response->set_matched_handler( $handler );
|
|
||||||
|
|
||||||
return $response;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->error_to_response(
|
return new WP_Error(
|
||||||
new WP_Error(
|
'rest_no_route',
|
||||||
'rest_no_route',
|
__( 'No route was found matching the URL and request method' ),
|
||||||
__( 'No route was found matching the URL and request method' ),
|
array( 'status' => 404 )
|
||||||
array( 'status' => 404 )
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dispatches the request to the callback handler.
|
||||||
|
*
|
||||||
|
* @access private
|
||||||
|
* @since 5.6.0
|
||||||
|
*
|
||||||
|
* @param WP_REST_Request $request The request object.
|
||||||
|
* @param array $handler The matched route handler.
|
||||||
|
* @param string $route The matched route regex.
|
||||||
|
* @param WP_Error|null $response The current error object if any.
|
||||||
|
*
|
||||||
|
* @return WP_REST_Response
|
||||||
|
*/
|
||||||
|
public function respond_to_request( $request, $route, $handler, $response ) {
|
||||||
|
/**
|
||||||
|
* Filters the response before executing any REST API callbacks.
|
||||||
|
*
|
||||||
|
* Allows plugins to perform additional validation after a
|
||||||
|
* request is initialized and matched to a registered route,
|
||||||
|
* but before it is executed.
|
||||||
|
*
|
||||||
|
* Note that this filter will not be called for requests that
|
||||||
|
* fail to authenticate or match to a registered route.
|
||||||
|
*
|
||||||
|
* @since 4.7.0
|
||||||
|
*
|
||||||
|
* @param WP_REST_Response|WP_HTTP_Response|WP_Error|mixed $response Result to send to the client. Usually a WP_REST_Response or WP_Error.
|
||||||
|
* @param array $handler Route handler used for the request.
|
||||||
|
* @param WP_REST_Request $request Request used to generate the response.
|
||||||
|
*/
|
||||||
|
$response = apply_filters( 'rest_request_before_callbacks', $response, $handler, $request );
|
||||||
|
|
||||||
|
// Check permission specified on the route.
|
||||||
|
if ( ! is_wp_error( $response ) && ! empty( $handler['permission_callback'] ) ) {
|
||||||
|
$permission = call_user_func( $handler['permission_callback'], $request );
|
||||||
|
|
||||||
|
if ( is_wp_error( $permission ) ) {
|
||||||
|
$response = $permission;
|
||||||
|
} elseif ( false === $permission || null === $permission ) {
|
||||||
|
$response = new WP_Error(
|
||||||
|
'rest_forbidden',
|
||||||
|
__( 'Sorry, you are not allowed to do that.' ),
|
||||||
|
array( 'status' => rest_authorization_required_code() )
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( ! is_wp_error( $response ) ) {
|
||||||
|
/**
|
||||||
|
* Filters the REST dispatch request result.
|
||||||
|
*
|
||||||
|
* Allow plugins to override dispatching the request.
|
||||||
|
*
|
||||||
|
* @since 4.4.0
|
||||||
|
* @since 4.5.0 Added `$route` and `$handler` parameters.
|
||||||
|
*
|
||||||
|
* @param mixed $dispatch_result Dispatch result, will be used if not empty.
|
||||||
|
* @param WP_REST_Request $request Request used to generate the response.
|
||||||
|
* @param string $route Route matched for the request.
|
||||||
|
* @param array $handler Route handler used for the request.
|
||||||
|
*/
|
||||||
|
$dispatch_result = apply_filters( 'rest_dispatch_request', null, $request, $route, $handler );
|
||||||
|
|
||||||
|
// Allow plugins to halt the request via this filter.
|
||||||
|
if ( null !== $dispatch_result ) {
|
||||||
|
$response = $dispatch_result;
|
||||||
|
} else {
|
||||||
|
$response = call_user_func( $handler['callback'], $request );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filters the response immediately after executing any REST API
|
||||||
|
* callbacks.
|
||||||
|
*
|
||||||
|
* Allows plugins to perform any needed cleanup, for example,
|
||||||
|
* to undo changes made during the {@see 'rest_request_before_callbacks'}
|
||||||
|
* filter.
|
||||||
|
*
|
||||||
|
* Note that this filter will not be called for requests that
|
||||||
|
* fail to authenticate or match to a registered route.
|
||||||
|
*
|
||||||
|
* Note that an endpoint's `permission_callback` can still be
|
||||||
|
* called after this filter - see `rest_send_allow_header()`.
|
||||||
|
*
|
||||||
|
* @since 4.7.0
|
||||||
|
*
|
||||||
|
* @param WP_REST_Response|WP_HTTP_Response|WP_Error|mixed $response Result to send to the client. Usually a WP_REST_Response or WP_Error.
|
||||||
|
* @param array $handler Route handler used for the request.
|
||||||
|
* @param WP_REST_Request $request Request used to generate the response.
|
||||||
|
*/
|
||||||
|
$response = apply_filters( 'rest_request_after_callbacks', $response, $handler, $request );
|
||||||
|
|
||||||
|
if ( is_wp_error( $response ) ) {
|
||||||
|
$response = $this->error_to_response( $response );
|
||||||
|
} else {
|
||||||
|
$response = rest_ensure_response( $response );
|
||||||
|
}
|
||||||
|
|
||||||
|
$response->set_matched_route( $route );
|
||||||
|
$response->set_matched_handler( $handler );
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns if an error occurred during most recent JSON encode/decode.
|
* Returns if an error occurred during most recent JSON encode/decode.
|
||||||
*
|
*
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
*
|
*
|
||||||
* @global string $wp_version
|
* @global string $wp_version
|
||||||
*/
|
*/
|
||||||
$wp_version = '5.6-alpha-48946';
|
$wp_version = '5.6-alpha-48947';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||||
|
|
Loading…
Reference in New Issue