← All changes
|
app/modules/import-export/processes/import.php
+29
-42
4.3.0-beta3
→
3.13.3
View file →
| @@ -7,10 +7,8 @@ | ||
| 7 | 7 | use Elementor\App\Modules\ImportExport\Compatibility\Kit_Library; |
| 8 | 8 | use Elementor\App\Modules\ImportExport\Utils; |
| 9 | 9 | use Elementor\Core\Base\Document; |
| 10 | 10 | use Elementor\Core\Kits\Documents\Kit; |
| 11 | -use Elementor\Modules\AtomicWidgets\Utils\Atomic_Prop_Remap; | |
| 12 | -use Elementor\Modules\Components\Module as Components_Module; | |
| 13 | 11 | use Elementor\Plugin; |
| 14 | 12 | |
| 15 | 13 | use Elementor\App\Modules\ImportExport\Runners\Import\Elementor_Content; |
| 16 | 14 | use Elementor\App\Modules\ImportExport\Runners\Import\Import_Runner_Base; |
| @@ -19,8 +17,9 @@ | ||
| 19 | 17 | use Elementor\App\Modules\ImportExport\Runners\Import\Taxonomies; |
| 20 | 18 | use Elementor\App\Modules\ImportExport\Runners\Import\Templates; |
| 21 | 19 | use Elementor\App\Modules\ImportExport\Runners\Import\Wp_Content; |
| 22 | 20 | use Elementor\App\Modules\ImportExport\Module; |
| 21 | +use Elementor\App\Modules\KitLibrary\Connect\Kit_Library as Kit_Library_Api; | |
| 23 | 22 | |
| 24 | 23 | class Import { |
| 25 | 24 | const MANIFEST_ERROR_KEY = 'manifest-error'; |
| 26 | 25 | |
| @@ -140,15 +139,14 @@ | ||
| 140 | 139 | */ |
| 141 | 140 | private $runners_import_metadata = []; |
| 142 | 141 | |
| 143 | 142 | /** |
| 144 | - * @param string $path session_id | zip_file_path | |
| 145 | - * @param array $settings Use to determine which content to import. | |
| 146 | - * (e.g: include, selected_plugins, selected_cpt, selected_override_conditions, etc.) | |
| 143 | + * @param string $path session_id | zip_file_path | |
| 144 | + * @param array $settings Use to determine which content to import. | |
| 145 | + * (e.g: include, selected_plugins, selected_cpt, selected_override_conditions, etc.) | |
| 147 | 146 | * @param array|null $old_instance An array of old instance parameters that will be used for creating new instance. |
| 148 | - * We are using it for quick creation of the instance when the import process is being split into chunks. | |
| 149 | - * | |
| 150 | - * @throws \Exception If the import session does not exist. | |
| 147 | + * We are using it for quick creation of the instance when the import process is being split into chunks. | |
| 148 | + * @throws \Exception | |
| 151 | 149 | */ |
| 152 | 150 | public function __construct( string $path, array $settings = [], array $old_instance = null ) { |
| 153 | 151 | if ( ! empty( $old_instance ) ) { |
| 154 | 152 | $this->set_import_object( $old_instance ); |
| @@ -222,12 +220,12 @@ | ||
| 222 | 220 | * |
| 223 | 221 | * @param string $session_id |
| 224 | 222 | * |
| 225 | 223 | * @return Import |
| 226 | - * @throws \Exception If the import session does not exist. | |
| 224 | + * @throws \Exception | |
| 227 | 225 | */ |
| 228 | 226 | public static function from_session( string $session_id ): Import { |
| 229 | - $import_sessions = Utils::get_import_sessions(); | |
| 227 | + $import_sessions = get_option( Module::OPTION_KEY_ELEMENTOR_IMPORT_SESSIONS ); | |
| 230 | 228 | |
| 231 | 229 | if ( ! $import_sessions || ! isset( $import_sessions[ $session_id ] ) ) { |
| 232 | 230 | throw new \Exception( 'Couldn’t execute the import process because the import session does not exist.' ); |
| 233 | 231 | } |
| @@ -304,9 +302,8 @@ | ||
| 304 | 302 | ]; |
| 305 | 303 | |
| 306 | 304 | $this->init_import_session(); |
| 307 | 305 | |
| 308 | - remove_filter( 'elementor/document/save/data', [ Plugin::$instance->modules_manager->get_modules( 'content-sanitizer' ), 'sanitize_content' ] ); | |
| 309 | 306 | add_filter( 'elementor/document/save/data', [ $this, 'prevent_saving_elements_on_post_creation' ], 10, 2 ); |
| 310 | 307 | |
| 311 | 308 | // Set the Request's state as an Elementor upload request, in order to support unfiltered file uploads. |
| 312 | 309 | Plugin::$instance->uploads_manager->set_elementor_upload_state( true ); |
| @@ -399,9 +396,9 @@ | ||
| 399 | 396 | * |
| 400 | 397 | * @return void |
| 401 | 398 | */ |
| 402 | 399 | public function init_import_session( $save_instance_data = false ) { |
| 403 | - $import_sessions = Utils::get_import_sessions( true ); | |
| 400 | + $import_sessions = get_option( Module::OPTION_KEY_ELEMENTOR_IMPORT_SESSIONS ); | |
| 404 | 401 | |
| 405 | 402 | $import_sessions[ $this->session_id ] = [ |
| 406 | 403 | 'session_id' => $this->session_id, |
| 407 | 404 | 'kit_title' => $this->manifest['title'] ?? '', |
| @@ -446,9 +443,20 @@ | ||
| 446 | 443 | if ( ! empty( $this->manifest['thumbnail'] ) ) { |
| 447 | 444 | return $this->manifest['thumbnail']; |
| 448 | 445 | } |
| 449 | 446 | |
| 450 | - return apply_filters( 'elementor/import/kit_thumbnail', '', $this->kit_id, $this->settings_referrer ); | |
| 447 | + if ( empty( $this->kit_id ) ) { | |
| 448 | + return ''; | |
| 449 | + } | |
| 450 | + | |
| 451 | + $api = new Kit_Library_Api(); | |
| 452 | + $kit = $api->get_by_id( $this->kit_id ); | |
| 453 | + | |
| 454 | + if ( is_wp_error( $kit ) ) { | |
| 455 | + return ''; | |
| 456 | + } | |
| 457 | + | |
| 458 | + return $kit->thumbnail; | |
| 451 | 459 | } |
| 452 | 460 | |
| 453 | 461 | public function get_runners_name(): array { |
| 454 | 462 | return array_keys( $this->runners ); |
| @@ -561,9 +569,9 @@ | ||
| 561 | 569 | |
| 562 | 570 | /** |
| 563 | 571 | * Prevent saving elements on elementor post creation. |
| 564 | 572 | * |
| 565 | - * @param array $data | |
| 573 | + * @param array $data | |
| 566 | 574 | * @param Document $document |
| 567 | 575 | * |
| 568 | 576 | * @return array |
| 569 | 577 | */ |
| @@ -586,9 +594,8 @@ | ||
| 586 | 594 | * Extract the zip file. |
| 587 | 595 | * |
| 588 | 596 | * @param string $zip_path The path to the zip file. |
| 589 | 597 | * @return string The extracted directory path. |
| 590 | - * @throws \Error If import process fails, file validation errors occur, or data corruption is detected. | |
| 591 | 598 | */ |
| 592 | 599 | private function extract_zip( $zip_path ) { |
| 593 | 600 | $extraction_result = Plugin::$instance->uploads_manager->extract_and_validate_zip( $zip_path, [ 'json', 'xml' ] ); |
| 594 | 601 | |
| @@ -593,12 +600,12 @@ | ||
| 593 | 600 | $extraction_result = Plugin::$instance->uploads_manager->extract_and_validate_zip( $zip_path, [ 'json', 'xml' ] ); |
| 594 | 601 | |
| 595 | 602 | if ( is_wp_error( $extraction_result ) ) { |
| 596 | 603 | if ( isset( $extraction_result->errors['zip_error'] ) ) { |
| 597 | - throw new \Error( static::ZIP_ARCHIVE_ERROR_KEY ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped | |
| 604 | + throw new \Error( static::ZIP_ARCHIVE_ERROR_KEY ); | |
| 598 | 605 | } |
| 599 | 606 | |
| 600 | - throw new \Error( static::ZIP_FILE_ERROR_KEY ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped | |
| 607 | + throw new \Error( static::ZIP_FILE_ERROR_KEY ); | |
| 601 | 608 | } |
| 602 | 609 | |
| 603 | 610 | return $extraction_result['extraction_directory']; |
| 604 | 611 | } |
| @@ -606,10 +613,8 @@ | ||
| 606 | 613 | /** |
| 607 | 614 | * Get the manifest file from the extracted directory and adapt it if needed. |
| 608 | 615 | * |
| 609 | 616 | * @return string The manifest file content. |
| 610 | - * | |
| 611 | - * @throws \Error If import validation fails or processing errors occur. | |
| 612 | 617 | */ |
| 613 | 618 | private function read_manifest_json() { |
| 614 | 619 | $manifest = Utils::read_json_file( $this->extracted_directory_path . 'manifest' ); |
| 615 | 620 | |
| @@ -614,9 +619,9 @@ | ||
| 614 | 619 | $manifest = Utils::read_json_file( $this->extracted_directory_path . 'manifest' ); |
| 615 | 620 | |
| 616 | 621 | if ( ! $manifest ) { |
| 617 | 622 | Plugin::$instance->logger->get_logger()->error( static::MANIFEST_ERROR_KEY ); |
| 618 | - throw new \Error( static::ZIP_FILE_ERROR_KEY ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped | |
| 623 | + throw new \Error( static::ZIP_FILE_ERROR_KEY ); | |
| 619 | 624 | } |
| 620 | 625 | |
| 621 | 626 | $this->init_adapters( $manifest ); |
| 622 | 627 | |
| @@ -723,9 +728,9 @@ | ||
| 723 | 728 | * Get the data that requires updating/replacement when imported. |
| 724 | 729 | * |
| 725 | 730 | * @return array{post_ids: array, term_ids: array} |
| 726 | 731 | */ |
| 727 | - private function get_imported_data_replacements(): array { | |
| 732 | + private function get_imported_data_replacements() : array { | |
| 728 | 733 | return [ |
| 729 | 734 | 'post_ids' => Utils::map_old_new_post_ids( $this->imported_data ), |
| 730 | 735 | 'term_ids' => Utils::map_old_new_term_ids( $this->imported_data ), |
| 731 | 736 | ]; |
| @@ -741,11 +746,8 @@ | ||
| 741 | 746 | foreach ( $this->documents_data as $new_id => $data ) { |
| 742 | 747 | $document = Plugin::$instance->documents->get( $new_id ); |
| 743 | 748 | |
| 744 | 749 | if ( isset( $data['elements'] ) ) { |
| 745 | - $data['elements'] = Components_Module::prepare_imported_elements( $data['elements'], $imported_data_replacements['post_ids'] ?? [] ); | |
| 746 | - $data['elements'] = Atomic_Prop_Remap::apply( $data['elements'], $imported_data_replacements ); | |
| 747 | - $this->merge_atomic_prop_remap_warnings( Atomic_Prop_Remap::consume_warnings() ); | |
| 748 | 750 | $data['elements'] = $document->on_import_update_dynamic_content( $data['elements'], $imported_data_replacements ); |
| 749 | 751 | } |
| 750 | 752 | |
| 751 | 753 | if ( isset( $data['settings'] ) ) { |
| @@ -761,21 +763,10 @@ | ||
| 761 | 763 | $document->save( $data ); |
| 762 | 764 | } |
| 763 | 765 | } |
| 764 | 766 | |
| 765 | - private function merge_atomic_prop_remap_warnings( array $warnings ): void { | |
| 766 | - if ( empty( $warnings ) ) { | |
| 767 | - return; | |
| 768 | - } | |
| 769 | - | |
| 770 | - $existing = $this->runners_import_metadata['atomic_prop_remap']['warnings'] ?? []; | |
| 771 | - $this->runners_import_metadata['atomic_prop_remap'] = [ | |
| 772 | - 'warnings' => array_merge( $existing, $warnings ), | |
| 773 | - ]; | |
| 774 | - } | |
| 775 | - | |
| 776 | 767 | private function update_instance_data_in_import_session_option() { |
| 777 | - $import_sessions = Utils::get_import_sessions(); | |
| 768 | + $import_sessions = get_option( Module::OPTION_KEY_ELEMENTOR_IMPORT_SESSIONS ); | |
| 778 | 769 | |
| 779 | 770 | $import_sessions[ $this->session_id ]['instance_data']['documents_data'] = $this->documents_data; |
| 780 | 771 | $import_sessions[ $this->session_id ]['instance_data']['imported_data'] = $this->imported_data; |
| 781 | 772 | $import_sessions[ $this->session_id ]['instance_data']['runners_import_metadata'] = $this->runners_import_metadata; |
| @@ -782,14 +773,10 @@ | ||
| 782 | 773 | |
| 783 | 774 | update_option( Module::OPTION_KEY_ELEMENTOR_IMPORT_SESSIONS, $import_sessions, false ); |
| 784 | 775 | } |
| 785 | 776 | |
| 786 | - public function finalize_import_session_option() { | |
| 787 | - $import_sessions = Utils::get_import_sessions(); | |
| 788 | - | |
| 789 | - if ( ! isset( $import_sessions[ $this->session_id ] ) ) { | |
| 790 | - return; | |
| 791 | - } | |
| 777 | + private function finalize_import_session_option() { | |
| 778 | + $import_sessions = get_option( Module::OPTION_KEY_ELEMENTOR_IMPORT_SESSIONS ); | |
| 792 | 779 | |
| 793 | 780 | unset( $import_sessions[ $this->session_id ]['instance_data'] ); |
| 794 | 781 | |
| 795 | 782 | $import_sessions[ $this->session_id ]['end_timestamp'] = current_time( 'timestamp' ); |