PHP Class CMB2, Give

Author: WebDevStudios
Show file Open project: wordimpress/give Class Usage Examples

Public Properties

Property Type Description
$data_to_save array Array of key => value data for saving. Likely $_POST data.

Protected Properties

Property Type Description
$cmb_id string Current CMB2 instance ID
$fields array Metabox field objects
$generated_nonce string Array of key => value data for saving. Likely $_POST data.
$has_columns boolean Whether there are fields to be shown in columns. Set in CMB2::add_field().
$hidden_fields array An array of hidden fields to output at the end of the form
$mb_defaults array Metabox Defaults
$mb_object_type string Type of object registered for metabox. (e.g., post, user, or comment)
$meta_box array Metabox Config array
$object_id mixed Object ID for metabox meta retrieving/saving
$object_type string Type of object being saved. (e.g., post, user, or comment)
$updated array List of fields that are changed/updated on save

Public Methods

Method Description
__construct ( array $meta_box, integer $object_id ) Get started
__get ( string $field ) : mixed Magic getter for our object.
add_field ( array $field, integer $position ) : mixed Add a field to the metabox
add_group_field ( string $parent_field_id, array $field, integer $position ) : mixed Add a field to a group
add_hidden_field ( array $field_args, $field_group = null ) Add a hidden field to the list of hidden fields to be rendered later
after_save ( )
box_classes ( ) : string Defines the classes for the CMB2 form/wrap.
current_object_type ( ) : string Get the object type for the current page, based on the $pagenow global.
get_field ( string | array | CMB2_Field $field, CMB2_Field $field_group = null ) : CMB2_Field | false Get a field object
get_field_args ( mixed $field_id, mixed $field_args, mixed $sub_field_id, mixed $field_group ) : array Handles determining which type of arguments to pass to CMB2_Field
get_field_ids ( string $field_id, string $parent_field_id = '' ) : mixed Check if field ids match a field and return the index/field id
get_sanitized_values ( array $data_to_sanitize ) Returns array of sanitized field values (without saving them)
is_options_page_mb ( ) : boolean Determines if metabox is for an options page
mb_object_type ( ) : string Sets the $object_type based on metabox settings
nonce ( ) : string Generate a unique nonce for each registered meta_box
nonce_field ( ) : string Generate a unique nonce field for each registered meta_box
object_id ( integer $object_id ) : integer Get object id from global space if no id is provided
object_type ( $object_type = '' ) : string Returns the object type
pre_process ( )
process_field ( array $field_args ) Process and save a field
process_fields ( ) Process and save form fields
prop ( string $property, mixed $fallback = null ) : mixed Get metabox property and optionally set a fallback
remove_field ( string $field_id, string $parent_field_id = '' ) : boolean Remove a field from the metabox
render_field ( array $field_args ) : mixed Renders a field based on the field type
render_form_close ( integer $object_id, string $object_type = '' ) : void Outputs the closing form markup and runs corresponding hooks: 'cmb2_after_form' and "cmb2_after_{$object_type}_form_{$this->cmb_id}"
render_form_open ( integer $object_id, string $object_type = '' ) : void Outputs the opening form markup and runs corresponding hooks: 'cmb2_before_form' and "cmb2_before_{$object_type}_form_{$this->cmb_id}"
render_group ( array $args ) : CMB2_Field | null Render a repeatable group.
render_group_row ( CMB2_Field $field_group, string $remove_disabled ) Render a repeatable group row
render_hidden_fields ( ) Loop through and output hidden fields
save_fields ( integer $object_id, string $object_type = '', array $data_to_save = [] ) Loops through and saves field data
save_group ( array $args ) : mixed Save a repeatable group
save_group_field ( array $field_group ) : mixed Save a repeatable group
search_old_school_array ( string $field_id, array $fields ) : mixed When using the old array filter, it is unlikely field array indexes will be the field id
set_prop ( string $property, mixed $value ) : mixed Set metabox property.
should_show ( ) : boolean Determine whether this cmb object should show, based on the 'show_on_cb' callback.
show_form ( integer $object_id, string $object_type = '' ) Loops through and displays fields
update_field_property ( string $field_id, string $property, mixed $value, string $parent_field_id = '' ) : mixed Update or add a property to a field

Protected Methods

Method Description
_add_field_to_array ( array $field, &$fields, integer $position ) Add a field array to a fields array in desired position
add_fields ( array $fields, mixed $parent_field_id = null ) When fields are added in the old-school way, intitate them as they should be
get_default_args ( array $field_args, CMB2_Field $field_group = null ) : array Get default field arguments specific to this CMB2 object.
get_new_field ( array $field_args, CMB2_Field $field_group = null ) : CMB2_Field Get a new field object specific to this CMB2 object.

Method Details

__construct() public method

Get started
Since: 0.4.0
public __construct ( array $meta_box, integer $object_id )
$meta_box array Metabox config array
$object_id integer Optional object id

__get() public method

Magic getter for our object.
public __get ( string $field ) : mixed
$field string
return mixed

_add_field_to_array() protected method

Add a field array to a fields array in desired position
Since: 2.0.2
protected _add_field_to_array ( array $field, &$fields, integer $position )
$field array Metabox field config array
$position integer Optionally specify a position in the array to be inserted

add_field() public method

Add a field to the metabox
Since: 2.0.0
public add_field ( array $field, integer $position ) : mixed
$field array Metabox field config array
$position integer (optional) Position of metabox. 1 for first, etc
return mixed Field id or false

add_fields() protected method

When fields are added in the old-school way, intitate them as they should be
Since: 2.1.0
protected add_fields ( array $fields, mixed $parent_field_id = null )
$fields array Array of fields to add
$parent_field_id mixed Parent field id or null

add_group_field() public method

Add a field to a group
Since: 2.0.0
public add_group_field ( string $parent_field_id, array $field, integer $position ) : mixed
$parent_field_id string The field id of the group field to add the field
$field array Metabox field config array
$position integer (optional) Position of metabox. 1 for first, etc
return mixed Array of parent/field ids or false

add_hidden_field() public method

Add a hidden field to the list of hidden fields to be rendered later
Since: 2.0.0
public add_hidden_field ( array $field_args, $field_group = null )
$field_args array Array of field arguments to be passed to CMB2_Field

after_save() public method

public after_save ( )

box_classes() public method

Defines the classes for the CMB2 form/wrap.
Since: 2.0.0
public box_classes ( ) : string
return string Space concatenated list of classes

current_object_type() public method

Get the object type for the current page, based on the $pagenow global.
Since: 2.2.2
public current_object_type ( ) : string
return string Page object type name.

get_default_args() protected method

Get default field arguments specific to this CMB2 object.
Since: 2.2.0
protected get_default_args ( array $field_args, CMB2_Field $field_group = null ) : array
$field_args array Metabox field config array.
$field_group CMB2_Field (optional) CMB2_Field object (group parent)
return array Array of field arguments.

get_field() public method

Get a field object
Since: 2.0.3
public get_field ( string | array | CMB2_Field $field, CMB2_Field $field_group = null ) : CMB2_Field | false
$field string | array | CMB2_Field Metabox field id or field config array or CMB2_Field object
$field_group CMB2_Field (optional) CMB2_Field object (group parent)
return CMB2_Field | false CMB2_Field object (or false)

get_field_args() public method

Handles determining which type of arguments to pass to CMB2_Field
Since: 2.0.7
public get_field_args ( mixed $field_id, mixed $field_args, mixed $sub_field_id, mixed $field_group ) : array
$field_id mixed Field (or group field) ID
$field_args mixed Array of field arguments
$sub_field_id mixed Sub field ID (if field_group exists)
$field_group mixed If a sub-field, will be the parent group CMB2_Field object
return array Array of CMB2_Field arguments

get_field_ids() public method

Check if field ids match a field and return the index/field id
Since: 2.0.2
public get_field_ids ( string $field_id, string $parent_field_id = '' ) : mixed
$field_id string Field id
$parent_field_id string (optional) Parent field id
return mixed Array of field/parent ids, or false

get_new_field() protected method

Get a new field object specific to this CMB2 object.
Since: 2.2.0
protected get_new_field ( array $field_args, CMB2_Field $field_group = null ) : CMB2_Field
$field_args array Metabox field config array.
$field_group CMB2_Field (optional) CMB2_Field object (group parent)
return CMB2_Field CMB2_Field object

get_sanitized_values() public method

Returns array of sanitized field values (without saving them)
Since: 2.0.3
public get_sanitized_values ( array $data_to_sanitize )
$data_to_sanitize array Array of field_id => value data for sanitizing (likely $_POST data).

is_options_page_mb() public method

Determines if metabox is for an options page
Since: 1.0.1
public is_options_page_mb ( ) : boolean
return boolean True/False

mb_object_type() public method

Sets the $object_type based on metabox settings
Since: 1.0.0
public mb_object_type ( ) : string
return string Object type

nonce() public method

Generate a unique nonce for each registered meta_box
Since: 2.0.0
public nonce ( ) : string
return string unique nonce string

nonce_field() public method

Generate a unique nonce field for each registered meta_box
Since: 2.0.0
public nonce_field ( ) : string
return string unique nonce hidden input

object_id() public method

Get object id from global space if no id is provided
Since: 1.0.0
public object_id ( integer $object_id ) : integer
$object_id integer Object ID
return integer $object_id Object ID

object_type() public method

Returns the object type
Since: 1.0.0
public object_type ( $object_type = '' ) : string
return string Object type

pre_process() public method

public pre_process ( )

process_field() public method

Process and save a field
Since: 2.0.0
public process_field ( array $field_args )
$field_args array Array of field arguments

process_fields() public method

Process and save form fields
Since: 2.0.0
public process_fields ( )

prop() public method

Get metabox property and optionally set a fallback
Since: 2.0.0
public prop ( string $property, mixed $fallback = null ) : mixed
$property string Metabox config property to retrieve
$fallback mixed Fallback value to set if no value found
return mixed Metabox config property value or false

remove_field() public method

Remove a field from the metabox
Since: 2.0.0
public remove_field ( string $field_id, string $parent_field_id = '' ) : boolean
$field_id string The field id of the field to remove
$parent_field_id string (optional) The field id of the group field to remove field from
return boolean True if field was removed

render_field() public method

Renders a field based on the field type
Since: 2.2.0
public render_field ( array $field_args ) : mixed
$field_args array A field configuration array.
return mixed CMB2_Field object if successful.

render_form_close() public method

Outputs the closing form markup and runs corresponding hooks: 'cmb2_after_form' and "cmb2_after_{$object_type}_form_{$this->cmb_id}"
Since: 2.2.0
public render_form_close ( integer $object_id, string $object_type = '' ) : void
$object_id integer Object ID
$object_type string Object type
return void

render_form_open() public method

Outputs the opening form markup and runs corresponding hooks: 'cmb2_before_form' and "cmb2_before_{$object_type}_form_{$this->cmb_id}"
Since: 2.2.0
public render_form_open ( integer $object_id, string $object_type = '' ) : void
$object_id integer Object ID
$object_type string Object type
return void

render_group() public method

Render a repeatable group.
public render_group ( array $args ) : CMB2_Field | null
$args array Array of field arguments for a group field parent.
return CMB2_Field | null Group field object.

render_group_row() public method

Render a repeatable group row
Since: 1.0.2
public render_group_row ( CMB2_Field $field_group, string $remove_disabled )
$field_group CMB2_Field CMB2_Field group field object
$remove_disabled string Attribute string to disable the remove button

render_hidden_fields() public method

Loop through and output hidden fields
Since: 2.0.0

save_fields() public method

Loops through and saves field data
Since: 1.0.0
public save_fields ( integer $object_id, string $object_type = '', array $data_to_save = [] )
$object_id integer Object ID
$object_type string Type of object being saved. (e.g., post, user, or comment)
$data_to_save array Array of key => value data for saving. Likely $_POST data.

save_group() public method

Save a repeatable group
Since: 1.x.x
public save_group ( array $args ) : mixed
$args array Field arguments array
return mixed Return of CMB2_Field::update_data()

save_group_field() public method

Save a repeatable group
Since: 1.x.x
public save_group_field ( array $field_group ) : mixed
$field_group array CMB2_Field group field object
return mixed Return of CMB2_Field::update_data()

search_old_school_array() public method

When using the old array filter, it is unlikely field array indexes will be the field id
Since: 2.0.2
public search_old_school_array ( string $field_id, array $fields ) : mixed
$field_id string The field id
$fields array Array of fields to search
return mixed Field index or false

set_prop() public method

Set metabox property.
Since: 2.2.2
public set_prop ( string $property, mixed $value ) : mixed
$property string Metabox config property to retrieve
$value mixed Value to set if no value found
return mixed Metabox config property value or false

should_show() public method

Determine whether this cmb object should show, based on the 'show_on_cb' callback.
Since: 2.0.9
public should_show ( ) : boolean
return boolean Whether this cmb should be shown.

show_form() public method

Loops through and displays fields
Since: 1.0.0
public show_form ( integer $object_id, string $object_type = '' )
$object_id integer Object ID
$object_type string Type of object being saved. (e.g., post, user, or comment)

update_field_property() public method

Update or add a property to a field
Since: 2.0.0
public update_field_property ( string $field_id, string $property, mixed $value, string $parent_field_id = '' ) : mixed
$field_id string Field id
$property string Field property to set/update
$value mixed Value to set the field property
$parent_field_id string (optional) The field id of the group field to remove field from
return mixed Field id. Strict compare to false, as success can return a falsey value (like 0)

Property Details

$cmb_id protected property

Current CMB2 instance ID
Since: 2.0.0
protected string $cmb_id
return string

$data_to_save public property

Array of key => value data for saving. Likely $_POST data.
Since: 2.0.0
public array $data_to_save
return array

$fields protected property

Metabox field objects
Since: 2.0.3
protected array $fields
return array

$generated_nonce protected property

Array of key => value data for saving. Likely $_POST data.
Since: 2.0.0
protected string $generated_nonce
return string

$has_columns protected property

Whether there are fields to be shown in columns. Set in CMB2::add_field().
Since: 2.2.2
protected bool $has_columns
return boolean

$hidden_fields protected property

An array of hidden fields to output at the end of the form
Since: 2.0.0
protected array $hidden_fields
return array

$mb_defaults protected property

Metabox Defaults
Since: 1.0.1
protected array $mb_defaults
return array

$mb_object_type protected property

Type of object registered for metabox. (e.g., post, user, or comment)
Since: 1.0.0
protected string $mb_object_type
return string

$meta_box protected property

Metabox Config array
Since: 0.9.0
protected array $meta_box
return array

$object_id protected property

Object ID for metabox meta retrieving/saving
Since: 1.0.0
protected mixed $object_id
return mixed

$object_type protected property

Type of object being saved. (e.g., post, user, or comment)
Since: 1.0.0
protected string $object_type
return string

$updated protected property

List of fields that are changed/updated on save
Since: 1.1.0
protected array $updated
return array