File "class-acf-field-select.php"
Full path: /home/kosmetik/public_html/wp-content/plugins/advanced-custom-fields/includes/fields/class-acf-field-select.php
File
size: 10.06 B
MIME-type: text/x-php
Charset: utf-8
Download Open Edit Advanced Editor Back
<?php
if (!class_exists('acf_field_select')) {
class acf_field_select extends acf_field
{
function initialize()
{
$this->name = 'select';
$this->label = _x('Select', 'noun', 'acf');
$this->category = 'choice';
$this->defaults = array('multiple' => 0, 'allow_null' => 0, 'choices' => array(), 'default_value' => '', 'ui' => 0, 'ajax' => 0, 'placeholder' => '', 'return_format' => 'value');
add_action('wp_ajax_acf/fields/select/query', array($this, 'ajax_query'));
add_action('wp_ajax_nopriv_acf/fields/select/query', array($this, 'ajax_query'));
}
function input_admin_enqueue_scripts()
{
if (!acf_get_setting('enqueue_select2')) {
return;
}
global $wp_scripts, $wp_styles;
$min = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
$major = acf_get_setting('select2_version');
$version = '';
$script = '';
$style = '';
if (isset($wp_scripts->registered['select2'])) {
$major = (int) $wp_scripts->registered['select2']->ver;
}
if ($major == 4) {
$version = '4.0';
$script = acf_get_url("assets/inc/select2/4/select2.full{$min}.js");
$style = acf_get_url("assets/inc/select2/4/select2{$min}.css");
} else {
$version = '3.5.2';
$script = acf_get_url("assets/inc/select2/3/select2{$min}.js");
$style = acf_get_url('assets/inc/select2/3/select2.css');
}
wp_enqueue_script('select2', $script, array('jquery'), $version);
wp_enqueue_style('select2', $style, '', $version);
acf_localize_data(array('select2L10n' => array('matches_1' => _x('One result is available, press enter to select it.', 'Select2 JS matches_1', 'acf'), 'matches_n' => _x('%d results are available, use up and down arrow keys to navigate.', 'Select2 JS matches_n', 'acf'), 'matches_0' => _x('No matches found', 'Select2 JS matches_0', 'acf'), 'input_too_short_1' => _x('Please enter 1 or more characters', 'Select2 JS input_too_short_1', 'acf'), 'input_too_short_n' => _x('Please enter %d or more characters', 'Select2 JS input_too_short_n', 'acf'), 'input_too_long_1' => _x('Please delete 1 character', 'Select2 JS input_too_long_1', 'acf'), 'input_too_long_n' => _x('Please delete %d characters', 'Select2 JS input_too_long_n', 'acf'), 'selection_too_long_1' => _x('You can only select 1 item', 'Select2 JS selection_too_long_1', 'acf'), 'selection_too_long_n' => _x('You can only select %d items', 'Select2 JS selection_too_long_n', 'acf'), 'load_more' => _x('Loading more results…', 'Select2 JS load_more', 'acf'), 'searching' => _x('Searching…', 'Select2 JS searching', 'acf'), 'load_fail' => _x('Loading failed', 'Select2 JS load_fail', 'acf'))));
}
function ajax_query()
{
if (!acf_verify_ajax()) {
die;
}
$response = $this->get_ajax_query($_POST);
acf_send_ajax_results($response);
}
function get_ajax_query($options = array())
{
$options = acf_parse_args($options, array('post_id' => 0, 's' => '', 'field_key' => '', 'paged' => 1));
$field = acf_get_field($options['field_key']);
if (!$field) {
return false;
}
$choices = acf_get_array($field['choices']);
if (empty($field['choices'])) {
return false;
}
$results = array();
$s = null;
if ($options['s'] !== '') {
$s = strval($options['s']);
$s = wp_unslash($s);
}
foreach ($field['choices'] as $k => $v) {
$v = strval($v);
if (is_string($s) && stripos($v, $s) === false) {
continue;
}
$results[] = array('id' => $k, 'text' => $v);
}
$response = array('results' => $results);
return $response;
}
function render_field($field)
{
$value = acf_get_array($field['value']);
$choices = acf_get_array($field['choices']);
if (empty($field['placeholder'])) {
$field['placeholder'] = _x('Select', 'verb', 'acf');
}
if (empty($value)) {
$value = array('');
}
if ($field['allow_null'] && !$field['multiple']) {
$choices = array('' => "- {$field['placeholder']} -") + $choices;
}
if ($field['ui'] && $field['ajax']) {
$minimal = array();
foreach ($value as $key) {
if (isset($choices[$key])) {
$minimal[$key] = $choices[$key];
}
}
$choices = $minimal;
}
$select = array('id' => $field['id'], 'class' => $field['class'], 'name' => $field['name'], 'data-ui' => $field['ui'], 'data-ajax' => $field['ajax'], 'data-multiple' => $field['multiple'], 'data-placeholder' => $field['placeholder'], 'data-allow_null' => $field['allow_null']);
if ($field['multiple']) {
$select['multiple'] = 'multiple';
$select['size'] = 5;
$select['name'] .= '[]';
if ($field['ui']) {
$select['size'] = 1;
}
}
if (!empty($field['readonly'])) {
$select['readonly'] = 'readonly';
}
if (!empty($field['disabled'])) {
$select['disabled'] = 'disabled';
}
if (!empty($field['ajax_action'])) {
$select['data-ajax_action'] = $field['ajax_action'];
}
if ($field['multiple'] || $field['ui']) {
acf_hidden_input(array('id' => $field['id'] . '-input', 'name' => $field['name']));
}
$select['value'] = $value;
$select['choices'] = $choices;
acf_select_input($select);
}
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'), 'name' => 'choices', 'type' => 'textarea'));
acf_render_field_setting($field, array('label' => __('Default Value', 'acf'), 'instructions' => __('Enter each default value on a new line', 'acf'), 'name' => 'default_value', 'type' => 'textarea'));
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' => __('Select multiple values?', 'acf'), 'instructions' => '', 'name' => 'multiple', 'type' => 'true_false', 'ui' => 1));
acf_render_field_setting($field, array('label' => __('Stylised UI', 'acf'), 'instructions' => '', 'name' => 'ui', 'type' => 'true_false', 'ui' => 1));
acf_render_field_setting($field, array('label' => __('Use AJAX to lazy load choices?', 'acf'), 'instructions' => '', 'name' => 'ajax', 'type' => 'true_false', 'ui' => 1, 'conditions' => array('field' => 'ui', 'operator' => '==', 'value' => 1)));
acf_render_field_setting($field, array('label' => __('Return Format', 'acf'), 'instructions' => __('Specify the value returned', 'acf'), 'type' => 'select', 'name' => 'return_format', 'choices' => array('value' => __('Value', 'acf'), 'label' => __('Label', 'acf'), 'array' => __('Both (Array)', 'acf'))));
}
function load_value($value, $post_id, $field)
{
if ($field['multiple']) {
if (acf_is_empty($value)) {
return array();
}
return acf_array($value);
}
return acf_unarray($value);
}
function update_field($field)
{
$field['choices'] = acf_decode_choices($field['choices']);
$field['default_value'] = acf_decode_choices($field['default_value'], true);
if (!$field['multiple']) {
$field['default_value'] = acf_unarray($field['default_value']);
}
return $field;
}
function update_value($value, $post_id, $field)
{
if (empty($value)) {
return $value;
}
if (is_array($value)) {
$value = array_map('strval', $value);
}
return $value;
}
function translate_field($field)
{
$field['choices'] = acf_translate($field['choices']);
return $field;
}
function format_value($value, $post_id, $field)
{
if (is_array($value)) {
foreach ($value as $i => $val) {
$value[$i] = $this->format_value_single($val, $post_id, $field);
}
} else {
$value = $this->format_value_single($value, $post_id, $field);
}
return $value;
}
function format_value_single($value, $post_id, $field)
{
if (acf_is_empty($value)) {
return $value;
}
$label = acf_maybe_get($field['choices'], $value, $value);
if ($field['return_format'] == 'value') {
} elseif ($field['return_format'] == 'label') {
$value = $label;
} elseif ($field['return_format'] == 'array') {
$value = array('value' => $value, 'label' => $label);
}
return $value;
}
}
acf_register_field_type('acf_field_select');
}