function acfe_query_fields()

in web/wp-content/plugins/acf-extended/includes/acfe-field-functions.php [243:555]


function acfe_query_fields($args = array()){
    
    // vars
    $storage = array();
    $fields = array();
    
    // validate query
    $args = wp_parse_args($args, array(
        'query'    => array(),                  // main query, should be compatible with wp_list_filter()
        'context'  => acf_get_field_groups(),   // can be field/field group array, or field/field group key, array of fields or field groups etc...
        'orderby'  => false,                    // orderby list
        'order'    => 'ASC',                    // order list
        'limit'    => 0,                        // limit list
        'offset'   => 0,                        // offset list
        'level'    => -1,                       // maximum allowed field level (-1 = any, 0 = only top level, 1 = max 1 sub level etc...)
        'field'    => false,                    // list pluck field
        'filters'  => true,                     // enable/disable acf_filters (such as clone, local)
        
        // internal args
        '_query'   => false,
        '_depth'   => 0,
        '_level'   => 0,
        '_filters' => false,
    ));
    
    // validate context
    $args['context'] = acf_get_array($args['context']);
    
    // validate query
    $args['query'] = acf_get_array($args['query']);
    
    // top-level call
    if(!$args['_depth']){
        
        // disable acf filters
        if(!$args['filters']){
            $args['_filters'] = acf_disable_filters();
        }
        
    }
    
    // process query
    if($args['_query'] === false){
        
        $_query = array();

        /**
         * $_query = array('type' => 'text');
         
         * $_query = array(
         *     array('type' => 'text'),
         *     array('type' => 'image'),
         * );
         
         * $_query = array(
         *     'relation' => 'AND',
         *     array(
         *         'type' => 'text',
         *         'name' => 'my_text'
         *     )
         * );
         
         * $_query = array(
         *     array(
         *         'relation' => 'AND',
         *         array(
         *             'type' => 'text',
         *             'name' => 'my_text'
         *         )
         *     ),
         *     array(
         *         'relation' => 'AND',
         *         array(
         *             'type' => 'image',
         *             'name' => 'my_image'
         *         )
         *     ),
         * );
         */
        
        /**
         * $_query = array(
         *     array('type' => 'text'),
         *     array('type' => 'image'),
         * )
         */
        if(acf_is_associative_array($args['query'])){
        
            if(!isset($args['query']['relation'])){
            
                $_query[] = array(
                    'relation' => 'AND',
                    $args['query']
                );
            
            }else{
            
                if(isset($args['query'][0]) && is_array($args['query'][0])){
                    
                    $_query[] = array(
                        'relation' => $args['query']['relation'],
                        $args['query'][0]
                    );
                    
                }
            
            }
        
        }else{
        
            foreach($args['query'] as $q){
            
                if(!isset($q['relation'])){
                
                    if(isset($q[0]) && is_array($q[0])){
                    
                        if(!empty($q[0])){
                            $_query[] = array(
                                'relation' => 'AND',
                                current($q)
                            );
                        }
                    
                    }elseif(!empty($q)){
                    
                        $_query[] = array(
                            'relation' => 'AND',
                            $q
                        );
                    
                    }
                
                }else{
                
                    if(isset($q[0]) && is_array($q[0]) && !empty($q[0])){
                    
                        $_query[] = array(
                            'relation' => $q['relation'],
                            $q[0]
                        );
                    
                    }
                
                }
            
            }
        
        }
        
        // empty query = all
        if(empty($_query)){
            
            $_query[] = array(
                'relation' => 'AND',
                array()
            );
            
        }
        
        // assign
        $args['_query'] = $_query;
        
    }
    
    // $field
    // $field_group
    if(acf_is_associative_array($args['context'])){
        
        // field group
        if(acf_is_field_group($args['context'])){
            $fields = acf_get_fields($args['context']);
            
        // field
        }else{
            
            foreach($args['_query'] as $q){
                $storage = array_merge($storage, wp_list_filter(array($args['context']), $q[0], $q['relation']));
            }
            
            // query sub fields
            if(isset($args['context']['sub_fields'])){
                $args['_level']++;
                $fields = acf_get_fields($args['context']);
            }
            
        }
    
        if($fields){
            foreach($fields as $field){
    
                foreach($args['_query'] as $q){
                    $storage = array_merge($storage, wp_list_filter(array($field), $q[0], $q['relation']));
                }
            
                // query sub fields
                if(isset($field['sub_fields'])){
                    
                    if($args['level'] === -1 || ($args['level'] > 0 && $args['_level'] < $args['level'])){
    
                        // sub query
                        $_args = $args;
                        $_args['context'] = $field;
                        $_args['_depth']++;
    
                        $storage = array_merge($storage, acfe_query_fields($_args));
                        
                    }
                
                }
            
            }
        }
        
    // array(field_abcdef123456, field_abcdef123456)
    // array(group_abcdef123456, group_abcdef123456)
    // array($field, $field)
    // array($field_group, $field_group)
    }else{
        
        foreach($args['context'] as $context){
            
            // set new sub context
            $_args = $args;
            $_args['_depth']++;
            
            // array
            if(is_array($context)){
    
                $_args['context'] = $context;
                
            // numeric
            }elseif(is_numeric($context)){
                
                $post_type = get_post_type($context);
                
                if($post_type === 'acf-field-group'){
                    $_args['context'] = acf_get_field_group($context);
                    
                }elseif($post_type === 'acf-field'){
                    $_args['context'] = acf_get_field($context);
                }
                
            // string
            }else{
                
                // group_abcdef123456
                if(acf_is_field_group_key($context)){
                    $_args['context'] = acf_get_fields($context);
                    
                // field_abcdef123456
                }else{
                    $_args['context'] = acf_get_field($context);
                }
                
            }
            
            // loop query
            $storage = array_merge($storage, acfe_query_fields($_args));
            
        }
        
    }
    
    // unique array
    // make sure returned fields are unique (based on field key)
    $temp = array();
    $storage = array_filter($storage, function($field) use(&$temp){
        if(in_array($field['key'], $temp)){
            return false;
        }else{
            $temp[] = $field['key'];
            return true;
        }
    });
    
    // reorder
    $storage = array_values($storage);
    
    // top-level call
    if(!$args['_depth']){
        
        // order
        if($args['orderby']){
            $args['order'] = $args['order'] === 'ASC' ? 'ASC' : 'DESC';
            $storage = wp_list_sort($storage, $args['orderby'], $args['order']);
        }
        
        // field
        if($args['field']){
            $storage = wp_list_pluck($storage, $args['field']);
        }
        
        // offset
        if($args['offset'] > 0){
            $storage = array_slice($storage, $args['offset']);
        }
        
        // limit
        if($args['limit'] > 0){
            $storage = array_slice($storage, 0, $args['limit']);
        }
        
        // re-enable acf filters
        if(!$args['filters']){
            acf_enable_filters($args['_filters']);
        }
        
    }
    
    // return
    return $storage;
    
}