Automattic\WooCommerce\Blocks\Domain\Services

Hydration::get_response_from_controller()privateWC 1.0

Helper method to generate GET response from a controller. Also fires the rest_request_after_callbacks backward compatibility.

Method of the class: Hydration{}

Return

false|Mixed|null. Response

Usage

// private - for code of main (parent) class only
$result = $this->get_response_from_controller( $controller_class, $path );
$controller_class(string) (required)
Controller class FQN that will respond to the request.
$path(string) (required)
Request path regex.

Hydration::get_response_from_controller() code WC 9.0.1

private function get_response_from_controller( $controller_class, $path ) {
	if ( null === $controller_class ) {
		return false;
	}

	$request           = new \WP_REST_Request( 'GET', $path );
	$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;
}