CMB2_Type_Colorpicker

CMB colorpicker field type.

Defined (1)

The class is defined in the following location(s).

/includes/types/CMB2_Type_Colorpicker.php  
  1. class CMB2_Type_Colorpicker extends CMB2_Type_Text { 
  2.  
  3. /** 
  4. * The optional value for the colorpicker field 
  5. * @var string 
  6. */ 
  7. public $value = ''; 
  8.  
  9. /** 
  10. * Constructor 
  11. * @since 2.2.2 
  12. * @param CMB2_Types $types 
  13. * @param array $args 
  14. */ 
  15. public function __construct( CMB2_Types $types, $args = array(), $value = '' ) { 
  16. parent::__construct( $types, $args ); 
  17. $this->value = $value ? $value : $this->value; 
  18.  
  19. public function render() { 
  20. $meta_value = $this->value ? $this->value : $this->field->escaped_value(); 
  21.  
  22. $hex_color = '(([a-fA-F0-9]) {3}) {1, 2}$'; 
  23. if ( preg_match( '/^' . $hex_color . '/i', $meta_value ) ) { 
  24. // Value is just 123abc, so prepend # 
  25. $meta_value = '#' . $meta_value; 
  26. } elseif ( ! preg_match( '/^#' . $hex_color . '/i', $meta_value ) ) { 
  27. // Value doesn't match #123abc, so sanitize to just # 
  28. $meta_value = '#'; 
  29.  
  30. wp_enqueue_style( 'wp-color-picker' ); 
  31.  
  32. $args = wp_parse_args( $this->args, array( 
  33. 'class' => 'cmb2-colorpicker cmb2-text-small',  
  34. 'value' => $meta_value,  
  35. 'js_dependencies' => 'wp-color-picker',  
  36. ) ); 
  37.  
  38. return parent::render( $args ); 
  39.