| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * REST API: WP_REST_Entity_Fields_Controller class 4 * 5 * @package WordPress 6 * @subpackage REST_API 7 * @since 7.2.0 8 */ 9 10 /** 11 * Controller which provides a REST endpoint for retrieving the fields 12 * registered for a given entity type. 13 * 14 * The fields are the ones registered on the server on the 15 * `wp_entity_fields_api_init` action: the serializable part of each field, plus 16 * the script modules that provide the JavaScript parts (render callbacks, 17 * components, value getters and setters), each with the ids of the fields 18 * it applies to. The client merges both into the fields it derives itself. 19 * 20 * @since 7.2.0 21 * 22 * @see WP_REST_Controller 23 */ 24 class WP_REST_Entity_Fields_Controller extends WP_REST_Controller { 25 26 /** 27 * Constructor. 28 * 29 * @since 7.2.0 30 */ 31 public function __construct() { 32 $this->namespace = 'wp/v2'; 33 $this->rest_base = 'entity-fields'; 34 } 35 36 /** 37 * Registers the routes for the controller. 38 * 39 * @since 7.2.0 40 */ 41 public function register_routes() { 42 register_rest_route( 43 $this->namespace, 44 '/' . $this->rest_base, 45 array( 46 array( 47 'methods' => WP_REST_Server::READABLE, 48 'callback' => array( $this, 'get_items' ), 49 'permission_callback' => array( $this, 'get_items_permissions_check' ), 50 'args' => array( 51 'kind' => array( 52 'description' => __( 'Entity kind.' ), 53 'type' => 'string', 54 'minLength' => 1, 55 'required' => true, 56 ), 57 'name' => array( 58 'description' => __( 'Entity name.' ), 59 'type' => 'string', 60 'minLength' => 1, 61 'required' => true, 62 ), 63 ), 64 ), 65 'schema' => array( $this, 'get_public_item_schema' ), 66 ) 67 ); 68 } 69 70 /** 71 * Checks if a given request has access to read the fields of an entity. 72 * 73 * @since 7.2.0 74 * 75 * @param WP_REST_Request $request Full details about the request. 76 * @return true|WP_Error True if the request has read access, WP_Error object otherwise. 77 */ 78 public function get_items_permissions_check( $request ) { 79 $kind = $request->get_param( 'kind' ); 80 $name = $request->get_param( 'name' ); 81 82 $capability = $this->get_required_capability( $kind, $name ); 83 84 if ( null === $capability ) { 85 return new WP_Error( 86 'rest_entity_fields_invalid_entity', 87 __( 'Invalid entity kind or name.' ), 88 array( 'status' => 404 ) 89 ); 90 } 91 92 if ( ! current_user_can( $capability ) ) { 93 return new WP_Error( 94 'rest_cannot_read', 95 __( 'Sorry, you are not allowed to read the fields of this entity.' ), 96 array( 'status' => rest_authorization_required_code() ) 97 ); 98 } 99 100 return true; 101 } 102 103 /** 104 * Resolves the capability required to read the fields of an entity. 105 * 106 * Mirrors the view config endpoint, which gates the same entities: post 107 * types use their own `edit_posts` capability (which honors custom 108 * `capability_type` registrations), taxonomies use `manage_terms`, and 109 * root-level entities use `manage_options`. A post type or taxonomy that is 110 * not registered, or not exposed to the REST API, resolves to `null` so the 111 * request is treated as referencing an unknown entity. 112 * 113 * Any other kind falls back to `edit_posts`, so fields registered for a 114 * custom entity kind stay readable behind a baseline capability. 115 * 116 * @since 7.2.0 117 * 118 * @param string $kind The entity kind (e.g. `postType`). 119 * @param string $name The entity name (e.g. `page`). 120 * @return string|null Capability required to read the fields, or null if 121 * the entity is not registered. 122 */ 123 protected function get_required_capability( $kind, $name ) { 124 switch ( $kind ) { 125 case 'postType': 126 $post_type = get_post_type_object( $name ); 127 if ( $post_type && $post_type->show_in_rest ) { 128 return $post_type->cap->edit_posts; 129 } 130 return null; 131 132 case 'taxonomy': 133 $taxonomy = get_taxonomy( $name ); 134 if ( $taxonomy && $taxonomy->show_in_rest ) { 135 return $taxonomy->cap->manage_terms; 136 } 137 return null; 138 139 case 'root': 140 return 'manage_options'; 141 } 142 143 return 'edit_posts'; 144 } 145 146 /** 147 * Returns the fields registered for the given entity type. 148 * 149 * @since 7.2.0 150 * 151 * @param WP_REST_Request $request Full details about the request. 152 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. 153 */ 154 public function get_items( $request ) { 155 $kind = $request->get_param( 'kind' ); 156 $name = $request->get_param( 'name' ); 157 158 $field_schema = $this->get_field_schema(); 159 $fields = array(); 160 foreach ( wp_get_registered_entity_fields( $kind, $name ) as $field ) { 161 $fields[] = wp_prepare_json_value_for_client( $field, $field_schema ); 162 } 163 164 $response = array( 165 'kind' => $kind, 166 'name' => $name, 167 'fields' => $fields, 168 'script_modules' => wp_get_registered_entity_field_modules( $kind, $name ), 169 ); 170 171 return rest_ensure_response( $response ); 172 } 173 174 /** 175 * Retrieves the item's schema, conforming to JSON Schema. 176 * 177 * @since 7.2.0 178 * 179 * @return array Item schema data. 180 */ 181 public function get_item_schema() { 182 if ( $this->schema ) { 183 return $this->add_additional_fields_schema( $this->schema ); 184 } 185 186 $this->schema = array( 187 '$schema' => 'http://json-schema.org/draft-04/schema#', 188 'title' => 'entity-fields', 189 'type' => 'object', 190 'properties' => array( 191 'kind' => array( 192 'description' => __( 'Entity kind.' ), 193 'type' => 'string', 194 'readonly' => true, 195 ), 196 'name' => array( 197 'description' => __( 'Entity name.' ), 198 'type' => 'string', 199 'readonly' => true, 200 ), 201 'fields' => array( 202 'description' => __( 'The fields registered for the entity, in registration order.' ), 203 'type' => 'array', 204 'readonly' => true, 205 'items' => $this->get_field_schema(), 206 ), 207 'script_modules' => array( 208 'description' => __( 'The script modules providing the JavaScript parts of the fields, each with the ids of the fields it applies to.' ), 209 'type' => 'array', 210 'readonly' => true, 211 'items' => array( 212 'type' => 'object', 213 'properties' => array( 214 'id' => array( 215 'description' => __( 'The id of the script module.' ), 216 'type' => 'string', 217 ), 218 'fields' => array( 219 'description' => __( 'The ids of the fields the script module applies to.' ), 220 'type' => 'array', 221 'items' => array( 222 'type' => 'string', 223 ), 224 ), 225 ), 226 ), 227 ), 228 ), 229 ); 230 231 return $this->add_additional_fields_schema( $this->schema ); 232 } 233 234 /** 235 * Returns the schema for a field definition. 236 * 237 * Describes the serializable subset of the DataViews Field API, see 238 * packages/dataviews/src/types/field-api.ts in Gutenberg. The properties 239 * that are JavaScript callbacks or components (`render`, `sort`, 240 * `getValue`, …) come from the script modules and are not part of the 241 * definition. 242 * Additional properties are allowed, so a plugin can register a field 243 * with properties of its own that its script module understands. 244 * 245 * @since 7.2.0 246 * 247 * @return array Schema for a field definition. 248 */ 249 protected function get_field_schema() { 250 $option_schema = array( 251 'type' => 'object', 252 'properties' => array( 253 'value' => array( 254 'description' => __( 'The value of the option.' ), 255 'type' => array( 'string', 'integer', 'number', 'boolean', 'null' ), 256 ), 257 'label' => array( 258 'description' => __( 'The label of the option.' ), 259 'type' => 'string', 260 ), 261 'description' => array( 262 'description' => __( 'The description of the option.' ), 263 'type' => 'string', 264 ), 265 ), 266 ); 267 268 return array( 269 'type' => 'object', 270 'properties' => array( 271 'id' => array( 272 'description' => __( 'The unique identifier of the field.' ), 273 'type' => 'string', 274 ), 275 'origin' => array( 276 'description' => sprintf( 277 /* translators: %s: core */ 278 __( 'Who registered and updated the field: %s, or the slug of a plugin or theme.' ), 279 'core' 280 ), 281 'type' => 'object', 282 'properties' => array( 283 'registeredBy' => array( 284 'description' => __( 'Who registered the field.' ), 285 'type' => 'string', 286 ), 287 'updatedBy' => array( 288 'description' => __( 'Who updated the field, in update order.' ), 289 'type' => 'array', 290 'items' => array( 291 'type' => 'string', 292 ), 293 ), 294 ), 295 ), 296 'type' => array( 297 'description' => __( 'The type of the field, one of the types DataViews provides a control for.' ), 298 'type' => 'string', 299 'enum' => array( 'array', 'boolean', 'color', 'date', 'datetime', 'email', 'integer', 'media', 'number', 'password', 'telephone', 'text', 'time', 'url' ), 300 ), 301 'label' => array( 302 'description' => __( 'The label of the field.' ), 303 'type' => 'string', 304 ), 305 'header' => array( 306 'description' => __( 'The header of the field. Defaults to the label.' ), 307 'type' => 'string', 308 ), 309 'description' => array( 310 'description' => __( 'The description of the field.' ), 311 'type' => 'string', 312 ), 313 'placeholder' => array( 314 'description' => __( 'The placeholder of the field.' ), 315 'type' => 'string', 316 ), 317 'Edit' => array( 318 'description' => __( 'The control used to edit the field: the name of a control, or a control configuration.' ), 319 'type' => array( 'string', 'object' ), 320 ), 321 'isValid' => array( 322 'description' => __( 'The validation rules of the field.' ), 323 'type' => 'object', 324 ), 325 'isDisabled' => array( 326 'description' => __( 'Whether the field is disabled.' ), 327 'type' => 'boolean', 328 ), 329 'enableSorting' => array( 330 'description' => __( 'Whether the field is sortable.' ), 331 'type' => 'boolean', 332 ), 333 'enableGlobalSearch' => array( 334 'description' => __( 'Whether the field is searchable.' ), 335 'type' => 'boolean', 336 ), 337 'enableHiding' => array( 338 'description' => __( 'Whether the field can be hidden.' ), 339 'type' => 'boolean', 340 ), 341 'elements' => array( 342 'description' => __( 'The options to pick from when using the field as a filter.' ), 343 'type' => 'array', 344 'items' => $option_schema, 345 ), 346 'filterBy' => array( 347 'description' => __( 'The filter configuration of the field, or false when the field cannot be filtered.' ), 348 'type' => array( 'object', 'boolean' ), 349 'properties' => array( 350 'operators' => array( 351 'type' => 'array', 352 'items' => array( 353 'type' => 'string', 354 'enum' => array( 355 'is', 356 'isNot', 357 'isAny', 358 'isNone', 359 'isAll', 360 'lessThan', 361 'greaterThan', 362 'lessThanOrEqual', 363 'greaterThanOrEqual', 364 'before', 365 'after', 366 'beforeInc', 367 'afterInc', 368 'contains', 369 'notContains', 370 'startsWith', 371 'between', 372 'on', 373 'notOn', 374 'inThePast', 375 'over', 376 ), 377 ), 378 ), 379 'isPrimary' => array( 380 'type' => 'boolean', 381 ), 382 ), 383 ), 384 'readOnly' => array( 385 'description' => __( 'Whether the field is read only.' ), 386 'type' => 'boolean', 387 ), 388 'format' => array( 389 'description' => __( 'The display format of the field.' ), 390 'type' => 'object', 391 ), 392 ), 393 'additionalProperties' => true, 394 ); 395 } 396 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Sun Nov 29 08:20:01 2026 | Cross-referenced by PHPXref |