Skip to content

Commit d415245

Browse files
committed
Include "args" argument in the registered rest routes for boxes/fields.
This enables better discovery in the schema, and also handles the required parameters for us for the DELETE endpoint.
1 parent 0583ba5 commit d415245

File tree

3 files changed

+65
-20
lines changed

3 files changed

+65
-20
lines changed

includes/CMB2_REST_Controller_Boxes.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,39 @@ public function __construct( WP_REST_Server $wp_rest_server ) {
4545
* @since 2.2.4
4646
*/
4747
public function register_routes() {
48+
$args = array(
49+
'_embed' => array(
50+
'description' => __( 'Includes the registered fields for the box in the response.', 'cmb2' ),
51+
),
52+
'_rendered' => array(
53+
'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+
),
55+
);
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' );
4862

4963
// Returns all boxes data.
5064
register_rest_route( $this->namespace, '/' . $this->rest_base, array(
5165
array(
5266
'methods' => WP_REST_Server::READABLE,
5367
'callback' => array( $this, 'get_items' ),
5468
'permission_callback' => array( $this, 'get_items_permissions_check' ),
55-
'args' => $this->get_collection_params(),
69+
'args' => $args,
5670
),
5771
'schema' => array( $this, 'get_item_schema' ),
5872
) );
5973

6074
// Returns specific box's data.
6175
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<cmb_id>[\w-]+)', array(
6276
array(
63-
'methods' => WP_REST_Server::READABLE,
64-
'callback' => array( $this, 'get_item' ),
77+
'methods' => WP_REST_Server::READABLE,
78+
'callback' => array( $this, 'get_item' ),
6579
'permission_callback' => array( $this, 'get_item_permissions_check' ),
80+
'args' => $args,
6681
),
6782
'schema' => array( $this, 'get_item_schema' ),
6883
) );

includes/CMB2_REST_Controller_Fields.php

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,34 +22,56 @@ class CMB2_REST_Controller_Fields extends CMB2_REST_Controller_Boxes {
2222
* @since 2.2.4
2323
*/
2424
public function register_routes() {
25+
$args = array(
26+
'_embed' => array(
27+
'description' => __( 'Includes the box object which the fields are registered to in the response.', 'cmb2' ),
28+
),
29+
'_rendered' => array(
30+
'description' => __( 'When the \'rendered\' argument is passed, the renderable field attributes will be returned fully rendered. By default, the names of the callback handers for the renderable attributes will be returned.', 'cmb2' ),
31+
),
32+
'object_id' => array(
33+
'description' => __( 'To view or modify the field\'s value, the \'object_id\' and \'object_type\' arguments are required.', 'cmb2' ),
34+
),
35+
'object_type' => array(
36+
'description' => __( 'To view or modify the field\'s value, the \'object_id\' and \'object_type\' arguments are required.', 'cmb2' ),
37+
),
38+
);
2539

2640
// Returns specific box's fields.
2741
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<cmb_id>[\w-]+)/fields/', array(
2842
array(
29-
'methods' => WP_REST_Server::READABLE,
30-
'callback' => array( $this, 'get_items' ),
43+
'methods' => WP_REST_Server::READABLE,
44+
'callback' => array( $this, 'get_items' ),
3145
'permission_callback' => array( $this, 'get_items_permissions_check' ),
46+
'args' => $args,
3247
),
3348
'schema' => array( $this, 'get_item_schema' ),
3449
) );
3550

51+
$delete_args = $args;
52+
$delete_args['object_id']['required'] = true;
53+
$delete_args['object_type']['required'] = true;
54+
3655
// Returns specific field data.
3756
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<cmb_id>[\w-]+)/fields/(?P<field_id>[\w-]+)', array(
3857
array(
39-
'methods' => WP_REST_Server::READABLE,
40-
'callback' => array( $this, 'get_item' ),
58+
'methods' => WP_REST_Server::READABLE,
59+
'callback' => array( $this, 'get_item' ),
4160
'permission_callback' => array( $this, 'get_item_permissions_check' ),
61+
'args' => $args,
4262
),
4363
array(
44-
'methods' => WP_REST_Server::EDITABLE,
45-
'callback' => array( $this, 'update_field_value' ),
46-
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
64+
'methods' => WP_REST_Server::EDITABLE,
65+
'callback' => array( $this, 'update_field_value' ),
66+
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
4767
'permission_callback' => array( $this, 'update_field_value_permissions_check' ),
68+
'args' => $args,
4869
),
4970
array(
50-
'methods' => WP_REST_Server::DELETABLE,
51-
'callback' => array( $this, 'delete_field_value' ),
71+
'methods' => WP_REST_Server::DELETABLE,
72+
'callback' => array( $this, 'delete_field_value' ),
5273
'permission_callback' => array( $this, 'delete_field_value_permissions_check' ),
74+
'args' => $delete_args,
5375
),
5476
'schema' => array( $this, 'get_item_schema' ),
5577
) );

tests/test-cmb-rest-controllers.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public function test_read_box_field() {
130130
$this->assertResponseStatuses( $url, array(
131131
'GET' => 200,
132132
'POST' => array( 403 => 'rest_forbidden' ),
133-
'DELETE' => array( 403 => 'rest_forbidden' ),
133+
'DELETE' => array( 400 => 'rest_missing_callback_param' ),
134134
) );
135135

136136
$mb = $this->metabox_array;
@@ -145,7 +145,7 @@ public function test_read_box_field() {
145145
$this->assertResponseStatuses( $url, array(
146146
'GET' => array( 403 => 'cmb2_rest_no_field_by_id_error' ),
147147
'POST' => array( 403 => 'rest_forbidden' ),
148-
'DELETE' => array( 403 => 'rest_forbidden' ),
148+
'DELETE' => array( 400 => 'rest_missing_callback_param' ),
149149
) );
150150
}
151151

@@ -155,8 +155,13 @@ public function test_read_box_field_filter() {
155155
$this->assertResponseStatuses( $url, array(
156156
'GET' => array( 403 => 'rest_forbidden' ),
157157
'POST' => array( 403 => 'rest_forbidden' ),
158-
'DELETE' => array( 403 => 'rest_forbidden' ),
158+
'DELETE' => array( 400 => 'rest_missing_callback_param' ),
159159
) );
160+
161+
$request = new WP_REST_Request( 'DELETE', $url );
162+
$request['object_id'] = $this->post_id;
163+
$request['object_type'] = 'post';
164+
$this->assertResponseStatus( 403, rest_do_request( $request ), 'rest_forbidden' );
160165
}
161166

162167
/**
@@ -268,19 +273,22 @@ public function test_delete_bad_request_for_admin() {
268273
$url = '/' . CMB2_REST::NAME_SPACE . '/boxes/test/fields/rest_test';
269274
$request = new WP_REST_Request( 'DELETE', $url );
270275
$response = rest_do_request( $request );
271-
$this->assertResponseStatus( 400, $response, 'cmb2_rest_modify_field_value_error' );
276+
$this->assertResponseStatus( 400, $response, 'rest_missing_callback_param' );
272277
$this->assertResponseData( array(
273-
'code' => 'cmb2_rest_modify_field_value_error',
274-
'message' => __( 'CMB2 Field value cannot be modified without the object_id and object_type parameters specified.', 'cmb2' ),
278+
'code' => 'rest_missing_callback_param',
279+
'message' => 'Missing parameter(s): object_id, object_type',
275280
'data' => array(
276281
'status' => 400,
282+
'params' => array(
283+
'object_id',
284+
'object_type',
285+
),
277286
),
278287
), $response );
279288

280-
281289
$request['object_id'] = $this->post_id;
282290
$response = rest_do_request( $request );
283-
$this->assertResponseStatus( 400, $response, 'cmb2_rest_modify_field_value_error' );
291+
$this->assertResponseStatus( 400, $response, 'rest_missing_callback_param' );
284292
}
285293

286294
public function test_delete_authorized_for_admin() {

0 commit comments

Comments
 (0)