WP_REST_Abilities_V1_Run_Controller
- Since
- 6.9.0
- Source
wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-run-controller.php:19
Core controller used to execute abilities via the REST API.
Compatibility
- WordPress
- since 6.9.0
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in 3 of the 5 tracked releases, added in 6.9.0.
Properties · 2
$namespacestringprotected- REST API namespace.
$rest_basestringprotected- REST API base route.
Methods · 11
- register_routes()Registers the routes for ability execution.
- execute_ability()Executes an ability.
- validate_request_method()Validates if the HTTP method matches the expected method for the ability based on its annotations.
- check_ability_permissions()Checks if a given request has permission to execute a specific ability.
- ensure_error_status()Ensures a WP_Error object carries an HTTP status, adding a default when none is set.
- get_input_from_request()Extracts input parameters from the request.
- sanitize_input_for_ability()Sanitizes the run input by coercing it to the ability's input schema.
- coerce_input_to_schema()Coerces raw request input to the types declared in the ability input schema.
- input_contains_error()Determines whether a sanitized value is, or contains, a WP_Error.
- get_run_args()Retrieves the arguments for ability execution endpoint.
- get_run_schema()Retrieves the schema for ability execution endpoint.
Source code
class WP_REST_Abilities_V1_Run_Controller extends WP_REST_Controller { /** * REST API namespace. * * @since 6.9.0 * @var string */ protected $namespace = 'wp-abilities/v1'; /** * REST API base route. * * @since 6.9.0 * @var string */ protected $rest_base = 'abilities'; /** * Registers the routes for ability execution. * * @since 6.9.0 * * @see register_rest_route() */ public function register_routes(): void { register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<name>[a-zA-Z0-9\-\/]+?)/run', array( 'args' => array( 'name' => array( 'description' => __( 'Unique identifier for the ability.' ), 'type' => 'string', 'pattern' => '^[a-zA-Z0-9\-\/]+$', ), ), // TODO: We register ALLMETHODS because at route registration time, we don't know which abilities // exist or their annotations (`destructive`, `idempotent`, `readonly`). This is due to WordPress // load order - routes are registered early, before plugins have registered their abilities. // This approach works but could be improved with lazy route registration or a different // architecture that allows type-specific routes after abilities are registered. // This was the same issue that we ended up seeing with the Feature API. array( 'methods' => WP_REST_Server::ALLMETHODS, 'callback' => array( $this, 'execute_ability' ), 'permission_callback' => array( $this, 'check_ability_permissions' ), 'args' => $this->get_run_args(), ), 'schema' => array( $this, 'get_run_schema' ), ) ); } /** * Executes an ability. * * @since 6.9.0 * * @param WP_REST_Request $request Full details about the request. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. */ public function execute_ability( $request ) { $ability = wp_has_ability( $request['name'] ) ? wp_get_ability( $request['name'] ) : null; if ( ! $ability ) { return new WP_Error( 'rest_ability_not_found', __( 'Ability not found.' ), array( 'status' => 404 ) ); } $input = $this->get_input_from_request( $request ); $result = $ability->execute( $input ); if ( is_wp_error( $result ) ) { return $result; } return rest_ensure_response( $result );Changelog
Introduced in 6.9.0. 4 changes between 6.9.7 and 7.1.0.
Signature, return type and hooks compared across 3 parsed releases.
7.1.0
Method
ensure_error_status() added.verified against source7.1.0
Method
sanitize_input_for_ability() added.verified against source7.1.0
Method
coerce_input_to_schema() added.verified against source7.1.0
Method
input_contains_error() added.verified against source6.9.0
Introduced.from the docblock
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-run-controller.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.