@@ -91,19 +91,35 @@ public function run_ability_with_method_check( $request ) {
9191 }
9292
9393 // Check if the HTTP method matches the ability annotations.
94- $ annotations = $ ability ->get_meta_item ( 'annotations ' );
95- $ is_readonly = ! empty ( $ annotations ['readonly ' ] );
96- $ method = $ request ->get_method ();
94+ $ annotations = $ ability ->get_meta_item ( 'annotations ' );
95+ $ is_readonly = ! empty ( $ annotations ['readonly ' ] );
96+ $ is_destructive = ! empty ( $ annotations ['destructive ' ] );
97+ $ is_idempotent = ! empty ( $ annotations ['idempotent ' ] );
98+ $ request_method = $ request ->get_method ();
99+ $ expected_method = 'POST ' ;
100+ if ( $ is_readonly ) {
101+ $ expected_method = 'GET ' ;
102+ } elseif ( $ is_destructive && $ is_idempotent ) {
103+ $ expected_method = 'DELETE ' ;
104+ }
97105
98- if ( $ is_readonly && 'GET ' !== $ method ) {
106+ if ( $ expected_method === 'GET ' && $ expected_method !== $ request_method ) {
99107 return new WP_Error (
100108 'rest_ability_invalid_method ' ,
101109 __ ( 'Read-only abilities require GET method. ' ),
102110 array ( 'status ' => 405 )
103111 );
104112 }
105113
106- if ( ! $ is_readonly && 'POST ' !== $ method ) {
114+ if ( $ expected_method === 'DELETE ' && $ expected_method !== $ request_method ) {
115+ return new WP_Error (
116+ 'rest_ability_invalid_method ' ,
117+ __ ( 'Abilities that perform destructive actions require DELETE method. ' ),
118+ array ( 'status ' => 405 )
119+ );
120+ }
121+
122+ if ( $ expected_method === 'POST ' && $ expected_method !== $ request_method ) {
107123 return new WP_Error (
108124 'rest_ability_invalid_method ' ,
109125 __ ( 'Abilities that perform updates require POST method. ' ),
@@ -183,8 +199,8 @@ public function run_ability_permissions_check( $request ) {
183199 * @return mixed|null The input parameters.
184200 */
185201 private function get_input_from_request ( $ request ) {
186- if ( ' GET ' === $ request ->get_method () ) {
187- // For GET requests, look for 'input' query parameter.
202+ if ( in_array ( $ request ->get_method (), array ( ' GET ' , ' DELETE ' ) ) ) {
203+ // For GET and DELETE requests, look for 'input' query parameter.
188204 $ query_params = $ request ->get_query_params ();
189205 return $ query_params ['input ' ] ?? null ;
190206 }
@@ -226,7 +242,7 @@ public function get_run_schema(): array {
226242 'properties ' => array (
227243 'result ' => array (
228244 'description ' => __ ( 'The result of the ability execution. ' ),
229- 'context ' => array ( 'view ' ),
245+ 'context ' => array ( 'view ' , ' edit ' ),
230246 'readonly ' => true ,
231247 ),
232248 ),
0 commit comments