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_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
  • Test_CMB2_Utils_WIN

Functions

  • _tests_cmb2_manually_load_plugin
  • cmb2_ajax
  • cmb2_autoload_classes
  • cmb2_bootstrap
  • cmb2_dir
  • cmb2_do_oembed
  • cmb2_get_field
  • cmb2_get_field_value
  • cmb2_get_metabox
  • cmb2_get_metabox_form
  • cmb2_get_metabox_sanitized_values
  • cmb2_get_oembed
  • cmb2_get_option
  • cmb2_metabox_form
  • cmb2_options
  • cmb2_print_metabox_form
  • cmb2_update_option
  • cmb2_utils
  • date_create_from_format
  • date_timestamp_get
  • new_cmb2_box

Hooks

  • Hook Reference

Class CMB2_REST

Handles hooking CMB2 objects/fields into the WordPres REST API which can allow fields to be read and/or updated.

CMB2_Hookup_Base
Extended by CMB2_REST

Direct known subclasses

Test_CMB2_REST_Object

Package: CMB2
Category: WordPress_Plugin
License: GPL-2.0+
Author: CMB2 team
Since: 2.2.3
Link: https://cmb2.io
Located at includes/rest-api/CMB2_REST.php

Methods summary

public static CMB2_Hookup_Base
# maybe_init_and_hookup( CMB2 $cmb )

A functionalized constructor, used for the hookup action callbacks.

A functionalized constructor, used for the hookup action callbacks.

Parameters

$cmb
The CMB2 object to hookup

Returns

CMB2_Hookup_Base
$hookup The hookup object.

Throws

Exception
Failed implementation.

Since

2.2.6

Overrides

CMB2_Hookup_Base::maybe_init_and_hookup
public
# __construct( CMB2 $cmb )

Constructor

Constructor

Parameters

$cmb
The CMB2 object to be registered for the API.

Since

2.2.3

Overrides

CMB2_Hookup_Base::__construct
public
# universal_hooks( )

Hooks to register on frontend and backend.

Hooks to register on frontend and backend.

Since

2.2.3
public static
# init_routes( )

Initiate the CMB2 Boxes and Fields routes

Initiate the CMB2 Boxes and Fields routes

Since

2.2.3
public static
# register_cmb2_fields( )

Loop through REST boxes and call register_rest_field for each object type.

Loop through REST boxes and call register_rest_field for each object type.

Since

2.2.3
protected static
# register_rest_field( string|array $object_types, string $object_type )

Wrapper for register_rest_field.

Wrapper for register_rest_field.

Parameters

$object_types

Object(s) the field is being registered to, "post"|"term"|"comment" etc.

$object_type
$object_types Canonical object type for callbacks.

Since

2.2.3
protected
# declare_read_edit_fields( )

Setup readable and editable fields.

Setup readable and editable fields.

Since

2.2.3
protected boolean
# can_read( boolean $show_in_rest )

Determines if a field is readable based on it's show_in_rest value and the box's show_in_rest value.

Determines if a field is readable based on it's show_in_rest value and the box's show_in_rest value.

Parameters

$show_in_rest
Field's show_in_rest value. Default null.

Returns

boolean
Whether field is readable.

Since

2.2.3
protected boolean
# can_edit( boolean $show_in_rest )

Determines if a field is editable based on it's show_in_rest value and the box's show_in_rest value.

Determines if a field is editable based on it's show_in_rest value and the box's show_in_rest value.

Parameters

$show_in_rest
Field's show_in_rest value. Default null.

Returns

boolean
Whether field is editable.

Since

2.2.3
public static mixed
# get_post_rest_values( array $object, string $field_name, WP_REST_Request $request, string $object_type )

Handler for getting post custom field data.

Handler for getting post custom field data.

Parameters

$object
The object data from the response
$field_name
Name of field
$request
Current request
$object_type
The request object type

Returns

mixed

Since

2.2.3
public static mixed
# get_user_rest_values( array $object, string $field_name, WP_REST_Request $request, string $object_type )

Handler for getting user custom field data.

Handler for getting user custom field data.

Parameters

$object
The object data from the response
$field_name
Name of field
$request
Current request
$object_type
The request object type

Returns

mixed

Since

2.2.3
public static mixed
# get_comment_rest_values( array $object, string $field_name, WP_REST_Request $request, string $object_type )

Handler for getting comment custom field data.

Handler for getting comment custom field data.

Parameters

$object
The object data from the response
$field_name
Name of field
$request
Current request
$object_type
The request object type

Returns

mixed

Since

2.2.3
public static mixed
# get_term_rest_values( array $object, string $field_name, WP_REST_Request $request, string $object_type )

Handler for getting term custom field data.

Handler for getting term custom field data.

Parameters

$object
The object data from the response
$field_name
Name of field
$request
Current request
$object_type
The request object type

Returns

mixed

Since

2.2.3
protected static mixed
# get_rest_values( array $object, WP_REST_Request $request, string $object_type, string $main_object_type = 'post' )

Handler for getting custom field data.

Handler for getting custom field data.

Parameters

$object
The object data from the response
$request
Current request
$object_type
The request object type
$main_object_type
The cmb main object type

Returns

mixed

Since

2.2.3
public static array
# get_box_rest_values( CMB2_REST $rest_box, integer $object_id = 0, string $main_object_type = 'post' )

Get box rest values.

Get box rest values.

Parameters

$rest_box
The CMB2_REST object.
$object_id
The object ID.
$main_object_type
The object type (post, user, term, etc)

Returns

array
Array of box rest values.

Since

2.7.0
public static boolean|integer
# update_post_rest_values( mixed $values, object $object, string $field_name, WP_REST_Request $request, string $object_type )

Handler for updating post custom field data.

Handler for updating post custom field data.

Parameters

$values
The value of the field
$object
The object from the response
$field_name
Name of field
$request
Current request
$object_type
The request object type

Returns

boolean|integer

Since

2.2.3
public static boolean|integer
# update_user_rest_values( mixed $values, object $object, string $field_name, WP_REST_Request $request, string $object_type )

Handler for updating user custom field data.

Handler for updating user custom field data.

Parameters

$values
The value of the field
$object
The object from the response
$field_name
Name of field
$request
Current request
$object_type
The request object type

Returns

boolean|integer

Since

2.2.3
public static boolean|integer
# update_comment_rest_values( mixed $values, object $object, string $field_name, WP_REST_Request $request, string $object_type )

Handler for updating comment custom field data.

Handler for updating comment custom field data.

Parameters

$values
The value of the field
$object
The object from the response
$field_name
Name of field
$request
Current request
$object_type
The request object type

Returns

boolean|integer

Since

2.2.3
public static boolean|integer
# update_term_rest_values( mixed $values, object $object, string $field_name, WP_REST_Request $request, string $object_type )

Handler for updating term custom field data.

Handler for updating term custom field data.

Parameters

$values
The value of the field
$object
The object from the response
$field_name
Name of field
$request
Current request
$object_type
The request object type

Returns

boolean|integer

Since

2.2.3
protected static boolean|integer
# update_rest_values( mixed $values, object $object, WP_REST_Request $request, string $object_type, string $main_object_type = 'post' )

Handler for updating custom field data.

Handler for updating custom field data.

Parameters

$values
The value of the field
$object
The object from the response
$request
Current request
$object_type
The request object type
$main_object_type
The cmb main object type

Returns

boolean|integer

Since

2.2.3
public static mixed|boolean
# santize_box_rest_values( array $values, CMB2_REST $rest_box, integer $object_id = 0, string $main_object_type = 'post' )

Updates box rest values.

Updates box rest values.

Parameters

$values
Array of values.
$rest_box
The CMB2_REST object.
$object_id
The object ID.
$main_object_type
The object type (post, user, term, etc)

Returns

mixed|boolean
Array of updated statuses if successful.

Since

2.7.0
public array
# sanitize_box_values( array $values )

Loop through box fields and sanitize the values.

Loop through box fields and sanitize the values.

Parameters

$values
Array of values being provided.

Returns

array
Array of updated/sanitized values.

Since

2.2.o
protected mixed
# sanitize_field_value( array $values, string $field_id )

Handles returning a sanitized field value.

Handles returning a sanitized field value.

Parameters

$values
Array of values being provided.
$field_id
The id of the field to update.

Returns

mixed
The results of saving/sanitizing a field value.

Since

2.2.3
protected mixed
# sanitize_group_value( array $values, CMB2_Field $field )

Handles returning a sanitized group field value.

Handles returning a sanitized group field value.

Parameters

$values
Array of values being provided.
$field
CMB2_Field object.

Returns

mixed
The results of saving/sanitizing the group field value.

Since

2.2.3
public
# is_protected_meta( boolean $protected, string $meta_key, string $meta_type )

Filter whether a meta key is protected.

Filter whether a meta key is protected.

Parameters

$protected
Whether the key is protected. Default false.
$meta_key
Meta key.
$meta_type
Meta type.

Since

2.2.3
public static integer
# get_object_id( mixed $object, string $object_type = 'post' )

Get the object ID for the given object/type.

Get the object ID for the given object/type.

Parameters

$object
The object to get the ID for.
$object_type
The object type we are looking for.

Returns

integer
The object ID if found.

Since

2.2.3
public mixed
# field_can_read( string|CMB2_Field $field_id, boolean $return_object = false )

Checks if a given field can be read.

Checks if a given field can be read.

Parameters

$field_id
Field ID or CMB2_Field object.
$return_object
Whether to return the Field object.

Returns

mixed
False if field can't be read or true|CMB2_Field object.

Since

2.2.3
public mixed
# field_can_edit( string|CMB2_Field $field_id, boolean $return_object = false )

Checks if a given field can be edited.

Checks if a given field can be edited.

Parameters

$field_id
Field ID or CMB2_Field object.
$return_object
Whether to return the Field object.

Returns

mixed
False if field can't be edited or true|CMB2_Field object.

Since

2.2.3
protected mixed
# field_can( string $type, string|CMB2_Field $field_id, boolean $return_object = false )

Checks if a given field can be read or edited.

Checks if a given field can be read or edited.

Parameters

$type
Whether we are checking for read or edit fields.
$field_id
Field ID or CMB2_Field object.
$return_object
Whether to return the Field object.

Returns

mixed
False if field can't be read or edited or true|CMB2_Field object.

Since

2.2.3
public static CMB2_REST|false
# get_rest_box( string $cmb_id )

Get a CMB2_REST instance object from the registry by a CMB2 id.

Get a CMB2_REST instance object from the registry by a CMB2 id.

Parameters

$cmb_id
CMB2 config id

Returns

CMB2_REST|false
The CMB2_REST object or false.

Since

2.2.3
public static
# remove( string $cmb_id )

Remove a CMB2_REST instance object from the registry.

Remove a CMB2_REST instance object from the registry.

Parameters

$cmb_id
A CMB2 instance id.

Since

2.2.3
public static CMB2[]
# get_all( )

Retrieve all CMB2_REST instances from the registry.

Retrieve all CMB2_REST instances from the registry.

Returns

CMB2[]
Array of all registered CMB2_REST instances.

Since

2.2.3
public static boolean
# is_readable( mixed $value )

Checks if given value is readable.

Checks if given value is readable.

Value is considered readable if it is not empty and if it does not match the editable blacklist.

Parameters

$value
Value to check.

Returns

boolean
Whether value is considered readable.

Since

2.2.3
public static boolean
# is_editable( mixed $value )

Checks if given value is editable.

Checks if given value is editable.

Value is considered editable if matches the editable whitelist.

Parameters

$value
Value to check.

Returns

boolean
Whether value is considered editable.

Since

2.2.3
public mixed
# __get( string $field )

Magic getter for our object.

Magic getter for our object.

Parameters

$field

Returns

mixed

Throws

Exception
Throws an exception if the field is invalid.

Overrides

CMB2_Hookup_Base::__get

Methods inherited from CMB2_Hookup_Base

once()

Constants summary

string VERSION

The current CMB2 REST endpoint version

The current CMB2 REST endpoint version

Since

2.2.3
# '1'
string NAME_SPACE

The CMB2 REST base namespace (v should always be followed by $version)

The CMB2 REST base namespace (v should always be followed by $version)

Since

2.2.3
# 'cmb2/v1'

Properties summary

public CMB2 $cmb

object

object

Since

2.2.3
#
protected static CMB2_REST[] $boxes

objects

objects

Since

2.2.3
# array()
protected static array $type_boxes

Array of cmb ids for each type.

Array of cmb ids for each type.

Since

2.2.3
# array( 'post' => array(), 'user' => array(), 'comment' => array(), 'term' => array(), )
protected CMB2_Field[] $read_fields

Array of readable field objects.

Array of readable field objects.

Since

2.2.3
# array()
protected CMB2_Field[] $edit_fields

Array of editable field objects.

Array of editable field objects.

Since

2.2.3
# array()
protected boolean $rest_read

Whether CMB2 object is readable via the rest api.

Whether CMB2 object is readable via the rest api.

# false
protected boolean $rest_edit

Whether CMB2 object is editable via the rest api.

Whether CMB2 object is editable via the rest api.

# false

Properties inherited from CMB2_Hookup_Base

$object_type

Magic properties inherited from CMB2_Hookup_Base

$cmb, $object_type

CMB2 Documentation API documentation generated by ApiGen