From 3dcecdb83956e1759e401ae78ffaff9211e6ad90 Mon Sep 17 00:00:00 2001 From: Sajid Date: Thu, 26 Nov 2026 11:21:51 +0530 Subject: [PATCH] Abilities API: Accept PUT for destructive and idempotent abilities in the run endpoint. Destructive and idempotent abilities now expect PUT, which carries the input in a JSON body. PATCH is also accepted, and DELETE stays accepted with the input in the query string for backward compatibility. See #66801. --- ...ss-wp-rest-abilities-v1-run-controller.php | 23 +++- .../wpRestAbilitiesV1RunController.php | 120 +++++++++++++++++- 2 files changed, 131 insertions(+), 12 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-run-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-run-controller.php index 8ed345bbc2321..155ad8a49855a 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-run-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-run-controller.php @@ -114,29 +114,40 @@ public function execute_ability( $request ) { /** * Validates if the HTTP method matches the expected method for the ability based on its annotations. * + * Read-only abilities require GET. Destructive and idempotent abilities require PUT, and also + * accept PATCH and DELETE. All other abilities require POST. + * * @since 6.9.0 + * @since 7.3.0 Destructive and idempotent abilities require PUT, and also accept PATCH and DELETE. * * @param string $request_method The HTTP method of the request. * @param array $annotations The ability annotations. * @return true|WP_Error True on success, or WP_Error object on failure. */ public function validate_request_method( string $request_method, array $annotations ) { - $expected_method = 'POST'; + $expected_method = 'POST'; + $accepted_methods = array(); if ( ! empty( $annotations['readonly'] ) ) { $expected_method = 'GET'; } elseif ( ! empty( $annotations['destructive'] ) && ! empty( $annotations['idempotent'] ) ) { - $expected_method = 'DELETE'; + /* + * Updates and deletes share these annotations. PUT is idempotent by definition + * and carries the input in a JSON body. PATCH also suits partial updates, and + * DELETE stays accepted for clients written before PUT was supported. + */ + $expected_method = 'PUT'; + $accepted_methods = array( 'PATCH', 'DELETE' ); } - if ( $expected_method === $request_method ) { + if ( $expected_method === $request_method || in_array( $request_method, $accepted_methods, true ) ) { return true; } $error_message = __( 'Abilities that perform updates require POST method.' ); if ( 'GET' === $expected_method ) { $error_message = __( 'Read-only abilities require GET method.' ); - } elseif ( 'DELETE' === $expected_method ) { - $error_message = __( 'Abilities that perform destructive actions require DELETE method.' ); + } elseif ( 'PUT' === $expected_method ) { + $error_message = __( 'Idempotent destructive abilities require PUT method.' ); } return new WP_Error( 'rest_ability_invalid_method', @@ -226,7 +237,7 @@ private function get_input_from_request( $request ) { return $query_params['input'] ?? null; } - // For POST requests, look for 'input' in JSON body. + // For POST, PUT and PATCH requests, look for 'input' in JSON body. $json_params = $request->get_json_params(); return $json_params['input'] ?? null; } diff --git a/tests/phpunit/tests/rest-api/wpRestAbilitiesV1RunController.php b/tests/phpunit/tests/rest-api/wpRestAbilitiesV1RunController.php index 0d3f14c01423c..43ee8a29e35df 100644 --- a/tests/phpunit/tests/rest-api/wpRestAbilitiesV1RunController.php +++ b/tests/phpunit/tests/rest-api/wpRestAbilitiesV1RunController.php @@ -239,7 +239,7 @@ private function register_test_abilities(): void { ) ); - // Destructive ability (DELETE method). + // Destructive and idempotent ability (PUT method, PATCH and DELETE also accepted). $this->register_test_ability( 'test/delete-user', array( @@ -702,12 +702,13 @@ public function test_readonly_ability_requires_get(): void { } /** - * Test HTTP method validation for destructive abilities. + * Test HTTP method validation for destructive and idempotent abilities. * * @ticket 64098 + * @ticket 66801 */ - public function test_destructive_ability_requires_delete(): void { - // Try POST on a destructive ability (should fail). + public function test_destructive_idempotent_ability_requires_put(): void { + // Try POST on a destructive and idempotent ability (should fail). $request = new WP_REST_Request( 'POST', '/wp-abilities/v1/abilities/test/delete-user/run' ); $request->set_header( 'Content-Type', 'application/json' ); $request->set_body( wp_json_encode( array( 'user_id' => 1 ) ) ); @@ -717,7 +718,100 @@ public function test_destructive_ability_requires_delete(): void { $this->assertSame( 405, $response->get_status() ); $data = $response->get_data(); $this->assertSame( 'rest_ability_invalid_method', $data['code'] ); - $this->assertSame( 'Abilities that perform destructive actions require DELETE method.', $data['message'] ); + $this->assertSame( 'Idempotent destructive abilities require PUT method.', $data['message'] ); + } + + /** + * Data provider for the HTTP methods each set of annotations accepts. + * + * @return array, 1: string, 2: bool}> Ability annotations, + * HTTP method, and whether it is accepted. + */ + public static function data_request_methods_for_annotations(): array { + $readonly = array( 'readonly' => true ); + $additive = array( 'destructive' => false ); + $additive_idempotent = array( + 'destructive' => false, + 'idempotent' => true, + ); + $destructive = array( 'destructive' => true ); + $destructive_idempotent = array( + 'destructive' => true, + 'idempotent' => true, + ); + + return array( + 'read-only with GET' => array( $readonly, 'GET', true ), + 'read-only with PUT' => array( $readonly, 'PUT', false ), + 'additive with POST' => array( $additive, 'POST', true ), + 'additive with PUT' => array( $additive, 'PUT', false ), + 'additive idempotent with POST' => array( $additive_idempotent, 'POST', true ), + 'additive idempotent with PUT' => array( $additive_idempotent, 'PUT', false ), + 'destructive with POST' => array( $destructive, 'POST', true ), + 'destructive with PUT' => array( $destructive, 'PUT', false ), + 'destructive with PATCH' => array( $destructive, 'PATCH', false ), + 'destructive with DELETE' => array( $destructive, 'DELETE', false ), + 'destructive idempotent with PUT' => array( $destructive_idempotent, 'PUT', true ), + 'destructive idempotent with PATCH' => array( $destructive_idempotent, 'PATCH', true ), + 'destructive idempotent with DELETE' => array( $destructive_idempotent, 'DELETE', true ), + 'destructive idempotent with POST' => array( $destructive_idempotent, 'POST', false ), + 'destructive idempotent with GET' => array( $destructive_idempotent, 'GET', false ), + 'destructive idempotent with HEAD' => array( $destructive_idempotent, 'HEAD', false ), + ); + } + + /** + * Tests which HTTP methods the run endpoint accepts for each set of annotations. + * + * @ticket 66801 + * + * @dataProvider data_request_methods_for_annotations + * + * @param array $annotations Ability annotations. + * @param string $method HTTP method. + * @param bool $is_accepted Whether the method is accepted. + */ + public function test_validate_request_method( array $annotations, string $method, bool $is_accepted ): void { + $controller = new WP_REST_Abilities_V1_Run_Controller(); + $result = $controller->validate_request_method( $method, $annotations ); + + if ( $is_accepted ) { + $this->assertTrue( $result ); + return; + } + + $this->assertWPError( $result ); + $this->assertSame( 'rest_ability_invalid_method', $result->get_error_code() ); + $this->assertSame( 405, $result->get_error_data()['status'] ); + } + + /** + * Tests that PUT does not read the input from the query string. + * + * @ticket 66801 + */ + public function test_put_ignores_input_in_query_string(): void { + $this->register_reflecting_ability( + 'test/update-title', + array( + 'type' => array( 'object', 'null' ), + 'properties' => array( + 'id' => array( 'type' => 'integer' ), + ), + ), + array( + 'destructive' => true, + 'idempotent' => true, + ) + ); + + $request = new WP_REST_Request( 'PUT', '/wp-abilities/v1/abilities/test/update-title/run' ); + $request->set_query_params( array( 'input' => array( 'id' => 5 ) ) ); + + $response = $this->server->dispatch( $request ); + + $this->assertSame( 200, $response->get_status() ); + $this->assertNull( $response->get_data()['value'] ); } /** @@ -1784,7 +1878,7 @@ private function register_reflecting_ability( string $name, array $input_schema, /** * Dispatches a run request for an ability, nesting the value under the `input` parameter. * - * GET and DELETE send the input as query parameters, POST sends it as a JSON body. Pass + * GET and DELETE send the input as query parameters, POST, PUT and PATCH send it as a JSON body. Pass * `null` to send no input at all. * * @param string $method HTTP method. @@ -1816,6 +1910,20 @@ public static function data_coercion_transports(): array { return array( 'GET' => array( 'GET', array( 'readonly' => true ) ), 'POST' => array( 'POST', array() ), + 'PUT' => array( + 'PUT', + array( + 'destructive' => true, + 'idempotent' => true, + ), + ), + 'PATCH' => array( + 'PATCH', + array( + 'destructive' => true, + 'idempotent' => true, + ), + ), 'DELETE' => array( 'DELETE', array(