| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\App\Modules\ImportExport\Runners\Import; |
| 4 |
|
| 5 |
use Elementor\App\Modules\ImportExport\Utils as ImportExportUtils; |
| 6 |
use Elementor\Core\Utils\ImportExport\WP_Import; |
| 7 |
|
| 8 |
class Wp_Content extends Import_Runner_Base { |
| 9 |
|
| 10 |
private $import_session_id; |
| 11 |
|
| 12 |
/** |
| 13 |
* @var array |
| 14 |
*/ |
| 15 |
private $selected_custom_post_types = []; |
| 16 |
|
| 17 |
public static function get_name(): string { |
| 18 |
return 'wp-content'; |
| 19 |
} |
| 20 |
|
| 21 |
public function should_import( array $data ) { |
| 22 |
return ( |
| 23 |
isset( $data['include'] ) && |
| 24 |
in_array( 'content', $data['include'], true ) && |
| 25 |
! empty( $data['extracted_directory_path'] ) && |
| 26 |
! empty( $data['manifest']['wp-content'] ) |
| 27 |
); |
| 28 |
} |
| 29 |
|
| 30 |
public function import( array $data, array $imported_data ) { |
| 31 |
$this->import_session_id = $data['session_id']; |
| 32 |
|
| 33 |
$path = $data['extracted_directory_path'] . 'wp-content/'; |
| 34 |
|
| 35 |
$post_types = $this->filter_post_types( $data['selected_custom_post_types'] ); |
| 36 |
|
| 37 |
$taxonomies = $imported_data['taxonomies'] ?? []; |
| 38 |
$imported_terms = ImportExportUtils::map_old_new_term_ids( $imported_data ); |
| 39 |
|
| 40 |
$result['wp-content'] = []; |
| 41 |
|
| 42 |
foreach ( $post_types as $post_type ) { |
| 43 |
$import = $this->import_wp_post_type( |
| 44 |
$path, |
| 45 |
$post_type, |
| 46 |
$imported_data, |
| 47 |
$taxonomies, |
| 48 |
$imported_terms |
| 49 |
); |
| 50 |
|
| 51 |
if ( empty( $import ) ) { |
| 52 |
continue; |
| 53 |
} |
| 54 |
|
| 55 |
$result['wp-content'][ $post_type ] = $import; |
| 56 |
$imported_data = array_merge( $imported_data, $result ); |
| 57 |
} |
| 58 |
|
| 59 |
return $result; |
| 60 |
} |
| 61 |
|
| 62 |
private function import_wp_post_type( $path, $post_type, array $imported_data, array $taxonomies, array $imported_terms ) { |
| 63 |
$args = [ |
| 64 |
'fetch_attachments' => true, |
| 65 |
'posts' => ImportExportUtils::map_old_new_post_ids( $imported_data ), |
| 66 |
'terms' => $imported_terms, |
| 67 |
'taxonomies' => ! empty( $taxonomies[ $post_type ] ) ? $taxonomies[ $post_type ] : [], |
| 68 |
'posts_meta' => [ |
| 69 |
static::META_KEY_ELEMENTOR_IMPORT_SESSION_ID => $this->import_session_id, |
| 70 |
], |
| 71 |
'terms_meta' => [ |
| 72 |
static::META_KEY_ELEMENTOR_IMPORT_SESSION_ID => $this->import_session_id, |
| 73 |
], |
| 74 |
]; |
| 75 |
|
| 76 |
$file = $path . $post_type . '/' . $post_type . '.xml'; |
| 77 |
|
| 78 |
if ( ! file_exists( $file ) ) { |
| 79 |
return []; |
| 80 |
} |
| 81 |
|
| 82 |
$wp_importer = new WP_Import( $file, $args ); |
| 83 |
$result = $wp_importer->run(); |
| 84 |
|
| 85 |
return $result['summary']['posts']; |
| 86 |
} |
| 87 |
|
| 88 |
private function filter_post_types( $selected_custom_post_types = [] ) { |
| 89 |
$wp_builtin_post_types = ImportExportUtils::get_builtin_wp_post_types(); |
| 90 |
|
| 91 |
foreach ( $selected_custom_post_types as $custom_post_type ) { |
| 92 |
if ( post_type_exists( $custom_post_type ) ) { |
| 93 |
$this->selected_custom_post_types[] = $custom_post_type; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
$post_types = array_merge( $wp_builtin_post_types, $this->selected_custom_post_types ); |
| 98 |
$post_types = $this->force_element_to_be_last_by_value( $post_types, 'nav_menu_item' ); |
| 99 |
|
| 100 |
return $post_types; |
| 101 |
} |
| 102 |
|
| 103 |
public function get_import_session_metadata(): array { |
| 104 |
return [ |
| 105 |
'custom_post_types' => $this->selected_custom_post_types, |
| 106 |
]; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* @param $array array The array we want to relocate his element. |
| 111 |
* @param $element mixed The value of the element in the array we want to shift to end of the array. |
| 112 |
* @return mixed |
| 113 |
*/ |
| 114 |
private function force_element_to_be_last_by_value( array $array, $element ) { |
| 115 |
$index = array_search( $element, $array, true ); |
| 116 |
|
| 117 |
if ( false !== $index ) { |
| 118 |
unset( $array[ $index ] ); |
| 119 |
$array[] = $element; |
| 120 |
} |
| 121 |
|
| 122 |
return $array; |
| 123 |
} |
| 124 |
} |
| 125 |
|