PluginProbe
Elementor Website Builder – more than just a page builder / 4.0.4
Elementor Website Builder – more than just a page builder v4.0.4
4.3.0-beta3 4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 All 452 releases
elementor / app / modules / import-export-customization / runners / import / taxonomies.php

taxonomies.php in Elementor Website Builder – more than just a page builder 4.0.4, at app/modules/import-export-customization/runners/import/taxonomies.php

170 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\App\Modules\ImportExportCustomization\Runners\Import;
4
5 use Elementor\App\Modules\ImportExportCustomization\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 if ( ! function_exists( 'wp_insert_term' ) ) {
26 require_once ABSPATH . 'wp-admin/includes/taxonomy.php';
27 }
28
29 $customization = $data['customization']['content'] ?? null;
30
31 if ( $customization ) {
32 return $this->import_with_customization( $data, $imported_data, $customization );
33 }
34
35 return $this->import_all( $data, $imported_data );
36 }
37
38 public function import_all( array $data, array $imported_data ) {
39 $path = $data['extracted_directory_path'] . 'taxonomies/';
40 $this->import_session_id = $data['session_id'];
41
42 $wp_builtin_post_types = ImportExportUtils::get_builtin_wp_post_types();
43 $selected_custom_post_types = isset( $data['selected_custom_post_types'] ) ? $data['selected_custom_post_types'] : [];
44 $post_types = array_merge( $wp_builtin_post_types, $selected_custom_post_types );
45
46 $result = [];
47
48 foreach ( $post_types as $post_type ) {
49 if ( empty( $data['manifest']['taxonomies'][ $post_type ] ) ) {
50 continue;
51 }
52
53 $result['taxonomies'][ $post_type ] = $this->import_taxonomies( $data['manifest']['taxonomies'][ $post_type ], $path );
54 }
55
56 return $result;
57 }
58 public function import_with_customization( array $data, array $imported_data, array $customization ) {
59 $result = apply_filters( 'elementor/import-export-customization/import/taxonomies/customization', null, $data, $imported_data, $customization, $this );
60
61 if ( is_array( $result ) ) {
62 return $result;
63 }
64
65 return $this->import_all( $data, $imported_data );
66 }
67
68
69 public function import_taxonomies( array $taxonomies, $path ) {
70 $result = [];
71 $imported_taxonomies = [];
72
73 foreach ( $taxonomies as $taxonomy_object ) {
74 $taxonomy = is_array( $taxonomy_object ) ? $taxonomy_object['name'] : $taxonomy_object;
75
76 if ( ! taxonomy_exists( $taxonomy ) ) {
77 continue;
78 }
79
80 if ( ! empty( $imported_taxonomies[ $taxonomy ] ) ) {
81 $result[ $taxonomy ] = $imported_taxonomies[ $taxonomy ];
82 continue;
83 }
84
85 $taxonomy_data = ImportExportUtils::read_json_file( $path . $taxonomy );
86 if ( empty( $taxonomy_data ) ) {
87 continue;
88 }
89
90 $import = $this->import_taxonomy( $taxonomy_data );
91 $result[ $taxonomy ] = $import;
92 $imported_taxonomies[ $taxonomy ] = $import;
93 }
94
95 return $result;
96 }
97
98 private function import_taxonomy( array $taxonomy_data ) {
99 $terms = [];
100
101 foreach ( $taxonomy_data as $term ) {
102 $old_slug = $term['slug'];
103
104 $existing_term = term_exists( $term['slug'], $term['taxonomy'] );
105 if ( $existing_term ) {
106 if ( 'nav_menu' === $term['taxonomy'] ) {
107 $term = $this->handle_duplicated_nav_menu_term( $term );
108 } else {
109 $terms[] = [
110 'old_id' => (int) $term['term_id'],
111 'new_id' => (int) $existing_term['term_id'],
112 'old_slug' => $old_slug,
113 'new_slug' => $term['slug'],
114 ];
115 continue;
116 }
117 }
118
119 $parent = $this->get_term_parent( $term, $terms );
120
121 $args = [
122 'slug' => $term['slug'],
123 'description' => wp_slash( $term['description'] ),
124 'parent' => (int) $parent,
125 ];
126
127 $new_term = wp_insert_term( wp_slash( $term['name'] ), $term['taxonomy'], $args );
128 if ( ! is_wp_error( $new_term ) ) {
129 $this->set_session_term_meta( (int) $new_term['term_id'], $this->import_session_id );
130
131 $terms[] = [
132 'old_id' => $term['term_id'],
133 'new_id' => (int) $new_term['term_id'],
134 'old_slug' => $old_slug,
135 'new_slug' => $term['slug'],
136 ];
137 }
138 }
139
140 return $terms;
141 }
142
143 private function handle_duplicated_nav_menu_term( $term ) {
144 do {
145 $term['slug'] = $term['slug'] . '-duplicate';
146 $term['name'] = $term['name'] . ' duplicate';
147 } while ( term_exists( $term['slug'], 'nav_menu' ) );
148
149 return $term;
150 }
151
152 private function get_term_parent( $term, array $imported_terms ) {
153 $parent = $term['parent'];
154 if ( 0 !== $parent && ! empty( $imported_terms ) ) {
155 foreach ( $imported_terms as $imported_term ) {
156 if ( $parent === $imported_term['old_id'] ) {
157 $parent_term = term_exists( $imported_term['new_id'], $term['taxonomy'] );
158 break;
159 }
160 }
161
162 if ( isset( $parent_term['term_id'] ) ) {
163 return $parent_term['term_id'];
164 }
165 }
166
167 return 0;
168 }
169 }
170