function acfe_get_post_id_field_groups()

in web/wp-content/plugins/acf-extended/includes/acfe-field-group-functions.php [16:253]


function acfe_get_post_id_field_groups($post_id = 0){
    
    /**
     * @string  $post_id  12   | term_46 | user_22 | my-option | comment_89 | widget_56 | menu_74 | menu_item_96 | block_my-block | blog_55 | site_36 | attachment_24
     * @string  $id       12   | 46      | 22      | my-option | 89         | widget_56 | 74      | 96           | block_my-block | 55      | 36      | 24
     * @string  $type     post | term    | user    | option    | comment    | option    | term    | post         | block          | blog    | blog    | post
     */
    $post_id = acf_get_valid_post_id($post_id);
    
    // extract
    extract(acf_decode_post_id($post_id));
    
    /** @var $type */
    /** @var $id */
    
    // vars
    $field_groups = array();
    $post_type = '';
    $taxonomy = '';
    
    // check post id is attachment
    if($type === 'post' && get_post_type($id) === 'attachment'){
        $post_id = "attachment_{$id}";
    }
    
    // override attachment
    if($type === 'post' && acfe_starts_with($post_id, 'attachment_')){
        
        $type = 'attachment';
        
    // override menu
    }elseif($type === 'term' && acfe_starts_with($post_id, 'menu_')){
        
        $type = 'menu';
        
    // override user list
    }elseif($post_id === 'user_options'){
        
        $type = 'user_list';
        
    // override attachment list
    }elseif($post_id === 'attachment_options'){
        
        $type = 'attachment_list';
        
    // override post type list
    }elseif($type === 'option' && strpos($post_id, 'tax_') === false && strpos($post_id, '_options') !== false){
        
        // check post types for post_type_list
        $post_types = acf_get_post_types();
        $found = false;
        
        // loop
        foreach($post_types as $_post_type){
            
            if($post_id !== "{$_post_type}_options") continue;
    
            $found = $_post_type;
            break;
            
        }
        
        if($found){
            
            $post_type = $found;
            $type = 'post_type_list';
            
        }
        
    // override taxonomy list
    }elseif($type === 'option' && strpos($post_id, 'tax_') === 0 && strpos($post_id, '_options') !== false){
        
        // check taxonomies for taxonomy_list
        $taxonomies = acf_get_taxonomies();
        $found = false;
        
        // loop
        foreach($taxonomies as $_taxonomy){
            
            if($post_id !== "tax_{$_taxonomy}_options") continue;
    
            $found = $_taxonomy;
            break;
            
        }
        
        if($found){
            
            $taxonomy = $found;
            $type = 'taxonomy_list';
            
        }
    
    // override settings page
    }elseif($type === 'option' && in_array($post_id, array('options-general', 'options-writing', 'options-reading', 'options-discussion', 'options-media', 'options-permalink'))){
        
        $type = 'settings_page';
        
    }
    
    // user
    if($type === 'user'){
        
        $keys = array();
        
        foreach(array('edit', 'new') as $user_form){
            
            $_field_groups = acf_get_field_groups(array(
                'user_id'   => $id,
                'user_form' => $user_form,
            ));
            
            foreach($_field_groups as $_field_group){
                
                if(in_array($_field_group['key'], $keys)) continue;
                
                $keys[] = $_field_group['key'];
                $field_groups[] = $_field_group;
                
            }
            
        }
        
    // attachment
    }elseif($type === 'attachment'){
        
        $field_groups = acf_get_field_groups(array(
            'attachment_id' => $id,
            'attachment'    => $id,
        ));
        
    // taxonomy
    }elseif($type === 'term'){
        
        $term = get_term($id);
        
        if($term && !is_wp_error($term)){
            
            $taxonomy = $term->taxonomy;
            
            $field_groups = acf_get_field_groups(array(
                'taxonomy' => $taxonomy,
                'term_id' => $id,
            ));
            
        }
        
    // post type
    }elseif($type === 'post'){
        
        $post_type = get_post_type($post_id);
        
        $field_groups = acf_get_field_groups(array(
            'post_id'   => $post_id,
            'post_type' => $post_type,
        ));
        
    // options page
    }elseif($type === 'option'){
        
        // vars
        $keys = array();
        $options_pages = array();
        
        // get all options pages using the post id
        foreach(acf_get_options_pages() as $page){
            
            if($page['post_id'] !== $id) continue;
            
            $options_pages[] = $page;
            
        }
        
        foreach($options_pages as $page){
            
            $_field_groups = acf_get_field_groups(array(
                'options_page' => $page['menu_slug'],
            ));
            
            foreach($_field_groups as $_field_group){
                
                if(in_array($_field_group['key'], $keys)) continue;
                
                $keys[] = $_field_group['key'];
                $field_groups[] = $_field_group;
                
            }
            
        }
        
    // nav menu
    }elseif($type === 'menu'){
        
        $field_groups = acf_get_field_groups(array(
            'screen'  => 'nav_menu',
            'post_id' => $id,
        ));
        
    // user list
    }elseif($type === 'user_list'){
    
        $field_groups = acf_get_field_groups(array(
            'user_list' => 1,
        ));
        
    // attachment list
    }elseif($type === 'attachment_list'){
    
        $field_groups = acf_get_field_groups(array(
            'attachment_list' => 1,
        ));
        
    // post type list
    }elseif($type === 'post_type_list'){
    
        $field_groups = acf_get_field_groups(array(
            'post_type_list' => $post_type,
        ));
        
    // taxonomy list
    }elseif($type === 'taxonomy_list'){
    
        $field_groups = acf_get_field_groups(array(
            'taxonomy_list' => $taxonomy,
        ));
        
    // settings page
    }elseif($type === 'settings_page'){
    
        $field_groups = acf_get_field_groups(array(
            'wp_settings' => $post_id
        ));
        
    }
    
    return $field_groups;
    
}