| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * CMB2 objects/boxes endpoint for WordPres REST API. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Allows access to boxes configuration data. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @todo  Add better documentation. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @todo  Research proper schema. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @since 2.2.4 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  | 	 * The base of this controller's route. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	 * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	protected $rest_base = 'boxes'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	 * The combined $namespace and $rest_base for these routes. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	 * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	protected $namespace_base = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	 * Constructor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	 * @since 2.2.4 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	public function __construct( WP_REST_Server $wp_rest_server ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 		$this->namespace_base = $this->namespace . '/' . $this->rest_base; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 		parent::__construct( $wp_rest_server ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	 * Register the routes for the objects of the controller. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	 * @since 2.2.4 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	public function register_routes() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 		// Returns all boxes data. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		register_rest_route( $this->namespace, '/' . $this->rest_base, array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 			array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 				'methods'  => WP_REST_Server::READABLE, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 				'callback' => array( $this, 'get_items' ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 				'permission_callback' => array( $this, 'get_items_permissions_check' ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 			), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 			'schema' => array( $this, 'get_item_schema' ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 		// Returns specific box's data. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 		register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<cmb_id>[\w-]+)', array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 			array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 				'methods'  => WP_REST_Server::READABLE, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 				'callback' => array( $this, 'get_item' ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 				'permission_callback' => array( $this, 'get_item_permissions_check' ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 			), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 			'schema' => array( $this, 'get_item_schema' ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 		) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	 * Get all public CMB2 boxes. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	 * @since 2.2.4 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	 * @param  WP_REST_Request $request Full data about the request. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	 * @return WP_Error|WP_REST_Response | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 77 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  | 	public function get_items( $request ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  | 		$this->initiate_request( $request, 'boxes_read' ); | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  | 		if ( empty( CMB2_REST::$boxes ) ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  | 			return new WP_Error( 'cmb2_rest_no_boxes', __( 'No boxes found.', 'cmb2' ), array( 'status' => 403 ) ); | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  | 		$boxes_data = array(); | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  | 		// Loop boxes and get specific field. | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  | 		foreach ( CMB2_REST::$boxes as $this->rest_box ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  | 			if ( $this->rest_box->rest_read ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  | 				$rest_box = $this->get_rest_box(); | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  | 				$boxes_data[ $this->rest_box->cmb->cmb_id ] = $this->server->response_to_data( $rest_box, isset( $this->request['_embed'] ) ); | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  | 			} | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  | 		return $this->prepare_item( $boxes_data ); | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	 * Get one CMB2 box from the collection. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 	 * @since 2.2.4 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 	 * @param  WP_REST_Request $request Full data about the request. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 	 * @return WP_Error|WP_REST_Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 	public function get_item( $request ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 		$this->initiate_rest_read_box( $request, 'box_read' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 		if ( is_wp_error( $this->rest_box ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 			return $this->rest_box; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 		return $this->prepare_item( $this->get_rest_box() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 	 * Get a CMB2 box prepared for REST | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 	 * @since 2.2.4 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 	 * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 	public function get_rest_box() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 		$cmb = $this->rest_box->cmb; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 		$boxes_data = $cmb->meta_box; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 		if ( isset( $this->request['_rendered'] ) && $this->namespace_base !== CMB2_REST_Controller::get_intial_route() ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 			$boxes_data['form_open'] = $this->get_cb_results( array( $cmb, 'render_form_open' ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 			$boxes_data['form_close'] = $this->get_cb_results( array( $cmb, 'render_form_close' ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 			global $wp_scripts, $wp_styles; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 			$before_css = $wp_styles->queue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 			$before_js = $wp_scripts->queue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 			CMB2_JS::enqueue(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 			$boxes_data['js_dependencies'] = array_values( array_diff( $wp_scripts->queue, $before_js ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 			$boxes_data['css_dependencies'] = array_values( array_diff( $wp_styles->queue, $before_css ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 		// TODO: look into 'embed' parameter. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 		// http://demo.wp-api.org/wp-json/wp/v2/posts?_embed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 		unset( $boxes_data['fields'] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 		// Handle callable properties. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 		unset( $boxes_data['show_on_cb'] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 		$response = rest_ensure_response( $boxes_data ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 		$response->add_links( $this->prepare_links( $cmb ) ); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 		return $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 	 * Return an array of contextual links for box/boxes. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 	 * @since  2.2.4 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 	 * @param  CMB2_REST $cmb CMB2_REST object to build links from. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 	 * @return array          Array of links | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 	protected function prepare_links( $cmb ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 		$boxbase      = $this->namespace_base . '/' . $cmb->cmb_id; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 		$query_string = $this->get_query_string(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  | 		return array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | 			// Standard Link Relations -- http://v2.wp-api.org/extending/linking/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  | 			'self' => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  | 				'href' => rest_url( $boxbase . $query_string ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 			), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  | 			'collection' => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  | 				'href' => rest_url( $this->namespace_base . $query_string ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  | 			), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  | 			// Custom Link Relations -- http://v2.wp-api.org/extending/linking/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  | 			// TODO URL should document relationship. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  | 			'https://cmb2.io/fields' => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  | 				'href' => rest_url( trailingslashit( $boxbase ) . 'fields' . $query_string ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  | 				'embeddable' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  | 			), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 184 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 185 |  |  |  | 
            
                        
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.