PluginProbe
Advanced Custom Fields (ACF®) / 4.4.12
Advanced Custom Fields (ACF®) v4.4.12
6.8.9 6.8.8 6.8.7 6.8.6 6.8.5 6.8.4 6.8.3 6.8.2 6.8.1 5.8.5 5.8.6 5.8.7 5.8.8 5.8.9 5.9.0 5.9.1 5.9.2 5.9.3 5.9.4 5.9.5 5.9.6 5.9.7 5.9.8 5.9.9 6.0.0 All 230 releases
advanced-custom-fields / core / fields / checkbox.php
checkbox.php
210 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class acf_field_checkbox extends acf_field
4 {
5
6 /*
7 * __construct
8 *
9 * Set name / label needed for actions / filters
10 *
11 * @since 3.6
12 * @date 23/01/13
13 */
14
15 function __construct()
16 {
17 // vars
18 $this->name = 'checkbox';
19 $this->label = __("Checkbox",'acf');
20 $this->category = __("Choice",'acf');
21 $this->defaults = array(
22 'layout' => 'vertical',
23 'choices' => array(),
24 'default_value' => '',
25 );
26
27
28 // do not delete!
29 parent::__construct();
30 }
31
32
33 /*
34 * create_field()
35 *
36 * Create the HTML interface for your field
37 *
38 * @param $field - an array holding all the field's data
39 *
40 * @type action
41 * @since 3.6
42 * @date 23/01/13
43 */
44
45 function create_field( $field )
46 {
47 // value must be array
48 if( !is_array($field['value']) )
49 {
50 // perhaps this is a default value with new lines in it?
51 if( strpos($field['value'], "\n") !== false )
52 {
53 // found multiple lines, explode it
54 $field['value'] = explode("\n", $field['value']);
55 }
56 else
57 {
58 $field['value'] = array( $field['value'] );
59 }
60 }
61
62
63 // trim value
64 $field['value'] = array_map('trim', $field['value']);
65
66
67 // vars
68 $i = 0;
69 $e = '<input type="hidden" name="' . esc_attr($field['name']) . '" value="" />';
70 $e .= '<ul class="acf-checkbox-list ' . esc_attr($field['class']) . ' ' . esc_attr($field['layout']) . '">';
71
72
73 // checkbox saves an array
74 $field['name'] .= '[]';
75
76
77 // foreach choices
78 foreach( $field['choices'] as $key => $value )
79 {
80 // vars
81 $i++;
82 $atts = '';
83
84
85 if( in_array($key, $field['value']) )
86 {
87 $atts = 'checked="yes"';
88 }
89 if( isset($field['disabled']) && in_array($key, $field['disabled']) )
90 {
91 $atts .= ' disabled="true"';
92 }
93
94
95 // each checkbox ID is generated with the $key, however, the first checkbox must not use $key so that it matches the field's label for attribute
96 $id = $field['id'];
97
98 if( $i > 1 )
99 {
100 $id .= '-' . $key;
101 }
102
103 $e .= '<li><label><input id="' . esc_attr($id) . '" type="checkbox" class="' . esc_attr($field['class']) . '" name="' . esc_attr($field['name']) . '" value="' . esc_attr($key) . '" ' . $atts . ' />' . $value . '</label></li>';
104 }
105
106 $e .= '</ul>';
107
108
109 // return
110 echo $e;
111 }
112
113
114 /*
115 * create_options()
116 *
117 * Create extra options for your field. This is rendered when editing a field.
118 * The value of $field['name'] can be used (like bellow) to save extra data to the $field
119 *
120 * @type action
121 * @since 3.6
122 * @date 23/01/13
123 *
124 * @param $field - an array holding all the field's data
125 */
126
127 function create_options( $field )
128 {
129 // vars
130 $key = $field['name'];
131
132
133 // implode checkboxes so they work in a textarea
134 if( is_array($field['choices']) )
135 {
136 foreach( $field['choices'] as $k => $v )
137 {
138 $field['choices'][ $k ] = $k . ' : ' . $v;
139 }
140 $field['choices'] = implode("\n", $field['choices']);
141 }
142
143 ?>
144 <tr class="field_option field_option_<?php echo $this->name; ?>">
145 <td class="label">
146 <label for=""><?php _e("Choices",'acf'); ?></label>
147 <p><?php _e("Enter each choice on a new line.",'acf'); ?></p>
148 <p><?php _e("For more control, you may specify both a value and label like this:",'acf'); ?></p>
149 <p><?php _e("red : Red",'acf'); ?><br /><?php _e("blue : Blue",'acf'); ?></p>
150 </td>
151 <td>
152 <?php
153
154 do_action('acf/create_field', array(
155 'type' => 'textarea',
156 'class' => 'textarea field_option-choices',
157 'name' => 'fields['.$key.'][choices]',
158 'value' => $field['choices'],
159 ));
160
161 ?>
162 </td>
163 </tr>
164 <tr class="field_option field_option_<?php echo $this->name; ?>">
165 <td class="label">
166 <label><?php _e("Default Value",'acf'); ?></label>
167 <p class="description"><?php _e("Enter each default value on a new line",'acf'); ?></p>
168 </td>
169 <td>
170 <?php
171
172 do_action('acf/create_field', array(
173 'type' => 'textarea',
174 'name' => 'fields['.$key.'][default_value]',
175 'value' => $field['default_value'],
176 ));
177
178 ?>
179 </td>
180 </tr>
181 <tr class="field_option field_option_<?php echo $this->name; ?>">
182 <td class="label">
183 <label for=""><?php _e("Layout",'acf'); ?></label>
184 </td>
185 <td>
186 <?php
187
188 do_action('acf/create_field', array(
189 'type' => 'radio',
190 'name' => 'fields['.$key.'][layout]',
191 'value' => $field['layout'],
192 'layout' => 'horizontal',
193 'choices' => array(
194 'vertical' => __("Vertical",'acf'),
195 'horizontal' => __("Horizontal",'acf')
196 )
197 ));
198
199 ?>
200 </td>
201 </tr>
202 <?php
203
204 }
205
206 }
207
208 new acf_field_checkbox();
209
210 ?>