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