| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\App\Modules\ImportExport\Runners\Import; |
| 4 |
|
| 5 |
use Elementor\App\Modules\ImportExport\Utils as ImportExportUtils; |
| 6 |
|
| 7 |
class Taxonomies extends Import_Runner_Base { |
| 8 |
|
| 9 |
private $import_session_id; |
| 10 |
|
| 11 |
public static function get_name(): string { |
| 12 |
return 'taxonomies'; |
| 13 |
} |
| 14 |
|
| 15 |
public function should_import( array $data ) { |
| 16 |
return ( |
| 17 |
isset( $data['include'] ) && |
| 18 |
in_array( 'content', $data['include'], true ) && |
| 19 |
! empty( $data['extracted_directory_path'] ) && |
| 20 |
! empty( $data['manifest']['taxonomies'] ) |
| 21 |
); |
| 22 |
} |
| 23 |
|
| 24 |
public function import( array $data, array $imported_data ) { |
| 25 |
$path = $data['extracted_directory_path'] . 'taxonomies/'; |
| 26 |
$this->import_session_id = $data['session_id']; |
| 27 |
|
| 28 |
$wp_builtin_post_types = ImportExportUtils::get_builtin_wp_post_types(); |
| 29 |
$selected_custom_post_types = isset( $data['selected_custom_post_types'] ) ? $data['selected_custom_post_types'] : []; |
| 30 |
$post_types = array_merge( $wp_builtin_post_types, $selected_custom_post_types ); |
| 31 |
|
| 32 |
$result = []; |
| 33 |
|
| 34 |
foreach ( $post_types as $post_type ) { |
| 35 |
if ( empty( $data['manifest']['taxonomies'][ $post_type ] ) ) { |
| 36 |
continue; |
| 37 |
} |
| 38 |
|
| 39 |
$result['taxonomies'][ $post_type ] = $this->import_taxonomies( $data['manifest']['taxonomies'][ $post_type ], $path ); |
| 40 |
} |
| 41 |
|
| 42 |
return $result; |
| 43 |
} |
| 44 |
|
| 45 |
private function import_taxonomies( array $taxonomies, $path ) { |
| 46 |
$result = []; |
| 47 |
$imported_taxonomies = []; |
| 48 |
|
| 49 |
foreach ( $taxonomies as $taxonomy ) { |
| 50 |
if ( ! taxonomy_exists( $taxonomy ) ) { |
| 51 |
continue; |
| 52 |
} |
| 53 |
|
| 54 |
if ( ! empty( $imported_taxonomies[ $taxonomy ] ) ) { |
| 55 |
$result[ $taxonomy ] = $imported_taxonomies[ $taxonomy ]; |
| 56 |
continue; |
| 57 |
} |
| 58 |
|
| 59 |
$taxonomy_data = ImportExportUtils::read_json_file( $path . $taxonomy ); |
| 60 |
if ( empty( $taxonomy_data ) ) { |
| 61 |
continue; |
| 62 |
} |
| 63 |
|
| 64 |
$import = $this->import_taxonomy( $taxonomy_data ); |
| 65 |
$result[ $taxonomy ] = $import; |
| 66 |
$imported_taxonomies[ $taxonomy ] = $import; |
| 67 |
} |
| 68 |
|
| 69 |
return $result; |
| 70 |
} |
| 71 |
|
| 72 |
private function import_taxonomy( array $taxonomy_data ) { |
| 73 |
$terms = []; |
| 74 |
|
| 75 |
foreach ( $taxonomy_data as $term ) { |
| 76 |
$old_slug = $term['slug']; |
| 77 |
|
| 78 |
$existing_term = term_exists( $term['slug'], $term['taxonomy'] ); |
| 79 |
if ( $existing_term ) { |
| 80 |
if ( 'nav_menu' === $term['taxonomy'] ) { |
| 81 |
$term = $this->handle_duplicated_nav_menu_term( $term ); |
| 82 |
} else { |
| 83 |
$terms[] = [ |
| 84 |
'old_id' => (int) $term['term_id'], |
| 85 |
'new_id' => (int) $existing_term['term_id'], |
| 86 |
'old_slug' => $old_slug, |
| 87 |
'new_slug' => $term['slug'], |
| 88 |
]; |
| 89 |
continue; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
$parent = $this->get_term_parent( $term, $terms ); |
| 94 |
|
| 95 |
$args = [ |
| 96 |
'slug' => $term['slug'], |
| 97 |
'description' => wp_slash( $term['description'] ), |
| 98 |
'parent' => (int) $parent, |
| 99 |
]; |
| 100 |
|
| 101 |
$new_term = wp_insert_term( wp_slash( $term['name'] ), $term['taxonomy'], $args ); |
| 102 |
if ( ! is_wp_error( $new_term ) ) { |
| 103 |
$this->set_session_term_meta( (int) $new_term['term_id'], $this->import_session_id ); |
| 104 |
|
| 105 |
$terms[] = [ |
| 106 |
'old_id' => $term['term_id'], |
| 107 |
'new_id' => (int) $new_term['term_id'], |
| 108 |
'old_slug' => $old_slug, |
| 109 |
'new_slug' => $term['slug'], |
| 110 |
]; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
return $terms; |
| 115 |
} |
| 116 |
|
| 117 |
private function handle_duplicated_nav_menu_term( $term ) { |
| 118 |
do { |
| 119 |
$term['slug'] = $term['slug'] . '-duplicate'; |
| 120 |
$term['name'] = $term['name'] . ' duplicate'; |
| 121 |
} while ( term_exists( $term['slug'], 'nav_menu' ) ); |
| 122 |
|
| 123 |
return $term; |
| 124 |
} |
| 125 |
|
| 126 |
private function get_term_parent( $term, array $imported_terms ) { |
| 127 |
$parent = $term['parent']; |
| 128 |
if ( 0 !== $parent && ! empty( $imported_terms ) ) { |
| 129 |
foreach ( $imported_terms as $imported_term ) { |
| 130 |
if ( $parent === $imported_term['old_id'] ) { |
| 131 |
$parent_term = term_exists( $imported_term['new_id'], $term['taxonomy'] ); |
| 132 |
break; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
if ( isset( $parent_term['term_id'] ) ) { |
| 137 |
return $parent_term['term_id']; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
return 0; |
| 142 |
} |
| 143 |
} |
| 144 |
|