| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\DefaultStyles\ImportExportCustomization\Runners; |
| 4 |
|
| 5 |
use Elementor\App\Modules\ImportExportCustomization\Design_System_Import_Context; |
| 6 |
use Elementor\App\Modules\ImportExportCustomization\Runners\Import\Import_Runner_Base; |
| 7 |
use Elementor\App\Modules\ImportExportCustomization\Utils as ImportExportUtils; |
| 8 |
use Elementor\Modules\AtomicWidgets\Module as Atomic_Widgets_Module; |
| 9 |
use Elementor\Modules\DefaultStyles\Default_Styles_Repository; |
| 10 |
use Elementor\Modules\DefaultStyles\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 'default-styles'; |
| 20 |
} |
| 21 |
|
| 22 |
public function should_import( array $data ): bool { |
| 23 |
$import_context = Design_System_Import_Context::from_data( $data ); |
| 24 |
|
| 25 |
return ( |
| 26 |
$import_context->is_included() && |
| 27 |
! empty( $data['extracted_directory_path'] ) && |
| 28 |
$this->is_feature_active() |
| 29 |
); |
| 30 |
} |
| 31 |
|
| 32 |
private function is_feature_active(): bool { |
| 33 |
return Plugin::$instance->experiments->is_feature_active( Atomic_Widgets_Module::EXPERIMENT_NAME ); |
| 34 |
} |
| 35 |
|
| 36 |
public function import( array $data, array $imported_data ): array { |
| 37 |
$kit = Plugin::$instance->kits_manager->get_active_kit(); |
| 38 |
$default_styles_dir = $data['extracted_directory_path'] . '/' . Import_Export_Customization::DIRECTORY_NAME; |
| 39 |
|
| 40 |
if ( ! $kit || ! is_dir( $default_styles_dir ) ) { |
| 41 |
return []; |
| 42 |
} |
| 43 |
|
| 44 |
$repository = Default_Styles_Repository::make( $kit ); |
| 45 |
$imported_tags = []; |
| 46 |
|
| 47 |
foreach ( glob( $default_styles_dir . '/*.json' ) as $file_path ) { |
| 48 |
$tag = basename( $file_path, '.json' ); |
| 49 |
|
| 50 |
if ( ! Default_Styles_Repository::is_allowed_tag( $tag ) ) { |
| 51 |
continue; |
| 52 |
} |
| 53 |
|
| 54 |
$style_data = ImportExportUtils::read_json_file( $file_path ); |
| 55 |
|
| 56 |
if ( ! $style_data ) { |
| 57 |
continue; |
| 58 |
} |
| 59 |
|
| 60 |
if ( ! $repository->put( $tag, $style_data ) ) { |
| 61 |
continue; |
| 62 |
} |
| 63 |
|
| 64 |
$imported_tags[] = $tag; |
| 65 |
} |
| 66 |
|
| 67 |
return [ |
| 68 |
'imported' => $imported_tags, |
| 69 |
]; |
| 70 |
} |
| 71 |
} |
| 72 |
|