| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\GlobalClasses\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\GlobalClasses\Global_Classes_Parser; |
| 9 |
use Elementor\Modules\GlobalClasses\Global_Classes_Repository; |
| 10 |
use Elementor\Modules\GlobalClasses\ImportExportCustomization\Import_Export_Customization; |
| 11 |
use Elementor\Plugin; |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
class Import extends Import_Runner_Base { |
| 18 |
public static function get_name(): string { |
| 19 |
return 'global-classes'; |
| 20 |
} |
| 21 |
|
| 22 |
public function should_import( array $data ): bool { |
| 23 |
return ( |
| 24 |
isset( $data['include'] ) && |
| 25 |
in_array( 'settings', $data['include'], true ) && |
| 26 |
! empty( $data['extracted_directory_path'] ) && |
| 27 |
$this->is_classes_enabled( $data ) |
| 28 |
); |
| 29 |
} |
| 30 |
|
| 31 |
private function is_classes_enabled( array $data ): bool { |
| 32 |
if ( isset( $data['customization']['settings']['classes'] ) ) { |
| 33 |
return (bool) $data['customization']['settings']['classes']; |
| 34 |
} |
| 35 |
|
| 36 |
return true; |
| 37 |
} |
| 38 |
|
| 39 |
public function import( array $data, array $imported_data ): array { |
| 40 |
$kit = Plugin::$instance->kits_manager->get_active_kit(); |
| 41 |
|
| 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 []; |
| 47 |
} |
| 48 |
|
| 49 |
$global_classes_result = Global_Classes_Parser::make()->parse( $global_classes ); |
| 50 |
|
| 51 |
if ( ! $global_classes_result->is_valid() ) { |
| 52 |
return []; |
| 53 |
} |
| 54 |
|
| 55 |
$imported_classes = $global_classes_result->unwrap(); |
| 56 |
$repository = Global_Classes_Repository::make(); |
| 57 |
|
| 58 |
$override_all = ! empty( $data['customization']['settings']['classesOverrideAll'] ); |
| 59 |
|
| 60 |
if ( $override_all ) { |
| 61 |
$repository->put( |
| 62 |
$imported_classes['items'], |
| 63 |
$imported_classes['order'] |
| 64 |
); |
| 65 |
|
| 66 |
return $imported_classes; |
| 67 |
} |
| 68 |
|
| 69 |
$existing_classes = $repository->all()->get(); |
| 70 |
$merged = $this->merge_classes( $existing_classes, $imported_classes ); |
| 71 |
|
| 72 |
$repository->put( |
| 73 |
$merged['items'], |
| 74 |
$merged['order'] |
| 75 |
); |
| 76 |
|
| 77 |
return $imported_classes; |
| 78 |
} |
| 79 |
|
| 80 |
private function merge_classes( array $existing, array $imported ): array { |
| 81 |
$existing_items = $existing['items'] ?? []; |
| 82 |
$existing_order = $existing['order'] ?? []; |
| 83 |
$existing_labels = $this->get_existing_labels( $existing_items ); |
| 84 |
|
| 85 |
$imported_items = $imported['items'] ?? []; |
| 86 |
$imported_order = $imported['order'] ?? []; |
| 87 |
|
| 88 |
foreach ( $imported_order as $imported_id ) { |
| 89 |
if ( ! isset( $imported_items[ $imported_id ] ) ) { |
| 90 |
continue; |
| 91 |
} |
| 92 |
|
| 93 |
$imported_class = $imported_items[ $imported_id ]; |
| 94 |
|
| 95 |
$id_exists = array_key_exists( $imported_id, $existing_items ); |
| 96 |
$new_id = $id_exists |
| 97 |
? $this->generate_unique_id( array_keys( $existing_items ) ) |
| 98 |
: $imported_id; |
| 99 |
|
| 100 |
$original_label = $imported_class['label'] ?? $imported_id; |
| 101 |
$new_label = ImportExportUtils::resolve_label_conflict( $original_label, $existing_labels ); |
| 102 |
$existing_labels[] = strtolower( $new_label ); |
| 103 |
|
| 104 |
$imported_class['id'] = $new_id; |
| 105 |
$imported_class['label'] = $new_label; |
| 106 |
|
| 107 |
$existing_items[ $new_id ] = $imported_class; |
| 108 |
$existing_order[] = $new_id; |
| 109 |
} |
| 110 |
|
| 111 |
return [ |
| 112 |
'items' => $existing_items, |
| 113 |
'order' => $existing_order, |
| 114 |
]; |
| 115 |
} |
| 116 |
|
| 117 |
private function get_existing_labels( array $items ): array { |
| 118 |
$labels = []; |
| 119 |
|
| 120 |
foreach ( $items as $item ) { |
| 121 |
if ( isset( $item['label'] ) ) { |
| 122 |
$labels[] = strtolower( $item['label'] ); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
return $labels; |
| 127 |
} |
| 128 |
|
| 129 |
private function generate_unique_id( array $existing_ids ): string { |
| 130 |
return Utils::generate_id( 'g-', $existing_ids ); |
| 131 |
} |
| 132 |
} |
| 133 |
|