1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* ACF Checkbox Field Class |
5
|
|
|
* |
6
|
|
|
* All the logic for this field type |
7
|
|
|
* |
8
|
|
|
* @class acf_field_checkbox |
9
|
|
|
* @extends acf_field |
10
|
|
|
* @package ACF |
11
|
|
|
* @subpackage Fields |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
if( ! class_exists('acf_field_checkbox') ) : |
15
|
|
|
|
16
|
|
|
class acf_field_checkbox extends acf_field { |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
/* |
20
|
|
|
* __construct |
21
|
|
|
* |
22
|
|
|
* This function will setup the field type data |
23
|
|
|
* |
24
|
|
|
* @type function |
25
|
|
|
* @date 5/03/2014 |
26
|
|
|
* @since 5.0.0 |
27
|
|
|
* |
28
|
|
|
* @param n/a |
29
|
|
|
* @return n/a |
30
|
|
|
*/ |
|
|
|
|
31
|
|
|
|
32
|
|
View Code Duplication |
function __construct() { |
|
|
|
|
33
|
|
|
|
34
|
|
|
// vars |
35
|
|
|
$this->name = 'checkbox'; |
36
|
|
|
$this->label = __("Checkbox",'acf'); |
|
|
|
|
37
|
|
|
$this->category = 'choice'; |
38
|
|
|
$this->defaults = array( |
39
|
|
|
'layout' => 'vertical', |
40
|
|
|
'choices' => array(), |
41
|
|
|
'default_value' => '', |
42
|
|
|
'toggle' => 0 |
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
// do not delete! |
47
|
|
|
parent::__construct(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
/* |
52
|
|
|
* render_field() |
53
|
|
|
* |
54
|
|
|
* Create the HTML interface for your field |
55
|
|
|
* |
56
|
|
|
* @param $field (array) the $field being rendered |
57
|
|
|
* |
58
|
|
|
* @type action |
59
|
|
|
* @since 3.6 |
60
|
|
|
* @date 23/01/13 |
61
|
|
|
* |
62
|
|
|
* @param $field (array) the $field being edited |
63
|
|
|
* @return n/a |
64
|
|
|
*/ |
|
|
|
|
65
|
|
|
|
66
|
|
|
function render_field( $field ) { |
|
|
|
|
67
|
|
|
|
68
|
|
|
// decode value (convert to array) |
69
|
|
|
$field['value'] = acf_get_array($field['value'], false); |
|
|
|
|
70
|
|
|
|
71
|
|
|
|
72
|
|
|
// hiden input |
73
|
|
|
acf_hidden_input(array( |
74
|
|
|
'type' => 'hidden', |
75
|
|
|
'name' => $field['name'], |
76
|
|
|
)); |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
// vars |
80
|
|
|
$i = 0; |
81
|
|
|
$li = ''; |
82
|
|
|
$all_checked = true; |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
// checkbox saves an array |
86
|
|
|
$field['name'] .= '[]'; |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
// foreach choices |
90
|
|
|
if( !empty($field['choices']) ) { |
91
|
|
|
|
92
|
|
View Code Duplication |
foreach( $field['choices'] as $value => $label ) { |
|
|
|
|
93
|
|
|
|
94
|
|
|
// increase counter |
95
|
|
|
$i++; |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
// vars |
99
|
|
|
$atts = array( |
100
|
|
|
'type' => 'checkbox', |
101
|
|
|
'id' => $field['id'], |
102
|
|
|
'name' => $field['name'], |
103
|
|
|
'value' => $value, |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
// is choice selected? |
108
|
|
|
if( in_array($value, $field['value']) ) { |
109
|
|
|
|
110
|
|
|
$atts['checked'] = 'checked'; |
111
|
|
|
|
112
|
|
|
} else { |
113
|
|
|
|
114
|
|
|
$all_checked = false; |
115
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
if( isset($field['disabled']) && acf_in_array($value, $field['disabled']) ) { |
120
|
|
|
|
121
|
|
|
$atts['disabled'] = 'disabled'; |
122
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
// each input ID is generated with the $key, however, the first input must not use $key so that it matches the field's label for attribute |
127
|
|
|
if( $i > 1 ) { |
128
|
|
|
|
129
|
|
|
$atts['id'] .= '-' . $value; |
130
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
// append HTML |
135
|
|
|
$li .= '<li><label><input ' . acf_esc_attr( $atts ) . '/>' . $label . '</label></li>'; |
136
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
// toggle all |
141
|
|
|
if( $field['toggle'] ) { |
142
|
|
|
|
143
|
|
|
// vars |
144
|
|
|
$label = __("Toggle All", 'acf'); |
145
|
|
|
$atts = array( |
146
|
|
|
'type' => 'checkbox', |
147
|
|
|
'class' => 'acf-checkbox-toggle' |
148
|
|
|
); |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
// custom label |
152
|
|
|
if( is_string($field['toggle']) ) { |
153
|
|
|
|
154
|
|
|
$label = $field['toggle']; |
155
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
|
159
|
|
|
// checked |
160
|
|
|
if( $all_checked ) { |
161
|
|
|
|
162
|
|
|
$atts['checked'] = 'checked'; |
163
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
|
167
|
|
|
// append HTML |
168
|
|
|
$li = '<li><label><input ' . acf_esc_attr( $atts ) . '/>' . $label . '</label></li>' . $li; |
169
|
|
|
|
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
|
175
|
|
|
// class |
176
|
|
|
$field['class'] .= ' acf-checkbox-list'; |
177
|
|
|
$field['class'] .= ($field['layout'] == 'horizontal') ? ' acf-hl' : ' acf-bl'; |
178
|
|
|
|
179
|
|
|
|
180
|
|
|
// return |
181
|
|
|
echo '<ul ' . acf_esc_attr(array( 'class' => $field['class'] )) . '>' . $li . '</ul>'; |
182
|
|
|
|
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
|
186
|
|
|
/* |
187
|
|
|
* render_field_settings() |
188
|
|
|
* |
189
|
|
|
* Create extra options for your field. This is rendered when editing a field. |
190
|
|
|
* The value of $field['name'] can be used (like bellow) to save extra data to the $field |
191
|
|
|
* |
192
|
|
|
* @type action |
193
|
|
|
* @since 3.6 |
194
|
|
|
* @date 23/01/13 |
195
|
|
|
* |
196
|
|
|
* @param $field - an array holding all the field's data |
197
|
|
|
*/ |
198
|
|
|
|
199
|
|
|
function render_field_settings( $field ) { |
|
|
|
|
200
|
|
|
|
201
|
|
|
// encode choices (convert from array) |
202
|
|
|
$field['choices'] = acf_encode_choices($field['choices']); |
203
|
|
|
$field['default_value'] = acf_encode_choices($field['default_value']); |
204
|
|
|
|
205
|
|
|
|
206
|
|
|
// choices |
207
|
|
|
acf_render_field_setting( $field, array( |
208
|
|
|
'label' => __('Choices','acf'), |
209
|
|
|
'instructions' => __('Enter each choice on a new line.','acf') . '<br /><br />' . __('For more control, you may specify both a value and label like this:','acf'). '<br /><br />' . __('red : Red','acf'), |
210
|
|
|
'type' => 'textarea', |
211
|
|
|
'name' => 'choices', |
212
|
|
|
)); |
213
|
|
|
|
214
|
|
|
|
215
|
|
|
// default_value |
216
|
|
|
acf_render_field_setting( $field, array( |
217
|
|
|
'label' => __('Default Value','acf'), |
218
|
|
|
'instructions' => __('Enter each default value on a new line','acf'), |
219
|
|
|
'type' => 'textarea', |
220
|
|
|
'name' => 'default_value', |
221
|
|
|
)); |
222
|
|
|
|
223
|
|
|
|
224
|
|
|
// layout |
225
|
|
|
acf_render_field_setting( $field, array( |
226
|
|
|
'label' => __('Layout','acf'), |
227
|
|
|
'instructions' => '', |
228
|
|
|
'type' => 'radio', |
229
|
|
|
'name' => 'layout', |
230
|
|
|
'layout' => 'horizontal', |
231
|
|
|
'choices' => array( |
232
|
|
|
'vertical' => __("Vertical",'acf'), |
233
|
|
|
'horizontal' => __("Horizontal",'acf') |
234
|
|
|
) |
235
|
|
|
)); |
236
|
|
|
|
237
|
|
|
|
238
|
|
|
// layout |
239
|
|
|
acf_render_field_setting( $field, array( |
240
|
|
|
'label' => __('Toggle','acf'), |
241
|
|
|
'instructions' => __('Prepend an extra checkbox to toggle all choices','acf'), |
242
|
|
|
'type' => 'radio', |
243
|
|
|
'name' => 'toggle', |
244
|
|
|
'layout' => 'horizontal', |
245
|
|
|
'choices' => array( |
246
|
|
|
1 => __("Yes",'acf'), |
247
|
|
|
0 => __("No",'acf'), |
248
|
|
|
) |
249
|
|
|
)); |
250
|
|
|
|
251
|
|
|
|
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
|
255
|
|
|
/* |
256
|
|
|
* update_field() |
257
|
|
|
* |
258
|
|
|
* This filter is appied to the $field before it is saved to the database |
259
|
|
|
* |
260
|
|
|
* @type filter |
261
|
|
|
* @since 3.6 |
262
|
|
|
* @date 23/01/13 |
263
|
|
|
* |
264
|
|
|
* @param $field - the field array holding all the field options |
265
|
|
|
* @param $post_id - the field group ID (post_type = acf) |
266
|
|
|
* |
267
|
|
|
* @return $field - the modified field |
268
|
|
|
*/ |
|
|
|
|
269
|
|
|
|
270
|
|
View Code Duplication |
function update_field( $field ) { |
|
|
|
|
271
|
|
|
|
272
|
|
|
// decode choices (convert to array) |
273
|
|
|
$field['choices'] = acf_decode_choices($field['choices']); |
274
|
|
|
$field['default_value'] = acf_decode_choices($field['default_value']); |
275
|
|
|
|
276
|
|
|
|
277
|
|
|
// return |
278
|
|
|
return $field; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
|
282
|
|
|
/* |
283
|
|
|
* update_value() |
284
|
|
|
* |
285
|
|
|
* This filter is appied to the $value before it is updated in the db |
286
|
|
|
* |
287
|
|
|
* @type filter |
288
|
|
|
* @since 3.6 |
289
|
|
|
* @date 23/01/13 |
290
|
|
|
* |
291
|
|
|
* @param $value - the value which will be saved in the database |
292
|
|
|
* @param $post_id - the $post_id of which the value will be saved |
293
|
|
|
* @param $field - the field array holding all the field options |
294
|
|
|
* |
295
|
|
|
* @return $value - the modified value |
296
|
|
|
*/ |
|
|
|
|
297
|
|
|
|
298
|
|
View Code Duplication |
function update_value( $value, $post_id, $field ) { |
|
|
|
|
299
|
|
|
|
300
|
|
|
// validate |
301
|
|
|
if( empty($value) ) { |
302
|
|
|
|
303
|
|
|
return $value; |
304
|
|
|
|
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
|
308
|
|
|
// array |
309
|
|
|
if( is_array($value) ) { |
310
|
|
|
|
311
|
|
|
// save value as strings, so we can clearly search for them in SQL LIKE statements |
312
|
|
|
$value = array_map('strval', $value); |
313
|
|
|
|
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
|
317
|
|
|
// return |
318
|
|
|
return $value; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
new acf_field_checkbox(); |
324
|
|
|
|
325
|
|
|
endif; |
326
|
|
|
|
327
|
|
|
?> |
|
|
|
|
328
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.