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