Repository navigation
Expand file tree
/
Copy pathclass-wp-rest-entity-actions-controller.php
More file actions
303 lines (284 loc) · 9.17 KB
/
Copy pathclass-wp-rest-entity-actions-controller.php
File metadata and controls
303 lines (284 loc) · 9.17 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
<?php
/**
* REST API: WP_REST_Entity_Actions_Controller class
*
* @package WordPress
* @subpackage REST_API
* @since 7.2.0
*/
/**
* Controller which provides a REST endpoint for retrieving the actions
* registered for a given entity type.
*
* The actions are the ones registered on the server on the
* `wp_entity_actions_api_init` action: the serializable part of each action, plus
* the script modules that provide the JavaScript parts (callbacks, modals,
* eligibility checks, icons), each with the ids of the actions it applies
* to. The client merges both into the actions it shows.
*
* @since 7.2.0
*
* @see WP_REST_Controller
*/
class WP_REST_Entity_Actions_Controller extends WP_REST_Controller {
/**
* Constructor.
*
* @since 7.2.0
*/
public function __construct() {
$this->namespace = 'wp/v2';
$this->rest_base = 'entity-actions';
}
/**
* 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 actions 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_actions_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 actions of this entity.' ),
array( 'status' => rest_authorization_required_code() )
);
}
return true;
}
/**
* Resolves the capability required to read the actions of an entity.
*
* Mirrors the fields 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 actions 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 actions, 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 actions 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' );
$response = array(
'kind' => $kind,
'name' => $name,
'actions' => wp_get_registered_entity_actions( $kind, $name ),
'script_modules' => wp_get_registered_entity_action_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-actions',
'type' => 'object',
'properties' => array(
'kind' => array(
'description' => __( 'Entity kind.' ),
'type' => 'string',
'readonly' => true,
),
'name' => array(
'description' => __( 'Entity name.' ),
'type' => 'string',
'readonly' => true,
),
'actions' => array(
'description' => __( 'The actions registered for the entity, in registration order.' ),
'type' => 'array',
'readonly' => true,
'items' => $this->get_action_schema(),
),
'script_modules' => array(
'description' => __( 'The script modules providing the JavaScript parts of the actions, each with the ids of the actions it applies to.' ),
'type' => 'array',
'readonly' => true,
'items' => array(
'type' => 'object',
'properties' => array(
'id' => array(
'description' => __( 'The id of the script module.' ),
'type' => 'string',
),
'actions' => array(
'description' => __( 'The ids of the actions the script module applies to.' ),
'type' => 'array',
'items' => array(
'type' => 'string',
),
),
),
),
),
),
);
return $this->add_additional_fields_schema( $this->schema );
}
/**
* Returns the schema for an action definition.
*
* Describes the serializable subset of the DataViews Action API, see
* packages/dataviews/src/types/dataviews.ts. The properties that are
* JavaScript callbacks, components, or elements (`callback`,
* `RenderModal`, `isEligible`, `icon`, …) come from the script modules and
* are not part of the definition. Additional properties are allowed, so a
* plugin can register an action with properties of its own that its script
* module understands.
*
* @since 7.2.0
*
* @return array Schema for an action definition.
*/
protected function get_action_schema() {
return array(
'type' => 'object',
'properties' => array(
'id' => array(
'description' => __( 'The unique identifier of the action.' ),
'type' => 'string',
),
'origin' => array(
'description' => __( 'Who registered the action: `core`, or the slug of a plugin or theme.' ),
'type' => 'object',
'properties' => array(
'registeredBy' => array(
'description' => __( 'Who registered the action.' ),
'type' => 'string',
),
),
),
'label' => array(
'description' => __( 'The label of the action.' ),
'type' => 'string',
),
'isPrimary' => array(
'description' => __( 'Whether the action is a primary action.' ),
'type' => 'boolean',
),
'supportsBulk' => array(
'description' => __( 'Whether the action can be performed on several items at once.' ),
'type' => 'boolean',
),
'disabled' => array(
'description' => __( 'Whether the action is disabled.' ),
'type' => 'boolean',
),
'context' => array(
'description' => __( 'The context the action is limited to, such as `list` for the lists of items. Without it, the action shows everywhere.' ),
'type' => 'string',
'enum' => WP_Entity_Actions_Registry::SUPPORTED_VALUES['context'],
),
'modalHeader' => array(
'description' => __( 'The header of the modal of the action.' ),
'type' => 'string',
),
'hideModalHeader' => array(
'description' => __( 'Whether to hide the header of the modal of the action.' ),
'type' => 'boolean',
),
'modalSize' => array(
'description' => __( 'The size of the modal of the action.' ),
'type' => 'string',
'enum' => WP_Entity_Actions_Registry::SUPPORTED_VALUES['modalSize'],
),
'modalFocusOnMount' => array(
'description' => __( 'Where the focus goes when the modal of the action opens.' ),
'type' => array( 'boolean', 'string' ),
),
),
'additionalProperties' => true,
);
}
}