| @@ -6,9 +6,8 @@ | ||
| 6 | 6 | use Elementor\App\Modules\ImportExport\Compatibility\Envato; |
| 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 | -use Elementor\Core\Kits\Documents\Kit; | |
| 11 | 10 | use Elementor\Plugin; |
| 12 | 11 | |
| 13 | 12 | use Elementor\App\Modules\ImportExport\Runners\Import\Elementor_Content; |
| 14 | 13 | use Elementor\App\Modules\ImportExport\Runners\Import\Import_Runner_Base; |
| @@ -21,12 +20,10 @@ | ||
| 21 | 20 | |
| 22 | 21 | class Import { |
| 23 | 22 | const MANIFEST_ERROR_KEY = 'manifest-error'; |
| 24 | 23 | |
| 25 | - const ZIP_FILE_ERROR_KEY = 'invalid-zip-file'; | |
| 24 | + const ZIP_FILE_ERROR_KEY = 'zip-file-error'; | |
| 26 | 25 | |
| 27 | - const ZIP_ARCHIVE_ERROR_KEY = 'zip-archive-module-missing'; | |
| 28 | - | |
| 29 | 26 | /** |
| 30 | 27 | * @var Import_Runner_Base[] |
| 31 | 28 | */ |
| 32 | 29 | protected $runners = []; |
| @@ -39,15 +36,8 @@ | ||
| 39 | 36 | */ |
| 40 | 37 | private $session_id; |
| 41 | 38 | |
| 42 | 39 | /** |
| 43 | - * The Kit ID. | |
| 44 | - * | |
| 45 | - * @var string | |
| 46 | - */ | |
| 47 | - private $kit_id; | |
| 48 | - | |
| 49 | - /** | |
| 50 | 40 | * Adapter for the kit compatibility. |
| 51 | 41 | * |
| 52 | 42 | * @var Base_Adapter[] |
| 53 | 43 | */ |
| @@ -53,13 +43,13 @@ | ||
| 53 | 43 | */ |
| 54 | 44 | private $adapters; |
| 55 | 45 | |
| 56 | 46 | /** |
| 57 | - * Document's data (elements and settings) that was imported during the process. | |
| 47 | + * Document's elements that imported during the process. | |
| 58 | 48 | * |
| 59 | - * @var array { [document_id] => { "elements": array , "settings": array } } | |
| 49 | + * @var array | |
| 60 | 50 | */ |
| 61 | - private $documents_data = []; | |
| 51 | + private $documents_elements = []; | |
| 62 | 52 | |
| 63 | 53 | /** |
| 64 | 54 | * Path to the extracted kit files. |
| 65 | 55 | * |
| @@ -138,15 +128,14 @@ | ||
| 138 | 128 | */ |
| 139 | 129 | private $runners_import_metadata = []; |
| 140 | 130 | |
| 141 | 131 | /** |
| 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.) | |
| 132 | + * @param string $path session_id | zip_file_path | |
| 133 | + * @param array $settings Use to determine which content to import. | |
| 134 | + * (e.g: include, selected_plugins, selected_cpt, selected_override_conditions, etc.) | |
| 145 | 135 | * @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. | |
| 136 | + * We are using it for quick creation of the instance when the import process is being split into chunks. | |
| 137 | + * @throws \Exception | |
| 149 | 138 | */ |
| 150 | 139 | public function __construct( string $path, array $settings = [], array $old_instance = null ) { |
| 151 | 140 | if ( ! empty( $old_instance ) ) { |
| 152 | 141 | $this->set_import_object( $old_instance ); |
| @@ -164,9 +153,8 @@ | ||
| 164 | 153 | $this->extracted_directory_path = $path . '/'; |
| 165 | 154 | } |
| 166 | 155 | |
| 167 | 156 | $this->session_id = basename( $this->extracted_directory_path ); |
| 168 | - $this->kit_id = $settings['id'] ?? ''; | |
| 169 | 157 | $this->settings_referrer = ! empty( $settings['referrer'] ) ? $settings['referrer'] : 'local'; |
| 170 | 158 | $this->settings_include = ! empty( $settings['include'] ) ? $settings['include'] : null; |
| 171 | 159 | |
| 172 | 160 | // Using isset and not empty is important since empty array is valid option. |
| @@ -178,12 +166,8 @@ | ||
| 178 | 166 | $this->site_settings = $this->read_site_settings_json(); |
| 179 | 167 | |
| 180 | 168 | $this->set_default_settings(); |
| 181 | 169 | } |
| 182 | - | |
| 183 | - add_filter( 'wp_php_error_args', function ( $args, $error ) { | |
| 184 | - return $this->filter_php_error_args( $args, $error ); | |
| 185 | - }, 10, 2 ); | |
| 186 | 170 | } |
| 187 | 171 | |
| 188 | 172 | /** |
| 189 | 173 | * Set the import object parameters. |
| @@ -209,9 +193,9 @@ | ||
| 209 | 193 | $this->settings_selected_override_conditions = $instance_data['settings_selected_override_conditions']; |
| 210 | 194 | $this->settings_selected_custom_post_types = $instance_data['settings_selected_custom_post_types']; |
| 211 | 195 | $this->settings_selected_plugins = $instance_data['settings_selected_plugins']; |
| 212 | 196 | |
| 213 | - $this->documents_data = $instance_data['documents_data']; | |
| 197 | + $this->documents_elements = $instance_data['documents_elements']; | |
| 214 | 198 | $this->imported_data = $instance_data['imported_data']; |
| 215 | 199 | $this->runners_import_metadata = $instance_data['runners_import_metadata']; |
| 216 | 200 | } |
| 217 | 201 | |
| @@ -220,12 +204,12 @@ | ||
| 220 | 204 | * |
| 221 | 205 | * @param string $session_id |
| 222 | 206 | * |
| 223 | 207 | * @return Import |
| 224 | - * @throws \Exception If the import session does not exist. | |
| 208 | + * @throws \Exception | |
| 225 | 209 | */ |
| 226 | 210 | public static function from_session( string $session_id ): Import { |
| 227 | - $import_sessions = Utils::get_import_sessions(); | |
| 211 | + $import_sessions = get_option( Module::OPTION_KEY_ELEMENTOR_IMPORT_SESSIONS ); | |
| 228 | 212 | |
| 229 | 213 | if ( ! $import_sessions || ! isset( $import_sessions[ $session_id ] ) ) { |
| 230 | 214 | throw new \Exception( 'Couldn’t execute the import process because the import session does not exist.' ); |
| 231 | 215 | } |
| @@ -302,9 +286,8 @@ | ||
| 302 | 286 | ]; |
| 303 | 287 | |
| 304 | 288 | $this->init_import_session(); |
| 305 | 289 | |
| 306 | - remove_filter( 'elementor/document/save/data', [ Plugin::$instance->modules_manager->get_modules( 'content-sanitizer' ), 'sanitize_content' ] ); | |
| 307 | 290 | add_filter( 'elementor/document/save/data', [ $this, 'prevent_saving_elements_on_post_creation' ], 10, 2 ); |
| 308 | 291 | |
| 309 | 292 | // Set the Request's state as an Elementor upload request, in order to support unfiltered file uploads. |
| 310 | 293 | Plugin::$instance->uploads_manager->set_elementor_upload_state( true ); |
| @@ -397,15 +380,15 @@ | ||
| 397 | 380 | * |
| 398 | 381 | * @return void |
| 399 | 382 | */ |
| 400 | 383 | public function init_import_session( $save_instance_data = false ) { |
| 401 | - $import_sessions = Utils::get_import_sessions( true ); | |
| 384 | + $import_sessions = get_option( Module::OPTION_KEY_ELEMENTOR_IMPORT_SESSIONS ); | |
| 402 | 385 | |
| 403 | 386 | $import_sessions[ $this->session_id ] = [ |
| 404 | 387 | 'session_id' => $this->session_id, |
| 405 | 388 | 'kit_title' => $this->manifest['title'] ?? '', |
| 406 | 389 | 'kit_name' => $this->manifest['name'] ?? '', |
| 407 | - 'kit_thumbnail' => $this->get_kit_thumbnail(), | |
| 390 | + 'kit_thumbnail' => $this->manifest['thumbnail'] ?? '', | |
| 408 | 391 | 'kit_source' => $this->settings_referrer, |
| 409 | 392 | 'user_id' => get_current_user_id(), |
| 410 | 393 | 'start_timestamp' => current_time( 'timestamp' ), |
| 411 | 394 | ]; |
| @@ -425,9 +408,9 @@ | ||
| 425 | 408 | 'settings_selected_override_conditions' => $this->settings_selected_override_conditions, |
| 426 | 409 | 'settings_selected_custom_post_types' => $this->settings_selected_custom_post_types, |
| 427 | 410 | 'settings_selected_plugins' => $this->settings_selected_plugins, |
| 428 | 411 | |
| 429 | - 'documents_data' => $this->documents_data, | |
| 412 | + 'documents_elements' => $this->documents_elements, | |
| 430 | 413 | 'imported_data' => $this->imported_data, |
| 431 | 414 | 'runners_import_metadata' => $this->runners_import_metadata, |
| 432 | 415 | ]; |
| 433 | 416 | } |
| @@ -434,21 +417,8 @@ | ||
| 434 | 417 | |
| 435 | 418 | update_option( Module::OPTION_KEY_ELEMENTOR_IMPORT_SESSIONS, $import_sessions, false ); |
| 436 | 419 | } |
| 437 | 420 | |
| 438 | - /** | |
| 439 | - * Get the Kit thumbnail, goes to the home page thumbnail if main doesn't exist | |
| 440 | - * | |
| 441 | - * @return string | |
| 442 | - */ | |
| 443 | - private function get_kit_thumbnail(): string { | |
| 444 | - if ( ! empty( $this->manifest['thumbnail'] ) ) { | |
| 445 | - return $this->manifest['thumbnail']; | |
| 446 | - } | |
| 447 | - | |
| 448 | - return apply_filters( 'elementor/import/kit_thumbnail', '', $this->kit_id, $this->settings_referrer ); | |
| 449 | - } | |
| 450 | - | |
| 451 | 421 | public function get_runners_name(): array { |
| 452 | 422 | return array_keys( $this->runners ); |
| 453 | 423 | } |
| 454 | 424 | |
| @@ -559,9 +529,9 @@ | ||
| 559 | 529 | |
| 560 | 530 | /** |
| 561 | 531 | * Prevent saving elements on elementor post creation. |
| 562 | 532 | * |
| 563 | - * @param array $data | |
| 533 | + * @param array $data | |
| 564 | 534 | * @param Document $document |
| 565 | 535 | * |
| 566 | 536 | * @return array |
| 567 | 537 | */ |
| @@ -566,18 +536,13 @@ | ||
| 566 | 536 | * @return array |
| 567 | 537 | */ |
| 568 | 538 | public function prevent_saving_elements_on_post_creation( array $data, Document $document ) { |
| 569 | 539 | if ( isset( $data['elements'] ) ) { |
| 570 | - $this->documents_data[ $document->get_main_id() ] = [ 'elements' => $data['elements'] ]; | |
| 540 | + $this->documents_elements[ $document->get_main_id() ] = $data['elements']; | |
| 571 | 541 | |
| 572 | 542 | $data['elements'] = []; |
| 573 | 543 | } |
| 574 | 544 | |
| 575 | - if ( isset( $data['settings'] ) ) { | |
| 576 | - $this->documents_data[ $document->get_main_id() ]['settings'] = $data['settings']; | |
| 577 | - | |
| 578 | - } | |
| 579 | - | |
| 580 | 545 | return $data; |
| 581 | 546 | } |
| 582 | 547 | |
| 583 | 548 | /** |
| @@ -584,19 +549,14 @@ | ||
| 584 | 549 | * Extract the zip file. |
| 585 | 550 | * |
| 586 | 551 | * @param string $zip_path The path to the zip file. |
| 587 | 552 | * @return string The extracted directory path. |
| 588 | - * @throws \Error If import process fails, file validation errors occur, or data corruption is detected. | |
| 589 | 553 | */ |
| 590 | 554 | private function extract_zip( $zip_path ) { |
| 591 | 555 | $extraction_result = Plugin::$instance->uploads_manager->extract_and_validate_zip( $zip_path, [ 'json', 'xml' ] ); |
| 592 | 556 | |
| 593 | 557 | 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 | |
| 558 | + throw new \Error( static::ZIP_FILE_ERROR_KEY ); | |
| 599 | 559 | } |
| 600 | 560 | |
| 601 | 561 | return $extraction_result['extraction_directory']; |
| 602 | 562 | } |
| @@ -604,17 +564,14 @@ | ||
| 604 | 564 | /** |
| 605 | 565 | * Get the manifest file from the extracted directory and adapt it if needed. |
| 606 | 566 | * |
| 607 | 567 | * @return string The manifest file content. |
| 608 | - * | |
| 609 | - * @throws \Error If import validation fails or processing errors occur. | |
| 610 | 568 | */ |
| 611 | 569 | private function read_manifest_json() { |
| 612 | 570 | $manifest = Utils::read_json_file( $this->extracted_directory_path . 'manifest' ); |
| 613 | 571 | |
| 614 | 572 | 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 | |
| 573 | + throw new \Error( static::MANIFEST_ERROR_KEY ); | |
| 617 | 574 | } |
| 618 | 575 | |
| 619 | 576 | $this->init_adapters( $manifest ); |
| 620 | 577 | |
| @@ -721,9 +678,9 @@ | ||
| 721 | 678 | * Get the data that requires updating/replacement when imported. |
| 722 | 679 | * |
| 723 | 680 | * @return array{post_ids: array, term_ids: array} |
| 724 | 681 | */ |
| 725 | - private function get_imported_data_replacements(): array { | |
| 682 | + private function get_imported_data_replacements() : array { | |
| 726 | 683 | return [ |
| 727 | 684 | 'post_ids' => Utils::map_old_new_post_ids( $this->imported_data ), |
| 728 | 685 | 'term_ids' => Utils::map_old_new_term_ids( $this->imported_data ), |
| 729 | 686 | ]; |
| @@ -733,35 +690,19 @@ | ||
| 733 | 690 | * Save the prevented elements on elementor post creation elements. |
| 734 | 691 | * Handle the replacement of all the dynamic content of the elements that probably have been changed during the import. |
| 735 | 692 | */ |
| 736 | 693 | private function save_elements_of_imported_posts() { |
| 737 | - $imported_data_replacements = $this->get_imported_data_replacements(); | |
| 738 | - | |
| 739 | - foreach ( $this->documents_data as $new_id => $data ) { | |
| 694 | + foreach ( $this->documents_elements as $new_id => $document_elements ) { | |
| 740 | 695 | $document = Plugin::$instance->documents->get( $new_id ); |
| 741 | - | |
| 742 | - if ( isset( $data['elements'] ) ) { | |
| 743 | - $data['elements'] = $document->on_import_update_dynamic_content( $data['elements'], $imported_data_replacements ); | |
| 744 | - } | |
| 745 | - | |
| 746 | - if ( isset( $data['settings'] ) ) { | |
| 747 | - | |
| 748 | - if ( $document instanceof Kit ) { | |
| 749 | - // Without post_status certain tabs in the Kit will not save properly. | |
| 750 | - $data['settings']['post_status'] = get_post_status( $new_id ); | |
| 751 | - } | |
| 752 | - | |
| 753 | - $data['settings'] = $document->on_import_update_settings( $data['settings'], $imported_data_replacements ); | |
| 754 | - } | |
| 755 | - | |
| 756 | - $document->save( $data ); | |
| 696 | + $updated_elements = $document->on_import_update_dynamic_content( $document_elements, $this->get_imported_data_replacements() ); | |
| 697 | + $document->save( [ 'elements' => $updated_elements ] ); | |
| 757 | 698 | } |
| 758 | 699 | } |
| 759 | 700 | |
| 760 | 701 | private function update_instance_data_in_import_session_option() { |
| 761 | - $import_sessions = Utils::get_import_sessions(); | |
| 702 | + $import_sessions = get_option( Module::OPTION_KEY_ELEMENTOR_IMPORT_SESSIONS ); | |
| 762 | 703 | |
| 763 | - $import_sessions[ $this->session_id ]['instance_data']['documents_data'] = $this->documents_data; | |
| 704 | + $import_sessions[ $this->session_id ]['instance_data']['documents_elements'] = $this->documents_elements; | |
| 764 | 705 | $import_sessions[ $this->session_id ]['instance_data']['imported_data'] = $this->imported_data; |
| 765 | 706 | $import_sessions[ $this->session_id ]['instance_data']['runners_import_metadata'] = $this->runners_import_metadata; |
| 766 | 707 | |
| 767 | 708 | update_option( Module::OPTION_KEY_ELEMENTOR_IMPORT_SESSIONS, $import_sessions, false ); |
| @@ -766,15 +707,11 @@ | ||
| 766 | 707 | |
| 767 | 708 | update_option( Module::OPTION_KEY_ELEMENTOR_IMPORT_SESSIONS, $import_sessions, false ); |
| 768 | 709 | } |
| 769 | 710 | |
| 770 | - public function finalize_import_session_option() { | |
| 771 | - $import_sessions = Utils::get_import_sessions(); | |
| 711 | + private function finalize_import_session_option() { | |
| 712 | + $import_sessions = get_option( Module::OPTION_KEY_ELEMENTOR_IMPORT_SESSIONS ); | |
| 772 | 713 | |
| 773 | - if ( ! isset( $import_sessions[ $this->session_id ] ) ) { | |
| 774 | - return; | |
| 775 | - } | |
| 776 | - | |
| 777 | 714 | unset( $import_sessions[ $this->session_id ]['instance_data'] ); |
| 778 | 715 | |
| 779 | 716 | $import_sessions[ $this->session_id ]['end_timestamp'] = current_time( 'timestamp' ); |
| 780 | 717 | $import_sessions[ $this->session_id ]['runners'] = $this->runners_import_metadata; |
| @@ -779,21 +716,6 @@ | ||
| 779 | 716 | $import_sessions[ $this->session_id ]['end_timestamp'] = current_time( 'timestamp' ); |
| 780 | 717 | $import_sessions[ $this->session_id ]['runners'] = $this->runners_import_metadata; |
| 781 | 718 | |
| 782 | 719 | 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 | 720 | } |
| 799 | 721 | } |