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