function acfe_get_orphan_meta()

in web/wp-content/plugins/acf-extended/includes/acfe-meta-functions.php [219:399]


function acfe_get_orphan_meta($post_id = 0){
    
    // validate post_id
    $post_id = acf_get_valid_post_id($post_id);
    
    // get meta
    $meta = acfe_get_meta($post_id);
    
    // allowed fields
    $allowed_fields = array();
    
    // allowed field groups
    $allowed_field_groups = acfe_get_post_id_field_groups($post_id);
    $allowed_field_groups = wp_list_pluck($allowed_field_groups, 'key');
    
    // collection
    $clones = array();
    
    // check clones
    foreach($meta as $row){
        
        // vars
        $field = $row['field'];
        $field_key = $row['key'];
        
        // get clone
        if(acf_maybe_get($field, 'type') === 'clone'){
            
            // add to collection
            $clones[] = $field;
            
        // get clone seamless sub field: field_123456abcdef_field_123456abcfed
        }elseif(substr_count($field_key, 'field_') > 1){
            
            // explode field_xxxxxxxxxxx
            $_clones = explode('_field_', $field_key);
    
            foreach($_clones as $_clone_key){
                
                // prepend 'field_'
                if(strpos($_clone_key, 'field_') !== 0){
                    $_clone_key = "field_{$_clone_key}";
                }
        
                // get clone field
                $clone = acf_get_field($_clone_key);
        
                // check type & add to collection
                if(acf_maybe_get($clone, 'type') === 'clone'){
                    $clones[] = $clone;
                }
        
            }
            
        }
        
    }
    
    // process clones
    $run = true;
    
    while($run){
        
        $run = false;
        
        foreach(array_keys($clones) as $i){
            
            // get field
            $field = $clones[$i];
            
            // get field group
            $field_group = acfe_get_field_group_from_field($field);
            
            // conditions
            $is_allowed_field = $field && in_array($field['key'], $allowed_fields);
            $is_allowed_field_group = $field_group && in_array($field_group['key'], $allowed_field_groups);
            
            // field or field group found in allowed list
            if($is_allowed_field || $is_allowed_field_group){
                
                // get cloned fields/field groups and sub fields to allowed list
                foreach(acf_get_array($field['clone']) as $cloned_key){
                    
                    // group_60e52a459bea4
                    if(acf_is_field_group_key($cloned_key)){
                        
                        $allowed_field_groups[] = $cloned_key;
                        
                    // field_60e242edd72e7
                    }elseif(acf_is_field_key($cloned_key)){
                        
                        $allowed_fields[] = $cloned_key;
                        
                        // get field group from cloned field
                        $clone_field_group = acfe_get_field_group_from_field($cloned_key);
    
                        // add field group in the allowed list
                        if($clone_field_group){
                            $allowed_field_groups[] = $clone_field_group['key'];
                        }
                        
                        // todo: enhance logic to only allow sub field of the targeted field
                        
                        // $is_enabled = acf_is_filter_enabled('clone');
                        //
                        // if($is_enabled){
                        //     acf_disable_filter('clone');
                        // }
                        //
                        // // also allow descendants in case of repeater, flexible content or group
                        // $descendants = acfe_get_field_descendants($cloned_key);
                        // $allowed_fields = array_merge($allowed_fields, $descendants);
                        //
                        // if($is_enabled){
                        //     acf_enable_filter('clone');
                        // }
                        
                        
                    }
                    
                }
                
                // remove from collection
                unset($clones[$i]);
                
                // run again
                $run = true;
                
            }
            
        }
        
    }
    
    // remove duplicated entries
    $allowed_fields = array_unique($allowed_fields);
    $allowed_field_groups = array_unique($allowed_field_groups);
    
    // orphan collection
    $orphan = array();
    
    foreach($meta as $row){
        
        // vars
        $field = $row['field'];
        $field_group = $row['field_group'];
        
        // field doesn't exist
        if(!$field){
    
            $orphan[] = $row;
            continue;
            
        }
        
        // field group doesn't exist
        if(!$field_group){
    
            $orphan[] = $row;
            continue;
            
        }
        
        // conditions
        $is_allowed_field = in_array($field['key'], $allowed_fields);
        $is_allowed_field_group = in_array($field_group['key'], $allowed_field_groups);
        
        // field is not allowed
        if(!$is_allowed_field && !$is_allowed_field_group){
    
            $orphan[] = $row;
            continue;
            
        }
        
    }
    
    // return
    return $orphan;
    
}