PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.0-beta2
Elementor Website Builder – more than just a page builder v4.3.0-beta2
4.3.0 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 All 453 releases
elementor / app / modules / import-export / processes / import.php

import.php in Elementor Website Builder – more than just a page builder 4.3.0-beta2, at app/modules/import-export/processes/import.php

802 lines 24.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\App\Modules\ImportExport\Processes;
4
5 use Elementor\App\Modules\ImportExport\Compatibility\Base_Adapter;
6 use Elementor\App\Modules\ImportExport\Compatibility\Envato;
7 use Elementor\App\Modules\ImportExport\Compatibility\Kit_Library;
8 use Elementor\App\Modules\ImportExport\Utils;
9 use Elementor\Core\Base\Document;
10 use Elementor\Core\Kits\Documents\Kit;
11 use Elementor\Modules\Components\Module as Components_Module;
12 use Elementor\Plugin;
13
14 use Elementor\App\Modules\ImportExport\Runners\Import\Elementor_Content;
15 use Elementor\App\Modules\ImportExport\Runners\Import\Import_Runner_Base;
16 use Elementor\App\Modules\ImportExport\Runners\Import\Plugins;
17 use Elementor\App\Modules\ImportExport\Runners\Import\Site_Settings;
18 use Elementor\App\Modules\ImportExport\Runners\Import\Taxonomies;
19 use Elementor\App\Modules\ImportExport\Runners\Import\Templates;
20 use Elementor\App\Modules\ImportExport\Runners\Import\Wp_Content;
21 use Elementor\App\Modules\ImportExport\Module;
22
23 class Import {
24 const MANIFEST_ERROR_KEY = 'manifest-error';
25
26 const ZIP_FILE_ERROR_KEY = 'invalid-zip-file';
27
28 const ZIP_ARCHIVE_ERROR_KEY = 'zip-archive-module-missing';
29
30 /**
31 * @var Import_Runner_Base[]
32 */
33 protected $runners = [];
34
35 /**
36 * The session ID of the import process.
37 * This ID is uniquely generated for each import process (by the temp folder which contains the extracted kit files).
38 *
39 * @var string
40 */
41 private $session_id;
42
43 /**
44 * The Kit ID.
45 *
46 * @var string
47 */
48 private $kit_id;
49
50 /**
51 * Adapter for the kit compatibility.
52 *
53 * @var Base_Adapter[]
54 */
55 private $adapters;
56
57 /**
58 * Document's data (elements and settings) that was imported during the process.
59 *
60 * @var array { [document_id] => { "elements": array , "settings": array } }
61 */
62 private $documents_data = [];
63
64 /**
65 * Path to the extracted kit files.
66 *
67 * @var string
68 */
69 private $extracted_directory_path;
70
71 /**
72 * Imported kit manifest.
73 *
74 * @var array
75 */
76 private $manifest;
77
78 /**
79 * Imported kit site settings. (e.g: custom_colors, custom_typography, etc.)
80 *
81 * @var array
82 */
83 private $site_settings;
84
85 /**
86 * Selected content types to import.
87 *
88 * @var array
89 */
90 private $settings_include;
91
92 /**
93 * Referer of the import. (e.g: kit-library, local, etc.)
94 *
95 * @var string
96 */
97 private $settings_referrer;
98
99 /**
100 * All the conflict between the exited templates and the kit templates.
101 *
102 * @var array
103 */
104 private $settings_conflicts;
105
106 /**
107 * Selected elementor templates conditions to override.
108 *
109 * @var array
110 */
111 private $settings_selected_override_conditions;
112
113 /**
114 * Selected custom post types to import.
115 *
116 * @var array
117 */
118 private $settings_selected_custom_post_types;
119
120 /**
121 * Selected plugins to import.
122 *
123 * @var array
124 */
125 private $settings_selected_plugins;
126
127 /**
128 * The imported data output.
129 *
130 * @var array
131 */
132 private $imported_data = [];
133
134 /**
135 * The metadata output of the import runners.
136 * Will be saved in the import_session and will be used to revert the import process.
137 *
138 * @var array
139 */
140 private $runners_import_metadata = [];
141
142 /**
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.)
146 * @param array|null $old_instance An array of old instance parameters that will be used for creating new instance.
147 * We are using it for quick creation of the instance when the import process is being split into chunks.
148 *
149 * @throws \Exception If the import session does not exist.
150 */
151 public function __construct( string $path, array $settings = [], array $old_instance = null ) {
152 if ( ! empty( $old_instance ) ) {
153 $this->set_import_object( $old_instance );
154 } else {
155 if ( is_file( $path ) ) {
156 $this->extracted_directory_path = $this->extract_zip( $path );
157 } else {
158 $elementor_tmp_directory = Plugin::$instance->uploads_manager->get_temp_dir();
159 $path = $elementor_tmp_directory . basename( $path );
160
161 if ( ! is_dir( $path ) ) {
162 throw new \Exception( 'Couldn’t execute the import process because the import session does not exist.' );
163 }
164
165 $this->extracted_directory_path = $path . '/';
166 }
167
168 $this->session_id = basename( $this->extracted_directory_path );
169 $this->kit_id = $settings['id'] ?? '';
170 $this->settings_referrer = ! empty( $settings['referrer'] ) ? $settings['referrer'] : 'local';
171 $this->settings_include = ! empty( $settings['include'] ) ? $settings['include'] : null;
172
173 // Using isset and not empty is important since empty array is valid option.
174 $this->settings_selected_override_conditions = $settings['overrideConditions'] ?? null;
175 $this->settings_selected_custom_post_types = $settings['selectedCustomPostTypes'] ?? null;
176 $this->settings_selected_plugins = $settings['plugins'] ?? null;
177
178 $this->manifest = $this->read_manifest_json();
179 $this->site_settings = $this->read_site_settings_json();
180
181 $this->set_default_settings();
182 }
183
184 add_filter( 'wp_php_error_args', function ( $args, $error ) {
185 return $this->filter_php_error_args( $args, $error );
186 }, 10, 2 );
187 }
188
189 /**
190 * Set the import object parameters.
191 *
192 * @param array $instance
193 * @return void
194 */
195 private function set_import_object( array $instance ) {
196 $this->session_id = $instance['session_id'];
197
198 $instance_data = $instance['instance_data'];
199
200 $this->extracted_directory_path = $instance_data['extracted_directory_path'];
201 $this->runners = $instance_data['runners'];
202 $this->adapters = $instance_data['adapters'];
203
204 $this->manifest = $instance_data['manifest'];
205 $this->site_settings = $instance_data['site_settings'];
206
207 $this->settings_include = $instance_data['settings_include'];
208 $this->settings_referrer = $instance_data['settings_referrer'];
209 $this->settings_conflicts = $instance_data['settings_conflicts'];
210 $this->settings_selected_override_conditions = $instance_data['settings_selected_override_conditions'];
211 $this->settings_selected_custom_post_types = $instance_data['settings_selected_custom_post_types'];
212 $this->settings_selected_plugins = $instance_data['settings_selected_plugins'];
213
214 $this->documents_data = $instance_data['documents_data'];
215 $this->imported_data = $instance_data['imported_data'];
216 $this->runners_import_metadata = $instance_data['runners_import_metadata'];
217 }
218
219 /**
220 * Creating a new instance of the import process by the id of the old import session.
221 *
222 * @param string $session_id
223 *
224 * @return Import
225 * @throws \Exception If the import session does not exist.
226 */
227 public static function from_session( string $session_id ): Import {
228 $import_sessions = Utils::get_import_sessions();
229
230 if ( ! $import_sessions || ! isset( $import_sessions[ $session_id ] ) ) {
231 throw new \Exception( 'Couldn’t execute the import process because the import session does not exist.' );
232 }
233
234 $import_session = $import_sessions[ $session_id ];
235
236 return new self( $session_id, [], $import_session );
237 }
238
239 /**
240 * Register a runner.
241 * Be aware that the runner will be executed in the order of registration, the order is crucial for the import process.
242 *
243 * @param Import_Runner_Base $runner_instance
244 */
245 public function register( Import_Runner_Base $runner_instance ) {
246 $this->runners[ $runner_instance::get_name() ] = $runner_instance;
247 }
248
249 public function register_default_runners() {
250 $this->register( new Site_Settings() );
251 $this->register( new Plugins() );
252 $this->register( new Templates() );
253 $this->register( new Taxonomies() );
254 $this->register( new Elementor_Content() );
255 $this->register( new Wp_Content() );
256 }
257
258 /**
259 * Set default settings for the import.
260 */
261 private function set_default_settings() {
262 if ( ! is_array( $this->get_settings_include() ) ) {
263 $this->settings_include( $this->get_default_settings_include() );
264 }
265
266 if ( ! is_array( $this->get_settings_conflicts() ) ) {
267 $this->settings_conflicts( $this->get_default_settings_conflicts() );
268 }
269
270 if ( ! is_array( $this->get_settings_selected_override_conditions() ) ) {
271 $this->settings_selected_override_conditions( $this->get_default_settings_override_conditions() );
272 }
273
274 if ( ! is_array( $this->get_settings_selected_custom_post_types() ) ) {
275 $this->settings_selected_custom_post_types( $this->get_default_settings_custom_post_types() );
276 }
277
278 if ( ! is_array( $this->get_settings_selected_plugins() ) ) {
279 $this->settings_selected_plugins( $this->get_default_settings_plugins() );
280 }
281 }
282
283 /**
284 * Execute the import process.
285 *
286 * @return array The imported data output.
287 *
288 * @throws \Exception If no import runners have been specified.
289 */
290 public function run() {
291 if ( empty( $this->runners ) ) {
292 throw new \Exception( 'Couldn’t execute the import process because no import runners have been specified. Try again by specifying import runners.' );
293 }
294
295 $data = [
296 'session_id' => $this->session_id,
297 'include' => $this->settings_include,
298 'manifest' => $this->manifest,
299 'site_settings' => $this->site_settings,
300 'selected_plugins' => $this->settings_selected_plugins,
301 'extracted_directory_path' => $this->extracted_directory_path,
302 'selected_custom_post_types' => $this->settings_selected_custom_post_types,
303 ];
304
305 $this->init_import_session();
306
307 remove_filter( 'elementor/document/save/data', [ Plugin::$instance->modules_manager->get_modules( 'content-sanitizer' ), 'sanitize_content' ] );
308 add_filter( 'elementor/document/save/data', [ $this, 'prevent_saving_elements_on_post_creation' ], 10, 2 );
309
310 // Set the Request's state as an Elementor upload request, in order to support unfiltered file uploads.
311 Plugin::$instance->uploads_manager->set_elementor_upload_state( true );
312
313 foreach ( $this->runners as $runner ) {
314 if ( $runner->should_import( $data ) ) {
315 $import = $runner->import( $data, $this->imported_data );
316 $this->imported_data = array_merge_recursive( $this->imported_data, $import );
317
318 $this->runners_import_metadata[ $runner::get_name() ] = $runner->get_import_session_metadata();
319 }
320 }
321
322 // After the upload complete, set the elementor upload state back to false.
323 Plugin::$instance->uploads_manager->set_elementor_upload_state( false );
324
325 remove_filter( 'elementor/document/save/data', [ $this, 'prevent_saving_elements_on_post_creation' ], 10 );
326
327 $this->finalize_import_session_option();
328
329 $this->save_elements_of_imported_posts();
330
331 Plugin::$instance->uploads_manager->remove_file_or_dir( $this->extracted_directory_path );
332 return $this->imported_data;
333 }
334
335 /**
336 * Run specific runner by runner_name
337 *
338 * @param string $runner_name
339 *
340 * @return array
341 *
342 * @throws \Exception If no export runners have been specified.
343 */
344 public function run_runner( string $runner_name ): array {
345 if ( empty( $this->runners ) ) {
346 throw new \Exception( 'Couldn’t execute the import process because no import runners have been specified. Try again by specifying import runners.' );
347 }
348
349 $data = [
350 'session_id' => $this->session_id,
351 'include' => $this->settings_include,
352 'manifest' => $this->manifest,
353 'site_settings' => $this->site_settings,
354 'selected_plugins' => $this->settings_selected_plugins,
355 'extracted_directory_path' => $this->extracted_directory_path,
356 'selected_custom_post_types' => $this->settings_selected_custom_post_types,
357 ];
358
359 add_filter( 'elementor/document/save/data', [ $this, 'prevent_saving_elements_on_post_creation' ], 10, 2 );
360
361 // Set the Request's state as an Elementor upload request, in order to support unfiltered file uploads.
362 Plugin::$instance->uploads_manager->set_elementor_upload_state( true );
363
364 $runner = $this->runners[ $runner_name ];
365
366 if ( empty( $runner ) ) {
367 throw new \Exception( 'Couldn’t execute the import process because the import runner was not found. Try again by specifying an import runner.' );
368 }
369
370 if ( $runner->should_import( $data ) ) {
371 $import = $runner->import( $data, $this->imported_data );
372 $this->imported_data = array_merge_recursive( $this->imported_data, $import );
373
374 $this->runners_import_metadata[ $runner::get_name() ] = $runner->get_import_session_metadata();
375 }
376
377 // After the upload complete, set the elementor upload state back to false.
378 Plugin::$instance->uploads_manager->set_elementor_upload_state( false );
379
380 remove_filter( 'elementor/document/save/data', [ $this, 'prevent_saving_elements_on_post_creation' ], 10 );
381
382 $is_last_runner = key( array_slice( $this->runners, -1, 1, true ) ) === $runner_name;
383 if ( $is_last_runner ) {
384 $this->finalize_import_session_option();
385 $this->save_elements_of_imported_posts();
386 } else {
387 $this->update_instance_data_in_import_session_option();
388 }
389
390 return [
391 'status' => 'success',
392 'runner' => $runner_name,
393 ];
394 }
395
396 /**
397 * Create and save all the instance data to the import sessions option.
398 *
399 * @return void
400 */
401 public function init_import_session( $save_instance_data = false ) {
402 $import_sessions = Utils::get_import_sessions( true );
403
404 $import_sessions[ $this->session_id ] = [
405 'session_id' => $this->session_id,
406 'kit_title' => $this->manifest['title'] ?? '',
407 'kit_name' => $this->manifest['name'] ?? '',
408 'kit_thumbnail' => $this->get_kit_thumbnail(),
409 'kit_source' => $this->settings_referrer,
410 'user_id' => get_current_user_id(),
411 'start_timestamp' => current_time( 'timestamp' ),
412 ];
413
414 if ( $save_instance_data ) {
415 $import_sessions[ $this->session_id ]['instance_data'] = [
416 'extracted_directory_path' => $this->extracted_directory_path,
417 'runners' => $this->runners,
418 'adapters' => $this->adapters,
419
420 'manifest' => $this->manifest,
421 'site_settings' => $this->site_settings,
422
423 'settings_include' => $this->settings_include,
424 'settings_referrer' => $this->settings_referrer,
425 'settings_conflicts' => $this->settings_conflicts,
426 'settings_selected_override_conditions' => $this->settings_selected_override_conditions,
427 'settings_selected_custom_post_types' => $this->settings_selected_custom_post_types,
428 'settings_selected_plugins' => $this->settings_selected_plugins,
429
430 'documents_data' => $this->documents_data,
431 'imported_data' => $this->imported_data,
432 'runners_import_metadata' => $this->runners_import_metadata,
433 ];
434 }
435
436 update_option( Module::OPTION_KEY_ELEMENTOR_IMPORT_SESSIONS, $import_sessions, false );
437 }
438
439 /**
440 * Get the Kit thumbnail, goes to the home page thumbnail if main doesn't exist
441 *
442 * @return string
443 */
444 private function get_kit_thumbnail(): string {
445 if ( ! empty( $this->manifest['thumbnail'] ) ) {
446 return $this->manifest['thumbnail'];
447 }
448
449 return apply_filters( 'elementor/import/kit_thumbnail', '', $this->kit_id, $this->settings_referrer );
450 }
451
452 public function get_runners_name(): array {
453 return array_keys( $this->runners );
454 }
455
456 public function get_manifest() {
457 return $this->manifest;
458 }
459
460 public function get_extracted_directory_path() {
461 return $this->extracted_directory_path;
462 }
463
464 public function get_session_id() {
465 return $this->session_id;
466 }
467
468 public function get_adapters() {
469 return $this->adapters;
470 }
471
472 public function get_imported_data() {
473 return $this->imported_data;
474 }
475
476 /**
477 * Get settings by key.
478 * Used for backward compatibility.
479 *
480 * @param string $key The key of the setting.
481 */
482 public function get_settings( $key ) {
483 switch ( $key ) {
484 case 'include':
485 return $this->get_settings_include();
486
487 case 'overrideConditions':
488 return $this->get_settings_selected_override_conditions();
489
490 case 'selectedCustomPostTypes':
491 return $this->get_settings_selected_custom_post_types();
492
493 case 'plugins':
494 return $this->get_settings_selected_plugins();
495
496 default:
497 return [];
498 }
499 }
500
501 public function settings_include( array $settings_include ) {
502 $this->settings_include = $settings_include;
503
504 return $this;
505 }
506
507 public function get_settings_include() {
508 return $this->settings_include;
509 }
510
511 public function settings_referrer( $settings_referrer ) {
512 $this->settings_referrer = $settings_referrer;
513
514 return $this;
515 }
516
517 public function get_settings_referrer() {
518 return $this->settings_referrer;
519 }
520
521 public function settings_conflicts( array $settings_conflicts ) {
522 $this->settings_conflicts = $settings_conflicts;
523
524 return $this;
525 }
526
527 public function get_settings_conflicts() {
528 return $this->settings_conflicts;
529 }
530
531 public function settings_selected_override_conditions( array $settings_selected_override_conditions ) {
532 $this->settings_selected_override_conditions = $settings_selected_override_conditions;
533
534 return $this;
535 }
536
537 public function get_settings_selected_override_conditions() {
538 return $this->settings_selected_override_conditions;
539 }
540
541 public function settings_selected_custom_post_types( array $settings_selected_custom_post_types ) {
542 $this->settings_selected_custom_post_types = $settings_selected_custom_post_types;
543
544 return $this;
545 }
546
547 public function get_settings_selected_custom_post_types() {
548 return $this->settings_selected_custom_post_types;
549 }
550
551 public function settings_selected_plugins( array $settings_selected_plugins ) {
552 $this->settings_selected_plugins = $settings_selected_plugins;
553
554 return $this;
555 }
556
557 public function get_settings_selected_plugins() {
558 return $this->settings_selected_plugins;
559 }
560
561 /**
562 * Prevent saving elements on elementor post creation.
563 *
564 * @param array $data
565 * @param Document $document
566 *
567 * @return array
568 */
569 public function prevent_saving_elements_on_post_creation( array $data, Document $document ) {
570 if ( isset( $data['elements'] ) ) {
571 $this->documents_data[ $document->get_main_id() ] = [ 'elements' => $data['elements'] ];
572
573 $data['elements'] = [];
574 }
575
576 if ( isset( $data['settings'] ) ) {
577 $this->documents_data[ $document->get_main_id() ]['settings'] = $data['settings'];
578
579 }
580
581 return $data;
582 }
583
584 /**
585 * Extract the zip file.
586 *
587 * @param string $zip_path The path to the zip file.
588 * @return string The extracted directory path.
589 * @throws \Error If import process fails, file validation errors occur, or data corruption is detected.
590 */
591 private function extract_zip( $zip_path ) {
592 $extraction_result = Plugin::$instance->uploads_manager->extract_and_validate_zip( $zip_path, [ 'json', 'xml' ] );
593
594 if ( is_wp_error( $extraction_result ) ) {
595 if ( isset( $extraction_result->errors['zip_error'] ) ) {
596 throw new \Error( static::ZIP_ARCHIVE_ERROR_KEY ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
597 }
598
599 throw new \Error( static::ZIP_FILE_ERROR_KEY ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
600 }
601
602 return $extraction_result['extraction_directory'];
603 }
604
605 /**
606 * Get the manifest file from the extracted directory and adapt it if needed.
607 *
608 * @return string The manifest file content.
609 *
610 * @throws \Error If import validation fails or processing errors occur.
611 */
612 private function read_manifest_json() {
613 $manifest = Utils::read_json_file( $this->extracted_directory_path . 'manifest' );
614
615 if ( ! $manifest ) {
616 Plugin::$instance->logger->get_logger()->error( static::MANIFEST_ERROR_KEY );
617 throw new \Error( static::ZIP_FILE_ERROR_KEY ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
618 }
619
620 $this->init_adapters( $manifest );
621
622 foreach ( $this->adapters as $adapter ) {
623 $manifest = $adapter->adapt_manifest( $manifest );
624 }
625
626 return $manifest;
627 }
628
629 /**
630 * Init the adapters and determine which ones to use.
631 *
632 * @param array $manifest_data The manifest file content.
633 */
634 private function init_adapters( array $manifest_data ) {
635 $this->adapters = [];
636
637 /** @var Base_Adapter[] $adapter_types */
638 $adapter_types = [ Envato::class, Kit_Library::class ];
639
640 foreach ( $adapter_types as $adapter_type ) {
641 if ( $adapter_type::is_compatibility_needed( $manifest_data, [ 'referrer' => $this->get_settings_referrer() ] ) ) {
642 $this->adapters[] = new $adapter_type( $this );
643 }
644 }
645 }
646
647 /**
648 * Get the site settings file from the extracted directory and adapt it if needed.
649 *
650 * @return string The site settings file content.
651 */
652 private function read_site_settings_json() {
653 $site_settings = Utils::read_json_file( $this->extracted_directory_path . 'site-settings' );
654
655 foreach ( $this->adapters as $adapter ) {
656 $site_settings = $adapter->adapt_site_settings( $site_settings, $this->manifest, $this->extracted_directory_path );
657 }
658
659 return $site_settings;
660 }
661
662 /**
663 * Get all the custom post types in the kit.
664 *
665 * @return array Custom post types names.
666 */
667 private function get_default_settings_custom_post_types() {
668 if ( empty( $this->manifest['custom-post-type-title'] ) ) {
669 return [];
670 }
671
672 $manifest_post_types = array_keys( $this->manifest['custom-post-type-title'] );
673
674 return array_diff( $manifest_post_types, Utils::get_builtin_wp_post_types() );
675 }
676
677 /**
678 * Get the default settings of elementor templates conditions to override.
679 *
680 * @return array
681 */
682 private function get_default_settings_conflicts() {
683 if ( empty( $this->manifest['templates'] ) ) {
684 return [];
685 }
686
687 return apply_filters( 'elementor/import/get_default_settings_conflicts', [], $this->manifest['templates'] );
688 }
689
690 /**
691 * Get the default settings of elementor templates conditions to override.
692 *
693 * @return array
694 */
695 private function get_default_settings_override_conditions() {
696 if ( empty( $this->settings_conflicts ) ) {
697 return [];
698 }
699
700 return array_keys( $this->settings_conflicts );
701 }
702
703 /**
704 * Get the default settings of the plugins that should be imported.
705 *
706 * @return array
707 */
708 private function get_default_settings_plugins() {
709 return ! empty( $this->manifest['plugins'] ) ? $this->manifest['plugins'] : [];
710 }
711
712 /**
713 * Get the default settings of which content types should be imported.
714 *
715 * @return array
716 */
717 private function get_default_settings_include() {
718 return [ 'templates', 'plugins', 'content', 'settings' ];
719 }
720
721 /**
722 * Get the data that requires updating/replacement when imported.
723 *
724 * @return array{post_ids: array, term_ids: array}
725 */
726 private function get_imported_data_replacements(): array {
727 return [
728 'post_ids' => Utils::map_old_new_post_ids( $this->imported_data ),
729 'term_ids' => Utils::map_old_new_term_ids( $this->imported_data ),
730 ];
731 }
732
733 /**
734 * Save the prevented elements on elementor post creation elements.
735 * Handle the replacement of all the dynamic content of the elements that probably have been changed during the import.
736 */
737 private function save_elements_of_imported_posts() {
738 $imported_data_replacements = $this->get_imported_data_replacements();
739
740 foreach ( $this->documents_data as $new_id => $data ) {
741 $document = Plugin::$instance->documents->get( $new_id );
742
743 if ( isset( $data['elements'] ) ) {
744 $data['elements'] = Components_Module::prepare_imported_elements( $data['elements'], $imported_data_replacements['post_ids'] ?? [] );
745 $data['elements'] = $document->on_import_update_dynamic_content( $data['elements'], $imported_data_replacements );
746 }
747
748 if ( isset( $data['settings'] ) ) {
749
750 if ( $document instanceof Kit ) {
751 // Without post_status certain tabs in the Kit will not save properly.
752 $data['settings']['post_status'] = get_post_status( $new_id );
753 }
754
755 $data['settings'] = $document->on_import_update_settings( $data['settings'], $imported_data_replacements );
756 }
757
758 $document->save( $data );
759 }
760 }
761
762 private function update_instance_data_in_import_session_option() {
763 $import_sessions = Utils::get_import_sessions();
764
765 $import_sessions[ $this->session_id ]['instance_data']['documents_data'] = $this->documents_data;
766 $import_sessions[ $this->session_id ]['instance_data']['imported_data'] = $this->imported_data;
767 $import_sessions[ $this->session_id ]['instance_data']['runners_import_metadata'] = $this->runners_import_metadata;
768
769 update_option( Module::OPTION_KEY_ELEMENTOR_IMPORT_SESSIONS, $import_sessions, false );
770 }
771
772 public function finalize_import_session_option() {
773 $import_sessions = Utils::get_import_sessions();
774
775 if ( ! isset( $import_sessions[ $this->session_id ] ) ) {
776 return;
777 }
778
779 unset( $import_sessions[ $this->session_id ]['instance_data'] );
780
781 $import_sessions[ $this->session_id ]['end_timestamp'] = current_time( 'timestamp' );
782 $import_sessions[ $this->session_id ]['runners'] = $this->runners_import_metadata;
783
784 update_option( Module::OPTION_KEY_ELEMENTOR_IMPORT_SESSIONS, $import_sessions, false );
785 }
786
787 /**
788 * Filter the php error args and return 408 status code if the error is a timeout.
789 *
790 * @param array $args
791 * @param array $error
792 * @return array
793 */
794 private function filter_php_error_args( $args, $error ) {
795 if ( strpos( $error['message'], 'Maximum execution time' ) !== false ) {
796 $args['response'] = 408;
797 }
798
799 return $args;
800 }
801 }
802