PluginProbe ʕ •ᴥ•ʔ
Secure Custom Fields / 6.4.1-beta6
Secure Custom Fields v6.4.1-beta6
6.9.5 6.9.4 6.9.3 6.9.2 6.9.1 6.9.0 6.8.9 6.8.7 6.8.8 6.8.6 6.8.4 6.8.5 trunk 6.4.0-beta1 6.4.0-beta2 6.4.1 6.4.1-beta3 6.4.1-beta4 6.4.1-beta5 6.4.1-beta6 6.4.1-beta7 6.4.2 6.5.0 6.5.1 6.5.2 6.5.3 6.5.4 6.5.5 6.5.6 6.5.7 6.6.0 6.7.0 6.7.1 6.8.0 6.8.1 6.8.2 6.8.3
secure-custom-fields / includes / fields / class-acf-field-radio.php
secure-custom-fields / includes / fields Last commit date
class-acf-field-accordion.php 1 year ago class-acf-field-button-group.php 1 year ago class-acf-field-checkbox.php 1 year ago class-acf-field-clone.php 1 year ago class-acf-field-color_picker.php 1 year ago class-acf-field-date_picker.php 1 year ago class-acf-field-date_time_picker.php 1 year ago class-acf-field-email.php 1 year ago class-acf-field-file.php 1 year ago class-acf-field-flexible-content.php 1 year ago class-acf-field-gallery.php 1 year ago class-acf-field-google-map.php 1 year ago class-acf-field-group.php 1 year ago class-acf-field-icon_picker.php 1 year ago class-acf-field-image.php 1 year ago class-acf-field-link.php 1 year ago class-acf-field-message.php 1 year ago class-acf-field-number.php 1 year ago class-acf-field-oembed.php 1 year ago class-acf-field-output.php 1 year ago class-acf-field-page_link.php 1 year ago class-acf-field-password.php 1 year ago class-acf-field-post_object.php 1 year ago class-acf-field-radio.php 1 year ago class-acf-field-range.php 1 year ago class-acf-field-relationship.php 1 year ago class-acf-field-repeater.php 1 year ago class-acf-field-select.php 1 year ago class-acf-field-separator.php 1 year ago class-acf-field-tab.php 1 year ago class-acf-field-taxonomy.php 1 year ago class-acf-field-text.php 1 year ago class-acf-field-textarea.php 1 year ago class-acf-field-time_picker.php 1 year ago class-acf-field-true_false.php 1 year ago class-acf-field-url.php 1 year ago class-acf-field-user.php 1 year ago class-acf-field-wysiwyg.php 1 year ago class-acf-field.php 1 year ago class-acf-repeater-table.php 1 year ago index.php 1 year ago
class-acf-field-radio.php
458 lines
1 <?php
2
3 if ( ! class_exists( 'acf_field_radio' ) ) :
4
5 class acf_field_radio extends acf_field {
6
7
8 /**
9 * This function will setup the field type data
10 *
11 * @type function
12 * @date 5/03/2014
13 * @since ACF 5.0.0
14 *
15 * @param n/a
16 * @return n/a
17 */
18 function initialize() {
19
20 // vars
21 $this->name = 'radio';
22 $this->label = __( 'Radio Button', 'secure-custom-fields' );
23 $this->category = 'choice';
24 $this->description = __( 'A group of radio button inputs that allows the user to make a single selection from values that you specify.', 'secure-custom-fields' );
25 $this->preview_image = acf_get_url() . '/assets/images/field-type-previews/field-preview-radio-button.png';
26 $this->doc_url = 'https://www.advancedcustomfields.com/resources/radio-button/';
27 $this->defaults = array(
28 'layout' => 'vertical',
29 'choices' => array(),
30 'default_value' => '',
31 'other_choice' => 0,
32 'save_other_choice' => 0,
33 'allow_null' => 0,
34 'return_format' => 'value',
35 );
36 }
37
38
39 /**
40 * Create the HTML interface for your field
41 *
42 * @param $field (array) the $field being rendered
43 *
44 * @type action
45 * @since ACF 3.6
46 * @date 23/01/13
47 *
48 * @param $field (array) the $field being edited
49 * @return n/a
50 */
51 function render_field( $field ) {
52
53 // vars
54 $e = '';
55 $ul = array(
56 'class' => 'acf-radio-list',
57 'data-allow_null' => $field['allow_null'],
58 'data-other_choice' => $field['other_choice'],
59 );
60
61 // append to class
62 $ul['class'] .= ' ' . ( $field['layout'] == 'horizontal' ? 'acf-hl' : 'acf-bl' );
63 $ul['class'] .= ' ' . $field['class'];
64
65 // Determine selected value.
66 $value = (string) $field['value'];
67
68 // 1. Selected choice.
69 if ( isset( $field['choices'][ $value ] ) ) {
70 $checked = (string) $value;
71
72 // 2. Custom choice.
73 } elseif ( $field['other_choice'] && $value !== '' ) {
74 $checked = 'other';
75
76 // 3. Empty choice.
77 } elseif ( $field['allow_null'] ) {
78 $checked = '';
79
80 // 4. Default to first choice.
81 } else {
82 $checked = (string) key( $field['choices'] );
83 }
84
85 // other choice
86 $other_input = false;
87 if ( $field['other_choice'] ) {
88
89 // Define other input attrs.
90 $other_input = array(
91 'type' => 'text',
92 'name' => $field['name'],
93 'value' => '',
94 'disabled' => 'disabled',
95 'class' => 'acf-disabled',
96 );
97
98 // Select other choice if value is not a valid choice.
99 if ( $checked === 'other' ) {
100 unset( $other_input['disabled'] );
101 $other_input['value'] = $field['value'];
102 }
103
104 // Ensure an 'other' choice is defined.
105 if ( ! isset( $field['choices']['other'] ) ) {
106 $field['choices']['other'] = '';
107 }
108 }
109
110 // Bail early if no choices.
111 if ( empty( $field['choices'] ) ) {
112 return;
113 }
114
115 // Hiden input.
116 $e .= acf_get_hidden_input( array( 'name' => $field['name'] ) );
117
118 // Open <ul>.
119 $e .= '<ul ' . acf_esc_attrs( $ul ) . '>';
120
121 // Loop through choices.
122 foreach ( $field['choices'] as $value => $label ) {
123 $is_selected = false;
124
125 // Ensure value is a string.
126 $value = (string) $value;
127
128 // Define input attrs.
129 $attrs = array(
130 'type' => 'radio',
131 'id' => sanitize_title( $field['id'] . '-' . $value ),
132 'name' => $field['name'],
133 'value' => $value,
134 );
135
136 // Check if selected.
137 if ( esc_attr( $value ) === esc_attr( $checked ) ) {
138 $attrs['checked'] = 'checked';
139 $is_selected = true;
140 }
141
142 // Check if is disabled.
143 if ( isset( $field['disabled'] ) && acf_in_array( $value, $field['disabled'] ) ) {
144 $attrs['disabled'] = 'disabled';
145 }
146
147 // Additional HTML (the "Other" input).
148 $additional_html = '';
149 if ( $value === 'other' && $other_input ) {
150 $additional_html = ' ' . acf_get_text_input( $other_input );
151 }
152
153 // append
154 $e .= '<li><label' . ( $is_selected ? ' class="selected"' : '' ) . '><input ' . acf_esc_attrs( $attrs ) . '/>' . acf_esc_html( $label ) . '</label>' . $additional_html . '</li>';
155 }
156
157 // Close <ul>.
158 $e .= '</ul>';
159
160 // Output HTML.
161 echo $e; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- escaped per attribute above.
162 }
163
164
165 /**
166 * Create extra options for your field. This is rendered when editing a field.
167 * The value of $field['name'] can be used (like bellow) to save extra data to the $field
168 *
169 * @type action
170 * @since ACF 3.6
171 * @date 23/01/13
172 *
173 * @param $field - an array holding all the field's data
174 */
175 function render_field_settings( $field ) {
176 // Encode choices (convert from array).
177 $field['choices'] = acf_encode_choices( $field['choices'] );
178
179 acf_render_field_setting(
180 $field,
181 array(
182 'label' => __( 'Choices', 'secure-custom-fields' ),
183 'instructions' => __( 'Enter each choice on a new line.', 'secure-custom-fields' ) . '<br />' . __( 'For more control, you may specify both a value and label like this:', 'secure-custom-fields' ) . '<br /><span class="acf-field-setting-example">' . __( 'red : Red', 'secure-custom-fields' ) . '</span>',
184 'type' => 'textarea',
185 'name' => 'choices',
186 )
187 );
188
189 acf_render_field_setting(
190 $field,
191 array(
192 'label' => __( 'Default Value', 'secure-custom-fields' ),
193 'instructions' => __( 'Appears when creating a new post', 'secure-custom-fields' ),
194 'type' => 'text',
195 'name' => 'default_value',
196 )
197 );
198
199 acf_render_field_setting(
200 $field,
201 array(
202 'label' => __( 'Return Value', 'secure-custom-fields' ),
203 'instructions' => __( 'Specify the returned value on front end', 'secure-custom-fields' ),
204 'type' => 'radio',
205 'name' => 'return_format',
206 'layout' => 'horizontal',
207 'choices' => array(
208 'value' => __( 'Value', 'secure-custom-fields' ),
209 'label' => __( 'Label', 'secure-custom-fields' ),
210 'array' => __( 'Both (Array)', 'secure-custom-fields' ),
211 ),
212 )
213 );
214 }
215
216 /**
217 * Renders the field settings used in the "Validation" tab.
218 *
219 * @since ACF 6.0
220 *
221 * @param array $field The field settings array.
222 * @return void
223 */
224 function render_field_validation_settings( $field ) {
225 acf_render_field_setting(
226 $field,
227 array(
228 'label' => __( 'Allow Null', 'secure-custom-fields' ),
229 'instructions' => '',
230 'name' => 'allow_null',
231 'type' => 'true_false',
232 'ui' => 1,
233 )
234 );
235
236 acf_render_field_setting(
237 $field,
238 array(
239 'label' => __( 'Allow Other Choice', 'secure-custom-fields' ),
240 'name' => 'other_choice',
241 'type' => 'true_false',
242 'ui' => 1,
243 'instructions' => __( "Add 'other' choice to allow for custom values", 'secure-custom-fields' ),
244 )
245 );
246
247 acf_render_field_setting(
248 $field,
249 array(
250 'label' => __( 'Save Other Choice', 'secure-custom-fields' ),
251 'name' => 'save_other_choice',
252 'type' => 'true_false',
253 'ui' => 1,
254 'instructions' => __( "Save 'other' values to the field's choices", 'secure-custom-fields' ),
255 'conditions' => array(
256 'field' => 'other_choice',
257 'operator' => '==',
258 'value' => 1,
259 ),
260 )
261 );
262 }
263
264 /**
265 * Renders the field settings used in the "Presentation" tab.
266 *
267 * @since ACF 6.0
268 *
269 * @param array $field The field settings array.
270 * @return void
271 */
272 function render_field_presentation_settings( $field ) {
273 acf_render_field_setting(
274 $field,
275 array(
276 'label' => __( 'Layout', 'secure-custom-fields' ),
277 'instructions' => '',
278 'type' => 'radio',
279 'name' => 'layout',
280 'layout' => 'horizontal',
281 'choices' => array(
282 'vertical' => __( 'Vertical', 'secure-custom-fields' ),
283 'horizontal' => __( 'Horizontal', 'secure-custom-fields' ),
284 ),
285 )
286 );
287 }
288
289 /**
290 * This filter is appied to the $field before it is saved to the database
291 *
292 * @type filter
293 * @since ACF 3.6
294 * @date 23/01/13
295 *
296 * @param $field - the field array holding all the field options
297 * @param $post_id - the field group ID (post_type = acf)
298 *
299 * @return $field - the modified field
300 */
301 function update_field( $field ) {
302
303 // decode choices (convert to array)
304 $field['choices'] = acf_decode_choices( $field['choices'] );
305
306 // return
307 return $field;
308 }
309
310
311 /**
312 * This filter is appied to the $value before it is updated in the db
313 *
314 * @type filter
315 * @since ACF 3.6
316 * @date 23/01/13
317 * @todo Fix bug where $field was found via json and has no ID
318 *
319 * @param $value - the value which will be saved in the database
320 * @param $post_id - the post_id of which the value will be saved
321 * @param $field - the field array holding all the field options
322 *
323 * @return $value - the modified value
324 */
325 function update_value( $value, $post_id, $field ) {
326
327 // bail early if no value (allow 0 to be saved)
328 if ( ! $value && ! is_numeric( $value ) ) {
329 return $value;
330 }
331
332 // save_other_choice
333 if ( $field['save_other_choice'] ) {
334
335 // value isn't in choices yet
336 if ( ! isset( $field['choices'][ $value ] ) ) {
337
338 // get raw $field (may have been changed via repeater field)
339 // if field is local, it won't have an ID
340 $selector = $field['ID'] ? $field['ID'] : $field['key'];
341 $field = acf_get_field( $selector );
342
343 // bail early if no ID (JSON only)
344 if ( ! $field['ID'] ) {
345 return $value;
346 }
347
348 // unslash (fixes serialize single quote issue)
349 $value = wp_unslash( $value );
350
351 // sanitize (remove tags)
352 $value = sanitize_text_field( $value );
353
354 // update $field
355 $field['choices'][ $value ] = $value;
356
357 // save
358 acf_update_field( $field );
359 }
360 }
361
362 // return
363 return $value;
364 }
365
366
367 /**
368 * This filter is appied to the $value after it is loaded from the db
369 *
370 * @type filter
371 * @since ACF 5.2.9
372 * @date 23/01/13
373 *
374 * @param $value - the value found in the database
375 * @param $post_id - the post_id from which the value was loaded from
376 * @param $field - the field array holding all the field options
377 *
378 * @return $value - the value to be saved in te database
379 */
380 function load_value( $value, $post_id, $field ) {
381
382 // must be single value
383 if ( is_array( $value ) ) {
384 $value = array_pop( $value );
385 }
386
387 // return
388 return $value;
389 }
390
391
392 /**
393 * This function will translate field settings
394 *
395 * @type function
396 * @date 8/03/2016
397 * @since ACF 5.3.2
398 *
399 * @param $field (array)
400 * @return $field
401 */
402 function translate_field( $field ) {
403
404 return acf_get_field_type( 'select' )->translate_field( $field );
405 }
406
407
408 /**
409 * This filter is appied to the $value after it is loaded from the db and before it is returned to the template
410 *
411 * @type filter
412 * @since ACF 3.6
413 * @date 23/01/13
414 *
415 * @param $value (mixed) the value which was loaded from the database
416 * @param $post_id (mixed) the post_id from which the value was loaded
417 * @param $field (array) the field array holding all the field options
418 *
419 * @return $value (mixed) the modified value
420 */
421 function format_value( $value, $post_id, $field ) {
422
423 return acf_get_field_type( 'select' )->format_value( $value, $post_id, $field );
424 }
425
426 /**
427 * Return the schema array for the REST API.
428 *
429 * @param array $field
430 * @return array
431 */
432 function get_rest_schema( array $field ) {
433 $schema = parent::get_rest_schema( $field );
434
435 if ( isset( $field['default_value'] ) && '' !== $field['default_value'] ) {
436 $schema['default'] = $field['default_value'];
437 }
438
439 // If other/custom choices are allowed, nothing else to do here.
440 if ( ! empty( $field['other_choice'] ) ) {
441 return $schema;
442 }
443
444 $schema['enum'] = acf_get_field_type( 'select' )->format_rest_choices( $field['choices'] );
445
446 if ( ! empty( $field['allow_null'] ) ) {
447 $schema['enum'][] = null;
448 }
449
450 return $schema;
451 }
452 }
453
454
455 // initialize
456 acf_register_field_type( 'acf_field_radio' );
457 endif; // class_exists check
458