CMB2_Type_Checkbox

CMB checkbox field type.

Defined (1)

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

/includes/types/CMB2_Type_Checkbox.php  
  1. class CMB2_Type_Checkbox extends CMB2_Type_Text { 
  2.  
  3. /** 
  4. * If checkbox is checked 
  5. * @var mixed 
  6. */ 
  7. public $is_checked = null; 
  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(), $is_checked = null ) { 
  16. parent::__construct( $types, $args ); 
  17. $this->is_checked = $is_checked; 
  18.  
  19. public function render() { 
  20. $defaults = array( 
  21. 'type' => 'checkbox',  
  22. 'class' => 'cmb2-option cmb2-list',  
  23. 'value' => 'on',  
  24. 'desc' => '',  
  25. ); 
  26.  
  27. $meta_value = $this->field->escaped_value(); 
  28.  
  29. $is_checked = null === $this->is_checked 
  30. ? ! empty( $meta_value ) 
  31. : $this->is_checked; 
  32.  
  33. if ( $is_checked ) { 
  34. $defaults['checked'] = 'checked'; 
  35.  
  36. $args = $this->parse_args( 'checkbox', $defaults ); 
  37.  
  38. return $this->rendered( 
  39. sprintf( 
  40. '%s <label for="%s">%s</label>',  
  41. parent::render( $args ),  
  42. $this->_id(),  
  43. $this->_desc() 
  44. ); 
  45.