PluginProbe
Elementor Website Builder – more than just a page builder / 3.24.4
Elementor Website Builder – more than just a page builder v3.24.4
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 4.0.7 All 451 releases
elementor / app / modules / import-export / processes / import.php

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

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