| 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.3 |
| 10 |
* |
| 11 |
* @category WordPress_Plugin |
| 12 |
* @package CMB2 |
| 13 |
* @author CMB2 team |
| 14 |
* @license GPL-2.0+ |
| 15 |
* @link https://cmb2.io |
| 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 |
* |
| 36 |
* @since 2.2.3 |
| 37 |
*/ |
| 38 |
public function __construct( WP_REST_Server $wp_rest_server ) { |
| 39 |
$this->namespace_base = $this->namespace . '/' . $this->rest_base; |
| 40 |
parent::__construct( $wp_rest_server ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Register the routes for the objects of the controller. |
| 45 |
* |
| 46 |
* @since 2.2.3 |
| 47 |
*/ |
| 48 |
public function register_routes() { |
| 49 |
$args = array( |
| 50 |
'_embed' => array( |
| 51 |
'description' => __( 'Includes the registered fields for the box in the response.', 'cmb2' ), |
| 52 |
), |
| 53 |
); |
| 54 |
|
| 55 |
// @todo determine what belongs in the context param. |
| 56 |
// $args['context'] = $this->get_context_param(); |
| 57 |
// $args['context']['required'] = false; |
| 58 |
// $args['context']['default'] = 'view'; |
| 59 |
// $args['context']['enum'] = array( 'view', 'embed' ); |
| 60 |
// Returns all boxes data. |
| 61 |
register_rest_route( $this->namespace, '/' . $this->rest_base, array( |
| 62 |
array( |
| 63 |
'methods' => WP_REST_Server::READABLE, |
| 64 |
'permission_callback' => array( $this, 'get_items_permissions_check' ), |
| 65 |
'callback' => array( $this, 'get_items' ), |
| 66 |
'args' => $args, |
| 67 |
), |
| 68 |
'schema' => array( $this, 'get_item_schema' ), |
| 69 |
) ); |
| 70 |
|
| 71 |
$args['_rendered'] = array( |
| 72 |
'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' ), |
| 73 |
); |
| 74 |
|
| 75 |
// Returns specific box's data. |
| 76 |
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<cmb_id>[\w-]+)', array( |
| 77 |
array( |
| 78 |
'methods' => WP_REST_Server::READABLE, |
| 79 |
'permission_callback' => array( $this, 'get_item_permissions_check' ), |
| 80 |
'callback' => array( $this, 'get_item' ), |
| 81 |
'args' => $args, |
| 82 |
), |
| 83 |
'schema' => array( $this, 'get_item_schema' ), |
| 84 |
) ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Check if a given request has access to get boxes. |
| 89 |
* |
| 90 |
* @since 2.2.3 |
| 91 |
* |
| 92 |
* @param WP_REST_Request $request Full data about the request. |
| 93 |
* @return WP_Error|boolean |
| 94 |
*/ |
| 95 |
public function get_items_permissions_check( $request ) { |
| 96 |
$this->initiate_request( $request, __FUNCTION__ ); |
| 97 |
|
| 98 |
/** |
| 99 |
* By default, no special permissions needed. |
| 100 |
* |
| 101 |
* @since 2.2.3 |
| 102 |
* |
| 103 |
* @param bool $can_access Whether this CMB2 endpoint can be accessed. |
| 104 |
* @param object $controller This CMB2_REST_Controller object. |
| 105 |
*/ |
| 106 |
return apply_filters( 'cmb2_api_get_boxes_permissions_check', true, $this ); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Get all public CMB2 boxes. |
| 111 |
* |
| 112 |
* @since 2.2.3 |
| 113 |
* |
| 114 |
* @param WP_REST_Request $request Full data about the request. |
| 115 |
* @return WP_Error|WP_REST_Response |
| 116 |
*/ |
| 117 |
public function get_items( $request ) { |
| 118 |
$this->initiate_request( $request, 'boxes_read' ); |
| 119 |
|
| 120 |
$boxes = CMB2_REST::get_all(); |
| 121 |
if ( empty( $boxes ) ) { |
| 122 |
return new WP_Error( 'cmb2_rest_no_boxes', __( 'No boxes found.', 'cmb2' ), array( |
| 123 |
'status' => 403, |
| 124 |
) ); |
| 125 |
} |
| 126 |
|
| 127 |
$boxes_data = array(); |
| 128 |
|
| 129 |
// Loop and prepare boxes data. |
| 130 |
foreach ( $boxes as $this->rest_box ) { |
| 131 |
if ( |
| 132 |
// Make sure this box can be read |
| 133 |
$this->rest_box->rest_read |
| 134 |
// And make sure current user can view this box. |
| 135 |
&& $this->get_item_permissions_check_filter( $this->request ) |
| 136 |
) { |
| 137 |
$boxes_data[] = $this->server->response_to_data( |
| 138 |
$this->get_rest_box(), |
| 139 |
isset( $this->request['_embed'] ) |
| 140 |
); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
return $this->prepare_item( $boxes_data ); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Check if a given request has access to a box. |
| 149 |
* By default, no special permissions needed, but filtering return value. |
| 150 |
* |
| 151 |
* @since 2.2.3 |
| 152 |
* |
| 153 |
* @param WP_REST_Request $request Full details about the request. |
| 154 |
* @return WP_Error|boolean |
| 155 |
*/ |
| 156 |
public function get_item_permissions_check( $request ) { |
| 157 |
$this->initiate_rest_read_box( $request, 'box_read' ); |
| 158 |
|
| 159 |
return $this->get_item_permissions_check_filter(); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Check by filter if a given request has access to a box. |
| 164 |
* By default, no special permissions needed, but filtering return value. |
| 165 |
* |
| 166 |
* @since 2.2.3 |
| 167 |
* |
| 168 |
* @param bool $can_access Whether the current request has access to view the box by default. |
| 169 |
* @return WP_Error|boolean |
| 170 |
*/ |
| 171 |
public function get_item_permissions_check_filter( $can_access = true ) { |
| 172 |
/** |
| 173 |
* By default, no special permissions needed. |
| 174 |
* |
| 175 |
* @since 2.2.3 |
| 176 |
* |
| 177 |
* @param bool $can_access Whether this CMB2 endpoint can be accessed. |
| 178 |
* @param object $controller This CMB2_REST_Controller object. |
| 179 |
*/ |
| 180 |
return $this->maybe_hook_callback_and_apply_filters( 'cmb2_api_get_box_permissions_check', $can_access ); |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Get one CMB2 box from the collection. |
| 185 |
* |
| 186 |
* @since 2.2.3 |
| 187 |
* |
| 188 |
* @param WP_REST_Request $request Full data about the request. |
| 189 |
* @return WP_Error|WP_REST_Response |
| 190 |
*/ |
| 191 |
public function get_item( $request ) { |
| 192 |
$this->initiate_rest_read_box( $request, 'box_read' ); |
| 193 |
|
| 194 |
if ( is_wp_error( $this->rest_box ) ) { |
| 195 |
return $this->rest_box; |
| 196 |
} |
| 197 |
|
| 198 |
return $this->prepare_item( $this->get_rest_box() ); |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Get a CMB2 box prepared for REST |
| 203 |
* |
| 204 |
* @since 2.2.3 |
| 205 |
* |
| 206 |
* @return array |
| 207 |
*/ |
| 208 |
public function get_rest_box() { |
| 209 |
$cmb = $this->rest_box->cmb; |
| 210 |
|
| 211 |
$boxes_data = $cmb->meta_box; |
| 212 |
|
| 213 |
if ( isset( $this->request['_rendered'] ) && $this->namespace_base !== ltrim( CMB2_REST_Controller::get_intial_route(), '/' ) ) { |
| 214 |
$boxes_data['form_open'] = $this->get_cb_results( array( $cmb, 'render_form_open' ) ); |
| 215 |
$boxes_data['form_close'] = $this->get_cb_results( array( $cmb, 'render_form_close' ) ); |
| 216 |
|
| 217 |
global $wp_scripts, $wp_styles; |
| 218 |
$before_css = $wp_styles->queue; |
| 219 |
$before_js = $wp_scripts->queue; |
| 220 |
|
| 221 |
CMB2_JS::enqueue(); |
| 222 |
|
| 223 |
$boxes_data['js_dependencies'] = array_values( array_diff( $wp_scripts->queue, $before_js ) ); |
| 224 |
$boxes_data['css_dependencies'] = array_values( array_diff( $wp_styles->queue, $before_css ) ); |
| 225 |
} |
| 226 |
|
| 227 |
// TODO: look into 'embed' parameter. |
| 228 |
// http://demo.wp-api.org/wp-json/wp/v2/posts?_embed |
| 229 |
unset( $boxes_data['fields'] ); |
| 230 |
// Handle callable properties. |
| 231 |
unset( $boxes_data['show_on_cb'] ); |
| 232 |
|
| 233 |
$response = rest_ensure_response( $boxes_data ); |
| 234 |
|
| 235 |
$response->add_links( $this->prepare_links( $cmb ) ); |
| 236 |
|
| 237 |
return $response; |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Return an array of contextual links for box/boxes. |
| 242 |
* |
| 243 |
* @since 2.2.3 |
| 244 |
* |
| 245 |
* @param CMB2_REST $cmb CMB2_REST object to build links from. |
| 246 |
* |
| 247 |
* @return array Array of links |
| 248 |
*/ |
| 249 |
protected function prepare_links( $cmb ) { |
| 250 |
$boxbase = $this->namespace_base . '/' . $cmb->cmb_id; |
| 251 |
$query_string = $this->get_query_string(); |
| 252 |
|
| 253 |
return array( |
| 254 |
// Standard Link Relations -- http://v2.wp-api.org/extending/linking/ |
| 255 |
'self' => array( |
| 256 |
'href' => rest_url( $boxbase . $query_string ), |
| 257 |
), |
| 258 |
'collection' => array( |
| 259 |
'href' => rest_url( $this->namespace_base . $query_string ), |
| 260 |
), |
| 261 |
// Custom Link Relations -- http://v2.wp-api.org/extending/linking/ |
| 262 |
// TODO URL should document relationship. |
| 263 |
'https://cmb2.io/fields' => array( |
| 264 |
'href' => rest_url( trailingslashit( $boxbase ) . 'fields' . $query_string ), |
| 265 |
'embeddable' => true, |
| 266 |
), |
| 267 |
); |
| 268 |
} |
| 269 |
|
| 270 |
} |
| 271 |
|