PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.2
Elementor Website Builder – more than just a page builder v4.3.2
4.3.2 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 All 455 releases
← All changes | modules/variables/import-export-customization/runners/import.php +105 -33 4.0.9 → 4.3.2 View file →
@@ -1,8 +1,9 @@
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;
5 6 use Elementor\App\Modules\ImportExportCustomization\Runners\Import\Import_Runner_Base;
6 7 use Elementor\App\Modules\ImportExportCustomization\Utils as ImportExportUtils;
7 8 use Elementor\Modules\AtomicWidgets\Utils\Utils;
8 9 use Elementor\Modules\Variables\ImportExportCustomization\Import_Export_Customization;
@@ -15,16 +16,25 @@
15 16 exit;
16 17 }
17 18
18 19 class Import extends Import_Runner_Base {
20 + const EMPTY_RESULT = [
21 + 'created' => [],
22 + 'renamed' => [],
23 + 'replaced' => [],
24 + 'skipped' => [],
25 + 'failed' => [],
26 + ];
27 +
19 28 public static function get_name(): string {
20 29 return 'global-variables';
21 30 }
22 31
23 32 public function should_import( array $data ): bool {
33 + $import_context = Design_System_Import_Context::from_data( $data );
34 +
24 35 return (
25 - isset( $data['include'] ) &&
26 - in_array( 'settings', $data['include'], true ) &&
36 + $import_context->is_included() &&
27 37 ! empty( $data['extracted_directory_path'] ) &&
28 38 $this->is_variables_enabled( $data )
29 39 );
30 40 }
@@ -43,68 +53,93 @@
43 53 $file_name = Import_Export_Customization::FILE_NAME;
44 54 $variables_data = ImportExportUtils::read_json_file( "{$data['extracted_directory_path']}/{$file_name}.json" );
45 55
46 56 if ( ! $kit || ! $variables_data || empty( $variables_data['data'] ) ) {
47 - return [];
57 + return self::EMPTY_RESULT;
48 58 }
49 59
50 60 $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' );
51 63
52 - $override_all = ! empty( $data['customization']['settings']['variablesOverrideAll'] );
53 -
54 - if ( $override_all ) {
64 + if ( 'override-all' === $conflict_resolution ) {
55 65 $imported_collection = Variables_Collection::hydrate( $variables_data );
56 66 $this->save_collection( $repository, $imported_collection );
57 67
58 - return $variables_data;
68 + return $this->build_override_all_result( $imported_collection );
59 69 }
60 70
61 - $existing_collection = $this->get_existing_collection( $repository, $imported_data );
71 + $existing_collection = $this->get_existing_collection( $repository );
62 72 $imported_collection = Variables_Collection::hydrate( $variables_data );
63 73
64 - $merged_collection = $this->merge_collections( $existing_collection, $imported_collection );
65 - $this->save_collection( $repository, $merged_collection );
74 + $result = $this->resolve_collections( $existing_collection, $imported_collection, $conflict_resolution );
75 + $this->save_collection( $repository, $existing_collection );
66 76
67 - return $variables_data;
77 + return $result;
68 78 }
69 79
70 - private function get_existing_collection( Variables_Repository $repository, array $imported_data ): Variables_Collection {
71 - $existing = $repository->load();
80 + private function build_override_all_result( Variables_Collection $imported_collection ): array {
81 + $result = self::EMPTY_RESULT;
72 82
73 - if ( count( $existing->all() ) > 0 ) {
74 - return $existing;
75 - }
83 + foreach ( $imported_collection->all() as $variable ) {
84 + if ( $variable->is_deleted() ) {
85 + continue;
86 + }
76 87
77 - $was_new_kit_created = ! empty( $imported_data['site-settings']['imported_kit_id'] );
78 -
79 - if ( ! $was_new_kit_created ) {
80 - return $existing;
88 + $result['created'][] = [
89 + 'import_entry' => $this->format_variable_as_entry( $variable ),
90 + ];
81 91 }
82 92
83 - $previous_kit_id = Plugin::$instance->kits_manager->get_previous_id();
93 + return $result;
94 + }
84 95
85 - if ( ! $previous_kit_id ) {
86 - return $existing;
87 - }
88 -
89 - $previous_kit = Plugin::$instance->kits_manager->get_kit( $previous_kit_id );
90 - $previous_repository = new Variables_Repository( $previous_kit );
91 -
92 - return $previous_repository->load();
96 + private function get_existing_collection( Variables_Repository $repository ): Variables_Collection {
97 + return $repository->load();
93 98 }
94 99
95 - private function merge_collections(
100 + private function resolve_collections(
96 101 Variables_Collection $existing,
97 - Variables_Collection $imported
98 - ): Variables_Collection {
102 + Variables_Collection $imported,
103 + string $conflict_resolution
104 + ): array {
99 105 $existing_labels = $this->get_existing_labels( $existing );
106 + $existing_label_type_map = $this->build_label_type_map( $existing );
100 107 $existing_ids = array_keys( $existing->all() );
101 108
109 + $result = self::EMPTY_RESULT;
110 +
102 111 foreach ( $imported->all() as $variable ) {
103 112 if ( $variable->is_deleted() ) {
104 113 continue;
105 114 }
106 115
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 +
107 142 $original_id = $variable->id();
108 143 $id_exists = in_array( $original_id, $existing_ids, true );
109 144 $new_id = $id_exists
110 145 ? $this->generate_unique_id( $existing_ids )
@@ -119,16 +154,44 @@
119 154 'type' => $variable->type(),
120 155 'label' => $new_label,
121 156 'value' => $variable->value(),
122 157 'order' => $existing->get_next_order(),
158 + 'sync_to_v3' => $variable->sync_to_v3(),
123 159 ] );
124 160
125 161 $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 + }
126 174 }
127 175
128 - return $existing;
176 + return $result;
129 177 }
130 178
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 +
131 194 private function get_existing_labels( Variables_Collection $collection ): array {
132 195 $labels = [];
133 196
134 197 foreach ( $collection->all() as $variable ) {
@@ -149,6 +212,15 @@
149 212
150 213 if ( false === $result ) {
151 214 throw new \RuntimeException( 'Failed to save global variables during import.' );
152 215 }
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 + ];
153 225 }
154 226 }