1818 */
1919class 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 ),
0 commit comments