| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * REST API run controller for Abilities API. 4 * 5 * @package WordPress 6 * @subpackage Abilities_API 7 * @since 6.9.0 8 */ 9 10 declare( strict_types = 1 ); 11 12 /** 13 * Core controller used to execute abilities via the REST API. 14 * 15 * @since 6.9.0 16 * 17 * @see WP_REST_Controller 18 */ 19 class WP_REST_Abilities_V1_Run_Controller extends WP_REST_Controller { 20 21 /** 22 * REST API namespace. 23 * 24 * @since 6.9.0 25 * @var string 26 */ 27 protected $namespace = 'wp-abilities/v1'; 28 29 /** 30 * REST API base route. 31 * 32 * @since 6.9.0 33 * @var string 34 */ 35 protected $rest_base = 'abilities'; 36 37 /** 38 * Registers the routes for ability execution. 39 * 40 * @since 6.9.0 41 * 42 * @see register_rest_route() 43 */ 44 public function register_routes(): void { 45 register_rest_route( 46 $this->namespace, 47 '/' . $this->rest_base . '/(?P<name>[a-zA-Z0-9\-\/]+?)/run', 48 array( 49 'args' => array( 50 'name' => array( 51 'description' => __( 'Unique identifier for the ability.' ), 52 'type' => 'string', 53 'pattern' => '^[a-zA-Z0-9\-\/]+$', 54 ), 55 ), 56 57 // TODO: We register ALLMETHODS because at route registration time, we don't know which abilities 58 // exist or their annotations (`destructive`, `idempotent`, `readonly`). This is due to WordPress 59 // load order - routes are registered early, before plugins have registered their abilities. 60 // This approach works but could be improved with lazy route registration or a different 61 // architecture that allows type-specific routes after abilities are registered. 62 // This was the same issue that we ended up seeing with the Feature API. 63 array( 64 'methods' => WP_REST_Server::ALLMETHODS, 65 'callback' => array( $this, 'execute_ability' ), 66 'permission_callback' => array( $this, 'check_ability_permissions' ), 67 'args' => $this->get_run_args(), 68 69 /* 70 * Serve the ability result as returned, so it follows the ability's output 71 * schema whether it is an object, an array, or a scalar value: `_fields`, 72 * `_embed` and response links leave it unchanged. 73 */ 74 'data_processing' => false, 75 ), 76 'schema' => array( $this, 'get_run_schema' ), 77 ) 78 ); 79 } 80 81 /** 82 * Executes an ability. 83 * 84 * @since 6.9.0 85 * 86 * @param WP_REST_Request $request Full details about the request. 87 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. 88 */ 89 public function execute_ability( $request ) { 90 $ability = wp_has_ability( $request['name'] ) ? wp_get_ability( $request['name'] ) : null; 91 if ( ! $ability ) { 92 return new WP_Error( 93 'rest_ability_not_found', 94 __( 'Ability not found.' ), 95 array( 'status' => 404 ) 96 ); 97 } 98 99 $input = $this->get_input_from_request( $request ); 100 $result = $ability->execute( $input ); 101 if ( is_wp_error( $result ) ) { 102 return $result; 103 } 104 105 /* 106 * Preserve empty objects when encoding the result as JSON. Preparation can 107 * convert empty PHP arrays to stdClass, also within object results. 108 */ 109 $result = wp_prepare_json_value_for_client( $result, $ability->get_output_schema() ); 110 111 return rest_ensure_response( $result ); 112 } 113 114 /** 115 * Validates if the HTTP method matches the expected method for the ability based on its annotations. 116 * 117 * @since 6.9.0 118 * 119 * @param string $request_method The HTTP method of the request. 120 * @param array<string, (null|bool)> $annotations The ability annotations. 121 * @return true|WP_Error True on success, or WP_Error object on failure. 122 */ 123 public function validate_request_method( string $request_method, array $annotations ) { 124 $expected_method = 'POST'; 125 if ( ! empty( $annotations['readonly'] ) ) { 126 $expected_method = 'GET'; 127 } elseif ( ! empty( $annotations['destructive'] ) && ! empty( $annotations['idempotent'] ) ) { 128 $expected_method = 'DELETE'; 129 } 130 131 if ( $expected_method === $request_method ) { 132 return true; 133 } 134 135 $error_message = __( 'Abilities that perform updates require POST method.' ); 136 if ( 'GET' === $expected_method ) { 137 $error_message = __( 'Read-only abilities require GET method.' ); 138 } elseif ( 'DELETE' === $expected_method ) { 139 $error_message = __( 'Abilities that perform destructive actions require DELETE method.' ); 140 } 141 return new WP_Error( 142 'rest_ability_invalid_method', 143 $error_message, 144 array( 'status' => 405 ) 145 ); 146 } 147 148 /** 149 * Checks if a given request has permission to execute a specific ability. 150 * 151 * @since 6.9.0 152 * @since 7.2.0 Prepares the input read from the raw request. 153 * 154 * @param WP_REST_Request $request Full details about the request. 155 * @return true|WP_Error True if the request has execution permission, WP_Error object otherwise. 156 */ 157 public function check_ability_permissions( $request ) { 158 $ability = wp_has_ability( $request['name'] ) ? wp_get_ability( $request['name'] ) : null; 159 if ( ! $ability || ! $ability->get_meta_item( 'show_in_rest' ) ) { 160 return new WP_Error( 161 'rest_ability_not_found', 162 __( 'Ability not found.' ), 163 array( 'status' => 404 ) 164 ); 165 } 166 167 $is_valid = $this->validate_request_method( 168 $request->get_method(), 169 $ability->get_meta_item( 'annotations' ) 170 ); 171 if ( is_wp_error( $is_valid ) ) { 172 return $is_valid; 173 } 174 175 $input = $ability->prepare_input( $this->get_input_from_request( $request ) ); 176 if ( is_wp_error( $input ) ) { 177 return $this->ensure_error_status( $input, 400 ); 178 } 179 180 $result = $ability->check_permissions( $input ); 181 if ( is_wp_error( $result ) ) { 182 $result->add_data( array( 'status' => rest_authorization_required_code() ) ); 183 return $result; 184 } 185 if ( ! $result ) { 186 return new WP_Error( 187 'rest_ability_cannot_execute', 188 __( 'Sorry, you are not allowed to execute this ability.' ), 189 array( 'status' => rest_authorization_required_code() ) 190 ); 191 } 192 193 return true; 194 } 195 196 /** 197 * Ensures a WP_Error object carries an HTTP status, adding a default when none is set. 198 * 199 * @since 7.1.0 200 * 201 * @param WP_Error $error Error object to update. 202 * @param int $status HTTP status code to add if not already present. 203 * @return WP_Error The error object, with a default status when needed. 204 */ 205 private function ensure_error_status( WP_Error $error, int $status ): WP_Error { 206 $error_data = $error->get_error_data(); 207 if ( ! is_array( $error_data ) || ! isset( $error_data['status'] ) ) { 208 $error->add_data( array( 'status' => $status ) ); 209 } 210 211 return $error; 212 } 213 214 /** 215 * Extracts input parameters from the request. 216 * 217 * @since 6.9.0 218 * 219 * @param WP_REST_Request $request The request object. 220 * @return mixed|null The input parameters. 221 */ 222 private function get_input_from_request( $request ) { 223 if ( in_array( $request->get_method(), array( 'GET', 'DELETE' ), true ) ) { 224 // For GET and DELETE requests, look for 'input' query parameter. 225 $query_params = $request->get_query_params(); 226 return $query_params['input'] ?? null; 227 } 228 229 // For POST requests, look for 'input' in JSON body. 230 $json_params = $request->get_json_params(); 231 return $json_params['input'] ?? null; 232 } 233 234 /** 235 * Returns run input unchanged for backward compatibility. 236 * 237 * This method is no longer registered as the input argument sanitizer. 238 * 239 * @since 7.1.0 240 * @deprecated 7.2.0 Input is prepared by `check_ability_permissions()` and `WP_Ability::execute()`. 241 * 242 * @param mixed $input Raw input extracted from the request. 243 * @param WP_REST_Request $request Unused. The request object. 244 * @return mixed The unchanged input. 245 */ 246 public function sanitize_input_for_ability( $input, $request ) { 247 _deprecated_function( __METHOD__, '7.2.0' ); 248 249 return $input; 250 } 251 252 /** 253 * Retrieves the arguments for ability execution endpoint. 254 * 255 * @since 6.9.0 256 * 257 * @return array<string, mixed> Arguments for the run endpoint. 258 */ 259 public function get_run_args(): array { 260 return array( 261 'input' => array( 262 'description' => __( 'Input parameters for the ability execution.' ), 263 'type' => array( 'integer', 'number', 'boolean', 'string', 'array', 'object', 'null' ), 264 'default' => null, 265 // Keep the raw input for the permission check and execution. This disables default REST coercion. 266 'sanitize_callback' => null, 267 ), 268 ); 269 } 270 271 /** 272 * Retrieves the schema for ability execution endpoint. 273 * 274 * @since 6.9.0 275 * 276 * @return array<string, mixed> Schema for the run endpoint. 277 */ 278 public function get_run_schema(): array { 279 return array( 280 '$schema' => 'http://json-schema.org/draft-04/schema#', 281 'title' => 'ability-execution', 282 'type' => 'object', 283 'properties' => array( 284 'result' => array( 285 'description' => __( 'The result of the ability execution.' ), 286 'type' => array( 'integer', 'number', 'boolean', 'string', 'array', 'object', 'null' ), 287 'context' => array( 'view', 'edit' ), 288 'readonly' => true, 289 ), 290 ), 291 ); 292 } 293 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Tue Nov 24 08:20:01 2026 | Cross-referenced by PHPXref |