function filter_terms()

in web/wp-content/plugins/acf-extended/includes/fields/field-taxonomy-terms.php [1210:1391]


    function filter_terms($all_terms, $field){
        
        if(empty($field['taxonomy']) && empty($field['allow_terms'])){
            
            $terms = wp_list_pluck($all_terms, 'term_id');
            $terms = array_unique($terms);
            
            $choices = $this->convert_terms_to_choices($terms, $field);
            
            return $choices;
            
        }
        
        $terms = array();
        
        // Filter taxonomy terms
        if(!empty($field['taxonomy'])){
            
            $allowed_tax_terms = array();
            
            foreach($all_terms as $term){
                
                if(in_array($term->taxonomy, $field['taxonomy'])){
                    $allowed_tax_terms[] = $term;
                }
                
            }
            
            $all_terms = $allowed_tax_terms;
            
        }
        
        if(empty($field['allow_terms'])){
            
            $terms = $all_terms;
            
        // Filter allowed terms
        }else{
            
            // Add term level
            foreach($all_terms as $term_id => &$_term){
                
                $level = acfe_get_term_level($_term->term_id, $_term->taxonomy);
                $_term->level = $level;
                
            }
            
            foreach($field['allow_terms'] as $id){
                
                // All terms
                if(acfe_starts_with($id, 'all_')){
                    
                    $taxonomy = substr($id, 4);
                    
                    $level = false;
                    
                    if(stripos($taxonomy, '|') !== false){
                        
                        $level = explode('|', $taxonomy);
                        $taxonomy = $level[0];
                        $level = $level[1];
                        
                    }
                    
                    if(!empty($field['taxonomy']) && !in_array($taxonomy, $field['taxonomy'])){
                        continue;
                    }
                    
                    $keep = array();
                    
                    if($level){
                        
                        foreach($all_terms as $all_term){
                            if((int) $all_term->level === (int) $level && $all_term->taxonomy === $taxonomy){
                                $keep[] = $all_term;
                            }
                        }
                        
                    }else{
                        
                        foreach($all_terms as $all_term){
                            if($all_term->taxonomy === $taxonomy){
                                $keep[] = $all_term;
                            }
                        }
                        
                    }
                    
                    $terms = array_merge($terms, $keep);
                    
                }
                
                // Terms all childs
                elseif(acfe_ends_with($id, '_all_childs')){
                    
                    $term_id = substr($id, 0, -11);
                    $term = get_term($term_id);
                    $taxonomy = $term->taxonomy;
                    
                    if(!empty($field['taxonomy']) && !in_array($taxonomy, $field['taxonomy'])){
                        continue;
                    }
                    
                    $keep = array();
                    
                    foreach($all_terms as $all_term){
                        
                        if($all_term->taxonomy === $taxonomy){
                            
                            $term_childs = get_term_children($term_id, $taxonomy);
                            
                            if(in_array($all_term->term_id, $term_childs)){
                                $keep[] = $all_term;
                            }
                            
                        }
                        
                    }
                    
                    // sort into hierachial order
                    if(is_taxonomy_hierarchical($taxonomy)){
                        $keep = _get_term_children($id, $keep, $taxonomy);
                        $keep = acf_get_array($keep);
                    }
                    
                    $terms = array_merge($terms, $keep);
                    
                }
                
                // Terms direct childs
                elseif(acfe_ends_with($id, '_childs')){
                    
                    $term_id = substr($id, 0, -7);
                    $term = get_term($term_id);
                    $taxonomy = $term->taxonomy;
                    
                    if(!empty($field['taxonomy']) && !in_array($taxonomy, $field['taxonomy'])){
                        continue;
                    }
                    
                    $keep = array();
                    
                    foreach($all_terms as $all_term){
                        if((int) $all_term->parent === (int) $term_id && $all_term->taxonomy === $taxonomy){
                            $keep[] = $all_term;
                        }
                    }
                    
                    $terms = array_merge($terms, $keep);
                    
                }
                
                // Term
                else{
                    
                    $term = get_term($id);
                    $taxonomy = $term->taxonomy;
                    
                    if(!empty($field['taxonomy']) && !in_array($taxonomy, $field['taxonomy'])){
                        continue;
                    }
                    
                    $keep = array();
                    $keep[] = $term;
                    
                    $terms = array_merge($terms, $keep);
                    
                }
                
            }
            
        }
        
        $terms = wp_list_pluck($terms, 'term_id');
        
        $terms = array_unique($terms);
        
        $choices = $this->convert_terms_to_choices($terms, $field);
        
        return $choices;
        
    }