|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Creates CMB2 objects/fields endpoint for WordPres REST API. |
|
4
|
|
|
* Allows access to fields registered to a specific post type and more. |
|
5
|
|
|
* |
|
6
|
|
|
* @todo Add better documentation. |
|
7
|
|
|
* @todo Research proper schema. |
|
8
|
|
|
* |
|
9
|
|
|
* @since 2.2.0 |
|
10
|
|
|
* |
|
11
|
|
|
* @category WordPress_Plugin |
|
12
|
|
|
* @package CMB2 |
|
13
|
|
|
* @author WebDevStudios |
|
14
|
|
|
* @license GPL-2.0+ |
|
15
|
|
|
* @link http://webdevstudios.com |
|
16
|
|
|
*/ |
|
17
|
|
|
class CMB2_REST_Controller_Boxes extends CMB2_REST_Controller { |
|
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Register the routes for the objects of the controller. |
|
21
|
|
|
* |
|
22
|
|
|
* @since 2.2.0 |
|
23
|
|
|
*/ |
|
24
|
|
View Code Duplication |
public function register_routes() { |
|
|
|
|
|
|
25
|
|
|
// Returns all boxes data. |
|
26
|
|
|
register_rest_route( CMB2_REST::BASE, '/boxes/', array( |
|
27
|
|
|
array( |
|
28
|
|
|
'methods' => WP_REST_Server::READABLE, |
|
29
|
|
|
'callback' => array( $this, 'get_boxes' ), |
|
30
|
|
|
'permission_callback' => array( $this, 'get_item_permissions_check' ), |
|
31
|
|
|
), |
|
32
|
|
|
'schema' => array( $this, 'get_item_schema' ), |
|
33
|
|
|
) ); |
|
34
|
|
|
|
|
35
|
|
|
// Returns specific box's data. |
|
36
|
|
|
register_rest_route( CMB2_REST::BASE, '/boxes/(?P<cmb_id>[\w-]+)', array( |
|
37
|
|
|
array( |
|
38
|
|
|
'methods' => WP_REST_Server::READABLE, |
|
39
|
|
|
'callback' => array( $this, 'get_box' ), |
|
40
|
|
|
'permission_callback' => array( $this, 'get_item_permissions_check' ), |
|
41
|
|
|
), |
|
42
|
|
|
'schema' => array( $this, 'get_item_schema' ), |
|
43
|
|
|
) ); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Get all public fields |
|
48
|
|
|
* |
|
49
|
|
|
* @since 2.2.0 |
|
50
|
|
|
* |
|
51
|
|
|
* @param WP_REST_Request $request The API request object. |
|
52
|
|
|
* @return array |
|
53
|
|
|
*/ |
|
54
|
|
|
public function get_boxes( $request ) { |
|
55
|
|
|
$this->initiate_request( $request ); |
|
56
|
|
|
|
|
57
|
|
|
$boxes = CMB2_Boxes::get_by_property( 'show_in_rest', false ); |
|
58
|
|
|
|
|
59
|
|
|
if ( empty( $boxes ) ) { |
|
60
|
|
|
return $this->prepare_item( array( 'error' => __( 'No boxes found.', 'cmb2' ) ), $this->request ); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$boxes_data = array(); |
|
64
|
|
|
// Loop boxes and get specific field. |
|
65
|
|
|
foreach ( $boxes as $key => $cmb ) { |
|
66
|
|
|
$boxes_data[ $cmb->cmb_id ] = $this->get_rest_box( $cmb ); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $this->prepare_item( $boxes_data ); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Get all public fields |
|
74
|
|
|
* |
|
75
|
|
|
* @since 2.2.0 |
|
76
|
|
|
* |
|
77
|
|
|
* @param WP_REST_Request $request The API request object. |
|
78
|
|
|
* @return array |
|
79
|
|
|
*/ |
|
80
|
|
|
public function get_box( $request ) { |
|
81
|
|
|
$this->initiate_request( $request ); |
|
82
|
|
|
|
|
83
|
|
|
$cmb_id = $this->request->get_param( 'cmb_id' ); |
|
84
|
|
|
|
|
85
|
|
|
if ( $cmb_id && ( $cmb = cmb2_get_metabox( $cmb_id, $this->object_id, $this->object_type ) ) ) { |
|
86
|
|
|
return $this->prepare_item( $this->get_rest_box( $cmb ) ); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $this->prepare_item( array( 'error' => __( 'No box found by that id.', 'cmb2' ) ) ); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.