123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498 |
- <?php
- if ( ! class_exists( 'acf_field_radio' ) ) :
- class acf_field_radio extends acf_field {
-
- function initialize() {
-
- $this->name = 'radio';
- $this->label = __( 'Radio Button', 'acf' );
- $this->category = 'choice';
- $this->defaults = array(
- 'layout' => 'vertical',
- 'choices' => array(),
- 'default_value' => '',
- 'other_choice' => 0,
- 'save_other_choice' => 0,
- 'allow_null' => 0,
- 'return_format' => 'value',
- );
- }
-
- function render_field( $field ) {
-
- $e = '';
- $ul = array(
- 'class' => 'acf-radio-list',
- 'data-allow_null' => $field['allow_null'],
- 'data-other_choice' => $field['other_choice'],
- );
-
- $ul['class'] .= ' ' . ( $field['layout'] == 'horizontal' ? 'acf-hl' : 'acf-bl' );
- $ul['class'] .= ' ' . $field['class'];
-
- $value = (string) $field['value'];
-
- if ( isset( $field['choices'][ $value ] ) ) {
- $checked = (string) $value;
-
- } elseif ( $field['other_choice'] && $value !== '' ) {
- $checked = 'other';
-
- } elseif ( $field['allow_null'] ) {
- $checked = '';
-
- } else {
- $checked = (string) key( $field['choices'] );
- }
-
- $other_input = false;
- if ( $field['other_choice'] ) {
-
- $other_input = array(
- 'type' => 'text',
- 'name' => $field['name'],
- 'value' => '',
- 'disabled' => 'disabled',
- 'class' => 'acf-disabled',
- );
-
- if ( $checked === 'other' ) {
- unset( $other_input['disabled'] );
- $other_input['value'] = $field['value'];
- }
-
- if ( ! isset( $field['choices']['other'] ) ) {
- $field['choices']['other'] = '';
- }
- }
-
- if ( empty( $field['choices'] ) ) {
- return;
- }
-
- $e .= acf_get_hidden_input( array( 'name' => $field['name'] ) );
-
- $e .= '<ul ' . acf_esc_attr( $ul ) . '>';
-
- foreach ( $field['choices'] as $value => $label ) {
- $is_selected = false;
-
- $value = (string) $value;
-
- $attrs = array(
- 'type' => 'radio',
- 'id' => sanitize_title( $field['id'] . '-' . $value ),
- 'name' => $field['name'],
- 'value' => $value,
- );
-
- if ( esc_attr( $value ) === esc_attr( $checked ) ) {
- $attrs['checked'] = 'checked';
- $is_selected = true;
- }
-
- if ( isset( $field['disabled'] ) && acf_in_array( $value, $field['disabled'] ) ) {
- $attrs['disabled'] = 'disabled';
- }
-
- $additional_html = '';
- if ( $value === 'other' && $other_input ) {
- $additional_html = ' ' . acf_get_text_input( $other_input );
- }
-
- $e .= '<li><label' . ( $is_selected ? ' class="selected"' : '' ) . '><input ' . acf_esc_attr( $attrs ) . '/>' . acf_esc_html( $label ) . '</label>' . $additional_html . '</li>';
- }
-
- $e .= '</ul>';
-
- echo $e;
- }
-
- function render_field_settings( $field ) {
-
- $field['choices'] = acf_encode_choices( $field['choices'] );
- acf_render_field_setting(
- $field,
- array(
- 'label' => __( 'Choices', 'acf' ),
- 'instructions' => __( 'Enter each choice on a new line.', 'acf' ) . '<br />' . __( 'For more control, you may specify both a value and label like this:', 'acf' ) . '<br /><span class="acf-field-setting-example">' . __( 'red : Red', 'acf' ) . '</span>',
- 'type' => 'textarea',
- 'name' => 'choices',
- )
- );
- acf_render_field_setting(
- $field,
- array(
- 'label' => __( 'Default Value', 'acf' ),
- 'instructions' => __( 'Appears when creating a new post', 'acf' ),
- 'type' => 'text',
- 'name' => 'default_value',
- )
- );
- acf_render_field_setting(
- $field,
- array(
- 'label' => __( 'Return Value', 'acf' ),
- 'instructions' => __( 'Specify the returned value on front end', 'acf' ),
- 'type' => 'radio',
- 'name' => 'return_format',
- 'layout' => 'horizontal',
- 'choices' => array(
- 'value' => __( 'Value', 'acf' ),
- 'label' => __( 'Label', 'acf' ),
- 'array' => __( 'Both (Array)', 'acf' ),
- ),
- )
- );
- }
-
- function render_field_validation_settings( $field ) {
- acf_render_field_setting(
- $field,
- array(
- 'label' => __( 'Allow Null?', 'acf' ),
- 'instructions' => '',
- 'name' => 'allow_null',
- 'type' => 'true_false',
- 'ui' => 1,
- )
- );
- acf_render_field_setting(
- $field,
- array(
- 'label' => __( 'Allow Other Choice', 'acf' ),
- 'name' => 'other_choice',
- 'type' => 'true_false',
- 'ui' => 1,
- 'instructions' => __( "Add 'other' choice to allow for custom values", 'acf' ),
- )
- );
- acf_render_field_setting(
- $field,
- array(
- 'label' => __( 'Save Other Choice', 'acf' ),
- 'name' => 'save_other_choice',
- 'type' => 'true_false',
- 'ui' => 1,
- 'instructions' => __( "Save 'other' values to the field's choices", 'acf' ),
- 'conditions' => array(
- 'field' => 'other_choice',
- 'operator' => '==',
- 'value' => 1,
- ),
- )
- );
- }
-
- function render_field_presentation_settings( $field ) {
- acf_render_field_setting(
- $field,
- array(
- 'label' => __( 'Layout', 'acf' ),
- 'instructions' => '',
- 'type' => 'radio',
- 'name' => 'layout',
- 'layout' => 'horizontal',
- 'choices' => array(
- 'vertical' => __( 'Vertical', 'acf' ),
- 'horizontal' => __( 'Horizontal', 'acf' ),
- ),
- )
- );
- }
-
- function update_field( $field ) {
-
- $field['choices'] = acf_decode_choices( $field['choices'] );
-
- return $field;
- }
-
- function update_value( $value, $post_id, $field ) {
-
- if ( ! $value && ! is_numeric( $value ) ) {
- return $value;
- }
-
- if ( $field['save_other_choice'] ) {
-
- if ( ! isset( $field['choices'][ $value ] ) ) {
-
-
- $selector = $field['ID'] ? $field['ID'] : $field['key'];
- $field = acf_get_field( $selector );
-
- if ( ! $field['ID'] ) {
- return $value;
- }
-
- $value = wp_unslash( $value );
-
- $value = sanitize_text_field( $value );
-
- $field['choices'][ $value ] = $value;
-
- acf_update_field( $field );
- }
- }
-
- return $value;
- }
-
- function load_value( $value, $post_id, $field ) {
-
- if ( is_array( $value ) ) {
- $value = array_pop( $value );
- }
-
- return $value;
- }
-
- function translate_field( $field ) {
- return acf_get_field_type( 'select' )->translate_field( $field );
- }
-
- function format_value( $value, $post_id, $field ) {
- return acf_get_field_type( 'select' )->format_value( $value, $post_id, $field );
- }
-
- function get_rest_schema( array $field ) {
- $schema = parent::get_rest_schema( $field );
- if ( isset( $field['default_value'] ) && '' !== $field['default_value'] ) {
- $schema['default'] = $field['default_value'];
- }
-
- if ( ! empty( $field['other_choice'] ) ) {
- return $schema;
- }
-
- $radio_keys = array_diff(
- array_keys( $field['choices'] ),
- array_values( $field['choices'] )
- );
- $schema['enum'] = empty( $radio_keys ) ? $field['choices'] : $radio_keys;
- if ( ! empty( $field['allow_null'] ) ) {
- $schema['enum'][] = null;
- }
- return $schema;
- }
- }
-
- acf_register_field_type( 'acf_field_radio' );
- endif;
|