Skip to content

Commit

Permalink
Clean up REST API classes and create classes specifically for handlin…
Browse files Browse the repository at this point in the history
…g api boxes and api fields
  • Loading branch information
jtsternberg committed Sep 8, 2016
1 parent 16b7157 commit 07ffdef
Show file tree
Hide file tree
Showing 6 changed files with 265 additions and 204 deletions.
2 changes: 1 addition & 1 deletion bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function cmb2_bootstrap() {
}

if ( $cmb->prop( 'show_in_rest' ) && function_exists( 'register_api_field' ) ) {
$rest = new CMB2_REST_Access( $cmb );
$rest = new CMB2_REST( $cmb );
$rest->universal_hooks();
}

Expand Down
30 changes: 27 additions & 3 deletions includes/CMB2_REST_Access.php → includes/CMB2_REST.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,21 @@
* @license GPL-2.0+
* @link http://webdevstudios.com
*/
class CMB2_REST_Access extends CMB2_Hookup_Base {
class CMB2_REST extends CMB2_Hookup_Base {

/**
* The current CMB2 REST endpoint version
* @var string
* @since 2.2.0
*/
const VERSION = '1';

/**
* The CMB2 REST base namespace (v should always be followed by $version)
* @var string
* @since 2.2.0
*/
const BASE = 'cmb2/v1';

/**
* @var CMB2[] objects
Expand Down Expand Up @@ -59,14 +73,24 @@ public function __construct( CMB2 $cmb ) {
public function universal_hooks() {
$this->once( 'rest_api_init', array( __CLASS__, 'register_fields' ), 50 );

// hook up the CMB rest endpoint class
$this->once( 'rest_api_init', array( cmb2_rest_endpoints(), 'register_routes' ), 0 );
// hook up the CMB rest endpoint classes
$this->once( 'rest_api_init', array( $this, 'init_routes' ), 0 );

$this->prepare_read_write_fields();

add_filter( 'is_protected_meta', array( $this, 'is_protected_meta' ), 10, 3 );
}

public function init_routes() {
global $wp_rest_server;

$boxes_controller = new CMB2_REST_Controller_Boxes( $wp_rest_server );
$boxes_controller->register_routes();

$fields_controller = new CMB2_REST_Controller_Fields( $wp_rest_server );
$fields_controller->register_routes();
}

public static function register_fields() {

$types = array();
Expand Down
230 changes: 41 additions & 189 deletions includes/CMB2_REST_Endpoints.php → includes/CMB2_REST_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,21 @@
* @license GPL-2.0+
* @link http://webdevstudios.com
*/
class CMB2_REST_Endpoints extends WP_REST_Controller {
abstract class CMB2_REST_Controller extends WP_REST_Controller {

/**
* The current CMB2 REST endpoint version
* @var string
* The current request object
* @var WP_REST_Request $request
* @since 2.2.0
*/
public $version = '1';
public $request;

/**
* The CMB2 REST namespace
* @var string
* The current server object
* @var WP_REST_Server $server
* @since 2.2.0
*/
public $namespace = 'cmb2/v';

/**
* The current request object
* @var WP_REST_Request $request
* @since 2.2.0
*/
public $request;
public $server;

/**
* Box object id
Expand All @@ -55,134 +48,8 @@ class CMB2_REST_Endpoints extends WP_REST_Controller {
* Constructor
* @since 2.2.0
*/
public function __construct() {
$this->namespace .= $this->version;
}

/**
* Register the routes for the objects of the controller.
*
* @since 2.2.0
*/
public function register_routes() {

// Returns all boxes data.
register_rest_route( $this->namespace, '/boxes/', array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_all_boxes' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
),
'schema' => array( $this, 'get_item_schema' ),
) );

// Returns specific box's data.
register_rest_route( $this->namespace, '/boxes/(?P<cmb_id>[\w-]+)', array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_box' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
),
'schema' => array( $this, 'get_item_schema' ),
) );

// Returns specific box's fields.
register_rest_route( $this->namespace, '/boxes/(?P<cmb_id>[\w-]+)/fields/', array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_box_fields' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
),
'schema' => array( $this, 'get_item_schema' ),
) );

// Returns specific field data.
register_rest_route( $this->namespace, '/boxes/(?P<cmb_id>[\w-]+)/fields/(?P<field_id>[\w-]+)', array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_field' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
),
'schema' => array( $this, 'get_item_schema' ),
) );

}

/**
* Get all public fields
*
* @since 2.2.0
*
* @param WP_REST_Request $request The API request object.
* @return array
*/
public function get_all_boxes( $request ) {
$this->initiate_request( $request );

$boxes = CMB2_Boxes::get_by_property( 'show_in_rest', false );

if ( empty( $boxes ) ) {
return $this->prepare_item( array( 'error' => __( 'No boxes found.', 'cmb2' ) ), $this->request );
}

$boxes_data = array();
// Loop boxes and get specific field.
foreach ( $boxes as $key => $cmb ) {
$boxes_data[ $cmb->cmb_id ] = $this->get_rest_box( $cmb );
}

return $this->prepare_item( $boxes_data );
}

/**
* Get all public fields
*
* @since 2.2.0
*
* @param WP_REST_Request $request The API request object.
* @return array
*/
public function get_box( $request ) {
$this->initiate_request( $request );

$cmb_id = $this->request->get_param( 'cmb_id' );

if ( $cmb_id && ( $cmb = cmb2_get_metabox( $cmb_id, $this->object_id, $this->object_type ) ) ) {
return $this->prepare_item( $this->get_rest_box( $cmb ) );
}

return $this->prepare_item( array( 'error' => __( 'No box found by that id.', 'cmb2' ) ) );
}

/**
* Get all box fields
*
* @since 2.2.0
*
* @param WP_REST_Request $request The API request object.
* @return array
*/
public function get_box_fields( $request ) {
$this->initiate_request( $request );

$cmb_id = $this->request->get_param( 'cmb_id' );

if ( $cmb_id && ( $cmb = cmb2_get_metabox( $cmb_id, $this->object_id, $this->object_type ) ) ) {
$fields = array();
foreach ( $cmb->prop( 'fields', array() ) as $field ) {
$field = $this->get_rest_field( $cmb, $field['id'] );

if ( ! is_wp_error( $field ) ) {
$fields[ $field['id'] ] = $field;
} else {
$fields[ $field['id'] ] = array( 'error' => $field->get_error_message() );
}
}

return $this->prepare_item( $fields );
}

return $this->prepare_item( array( 'error' => __( 'No box found by that id.', 'cmb2' ) ) );
public function __construct( WP_REST_Server $wp_rest_server ) {
$this->server = $wp_rest_server;
}

/**
Expand All @@ -199,7 +66,7 @@ public function get_rest_box( $cmb ) {

$boxes_data = $cmb->meta_box;

if ( isset( $_GET['rendered'] ) ) {
if ( isset( $_REQUEST['rendered'] ) ) {
$boxes_data['form_open'] = $this->get_cb_results( array( $cmb, 'render_form_open' ) );
$boxes_data['form_close'] = $this->get_cb_results( array( $cmb, 'render_form_close' ) );

Expand All @@ -214,11 +81,12 @@ public function get_rest_box( $cmb ) {
}

// TODO: look into 'embed' parameter.
// http://demo.wp-api.org/wp-json/wp/v2/posts?_embed
unset( $boxes_data['fields'] );
// Handle callable properties.
unset( $boxes_data['show_on_cb'] );

$base = $this->namespace . '/boxes/' . $cmb->cmb_id;
$base = CMB2_REST::BASE . '/boxes/' . $cmb->cmb_id;
$boxbase = $base . '/' . $cmb->cmb_id;

$response = new WP_REST_Response( $boxes_data );
Expand All @@ -239,32 +107,6 @@ public function get_rest_box( $cmb ) {
return $boxes_data;
}

/**
* Get a specific field
*
* @since 2.2.0
*
* @param WP_REST_Request $request The API request object.
* @return array|WP_Error
*/
public function get_field( $request ) {
$this->initiate_request( $request );

$cmb = cmb2_get_metabox( $this->request->get_param( 'cmb_id' ), $this->object_id, $this->object_type );

if ( ! $cmb ) {
return $this->prepare_item( array( 'error' => __( 'No box found by that id.', 'cmb2' ) ) );
}

$field = $this->get_rest_field( $cmb, $this->request->get_param( 'field_id' ) );

if ( is_wp_error( $field ) ) {
return $this->prepare_item( array( 'error' => $field->get_error_message() ) );
}

return $this->prepare_item( $field );
}

/**
* Get a specific field
*
Expand All @@ -276,6 +118,9 @@ public function get_field( $request ) {
public function get_rest_field( $cmb, $field_id ) {

// TODO: more robust show_in_rest checking. use rest_read/rest_write properties.
// TODO: more robust show_in_rest checking. use rest_read/rest_write properties.
// TODO: more robust show_in_rest checking. use rest_read/rest_write properties.

if ( ! $cmb->prop( 'show_in_rest' ) ) {
return new WP_Error( 'cmb2_rest_error', __( "You don't have permission to view this field.", 'cmb2' ) );
}
Expand All @@ -292,6 +137,29 @@ public function get_rest_field( $cmb, $field_id ) {
// : in_array( $show_in_rest, array( 'read_and_write', 'read_only' ), true );


$field_data = $this->prepare_field_data( $field );

$base = CMB2_REST::BASE . '/boxes/' . $cmb->cmb_id;

$response = new WP_REST_Response( $field_data );
$response->add_links( array(
'self' => array(
'href' => rest_url( trailingslashit( $base ) . 'fields/' . $field->_id() ),
),
'collection' => array(
'href' => rest_url( trailingslashit( $base ) . 'fields/' ),
),
'box' => array(
'href' => rest_url( trailingslashit( $base ) ),
),
) );

$field_data['_links'] = $response->get_links();

return $field_data;
}

public function prepare_field_data( CMB2_Field $field ) {
$field_data = array();
$params_to_ignore = array( 'show_on_cb', 'show_in_rest', 'options' );
$params_to_rename = array(
Expand All @@ -301,7 +169,7 @@ public function get_rest_field( $cmb, $field_id ) {

// TODO: Use request get object
// Run this first so the js_dependencies arg is populated.
$rendered = isset( $_GET['rendered'] ) && ( $cb = $field->maybe_callback( 'render_row_cb' ) )
$rendered = isset( $_REQUEST['rendered'] ) && ( $cb = $field->maybe_callback( 'render_row_cb' ) )
// Ok, callback is good, let's run it.
? $this->get_cb_results( $cb, $field->args(), $field )
: false;
Expand Down Expand Up @@ -330,29 +198,12 @@ public function get_rest_field( $cmb, $field_id ) {
}
}

if ( isset( $_GET['rendered'] ) ) {
if ( isset( $_REQUEST['rendered'] ) ) {
$field_data['rendered'] = $rendered;
}

$field_data['value'] = $field->get_data();

$base = $this->namespace . '/boxes/' . $cmb->cmb_id;

$response = new WP_REST_Response( $field_data );
$response->add_links( array(
'self' => array(
'href' => rest_url( trailingslashit( $base ) . 'fields/' . $field->_id() ),
),
'collection' => array(
'href' => rest_url( trailingslashit( $base ) . 'fields/' ),
),
'box' => array(
'href' => rest_url( trailingslashit( $base ) ),
),
) );

$field_data['_links'] = $response->get_links();

return $field_data;
}

Expand Down Expand Up @@ -476,6 +327,7 @@ public function get_item_schema() {
),
),
);

return $this->add_additional_fields_schema( $schema );
}

Expand Down

0 comments on commit 07ffdef

Please sign in to comment.