CMB2 Documentation
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  • Hooks
  • Download Docs
  • Github

Packages

  • CMB2
  • Demo
    • CMB2
  • None
  • Tests
    • CMB2

Classes

  • CMB2
  • CMB2_Ajax
  • CMB2_Base
  • CMB2_Bootstrap_2101
  • CMB2_Boxes
  • CMB2_Display_Checkbox
  • CMB2_Display_Colorpicker
  • CMB2_Display_File
  • CMB2_Display_File_List
  • CMB2_Display_Multicheck
  • CMB2_Display_oEmbed
  • CMB2_Display_Select
  • CMB2_Display_Taxonomy_Multicheck
  • CMB2_Display_Taxonomy_Radio
  • CMB2_Display_Text_Date
  • CMB2_Display_Text_Date_Timezone
  • CMB2_Display_Text_Money
  • CMB2_Display_Text_Time
  • CMB2_Display_Text_Url
  • CMB2_Display_Textarea
  • CMB2_Display_Textarea_Code
  • CMB2_Field
  • CMB2_Field_Display
  • CMB2_Hookup
  • CMB2_Hookup_Base
  • CMB2_Integration_Box
  • CMB2_JS
  • CMB2_Option
  • CMB2_Options
  • CMB2_Options_Hookup
  • CMB2_REST
  • CMB2_REST_Controller
  • CMB2_REST_Controller_Boxes
  • CMB2_REST_Controller_Fields
  • CMB2_Sanitize
  • CMB2_Show_Filters
  • CMB2_Type_Base
  • CMB2_Type_Checkbox
  • CMB2_Type_Colorpicker
  • CMB2_Type_Counter_Base
  • CMB2_Type_File
  • CMB2_Type_File_Base
  • CMB2_Type_File_List
  • CMB2_Type_Multi_Base
  • CMB2_Type_Multicheck
  • CMB2_Type_Oembed
  • CMB2_Type_Picker_Base
  • CMB2_Type_Radio
  • CMB2_Type_Select
  • CMB2_Type_Select_Timezone
  • CMB2_Type_Taxonomy_Base
  • CMB2_Type_Taxonomy_Multicheck
  • CMB2_Type_Taxonomy_Multicheck_Hierarchical
  • CMB2_Type_Taxonomy_Radio
  • CMB2_Type_Taxonomy_Radio_Hierarchical
  • CMB2_Type_Taxonomy_Select
  • CMB2_Type_Taxonomy_Select_Hierarchical
  • CMB2_Type_Text
  • CMB2_Type_Text_Date
  • CMB2_Type_Text_Datetime_Timestamp
  • CMB2_Type_Text_Datetime_Timestamp_Timezone
  • CMB2_Type_Text_Time
  • CMB2_Type_Textarea
  • CMB2_Type_Textarea_Code
  • CMB2_Type_Title
  • CMB2_Type_Wysiwyg
  • CMB2_Types
  • CMB2_Utils

Hooks

  • Hook Reference
  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 
<?php
/**
 * CMB2 objects/boxes endpoint for WordPres REST API.
 * Allows access to boxes configuration data.
 *
 * @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
 */
class CMB2_REST_Controller_Boxes extends CMB2_REST_Controller {

    /**
     * The base of this controller's route.
     *
     * @var string
     */
    protected $rest_base = 'boxes';

    /**
     * The combined $namespace and $rest_base for these routes.
     *
     * @var string
     */
    protected $namespace_base = '';

    /**
     * Constructor
     *
     * @since 2.2.3
     */
    public function __construct( WP_REST_Server $wp_rest_server ) {
        $this->namespace_base = $this->namespace . '/' . $this->rest_base;
        parent::__construct( $wp_rest_server );
    }

    /**
     * Register the routes for the objects of the controller.
     *
     * @since 2.2.3
     */
    public function register_routes() {
        $args = array(
            '_embed' => array(
                'description' => __( 'Includes the registered fields for the box in the response.', 'cmb2' ),
            ),
        );

        // @todo determine what belongs in the context param.
        // $args['context'] = $this->get_context_param();
        // $args['context']['required'] = false;
        // $args['context']['default'] = 'view';
        // $args['context']['enum'] = array( 'view', 'embed' );
        // Returns all boxes data.
        register_rest_route( $this->namespace, '/' . $this->rest_base, array(
            array(
                'methods'             => WP_REST_Server::READABLE,
                'permission_callback' => array( $this, 'get_items_permissions_check' ),
                'callback'            => array( $this, 'get_items' ),
                'args'                => $args,
            ),
            'schema' => array( $this, 'get_item_schema' ),
        ) );

        $args['_rendered'] = array(
            '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' ),
        );

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

    /**
     * Check if a given request has access to get boxes.
     *
     * @since 2.2.3
     *
     * @param  WP_REST_Request $request Full data about the request.
     * @return WP_Error|boolean
     */
    public function get_items_permissions_check( $request ) {
        $this->initiate_request( $request, __FUNCTION__ );

        /**
         * By default, no special permissions needed.
         *
         * @since 2.2.3
         *
         * @param bool   $can_access Whether this CMB2 endpoint can be accessed.
         * @param object $controller This CMB2_REST_Controller object.
         */
        return apply_filters( 'cmb2_api_get_boxes_permissions_check', true, $this );
    }

    /**
     * Get all public CMB2 boxes.
     *
     * @since 2.2.3
     *
     * @param  WP_REST_Request $request Full data about the request.
     * @return WP_Error|WP_REST_Response
     */
    public function get_items( $request ) {
        $this->initiate_request( $request, 'boxes_read' );

        $boxes = CMB2_REST::get_all();
        if ( empty( $boxes ) ) {
            return new WP_Error( 'cmb2_rest_no_boxes', __( 'No boxes found.', 'cmb2' ), array(
                'status' => 403,
            ) );
        }

        $boxes_data = array();

        // Loop and prepare boxes data.
        foreach ( $boxes as $this->rest_box ) {
            if (
                // Make sure this box can be read
                $this->rest_box->rest_read
                // And make sure current user can view this box.
                && $this->get_item_permissions_check_filter( $this->request )
            ) {
                $boxes_data[] = $this->server->response_to_data(
                    $this->get_rest_box(),
                    isset( $this->request['_embed'] )
                );
            }
        }

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

    /**
     * Check if a given request has access to a box.
     * By default, no special permissions needed, but filtering return value.
     *
     * @since 2.2.3
     *
     * @param  WP_REST_Request $request Full details about the request.
     * @return WP_Error|boolean
     */
    public function get_item_permissions_check( $request ) {
        $this->initiate_rest_read_box( $request, 'box_read' );

        return $this->get_item_permissions_check_filter();
    }

    /**
     * Check by filter if a given request has access to a box.
     * By default, no special permissions needed, but filtering return value.
     *
     * @since 2.2.3
     *
     * @param  bool $can_access Whether the current request has access to view the box by default.
     * @return WP_Error|boolean
     */
    public function get_item_permissions_check_filter( $can_access = true ) {
        /**
         * By default, no special permissions needed.
         *
         * @since 2.2.3
         *
         * @param bool   $can_access Whether this CMB2 endpoint can be accessed.
         * @param object $controller This CMB2_REST_Controller object.
         */
        return $this->maybe_hook_callback_and_apply_filters( 'cmb2_api_get_box_permissions_check', $can_access );
    }

    /**
     * Get one CMB2 box from the collection.
     *
     * @since 2.2.3
     *
     * @param  WP_REST_Request $request Full data about the request.
     * @return WP_Error|WP_REST_Response
     */
    public function get_item( $request ) {
        $this->initiate_rest_read_box( $request, 'box_read' );

        if ( is_wp_error( $this->rest_box ) ) {
            return $this->rest_box;
        }

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

    /**
     * Get a CMB2 box prepared for REST
     *
     * @since 2.2.3
     *
     * @return array
     */
    public function get_rest_box() {
        $cmb = $this->rest_box->cmb;

        $boxes_data = $cmb->meta_box;

        if ( isset( $this->request['_rendered'] ) && $this->namespace_base !== ltrim( CMB2_REST_Controller::get_intial_route(), '/' ) ) {
            $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' ) );

            global $wp_scripts, $wp_styles;
            $before_css = $wp_styles->queue;
            $before_js = $wp_scripts->queue;

            CMB2_JS::enqueue();

            $boxes_data['js_dependencies'] = array_values( array_diff( $wp_scripts->queue, $before_js ) );
            $boxes_data['css_dependencies'] = array_values( array_diff( $wp_styles->queue, $before_css ) );
        }

        // 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'] );

        $response = rest_ensure_response( $boxes_data );

        $response->add_links( $this->prepare_links( $cmb ) );

        return $response;
    }

    /**
     * Return an array of contextual links for box/boxes.
     *
     * @since  2.2.3
     *
     * @param  CMB2_REST $cmb CMB2_REST object to build links from.
     *
     * @return array          Array of links
     */
    protected function prepare_links( $cmb ) {
        $boxbase      = $this->namespace_base . '/' . $cmb->cmb_id;
        $query_string = $this->get_query_string();

        return array(
            // Standard Link Relations -- http://v2.wp-api.org/extending/linking/
            'self' => array(
                'href' => rest_url( $boxbase . $query_string ),
            ),
            'collection' => array(
                'href' => rest_url( $this->namespace_base . $query_string ),
            ),
            // Custom Link Relations -- http://v2.wp-api.org/extending/linking/
            // TODO URL should document relationship.
            'https://cmb2.io/fields' => array(
                'href' => rest_url( trailingslashit( $boxbase ) . 'fields' . $query_string ),
                'embeddable' => true,
            ),
        );
    }

}
CMB2 Documentation API documentation generated by ApiGen