[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * REST API list controller for Abilities API. 4 * 5 * @package WordPress 6 * @subpackage Abilities_API 7 * @since 6.9.0 8 */ 9 10 declare( strict_types = 1 ); 11 12 /** 13 * Core controller used to access abilities via the REST API. 14 * 15 * @since 6.9.0 16 * 17 * @see WP_REST_Controller 18 */ 19 class WP_REST_Abilities_V1_List_Controller extends WP_REST_Controller { 20 21 /** 22 * REST API namespace. 23 * 24 * @since 6.9.0 25 * @var string 26 */ 27 protected $namespace = 'wp-abilities/v1'; 28 29 /** 30 * REST API base route. 31 * 32 * @since 6.9.0 33 * @var string 34 */ 35 protected $rest_base = 'abilities'; 36 37 /** 38 * Registers the routes for abilities. 39 * 40 * @since 6.9.0 41 * 42 * @see register_rest_route() 43 */ 44 public function register_routes(): void { 45 register_rest_route( 46 $this->namespace, 47 '/' . $this->rest_base, 48 array( 49 array( 50 'methods' => WP_REST_Server::READABLE, 51 'callback' => array( $this, 'get_items' ), 52 'permission_callback' => array( $this, 'get_items_permissions_check' ), 53 'args' => $this->get_collection_params(), 54 ), 55 'schema' => array( $this, 'get_public_item_schema' ), 56 ) 57 ); 58 59 register_rest_route( 60 $this->namespace, 61 '/' . $this->rest_base . '/(?P<name>[a-zA-Z0-9\-\/]+)', 62 array( 63 'args' => array( 64 'name' => array( 65 'description' => __( 'Unique identifier for the ability.' ), 66 'type' => 'string', 67 'pattern' => '^[a-zA-Z0-9\-\/]+$', 68 ), 69 ), 70 array( 71 'methods' => WP_REST_Server::READABLE, 72 'callback' => array( $this, 'get_item' ), 73 'permission_callback' => array( $this, 'get_item_permissions_check' ), 74 ), 75 'schema' => array( $this, 'get_public_item_schema' ), 76 ) 77 ); 78 } 79 80 /** 81 * Retrieves all abilities. 82 * 83 * @since 6.9.0 84 * 85 * @param WP_REST_Request $request Full details about the request. 86 * @return WP_REST_Response Response object on success. 87 */ 88 public function get_items( $request ) { 89 $abilities = array_filter( 90 wp_get_abilities(), 91 static function ( $ability ) { 92 return $ability->get_meta_item( 'show_in_rest' ); 93 } 94 ); 95 96 // Filter by ability category if specified. 97 $category = $request['category']; 98 if ( ! empty( $category ) ) { 99 $abilities = array_filter( 100 $abilities, 101 static function ( $ability ) use ( $category ) { 102 return $ability->get_category() === $category; 103 } 104 ); 105 // Reset array keys after filtering. 106 $abilities = array_values( $abilities ); 107 } 108 109 $page = $request['page']; 110 $per_page = $request['per_page']; 111 $offset = ( $page - 1 ) * $per_page; 112 113 $total_abilities = count( $abilities ); 114 $max_pages = (int) ceil( $total_abilities / $per_page ); 115 116 if ( $request->get_method() === 'HEAD' ) { 117 $response = new WP_REST_Response( array() ); 118 } else { 119 $abilities = array_slice( $abilities, $offset, $per_page ); 120 121 $data = array(); 122 foreach ( $abilities as $ability ) { 123 $item = $this->prepare_item_for_response( $ability, $request ); 124 $data[] = $this->prepare_response_for_collection( $item ); 125 } 126 127 $response = rest_ensure_response( $data ); 128 } 129 130 $response->header( 'X-WP-Total', (string) $total_abilities ); 131 $response->header( 'X-WP-TotalPages', (string) $max_pages ); 132 133 $query_params = $request->get_query_params(); 134 $base = add_query_arg( urlencode_deep( $query_params ), rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) ); 135 136 if ( $page > 1 ) { 137 $prev_page = $page - 1; 138 $prev_link = add_query_arg( 'page', $prev_page, $base ); 139 $response->link_header( 'prev', $prev_link ); 140 } 141 142 if ( $page < $max_pages ) { 143 $next_page = $page + 1; 144 $next_link = add_query_arg( 'page', $next_page, $base ); 145 $response->link_header( 'next', $next_link ); 146 } 147 148 return $response; 149 } 150 151 /** 152 * Retrieves a specific ability. 153 * 154 * @since 6.9.0 155 * 156 * @param WP_REST_Request $request Full details about the request. 157 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. 158 */ 159 public function get_item( $request ) { 160 $ability = wp_get_ability( $request['name'] ); 161 if ( ! $ability || ! $ability->get_meta_item( 'show_in_rest' ) ) { 162 return new WP_Error( 163 'rest_ability_not_found', 164 __( 'Ability not found.' ), 165 array( 'status' => 404 ) 166 ); 167 } 168 169 $data = $this->prepare_item_for_response( $ability, $request ); 170 return rest_ensure_response( $data ); 171 } 172 173 /** 174 * Checks if a given request has access to read ability items. 175 * 176 * @since 6.9.0 177 * 178 * @param WP_REST_Request $request Full details about the request. 179 * @return bool True if the request has read access. 180 */ 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 ) { 194 return current_user_can( 'read' ); 195 } 196 197 /** 198 * Prepares an ability for response. 199 * 200 * @since 6.9.0 201 * 202 * @param WP_Ability $ability The ability object. 203 * @param WP_REST_Request $request Request object. 204 * @return WP_REST_Response Response object. 205 */ 206 public function prepare_item_for_response( $ability, $request ) { 207 $data = array( 208 'name' => $ability->get_name(), 209 'label' => $ability->get_label(), 210 'description' => $ability->get_description(), 211 'category' => $ability->get_category(), 212 'input_schema' => $ability->get_input_schema(), 213 'output_schema' => $ability->get_output_schema(), 214 'meta' => $ability->get_meta(), 215 ); 216 217 $context = $request['context'] ?? 'view'; 218 $data = $this->add_additional_fields_to_object( $data, $request ); 219 $data = $this->filter_response_by_context( $data, $context ); 220 221 $response = rest_ensure_response( $data ); 222 223 $fields = $this->get_fields_for_response( $request ); 224 if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) { 225 $links = array( 226 'self' => array( 227 'href' => rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $ability->get_name() ) ), 228 ), 229 'collection' => array( 230 'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ), 231 ), 232 ); 233 234 $links['wp:action-run'] = array( 235 'href' => rest_url( sprintf( '%s/%s/%s/run', $this->namespace, $this->rest_base, $ability->get_name() ) ), 236 ); 237 238 $response->add_links( $links ); 239 } 240 241 return $response; 242 } 243 244 /** 245 * Retrieves the ability's schema, conforming to JSON Schema. 246 * 247 * @since 6.9.0 248 * 249 * @return array<string, mixed> Item schema data. 250 */ 251 public function get_item_schema(): array { 252 $schema = array( 253 '$schema' => 'http://json-schema.org/draft-04/schema#', 254 'title' => 'ability', 255 'type' => 'object', 256 'properties' => array( 257 'name' => array( 258 'description' => __( 'Unique identifier for the ability.' ), 259 'type' => 'string', 260 'context' => array( 'view', 'edit', 'embed' ), 261 'readonly' => true, 262 ), 263 'label' => array( 264 'description' => __( 'Display label for the ability.' ), 265 'type' => 'string', 266 'context' => array( 'view', 'edit', 'embed' ), 267 'readonly' => true, 268 ), 269 'description' => array( 270 'description' => __( 'Description of the ability.' ), 271 'type' => 'string', 272 'context' => array( 'view', 'edit' ), 273 'readonly' => true, 274 ), 275 'category' => array( 276 'description' => __( 'Ability category this ability belongs to.' ), 277 'type' => 'string', 278 'context' => array( 'view', 'edit', 'embed' ), 279 'readonly' => true, 280 ), 281 'input_schema' => array( 282 'description' => __( 'JSON Schema for the ability input.' ), 283 'type' => 'object', 284 'context' => array( 'view', 'edit' ), 285 'readonly' => true, 286 ), 287 'output_schema' => array( 288 'description' => __( 'JSON Schema for the ability output.' ), 289 'type' => 'object', 290 'context' => array( 'view', 'edit' ), 291 'readonly' => true, 292 ), 293 'meta' => array( 294 'description' => __( 'Meta information about the ability.' ), 295 'type' => 'object', 296 'properties' => array( 297 'annotations' => array( 298 'description' => __( 'Annotations for the ability.' ), 299 'type' => array( 'boolean', 'null' ), 300 'default' => null, 301 ), 302 ), 303 'context' => array( 'view', 'edit' ), 304 'readonly' => true, 305 ), 306 ), 307 ); 308 309 return $this->add_additional_fields_schema( $schema ); 310 } 311 312 /** 313 * Retrieves the query params for collections. 314 * 315 * @since 6.9.0 316 * 317 * @return array<string, mixed> Collection parameters. 318 */ 319 public function get_collection_params(): array { 320 return array( 321 'context' => $this->get_context_param( array( 'default' => 'view' ) ), 322 'page' => array( 323 'description' => __( 'Current page of the collection.' ), 324 'type' => 'integer', 325 'default' => 1, 326 'minimum' => 1, 327 ), 328 'per_page' => array( 329 'description' => __( 'Maximum number of items to be returned in result set.' ), 330 'type' => 'integer', 331 'default' => 50, 332 'minimum' => 1, 333 'maximum' => 100, 334 ), 335 'category' => array( 336 'description' => __( 'Limit results to abilities in specific ability category.' ), 337 'type' => 'string', 338 'sanitize_callback' => 'sanitize_key', 339 'validate_callback' => 'rest_validate_request_arg', 340 ), 341 ); 342 } 343 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Thu Oct 23 08:20:05 2025 | Cross-referenced by PHPXref |