Skip to content

Commit 35e1e72

Browse files
committed
Properly handle getting/updating fields for different object types.
Squashed commit of the following: commit 7e9ee84159c9b45781f86423c9ddb66d6f8eaf05 Author: Justin Sternberg <[email protected]> Date: Thu Oct 20 17:35:08 2016 -0400 Fix bleeding global scope by resetting object params at teardown, and uncomment tests commit 41c542277b6c9a2c730793f0b8507e178d522fb1 Author: Justin Sternberg <[email protected]> Date: Thu Oct 20 17:31:09 2016 -0400 Make CMB2_REST::$boxes and CMB2_REST::$type_boxes properties protected Also add a get_all method for $boxes commit 4e6044d6bd9a3a400286a4c7cf58b10dc93f39df Merge: 82a6679 4a91731 Author: Justin Sternberg <[email protected]> Date: Wed Oct 19 11:46:16 2016 -0400 Merge branch 'trunk' into cmb2-rest-api-update commit 82a6679ddcc1b3084c49ee125ed23333793478a6 Author: Justin Sternberg <[email protected]> Date: Wed Oct 19 09:53:36 2016 -0400 Need to figure out what the deal is with the tests initiations since they run in isolation, but not with all tests
1 parent f19a751 commit 35e1e72

File tree

6 files changed

+142
-84
lines changed

6 files changed

+142
-84
lines changed

includes/CMB2_REST.php

Lines changed: 74 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,18 @@ class CMB2_REST extends CMB2_Hookup_Base {
3737
* @var CMB2_REST[] objects
3838
* @since 2.2.4
3939
*/
40-
public static $boxes;
40+
protected static $boxes = array();
41+
42+
/**
43+
* @var array Array of cmb ids for each type.
44+
* @since 2.2.4
45+
*/
46+
protected static $type_boxes = array(
47+
'post' => array(),
48+
'user' => array(),
49+
'comment' => array(),
50+
'term' => array(),
51+
);
4152

4253
/**
4354
* Array of readable field objects.
@@ -128,19 +139,18 @@ public function init_routes() {
128139
*/
129140
public static function register_cmb2_fields() {
130141
$alltypes = $taxonomies = array();
131-
$has_user = $has_comment = $has_taxonomies = false;
132142

133143
foreach ( self::$boxes as $cmb_id => $rest_box ) {
134144
$types = array_flip( $rest_box->cmb->box_types() );
135145

136146
if ( isset( $types['user'] ) ) {
137147
unset( $types['user'] );
138-
$has_user = true;
148+
self::$type_boxes['user'][ $cmb_id ] = $cmb_id;
139149
}
140150

141151
if ( isset( $types['comment'] ) ) {
142152
unset( $types['comment'] );
143-
$has_comment = true;
153+
self::$type_boxes['comment'][ $cmb_id ] = $cmb_id;
144154
}
145155

146156
if ( isset( $types['term'] ) ) {
@@ -151,11 +161,12 @@ public static function register_cmb2_fields() {
151161
CMB2_Utils::ensure_array( $rest_box->cmb->prop( 'taxonomies' ) )
152162
);
153163

154-
$has_taxonomies = true;
164+
self::$type_boxes['term'][ $cmb_id ] = $cmb_id;
155165
}
156166

157167
if ( ! empty( $types ) ) {
158168
$alltypes = array_merge( $alltypes, array_flip( $types ) );
169+
self::$type_boxes['post'][ $cmb_id ] = $cmb_id;
159170
}
160171
}
161172

@@ -165,15 +176,15 @@ public static function register_cmb2_fields() {
165176
self::register_rest_field( $alltypes, 'post' );
166177
}
167178

168-
if ( $has_user ) {
179+
if ( ! empty( self::$type_boxes['user'] ) ) {
169180
self::register_rest_field( 'user', 'user' );
170181
}
171182

172-
if ( $has_comment ) {
183+
if ( ! empty( self::$type_boxes['comment'] ) ) {
173184
self::register_rest_field( 'comment', 'comment' );
174185
}
175186

176-
if ( $has_taxonomies ) {
187+
if ( ! empty( self::$type_boxes['term'] ) ) {
177188
self::register_rest_field( $taxonomies, 'term' );
178189
}
179190
}
@@ -354,24 +365,21 @@ protected static function get_rest_values( $object, $request, $object_type, $mai
354365

355366
$values = array();
356367

357-
foreach ( self::$boxes as $cmb_id => $rest_box ) {
358-
$check = 'term' === $main_object_type ? 'term' : $object_type;
359-
360-
// This is not the box you're looking for...
361-
if ( ! in_array( $check, $rest_box->cmb->box_types() ) ) {
362-
continue;
363-
}
368+
if ( ! empty( self::$type_boxes[ $main_object_type ] ) ) {
369+
foreach ( self::$type_boxes[ $main_object_type ] as $cmb_id ) {
370+
$rest_box = self::$boxes[ $cmb_id ];
364371

365-
foreach ( $rest_box->read_fields as $field_id ) {
366-
$rest_box->cmb->object_id( $object['id'] );
367-
$rest_box->cmb->object_type( $main_object_type );
372+
foreach ( $rest_box->read_fields as $field_id ) {
373+
$rest_box->cmb->object_id( $object['id'] );
374+
$rest_box->cmb->object_type( $main_object_type );
368375

369-
$field = $rest_box->cmb->get_field( $field_id );
376+
$field = $rest_box->cmb->get_field( $field_id );
370377

371-
$field->object_id( $object['id'] );
372-
$field->object_type( $main_object_type );
378+
$field->object_id( $object['id'] );
379+
$field->object_type( $main_object_type );
373380

374-
$values[ $cmb_id ][ $field->id( true ) ] = $field->get_data();
381+
$values[ $cmb_id ][ $field->id( true ) ] = $field->get_data();
382+
}
375383
}
376384
}
377385

@@ -472,25 +480,27 @@ protected static function update_rest_values( $values, $object, $request, $objec
472480
return;
473481
}
474482

475-
// @todo verify that $object_type matches this output.
476-
$data = self::get_object_data( $object );
477-
if ( ! $data ) {
483+
$object_id = self::get_object_id( $object, $main_object_type );
484+
485+
if ( ! $object_id ) {
478486
return;
479487
}
480488

481489
$updated = array();
482490

483-
// @todo Security hardening... check for object type, check for show_in_rest values.
484-
foreach ( self::$boxes as $cmb_id => $rest_box ) {
485-
if ( ! array_key_exists( $cmb_id, $values ) ) {
486-
continue;
487-
}
491+
if ( ! empty( self::$type_boxes[ $main_object_type ] ) ) {
492+
foreach ( self::$type_boxes[ $main_object_type ] as $cmb_id ) {
493+
$rest_box = self::$boxes[ $cmb_id ];
488494

489-
$rest_box->cmb->object_id( $data['object_id'] );
490-
$rest_box->cmb->object_type( $data['object_type'] );
495+
if ( ! array_key_exists( $cmb_id, $values ) ) {
496+
continue;
497+
}
491498

492-
// TODO: Test since refactor.
493-
$updated[ $cmb_id ] = $rest_box->sanitize_box_values( $values );
499+
$rest_box->cmb->object_id( $object_id );
500+
$rest_box->cmb->object_type( $main_object_type );
501+
502+
$updated[ $cmb_id ] = $rest_box->sanitize_box_values( $values );
503+
}
494504
}
495505

496506
return $updated;
@@ -587,27 +597,26 @@ public function is_protected_meta( $protected, $meta_key, $meta_type ) {
587597
return $protected;
588598
}
589599

590-
protected static function get_object_data( $object ) {
591-
$object_id = 0;
592-
if ( isset( $object->ID ) ) {
593-
$object_id = intval( $object->ID );
594-
$object_type = isset( $object->user_login ) ? 'user' : 'post';
595-
} elseif ( isset( $object->comment_ID ) ) {
596-
$object_id = intval( $object->comment_ID );
597-
$object_type = 'comment';
598-
} elseif ( is_array( $object ) && isset( $object['term_id'] ) ) {
599-
$object_id = intval( $object['term_id'] );
600-
$object_type = 'term';
601-
} elseif ( isset( $object->term_id ) ) {
602-
$object_id = intval( $object->term_id );
603-
$object_type = 'term';
604-
}
605-
606-
if ( empty( $object_id ) ) {
607-
return false;
608-
}
609-
610-
return compact( 'object_id', 'object_type' );
600+
protected static function get_object_id( $object, $object_type = 'post' ) {
601+
switch ( $object_type ) {
602+
case 'user':
603+
case 'post':
604+
if ( isset( $object->ID ) ) {
605+
return intval( $object->ID );
606+
}
607+
case 'comment':
608+
if ( isset( $object->comment_ID ) ) {
609+
return intval( $object->comment_ID );
610+
}
611+
case 'term':
612+
if ( is_array( $object ) && isset( $object['term_id'] ) ) {
613+
return intval( $object['term_id'] );
614+
} elseif ( isset( $object->term_id ) ) {
615+
return intval( $object->term_id );
616+
}
617+
}
618+
619+
return 0;
611620
}
612621

613622
public function field_can_read( $field_id, $return_object = false ) {
@@ -652,6 +661,16 @@ public static function remove( $cmb_id ) {
652661
}
653662
}
654663

664+
/**
665+
* Retrieve all CMB2_REST instances from the registry.
666+
*
667+
* @since 2.2.4
668+
* @return CMB2[] Array of all registered CMB2_REST instances.
669+
*/
670+
public static function get_all() {
671+
return self::$boxes;
672+
}
673+
655674
/**
656675
* Checks if given value is readable.
657676
*

includes/CMB2_REST_Controller_Boxes.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,14 @@ public function register_routes() {
7878
public function get_items( $request ) {
7979
$this->initiate_request( $request, 'boxes_read' );
8080

81-
if ( empty( CMB2_REST::$boxes ) ) {
81+
$boxes = CMB2_REST::get_all();
82+
if ( empty( $boxes ) ) {
8283
return new WP_Error( 'cmb2_rest_no_boxes', __( 'No boxes found.', 'cmb2' ), array( 'status' => 403 ) );
8384
}
8485

8586
$boxes_data = array();
8687
// Loop boxes and get specific field.
87-
foreach ( CMB2_REST::$boxes as $this->rest_box ) {
88+
foreach ( $boxes as $this->rest_box ) {
8889
if ( $this->rest_box->rest_read ) {
8990
$rest_box = $this->get_rest_box();
9091
$boxes_data[ $this->rest_box->cmb->cmb_id ] = $this->server->response_to_data( $rest_box, isset( $this->request['_embed'] ) );

tests/cmb-rest-tests-base.php

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,32 @@ public function set_up_and_init( $metabox_array ) {
4343
foreach ( $this->metabox_array['fields'] as $field ) {
4444
update_post_meta( $this->post_id, $field['id'], md5( $field['id'] ) );
4545
}
46+
47+
CMB2_REST::register_cmb2_fields();
4648
}
4749

4850
public function tearDown() {
4951
parent::tearDown();
50-
foreach ( $this->metabox_array['fields'] as $field ) {
51-
delete_post_meta( $this->post_id, $field['id'] );
52+
if ( ! empty( $this->metabox_array['fields'] ) ) {
53+
foreach ( $this->metabox_array['fields'] as $field ) {
54+
delete_post_meta( $this->post_id, $field['id'] );
55+
}
5256
}
53-
CMB2_Boxes::remove( $this->cmb_id );
54-
CMB2_REST::remove( $this->cmb_id );
57+
58+
Test_CMB2_REST_Object::reset_boxes();
59+
Test_CMB2_REST_Object::reset_type_boxes();
60+
61+
foreach ( CMB2_Boxes::get_all() as $box ) {
62+
CMB2_Boxes::remove( $box->cmb_id );
63+
}
64+
65+
global $wp_actions, $wp_rest_server;
66+
unset( $wp_rest_server );
67+
unset( $wp_actions['rest_api_init'] );
5568
}
5669

5770
protected function reset_instances() {
58-
foreach ( CMB2_REST::$boxes as $cmb_id => $rest ) {
71+
foreach ( CMB2_REST::get_all() as $cmb_id => $rest ) {
5972
$rest = new CMB2_REST( $rest->cmb );
6073
$rest->universal_hooks();
6174
}
@@ -116,7 +129,7 @@ protected function assertResponseData( $data, $response ) {
116129

117130
if ( ! class_exists( 'Test_CMB2_REST_Object' ) ) {
118131
/**
119-
* Simply allows access to the mb_defaults protected property (for testing)
132+
* Make some things accessible in the CMB2_REST class.
120133
*/
121134
class Test_CMB2_REST_Object extends CMB2_REST {
122135
public function declare_read_edit_fields() {
@@ -128,8 +141,19 @@ public function can_read( $show_in_rest ) {
128141
public function can_edit( $show_in_rest ) {
129142
return parent::can_edit( $show_in_rest );
130143
}
131-
public static function get_object_data( $object ) {
132-
return parent::get_object_data( $object );
144+
public static function get_object_id( $object, $object_type = 'post' ) {
145+
return parent::get_object_id( $object, $object_type );
146+
}
147+
public static function reset_boxes() {
148+
parent::$boxes = array();
149+
}
150+
public static function reset_type_boxes() {
151+
parent::$type_boxes = array(
152+
'post' => array(),
153+
'user' => array(),
154+
'comment' => array(),
155+
'term' => array(),
156+
);
133157
}
134158
}
135159
}

tests/test-cmb-rest-controllers.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ public function setUp() {
4343
) );
4444
}
4545

46+
public function tearDown() {
47+
parent::tearDown();
48+
}
49+
4650
public function test_get_schema() {
4751
$this->assertResponseStatuses( '/' . CMB2_REST::NAME_SPACE, array(
4852
'GET' => 200,

0 commit comments

Comments
 (0)