11<?php
22/**
3- * Creates CMB2 objects/fields endpoint for WordPres REST API
3+ * Creates CMB2 objects/fields endpoint for WordPres REST API
44 * allows access to what fields are registered to a specific post type and more.
55 *
66 * @since 2.1.3
@@ -19,27 +19,29 @@ class CMB2_REST_Endpoints extends WP_REST_Controller {
1919 */
2020 public function register_routes () {
2121
22- register_rest_route ( 'cmb2/v1 ' , '/fields/ ' , array (
22+ $ version = '1 ' ;
23+ $ namespace = 'cmb2/v ' . $ version ;
24+
25+ // returns all boxes data
26+ register_rest_route ( $ namespace , '/boxes/ ' , array (
2327 array (
2428 'methods ' => WP_REST_Server::READABLE ,
2529 'callback ' => array ( $ this , 'get_items ' ),
26- 'args ' => array (
27- 'post_type ' => array (
28- 'sanitize_callback ' => 'sanitize_key ' ,
29- ),
30- ),
30+ 'permission_callback ' => array ( $ this , 'get_item_permissions_check ' ),
3131 ),
3232 'schema ' => array ( $ this , 'get_item_schema ' ),
3333 ) );
3434
35- register_rest_route ( 'cmb2/v1 ' , '/fields/(?P<field_slug>[\w-]+) ' , array (
35+ // returns specific field data
36+ register_rest_route ( $ namespace , '/boxes/(?P<box_slug>[\w-]+)/fields/(?P<field_slug>[\w-]+) ' , array (
3637 array (
3738 'methods ' => WP_REST_Server::READABLE ,
3839 'callback ' => array ( $ this , 'get_item ' ),
3940 'permission_callback ' => array ( $ this , 'get_item_permissions_check ' ),
4041 ),
4142 'schema ' => array ( $ this , 'get_item_schema ' ),
4243 ) );
44+
4345 }
4446
4547 /**
@@ -50,9 +52,25 @@ public function register_routes() {
5052 */
5153 public function get_items ( $ request ) {
5254
53- $ data = array ( 'cmb box ' ,'cmb box ' ,'cmb box ' ,'cmb box ' );
54-
55- return $ data ;
55+ $ cmb2 = new CMB2_Boxes ();
56+ $ cmb2_boxes = $ cmb2 ->get_all ();
57+
58+ $ data = array ();
59+
60+ if ( $ cmb2_boxes ) {
61+ // loop meta box and get specific field
62+ foreach ( $ cmb2_boxes as $ box ) {
63+ foreach ( $ box ->meta_box as $ key => $ value ) {
64+ if ( true === $ box ->meta_box ['show_in_rest ' ] ){
65+ $ data [$ box ->meta_box ['id ' ]][$ key ] = $ value ;
66+ }
67+ }
68+ }
69+ } else {
70+ $ data = array ( __ ( 'no boxes found ' , 'cmb2 ' ) );
71+ }
72+ // return CMB2_REST_Endpoints box object to prepare_item_for_response
73+ return $ this ->prepare_item_for_response ( $ data , $ request );
5674 }
5775
5876 /**
@@ -63,22 +81,42 @@ public function get_items( $request ) {
6381 */
6482 public function get_item ( $ request ) {
6583
66- $ data = array (
67- 'name ' => 'single cmb box ' ,
68- 'description ' => 'the description of a single cmb box '
69- );
70-
71- return $ this ->prepare_item_for_response ( (object ) $ data , $ request );
84+ $ box_slug = $ request ->get_param ( 'box_slug ' );
85+ $ field_slug = $ request ->get_param ( 'field_slug ' );
86+
87+ $ cmb2 = new CMB2_Boxes ();
88+ $ cmb2_boxes = $ cmb2 ->get ( $ box_slug );
89+
90+ $ data = array ();
91+
92+ if ( $ cmb2_boxes ) {
93+ // loop meta box and get specific field
94+ foreach ( $ cmb2_boxes ->meta_box as $ key => $ value ) {
95+ if ( true === $ cmb2_boxes ->meta_box ['show_in_rest ' ] ){
96+ foreach ( $ cmb2_boxes ->meta_box ['fields ' ] as $ field => $ field_value ) {
97+ if ( $ field_slug === $ field ){
98+ $ data [$ field ] = $ field_value ;
99+ } else {
100+ $ data = array ( __ ( 'field does not exist ' , 'cmb2 ' ) );
101+ }
102+ }
103+ }
104+ }
105+
106+ } else {
107+ $ data = array ( __ ( 'box does not exist ' , 'cmb2 ' ) );
108+ }
109+ // return CMB2_REST_Endpoints field to prepare_item_for_response
110+ return $ this ->prepare_item_for_response ( $ data , $ request );
72111 }
73112
74113 /**
75- * Check if a given request has access to a field
114+ * Check if a given request has access to a field or box
76115 *
77116 * @param WP_REST_Request $request Full details about the request.
78117 * @return bool
79118 */
80119 public function get_item_permissions_check ( $ request ) {
81-
82120 return true ;
83121 }
84122
@@ -88,17 +126,13 @@ public function get_item_permissions_check( $request ) {
88126 * @param WP_REST_Request $request
89127 * @return array Taxonomy data
90128 */
91- public function prepare_item_for_response ( $ cmb2 , $ request ) {
129+ public function prepare_item_for_response ( $ data , $ request ) {
92130
93- $ data = array (
94- 'name ' => $ cmb2 ->name ,
95- 'description ' => $ cmb2 ->description ,
96- );
97131
98132 $ context = ! empty ( $ request ['context ' ] ) ? $ request ['context ' ] : 'view ' ;
99133 $ data = $ this ->filter_response_by_context ( $ data , $ context );
100134
101- return apply_filters ( 'rest_prepare_cmb2 ' , $ data , $ cmb2 , $ request );
135+ return apply_filters ( 'rest_prepare_cmb2 ' , $ data , $ request );
102136 }
103137
104138 /**
@@ -117,6 +151,11 @@ public function get_item_schema() {
117151 'type ' => 'string ' ,
118152 'context ' => array ( 'view ' ),
119153 ),
154+ 'name ' => array (
155+ 'description ' => 'The id for the object. ' ,
156+ 'type ' => 'integer ' ,
157+ 'context ' => array ( 'view ' ),
158+ ),
120159 'name ' => array (
121160 'description ' => 'The title for the object. ' ,
122161 'type ' => 'string ' ,
0 commit comments