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