1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CMB2 objects/boxes endpoint for WordPres REST API. |
4
|
|
|
* Allows access to boxes configuration data. |
5
|
|
|
* |
6
|
|
|
* @todo Add better documentation. |
7
|
|
|
* @todo Research proper schema. |
8
|
|
|
* |
9
|
|
|
* @since 2.2.4 |
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
|
|
|
* The base of this controller's route. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $rest_base = 'boxes'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The combined $namespace and $rest_base for these routes. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $namespace_base = ''; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Constructor |
35
|
|
|
* @since 2.2.4 |
36
|
|
|
*/ |
37
|
1 |
|
public function __construct( WP_REST_Server $wp_rest_server ) { |
38
|
1 |
|
$this->namespace_base = $this->namespace . '/' . $this->rest_base; |
39
|
1 |
|
parent::__construct( $wp_rest_server ); |
40
|
1 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Register the routes for the objects of the controller. |
44
|
|
|
* |
45
|
|
|
* @since 2.2.4 |
46
|
|
|
*/ |
47
|
1 |
|
public function register_routes() { |
48
|
|
|
$args = array( |
49
|
|
|
'_embed' => array( |
50
|
1 |
|
'description' => __( 'Includes the registered fields for the box in the response.', 'cmb2' ), |
51
|
1 |
|
), |
52
|
|
|
'_rendered' => array( |
53
|
1 |
|
'description' => __( 'Includes the fully rendered attributes, \'form_open\', \'form_close\', as well as the enqueued \'js_dependencies\' script handles, and \'css_dependencies\' stylesheet handles.', 'cmb2' ), |
54
|
1 |
|
), |
55
|
1 |
|
); |
56
|
|
|
|
57
|
|
|
// @todo determine what belongs in the context param. |
58
|
|
|
// $args['context'] = $this->get_context_param(); |
|
|
|
|
59
|
|
|
// $args['context']['required'] = false; |
|
|
|
|
60
|
|
|
// $args['context']['default'] = 'view'; |
|
|
|
|
61
|
|
|
// $args['context']['enum'] = array( 'view', 'embed' ); |
|
|
|
|
62
|
|
|
|
63
|
|
|
// Returns all boxes data. |
64
|
1 |
|
register_rest_route( $this->namespace, '/' . $this->rest_base, array( |
65
|
|
|
array( |
66
|
1 |
|
'methods' => WP_REST_Server::READABLE, |
67
|
1 |
|
'callback' => array( $this, 'get_items' ), |
68
|
1 |
|
'permission_callback' => array( $this, 'get_items_permissions_check' ), |
69
|
1 |
|
'args' => $args, |
70
|
1 |
|
), |
71
|
1 |
|
'schema' => array( $this, 'get_item_schema' ), |
72
|
1 |
|
) ); |
73
|
|
|
|
74
|
|
|
// Returns specific box's data. |
75
|
1 |
|
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<cmb_id>[\w-]+)', array( |
76
|
|
|
array( |
77
|
1 |
|
'methods' => WP_REST_Server::READABLE, |
78
|
1 |
|
'callback' => array( $this, 'get_item' ), |
79
|
1 |
|
'permission_callback' => array( $this, 'get_item_permissions_check' ), |
80
|
1 |
|
'args' => $args, |
81
|
1 |
|
), |
82
|
1 |
|
'schema' => array( $this, 'get_item_schema' ), |
83
|
1 |
|
) ); |
84
|
1 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get all public CMB2 boxes. |
88
|
|
|
* |
89
|
|
|
* @since 2.2.4 |
90
|
|
|
* |
91
|
|
|
* @param WP_REST_Request $request Full data about the request. |
92
|
|
|
* @return WP_Error|WP_REST_Response |
93
|
|
|
*/ |
94
|
1 |
|
public function get_items( $request ) { |
95
|
1 |
|
$this->initiate_request( $request, 'boxes_read' ); |
96
|
|
|
|
97
|
1 |
|
$boxes = CMB2_REST::get_all(); |
98
|
1 |
|
if ( empty( $boxes ) ) { |
99
|
|
|
return new WP_Error( 'cmb2_rest_no_boxes', __( 'No boxes found.', 'cmb2' ), array( 'status' => 403 ) ); |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
$boxes_data = array(); |
103
|
|
|
// Loop boxes and get specific field. |
104
|
1 |
|
foreach ( $boxes as $this->rest_box ) { |
105
|
1 |
|
if ( $this->rest_box->rest_read ) { |
|
|
|
|
106
|
1 |
|
$rest_box = $this->get_rest_box(); |
107
|
1 |
|
$boxes_data[] = $this->server->response_to_data( $rest_box, isset( $this->request['_embed'] ) ); |
108
|
1 |
|
} |
109
|
1 |
|
} |
110
|
|
|
|
111
|
1 |
|
return $this->prepare_item( $boxes_data ); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get one CMB2 box from the collection. |
116
|
|
|
* |
117
|
|
|
* @since 2.2.4 |
118
|
|
|
* |
119
|
|
|
* @param WP_REST_Request $request Full data about the request. |
120
|
|
|
* @return WP_Error|WP_REST_Response |
121
|
|
|
*/ |
122
|
1 |
|
public function get_item( $request ) { |
123
|
1 |
|
$this->initiate_rest_read_box( $request, 'box_read' ); |
124
|
|
|
|
125
|
1 |
|
if ( is_wp_error( $this->rest_box ) ) { |
126
|
1 |
|
return $this->rest_box; |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
1 |
|
return $this->prepare_item( $this->get_rest_box() ); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Get a CMB2 box prepared for REST |
134
|
|
|
* |
135
|
|
|
* @since 2.2.4 |
136
|
|
|
* |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
2 |
|
public function get_rest_box() { |
140
|
2 |
|
$cmb = $this->rest_box->cmb; |
141
|
|
|
|
142
|
2 |
|
$boxes_data = $cmb->meta_box; |
143
|
|
|
|
144
|
2 |
|
if ( isset( $this->request['_rendered'] ) && $this->namespace_base !== CMB2_REST_Controller::get_intial_route() ) { |
145
|
|
|
$boxes_data['form_open'] = $this->get_cb_results( array( $cmb, 'render_form_open' ) ); |
146
|
|
|
$boxes_data['form_close'] = $this->get_cb_results( array( $cmb, 'render_form_close' ) ); |
147
|
|
|
|
148
|
|
|
global $wp_scripts, $wp_styles; |
|
|
|
|
149
|
|
|
$before_css = $wp_styles->queue; |
150
|
|
|
$before_js = $wp_scripts->queue; |
151
|
|
|
|
152
|
|
|
CMB2_JS::enqueue(); |
153
|
|
|
|
154
|
|
|
$boxes_data['js_dependencies'] = array_values( array_diff( $wp_scripts->queue, $before_js ) ); |
155
|
|
|
$boxes_data['css_dependencies'] = array_values( array_diff( $wp_styles->queue, $before_css ) ); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
// TODO: look into 'embed' parameter. |
159
|
|
|
// http://demo.wp-api.org/wp-json/wp/v2/posts?_embed |
160
|
2 |
|
unset( $boxes_data['fields'] ); |
161
|
|
|
// Handle callable properties. |
162
|
2 |
|
unset( $boxes_data['show_on_cb'] ); |
163
|
|
|
|
164
|
2 |
|
$response = rest_ensure_response( $boxes_data ); |
165
|
|
|
|
166
|
2 |
|
$response->add_links( $this->prepare_links( $cmb ) ); |
|
|
|
|
167
|
|
|
|
168
|
2 |
|
return $response; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Return an array of contextual links for box/boxes. |
173
|
|
|
* |
174
|
|
|
* @since 2.2.4 |
175
|
|
|
* |
176
|
|
|
* @param CMB2_REST $cmb CMB2_REST object to build links from. |
177
|
|
|
* |
178
|
|
|
* @return array Array of links |
179
|
|
|
*/ |
180
|
2 |
|
protected function prepare_links( $cmb ) { |
181
|
2 |
|
$boxbase = $this->namespace_base . '/' . $cmb->cmb_id; |
|
|
|
|
182
|
2 |
|
$query_string = $this->get_query_string(); |
183
|
|
|
|
184
|
|
|
return array( |
185
|
|
|
// Standard Link Relations -- http://v2.wp-api.org/extending/linking/ |
186
|
|
|
'self' => array( |
187
|
2 |
|
'href' => rest_url( $boxbase . $query_string ), |
188
|
2 |
|
), |
189
|
|
|
'collection' => array( |
190
|
2 |
|
'href' => rest_url( $this->namespace_base . $query_string ), |
191
|
2 |
|
), |
192
|
|
|
// Custom Link Relations -- http://v2.wp-api.org/extending/linking/ |
193
|
|
|
// TODO URL should document relationship. |
194
|
|
|
'https://cmb2.io/fields' => array( |
195
|
2 |
|
'href' => rest_url( trailingslashit( $boxbase ) . 'fields' . $query_string ), |
196
|
2 |
|
'embeddable' => true, |
197
|
2 |
|
), |
198
|
2 |
|
); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
} |
202
|
|
|
|
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.