PluginProbe
Elementor Website Builder – more than just a page builder / 4.0.0-dev4
Elementor Website Builder – more than just a page builder v4.0.0-dev4
4.3.1 4.3.0 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 All 454 releases
← All changes | modules/variables/import-export-customization/runners/import.php +15 -112 4.3.0-beta2 → 4.0.0-dev4 View file →
@@ -1,9 +1,8 @@
1 1 <?php
2 2
3 3 namespace Elementor\Modules\Variables\ImportExportCustomization\Runners;
4 4
5 -use Elementor\App\Modules\ImportExportCustomization\Design_System_Import_Context;
6 5 use Elementor\App\Modules\ImportExportCustomization\Runners\Import\Import_Runner_Base;
7 6 use Elementor\App\Modules\ImportExportCustomization\Utils as ImportExportUtils;
8 7 use Elementor\Modules\AtomicWidgets\Utils\Utils;
9 8 use Elementor\Modules\Variables\ImportExportCustomization\Import_Export_Customization;
@@ -16,25 +15,16 @@
16 15 exit;
17 16 }
18 17
19 18 class Import extends Import_Runner_Base {
20 - const EMPTY_RESULT = [
21 - 'created' => [],
22 - 'renamed' => [],
23 - 'replaced' => [],
24 - 'skipped' => [],
25 - 'failed' => [],
26 - ];
27 -
28 19 public static function get_name(): string {
29 20 return 'global-variables';
30 21 }
31 22
32 23 public function should_import( array $data ): bool {
33 - $import_context = Design_System_Import_Context::from_data( $data );
34 -
35 24 return (
36 - $import_context->is_included() &&
25 + isset( $data['include'] ) &&
26 + in_array( 'settings', $data['include'], true ) &&
37 27 ! empty( $data['extracted_directory_path'] ) &&
38 28 $this->is_variables_enabled( $data )
39 29 );
40 30 }
@@ -53,93 +43,43 @@
53 43 $file_name = Import_Export_Customization::FILE_NAME;
54 44 $variables_data = ImportExportUtils::read_json_file( "{$data['extracted_directory_path']}/{$file_name}.json" );
55 45
56 46 if ( ! $kit || ! $variables_data || empty( $variables_data['data'] ) ) {
57 - return self::EMPTY_RESULT;
47 + return [];
58 48 }
59 49
60 50 $repository = new Variables_Repository( $kit );
61 - $import_context = Design_System_Import_Context::from_data( $data );
62 - $conflict_resolution = $import_context->resolve_conflict_resolution( $data, 'variablesOverrideAll' );
63 51
64 - if ( 'override-all' === $conflict_resolution ) {
52 + $override_all = ! empty( $data['customization']['settings']['variablesOverrideAll'] );
53 +
54 + if ( $override_all ) {
65 55 $imported_collection = Variables_Collection::hydrate( $variables_data );
66 56 $this->save_collection( $repository, $imported_collection );
67 57
68 - return $this->build_override_all_result( $imported_collection );
58 + return $variables_data;
69 59 }
70 60
71 - $existing_collection = $this->get_existing_collection( $repository );
61 + $existing_collection = $repository->load();
72 62 $imported_collection = Variables_Collection::hydrate( $variables_data );
73 63
74 - $result = $this->resolve_collections( $existing_collection, $imported_collection, $conflict_resolution );
75 - $this->save_collection( $repository, $existing_collection );
64 + $merged_collection = $this->merge_collections( $existing_collection, $imported_collection );
65 + $this->save_collection( $repository, $merged_collection );
76 66
77 - return $result;
67 + return $variables_data;
78 68 }
79 69
80 - private function build_override_all_result( Variables_Collection $imported_collection ): array {
81 - $result = self::EMPTY_RESULT;
82 -
83 - foreach ( $imported_collection->all() as $variable ) {
84 - if ( $variable->is_deleted() ) {
85 - continue;
86 - }
87 -
88 - $result['created'][] = [
89 - 'import_entry' => $this->format_variable_as_entry( $variable ),
90 - ];
91 - }
92 -
93 - return $result;
94 - }
95 -
96 - private function get_existing_collection( Variables_Repository $repository ): Variables_Collection {
97 - return $repository->load();
98 - }
99 -
100 - private function resolve_collections(
70 + private function merge_collections(
101 71 Variables_Collection $existing,
102 - Variables_Collection $imported,
103 - string $conflict_resolution
104 - ): array {
72 + Variables_Collection $imported
73 + ): Variables_Collection {
105 74 $existing_labels = $this->get_existing_labels( $existing );
106 - $existing_label_type_map = $this->build_label_type_map( $existing );
107 75 $existing_ids = array_keys( $existing->all() );
108 76
109 - $result = self::EMPTY_RESULT;
110 -
111 77 foreach ( $imported->all() as $variable ) {
112 78 if ( $variable->is_deleted() ) {
113 79 continue;
114 80 }
115 81
116 - $import_entry = $this->format_variable_as_entry( $variable );
117 - $label_lower = strtolower( $variable->label() );
118 - $has_label_conflict = in_array( $label_lower, $existing_labels, true );
119 -
120 - if ( $has_label_conflict && 'skip' === $conflict_resolution ) {
121 - $result['skipped'][] = [ 'import_entry' => $import_entry ];
122 - continue;
123 - }
124 -
125 - if ( $has_label_conflict && 'replace' === $conflict_resolution ) {
126 - $existing_entry = $existing_label_type_map[ $label_lower ] ?? null;
127 - $type_matches = $existing_entry && $existing_entry['type'] === $variable->type();
128 -
129 - if ( $type_matches ) {
130 - $existing_var = $existing->get( $existing_entry['id'] );
131 - $existing_var->set_value( $variable->value() );
132 - $existing_var->set_sync_to_v3( $variable->sync_to_v3() );
133 -
134 - $result['replaced'][] = [
135 - 'import_entry' => $import_entry,
136 - 'result_entry' => $this->format_variable_as_entry( $existing_var ),
137 - ];
138 - continue;
139 - }
140 - }
141 -
142 82 $original_id = $variable->id();
143 83 $id_exists = in_array( $original_id, $existing_ids, true );
144 84 $new_id = $id_exists
145 85 ? $this->generate_unique_id( $existing_ids )
@@ -154,44 +94,16 @@
154 94 'type' => $variable->type(),
155 95 'label' => $new_label,
156 96 'value' => $variable->value(),
157 97 'order' => $existing->get_next_order(),
158 - 'sync_to_v3' => $variable->sync_to_v3(),
159 98 ] );
160 99
161 100 $existing->add_variable( $new_variable );
162 -
163 - $was_renamed = strtolower( $new_label ) !== $label_lower;
164 - $result_entry = $this->format_variable_as_entry( $new_variable );
165 -
166 - if ( $was_renamed ) {
167 - $result['renamed'][] = [
168 - 'import_entry' => $import_entry,
169 - 'result_entry' => $result_entry,
170 - ];
171 - } else {
172 - $result['created'][] = [ 'import_entry' => $import_entry ];
173 - }
174 101 }
175 102
176 - return $result;
103 + return $existing;
177 104 }
178 105
179 - private function build_label_type_map( Variables_Collection $collection ): array {
180 - $map = [];
181 -
182 - foreach ( $collection->all() as $variable ) {
183 - if ( ! $variable->is_deleted() ) {
184 - $map[ strtolower( $variable->label() ) ] = [
185 - 'id' => $variable->id(),
186 - 'type' => $variable->type(),
187 - ];
188 - }
189 - }
190 -
191 - return $map;
192 - }
193 -
194 106 private function get_existing_labels( Variables_Collection $collection ): array {
195 107 $labels = [];
196 108
197 109 foreach ( $collection->all() as $variable ) {
@@ -212,15 +124,6 @@
212 124
213 125 if ( false === $result ) {
214 126 throw new \RuntimeException( 'Failed to save global variables during import.' );
215 127 }
216 -
217 - Plugin::$instance->files_manager->clear_cache();
218 - }
219 -
220 - private function format_variable_as_entry( Variable $variable ): array {
221 - return [
222 - 'id' => $variable->id(),
223 - 'label' => $variable->label(),
224 - ];
225 128 }
226 129 }