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

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

Download   Open   Edit   Advanced Editor   Back

<?php

if (!class_exists('acf_field_checkbox')) {
    class acf_field_checkbox extends acf_field
    {
        function initialize()
        {
            $this->name = 'checkbox';
            $this->label = __('Checkbox', 'acf');
            $this->category = 'choice';
            $this->defaults = array('layout' => 'vertical', 'choices' => array(), 'default_value' => '', 'allow_custom' => 0, 'save_custom' => 0, 'toggle' => 0, 'return_format' => 'value');
        }
        function render_field($field)
        {
            $this->_values = array();
            $this->_all_checked = true;
            $field['value'] = acf_get_array($field['value']);
            $field['choices'] = acf_get_array($field['choices']);
            acf_hidden_input(array('name' => $field['name']));
            $li = '';
            $ul = array('class' => 'acf-checkbox-list');
            $ul['class'] .= ' ' . ($field['layout'] == 'horizontal' ? 'acf-hl' : 'acf-bl');
            $ul['class'] .= ' ' . $field['class'];
            $field['name'] .= '[]';
            if (!empty($field['choices'])) {
                $li .= $this->render_field_choices($field);
                if ($field['toggle']) {
                    $li = $this->render_field_toggle($field) . $li;
                }
            }
            if ($field['allow_custom']) {
                $li .= $this->render_field_custom($field);
            }
            echo '<ul ' . acf_esc_attr($ul) . '>' . "\n" . $li . '</ul>' . "\n";
        }
        function render_field_choices($field)
        {
            return $this->walk($field['choices'], $field);
        }
        function render_field_toggle($field)
        {
            $atts = array('type' => 'checkbox', 'class' => 'acf-checkbox-toggle', 'label' => __('Toggle All', 'acf'));
            if (is_string($field['toggle'])) {
                $atts['label'] = $field['toggle'];
            }
            if ($this->_all_checked) {
                $atts['checked'] = 'checked';
            }
            return '<li>' . acf_get_checkbox_input($atts) . '</li>' . "\n";
        }
        function render_field_custom($field)
        {
            $html = '';
            foreach ($field['value'] as $value) {
                if (isset($field['choices'][$value])) {
                    continue;
                }
                $esc_value = esc_attr($value);
                $text_input = array('name' => $field['name'], 'value' => $value);
                if (in_array($esc_value, $this->_values)) {
                    continue;
                }
                $html .= '<li><input class="acf-checkbox-custom" type="checkbox" checked="checked" />' . acf_get_text_input($text_input) . '</li>' . "\n";
            }
            $html .= '<li><a href="#" class="button acf-add-checkbox">' . esc_attr__('Add new choice', 'acf') . '</a></li>' . "\n";
            return $html;
        }
        function walk($choices = array(), $args = array(), $depth = 0)
        {
            if (empty($choices)) {
                return '';
            }
            $args = wp_parse_args($args, array('id' => '', 'type' => 'checkbox', 'name' => '', 'value' => array(), 'disabled' => array()));
            $html = '';
            if ($depth == 0) {
                $args['value'] = array_map('esc_attr', $args['value']);
                $args['disabled'] = array_map('esc_attr', $args['disabled']);
            }
            foreach ($choices as $value => $label) {
                $html .= '<li>';
                if (is_array($label)) {
                    $html .= '<ul>' . "\n";
                    $html .= $this->walk($label, $args, $depth + 1);
                    $html .= '</ul>';
                } else {
                    $esc_value = esc_attr($value);
                    $atts = array('id' => $args['id'] . '-' . str_replace(' ', '-', $value), 'type' => $args['type'], 'name' => $args['name'], 'value' => $value, 'label' => $label);
                    if (in_array($esc_value, $args['value'])) {
                        $atts['checked'] = 'checked';
                    } else {
                        $this->_all_checked = false;
                    }
                    if (in_array($esc_value, $args['disabled'])) {
                        $atts['disabled'] = 'disabled';
                    }
                    $this->_values[] = $esc_value;
                    $html .= acf_get_checkbox_input($atts);
                }
                $html .= '</li>' . "\n";
            }
            return $html;
        }
        function render_field_settings($field)
        {
            $field['choices'] = acf_encode_choices($field['choices']);
            $field['default_value'] = acf_encode_choices($field['default_value'], false);
            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 Custom', 'acf'), 'instructions' => '', 'name' => 'allow_custom', 'type' => 'true_false', 'ui' => 1, 'message' => __("Allow 'custom' values to be added", 'acf')));
            acf_render_field_setting($field, array('label' => __('Save Custom', 'acf'), 'instructions' => '', 'name' => 'save_custom', 'type' => 'true_false', 'ui' => 1, 'message' => __("Save 'custom' values to the field's choices", 'acf'), 'conditions' => array('field' => 'allow_custom', 'operator' => '==', 'value' => 1)));
            acf_render_field_setting($field, array('label' => __('Default Value', 'acf'), 'instructions' => __('Enter each default value on a new line', 'acf'), 'type' => 'textarea', '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' => __('Toggle', 'acf'), 'instructions' => __('Prepend an extra checkbox to toggle all choices', 'acf'), 'name' => 'toggle', 'type' => 'true_false', 'ui' => 1));
            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']);
            $field['default_value'] = acf_decode_choices($field['default_value'], true);
            return $field;
        }
        function update_value($value, $post_id, $field)
        {
            if (empty($value)) {
                return $value;
            }
            $value = acf_get_field_type('select')->update_value($value, $post_id, $field);
            if ($field['save_custom']) {
                $selector = $field['ID'] ? $field['ID'] : $field['key'];
                $field = acf_get_field($selector);
                if (!$field) {
                    return false;
                }
                if (!$field['ID']) {
                    return $value;
                }
                foreach ($value as $v) {
                    if (isset($field['choices'][$v])) {
                        continue;
                    }
                    $v = wp_unslash($v);
                    $v = sanitize_text_field($v);
                    $field['choices'][$v] = $v;
                }
                acf_update_field($field);
            }
            return $value;
        }
        function translate_field($field)
        {
            return acf_get_field_type('select')->translate_field($field);
        }
        function format_value($value, $post_id, $field)
        {
            if (acf_is_empty($value)) {
                return array();
            }
            $value = acf_array($value);
            return acf_get_field_type('select')->format_value($value, $post_id, $field);
        }
    }
    acf_register_field_type('acf_field_checkbox');
}