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
|
|
|
* CMB2 Instance |
21
|
|
|
* |
22
|
|
|
* @var CMB2 |
23
|
|
|
*/ |
24
|
|
|
protected $cmb; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Register the routes for the objects of the controller. |
28
|
|
|
* |
29
|
|
|
* @since 2.2.0 |
30
|
|
|
*/ |
31
|
|
View Code Duplication |
public function register_routes() { |
|
|
|
|
32
|
|
|
// Returns all boxes data. |
33
|
|
|
register_rest_route( CMB2_REST::BASE, '/boxes/', array( |
34
|
|
|
array( |
35
|
|
|
'methods' => WP_REST_Server::READABLE, |
36
|
|
|
'callback' => array( $this, 'get_items' ), |
37
|
|
|
'permission_callback' => array( $this, 'get_item_permissions_check' ), |
38
|
|
|
), |
39
|
|
|
'schema' => array( $this, 'get_item_schema' ), |
40
|
|
|
) ); |
41
|
|
|
|
42
|
|
|
// Returns specific box's data. |
43
|
|
|
register_rest_route( CMB2_REST::BASE, '/boxes/(?P<cmb_id>[\w-]+)', array( |
44
|
|
|
array( |
45
|
|
|
'methods' => WP_REST_Server::READABLE, |
46
|
|
|
'callback' => array( $this, 'get_item' ), |
47
|
|
|
'permission_callback' => array( $this, 'get_item_permissions_check' ), |
48
|
|
|
), |
49
|
|
|
'schema' => array( $this, 'get_item_schema' ), |
50
|
|
|
) ); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get all public fields |
55
|
|
|
* |
56
|
|
|
* @since 2.2.0 |
57
|
|
|
* |
58
|
|
|
* @param WP_REST_Request $request The API request object. |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
|
|
public function get_items( $request ) { |
|
|
|
|
62
|
|
|
$this->initiate_request( $request, 'boxes_read' ); |
63
|
|
|
|
64
|
|
|
if ( empty( CMB2_REST::$boxes ) ) { |
65
|
|
|
return $this->prepare_item( array( 'error' => __( 'No boxes found.', 'cmb2' ) ), $this->request ); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$boxes_data = array(); |
69
|
|
|
// Loop boxes and get specific field. |
70
|
|
|
foreach ( CMB2_REST::$boxes as $this->rest_box ) { |
71
|
|
|
if ( $this->rest_box->rest_read ) { |
72
|
|
|
$rest_box = $this->get_rest_box(); |
73
|
|
|
$boxes_data[ $this->rest_box->cmb->cmb_id ] = $this->server->response_to_data( $rest_box, isset( $_GET['_embed'] ) ); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this->prepare_item( $boxes_data ); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get all public fields |
82
|
|
|
* |
83
|
|
|
* @since 2.2.0 |
84
|
|
|
* |
85
|
|
|
* @param WP_REST_Request $request The API request object. |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
|
|
public function get_item( $request ) { |
89
|
|
|
$this->initiate_rest_read_box( $request, 'box_read' ); |
90
|
|
|
|
91
|
|
|
if ( is_wp_error( $this->rest_box ) ) { |
92
|
|
|
return $this->prepare_item( array( 'error' => $this->rest_box->get_error_message() ) ); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $this->prepare_item( $this->get_rest_box() ); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get a CMB2 box prepared for REST |
100
|
|
|
* |
101
|
|
|
* @since 2.2.0 |
102
|
|
|
* |
103
|
|
|
* @param CMB2 $cmb |
|
|
|
|
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
|
|
public function get_rest_box() { |
|
|
|
|
107
|
|
|
$cmb = $this->rest_box->cmb; |
108
|
|
|
$cmb->object_type( $this->object_id ); |
109
|
|
|
$cmb->object_id( $this->object_type ); |
110
|
|
|
|
111
|
|
|
$boxes_data = $cmb->meta_box; |
112
|
|
|
|
113
|
|
|
if ( isset( $_REQUEST['_rendered'] ) && '/cmb2/v1/boxes' !== CMB2_REST_Controller::get_intial_route() ) { |
114
|
|
|
$boxes_data['form_open'] = $this->get_cb_results( array( $cmb, 'render_form_open' ) ); |
115
|
|
|
$boxes_data['form_close'] = $this->get_cb_results( array( $cmb, 'render_form_close' ) ); |
116
|
|
|
|
117
|
|
|
global $wp_scripts, $wp_styles; |
|
|
|
|
118
|
|
|
$before_css = $wp_styles->queue; |
119
|
|
|
$before_js = $wp_scripts->queue; |
120
|
|
|
|
121
|
|
|
CMB2_JS::enqueue(); |
122
|
|
|
|
123
|
|
|
$boxes_data['js_dependencies'] = array_values( array_diff( $wp_scripts->queue, $before_js ) ); |
124
|
|
|
$boxes_data['css_dependencies'] = array_values( array_diff( $wp_styles->queue, $before_css ) ); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
// TODO: look into 'embed' parameter. |
128
|
|
|
// http://demo.wp-api.org/wp-json/wp/v2/posts?_embed |
129
|
|
|
unset( $boxes_data['fields'] ); |
130
|
|
|
// Handle callable properties. |
131
|
|
|
unset( $boxes_data['show_on_cb'] ); |
132
|
|
|
|
133
|
|
|
$response = rest_ensure_response( $boxes_data ); |
134
|
|
|
|
135
|
|
|
$response->add_links( $this->prepare_links( $cmb ) ); |
136
|
|
|
|
137
|
|
|
return $response; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
protected function prepare_links( $cmb ) { |
141
|
|
|
$base = CMB2_REST::BASE . '/boxes'; |
142
|
|
|
$boxbase = $base . '/' . $cmb->cmb_id; |
143
|
|
|
|
144
|
|
|
return array( |
145
|
|
|
// Standard Link Relations -- http://v2.wp-api.org/extending/linking/ |
146
|
|
|
'self' => array( |
147
|
|
|
'href' => rest_url( $boxbase ), |
148
|
|
|
), |
149
|
|
|
'collection' => array( |
150
|
|
|
'href' => rest_url( $base ), |
151
|
|
|
), |
152
|
|
|
// Custom Link Relations -- http://v2.wp-api.org/extending/linking/ |
153
|
|
|
// TODO URL should document relationship. |
154
|
|
|
'https://cmb2.io/fields' => array( |
155
|
|
|
'href' => rest_url( trailingslashit( $boxbase ) . 'fields' ), |
156
|
|
|
'embeddable' => true, |
157
|
|
|
), |
158
|
|
|
); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
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.