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
/**
 * Filters the data to remove those values which are not suppose to be enabled to edit according to the declared conditionals.
 */
function cmb2_conditional_filter_data_to_save(CMB2 $cmb2, $object_id)
{
    foreach ($cmb2->prop('fields') as $field_args) {
        if (!(array_key_exists('attributes', $field_args) && array_key_exists('data-conditional-id', $field_args['attributes']))) {
            continue;
        }
        $field_id = $field_args['id'];
        $conditional_id = $field_args['attributes']['data-conditional-id'];
        if (array_key_exists('data-conditional-value', $field_args['attributes'])) {
            $conditional_value = $field_args['attributes']['data-conditional-value'];
            $conditional_value = ($decoded_conditional_value = @json_decode($conditional_value)) ? $decoded_conditional_value : $conditional_value;
            if (is_array($conditional_value) && !in_array($cmb2->data_to_save[$conditional_id], $conditional_value)) {
                unset($cmb2->data_to_save[$field_id]);
                continue;
            }
            if (!is_array($conditional_value) && $cmb2->data_to_save[$conditional_id] != $conditional_value) {
                unset($cmb2->data_to_save[$field_id]);
                continue;
            }
        }
        if (!$cmb2->data_to_save[$conditional_id]) {
            unset($cmb2->data_to_save[$field_id]);
            continue;
        }
    }
}
 /**
  * Displays form markup
  * @since  0.1.3
  * @param  int  $term_id Term ID
  */
 public function display_form($term_id)
 {
     if (!class_exists('CMB2')) {
         return;
     }
     $this->do_override_filters($term_id);
     $object_id = $this->id($term_id);
     // Hard-code object ID
     $this->cmb->object_id($object_id);
     // Hard-code object type
     $this->cmb->object_type('options-page');
     // Enqueue JS/CSS
     if ($this->cmb->prop('cmb_styles')) {
         CMB2_hookup::enqueue_cmb_css();
     }
     if ($this->cmb->prop('enqueue_js')) {
         CMB2_hookup::enqueue_cmb_js();
     }
     // Add object id to the form for easy access
     printf('<input type="hidden" name="term_opt_name" value="%s">', $object_id);
     printf('<input type="hidden" name="object_id" value="%s">', $object_id);
     // Show cmb form
     $this->cmb->show_form();
 }
Exemplo n.º 3
0
 public function test_disable_save_fields()
 {
     $this->assertTrue($this->cmb->prop('save_fields'));
     $args = $this->metabox_array;
     $args['save_fields'] = false;
     $cmb = new CMB2($args);
     $this->assertFalse($cmb->prop('save_fields'));
 }
Exemplo n.º 4
0
/**
 * Helper function to provide directory path to CMB
 * @since  2.0.0
 * @param  string  $path Path to append
 * @return string        Directory with optional path appended
 */
function cmb2_dir($path = '')
{
    return trailingslashit(dirname(__FILE__)) . $path;
}
require_once cmb2_dir('includes/helper-functions.php');
$meta_boxes_config = apply_filters('cmb2_meta_boxes', array());
foreach ((array) $meta_boxes_config as $meta_box) {
    $cmb = new CMB2($meta_box);
    if ($cmb->prop('hookup')) {
        $hookup = new CMB2_hookup($cmb);
    }
}
/**
 * Create meta boxes
 */
class CMB2
{
    /**
     * Current field's ID
     * @var   string
     * @since 2.0.0
     */
    protected $cmb_id = '';
    /**
 /**
  * Determine whether the CMB2 object should be saved. All tests must be true, hence return false for
  * any failure.
  *
  * @param string $id @since 1.1.0
  * @param \CMB2 $cmb
  *
  * @return bool
  *
  * @since 1.1.0 static unclung
  * @since 1.0.3 made static method
  * @since 1.0.1
  */
 private function should_save($cmb, $id)
 {
     // was this flagged to save fields?
     if (!$cmb->prop('save_fields')) {
         return FALSE;
     }
     // are these values set?
     if (!isset($_POST['submit-cmb'], $_POST['object_id'], $_POST[$cmb->nonce()])) {
         return FALSE;
     }
     // does the nonce match?
     if (!wp_verify_nonce($_POST[$cmb->nonce()], $cmb->nonce())) {
         return FALSE;
     }
     // does the object_id equal the settings key?
     if (!$_POST['object_id'] == self::$props[$id]['key']) {
         return FALSE;
     }
     return TRUE;
 }
Exemplo n.º 6
0
 /**
  * Filter the data received from the form in order to remove those values
  * which are not suppose to be enabled to edit according to the declared conditionals.
  *
  * @param \CMB2 $cmb2      An instance of the CMB2 class.
  * @param int   $object_id The id of the object being saved, could post_id, comment_id, user_id.
  *
  * The potentially adjusted array is returned via reference $cmb2.
  */
 public function filter_data_to_save(CMB2 $cmb2, $object_id)
 {
     foreach ($cmb2->prop('fields') as $field_args) {
         if (!('group' === $field_args['type'] || array_key_exists('attributes', $field_args) && array_key_exists('data-conditional-id', $field_args['attributes']))) {
             continue;
         }
         if ('group' === $field_args['type']) {
             foreach ($field_args['fields'] as $group_field) {
                 if (!(array_key_exists('attributes', $group_field) && array_key_exists('data-conditional-id', $group_field['attributes']))) {
                     continue;
                 }
                 $field_id = $group_field['id'];
                 $conditional_id = $group_field['attributes']['data-conditional-id'];
                 $decoded_conditional_id = @json_decode($conditional_id);
                 if ($decoded_conditional_id) {
                     $conditional_id = $decoded_conditional_id;
                 }
                 if (is_array($conditional_id) && !empty($conditional_id) && !empty($cmb2->data_to_save[$conditional_id[0]])) {
                     foreach ($cmb2->data_to_save[$conditional_id[0]] as $key => $group_data) {
                         $cmb2->data_to_save[$conditional_id[0]][$key] = $this->filter_field_data_to_save($group_data, $field_id, $conditional_id[1], $group_field['attributes']);
                     }
                 }
                 continue;
             }
         } else {
             $field_id = $field_args['id'];
             $conditional_id = $field_args['attributes']['data-conditional-id'];
             $cmb2->data_to_save = $this->filter_field_data_to_save($cmb2->data_to_save, $field_id, $conditional_id, $field_args['attributes']);
         }
     }
 }