| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\GlobalClasses\ImportExportUtils; |
| 4 |
|
| 5 |
use Elementor\App\Modules\ImportExportCustomization\Utils as ImportExportUtils; |
| 6 |
use Elementor\Modules\AtomicWidgets\Utils\Utils; |
| 7 |
use Elementor\Modules\GlobalClasses\Global_Classes_Parser; |
| 8 |
use Elementor\Modules\GlobalClasses\Global_Classes_Repository; |
| 9 |
use Elementor\Plugin; |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
class Legacy_Import_Utils { |
| 16 |
public static function import_classes( string $global_classes_file, string $conflict_resolution ): array { |
| 17 |
$global_classes = ImportExportUtils::read_json_file( $global_classes_file ); |
| 18 |
$active_kit = Plugin::$instance->kits_manager->get_active_kit(); |
| 19 |
|
| 20 |
if ( ! $active_kit || ! $global_classes ) { |
| 21 |
return Import_Utils::EMPTY_RESULT; |
| 22 |
} |
| 23 |
|
| 24 |
$global_classes_result = Global_Classes_Parser::make()->parse( $global_classes ); |
| 25 |
|
| 26 |
if ( ! $global_classes_result->is_valid() ) { |
| 27 |
throw new \Exception( 'Invalid global classes file: ' . esc_html( $global_classes_result->errors()->to_string() ) ); |
| 28 |
} |
| 29 |
|
| 30 |
$imported_classes = $global_classes_result->unwrap(); |
| 31 |
|
| 32 |
if ( empty( $imported_classes['items'] ) ) { |
| 33 |
return Import_Utils::EMPTY_RESULT; |
| 34 |
} |
| 35 |
|
| 36 |
$classes_repository = Global_Classes_Repository::make( $active_kit ); |
| 37 |
$classes_repository->set_preview( false ); |
| 38 |
|
| 39 |
if ( 'override-all' === $conflict_resolution ) { |
| 40 |
$ids_to_delete = $classes_repository->get_order(); |
| 41 |
|
| 42 |
$imported_items = $imported_classes['items']; |
| 43 |
$imported_order = $imported_classes['order']; |
| 44 |
$added_ids = array_keys( $imported_items ); |
| 45 |
|
| 46 |
$classes_repository->apply_changes( |
| 47 |
$imported_items, |
| 48 |
[ |
| 49 |
'added' => $added_ids, |
| 50 |
'deleted' => $ids_to_delete, |
| 51 |
'order' => true, |
| 52 |
], |
| 53 |
$imported_order |
| 54 |
); |
| 55 |
|
| 56 |
$result = Import_Utils::EMPTY_RESULT; |
| 57 |
|
| 58 |
foreach ( $imported_classes['order'] as $id ) { |
| 59 |
if ( ! isset( $imported_classes['items'][ $id ] ) ) { |
| 60 |
continue; |
| 61 |
} |
| 62 |
|
| 63 |
$item = $imported_classes['items'][ $id ]; |
| 64 |
$entry = [ |
| 65 |
'id' => $id, |
| 66 |
'label' => $item['label'] ?? $id, |
| 67 |
]; |
| 68 |
$result['created'][] = [ |
| 69 |
'import_entry' => $entry, |
| 70 |
'result_entry' => $entry, |
| 71 |
]; |
| 72 |
} |
| 73 |
|
| 74 |
return $result; |
| 75 |
} |
| 76 |
|
| 77 |
$existing_labels = $classes_repository->all_labels(); |
| 78 |
$existing_order = $classes_repository->get_order(); |
| 79 |
$existing_ids_set = array_flip( $existing_order ); |
| 80 |
$existing_label_keys = array_values( array_map( 'strtolower', $existing_labels ) ); |
| 81 |
|
| 82 |
$imported_items = $imported_classes['items'] ?? []; |
| 83 |
$imported_order = $imported_classes['order'] ?? []; |
| 84 |
|
| 85 |
$items_to_add = []; |
| 86 |
$added_order = []; |
| 87 |
$result = Import_Utils::EMPTY_RESULT; |
| 88 |
|
| 89 |
foreach ( $imported_order as $imported_id ) { |
| 90 |
if ( ! isset( $imported_items[ $imported_id ] ) ) { |
| 91 |
continue; |
| 92 |
} |
| 93 |
|
| 94 |
$imported_class = $imported_items[ $imported_id ]; |
| 95 |
|
| 96 |
$new_id = $imported_id; |
| 97 |
if ( isset( $existing_ids_set[ $new_id ] ) || isset( $items_to_add[ $new_id ] ) ) { |
| 98 |
$all_ids = array_merge( array_keys( $existing_ids_set ), array_keys( $items_to_add ) ); |
| 99 |
$new_id = self::generate_unique_id( $all_ids ); |
| 100 |
} |
| 101 |
|
| 102 |
$original_label = $imported_class['label'] ?? $imported_id; |
| 103 |
$new_label = ImportExportUtils::resolve_label_conflict( $original_label, $existing_label_keys ); |
| 104 |
$existing_label_keys[] = strtolower( $new_label ); |
| 105 |
|
| 106 |
$imported_class['id'] = $new_id; |
| 107 |
$imported_class['label'] = $new_label; |
| 108 |
|
| 109 |
$items_to_add[ $new_id ] = $imported_class; |
| 110 |
$added_order[] = $new_id; |
| 111 |
|
| 112 |
$import_entry = [ |
| 113 |
'id' => $imported_id, |
| 114 |
'label' => $original_label, |
| 115 |
]; |
| 116 |
$result_entry = [ |
| 117 |
'id' => $new_id, |
| 118 |
'label' => $new_label, |
| 119 |
]; |
| 120 |
|
| 121 |
$was_renamed = strtolower( $new_label ) !== strtolower( $original_label ); |
| 122 |
|
| 123 |
if ( $was_renamed ) { |
| 124 |
$result['renamed'][] = [ |
| 125 |
'import_entry' => $import_entry, |
| 126 |
'result_entry' => $result_entry, |
| 127 |
]; |
| 128 |
} else { |
| 129 |
$result['created'][] = [ |
| 130 |
'import_entry' => $import_entry, |
| 131 |
'result_entry' => $result_entry, |
| 132 |
]; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
if ( ! empty( $items_to_add ) ) { |
| 137 |
$final_order = array_merge( $added_order, $existing_order ); |
| 138 |
$added_ids = array_keys( $items_to_add ); |
| 139 |
|
| 140 |
$classes_repository->apply_changes( |
| 141 |
$items_to_add, |
| 142 |
[ |
| 143 |
'added' => $added_ids, |
| 144 |
'order' => true, |
| 145 |
], |
| 146 |
$final_order |
| 147 |
); |
| 148 |
} |
| 149 |
|
| 150 |
return $result; |
| 151 |
} |
| 152 |
|
| 153 |
private static function generate_unique_id( array $existing_ids ): string { |
| 154 |
return Utils::generate_id( 'g-', $existing_ids ); |
| 155 |
} |
| 156 |
} |
| 157 |
|