Automattic\WooCommerce\Blocks\Domain\Services
Hydration::get_response_from_controller
Helper method to generate GET response from a controller. Also fires the rest_request_after_callbacks backward compatibility.
Method of the class: Hydration{}
Hooks from the method
Returns
false|Mixed|null. Response
Usage
// private - for code of main (parent) class only $result = $this->get_response_from_controller( $controller_class, $path, $url_params, $query_params );
- $controller_class(string) (required)
- Controller class FQN that will respond to the request.
- $path(string) (required)
- Request path regex.
- $url_params(array)
- URL parameters extracted from route (e.g., ['id' => '123']).
Default:array() - $query_params(array)
- Query string parameters (e.g., ['key' => 'value']).
Default:array()
Hydration::get_response_from_controller() Hydration::get response from controller code WC 10.7.0
private function get_response_from_controller( $controller_class, $path, $url_params = array(), $query_params = array() ) {
if ( null === $controller_class ) {
return false;
}
$request = new \WP_REST_Request( 'GET', $path );
// Set URL parameters (from route segments like /products/123).
if ( ! empty( $url_params ) ) {
$request->set_url_params( $url_params );
}
// Set query parameters (from query string like ?key=value).
if ( ! empty( $query_params ) ) {
$request->set_query_params( $query_params );
}
$schema_controller = StoreApi::container()->get( SchemaController::class );
$controller = new $controller_class(
$schema_controller,
$schema_controller->get( $controller_class::SCHEMA_TYPE, $controller_class::SCHEMA_VERSION )
);
$controller_args = is_callable( array( $controller, 'get_args' ) ) ? $controller->get_args() : array();
if ( empty( $controller_args ) ) {
return false;
}
// Get the handler that responds to read request.
$handler = current(
array_filter(
$controller_args,
function ( $method_handler ) {
return is_array( $method_handler ) && isset( $method_handler['methods'] ) && \WP_REST_Server::READABLE === $method_handler['methods'];
}
)
);
if ( ! $handler ) {
return false;
}
/**
* Similar to WP core's `rest_dispatch_request` filter, this allows plugin to override hydrating the request.
* Allows backward compatibility with the `rest_dispatch_request` filter by providing the same arguments.
*
* @since 8.9.0
*
* @param mixed $hydration_result Result of the hydration. If not null, this will be used as the response.
* @param WP_REST_Request $request Request used to generate the response.
* @param string $path Request path matched for the request..
* @param array $handler Route handler used for the request.
*/
$hydration_result = apply_filters( 'woocommerce_hydration_dispatch_request', null, $request, $path, $handler );
if ( null !== $hydration_result ) {
$response = $hydration_result;
} else {
$response = call_user_func_array( $handler['callback'], array( $request ) );
}
/**
* Similar to WP core's `rest_request_after_callbacks` filter, this allows to modify the response after it has been generated.
* Allows backward compatibility with the `rest_request_after_callbacks` filter by providing the same arguments.
*
* @since 8.9.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( 'woocommerce_hydration_request_after_callbacks', $response, $handler, $request );
return $response;
}