Skip to content

Commit cebce32

Browse files
Apply suggestions from code review
Co-authored-by: Timothy Jacobs <[email protected]>
1 parent a343b9e commit cebce32

File tree

3 files changed

+43
-43
lines changed

3 files changed

+43
-43
lines changed

src/wp-includes/rest-api/endpoints/class-wp-rest-abilities-list-controller.php

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,6 @@
1818
*/
1919
class WP_REST_Abilities_List_Controller extends WP_REST_Controller {
2020

21-
/**
22-
* Default number of items per page for pagination.
23-
*
24-
* @since 6.9.0
25-
* @var int
26-
*/
27-
public const DEFAULT_PER_PAGE = 50;
28-
2921
/**
3022
* REST API namespace.
3123
*
@@ -57,7 +49,7 @@ public function register_routes(): void {
5749
array(
5850
'methods' => WP_REST_Server::READABLE,
5951
'callback' => array( $this, 'get_items' ),
60-
'permission_callback' => array( $this, 'get_permissions_check' ),
52+
'permission_callback' => array( $this, 'get_items_permissions_check' ),
6153
'args' => $this->get_collection_params(),
6254
),
6355
'schema' => array( $this, 'get_public_item_schema' ),
@@ -78,7 +70,7 @@ public function register_routes(): void {
7870
array(
7971
'methods' => WP_REST_Server::READABLE,
8072
'callback' => array( $this, 'get_item' ),
81-
'permission_callback' => array( $this, 'get_permissions_check' ),
73+
'permission_callback' => array( $this, 'get_item_permissions_check' ),
8274
),
8375
'schema' => array( $this, 'get_public_item_schema' ),
8476
)
@@ -90,7 +82,7 @@ public function register_routes(): void {
9082
*
9183
* @since 6.9.0
9284
*
93-
* @param WP_REST_Request<array<string, mixed>> $request Full details about the request.
85+
* @param WP_REST_Request $request Full details about the request.
9486
* @return WP_REST_Response Response object on success.
9587
*/
9688
public function get_items( $request ) {
@@ -102,7 +94,7 @@ static function ( $ability ) {
10294
);
10395

10496
// Filter by ability category if specified.
105-
$category = $request->get_param( 'category' );
97+
$category = $request['category'];
10698
if ( ! empty( $category ) ) {
10799
$abilities = array_filter(
108100
$abilities,
@@ -114,10 +106,8 @@ static function ( $ability ) use ( $category ) {
114106
$abilities = array_values( $abilities );
115107
}
116108

117-
// Handle pagination with explicit defaults.
118-
$params = $request->get_params();
119-
$page = $params['page'] ?? 1;
120-
$per_page = $params['per_page'] ?? self::DEFAULT_PER_PAGE;
109+
$page = $request['page'];
110+
$per_page = $request['per_page'];
121111
$offset = ( $page - 1 ) * $per_page;
122112

123113
$total_abilities = count( $abilities );
@@ -163,11 +153,11 @@ static function ( $ability ) use ( $category ) {
163153
*
164154
* @since 6.9.0
165155
*
166-
* @param WP_REST_Request<array<string, mixed>> $request Full details about the request.
156+
* @param WP_REST_Request $request Full details about the request.
167157
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
168158
*/
169159
public function get_item( $request ) {
170-
$ability = wp_get_ability( $request->get_param( 'name' ) );
160+
$ability = wp_get_ability( $request['name'] );
171161
if ( ! $ability || ! $ability->get_meta_item( 'show_in_rest' ) ) {
172162
return new WP_Error(
173163
'rest_ability_not_found',
@@ -181,14 +171,26 @@ public function get_item( $request ) {
181171
}
182172

183173
/**
184-
* Checks if a given request has access to read abilities.
174+
* Checks if a given request has access to read ability items.
185175
*
186176
* @since 6.9.0
187177
*
188-
* @param WP_REST_Request<array<string, mixed>> $request Full details about the request.
178+
* @param WP_REST_Request $request Full details about the request.
189179
* @return bool True if the request has read access.
190180
*/
191-
public function get_permissions_check( $request ) {
181+
public function get_items_permissions_check( $request ) {
182+
return current_user_can( 'read' );
183+
}
184+
185+
/**
186+
* Checks if a given request has access to read an ability item.
187+
*
188+
* @since 6.9.0
189+
*
190+
* @param WP_REST_Request $request Full details about the request.
191+
* @return bool True if the request has read access.
192+
*/
193+
public function get_item_permissions_check( $request ) {
192194
return current_user_can( 'read' );
193195
}
194196

@@ -197,8 +199,8 @@ public function get_permissions_check( $request ) {
197199
*
198200
* @since 6.9.0
199201
*
200-
* @param WP_Ability $ability The ability object.
201-
* @param WP_REST_Request<array<string, mixed>> $request Request object.
202+
* @param WP_Ability $ability The ability object.
203+
* @param WP_REST_Request $request Request object.
202204
* @return WP_REST_Response Response object.
203205
*/
204206
public function prepare_item_for_response( $ability, $request ) {
@@ -212,7 +214,7 @@ public function prepare_item_for_response( $ability, $request ) {
212214
'meta' => $ability->get_meta(),
213215
);
214216

215-
$context = $request->get_param( 'context' ) ?? 'view';
217+
$context = $request['context'] ?? 'view';
216218
$data = $this->add_additional_fields_to_object( $data, $request );
217219
$data = $this->filter_response_by_context( $data, $context );
218220

@@ -229,7 +231,7 @@ public function prepare_item_for_response( $ability, $request ) {
229231
),
230232
);
231233

232-
$links['run'] = array(
234+
$links['wp:action-run'] = array(
233235
'href' => rest_url( sprintf( '%s/%s/%s/run', $this->namespace, $this->rest_base, $ability->get_name() ) ),
234236
);
235237

@@ -291,11 +293,17 @@ public function get_item_schema(): array {
291293
'meta' => array(
292294
'description' => __( 'Meta information about the ability.' ),
293295
'type' => 'object',
296+
'properties' => array(
297+
'annotations' => array(
298+
'description' => __( 'Annotations for the ability.' ),
299+
'type' => array( 'boolean', 'null' ),
300+
'default' => null,
301+
),
302+
),
294303
'context' => array( 'view', 'edit' ),
295304
'readonly' => true,
296305
),
297306
),
298-
'required' => array( 'name', 'label', 'meta', 'description', 'category', 'input_schema', 'output_schema' ),
299307
);
300308

301309
return $this->add_additional_fields_schema( $schema );
@@ -320,7 +328,7 @@ public function get_collection_params(): array {
320328
'per_page' => array(
321329
'description' => __( 'Maximum number of items to be returned in result set.' ),
322330
'type' => 'integer',
323-
'default' => self::DEFAULT_PER_PAGE,
331+
'default' => 50,
324332
'minimum' => 1,
325333
'maximum' => 100,
326334
),

src/wp-includes/rest-api/endpoints/class-wp-rest-abilities-run-controller.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ public function register_routes(): void {
7676
*
7777
* @since 6.9.0
7878
*
79-
* @param WP_REST_Request<array<string, mixed>> $request Full details about the request.
79+
* @param WP_REST_Request $request Full details about the request.
8080
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
8181
*/
8282
public function execute_ability( $request ) {
83-
$ability = wp_get_ability( $request->get_param( 'name' ) );
83+
$ability = wp_get_ability( $request['name'] );
8484
if ( ! $ability ) {
8585
return new WP_Error(
8686
'rest_ability_not_found',
@@ -140,11 +140,11 @@ public function validate_request_method( string $request_method, array $annotati
140140
*
141141
* @since 6.9.0
142142
*
143-
* @param WP_REST_Request<array<string, mixed>> $request Full details about the request.
143+
* @param WP_REST_Request $request Full details about the request.
144144
* @return true|WP_Error True if the request has execution permission, WP_Error object otherwise.
145145
*/
146146
public function check_ability_permissions( $request ) {
147-
$ability = wp_get_ability( $request->get_param( 'name' ) );
147+
$ability = wp_get_ability( $request['name'] );
148148
if ( ! $ability || ! $ability->get_meta_item( 'show_in_rest' ) ) {
149149
return new WP_Error(
150150
'rest_ability_not_found',
@@ -185,7 +185,7 @@ public function check_ability_permissions( $request ) {
185185
*
186186
* @since 6.9.0
187187
*
188-
* @param WP_REST_Request<array<string, mixed>> $request The request object.
188+
* @param WP_REST_Request $request The request object.
189189
* @return mixed|null The input parameters.
190190
*/
191191
private function get_input_from_request( $request ) {
@@ -232,6 +232,7 @@ public function get_run_schema(): array {
232232
'properties' => array(
233233
'result' => array(
234234
'description' => __( 'The result of the ability execution.' ),
235+
'type' => array( 'integer', 'number', 'boolean', 'string', 'array', 'object', 'null' ),
235236
'context' => array( 'view', 'edit' ),
236237
'readonly' => true,
237238
),

tests/phpunit/tests/rest-api/wpRestAbilitiesListController.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ public function test_ability_response_links(): void {
516516
$links = $response->get_links();
517517
$this->assertArrayHasKey( 'self', $links );
518518
$this->assertArrayHasKey( 'collection', $links );
519-
$this->assertArrayHasKey( 'run', $links );
519+
$this->assertArrayHasKey( 'wp:action-run', $links );
520520

521521
// Verify link URLs
522522
$self_link = $links['self'][0]['href'];
@@ -525,7 +525,7 @@ public function test_ability_response_links(): void {
525525
$collection_link = $links['collection'][0]['href'];
526526
$this->assertStringContainsString( '/wp/v2/abilities', $collection_link );
527527

528-
$run_link = $links['run'][0]['href'];
528+
$run_link = $links['wp:action-run'][0]['href'];
529529
$this->assertStringContainsString( '/wp/v2/abilities/test/calculator/run', $run_link );
530530
}
531531

@@ -580,15 +580,6 @@ public function test_get_schema(): void {
580580
$this->assertArrayHasKey( 'output_schema', $properties );
581581
$this->assertArrayHasKey( 'meta', $properties );
582582
$this->assertArrayHasKey( 'category', $properties );
583-
584-
// Test category property details
585-
$category_property = $properties['category'];
586-
$this->assertEquals( 'string', $category_property['type'] );
587-
$this->assertTrue( $category_property['readonly'] );
588-
589-
// Check that category is in required fields
590-
$this->assertArrayHasKey( 'required', $schema );
591-
$this->assertContains( 'category', $schema['required'] );
592583
}
593584

594585
/**

0 commit comments

Comments
 (0)