1
|
|
|
<?php |
|
|
|
|
2
|
1 |
|
if ( ! class_exists( 'WP_REST_Controller' ) ) { |
3
|
|
|
// Shim the WP_REST_Controller class if wp-api plugin not installed, & not in core. |
4
|
|
|
require_once cmb2_dir( 'includes/shim/WP_REST_Controller.php' ); |
5
|
|
|
} |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Creates CMB2 objects/fields endpoint for WordPres REST API. |
9
|
|
|
* Allows access to fields registered to a specific post type and more. |
10
|
|
|
* |
11
|
|
|
* @todo Add better documentation. |
12
|
|
|
* @todo Research proper schema. |
13
|
|
|
* |
14
|
|
|
* @since 2.2.4 |
15
|
|
|
* |
16
|
|
|
* @category WordPress_Plugin |
17
|
|
|
* @package CMB2 |
18
|
|
|
* @author WebDevStudios |
19
|
|
|
* @license GPL-2.0+ |
20
|
|
|
* @link http://webdevstudios.com |
21
|
|
|
*/ |
22
|
|
|
abstract class CMB2_REST_Controller extends WP_REST_Controller { |
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The namespace of this controller's route. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $namespace = CMB2_REST::NAME_SPACE; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The base of this controller's route. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $rest_base; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The current request object |
40
|
|
|
* @var WP_REST_Request $request |
41
|
|
|
* @since 2.2.4 |
42
|
|
|
*/ |
43
|
|
|
public $request; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The current server object |
47
|
|
|
* @var WP_REST_Server $server |
48
|
|
|
* @since 2.2.4 |
49
|
|
|
*/ |
50
|
|
|
public $server; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Box object id |
54
|
|
|
* @var mixed |
55
|
|
|
* @since 2.2.4 |
56
|
|
|
*/ |
57
|
|
|
public $object_id = null; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Box object type |
61
|
|
|
* @var string |
62
|
|
|
* @since 2.2.4 |
63
|
|
|
*/ |
64
|
|
|
public $object_type = ''; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* CMB2 Instance |
68
|
|
|
* |
69
|
|
|
* @var CMB2_REST |
70
|
|
|
*/ |
71
|
|
|
protected $rest_box; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* The initial route |
75
|
|
|
* @var string |
76
|
|
|
* @since 2.2.4 |
77
|
|
|
*/ |
78
|
|
|
protected static $route = ''; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Defines which endpoint the initial request is. |
82
|
|
|
* @var string $request_type |
83
|
|
|
* @since 2.2.4 |
84
|
|
|
*/ |
85
|
|
|
protected static $request_type = ''; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Constructor |
89
|
|
|
* @since 2.2.4 |
90
|
|
|
*/ |
91
|
1 |
|
public function __construct( WP_REST_Server $wp_rest_server ) { |
92
|
1 |
|
$this->server = $wp_rest_server; |
93
|
1 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Check if a given request has access to get items. |
97
|
|
|
* |
98
|
|
|
* @since 2.2.4 |
99
|
|
|
* |
100
|
|
|
* @param WP_REST_Request $request Full data about the request. |
101
|
|
|
* @return WP_Error|boolean |
102
|
|
|
*/ |
103
|
2 |
|
public function get_items_permissions_check( $request ) { |
104
|
2 |
|
$this->initiate_request( $request, __FUNCTION__ ); |
105
|
2 |
|
$can_access = true; |
106
|
|
|
|
107
|
2 |
|
$this->maybe_hook_callback( 'get_items_permissions_check_cb' ); |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* By default, no special permissions needed. |
111
|
|
|
* |
112
|
|
|
* @since 2.2.4 |
113
|
|
|
* |
114
|
|
|
* @param bool $can_access Whether this CMB2 endpoint can be accessed. |
115
|
|
|
* @param object $request The WP_REST_Request object |
116
|
|
|
*/ |
117
|
2 |
|
return apply_filters( 'cmb2_api_get_items_permissions_check', $can_access, $this ); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Check if a given request has access to a field or box. |
122
|
|
|
* By default, no special permissions needed, but filtering return value. |
123
|
|
|
* |
124
|
|
|
* @since 2.2.4 |
125
|
|
|
* |
126
|
|
|
* @param WP_REST_Request $request Full details about the request. |
127
|
|
|
* @return WP_Error|boolean |
128
|
|
|
*/ |
129
|
6 |
|
public function get_item_permissions_check( $request ) { |
130
|
6 |
|
$this->initiate_request( $request, __FUNCTION__ ); |
131
|
6 |
|
$can_access = true; |
132
|
|
|
|
133
|
6 |
|
$this->maybe_hook_callback( 'get_item_permissions_check_cb' ); |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* By default, no special permissions needed. |
137
|
|
|
* |
138
|
|
|
* @since 2.2.4 |
139
|
|
|
* |
140
|
|
|
* @param bool $can_access Whether this CMB2 endpoint can be accessed. |
141
|
|
|
* @param object $request The WP_REST_Request object |
142
|
|
|
*/ |
143
|
6 |
|
return apply_filters( 'cmb2_api_get_item_permissions_check', $can_access, $this ); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Check if a given request has access to update a field value. |
148
|
|
|
* By default, requires 'edit_others_posts' capability, but filtering return value. |
149
|
|
|
* |
150
|
|
|
* @since 2.2.4 |
151
|
|
|
* |
152
|
|
|
* @param WP_REST_Request $request Full details about the request. |
153
|
|
|
* @return WP_Error|boolean |
154
|
|
|
*/ |
155
|
5 |
|
public function update_field_value_permissions_check( $request ) { |
156
|
5 |
|
$this->initiate_request( $request, __FUNCTION__ ); |
157
|
5 |
|
$can_update = current_user_can( 'edit_others_posts' ); |
158
|
|
|
|
159
|
5 |
|
$this->maybe_hook_callback( 'update_field_value_permissions_check_cb' ); |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* By default, 'edit_others_posts' is required capability. |
163
|
|
|
* |
164
|
|
|
* @since 2.2.4 |
165
|
|
|
* |
166
|
|
|
* @param bool $can_update Whether this CMB2 endpoint can be accessed. |
167
|
|
|
* @param object $request The WP_REST_Request object |
168
|
|
|
*/ |
169
|
5 |
|
return apply_filters( 'cmb2_api_update_field_value_permissions_check', $can_update, $this ); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Check if a given request has access to delete a field value. |
174
|
|
|
* By default, requires 'delete_others_posts' capability, but filtering return value. |
175
|
|
|
* |
176
|
|
|
* @since 2.2.4 |
177
|
|
|
* |
178
|
|
|
* @param WP_REST_Request $request Full details about the request. |
179
|
|
|
* @return WP_Error|boolean |
180
|
|
|
*/ |
181
|
3 |
|
public function delete_field_value_permissions_check( $request ) { |
182
|
3 |
|
$this->initiate_request( $request, __FUNCTION__ ); |
183
|
3 |
|
$can_delete = current_user_can( 'delete_others_posts' ); |
184
|
|
|
|
185
|
3 |
|
$this->maybe_hook_callback( 'delete_field_value_permissions_check_cb' ); |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* By default, 'delete_others_posts' is required capability. |
189
|
|
|
* |
190
|
|
|
* @since 2.2.4 |
191
|
|
|
* |
192
|
|
|
* @param bool $can_delete Whether this CMB2 endpoint can be accessed. |
193
|
|
|
* @param object $request The WP_REST_Request object |
194
|
|
|
*/ |
195
|
3 |
|
return apply_filters( 'cmb2_api_delete_field_value_permissions_check', $can_delete, $this ); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Check if a CMB object callback property exists, and if it does, |
200
|
|
|
* hook it to the permissions filter. |
201
|
|
|
* |
202
|
|
|
* @since 2.2.4 |
203
|
|
|
* |
204
|
|
|
* @param string $to_check The callback property to check. |
205
|
|
|
* |
206
|
|
|
* @return void |
207
|
|
|
*/ |
208
|
13 |
|
public function maybe_hook_callback( $to_check ) { |
209
|
13 |
|
if ( ! $this->request->get_param( 'cmb_id' ) ) { |
210
|
1 |
|
return; |
211
|
|
|
} |
212
|
|
|
|
213
|
12 |
|
$rest_box = CMB2_REST::get_rest_box( $this->request->get_param( 'cmb_id' ) ); |
214
|
|
|
|
215
|
12 |
|
if ( $rest_box && $rest_box->cmb->prop( "{$to_check}" ) ) { |
216
|
2 |
|
$filter = 'cmb2_api_' . ( substr( $to_check, 0, strlen( $to_check ) - 3 ) ); |
217
|
2 |
|
add_filter( $filter, $rest_box->cmb->prop( "{$to_check}" ), 10, 2 ); |
218
|
2 |
|
} |
219
|
12 |
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Prepare a CMB2 object for serialization |
223
|
|
|
* |
224
|
|
|
* @since 2.2.4 |
225
|
|
|
* |
226
|
|
|
* @param mixed $data |
|
|
|
|
227
|
|
|
* @return array $data |
228
|
|
|
*/ |
229
|
7 |
|
public function prepare_item( $post ) { |
230
|
7 |
|
return $this->prepare_item_for_response( $post, $this->request ); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Output buffers a callback and returns the results. |
235
|
|
|
* |
236
|
|
|
* @since 2.2.4 |
237
|
|
|
* |
238
|
|
|
* @param mixed $cb Callable function/method. |
239
|
|
|
* @return mixed Results of output buffer after calling function/method. |
240
|
|
|
*/ |
241
|
5 |
|
public function get_cb_results( $cb ) { |
242
|
5 |
|
$args = func_get_args(); |
243
|
5 |
|
array_shift( $args ); // ignore $cb |
244
|
5 |
|
ob_start(); |
245
|
5 |
|
call_user_func_array( $cb, $args ); |
246
|
|
|
|
247
|
5 |
|
return ob_get_clean(); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Prepare the CMB2 item for the REST response. |
252
|
|
|
* |
253
|
|
|
* @since 2.2.4 |
254
|
|
|
* |
255
|
|
|
* @param mixed $item WordPress representation of the item. |
|
|
|
|
256
|
|
|
* @param WP_REST_Request $request Request object. |
257
|
|
|
* @return WP_REST_Response $response |
258
|
|
|
*/ |
259
|
7 |
|
public function prepare_item_for_response( $data, $request = null ) { |
260
|
7 |
|
$data = $this->filter_response_by_context( $data, $this->request['context'] ); |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Filter the prepared CMB2 item response. |
264
|
|
|
* |
265
|
|
|
* @since 2.2.4 |
266
|
|
|
* |
267
|
|
|
* @param mixed $data Prepared data |
268
|
|
|
* @param object $request The WP_REST_Request object |
269
|
|
|
* @param object $cmb2_endpoints This endpoints object |
270
|
|
|
*/ |
271
|
7 |
|
return apply_filters( 'cmb2_rest_prepare', rest_ensure_response( $data ), $this->request, $this ); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Initiates the request property and the rest_box property if box is readable. |
276
|
|
|
* |
277
|
|
|
* @since 2.2.4 |
278
|
|
|
* |
279
|
|
|
* @param WP_REST_Request $request Request object. |
280
|
|
|
* @param string $request_type A description of the type of request being made. |
281
|
|
|
* |
282
|
|
|
* @return void |
283
|
|
|
*/ |
284
|
7 |
View Code Duplication |
protected function initiate_rest_read_box( $request, $request_type ) { |
|
|
|
|
285
|
7 |
|
$this->initiate_rest_box( $request, $request_type ); |
286
|
|
|
|
287
|
7 |
|
if ( ! is_wp_error( $this->rest_box ) && ! $this->rest_box->rest_read ) { |
|
|
|
|
288
|
1 |
|
$this->rest_box = new WP_Error( 'cmb2_rest_no_read_error', __( 'This box does not have read permissions.', 'cmb2' ), array( 'status' => 403 ) ); |
|
|
|
|
289
|
1 |
|
} |
290
|
7 |
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Initiates the request property and the rest_box property if box is writeable. |
294
|
|
|
* |
295
|
|
|
* @since 2.2.4 |
296
|
|
|
* |
297
|
|
|
* @param WP_REST_Request $request Request object. |
298
|
|
|
* @param string $request_type A description of the type of request being made. |
299
|
|
|
* |
300
|
|
|
* @return void |
301
|
|
|
*/ |
302
|
|
View Code Duplication |
protected function initiate_rest_edit_box( $request, $request_type ) { |
|
|
|
|
303
|
|
|
$this->initiate_rest_box( $request, $request_type ); |
304
|
|
|
|
305
|
|
|
if ( ! is_wp_error( $this->rest_box ) && ! $this->rest_box->rest_edit ) { |
|
|
|
|
306
|
|
|
$this->rest_box = new WP_Error( 'cmb2_rest_no_write_error', __( 'This box does not have write permissions.', 'cmb2' ), array( 'status' => 403 ) ); |
|
|
|
|
307
|
|
|
} |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Initiates the request property and the rest_box property. |
312
|
|
|
* |
313
|
|
|
* @since 2.2.4 |
314
|
|
|
* |
315
|
|
|
* @param WP_REST_Request $request Request object. |
316
|
|
|
* @param string $request_type A description of the type of request being made. |
317
|
|
|
* |
318
|
|
|
* @return void |
319
|
|
|
*/ |
320
|
7 |
|
protected function initiate_rest_box( $request, $request_type ) { |
321
|
7 |
|
$this->initiate_request( $request, $request_type ); |
322
|
|
|
|
323
|
7 |
|
$this->rest_box = CMB2_REST::get_rest_box( $this->request->get_param( 'cmb_id' ) ); |
|
|
|
|
324
|
|
|
|
325
|
7 |
|
if ( ! $this->rest_box ) { |
326
|
|
|
|
327
|
1 |
|
$this->rest_box = new WP_Error( 'cmb2_rest_box_not_found_error', __( 'No box found by that id. A box needs to be registered with the "show_in_rest" parameter configured.', 'cmb2' ), array( 'status' => 403 ) ); |
|
|
|
|
328
|
|
|
|
329
|
1 |
|
} else { |
330
|
|
|
|
331
|
7 |
View Code Duplication |
if ( isset( $this->request['object_id'] ) ) { |
|
|
|
|
332
|
4 |
|
$this->rest_box->cmb->object_id( sanitize_text_field( $this->request['object_id'] ) ); |
333
|
4 |
|
} |
334
|
|
|
|
335
|
7 |
View Code Duplication |
if ( isset( $this->request['object_type'] ) ) { |
|
|
|
|
336
|
3 |
|
$this->rest_box->cmb->object_type( sanitize_text_field( $this->request['object_type'] ) ); |
337
|
3 |
|
} |
338
|
|
|
} |
339
|
7 |
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* Initiates the request property and sets up the initial static properties. |
343
|
|
|
* |
344
|
|
|
* @since 2.2.4 |
345
|
|
|
* |
346
|
|
|
* @param WP_REST_Request $request Request object. |
347
|
|
|
* @param string $request_type A description of the type of request being made. |
348
|
|
|
* |
349
|
|
|
* @return void |
350
|
|
|
*/ |
351
|
13 |
|
public function initiate_request( $request, $request_type ) { |
352
|
13 |
|
$this->request = $request; |
353
|
|
|
|
354
|
13 |
|
if ( ! isset( $this->request['context'] ) || empty( $this->request['context'] ) ) { |
355
|
13 |
|
$this->request['context'] = 'view'; |
356
|
13 |
|
} |
357
|
|
|
|
358
|
13 |
|
if ( ! self::$request_type ) { |
359
|
1 |
|
self::$request_type = $request_type; |
360
|
1 |
|
} |
361
|
|
|
|
362
|
13 |
|
if ( ! self::$route ) { |
363
|
1 |
|
self::$route = $this->request->get_route(); |
364
|
1 |
|
} |
365
|
13 |
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* Useful when getting `_embed`-ed items |
369
|
|
|
* |
370
|
|
|
* @since 2.2.4 |
371
|
|
|
* |
372
|
|
|
* @return string Initial requested type. |
373
|
|
|
*/ |
374
|
|
|
public static function get_intial_request_type() { |
375
|
|
|
return self::$request_type; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* Useful when getting `_embed`-ed items |
380
|
|
|
* |
381
|
|
|
* @since 2.2.4 |
382
|
|
|
* |
383
|
|
|
* @return string Initial requested route. |
384
|
|
|
*/ |
385
|
5 |
|
public static function get_intial_route() { |
386
|
5 |
|
return self::$route; |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* Get CMB2 fields schema, conforming to JSON Schema |
391
|
|
|
* |
392
|
|
|
* @since 2.2.4 |
393
|
|
|
* |
394
|
|
|
* @return array |
395
|
|
|
*/ |
396
|
8 |
|
public function get_item_schema() { |
397
|
|
|
$schema = array( |
398
|
8 |
|
'$schema' => 'http://json-schema.org/draft-04/schema#', |
399
|
8 |
|
'title' => 'CMB2', |
400
|
8 |
|
'type' => 'object', |
401
|
|
|
'properties' => array( |
402
|
|
|
'description' => array( |
403
|
8 |
|
'description' => __( 'A human-readable description of the object.', 'cmb2' ), |
404
|
8 |
|
'type' => 'string', |
405
|
8 |
|
'context' => array( 'view' ), |
406
|
8 |
|
), |
407
|
|
|
'name' => array( |
408
|
8 |
|
'description' => __( 'The id for the object.', 'cmb2' ), |
409
|
8 |
|
'type' => 'integer', |
410
|
8 |
|
'context' => array( 'view' ), |
411
|
8 |
|
), |
412
|
|
|
'name' => array( |
413
|
8 |
|
'description' => __( 'The title for the object.', 'cmb2' ), |
414
|
8 |
|
'type' => 'string', |
415
|
8 |
|
'context' => array( 'view' ), |
416
|
8 |
|
), |
417
|
8 |
|
), |
418
|
8 |
|
); |
419
|
|
|
|
420
|
8 |
|
return $this->add_additional_fields_schema( $schema ); |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
/** |
424
|
|
|
* Return an array of contextual links for endpoint/object |
425
|
|
|
* @link http://v2.wp-api.org/extending/linking/ |
426
|
|
|
* @link http://www.iana.org/assignments/link-relations/link-relations.xhtml |
427
|
|
|
* |
428
|
|
|
* @since 2.2.4 |
429
|
|
|
* |
430
|
|
|
* @param mixed $object Object to build links from. |
431
|
|
|
* |
432
|
|
|
* @return array Array of links |
433
|
|
|
*/ |
434
|
|
|
abstract protected function prepare_links( $object ); |
435
|
|
|
|
436
|
|
|
/** |
437
|
|
|
* Get whitelisted query strings from URL for appending to link URLS. |
438
|
|
|
* |
439
|
|
|
* @since 2.2.4 |
440
|
|
|
* |
441
|
|
|
* @return string URL query stringl |
442
|
|
|
*/ |
443
|
7 |
|
public function get_query_string() { |
444
|
|
|
$defaults = array( |
445
|
7 |
|
'object_id' => 0, |
446
|
7 |
|
'object_type' => '', |
447
|
7 |
|
'_rendered' => '', |
448
|
|
|
// '_embed' => '', |
|
|
|
|
449
|
7 |
|
); |
450
|
|
|
|
451
|
7 |
|
$query_string = ''; |
452
|
|
|
|
453
|
7 |
|
foreach ( $defaults as $key => $value ) { |
454
|
7 |
|
if ( isset( $this->request[ $key ] ) ) { |
455
|
3 |
|
$query_string .= $query_string ? '&' : '?'; |
456
|
3 |
|
$query_string .= $key; |
457
|
3 |
|
if ( $value = sanitize_text_field( $this->request[ $key ] ) ) { |
458
|
3 |
|
$query_string .= '=' . $value; |
459
|
3 |
|
} |
460
|
3 |
|
} |
461
|
7 |
|
} |
462
|
|
|
|
463
|
7 |
|
return $query_string; |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
} |
467
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.