← All changes
|
app/modules/import-export/processes/import.php
+29
-68
4.3.0-beta3
→
3.13.2
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,16 +17,15 @@ | ||
| 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 | |
| 27 | - const ZIP_FILE_ERROR_KEY = 'invalid-zip-file'; | |
| 26 | + const ZIP_FILE_ERROR_KEY = 'zip-file-error'; | |
| 28 | 27 | |
| 29 | - const ZIP_ARCHIVE_ERROR_KEY = 'zip-archive-module-missing'; | |
| 30 | - | |
| 31 | 28 | /** |
| 32 | 29 | * @var Import_Runner_Base[] |
| 33 | 30 | */ |
| 34 | 31 | protected $runners = []; |
| @@ -140,15 +137,14 @@ | ||
| 140 | 137 | */ |
| 141 | 138 | private $runners_import_metadata = []; |
| 142 | 139 | |
| 143 | 140 | /** |
| 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.) | |
| 141 | + * @param string $path session_id | zip_file_path | |
| 142 | + * @param array $settings Use to determine which content to import. | |
| 143 | + * (e.g: include, selected_plugins, selected_cpt, selected_override_conditions, etc.) | |
| 147 | 144 | * @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. | |
| 145 | + * We are using it for quick creation of the instance when the import process is being split into chunks. | |
| 146 | + * @throws \Exception | |
| 151 | 147 | */ |
| 152 | 148 | public function __construct( string $path, array $settings = [], array $old_instance = null ) { |
| 153 | 149 | if ( ! empty( $old_instance ) ) { |
| 154 | 150 | $this->set_import_object( $old_instance ); |
| @@ -180,12 +176,8 @@ | ||
| 180 | 176 | $this->site_settings = $this->read_site_settings_json(); |
| 181 | 177 | |
| 182 | 178 | $this->set_default_settings(); |
| 183 | 179 | } |
| 184 | - | |
| 185 | - add_filter( 'wp_php_error_args', function ( $args, $error ) { | |
| 186 | - return $this->filter_php_error_args( $args, $error ); | |
| 187 | - }, 10, 2 ); | |
| 188 | 180 | } |
| 189 | 181 | |
| 190 | 182 | /** |
| 191 | 183 | * Set the import object parameters. |
| @@ -222,12 +214,12 @@ | ||
| 222 | 214 | * |
| 223 | 215 | * @param string $session_id |
| 224 | 216 | * |
| 225 | 217 | * @return Import |
| 226 | - * @throws \Exception If the import session does not exist. | |
| 218 | + * @throws \Exception | |
| 227 | 219 | */ |
| 228 | 220 | public static function from_session( string $session_id ): Import { |
| 229 | - $import_sessions = Utils::get_import_sessions(); | |
| 221 | + $import_sessions = get_option( Module::OPTION_KEY_ELEMENTOR_IMPORT_SESSIONS ); | |
| 230 | 222 | |
| 231 | 223 | if ( ! $import_sessions || ! isset( $import_sessions[ $session_id ] ) ) { |
| 232 | 224 | throw new \Exception( 'Couldn’t execute the import process because the import session does not exist.' ); |
| 233 | 225 | } |
| @@ -304,9 +296,8 @@ | ||
| 304 | 296 | ]; |
| 305 | 297 | |
| 306 | 298 | $this->init_import_session(); |
| 307 | 299 | |
| 308 | - remove_filter( 'elementor/document/save/data', [ Plugin::$instance->modules_manager->get_modules( 'content-sanitizer' ), 'sanitize_content' ] ); | |
| 309 | 300 | add_filter( 'elementor/document/save/data', [ $this, 'prevent_saving_elements_on_post_creation' ], 10, 2 ); |
| 310 | 301 | |
| 311 | 302 | // Set the Request's state as an Elementor upload request, in order to support unfiltered file uploads. |
| 312 | 303 | Plugin::$instance->uploads_manager->set_elementor_upload_state( true ); |
| @@ -399,9 +390,9 @@ | ||
| 399 | 390 | * |
| 400 | 391 | * @return void |
| 401 | 392 | */ |
| 402 | 393 | public function init_import_session( $save_instance_data = false ) { |
| 403 | - $import_sessions = Utils::get_import_sessions( true ); | |
| 394 | + $import_sessions = get_option( Module::OPTION_KEY_ELEMENTOR_IMPORT_SESSIONS ); | |
| 404 | 395 | |
| 405 | 396 | $import_sessions[ $this->session_id ] = [ |
| 406 | 397 | 'session_id' => $this->session_id, |
| 407 | 398 | 'kit_title' => $this->manifest['title'] ?? '', |
| @@ -446,9 +437,20 @@ | ||
| 446 | 437 | if ( ! empty( $this->manifest['thumbnail'] ) ) { |
| 447 | 438 | return $this->manifest['thumbnail']; |
| 448 | 439 | } |
| 449 | 440 | |
| 450 | - return apply_filters( 'elementor/import/kit_thumbnail', '', $this->kit_id, $this->settings_referrer ); | |
| 441 | + if ( empty( $this->kit_id ) ) { | |
| 442 | + return ''; | |
| 443 | + } | |
| 444 | + | |
| 445 | + $api = new Kit_Library_Api(); | |
| 446 | + $kit = $api->get_by_id( $this->kit_id ); | |
| 447 | + | |
| 448 | + if ( is_wp_error( $kit ) ) { | |
| 449 | + return ''; | |
| 450 | + } | |
| 451 | + | |
| 452 | + return $kit->thumbnail; | |
| 451 | 453 | } |
| 452 | 454 | |
| 453 | 455 | public function get_runners_name(): array { |
| 454 | 456 | return array_keys( $this->runners ); |
| @@ -561,9 +563,9 @@ | ||
| 561 | 563 | |
| 562 | 564 | /** |
| 563 | 565 | * Prevent saving elements on elementor post creation. |
| 564 | 566 | * |
| 565 | - * @param array $data | |
| 567 | + * @param array $data | |
| 566 | 568 | * @param Document $document |
| 567 | 569 | * |
| 568 | 570 | * @return array |
| 569 | 571 | */ |
| @@ -586,19 +588,14 @@ | ||
| 586 | 588 | * Extract the zip file. |
| 587 | 589 | * |
| 588 | 590 | * @param string $zip_path The path to the zip file. |
| 589 | 591 | * @return string The extracted directory path. |
| 590 | - * @throws \Error If import process fails, file validation errors occur, or data corruption is detected. | |
| 591 | 592 | */ |
| 592 | 593 | private function extract_zip( $zip_path ) { |
| 593 | 594 | $extraction_result = Plugin::$instance->uploads_manager->extract_and_validate_zip( $zip_path, [ 'json', 'xml' ] ); |
| 594 | 595 | |
| 595 | 596 | if ( is_wp_error( $extraction_result ) ) { |
| 596 | - if ( isset( $extraction_result->errors['zip_error'] ) ) { | |
| 597 | - throw new \Error( static::ZIP_ARCHIVE_ERROR_KEY ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped | |
| 598 | - } | |
| 599 | - | |
| 600 | - throw new \Error( static::ZIP_FILE_ERROR_KEY ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped | |
| 597 | + throw new \Error( static::ZIP_FILE_ERROR_KEY ); | |
| 601 | 598 | } |
| 602 | 599 | |
| 603 | 600 | return $extraction_result['extraction_directory']; |
| 604 | 601 | } |
| @@ -606,17 +603,14 @@ | ||
| 606 | 603 | /** |
| 607 | 604 | * Get the manifest file from the extracted directory and adapt it if needed. |
| 608 | 605 | * |
| 609 | 606 | * @return string The manifest file content. |
| 610 | - * | |
| 611 | - * @throws \Error If import validation fails or processing errors occur. | |
| 612 | 607 | */ |
| 613 | 608 | private function read_manifest_json() { |
| 614 | 609 | $manifest = Utils::read_json_file( $this->extracted_directory_path . 'manifest' ); |
| 615 | 610 | |
| 616 | 611 | if ( ! $manifest ) { |
| 617 | - 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 | |
| 612 | + throw new \Error( static::MANIFEST_ERROR_KEY ); | |
| 619 | 613 | } |
| 620 | 614 | |
| 621 | 615 | $this->init_adapters( $manifest ); |
| 622 | 616 | |
| @@ -723,9 +717,9 @@ | ||
| 723 | 717 | * Get the data that requires updating/replacement when imported. |
| 724 | 718 | * |
| 725 | 719 | * @return array{post_ids: array, term_ids: array} |
| 726 | 720 | */ |
| 727 | - private function get_imported_data_replacements(): array { | |
| 721 | + private function get_imported_data_replacements() : array { | |
| 728 | 722 | return [ |
| 729 | 723 | 'post_ids' => Utils::map_old_new_post_ids( $this->imported_data ), |
| 730 | 724 | 'term_ids' => Utils::map_old_new_term_ids( $this->imported_data ), |
| 731 | 725 | ]; |
| @@ -741,11 +735,8 @@ | ||
| 741 | 735 | foreach ( $this->documents_data as $new_id => $data ) { |
| 742 | 736 | $document = Plugin::$instance->documents->get( $new_id ); |
| 743 | 737 | |
| 744 | 738 | 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 | 739 | $data['elements'] = $document->on_import_update_dynamic_content( $data['elements'], $imported_data_replacements ); |
| 749 | 740 | } |
| 750 | 741 | |
| 751 | 742 | if ( isset( $data['settings'] ) ) { |
| @@ -761,21 +752,10 @@ | ||
| 761 | 752 | $document->save( $data ); |
| 762 | 753 | } |
| 763 | 754 | } |
| 764 | 755 | |
| 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 | 756 | private function update_instance_data_in_import_session_option() { |
| 777 | - $import_sessions = Utils::get_import_sessions(); | |
| 757 | + $import_sessions = get_option( Module::OPTION_KEY_ELEMENTOR_IMPORT_SESSIONS ); | |
| 778 | 758 | |
| 779 | 759 | $import_sessions[ $this->session_id ]['instance_data']['documents_data'] = $this->documents_data; |
| 780 | 760 | $import_sessions[ $this->session_id ]['instance_data']['imported_data'] = $this->imported_data; |
| 781 | 761 | $import_sessions[ $this->session_id ]['instance_data']['runners_import_metadata'] = $this->runners_import_metadata; |
| @@ -782,15 +762,11 @@ | ||
| 782 | 762 | |
| 783 | 763 | update_option( Module::OPTION_KEY_ELEMENTOR_IMPORT_SESSIONS, $import_sessions, false ); |
| 784 | 764 | } |
| 785 | 765 | |
| 786 | - public function finalize_import_session_option() { | |
| 787 | - $import_sessions = Utils::get_import_sessions(); | |
| 766 | + private function finalize_import_session_option() { | |
| 767 | + $import_sessions = get_option( Module::OPTION_KEY_ELEMENTOR_IMPORT_SESSIONS ); | |
| 788 | 768 | |
| 789 | - if ( ! isset( $import_sessions[ $this->session_id ] ) ) { | |
| 790 | - return; | |
| 791 | - } | |
| 792 | - | |
| 793 | 769 | unset( $import_sessions[ $this->session_id ]['instance_data'] ); |
| 794 | 770 | |
| 795 | 771 | $import_sessions[ $this->session_id ]['end_timestamp'] = current_time( 'timestamp' ); |
| 796 | 772 | $import_sessions[ $this->session_id ]['runners'] = $this->runners_import_metadata; |
| @@ -795,21 +771,6 @@ | ||
| 795 | 771 | $import_sessions[ $this->session_id ]['end_timestamp'] = current_time( 'timestamp' ); |
| 796 | 772 | $import_sessions[ $this->session_id ]['runners'] = $this->runners_import_metadata; |
| 797 | 773 | |
| 798 | 774 | update_option( Module::OPTION_KEY_ELEMENTOR_IMPORT_SESSIONS, $import_sessions, false ); |
| 799 | - } | |
| 800 | - | |
| 801 | - /** | |
| 802 | - * Filter the php error args and return 408 status code if the error is a timeout. | |
| 803 | - * | |
| 804 | - * @param array $args | |
| 805 | - * @param array $error | |
| 806 | - * @return array | |
| 807 | - */ | |
| 808 | - private function filter_php_error_args( $args, $error ) { | |
| 809 | - if ( strpos( $error['message'], 'Maximum execution time' ) !== false ) { | |
| 810 | - $args['response'] = 408; | |
| 811 | - } | |
| 812 | - | |
| 813 | - return $args; | |
| 814 | 775 | } |
| 815 | 776 | } |