This repository has been archived by the owner on Apr 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
class-wp-rest-meta-controller.php
481 lines (411 loc) · 15.5 KB
/
class-wp-rest-meta-controller.php
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
<?php
/**
* Metadata base class.
*/
abstract class WP_REST_Meta_Controller extends WP_REST_Controller {
/**
* Associated object type.
*
* @var string Type slug ("post", "user", or "comment")
*/
protected $parent_type = null;
/**
* Base path for parent meta type endpoints.
*
* @var string
*/
protected $parent_base = null;
/**
* Construct the API handler object.
*/
public function __construct() {
if ( empty( $this->parent_type ) ) {
_doing_it_wrong( 'WP_REST_Meta_Controller::__construct', __( 'The object type must be overridden' ), 'WPAPI-2.0' );
return;
}
if ( empty( $this->parent_base ) ) {
_doing_it_wrong( 'WP_REST_Meta_Controller::__construct', __( 'The parent base must be overridden' ), 'WPAPI-2.0' );
return;
}
}
/**
* Register the meta-related routes.
*/
public function register_routes() {
register_rest_route( $this->namespace, '/' . $this->parent_base . '/(?P<parent_id>[\d]+)/' . $this->rest_base, array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => $this->get_collection_params(),
),
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'create_item' ),
'permission_callback' => array( $this, 'create_item_permissions_check' ),
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
),
'schema' => array( $this, 'get_public_item_schema' ),
) );
register_rest_route( $this->namespace, '/' . $this->parent_base . '/(?P<parent_id>[\d]+)/' . $this->rest_base . '/(?P<id>[\d]+)', array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_item' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
'args' => array(
'context' => $this->get_context_param( array( 'default' => 'edit' ) ),
),
),
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array( $this, 'update_item' ),
'permission_callback' => array( $this, 'update_item_permissions_check' ),
'args' => $this->get_endpoint_args_for_item_schema( false ),
),
array(
'methods' => WP_REST_Server::DELETABLE,
'callback' => array( $this, 'delete_item' ),
'permission_callback' => array( $this, 'delete_item_permissions_check' ),
'args' => array(
'force' => array(
'default' => false,
'description' => __( 'Required to be true, as resource does not support trashing.' ),
),
),
),
'schema' => array( $this, 'get_public_item_schema' ),
) );
}
/**
* Get the meta schema, conforming to JSON Schema
*
* @return array
*/
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'meta',
'type' => 'object',
/*
* Base properties for every Post
*/
'properties' => array(
'id' => array(
'description' => __( 'Unique identifier for the object.' ),
'type' => 'integer',
'context' => array( 'edit' ),
'readonly' => true,
),
'key' => array(
'description' => __( 'The key for the custom field.' ),
'type' => 'string',
'context' => array( 'edit' ),
'required' => true,
'arg_options' => array(
'sanitize_callback' => 'sanitize_text_field',
),
),
'value' => array(
'description' => __( 'The value of the custom field.' ),
'type' => 'string',
'context' => array( 'edit' ),
),
),
);
return $schema;
}
/**
* Get the query params for collections
*
* @return array
*/
public function get_collection_params() {
$params = parent::get_collection_params();
$new_params = array();
$new_params['context'] = $params['context'];
$new_params['context']['default'] = 'edit';
return $new_params;
}
/**
* Get the meta ID column for the relevant table.
*
* @return string
*/
protected function get_id_column() {
return ( 'user' === $this->parent_type ) ? 'umeta_id' : 'meta_id';
}
/**
* Get the object (parent) ID column for the relevant table.
*
* @return string
*/
protected function get_parent_column() {
$parent_column = 'post_id';
switch ( $this->parent_type ) {
case 'user':
return 'user_id';
case 'comment':
return 'comment_id';
case 'term':
return 'term_id';
default:
return $parent_column;
}
return $parent_column;
}
/**
* Retrieve custom fields for object.
*
* @param WP_REST_Request $request
* @return WP_REST_Request|WP_Error List of meta object data on success, WP_Error otherwise
*/
public function get_items( $request ) {
$parent_id = (int) $request['parent_id'];
global $wpdb;
$table = _get_meta_table( $this->parent_type );
$parent_column = $this->get_parent_column();
$id_column = $this->get_id_column();
// @codingStandardsIgnoreStart
$results = $wpdb->get_results( $wpdb->prepare( "SELECT $id_column, $parent_column, meta_key, meta_value FROM $table WHERE $parent_column = %d", $parent_id ) );
// @codingStandardsIgnoreEnd
$meta = array();
foreach ( $results as $row ) {
$value = $this->prepare_item_for_response( $row, $request, true );
if ( is_wp_error( $value ) ) {
continue;
}
$meta[] = $this->prepare_response_for_collection( $value );
}
return rest_ensure_response( $meta );
}
/**
* Retrieve custom field object.
*
* @param WP_REST_Request $request
* @return WP_REST_Request|WP_Error Meta object data on success, WP_Error otherwise
*/
public function get_item( $request ) {
$parent_id = (int) $request['parent_id'];
$mid = (int) $request['id'];
$parent_column = $this->get_parent_column();
$meta = get_metadata_by_mid( $this->parent_type, $mid );
if ( empty( $meta ) ) {
return new WP_Error( 'rest_meta_invalid_id', __( 'Invalid meta id.' ), array( 'status' => 404 ) );
}
if ( absint( $meta->$parent_column ) !== $parent_id ) {
return new WP_Error( 'rest_meta_' . $this->parent_type . '_mismatch', __( 'Meta does not belong to this object' ), array( 'status' => 400 ) );
}
return $this->prepare_item_for_response( $meta, $request );
}
/**
* Prepares meta data for return as an object.
*
* @param stdClass $data Metadata row from database
* @param WP_REST_Request $request
* @param boolean $is_raw Is the value field still serialized? (False indicates the value has been unserialized)
* @return WP_REST_Response|WP_Error Meta object data on success, WP_Error otherwise
*/
public function prepare_item_for_response( $data, $request, $is_raw = false ) {
$id_column = $this->get_id_column();
$id = $data->$id_column;
$key = $data->meta_key;
$value = $data->meta_value;
// Don't expose protected fields.
if ( is_protected_meta( $key ) ) {
return new WP_Error( 'rest_meta_protected', sprintf( __( '%s is marked as a protected field.' ), $key ), array( 'status' => 403 ) );
}
// Normalize serialized strings
if ( $is_raw && is_serialized_string( $value ) ) {
$value = unserialize( $value );
}
// Don't expose serialized data
if ( is_serialized( $value ) || ! is_string( $value ) ) {
return new WP_Error( 'rest_meta_protected', sprintf( __( '%s contains serialized data.' ), $key ), array( 'status' => 403 ) );
}
$meta = array(
'id' => (int) $id,
'key' => $key,
'value' => $value,
);
$response = rest_ensure_response( $meta );
$parent_column = $this->get_parent_column();
$response->add_link( 'about', rest_url( $this->namespace . '/' . $this->parent_base . '/' . $data->$parent_column ), array( 'embeddable' => true ) );
/**
* Filter a meta value returned from the API.
*
* Allows modification of the meta value right before it is returned.
*
* @param array $response Key value array of meta data: id, key, value.
* @param WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'rest_prepare_meta_value', $response, $request );
}
/**
* Add meta to an object.
*
* @param WP_REST_Request $request
* @return WP_REST_Response|WP_Error
*/
public function update_item( $request ) {
$parent_id = (int) $request['parent_id'];
$mid = (int) $request['id'];
$parent_column = $this->get_parent_column();
$current = get_metadata_by_mid( $this->parent_type, $mid );
if ( empty( $current ) ) {
return new WP_Error( 'rest_meta_invalid_id', __( 'Invalid meta id.' ), array( 'status' => 404 ) );
}
if ( absint( $current->$parent_column ) !== $parent_id ) {
return new WP_Error( 'rest_meta_' . $this->parent_type . '_mismatch', __( 'Meta does not belong to this object' ), array( 'status' => 400 ) );
}
if ( ! isset( $request['key'] ) && ! isset( $request['value'] ) ) {
return new WP_Error( 'rest_meta_data_invalid', __( 'Invalid meta parameters.' ), array( 'status' => 400 ) );
}
if ( isset( $request['key'] ) ) {
$key = $request['key'];
} else {
$key = $current->meta_key;
}
if ( isset( $request['value'] ) ) {
$value = $request['value'];
} else {
$value = $current->meta_value;
}
if ( ! $key ) {
return new WP_Error( 'rest_meta_invalid_key', __( 'Invalid meta key.' ), array( 'status' => 400 ) );
}
// for now let's not allow updating of arrays, objects or serialized values.
if ( ! $this->is_valid_meta_data( $current->meta_value ) ) {
$code = ( $this->parent_type === 'post' ) ? 'rest_post_invalid_action' : 'rest_meta_invalid_action';
return new WP_Error( $code, __( 'Invalid existing meta data for action.' ), array( 'status' => 400 ) );
}
if ( ! $this->is_valid_meta_data( $value ) ) {
$code = ( $this->parent_type === 'post' ) ? 'rest_post_invalid_action' : 'rest_meta_invalid_action';
return new WP_Error( $code, __( 'Invalid provided meta data for action.' ), array( 'status' => 400 ) );
}
if ( is_protected_meta( $current->meta_key ) ) {
return new WP_Error( 'rest_meta_protected', sprintf( __( '%s is marked as a protected field.' ), $current->meta_key ), array( 'status' => 403 ) );
}
if ( is_protected_meta( $key ) ) {
return new WP_Error( 'rest_meta_protected', sprintf( __( '%s is marked as a protected field.' ), $key ), array( 'status' => 403 ) );
}
// update_metadata_by_mid will return false if these are equal, so check
// first and pass through
if ( (string) $value === $current->meta_value && (string) $key === $current->meta_key ) {
return $this->get_item( $request );
}
if ( ! update_metadata_by_mid( $this->parent_type, $mid, $value, $key ) ) {
return new WP_Error( 'rest_meta_could_not_update', __( 'Could not update meta.' ), array( 'status' => 500 ) );
}
$request = new WP_REST_Request( 'GET' );
$request->set_query_params( array(
'context' => 'edit',
'parent_id' => $parent_id,
'id' => $mid,
) );
$response = $this->get_item( $request );
/**
* Fires after meta is added to an object or updated via the REST API.
*
* @param array $value The inserted meta data.
* @param WP_REST_Request $request The request sent to the API.
* @param boolean $creating True when adding meta, false when updating.
*/
do_action( 'rest_insert_meta', $value, $request, false );
return rest_ensure_response( $response );
}
/**
* Check if the data provided is valid data.
*
* Excludes serialized data from being sent via the API.
*
* @see https://github.com/WP-API/WP-API/pull/68
* @param mixed $data Data to be checked
* @return boolean Whether the data is valid or not
*/
protected function is_valid_meta_data( $data ) {
if ( is_array( $data ) || is_object( $data ) || is_serialized( $data ) ) {
return false;
}
return true;
}
/**
* Add meta to an object.
*
* @param WP_REST_Request $request
* @return WP_REST_Response|WP_Error
*/
public function create_item( $request ) {
$parent_id = (int) $request['parent_id'];
if ( ! $this->is_valid_meta_data( $request['value'] ) ) {
$code = ( $this->parent_type === 'post' ) ? 'rest_post_invalid_action' : 'rest_meta_invalid_action';
// for now let's not allow updating of arrays, objects or serialized values.
return new WP_Error( $code, __( 'Invalid provided meta data for action.' ), array( 'status' => 400 ) );
}
if ( empty( $request['key'] ) ) {
return new WP_Error( 'rest_meta_invalid_key', __( 'Invalid meta key.' ), array( 'status' => 400 ) );
}
if ( is_protected_meta( $request['key'] ) ) {
return new WP_Error( 'rest_meta_protected', sprintf( __( '%s is marked as a protected field.' ), $request['key'] ), array( 'status' => 403 ) );
}
$meta_key = wp_slash( $request['key'] );
$value = wp_slash( $request['value'] );
$mid = add_metadata( $this->parent_type, $parent_id, $meta_key, $value );
if ( ! $mid ) {
return new WP_Error( 'rest_meta_could_not_add', __( 'Could not add meta.' ), array( 'status' => 400 ) );
}
$request = new WP_REST_Request( 'GET' );
$request->set_query_params( array(
'context' => 'edit',
'parent_id' => $parent_id,
'id' => $mid,
) );
$response = rest_ensure_response( $this->get_item( $request ) );
$response->set_status( 201 );
$data = $response->get_data();
$response->header( 'Location', rest_url( $this->namespace . '/' . $this->parent_base . '/' . $parent_id . '/meta/' . $data['id'] ) );
/* This action is documented in lib/endpoints/class-wp-rest-meta-controller.php */
do_action( 'rest_insert_meta', $data, $request, true );
return $response;
}
/**
* Delete meta from an object.
*
* @param WP_REST_Request $request
* @return WP_REST_Response|WP_Error Message on success, WP_Error otherwise
*/
public function delete_item( $request ) {
$parent_id = (int) $request['parent_id'];
$mid = (int) $request['id'];
$force = isset( $request['force'] ) ? (bool) $request['force'] : false;
// We don't support trashing for this type, error out
if ( ! $force ) {
return new WP_Error( 'rest_trash_not_supported', __( 'Meta does not support trashing.' ), array( 'status' => 501 ) );
}
$parent_column = $this->get_parent_column();
$current = get_metadata_by_mid( $this->parent_type, $mid );
if ( empty( $current ) ) {
return new WP_Error( 'rest_meta_invalid_id', __( 'Invalid meta id.' ), array( 'status' => 404 ) );
}
if ( absint( $current->$parent_column ) !== (int) $parent_id ) {
return new WP_Error( 'rest_meta_' . $this->parent_type . '_mismatch', __( 'Meta does not belong to this object' ), array( 'status' => 400 ) );
}
// for now let's not allow updating of arrays, objects or serialized values.
if ( ! $this->is_valid_meta_data( $current->meta_value ) ) {
$code = ( $this->parent_type === 'post' ) ? 'rest_post_invalid_action' : 'rest_meta_invalid_action';
return new WP_Error( $code, __( 'Invalid existing meta data for action.' ), array( 'status' => 400 ) );
}
if ( is_protected_meta( $current->meta_key ) ) {
return new WP_Error( 'rest_meta_protected', sprintf( __( '%s is marked as a protected field.' ), $current->meta_key ), array( 'status' => 403 ) );
}
if ( ! delete_metadata_by_mid( $this->parent_type, $mid ) ) {
return new WP_Error( 'rest_meta_could_not_delete', __( 'Could not delete meta.' ), array( 'status' => 500 ) );
}
/**
* Fires after a meta value is deleted via the REST API.
*
* @param WP_REST_Request $request The request sent to the API.
*/
do_action( 'rest_delete_meta', $request );
return rest_ensure_response( array( 'message' => __( 'Deleted meta' ) ) );
}
}