Skip to content

Commit f2a8406

Browse files
committed
Move methods to appropriate classes, clean up, and get embeddable links working.
1 parent 8353dd4 commit f2a8406

File tree

3 files changed

+202
-181
lines changed

3 files changed

+202
-181
lines changed

includes/CMB2_REST_Controller.php

Lines changed: 2 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -52,161 +52,6 @@ public function __construct( WP_REST_Server $wp_rest_server ) {
5252
$this->server = $wp_rest_server;
5353
}
5454

55-
/**
56-
* Get a CMB2 box prepared for REST
57-
*
58-
* @since 2.2.0
59-
*
60-
* @param CMB2 $cmb
61-
* @return array
62-
*/
63-
public function get_rest_box( $cmb ) {
64-
$cmb->object_type( $this->object_id );
65-
$cmb->object_id( $this->object_type );
66-
67-
$boxes_data = $cmb->meta_box;
68-
69-
if ( isset( $_REQUEST['rendered'] ) ) {
70-
$boxes_data['form_open'] = $this->get_cb_results( array( $cmb, 'render_form_open' ) );
71-
$boxes_data['form_close'] = $this->get_cb_results( array( $cmb, 'render_form_close' ) );
72-
73-
global $wp_scripts, $wp_styles;
74-
$before_css = $wp_styles->queue;
75-
$before_js = $wp_scripts->queue;
76-
77-
CMB2_JS::enqueue();
78-
79-
$boxes_data['js_dependencies'] = array_values( array_diff( $wp_scripts->queue, $before_js ) );
80-
$boxes_data['css_dependencies'] = array_values( array_diff( $wp_styles->queue, $before_css ) );
81-
}
82-
83-
// TODO: look into 'embed' parameter.
84-
// http://demo.wp-api.org/wp-json/wp/v2/posts?_embed
85-
unset( $boxes_data['fields'] );
86-
// Handle callable properties.
87-
unset( $boxes_data['show_on_cb'] );
88-
89-
$base = CMB2_REST::BASE . '/boxes/' . $cmb->cmb_id;
90-
$boxbase = $base . '/' . $cmb->cmb_id;
91-
92-
$response = new WP_REST_Response( $boxes_data );
93-
$response->add_links( array(
94-
'self' => array(
95-
'href' => rest_url( trailingslashit( $boxbase ) ),
96-
),
97-
'collection' => array(
98-
'href' => rest_url( trailingslashit( $base ) ),
99-
),
100-
'fields' => array(
101-
'href' => rest_url( trailingslashit( $boxbase ) . 'fields/' ),
102-
),
103-
) );
104-
105-
$boxes_data['_links'] = $response->get_links();
106-
107-
return $boxes_data;
108-
}
109-
110-
/**
111-
* Get a specific field
112-
*
113-
* @since 2.2.0
114-
*
115-
* @param CMB2 $cmb
116-
* @return array|WP_Error
117-
*/
118-
public function get_rest_field( $cmb, $field_id ) {
119-
120-
// TODO: more robust show_in_rest checking. use rest_read/rest_write properties.
121-
// TODO: more robust show_in_rest checking. use rest_read/rest_write properties.
122-
// TODO: more robust show_in_rest checking. use rest_read/rest_write properties.
123-
124-
if ( ! $cmb->prop( 'show_in_rest' ) ) {
125-
return new WP_Error( 'cmb2_rest_error', __( "You don't have permission to view this field.", 'cmb2' ) );
126-
}
127-
128-
$field = $cmb->get_field( $field_id );
129-
130-
if ( ! $field ) {
131-
return new WP_Error( 'cmb2_rest_error', __( 'No field found by that id.', 'cmb2' ) );
132-
}
133-
134-
// TODO: check for show_in_rest property.
135-
// $can_read = $this->can_read
136-
// ? 'write_only' !== $show_in_rest
137-
// : in_array( $show_in_rest, array( 'read_and_write', 'read_only' ), true );
138-
139-
140-
$field_data = $this->prepare_field_data( $field );
141-
142-
$base = CMB2_REST::BASE . '/boxes/' . $cmb->cmb_id;
143-
144-
$response = new WP_REST_Response( $field_data );
145-
$response->add_links( array(
146-
'self' => array(
147-
'href' => rest_url( trailingslashit( $base ) . 'fields/' . $field->_id() ),
148-
),
149-
'collection' => array(
150-
'href' => rest_url( trailingslashit( $base ) . 'fields/' ),
151-
),
152-
'box' => array(
153-
'href' => rest_url( trailingslashit( $base ) ),
154-
),
155-
) );
156-
157-
$field_data['_links'] = $response->get_links();
158-
159-
return $field_data;
160-
}
161-
162-
public function prepare_field_data( CMB2_Field $field ) {
163-
$field_data = array();
164-
$params_to_ignore = array( 'show_on_cb', 'show_in_rest', 'options' );
165-
$params_to_rename = array(
166-
'label_cb' => 'label',
167-
'options_cb' => 'options',
168-
);
169-
170-
// TODO: Use request get object
171-
// Run this first so the js_dependencies arg is populated.
172-
$rendered = isset( $_REQUEST['rendered'] ) && ( $cb = $field->maybe_callback( 'render_row_cb' ) )
173-
// Ok, callback is good, let's run it.
174-
? $this->get_cb_results( $cb, $field->args(), $field )
175-
: false;
176-
177-
foreach ( $field->args() as $key => $value ) {
178-
if ( in_array( $key, $params_to_ignore, true ) ) {
179-
continue;
180-
}
181-
182-
if ( 'render_row_cb' === $key ) {
183-
continue;
184-
}
185-
186-
if ( 'options_cb' === $key ) {
187-
$value = $field->options();
188-
} elseif ( in_array( $key, CMB2_Field::$callable_fields ) ) {
189-
$value = $field->get_param_callback_result( $key );
190-
}
191-
192-
$key = isset( $params_to_rename[ $key ] ) ? $params_to_rename[ $key ] : $key;
193-
194-
if ( empty( $value ) || is_scalar( $value ) || is_array( $value ) ) {
195-
$field_data[ $key ] = $value;
196-
} else {
197-
$field_data[ $key ] = __( 'Value Error', 'cmb2' );
198-
}
199-
}
200-
201-
if ( isset( $_REQUEST['rendered'] ) ) {
202-
$field_data['rendered'] = $rendered;
203-
}
204-
205-
$field_data['value'] = $field->get_data();
206-
207-
return $field_data;
208-
}
209-
21055
/**
21156
* Check if a given request has access to a field or box.
21257
* By default, no special permissions needed, but filtering return value.
@@ -280,7 +125,7 @@ public function initiate_request( $request ) {
280125
* @param WP_REST_Request $request Request object
281126
* @return array $data
282127
*/
283-
public function prepare_item_for_response( $data, $request ) {
128+
public function prepare_item_for_response( $data, $request = null ) {
284129

285130
$context = ! empty( $this->request['context'] ) ? $this->request['context'] : 'view';
286131
$data = $this->filter_response_by_context( $data, $context );
@@ -294,7 +139,7 @@ public function prepare_item_for_response( $data, $request ) {
294139
* @param object $request The WP_REST_Request object
295140
* @param object $cmb2_endpoints This endpoints object
296141
*/
297-
return apply_filters( 'cmb2_rest_prepare', $data, $this->request, $this );
142+
return apply_filters( 'cmb2_rest_prepare', rest_ensure_response( $data ), $this->request, $this );
298143
}
299144

300145
/**

includes/CMB2_REST_Controller_Boxes.php

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
*/
1717
class CMB2_REST_Controller_Boxes extends CMB2_REST_Controller {
1818

19+
/**
20+
* CMB2 Instance
21+
*
22+
* @var CMB2
23+
*/
24+
protected $cmb;
25+
1926
/**
2027
* Register the routes for the objects of the controller.
2128
*
@@ -26,7 +33,7 @@ public function register_routes() {
2633
register_rest_route( CMB2_REST::BASE, '/boxes/', array(
2734
array(
2835
'methods' => WP_REST_Server::READABLE,
29-
'callback' => array( $this, 'get_boxes' ),
36+
'callback' => array( $this, 'get_items' ),
3037
'permission_callback' => array( $this, 'get_item_permissions_check' ),
3138
),
3239
'schema' => array( $this, 'get_item_schema' ),
@@ -36,7 +43,7 @@ public function register_routes() {
3643
register_rest_route( CMB2_REST::BASE, '/boxes/(?P<cmb_id>[\w-]+)', array(
3744
array(
3845
'methods' => WP_REST_Server::READABLE,
39-
'callback' => array( $this, 'get_box' ),
46+
'callback' => array( $this, 'get_item' ),
4047
'permission_callback' => array( $this, 'get_item_permissions_check' ),
4148
),
4249
'schema' => array( $this, 'get_item_schema' ),
@@ -51,7 +58,7 @@ public function register_routes() {
5158
* @param WP_REST_Request $request The API request object.
5259
* @return array
5360
*/
54-
public function get_boxes( $request ) {
61+
public function get_items( $request ) {
5562
$this->initiate_request( $request );
5663

5764
$boxes = CMB2_Boxes::get_by_property( 'show_in_rest', false );
@@ -77,7 +84,7 @@ public function get_boxes( $request ) {
7784
* @param WP_REST_Request $request The API request object.
7885
* @return array
7986
*/
80-
public function get_box( $request ) {
87+
public function get_item( $request ) {
8188
$this->initiate_request( $request );
8289

8390
$cmb_id = $this->request->get_param( 'cmb_id' );
@@ -88,4 +95,64 @@ public function get_box( $request ) {
8895

8996
return $this->prepare_item( array( 'error' => __( 'No box found by that id.', 'cmb2' ) ) );
9097
}
98+
99+
/**
100+
* Get a CMB2 box prepared for REST
101+
*
102+
* @since 2.2.0
103+
*
104+
* @param CMB2 $cmb
105+
* @return array
106+
*/
107+
public function get_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( $this->request['_rendered'] ) ) {
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+
public function prepare_links( $cmb ) {
141+
$base = CMB2_REST::BASE . '/boxes';
142+
$boxbase = $base . '/' . $cmb->cmb_id;
143+
144+
return array(
145+
'self' => array(
146+
'href' => rest_url( $boxbase ),
147+
),
148+
'collection' => array(
149+
'href' => rest_url( $base ),
150+
),
151+
'fields' => array(
152+
'href' => rest_url( trailingslashit( $boxbase ) . 'fields' ),
153+
'embeddable' => true,
154+
),
155+
);
156+
}
157+
91158
}

0 commit comments

Comments
 (0)