1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* ACF Radio Button Field Class |
5
|
|
|
* |
6
|
|
|
* All the logic for this field type |
7
|
|
|
* |
8
|
|
|
* @class acf_field_radio |
9
|
|
|
* @extends acf_field |
10
|
|
|
* @package ACF |
11
|
|
|
* @subpackage Fields |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
if( ! class_exists('acf_field_radio') ) : |
15
|
|
|
|
16
|
|
|
class acf_field_radio 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 = 'radio'; |
36
|
|
|
$this->label = __("Radio Button",'acf'); |
|
|
|
|
37
|
|
|
$this->category = 'choice'; |
38
|
|
|
$this->defaults = array( |
39
|
|
|
'layout' => 'vertical', |
40
|
|
|
'choices' => array(), |
41
|
|
|
'default_value' => '', |
42
|
|
|
'other_choice' => 0, |
43
|
|
|
'save_other_choice' => 0, |
44
|
|
|
); |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
// do not delete! |
48
|
|
|
parent::__construct(); |
49
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/* |
54
|
|
|
* render_field() |
55
|
|
|
* |
56
|
|
|
* Create the HTML interface for your field |
57
|
|
|
* |
58
|
|
|
* @param $field (array) the $field being rendered |
59
|
|
|
* |
60
|
|
|
* @type action |
61
|
|
|
* @since 3.6 |
62
|
|
|
* @date 23/01/13 |
63
|
|
|
* |
64
|
|
|
* @param $field (array) the $field being edited |
65
|
|
|
* @return n/a |
66
|
|
|
*/ |
|
|
|
|
67
|
|
|
|
68
|
|
|
function render_field( $field ) { |
|
|
|
|
69
|
|
|
|
70
|
|
|
// vars |
71
|
|
|
$i = 0; |
72
|
|
|
$checked = false; |
|
|
|
|
73
|
|
|
|
74
|
|
|
|
75
|
|
|
// class |
76
|
|
|
$field['class'] .= ' acf-radio-list'; |
77
|
|
|
$field['class'] .= ($field['layout'] == 'horizontal') ? ' acf-hl' : ' acf-bl'; |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
// e |
81
|
|
|
$e = '<ul ' . acf_esc_attr(array( 'class' => $field['class'] )) . '>'; |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
// other choice |
85
|
|
|
if( $field['other_choice'] ) { |
86
|
|
|
|
87
|
|
|
// vars |
88
|
|
|
$input = array( |
89
|
|
|
'type' => 'text', |
90
|
|
|
'name' => $field['name'], |
91
|
|
|
'value' => '', |
92
|
|
|
'disabled' => 'disabled' |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
// select other choice if value is not a valid choice |
97
|
|
|
if( !isset($field['choices'][ $field['value'] ]) ) { |
98
|
|
|
|
99
|
|
|
unset($input['disabled']); |
100
|
|
|
$input['value'] = $field['value']; |
101
|
|
|
$field['value'] = 'other'; |
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
$field['choices']['other'] = '</label><input type="text" ' . acf_esc_attr($input) . ' /><label>'; |
107
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
// require choices |
112
|
|
|
if( !empty($field['choices']) ) { |
113
|
|
|
|
114
|
|
|
// select first choice if value is not a valid choice |
115
|
|
View Code Duplication |
if( !isset($field['choices'][ $field['value'] ]) ) { |
|
|
|
|
116
|
|
|
|
117
|
|
|
$field['value'] = key($field['choices']); |
118
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
// foreach choices |
123
|
|
View Code Duplication |
foreach( $field['choices'] as $value => $label ) { |
|
|
|
|
124
|
|
|
|
125
|
|
|
// increase counter |
126
|
|
|
$i++; |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
// vars |
130
|
|
|
$atts = array( |
131
|
|
|
'type' => 'radio', |
132
|
|
|
'id' => $field['id'], |
133
|
|
|
'name' => $field['name'], |
134
|
|
|
'value' => $value, |
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
if( strval($value) === strval($field['value']) ) { |
139
|
|
|
|
140
|
|
|
$atts['checked'] = 'checked'; |
141
|
|
|
$checked = true; |
|
|
|
|
142
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if( isset($field['disabled']) && acf_in_array($value, $field['disabled']) ) { |
146
|
|
|
|
147
|
|
|
$atts['disabled'] = 'disabled'; |
148
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
|
152
|
|
|
// 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 |
153
|
|
|
if( $i > 1 ) { |
154
|
|
|
|
155
|
|
|
$atts['id'] .= '-' . $value; |
156
|
|
|
|
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$e .= '<li><label><input ' . acf_esc_attr( $atts ) . '/>' . $label . '</label></li>'; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
|
165
|
|
|
$e .= '</ul>'; |
166
|
|
|
|
167
|
|
|
echo $e; |
168
|
|
|
|
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
|
172
|
|
|
/* |
173
|
|
|
* render_field_settings() |
174
|
|
|
* |
175
|
|
|
* Create extra options for your field. This is rendered when editing a field. |
176
|
|
|
* The value of $field['name'] can be used (like bellow) to save extra data to the $field |
177
|
|
|
* |
178
|
|
|
* @type action |
179
|
|
|
* @since 3.6 |
180
|
|
|
* @date 23/01/13 |
181
|
|
|
* |
182
|
|
|
* @param $field - an array holding all the field's data |
183
|
|
|
*/ |
184
|
|
|
|
185
|
|
|
function render_field_settings( $field ) { |
|
|
|
|
186
|
|
|
|
187
|
|
|
// encode choices (convert from array) |
188
|
|
|
$field['choices'] = acf_encode_choices($field['choices']); |
189
|
|
|
|
190
|
|
|
|
191
|
|
|
// choices |
192
|
|
|
acf_render_field_setting( $field, array( |
193
|
|
|
'label' => __('Choices','acf'), |
194
|
|
|
'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'), |
195
|
|
|
'type' => 'textarea', |
196
|
|
|
'name' => 'choices', |
197
|
|
|
)); |
198
|
|
|
|
199
|
|
|
|
200
|
|
|
// other_choice |
201
|
|
|
acf_render_field_setting( $field, array( |
202
|
|
|
'label' => __('Other','acf'), |
203
|
|
|
'instructions' => '', |
204
|
|
|
'type' => 'true_false', |
205
|
|
|
'name' => 'other_choice', |
206
|
|
|
'message' => __("Add 'other' choice to allow for custom values", 'acf') |
207
|
|
|
)); |
208
|
|
|
|
209
|
|
|
|
210
|
|
|
// save_other_choice |
211
|
|
|
acf_render_field_setting( $field, array( |
212
|
|
|
'label' => __('Save Other','acf'), |
213
|
|
|
'instructions' => '', |
214
|
|
|
'type' => 'true_false', |
215
|
|
|
'name' => 'save_other_choice', |
216
|
|
|
'message' => __("Save 'other' values to the field's choices", 'acf') |
217
|
|
|
)); |
218
|
|
|
|
219
|
|
|
|
220
|
|
|
// default_value |
221
|
|
|
acf_render_field_setting( $field, array( |
222
|
|
|
'label' => __('Default Value','acf'), |
223
|
|
|
'instructions' => __('Appears when creating a new post','acf'), |
224
|
|
|
'type' => 'text', |
225
|
|
|
'name' => 'default_value', |
226
|
|
|
)); |
227
|
|
|
|
228
|
|
|
|
229
|
|
|
// layout |
230
|
|
|
acf_render_field_setting( $field, array( |
231
|
|
|
'label' => __('Layout','acf'), |
232
|
|
|
'instructions' => '', |
233
|
|
|
'type' => 'radio', |
234
|
|
|
'name' => 'layout', |
235
|
|
|
'layout' => 'horizontal', |
236
|
|
|
'choices' => array( |
237
|
|
|
'vertical' => __("Vertical",'acf'), |
238
|
|
|
'horizontal' => __("Horizontal",'acf') |
239
|
|
|
) |
240
|
|
|
)); |
241
|
|
|
|
242
|
|
|
|
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
|
246
|
|
|
/* |
247
|
|
|
* update_field() |
248
|
|
|
* |
249
|
|
|
* This filter is appied to the $field before it is saved to the database |
250
|
|
|
* |
251
|
|
|
* @type filter |
252
|
|
|
* @since 3.6 |
253
|
|
|
* @date 23/01/13 |
254
|
|
|
* |
255
|
|
|
* @param $field - the field array holding all the field options |
256
|
|
|
* @param $post_id - the field group ID (post_type = acf) |
257
|
|
|
* |
258
|
|
|
* @return $field - the modified field |
259
|
|
|
*/ |
|
|
|
|
260
|
|
|
|
261
|
|
|
function update_field( $field ) { |
|
|
|
|
262
|
|
|
|
263
|
|
|
// decode choices (convert to array) |
264
|
|
|
$field['choices'] = acf_decode_choices($field['choices']); |
265
|
|
|
|
266
|
|
|
|
267
|
|
|
// return |
268
|
|
|
return $field; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
|
272
|
|
|
/* |
273
|
|
|
* update_value() |
274
|
|
|
* |
275
|
|
|
* This filter is appied to the $value before it is updated in the db |
276
|
|
|
* |
277
|
|
|
* @type filter |
278
|
|
|
* @since 3.6 |
279
|
|
|
* @date 23/01/13 |
280
|
|
|
* @todo Fix bug where $field was found via json and has no ID |
281
|
|
|
* |
282
|
|
|
* @param $value - the value which will be saved in the database |
283
|
|
|
* @param $post_id - the $post_id of which the value will be saved |
284
|
|
|
* @param $field - the field array holding all the field options |
285
|
|
|
* |
286
|
|
|
* @return $value - the modified value |
287
|
|
|
*/ |
|
|
|
|
288
|
|
|
|
289
|
|
|
function update_value( $value, $post_id, $field ) { |
|
|
|
|
290
|
|
|
|
291
|
|
|
// save_other_choice |
292
|
|
|
if( $field['save_other_choice'] ) { |
293
|
|
|
|
294
|
|
|
// value isn't in choices yet |
295
|
|
|
if( !isset($field['choices'][ $value ]) ) { |
296
|
|
|
|
297
|
|
|
// get ID if local |
298
|
|
|
if( !$field['ID'] ) { |
299
|
|
|
|
300
|
|
|
$field = acf_get_field( $field['key'], true ); |
301
|
|
|
|
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
|
305
|
|
|
// bail early if no ID |
306
|
|
|
if( !$field['ID'] ) { |
307
|
|
|
|
308
|
|
|
return $value; |
309
|
|
|
|
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
|
313
|
|
|
// update $field |
314
|
|
|
$field['choices'][ $value ] = $value; |
315
|
|
|
|
316
|
|
|
|
317
|
|
|
// save |
318
|
|
|
acf_update_field( $field ); |
319
|
|
|
|
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
|
325
|
|
|
// return |
326
|
|
|
return $value; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
|
330
|
|
|
/* |
331
|
|
|
* load_value() |
332
|
|
|
* |
333
|
|
|
* This filter is appied to the $value after it is loaded from the db |
334
|
|
|
* |
335
|
|
|
* @type filter |
336
|
|
|
* @since 5.2.9 |
337
|
|
|
* @date 23/01/13 |
338
|
|
|
* |
339
|
|
|
* @param $value - the value found in the database |
340
|
|
|
* @param $post_id - the $post_id from which the value was loaded from |
341
|
|
|
* @param $field - the field array holding all the field options |
342
|
|
|
* |
343
|
|
|
* @return $value - the value to be saved in te database |
344
|
|
|
*/ |
|
|
|
|
345
|
|
|
|
346
|
|
|
function load_value( $value, $post_id, $field ) { |
|
|
|
|
347
|
|
|
|
348
|
|
|
// must be single value |
349
|
|
|
if( is_array($value) ) { |
350
|
|
|
|
351
|
|
|
$value = array_pop($value); |
352
|
|
|
|
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
|
356
|
|
|
// return |
357
|
|
|
return $value; |
358
|
|
|
|
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
new acf_field_radio(); |
364
|
|
|
|
365
|
|
|
endif; |
366
|
|
|
|
367
|
|
|
?> |
|
|
|
|
368
|
|
|
|
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.