1 <?php
2
3 /**
4 * A set of multiple checkboxes which submits as an array of the checked values.
5 *
6 * This class extends {@link Fieldmanager_Options}, which allows you to define
7 * options (values) via an array or via a dynamic
8 * {@link Fieldmanager_Datasource}, like {@link Fieldmanager_Datasource_Post},
9 * {@link Fieldmanager_Datasource_Term}, or {@link Fieldmanager_Datasource_User}.
10 *
11 * @package Fieldmanager_Field
12 */
13 class Fieldmanager_Checkboxes extends Fieldmanager_Options {
14
15 /**
16 * @var string
17 * Override field_class
18 */
19 public $field_class = 'checkboxes';
20
21 public $multiple = True;
22
23 /**
24 * Render form element
25 * @param mixed $value
26 * @return string HTML
27 */
28 public function form_element( $value = array() ) {
29
30 return sprintf(
31 '<div class="fm-checkbox-group" id="%s">%s</div>',
32 esc_attr( $this->get_element_id() ),
33 $this->form_data_elements( $value )
34 );
35 }
36
37 /**
38 * Override function to allow all boxes to be checked by default.
39 * @param string $current_option this option
40 * @param array $options all valid options
41 * @param string $attribute
42 * @return string $attribute on match, empty on failure.
43 */
44 public function option_selected( $current_option, $options, $attribute ) {
45 if ( ( ( $options != null && !empty( $options ) ) && in_array( $current_option, $options ) ) || ( 'checked' == $this->default_value && in_array( $this->default_value, $options ) ) ) {
46 return $attribute;
47 } else {
48 return '';
49 }
50 }
51
52 }