in web/wp-content/plugins/acf-extended/includes/modules/form/module-form-fields.php [697:955]
function ajax_select_choices(){
$nonce = acf_request_arg('nonce', '');
$key = acf_request_arg('field_key', '');
// Back-compat for field settings.
if(!acf_is_field_key($key)){
$nonce = '';
$key = '';
}
// validate
if(!acf_verify_ajax($nonce, $key)){
die();
}
// default options
$options = acf_parse_args($_POST, array(
'post_id' => 0,
's' => '',
'value' => '',
'choices' => array(),
'custom_choices' => array(),
'field_key' => '',
'field_label' => '',
'field_groups' => array(),
'is_load' => false,
'paged' => 1,
));
// vars
$is_load = (bool) $options['is_load'];
$results = array();
$values = array();
$search = null;
// search
if($options['s'] !== ''){
// strip slashes (search may be integer)
$search = strval($options['s']);
$search = wp_unslash($search);
}
// custom choices
if($options['custom_choices']){
$children = array();
foreach($options['custom_choices'] as $key => $value){
// ensure value is a string
$value = strval($value);
// append to collection
$values[] = $key;
// if searching, but doesn't exist
if(is_string($search) && stripos($value, $search) === false && stripos($key, $search) === false){
continue;
}
$children[] = array(
'id' => $key,
'text' => $value,
);
}
if($children){
$results[] = array(
'text' => __('Custom', 'acfe'),
'children' => $children
);
}
}
// generic choices
if($options['choices']){
$children = array();
$is_array = false;
foreach($options['choices'] as $key => $value){
// value is array (multi dimentional choices)
if(is_array($value)){
$is_array = true;
$sub_children = array();
foreach($value as $sub_key => $sub_value){
// ensure value is a string
$sub_value = strval($sub_value);
// append to collection
$values[] = $sub_key;
// if searching, but doesn't exist
if(is_string($search) && stripos($sub_value, $search) === false && stripos($sub_key, $search) === false){
continue;
}
$sub_children[] = array(
'id' => $sub_key,
'text' => $sub_value,
);
}
if($sub_children){
$results[] = array(
'text' => $key,
'children' => $sub_children
);
}
// normal value
}else{
// ensure value is a string
$value = strval($value);
// append to collection
$values[] = $key;
// if searching, but doesn't exist
if(is_string($search) && stripos($value, $search) === false && stripos($key, $search) === false){
continue;
}
$children[] = array(
'id' => $key,
'text' => $value,
);
}
}
if(!$is_array && $children){
$results[] = array(
'text' => $options['field_label'],
'children' => $children
);
}
}
// field groups fields
foreach($options['field_groups'] as $field_group_key){
// vars
$field_group = acf_get_field_group($field_group_key);
// get fields
$fields = $this->get_fields($field_group_key);
$children = array();
foreach($fields as $field){
$key = $is_load ? $field['key'] : "{field:{$field['key']}}";
$value = $field['field']['label'];
// ensure value is a string
$value = strval($value);
$value = empty($value) ? $field['key'] : "{$value} ({$field['key']})";
// append to collection
$values[] = $key;
// if searching, but doesn't exist
if(is_string($search) && stripos($value, $search) === false && stripos($key, $search) === false){
continue;
}
$children[] = array(
'id' => $key,
'text' => $field['label'],
);
}
if($children){
$results[] = array(
'text' => $field_group['title'],
'children' => $children
);
}
}
// custom value
$children = array();
$value_array = acf_get_array($options['value']);
$value_array[] = ''; // we must append empty string to let search being a custom value (in case a value was never selected before)
foreach($value_array as $value){
$key = $value;
$value = strval($value);
// if search
if(is_string($search)){
// search already exists in custom values
if($search === $value && !in_array($value, $values)){
$children[] = array(
'id' => $key,
'text' => $this->get_field_label($value)
);
// search not found in any value, generate choice
}elseif(!in_array($search, $values)){
$_key = $search;
$_value = $search;
// reset children to avoid duplicate append
$children = array();
$children[] = array(
'id' => $_key,
'text' => $this->get_field_label($_value)
);
}
// no search and value not found in choices, generate custom choice
}elseif($value && !in_array($value, $values)){
$children[] = array(
'id' => $key,
'text' => $this->get_field_label($value)
);
}
}
if($children){
array_unshift($results, array(
'text' => __('Custom', 'acfe'),
'children' => $children
));
}
// response
$response = array(
'results' => $results,
);
// return
acf_send_ajax_results($response);
}