function load_action()

in web/wp-content/plugins/acf-extended/includes/modules/form/module-form-action-post.php [71:254]


    function load_action($form, $action){
        
        // check source
        if(!$action['load']['source']){
            return $form;
        }
        
        // apply template tags
        acfe_apply_tags($action['load']['source'], array('context' => 'load', 'format' => false));
        
        // vars
        $load = $action['load'];
        $post_id = acf_extract_var($load, 'source');
        $post_thumbnail = acf_extract_var($load, 'post_thumbnail');
        $post_terms = acf_extract_var($load, 'post_terms');
        $acf_fields = acf_extract_var($load, 'acf_fields');
        $acf_fields = acf_get_array($acf_fields);
        $acf_fields_exclude = array();
        
        // filters
        $post_id = apply_filters("acfe/form/load_post_id",                          $post_id, $form, $action);
        $post_id = apply_filters("acfe/form/load_post_id/form={$form['name']}",     $post_id, $form, $action);
        $post_id = apply_filters("acfe/form/load_post_id/action={$action['name']}", $post_id, $form, $action);
        
        // bail early if no source
        if(!$post_id){
            return $form;
        }
        
        // get source post
        $post = get_post($post_id);
    
        // no post found
        if(!$post){
            return $form;
        }
        
        /**
         * load post fields
         *
         * $load = array(
         *     post_type    => 'field_655af3dd3bd56',
         *     post_status  => 'field_655af3dd3bd56',
         *     post_title   => 'field_655af3dd3bd56',
         *     post_name    => '',
         *     post_content => '',
         *     post_excerpt => '',
         *     post_author  => '',
         *     post_parent  => '',
         * )
         */
        foreach($load as $post_field => $field_key){
            
            // check field is not hidden and has no value set in 'acfe/form/load_form'
            if(acf_maybe_get($form['map'], $field_key) !== false && !isset($form['map'][ $field_key ]['value'])){
                
                // check key exists in WP_Post and is field key
                if(in_array($post_field, $this->fields) && !empty($field_key) && is_string($field_key) && acf_is_field_key($field_key)){
                    
                    // add field to excluded list
                    $acf_fields_exclude[] = $field_key;
                    
                    // assign post field as value
                    $form['map'][ $field_key ]['value'] = get_post_field($post_field, $post_id);
                    
                }
                
            }
            
        }
        
        // load post thumbnail
        if(!empty($post_thumbnail) && is_string($post_thumbnail) && acf_is_field_key($post_thumbnail)){
            
            // vars
            $field_key = $post_thumbnail;
            
            // check field is not hidden and has no value set in 'acfe/form/load_form'
            if(acf_maybe_get($form['map'], $field_key) !== false && !isset($form['map'][ $field_key ]['value'])){
        
                // add field to excluded list
                $acf_fields_exclude[] = $field_key;
                
                // get thumbnail
                $thumbnail_id = get_post_thumbnail_id($post_id);
        
                // map thumbnail value
                if($thumbnail_id){
                    $form['map'][ $field_key ]['value'] = $thumbnail_id;
                }
            
            }
        
        }
        
        // load post terms
        if(!empty($post_terms) && is_string($post_terms) && acf_is_field_key($post_terms)){
            
            // field key
            $field_key = $post_terms;
            
            // check field is not hidden and has no value set in 'acfe/form/load_form'
            if(acf_maybe_get($form['map'], $field_key) !== false && !isset($form['map'][ $field_key ]['value'])){
                
                // vars
                $terms = array();
        
                // add field to excluded list
                $acf_fields_exclude[] = $field_key;
                
                // get taxonomies
                $taxonomies = acf_get_taxonomies(array(
                    'post_type' => get_post_type($post_id)
                ));
        
                // loop
                foreach($taxonomies as $taxonomy){
            
                    // get taxonomy terms
                    $_terms = get_the_terms($post_id, $taxonomy);
                    
                    // validate
                    if($_terms && !is_wp_error($_terms)){
                        $terms = array_merge($terms, $_terms);
                    }
            
                }
        
                // map terms value
                if($terms){
                    $form['map'][ $field_key ]['value'] = wp_list_pluck($terms, 'term_id');
                }
            
            }
        
        }
    
        // load acf values
        $form = $this->load_acf_values($form, $post_id, $acf_fields, $acf_fields_exclude);
        
        // media field keys
        if($action['type'] === 'update_post'){
            
            // vars
            $field_keys = array();
            $all_fields = array_merge(array_values($load), array_values($acf_fields));
            
            // loop post fields
            foreach($all_fields as $field_key){
                
                // get field
                $field = acf_get_field($field_key);
                
                // check field type
                if($field && in_array($field['type'], array('file', 'image', 'gallery'))){
                    $field_keys[] = $field_key;
                }
                
            }
            
            // localize data
            if(!empty($field_keys)){
                
                add_filter('acfe/form/set_form_data', function($data, $displayed_form) use($post_id, $field_keys, $form){
                    
                    if($displayed_form['cid'] === $form['cid']){
                        $data['media'] = array(
                            'post_id' => $post_id,
                            'fields'  => $field_keys
                        );
                    }
                    
                    return $data;
                    
                }, 10, 2);
                
            }
            
        }
        
        // return
        return $form;
    
    }