1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
<?php
if ( ! class_exists( 'WP_REST_Controller' ) ) {
// Shim the WP_REST_Controller class if wp-api plugin not installed, & not in core.
require_once cmb2_dir( 'includes/shim/WP_REST_Controller.php' );
}
/**
* Creates CMB2 objects/fields endpoint for WordPres REST API.
* Allows access to fields registered to a specific post type and more.
*
* @todo Add better documentation.
* @todo Research proper schema.
*
* @since 2.2.3
*
* @category WordPress_Plugin
* @package CMB2
* @author CMB2 team
* @license GPL-2.0+
* @link https://cmb2.io
*/
abstract class CMB2_REST_Controller extends WP_REST_Controller {
/**
* The namespace of this controller's route.
*
* @var string
*/
protected $namespace = CMB2_REST::NAME_SPACE;
/**
* The base of this controller's route.
*
* @var string
*/
protected $rest_base;
/**
* The current request object
*
* @var WP_REST_Request $request
* @since 2.2.3
*/
public $request;
/**
* The current server object
*
* @var WP_REST_Server $server
* @since 2.2.3
*/
public $server;
/**
* Box object id
*
* @var mixed
* @since 2.2.3
*/
public $object_id = null;
/**
* Box object type
*
* @var string
* @since 2.2.3
*/
public $object_type = '';
/**
* CMB2 Instance
*
* @var CMB2_REST
*/
protected $rest_box;
/**
* CMB2_Field Instance
*
* @var CMB2_Field
*/
protected $field;
/**
* The initial route
*
* @var string
* @since 2.2.3
*/
protected static $route = '';
/**
* Defines which endpoint the initial request is.
*
* @var string $request_type
* @since 2.2.3
*/
protected static $request_type = '';
/**
* Constructor
*
* @since 2.2.3
*/
public function __construct( WP_REST_Server $wp_rest_server ) {
$this->server = $wp_rest_server;
}
/**
* A wrapper for `apply_filters` which checks for box/field properties to hook to the filter.
*
* Checks if a CMB object callback property exists, and if it does,
* hook it to the permissions filter.
*
* @since 2.2.3
*
* @param string $filter The name of the filter to apply.
* @param bool $default_access The default access for this request.
*
* @return void
*/
public function maybe_hook_callback_and_apply_filters( $filter, $default_access ) {
if ( ! $this->rest_box && $this->request->get_param( 'cmb_id' ) ) {
$this->rest_box = CMB2_REST::get_rest_box( $this->request->get_param( 'cmb_id' ) );
}
$default_access = $this->maybe_hook_registered_callback( $filter, $default_access );
/**
* Apply the permissions check filter.
*
* @since 2.2.3
*
* @param bool $default_access Whether this CMB2 endpoint can be accessed.
* @param object $controller This CMB2_REST_Controller object.
*/
$default_access = apply_filters( $filter, $default_access, $this );
$this->maybe_unhook_registered_callback( $filter );
return $default_access;
}
/**
* Checks if the CMB2 box has any registered callback parameters for the given filter.
*
* The registered handlers will have a property name which matches the filter, except:
* - The 'cmb2_api' prefix will be removed
* - A '_cb' suffix will be added (to stay inline with other '*_cb' parameters).
*
* @since 2.2.3
*
* @param string $filter The filter name.
* @param bool $default_val The default filter value.
*
* @return bool The possibly-modified filter value (if the '*_cb' param is non-callable).
*/
public function maybe_hook_registered_callback( $filter, $default_val ) {
if ( ! $this->rest_box || is_wp_error( $this->rest_box ) ) {
return $default_val;
}
// Hook box specific filter callbacks.
$val = $this->rest_box->cmb->maybe_hook_parameter( $filter, $default_val );
if ( null !== $val ) {
$default_val = $val;
}
return $default_val;
}
/**
* Unhooks any CMB2 box registered callback parameters for the given filter.
*
* @since 2.2.3
*
* @param string $filter The filter name.
*
* @return void
*/
public function maybe_unhook_registered_callback( $filter ) {
if ( ! $this->rest_box || is_wp_error( $this->rest_box ) ) {
return;
}
// Unhook box specific filter callbacks.
$this->rest_box->cmb->maybe_hook_parameter( $filter, null, 'remove_filter' );
}
/**
* Prepare a CMB2 object for serialization
*
* @since 2.2.3
*
* @param mixed $data
* @return array $data
*/
public function prepare_item( $data ) {
return $this->prepare_item_for_response( $data, $this->request );
}
/**
* Output buffers a callback and returns the results.
*
* @since 2.2.3
*
* @param mixed $cb Callable function/method.
* @return mixed Results of output buffer after calling function/method.
*/
public function get_cb_results( $cb ) {
$args = func_get_args();
array_shift( $args ); // ignore $cb
ob_start();
call_user_func_array( $cb, $args );
return ob_get_clean();
}
/**
* Prepare the CMB2 item for the REST response.
*
* @since 2.2.3
*
* @param mixed $item WordPress representation of the item.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response $response
*/
public function prepare_item_for_response( $data, $request = null ) {
$data = $this->filter_response_by_context( $data, $this->request['context'] );
/**
* Filter the prepared CMB2 item response.
*
* @since 2.2.3
*
* @param mixed $data Prepared data
* @param object $request The WP_REST_Request object
* @param object $cmb2_endpoints This endpoints object
*/
return apply_filters( 'cmb2_rest_prepare', rest_ensure_response( $data ), $this->request, $this );
}
/**
* Initiates the request property and the rest_box property if box is readable.
*
* @since 2.2.3
*
* @param WP_REST_Request $request Request object.
* @param string $request_type A description of the type of request being made.
*
* @return void
*/
protected function initiate_rest_read_box( $request, $request_type ) {
$this->initiate_rest_box( $request, $request_type );
if ( ! is_wp_error( $this->rest_box ) && ! $this->rest_box->rest_read ) {
$this->rest_box = new WP_Error( 'cmb2_rest_no_read_error', __( 'This box does not have read permissions.', 'cmb2' ), array(
'status' => 403,
) );
}
}
/**
* Initiates the request property and the rest_box property if box is writeable.
*
* @since 2.2.3
*
* @param WP_REST_Request $request Request object.
* @param string $request_type A description of the type of request being made.
*
* @return void
*/
protected function initiate_rest_edit_box( $request, $request_type ) {
$this->initiate_rest_box( $request, $request_type );
if ( ! is_wp_error( $this->rest_box ) && ! $this->rest_box->rest_edit ) {
$this->rest_box = new WP_Error( 'cmb2_rest_no_write_error', __( 'This box does not have write permissions.', 'cmb2' ), array(
'status' => 403,
) );
}
}
/**
* Initiates the request property and the rest_box property.
*
* @since 2.2.3
*
* @param WP_REST_Request $request Request object.
* @param string $request_type A description of the type of request being made.
*
* @return void
*/
protected function initiate_rest_box( $request, $request_type ) {
$this->initiate_request( $request, $request_type );
$this->rest_box = CMB2_REST::get_rest_box( $this->request->get_param( 'cmb_id' ) );
if ( ! $this->rest_box ) {
$this->rest_box = new WP_Error( 'cmb2_rest_box_not_found_error', __( 'No box found by that id. A box needs to be registered with the "show_in_rest" parameter configured.', 'cmb2' ), array(
'status' => 403,
) );
} else {
if ( isset( $this->request['object_id'] ) ) {
$this->rest_box->cmb->object_id( sanitize_text_field( $this->request['object_id'] ) );
}
if ( isset( $this->request['object_type'] ) ) {
$this->rest_box->cmb->object_type( sanitize_text_field( $this->request['object_type'] ) );
}
}
}
/**
* Initiates the request property and sets up the initial static properties.
*
* @since 2.2.3
*
* @param WP_REST_Request $request Request object.
* @param string $request_type A description of the type of request being made.
*
* @return void
*/
public function initiate_request( $request, $request_type ) {
$this->request = $request;
if ( ! isset( $this->request['context'] ) || empty( $this->request['context'] ) ) {
$this->request['context'] = 'view';
}
if ( ! self::$request_type ) {
self::$request_type = $request_type;
}
if ( ! self::$route ) {
self::$route = $this->request->get_route();
}
}
/**
* Useful when getting `_embed`-ed items
*
* @since 2.2.3
*
* @return string Initial requested type.
*/
public static function get_intial_request_type() {
return self::$request_type;
}
/**
* Useful when getting `_embed`-ed items
*
* @since 2.2.3
*
* @return string Initial requested route.
*/
public static function get_intial_route() {
return self::$route;
}
/**
* Get CMB2 fields schema, conforming to JSON Schema
*
* @since 2.2.3
*
* @return array
*/
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'CMB2',
'type' => 'object',
'properties' => array(
'description' => array(
'description' => __( 'A human-readable description of the object.', 'cmb2' ),
'type' => 'string',
'context' => array(
'view',
),
),
'name' => array(
'description' => __( 'The id for the object.', 'cmb2' ),
'type' => 'integer',
'context' => array(
'view',
),
),
'name' => array(
'description' => __( 'The title for the object.', 'cmb2' ),
'type' => 'string',
'context' => array(
'view',
),
),
),
);
return $this->add_additional_fields_schema( $schema );
}
/**
* Return an array of contextual links for endpoint/object
*
* @link http://v2.wp-api.org/extending/linking/
* @link http://www.iana.org/assignments/link-relations/link-relations.xhtml
*
* @since 2.2.3
*
* @param mixed $object Object to build links from.
*
* @return array Array of links
*/
abstract protected function prepare_links( $object );
/**
* Get whitelisted query strings from URL for appending to link URLS.
*
* @since 2.2.3
*
* @return string URL query stringl
*/
public function get_query_string() {
$defaults = array(
'object_id' => 0,
'object_type' => '',
'_rendered' => '',
// '_embed' => '',
);
$query_string = '';
foreach ( $defaults as $key => $value ) {
if ( isset( $this->request[ $key ] ) ) {
$query_string .= $query_string ? '&' : '?';
$query_string .= $key;
if ( $value = sanitize_text_field( $this->request[ $key ] ) ) {
$query_string .= '=' . $value;
}
}
}
return $query_string;
}
}