PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.0-beta3
Elementor Website Builder – more than just a page builder v4.3.0-beta3
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 / modules / variables / import-export-customization / runners / import.php

import.php in Elementor Website Builder – more than just a page builder 4.3.0-beta3, at modules/variables/import-export-customization/runners/import.php

227 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\Modules\Variables\ImportExportCustomization\Runners;
4
5 use Elementor\App\Modules\ImportExportCustomization\Design_System_Import_Context;
6 use Elementor\App\Modules\ImportExportCustomization\Runners\Import\Import_Runner_Base;
7 use Elementor\App\Modules\ImportExportCustomization\Utils as ImportExportUtils;
8 use Elementor\Modules\AtomicWidgets\Utils\Utils;
9 use Elementor\Modules\Variables\ImportExportCustomization\Import_Export_Customization;
10 use Elementor\Modules\Variables\Storage\Entities\Variable;
11 use Elementor\Modules\Variables\Storage\Variables_Collection;
12 use Elementor\Modules\Variables\Storage\Variables_Repository;
13 use Elementor\Plugin;
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit;
17 }
18
19 class Import extends Import_Runner_Base {
20 const EMPTY_RESULT = [
21 'created' => [],
22 'renamed' => [],
23 'replaced' => [],
24 'skipped' => [],
25 'failed' => [],
26 ];
27
28 public static function get_name(): string {
29 return 'global-variables';
30 }
31
32 public function should_import( array $data ): bool {
33 $import_context = Design_System_Import_Context::from_data( $data );
34
35 return (
36 $import_context->is_included() &&
37 ! empty( $data['extracted_directory_path'] ) &&
38 $this->is_variables_enabled( $data )
39 );
40 }
41
42 private function is_variables_enabled( array $data ): bool {
43 if ( isset( $data['customization']['settings']['variables'] ) ) {
44 return (bool) $data['customization']['settings']['variables'];
45 }
46
47 return true;
48 }
49
50 public function import( array $data, array $imported_data ): array {
51 $kit = Plugin::$instance->kits_manager->get_active_kit();
52
53 $file_name = Import_Export_Customization::FILE_NAME;
54 $variables_data = ImportExportUtils::read_json_file( "{$data['extracted_directory_path']}/{$file_name}.json" );
55
56 if ( ! $kit || ! $variables_data || empty( $variables_data['data'] ) ) {
57 return self::EMPTY_RESULT;
58 }
59
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' );
63
64 if ( 'override-all' === $conflict_resolution ) {
65 $imported_collection = Variables_Collection::hydrate( $variables_data );
66 $this->save_collection( $repository, $imported_collection );
67
68 return $this->build_override_all_result( $imported_collection );
69 }
70
71 $existing_collection = $this->get_existing_collection( $repository );
72 $imported_collection = Variables_Collection::hydrate( $variables_data );
73
74 $result = $this->resolve_collections( $existing_collection, $imported_collection, $conflict_resolution );
75 $this->save_collection( $repository, $existing_collection );
76
77 return $result;
78 }
79
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(
101 Variables_Collection $existing,
102 Variables_Collection $imported,
103 string $conflict_resolution
104 ): array {
105 $existing_labels = $this->get_existing_labels( $existing );
106 $existing_label_type_map = $this->build_label_type_map( $existing );
107 $existing_ids = array_keys( $existing->all() );
108
109 $result = self::EMPTY_RESULT;
110
111 foreach ( $imported->all() as $variable ) {
112 if ( $variable->is_deleted() ) {
113 continue;
114 }
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
142 $original_id = $variable->id();
143 $id_exists = in_array( $original_id, $existing_ids, true );
144 $new_id = $id_exists
145 ? $this->generate_unique_id( $existing_ids )
146 : $original_id;
147 $existing_ids[] = $new_id;
148
149 $new_label = ImportExportUtils::resolve_label_conflict( $variable->label(), $existing_labels );
150 $existing_labels[] = strtolower( $new_label );
151
152 $new_variable = Variable::create_new( [
153 'id' => $new_id,
154 'type' => $variable->type(),
155 'label' => $new_label,
156 'value' => $variable->value(),
157 'order' => $existing->get_next_order(),
158 'sync_to_v3' => $variable->sync_to_v3(),
159 ] );
160
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 }
174 }
175
176 return $result;
177 }
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
194 private function get_existing_labels( Variables_Collection $collection ): array {
195 $labels = [];
196
197 foreach ( $collection->all() as $variable ) {
198 if ( ! $variable->is_deleted() ) {
199 $labels[] = strtolower( $variable->label() );
200 }
201 }
202
203 return $labels;
204 }
205
206 private function generate_unique_id( array $existing_ids ): string {
207 return Utils::generate_id( 'e-gv-', $existing_ids );
208 }
209
210 private function save_collection( Variables_Repository $repository, Variables_Collection $collection ): void {
211 $result = $repository->save( $collection );
212
213 if ( false === $result ) {
214 throw new \RuntimeException( 'Failed to save global variables during import.' );
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 ];
225 }
226 }
227