/** * Hooks the filtering of the data being saved. */ function cmb2_conditionals_hook_data_to_save_filtering() { $cmb2_boxes = CMB2_Boxes::get_all(); foreach ($cmb2_boxes as $cmb_id => $cmb2_box) { add_action("cmb2_{$cmb2_box->object_type()}_process_fields_{$cmb_id}", 'cmb2_conditional_filter_data_to_save', CMB2_CONDITIONALS_PRIORITY, 2); } }
/** * Get all meta boxes that added for page options * @since 1.0.0 * @return array */ function get_option_boxes() { $boxes = CMB2_Boxes::get_all(); $options_boxes = array(); foreach ($boxes as $k => $mb) { $object = $mb->mb_object_type(); $is_true = false; if (is_array($object)) { if (in_array('options-page', $object)) { $is_true = true; } } else { if ($object == 'options-page') { $is_true = true; } } if ($is_true) { $is_true = false; if (isset($mb->meta_box['show_on'])) { if (is_string($mb->meta_box['show_on']['value'])) { $is_true = $mb->meta_box['show_on']['value'] == $this->key; } else { if (is_array($mb->meta_box['show_on']['value'])) { $is_true = in_array($this->key, $mb->meta_box['show_on']['value']); } } } } if ($is_true) { $options_boxes[$k] = $mb; } } return $options_boxes; }
/** * Get started */ function __construct($meta_box, $object_id = 0) { if (empty($meta_box['id'])) { wp_die(__('Metabox configuration is required to have an ID parameter', 'cmb2')); } $this->meta_box = wp_parse_args($meta_box, $this->mb_defaults); $this->object_id($object_id); $this->mb_object_type(); $this->cmb_id = $meta_box['id']; CMB2_Boxes::add($this); }
/** * Function to encapsulate the CMB2 bootstrap process. * @since 2.2.0 * @return void */ function cmb2_bootstrap() { if (is_admin()) { /** * Fires on the admin side when CMB2 is included/loaded. * * In most cases, this should be used to add metaboxes. See example-functions.php */ do_action('cmb2_admin_init'); } /** * Fires when CMB2 is included/loaded * * Can be used to add metaboxes if needed on the front-end or WP-API (or the front and backend). */ do_action('cmb2_init'); /** * For back-compat. Does the dirty-work of instantiating all the * CMB2 instances for the cmb2_meta_boxes filter * @since 2.0.2 */ $cmb_config_arrays = apply_filters('cmb2_meta_boxes', array()); foreach ((array) $cmb_config_arrays as $cmb_config) { new CMB2($cmb_config); } /** * Fires after all CMB2 instances are created */ do_action('cmb2_init_before_hookup'); /** * Get all created metaboxes, and instantiate CMB2_hookup * on metaboxes which require it. * @since 2.0.2 */ foreach (CMB2_Boxes::get_all() as $cmb) { if ($cmb->prop('hookup')) { $hookup = new CMB2_hookup($cmb); } } /** * Fires after CMB2 initiation process has been completed */ do_action('cmb2_after_init'); }
/** * Get started */ function __construct($meta_box, $object_id = 0) { if (empty($meta_box['id'])) { wp_die(__('Metabox configuration is required to have an ID parameter', 'cmb2')); } $this->meta_box = wp_parse_args($meta_box, $this->mb_defaults); $this->object_id($object_id); $this->mb_object_type(); $this->cmb_id = $meta_box['id']; CMB2_Boxes::add($this); /** * Hook during initiation of CMB2 object * * The dynamic portion of the hook name, $this->cmb_id, is this meta_box id. * * @param array $cmb This CMB2 object */ do_action("cmb2_init_{$this->cmb_id}", $this); }
public function start_customizer($wp_customize) { $customizer_objects = array(); $customizer_boxes = CMB2_Boxes::get_all(); foreach ($customizer_boxes as $type => $instance) { $customizer_objects[] = cmb2_get_metabox($type, 0, 'customizer'); } $field_type_mapping = array('title' => 'WP_Customize_Control', 'text' => 'WP_Customize_Control', 'text_small' => 'WP_Customize_Control', 'text_medium' => 'WP_Customize_Control', 'text_email' => 'WP_Customize_Control', 'text_url' => 'WP_Customize_Control', 'text_money' => 'WP_Customize_Control', 'colorpicker' => 'WP_Customize_Color_Control', 'file' => 'WP_Customize_Media_Control'); foreach ($customizer_objects as $index => $cmb) { /* Add Address Info to Customizer */ $customizer_id = $cmb->prop('id'); $wp_customize->add_section($customizer_id, array('title' => $cmb->prop('title'), 'priority' => $cmb->prop('priority'), 'capability' => 'edit_theme_options')); $fields = $cmb->prop('fields'); foreach ($fields as $field_type) { $type = $field_type['type']; $type_class = isset($field_type_mapping[$type]) ? $field_type_mapping[$type] : false; /* Street */ $wp_customize->add_setting($field_type['id'], array('type' => 'option')); $wp_customize->add_control(new $type_class($wp_customize, $field_type['id'], array('label' => $field_type['name'], 'section' => $customizer_id, 'settings' => $field_type['id'], 'priority' => 10))); } } }
/** * Retrieve a CMB2 instance by the metabox ID * @since 2.0.0 * @param mixed $meta_box Metabox ID or Metabox config array * @param int $object_id Object ID * @param string $object_type Type of object being saved. (e.g., post, user, comment, or options-page). * Defaults to metabox object type. * @return CMB2 object */ function cmb2_get_metabox($meta_box, $object_id = 0, $object_type = '') { if ($meta_box instanceof CMB2) { return $meta_box; } if (is_string($meta_box)) { $cmb = CMB2_Boxes::get($meta_box); } else { // See if we already have an instance of this metabox $cmb = CMB2_Boxes::get($meta_box['id']); // If not, we'll initate a new metabox $cmb = $cmb ? $cmb : new CMB2($meta_box, $object_id); } if ($cmb && $object_id) { $cmb->object_id($object_id); } if ($cmb && $object_type) { $cmb->object_type($object_type); } return $cmb; }
/** * Get registered CMB2 fields. * * @return array Multidimensional array of field data. Each array item contains 'id', 'label', * and 'metabox_id' keys. */ protected function get_cmb_fields() { $return = array(); if (!class_exists('CMB2_Boxes', false)) { return $return; } $boxes = CMB2_Boxes::get_all(); foreach ($boxes as $cmb) { // Secret override method to skip indexing a metabox's fields if ($cmb->prop('no_facetwp_index', false)) { continue; } /** * Filter to skip metaboxes with no default hookup. * * Typically "hookup" => false is used by metaboxes that are on option pages, or are dispalyed on the front * end. * * @since 1.0.0 * * @param bool $skip_false_hookup Whether to skip metaboxes with hookup => false. */ $skip_false_hookup = apply_filters('facetwp_cmb2_skip_false_hookup', true); if ($skip_false_hookup && false === $cmb->prop('hookup')) { continue; } $fields = $cmb->prop('fields', array()); foreach ($fields as $field) { /** * Filter to skip indexing hidden fields. * * @since 1.0.0 * * @param bool $skip_hidden_fields Whether to skip indexing hidden fields. */ $skip_hidden_fields = apply_filters('facetwp_cmb2_skip_hidden_fields', true); if ($skip_hidden_fields && 'hidden' == $field['type']) { continue; } $return[] = array('id' => $field['id'], 'label' => isset($field['name']) ? $field['name'] : $field['id'], 'metabox_id' => $cmb->cmb_id); } } return $return; }
public function test_boxes_get_all() { $this->assertContainsOnlyInstancesOf('CMB2', CMB2_Boxes::get_all()); }
* Should be used to to add metaboxes. See cmb-functions.php */ do_action('cmb2_init'); /** * For back-compat. Does the dirtywork of instantiatiating all the * CMB2 instances for the cmb2_meta_boxes filter * @since 2.0.2 */ $all_meta_boxes_config = apply_filters('cmb2_meta_boxes', array()); foreach ((array) $all_meta_boxes_config as $meta_box_config) { new CMB2($meta_box_config); } /** * Fires after all CMB2 instances are created */ do_action('cmb2_init_before_hookup'); /** * Get all created metaboxes, and instantiate CMB2_hookup * on metaboxes which require it. * @since 2.0.2 */ foreach (CMB2_Boxes::get_all() as $cmb) { if ($cmb->prop('hookup')) { $hookup = new CMB2_hookup($cmb); } } /** * Fires after CMB2 initiation process has been completed */ do_action('cmb2_after_init'); // End. That's it, folks! //
/** * You can remove an entire CMB2 metabox. But be polite. */ function yourprefix_remove_metabox() { // to remove a cmb metabox.. use wisely CMB2_Boxes::remove('_yourprefix_demo_metabox'); }
/** * Allows three methods of adding metaboxes: * * 1) Injected boxes are added to the boxes array * 2) Add additional boxes (or boxes if none were injected) the usual way within this function * 3) If array is still empty and getboxes === true, call CMB2_Boxes::get_all(); * * @param string $id @since 1.1.0 * * @return array|\CMB2[] * * @since 1.1.0 allow skipping of call to CMB2_Boxes::get_all(); * - ok to pass CMB2 metabox ID or CMB2 object; previously only objects allowed * @since 1.0.0 */ private function cmb2_metaboxes($id) { // add any injected metaboxes $boxes = self::$props[$id]['boxes']; // if boxes is not empty, check to see if they're CMB2 objects, or strings if (!empty($boxes)) { foreach ($boxes as $key => $box) { if (!is_object($box)) { $boxes[$key] = CMB2_Boxes::get($box); } } } // if $boxes is still empty and getboxes is true, try grabbing boxes from CMB2 $boxes = empty($boxes) && self::$props[$id]['getboxes'] === TRUE ? CMB2_Boxes::get_all() : $boxes; return $boxes; }
/** * Hook in the filtering of the data being saved. */ public function admin_init() { $cmb2_boxes = CMB2_Boxes::get_all(); foreach ($cmb2_boxes as $cmb_id => $cmb2_box) { add_action("cmb2_{$cmb2_box->object_type()}_process_fields_{$cmb_id}", array($this, 'filter_data_to_save'), self::PRIORITY, 2); } }
/** * Get started * @since 0.4.0 * @param array $meta_box Metabox config array * @param integer $object_id Optional object id */ public function __construct($meta_box, $object_id = 0) { if (empty($meta_box['id'])) { wp_die(__('Metabox configuration is required to have an ID parameter', 'cmb2')); } $this->meta_box = wp_parse_args($meta_box, $this->mb_defaults); $this->object_id($object_id); $this->mb_object_type(); $this->cmb_id = $meta_box['id']; // Add custom classes to metabox $post_type_object = $this->meta_box['object_types']; foreach ($post_type_object as $page) { add_filter("postbox_classes_{$page}_{$this->meta_box['id']}", array($this, 'add_class_to_metabox')); } CMB2_Boxes::add($this); /** * Hook during initiation of CMB2 object * * The dynamic portion of the hook name, $this->cmb_id, is this meta_box id. * * @param array $cmb This CMB2 object */ do_action("cmb2_init_{$this->cmb_id}", $this); }
/** * Gets all CMB2 custom metaboxes associated with a post type. * * Loops through all custom metabox fields registered with CMB2 and * looks through them for matches on the given post type ID. Returns a single * array of all boxes associated with the post type. * * @access private * * @see cmb2_meta_boxes * * @param string $slug a custom post type ID. * @return array Array of fields. */ private function get_cmb2_metaboxes($slug) { $fields = array(); // Get all metaboxes from CMB2 library $all_metaboxes = apply_filters('cmb2_meta_boxes', array()); // Loop through all possible sets of metaboxes added the old way foreach ($all_metaboxes as $metabox_array) { // If the custom post type ID matches this set of fields, set & stop if (in_array($slug, $metabox_array['object_types'])) { // If this is the first group of fields, simply set the value // Else, merge this group with the previous one if (empty($fields)) { $fields = $metabox_array['fields']; } else { $fields = array_merge($fields, $metabox_array['fields']); } } } // Loop through all metaboxes added the new way foreach (\CMB2_Boxes::get_all() as $cmb) { // Create the default $match = false; // Establish correct cmb types if (is_string($cmb->meta_box['object_types'])) { if ($cmb->meta_box['object_types'] == $slug) { $match = true; } } else { if (in_array($slug, $cmb->meta_box['object_types'])) { $match = true; } } if ($match !== true) { continue; } if (empty($fields)) { $fields = $cmb->meta_box['fields']; } else { $fields = array_merge($fields, $cmb->meta_box['fields']); } } return $fields; }