Esempio n. 1
0
 /**
  * Setup our class vars
  * @since 1.1.0
  * @param object $field A CMB field object
  * @param mixed  $value Field value
  */
 public function __construct($field, $value)
 {
     $this->field = $field;
     $this->value = $value;
     $this->object_id = cmb_Meta_Box::get_object_id();
     $this->object_type = cmb_Meta_Box::get_object_type();
 }
Esempio n. 2
0
 /**
  * Constructs our field object
  * @since 1.1.0
  * @param array $field_args  Field arguments
  * @param array $group_field (optional) Group field object
  */
 public function __construct($field_args, $group_field = null)
 {
     $this->object_id = cmb_Meta_Box::get_object_id();
     $this->object_type = cmb_Meta_Box::get_object_type();
     $this->group = !empty($group_field) ? $group_field : false;
     $this->args = $this->_set_field_defaults($field_args);
     // Allow an override for the field's value
     // (assuming no one would want to save 'cmb_no_override_val' as a value)
     $this->value = apply_filters('cmb_override_meta_value', 'cmb_no_override_val', $this->object_id, $this->args(), $this->object_type, $this);
     // If no override, get our meta
     $this->value = 'cmb_no_override_val' === $this->value ? $this->get_data() : $this->value;
 }
Esempio n. 3
0
 /**
  * Add metaboxes for an specific Page Template
  * @since  1.0.0
  * @param  bool  $display  To display or not
  * @param  array $meta_box Metabox config array
  * @return bool            Whether to display this metabox on the current page.
  */
 public static function check_page_template($display, $meta_box)
 {
     if (!isset($meta_box['show_on']['key']) || 'page-template' !== $meta_box['show_on']['key']) {
         return $display;
     }
     $object_id = cmb_Meta_Box::get_object_id();
     if (!$object_id || cmb_Meta_Box::get_object_type() !== 'post') {
         return false;
     }
     // Get current template
     $current_template = get_post_meta($object_id, '_wp_page_template', true);
     // See if there's a match
     if ($current_template && in_array($current_template, (array) $meta_box['show_on']['value'])) {
         return true;
     }
     return false;
 }
 /**
  * Default fallback sanitization method. Applies filters.
  * @since  1.0.2
  * @param  mixed $meta_value Meta value
  * @param  array $field      Field config array
  */
 public static function default_sanitization($meta_value, $field)
 {
     $object_type = cmb_Meta_Box::get_object_type();
     $object_id = cmb_Meta_Box::get_object_id();
     // Allow field type validation via filter
     $updated = apply_filters('cmb_validate_' . $field['type'], null, $meta_value, $object_id, $field, $object_type);
     if (null != $updated) {
         return $updated;
     }
     // we'll fallback to 'sanitize_text_field', or 'wp_kses_post`
     switch ($field['type']) {
         case 'wysiwyg':
             // $cb = 'wp_kses';
             // break;
         // $cb = 'wp_kses';
         // break;
         case 'textarea_small':
             $cb = array('cmb_Meta_Box_Sanitize', 'textarea');
             break;
         default:
             $cb = 'sanitize_text_field';
             break;
     }
     // Handle repeatable fields array
     if (is_array($meta_value)) {
         foreach ($meta_value as $key => $value) {
             $meta_value[$key] = call_user_func($cb, $value);
         }
     } else {
         $meta_value = call_user_func($cb, $meta_value);
     }
     return $meta_value;
 }
 /**
  * Default fallback if field's 'sanitization_cb' is NOT defined, or field type does not have a corresponding validation method
  * @since  1.0.0
  * @param  string $name      Non-existent method name
  * @param  array  $arguments All arguments passed to the method
  */
 public function __call($name, $arguments)
 {
     list($meta_value, $field) = $arguments;
     // Handle repeatable fields array
     if (is_array($meta_value)) {
         foreach ($meta_value as $key => $value) {
             // Allow field type validation via filter
             $updated = apply_filters('cmb_validate_' . $field['type'], $value, cmb_Meta_Box::get_object_id(), $field, cmb_Meta_Box::get_object_type());
             if ($updated === $value) {
                 // If nothing changed, we'll fallback to 'sanitize_text_field'
                 $updated = sanitize_text_field($value);
             }
             $meta_value[$key] = $updated;
         }
     } else {
         switch ($field['type']) {
             case 'wysiwyg':
             case 'textarea_small':
                 return self::textarea($meta_value);
             default:
                 // Allow field type validation via filter
                 $updated = apply_filters('cmb_validate_' . $field['type'], $meta_value, cmb_Meta_Box::get_object_id(), $field, cmb_Meta_Box::get_object_type());
                 if ($updated === $meta_value) {
                     // If nothing changed, we'll fallback to 'sanitize_text_field'
                     return sanitize_text_field($meta_value);
                 }
                 return $updated;
         }
     }
     return $meta_value;
 }