← All changes
|
modules/variables/import-export-customization/runners/import.php
+33
-105
4.3.0-beta3
→
4.0.8
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,68 @@ | ||
| 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 = $this->get_existing_collection( $repository, $imported_data ); | |
| 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; | |
| 70 | + private function get_existing_collection( Variables_Repository $repository, array $imported_data ): Variables_Collection { | |
| 71 | + $existing = $repository->load(); | |
| 82 | 72 | |
| 83 | - foreach ( $imported_collection->all() as $variable ) { | |
| 84 | - if ( $variable->is_deleted() ) { | |
| 85 | - continue; | |
| 86 | - } | |
| 73 | + if ( count( $existing->all() ) > 0 ) { | |
| 74 | + return $existing; | |
| 75 | + } | |
| 87 | 76 | |
| 88 | - $result['created'][] = [ | |
| 89 | - 'import_entry' => $this->format_variable_as_entry( $variable ), | |
| 90 | - ]; | |
| 77 | + $was_new_kit_created = ! empty( $imported_data['site-settings']['imported_kit_id'] ); | |
| 78 | + | |
| 79 | + if ( ! $was_new_kit_created ) { | |
| 80 | + return $existing; | |
| 91 | 81 | } |
| 92 | 82 | |
| 93 | - return $result; | |
| 94 | - } | |
| 83 | + $previous_kit_id = Plugin::$instance->kits_manager->get_previous_id(); | |
| 95 | 84 | |
| 96 | - private function get_existing_collection( Variables_Repository $repository ): Variables_Collection { | |
| 97 | - return $repository->load(); | |
| 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(); | |
| 98 | 93 | } |
| 99 | 94 | |
| 100 | - private function resolve_collections( | |
| 95 | + private function merge_collections( | |
| 101 | 96 | Variables_Collection $existing, |
| 102 | - Variables_Collection $imported, | |
| 103 | - string $conflict_resolution | |
| 104 | - ): array { | |
| 97 | + Variables_Collection $imported | |
| 98 | + ): Variables_Collection { | |
| 105 | 99 | $existing_labels = $this->get_existing_labels( $existing ); |
| 106 | - $existing_label_type_map = $this->build_label_type_map( $existing ); | |
| 107 | 100 | $existing_ids = array_keys( $existing->all() ); |
| 108 | 101 | |
| 109 | - $result = self::EMPTY_RESULT; | |
| 110 | - | |
| 111 | 102 | foreach ( $imported->all() as $variable ) { |
| 112 | 103 | if ( $variable->is_deleted() ) { |
| 113 | 104 | continue; |
| 114 | 105 | } |
| 115 | 106 | |
| 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 | 107 | $original_id = $variable->id(); |
| 143 | 108 | $id_exists = in_array( $original_id, $existing_ids, true ); |
| 144 | 109 | $new_id = $id_exists |
| 145 | 110 | ? $this->generate_unique_id( $existing_ids ) |
| @@ -154,44 +119,16 @@ | ||
| 154 | 119 | 'type' => $variable->type(), |
| 155 | 120 | 'label' => $new_label, |
| 156 | 121 | 'value' => $variable->value(), |
| 157 | 122 | 'order' => $existing->get_next_order(), |
| 158 | - 'sync_to_v3' => $variable->sync_to_v3(), | |
| 159 | 123 | ] ); |
| 160 | 124 | |
| 161 | 125 | $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 | 126 | } |
| 175 | 127 | |
| 176 | - return $result; | |
| 128 | + return $existing; | |
| 177 | 129 | } |
| 178 | 130 | |
| 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 | 131 | private function get_existing_labels( Variables_Collection $collection ): array { |
| 195 | 132 | $labels = []; |
| 196 | 133 | |
| 197 | 134 | foreach ( $collection->all() as $variable ) { |
| @@ -212,15 +149,6 @@ | ||
| 212 | 149 | |
| 213 | 150 | if ( false === $result ) { |
| 214 | 151 | throw new \RuntimeException( 'Failed to save global variables during import.' ); |
| 215 | 152 | } |
| 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 | 153 | } |
| 226 | 154 | } |