function ajax_field_groups_choices()

in web/wp-content/plugins/acf-extended/includes/modules/form/module-form-fields.php [392:564]


    function ajax_field_groups_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'      => '',
            'paged'          => 1,
        ));
    
        // vars
        $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
                );
            }
        
        }
    
        // field groups
        $children = array();
        
        foreach(acf_get_field_groups() as $field_group){
            
            // vars
            $key = $field_group['key'];
            $value = $field_group['title'];
    
            // 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'     => 'Field Groups',
                'children' => $children
            );
        }
    
        // custom value
        $children = array();
        $value_array = acf_get_array($options['value']);
        $value_array[] = '';
        
        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);
        
    }