← All changes
|
modules/global-classes/import-export-customization/runners/import.php
+18
-120
4.0.8
→
4.3.1
View file →
| @@ -1,15 +1,15 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Elementor\Modules\GlobalClasses\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 | -use Elementor\App\Modules\ImportExportCustomization\Utils as ImportExportUtils; | |
| 7 | -use Elementor\Modules\AtomicWidgets\Utils\Utils; | |
| 8 | -use Elementor\Modules\GlobalClasses\Global_Classes_Parser; | |
| 9 | -use Elementor\Modules\GlobalClasses\Global_Classes_Repository; | |
| 10 | 7 | use Elementor\Modules\GlobalClasses\ImportExportCustomization\Import_Export_Customization; |
| 8 | +use Elementor\Modules\GlobalClasses\ImportExportUtils\Import_Utils; | |
| 11 | 9 | use Elementor\Plugin; |
| 10 | +use Elementor\Core\Kits\Documents\Kit; | |
| 11 | +use Elementor\Modules\GlobalClasses\ImportExportUtils\Legacy_Import_Utils; | |
| 12 | 12 | |
| 13 | 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | 14 | exit; |
| 15 | 15 | } |
| @@ -19,11 +19,12 @@ | ||
| 19 | 19 | return 'global-classes'; |
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | public function should_import( array $data ): bool { |
| 23 | + $import_context = Design_System_Import_Context::from_data( $data ); | |
| 24 | + | |
| 23 | 25 | return ( |
| 24 | - isset( $data['include'] ) && | |
| 25 | - in_array( 'settings', $data['include'], true ) && | |
| 26 | + $import_context->is_included() && | |
| 26 | 27 | ! empty( $data['extracted_directory_path'] ) && |
| 27 | 28 | $this->is_classes_enabled( $data ) |
| 28 | 29 | ); |
| 29 | 30 | } |
| @@ -36,126 +37,23 @@ | ||
| 36 | 37 | return true; |
| 37 | 38 | } |
| 38 | 39 | |
| 39 | 40 | public function import( array $data, array $imported_data ): array { |
| 40 | - $kit = Plugin::$instance->kits_manager->get_active_kit(); | |
| 41 | + $import_context = Design_System_Import_Context::from_data( $data ); | |
| 42 | + $conflict_resolution = $import_context->resolve_conflict_resolution( $data, 'classesOverrideAll' ); | |
| 41 | 43 | |
| 42 | - $file_name = Import_Export_Customization::FILE_NAME; | |
| 43 | - $global_classes = ImportExportUtils::read_json_file( "{$data['extracted_directory_path']}/{$file_name}.json" ); | |
| 44 | - | |
| 45 | - if ( ! $kit || ! $global_classes ) { | |
| 46 | - return []; | |
| 44 | + if ( $this->is_legacy_import_format( $data ) ) { | |
| 45 | + $global_classes_file = $data['extracted_directory_path'] . '/' . Import_Export_Customization::FILE_NAME . '.json'; | |
| 46 | + return Legacy_Import_Utils::import_classes( $global_classes_file, $conflict_resolution ); | |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | - $global_classes['order'] = Global_Classes_Parser::sanitize_order( | |
| 50 | - $global_classes['items'] ?? [], | |
| 51 | - $global_classes['order'] ?? [] | |
| 52 | - ); | |
| 53 | - | |
| 54 | - $global_classes_result = Global_Classes_Parser::make()->parse( $global_classes ); | |
| 55 | - | |
| 56 | - if ( ! $global_classes_result->is_valid() ) { | |
| 57 | - return []; | |
| 58 | - } | |
| 59 | - | |
| 60 | - $imported_classes = $global_classes_result->unwrap(); | |
| 61 | - $repository = Global_Classes_Repository::make(); | |
| 62 | - | |
| 63 | - $override_all = ! empty( $data['customization']['settings']['classesOverrideAll'] ); | |
| 64 | - | |
| 65 | - if ( $override_all ) { | |
| 66 | - $repository->put( | |
| 67 | - $imported_classes['items'], | |
| 68 | - $imported_classes['order'] | |
| 69 | - ); | |
| 70 | - | |
| 71 | - return $imported_classes; | |
| 72 | - } | |
| 73 | - | |
| 74 | - $existing_classes = $this->get_existing_classes( $repository, $imported_data ); | |
| 75 | - $merged = $this->merge_classes( $existing_classes, $imported_classes ); | |
| 76 | - | |
| 77 | - $repository->put( | |
| 78 | - $merged['items'], | |
| 79 | - $merged['order'] | |
| 80 | - ); | |
| 81 | - | |
| 82 | - return $imported_classes; | |
| 49 | + $global_classes_dir = $data['extracted_directory_path'] . '/' . Import_Export_Customization::DIRECTORY_NAME; | |
| 50 | + return Import_Utils::import_classes( $global_classes_dir, [ 'conflict_resolution' => $conflict_resolution ] ); | |
| 83 | 51 | } |
| 84 | 52 | |
| 85 | - private function get_existing_classes( Global_Classes_Repository $repository, array $imported_data ): array { | |
| 86 | - $existing = $repository->all()->get(); | |
| 53 | + protected function is_legacy_import_format( array $data ): bool { | |
| 54 | + $manifest = $data['manifest']; | |
| 55 | + $elementor_version = $manifest['elementor_version']; | |
| 87 | 56 | |
| 88 | - if ( ! empty( $existing['items'] ) ) { | |
| 89 | - return $existing; | |
| 90 | - } | |
| 91 | - | |
| 92 | - $was_new_kit_created = ! empty( $imported_data['site-settings']['imported_kit_id'] ); | |
| 93 | - | |
| 94 | - if ( ! $was_new_kit_created ) { | |
| 95 | - return $existing; | |
| 96 | - } | |
| 97 | - | |
| 98 | - $previous_kit_id = Plugin::$instance->kits_manager->get_previous_id(); | |
| 99 | - | |
| 100 | - if ( ! $previous_kit_id ) { | |
| 101 | - return $existing; | |
| 102 | - } | |
| 103 | - | |
| 104 | - $previous_kit = Plugin::$instance->kits_manager->get_kit( $previous_kit_id ); | |
| 105 | - | |
| 106 | - return Global_Classes_Repository::make( $previous_kit )->all()->get(); | |
| 107 | - } | |
| 108 | - | |
| 109 | - private function merge_classes( array $existing, array $imported ): array { | |
| 110 | - $existing_items = $existing['items'] ?? []; | |
| 111 | - $existing_order = $existing['order'] ?? []; | |
| 112 | - $existing_labels = $this->get_existing_labels( $existing_items ); | |
| 113 | - | |
| 114 | - $imported_items = $imported['items'] ?? []; | |
| 115 | - $imported_order = $imported['order'] ?? []; | |
| 116 | - | |
| 117 | - foreach ( $imported_order as $imported_id ) { | |
| 118 | - if ( ! isset( $imported_items[ $imported_id ] ) ) { | |
| 119 | - continue; | |
| 120 | - } | |
| 121 | - | |
| 122 | - $imported_class = $imported_items[ $imported_id ]; | |
| 123 | - | |
| 124 | - $id_exists = array_key_exists( $imported_id, $existing_items ); | |
| 125 | - $new_id = $id_exists | |
| 126 | - ? $this->generate_unique_id( array_keys( $existing_items ) ) | |
| 127 | - : $imported_id; | |
| 128 | - | |
| 129 | - $original_label = $imported_class['label'] ?? $imported_id; | |
| 130 | - $new_label = ImportExportUtils::resolve_label_conflict( $original_label, $existing_labels ); | |
| 131 | - $existing_labels[] = strtolower( $new_label ); | |
| 132 | - | |
| 133 | - $imported_class['id'] = $new_id; | |
| 134 | - $imported_class['label'] = $new_label; | |
| 135 | - | |
| 136 | - $existing_items[ $new_id ] = $imported_class; | |
| 137 | - $existing_order[] = $new_id; | |
| 138 | - } | |
| 139 | - | |
| 140 | - return [ | |
| 141 | - 'items' => $existing_items, | |
| 142 | - 'order' => $existing_order, | |
| 143 | - ]; | |
| 144 | - } | |
| 145 | - | |
| 146 | - private function get_existing_labels( array $items ): array { | |
| 147 | - $labels = []; | |
| 148 | - | |
| 149 | - foreach ( $items as $item ) { | |
| 150 | - if ( isset( $item['label'] ) ) { | |
| 151 | - $labels[] = strtolower( $item['label'] ); | |
| 152 | - } | |
| 153 | - } | |
| 154 | - | |
| 155 | - return $labels; | |
| 156 | - } | |
| 157 | - | |
| 158 | - private function generate_unique_id( array $existing_ids ): string { | |
| 159 | - return Utils::generate_id( 'g-', $existing_ids ); | |
| 57 | + return version_compare( $elementor_version, '4.1.0-beta1', '<' ); | |
| 160 | 58 | } |
| 161 | 59 | } |