Automattic\WooCommerce\Internal\Abilities\REST
RestAbilityFactory::execute_operation
Execute the REST operation.
Method of the class: RestAbilityFactory{}
No Hooks.
Returns
Mixed. Operation result.
Usage
$result = RestAbilityFactory::execute_operation( $controller, $operation, $input, $route );
- $controller(object) (required)
- REST controller instance.
- $operation(string) (required)
- Operation type.
- $input(array) (required)
- Input parameters.
- $route(string) (required)
- REST route for this controller.
RestAbilityFactory::execute_operation() RestAbilityFactory::execute operation code WC 10.7.0
private static function execute_operation( $controller, string $operation, array $input, string $route ) {
$method = self::get_http_method_for_operation( $operation );
// Build final route - add ID for single item operations.
$request_route = $route;
if ( isset( $input['id'] ) && in_array( $operation, array( 'get', 'update', 'delete' ), true ) ) {
$request_route .= '/' . intval( $input['id'] );
unset( $input['id'] );
}
// Create REST request.
$request = new \WP_REST_Request( $method, $request_route );
foreach ( $input as $key => $value ) {
$request->set_param( $key, $value );
}
// Dispatch through REST API for proper validation and permissions.
$response = rest_do_request( $request );
if ( is_wp_error( $response ) ) {
return $response;
}
$data = $response instanceof \WP_REST_Response ? $response->get_data() : $response;
// For list operations, wrap in data object to match schema.
if ( 'list' === $operation ) {
return array( 'data' => $data );
}
return $data;
}