File "class-acf-field-radio.php"

Full path: /home/kosmetik/public_html/wp-content/plugins/advanced-custom-fields/includes/fields/class-acf-field-radio.php
File size: 6.46 B
MIME-type: text/x-php
Charset: utf-8

Download   Open   Edit   Advanced Editor   Back

<?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 /><br />' . __('For more control, you may specify both a value and label like this:', 'acf') . '<br /><br />' . __('red : Red', 'acf'), 'type' => 'textarea', 'name' => 'choices'));
            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' => __('Other', 'acf'), 'instructions' => '', 'name' => 'other_choice', 'type' => 'true_false', 'ui' => 1, 'message' => __("Add 'other' choice to allow for custom values", 'acf')));
            acf_render_field_setting($field, array('label' => __('Save Other', 'acf'), 'instructions' => '', 'name' => 'save_other_choice', 'type' => 'true_false', 'ui' => 1, 'message' => __("Save 'other' values to the field's choices", 'acf'), 'conditions' => array('field' => 'other_choice', 'operator' => '==', 'value' => 1)));
            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' => __('Layout', 'acf'), 'instructions' => '', 'type' => 'radio', 'name' => 'layout', 'layout' => 'horizontal', 'choices' => array('vertical' => __('Vertical', 'acf'), 'horizontal' => __('Horizontal', 'acf'))));
            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 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, true);
                    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);
        }
    }
    acf_register_field_type('acf_field_radio');
}