PluginProbe
Elementor Website Builder – more than just a page builder / 4.0.0-dev2
Elementor Website Builder – more than just a page builder v4.0.0-dev2
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 4.0.7 All 451 releases
elementor / modules / variables / import-export-customization / runners / import.php

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

130 lines 3.9 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\Runners\Import\Import_Runner_Base;
6 use Elementor\App\Modules\ImportExportCustomization\Utils as ImportExportUtils;
7 use Elementor\Modules\AtomicWidgets\Utils\Utils;
8 use Elementor\Modules\Variables\ImportExportCustomization\Import_Export_Customization;
9 use Elementor\Modules\Variables\Storage\Entities\Variable;
10 use Elementor\Modules\Variables\Storage\Variables_Collection;
11 use Elementor\Modules\Variables\Storage\Variables_Repository;
12 use Elementor\Plugin;
13
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 class Import extends Import_Runner_Base {
19 public static function get_name(): string {
20 return 'global-variables';
21 }
22
23 public function should_import( array $data ): bool {
24 return (
25 isset( $data['include'] ) &&
26 in_array( 'settings', $data['include'], true ) &&
27 ! empty( $data['extracted_directory_path'] ) &&
28 $this->is_variables_enabled( $data )
29 );
30 }
31
32 private function is_variables_enabled( array $data ): bool {
33 if ( isset( $data['customization']['settings']['variables'] ) ) {
34 return (bool) $data['customization']['settings']['variables'];
35 }
36
37 return true;
38 }
39
40 public function import( array $data, array $imported_data ): array {
41 $kit = Plugin::$instance->kits_manager->get_active_kit();
42
43 $file_name = Import_Export_Customization::FILE_NAME;
44 $variables_data = ImportExportUtils::read_json_file( "{$data['extracted_directory_path']}/{$file_name}.json" );
45
46 if ( ! $kit || ! $variables_data || empty( $variables_data['data'] ) ) {
47 return [];
48 }
49
50 $repository = new Variables_Repository( $kit );
51
52 $override_all = ! empty( $data['customization']['settings']['variablesOverrideAll'] );
53
54 if ( $override_all ) {
55 $imported_collection = Variables_Collection::hydrate( $variables_data );
56 $this->save_collection( $repository, $imported_collection );
57
58 return $variables_data;
59 }
60
61 $existing_collection = $repository->load();
62 $imported_collection = Variables_Collection::hydrate( $variables_data );
63
64 $merged_collection = $this->merge_collections( $existing_collection, $imported_collection );
65 $this->save_collection( $repository, $merged_collection );
66
67 return $variables_data;
68 }
69
70 private function merge_collections(
71 Variables_Collection $existing,
72 Variables_Collection $imported
73 ): Variables_Collection {
74 $existing_labels = $this->get_existing_labels( $existing );
75 $existing_ids = array_keys( $existing->all() );
76
77 foreach ( $imported->all() as $variable ) {
78 if ( $variable->is_deleted() ) {
79 continue;
80 }
81
82 $original_id = $variable->id();
83 $id_exists = in_array( $original_id, $existing_ids, true );
84 $new_id = $id_exists
85 ? $this->generate_unique_id( $existing_ids )
86 : $original_id;
87 $existing_ids[] = $new_id;
88
89 $new_label = ImportExportUtils::resolve_label_conflict( $variable->label(), $existing_labels );
90 $existing_labels[] = strtolower( $new_label );
91
92 $new_variable = Variable::create_new( [
93 'id' => $new_id,
94 'type' => $variable->type(),
95 'label' => $new_label,
96 'value' => $variable->value(),
97 'order' => $existing->get_next_order(),
98 ] );
99
100 $existing->add_variable( $new_variable );
101 }
102
103 return $existing;
104 }
105
106 private function get_existing_labels( Variables_Collection $collection ): array {
107 $labels = [];
108
109 foreach ( $collection->all() as $variable ) {
110 if ( ! $variable->is_deleted() ) {
111 $labels[] = strtolower( $variable->label() );
112 }
113 }
114
115 return $labels;
116 }
117
118 private function generate_unique_id( array $existing_ids ): string {
119 return Utils::generate_id( 'e-gv-', $existing_ids );
120 }
121
122 private function save_collection( Variables_Repository $repository, Variables_Collection $collection ): void {
123 $result = $repository->save( $collection );
124
125 if ( false === $result ) {
126 throw new \RuntimeException( 'Failed to save global variables during import.' );
127 }
128 }
129 }
130