| 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 = $this->get_existing_collection( $repository, $imported_data ); |
| 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 get_existing_collection( Variables_Repository $repository, array $imported_data ): Variables_Collection { |
| 71 |
$existing = $repository->load(); |
| 72 |
|
| 73 |
if ( count( $existing->all() ) > 0 ) { |
| 74 |
return $existing; |
| 75 |
} |
| 76 |
|
| 77 |
$was_new_kit_created = ! empty( $imported_data['site-settings']['imported_kit_id'] ); |
| 78 |
|
| 79 |
if ( ! $was_new_kit_created ) { |
| 80 |
return $existing; |
| 81 |
} |
| 82 |
|
| 83 |
$previous_kit_id = Plugin::$instance->kits_manager->get_previous_id(); |
| 84 |
|
| 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(); |
| 93 |
} |
| 94 |
|
| 95 |
private function merge_collections( |
| 96 |
Variables_Collection $existing, |
| 97 |
Variables_Collection $imported |
| 98 |
): Variables_Collection { |
| 99 |
$existing_labels = $this->get_existing_labels( $existing ); |
| 100 |
$existing_ids = array_keys( $existing->all() ); |
| 101 |
|
| 102 |
foreach ( $imported->all() as $variable ) { |
| 103 |
if ( $variable->is_deleted() ) { |
| 104 |
continue; |
| 105 |
} |
| 106 |
|
| 107 |
$original_id = $variable->id(); |
| 108 |
$id_exists = in_array( $original_id, $existing_ids, true ); |
| 109 |
$new_id = $id_exists |
| 110 |
? $this->generate_unique_id( $existing_ids ) |
| 111 |
: $original_id; |
| 112 |
$existing_ids[] = $new_id; |
| 113 |
|
| 114 |
$new_label = ImportExportUtils::resolve_label_conflict( $variable->label(), $existing_labels ); |
| 115 |
$existing_labels[] = strtolower( $new_label ); |
| 116 |
|
| 117 |
$new_variable = Variable::create_new( [ |
| 118 |
'id' => $new_id, |
| 119 |
'type' => $variable->type(), |
| 120 |
'label' => $new_label, |
| 121 |
'value' => $variable->value(), |
| 122 |
'order' => $existing->get_next_order(), |
| 123 |
] ); |
| 124 |
|
| 125 |
$existing->add_variable( $new_variable ); |
| 126 |
} |
| 127 |
|
| 128 |
return $existing; |
| 129 |
} |
| 130 |
|
| 131 |
private function get_existing_labels( Variables_Collection $collection ): array { |
| 132 |
$labels = []; |
| 133 |
|
| 134 |
foreach ( $collection->all() as $variable ) { |
| 135 |
if ( ! $variable->is_deleted() ) { |
| 136 |
$labels[] = strtolower( $variable->label() ); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
return $labels; |
| 141 |
} |
| 142 |
|
| 143 |
private function generate_unique_id( array $existing_ids ): string { |
| 144 |
return Utils::generate_id( 'e-gv-', $existing_ids ); |
| 145 |
} |
| 146 |
|
| 147 |
private function save_collection( Variables_Repository $repository, Variables_Collection $collection ): void { |
| 148 |
$result = $repository->save( $collection ); |
| 149 |
|
| 150 |
if ( false === $result ) { |
| 151 |
throw new \RuntimeException( 'Failed to save global variables during import.' ); |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
|