/var/www/html/wp-content/plugins/elementor/app/modules/import-export/runners/import/wp-content.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php

namespace Elementor\App\Modules\ImportExport\Runners\Import;

use 
Elementor\App\Modules\ImportExport\Utils as ImportExportUtils;
use 
Elementor\Core\Utils\ImportExport\WP_Import;

class 
Wp_Content extends Import_Runner_Base {

    private 
$import_session_id;

    
/**
     * @var array
     */
    
private $selected_custom_post_types = [];

    public static function 
get_name() : string {
        return 
'wp-content';
    }

    public function 
should_import( array $data ) {
        return (
            isset( 
$data['include'] ) &&
            
in_array'content'$data['include'], true ) &&
            ! empty( 
$data['extracted_directory_path'] ) &&
            ! empty( 
$data['manifest']['wp-content'] )
        );
    }

    public function 
import( array $data, array $imported_data ) {
        
$this->import_session_id $data['session_id'];

        
$path $data['extracted_directory_path'] . 'wp-content/';

        
$post_types $this->filter_post_types$data['selected_custom_post_types'] );

        
$taxonomies $imported_data['taxonomies'] ?? [];
        
$imported_terms ImportExportUtils::map_old_new_term_ids$imported_data );

        
$result['wp-content'] = [];

        foreach ( 
$post_types as $post_type ) {
            
$import $this->import_wp_post_type(
                
$path,
                
$post_type,
                
$imported_data,
                
$taxonomies,
                
$imported_terms
            
);

            if ( empty( 
$import ) ) {
                continue;
            }

            
$result['wp-content'][ $post_type ] = $import;
            
$imported_data array_merge$imported_data$result );
        }

        return 
$result;
    }

    private function 
import_wp_post_type$path$post_type, array $imported_data, array $taxonomies, array $imported_terms ) {
        
$args = [
            
'fetch_attachments' => true,
            
'posts' => ImportExportUtils::map_old_new_post_ids$imported_data ),
            
'terms' => $imported_terms,
            
'taxonomies' => ! empty( $taxonomies$post_type ] ) ? $taxonomies$post_type ] : [],
            
'posts_meta' => [
                static::
META_KEY_ELEMENTOR_IMPORT_SESSION_ID => $this->import_session_id,
            ],
            
'terms_meta' => [
                static::
META_KEY_ELEMENTOR_IMPORT_SESSION_ID => $this->import_session_id,
            ],
        ];

        
$file $path $post_type '/' $post_type '.xml';

        if ( ! 
file_exists$file ) ) {
            return [];
        }

        
$wp_importer = new WP_Import$file$args );
        
$result $wp_importer->run();

        return 
$result['summary']['posts'];
    }

    private function 
filter_post_types$selected_custom_post_types = [] ) {
        
$wp_builtin_post_types ImportExportUtils::get_builtin_wp_post_types();

        foreach ( 
$selected_custom_post_types as $custom_post_type ) {
            if ( 
post_type_exists$custom_post_type ) ) {
                
$this->selected_custom_post_types[] = $custom_post_type;
            }
        }

        
$post_types array_merge$wp_builtin_post_types$this->selected_custom_post_types );
        
$post_types $this->force_element_to_be_last_by_value$post_types'nav_menu_item' );

        return 
$post_types;
    }

    public function 
get_import_session_metadata() : array {
        return [
            
'custom_post_types' => $this->selected_custom_post_types,
        ];
    }

    
/**
     * @param $array array The array we want to relocate his element.
     * @param $element mixed The value of the element in the array we want to shift to end of the array.
     * @return mixed
     */
    
private function force_element_to_be_last_by_value( array $array$element ) {
        
$index array_search$element$arraytrue );

        if ( 
false !== $index ) {
            unset( 
$array$index ] );
            
$array[] = $element;
        }

        return 
$array;
    }
}