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