PluginProbe
Elementor Website Builder – more than just a page builder / 3.16.2
Elementor Website Builder – more than just a page builder v3.16.2
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.16.2, at app/modules/import-export/processes/import.php

807 lines 23.7 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 add_filter( 'elementor/document/save/data', [ $this, 'prevent_saving_elements_on_post_creation' ], 10, 2 );
307
308 // Set the Request's state as an Elementor upload request, in order to support unfiltered file uploads.
309 Plugin::$instance->uploads_manager->set_elementor_upload_state( true );
310
311 foreach ( $this->runners as $runner ) {
312 if ( $runner->should_import( $data ) ) {
313 $import = $runner->import( $data, $this->imported_data );
314 $this->imported_data = array_merge_recursive( $this->imported_data, $import );
315
316 $this->runners_import_metadata[ $runner::get_name() ] = $runner->get_import_session_metadata();
317 }
318 }
319
320 // After the upload complete, set the elementor upload state back to false.
321 Plugin::$instance->uploads_manager->set_elementor_upload_state( false );
322
323 remove_filter( 'elementor/document/save/data', [ $this, 'prevent_saving_elements_on_post_creation' ], 10 );
324
325 $this->finalize_import_session_option();
326
327 $this->save_elements_of_imported_posts();
328
329 Plugin::$instance->uploads_manager->remove_file_or_dir( $this->extracted_directory_path );
330 return $this->imported_data;
331 }
332
333 /**
334 * Run specific runner by runner_name
335 *
336 * @param string $runner_name
337 *
338 * @return array
339 *
340 * @throws \Exception If no export runners have been specified.
341 */
342 public function run_runner( string $runner_name ): array {
343 if ( empty( $this->runners ) ) {
344 throw new \Exception( 'Couldn’t execute the import process because no import runners have been specified. Try again by specifying import runners.' );
345 }
346
347 $data = [
348 'session_id' => $this->session_id,
349 'include' => $this->settings_include,
350 'manifest' => $this->manifest,
351 'site_settings' => $this->site_settings,
352 'selected_plugins' => $this->settings_selected_plugins,
353 'extracted_directory_path' => $this->extracted_directory_path,
354 'selected_custom_post_types' => $this->settings_selected_custom_post_types,
355 ];
356
357 add_filter( 'elementor/document/save/data', [ $this, 'prevent_saving_elements_on_post_creation' ], 10, 2 );
358
359 // Set the Request's state as an Elementor upload request, in order to support unfiltered file uploads.
360 Plugin::$instance->uploads_manager->set_elementor_upload_state( true );
361
362 $runner = $this->runners[ $runner_name ];
363
364 if ( empty( $runner ) ) {
365 throw new \Exception( 'Couldn’t execute the import process because the import runner was not found. Try again by specifying an import runner.' );
366 }
367
368 if ( $runner->should_import( $data ) ) {
369 $import = $runner->import( $data, $this->imported_data );
370 $this->imported_data = array_merge_recursive( $this->imported_data, $import );
371
372 $this->runners_import_metadata[ $runner::get_name() ] = $runner->get_import_session_metadata();
373 }
374
375 // After the upload complete, set the elementor upload state back to false.
376 Plugin::$instance->uploads_manager->set_elementor_upload_state( false );
377
378 remove_filter( 'elementor/document/save/data', [ $this, 'prevent_saving_elements_on_post_creation' ], 10 );
379
380 $is_last_runner = key( array_slice( $this->runners, -1, 1, true ) ) === $runner_name;
381 if ( $is_last_runner ) {
382 $this->finalize_import_session_option();
383 $this->save_elements_of_imported_posts();
384 } else {
385 $this->update_instance_data_in_import_session_option();
386 }
387
388 return [
389 'status' => 'success',
390 'runner' => $runner_name,
391 ];
392 }
393
394 /**
395 * Create and save all the instance data to the import sessions option.
396 *
397 * @return void
398 */
399 public function init_import_session( $save_instance_data = false ) {
400 $import_sessions = Utils::get_import_sessions( true );
401
402 $import_sessions[ $this->session_id ] = [
403 'session_id' => $this->session_id,
404 'kit_title' => $this->manifest['title'] ?? '',
405 'kit_name' => $this->manifest['name'] ?? '',
406 'kit_thumbnail' => $this->get_kit_thumbnail(),
407 'kit_source' => $this->settings_referrer,
408 'user_id' => get_current_user_id(),
409 'start_timestamp' => current_time( 'timestamp' ),
410 ];
411
412 if ( $save_instance_data ) {
413 $import_sessions[ $this->session_id ]['instance_data'] = [
414 'extracted_directory_path' => $this->extracted_directory_path,
415 'runners' => $this->runners,
416 'adapters' => $this->adapters,
417
418 'manifest' => $this->manifest,
419 'site_settings' => $this->site_settings,
420
421 'settings_include' => $this->settings_include,
422 'settings_referrer' => $this->settings_referrer,
423 'settings_conflicts' => $this->settings_conflicts,
424 'settings_selected_override_conditions' => $this->settings_selected_override_conditions,
425 'settings_selected_custom_post_types' => $this->settings_selected_custom_post_types,
426 'settings_selected_plugins' => $this->settings_selected_plugins,
427
428 'documents_data' => $this->documents_data,
429 'imported_data' => $this->imported_data,
430 'runners_import_metadata' => $this->runners_import_metadata,
431 ];
432 }
433
434 update_option( Module::OPTION_KEY_ELEMENTOR_IMPORT_SESSIONS, $import_sessions, false );
435 }
436
437 /**
438 * Get the Kit thumbnail, goes to the home page thumbnail if main doesn't exist
439 *
440 * @return string
441 */
442 private function get_kit_thumbnail(): string {
443 if ( ! empty( $this->manifest['thumbnail'] ) ) {
444 return $this->manifest['thumbnail'];
445 }
446
447 if ( empty( $this->kit_id ) ) {
448 return '';
449 }
450
451 $api = new Kit_Library_Api();
452 $kit = $api->get_by_id( $this->kit_id );
453
454 if ( is_wp_error( $kit ) ) {
455 return '';
456 }
457
458 return $kit->thumbnail;
459 }
460
461 public function get_runners_name(): array {
462 return array_keys( $this->runners );
463 }
464
465 public function get_manifest() {
466 return $this->manifest;
467 }
468
469 public function get_extracted_directory_path() {
470 return $this->extracted_directory_path;
471 }
472
473 public function get_session_id() {
474 return $this->session_id;
475 }
476
477 public function get_adapters() {
478 return $this->adapters;
479 }
480
481 public function get_imported_data() {
482 return $this->imported_data;
483 }
484
485 /**
486 * Get settings by key.
487 * Used for backward compatibility.
488 *
489 * @param string $key The key of the setting.
490 */
491 public function get_settings( $key ) {
492 switch ( $key ) {
493 case 'include':
494 return $this->get_settings_include();
495
496 case 'overrideConditions':
497 return $this->get_settings_selected_override_conditions();
498
499 case 'selectedCustomPostTypes':
500 return $this->get_settings_selected_custom_post_types();
501
502 case 'plugins':
503 return $this->get_settings_selected_plugins();
504
505 default:
506 return [];
507 }
508 }
509
510 public function settings_include( array $settings_include ) {
511 $this->settings_include = $settings_include;
512
513 return $this;
514 }
515
516 public function get_settings_include() {
517 return $this->settings_include;
518 }
519
520 public function settings_referrer( $settings_referrer ) {
521 $this->settings_referrer = $settings_referrer;
522
523 return $this;
524 }
525
526 public function get_settings_referrer() {
527 return $this->settings_referrer;
528 }
529
530 public function settings_conflicts( array $settings_conflicts ) {
531 $this->settings_conflicts = $settings_conflicts;
532
533 return $this;
534 }
535
536 public function get_settings_conflicts() {
537 return $this->settings_conflicts;
538 }
539
540 public function settings_selected_override_conditions( array $settings_selected_override_conditions ) {
541 $this->settings_selected_override_conditions = $settings_selected_override_conditions;
542
543 return $this;
544 }
545
546 public function get_settings_selected_override_conditions() {
547 return $this->settings_selected_override_conditions;
548 }
549
550 public function settings_selected_custom_post_types( array $settings_selected_custom_post_types ) {
551 $this->settings_selected_custom_post_types = $settings_selected_custom_post_types;
552
553 return $this;
554 }
555
556 public function get_settings_selected_custom_post_types() {
557 return $this->settings_selected_custom_post_types;
558 }
559
560 public function settings_selected_plugins( array $settings_selected_plugins ) {
561 $this->settings_selected_plugins = $settings_selected_plugins;
562
563 return $this;
564 }
565
566 public function get_settings_selected_plugins() {
567 return $this->settings_selected_plugins;
568 }
569
570 /**
571 * Prevent saving elements on elementor post creation.
572 *
573 * @param array $data
574 * @param Document $document
575 *
576 * @return array
577 */
578 public function prevent_saving_elements_on_post_creation( array $data, Document $document ) {
579 if ( isset( $data['elements'] ) ) {
580 $this->documents_data[ $document->get_main_id() ] = [ 'elements' => $data['elements'] ];
581
582 $data['elements'] = [];
583 }
584
585 if ( isset( $data['settings'] ) ) {
586 $this->documents_data[ $document->get_main_id() ]['settings'] = $data['settings'];
587
588 }
589
590 return $data;
591 }
592
593 /**
594 * Extract the zip file.
595 *
596 * @param string $zip_path The path to the zip file.
597 * @return string The extracted directory path.
598 */
599 private function extract_zip( $zip_path ) {
600 $extraction_result = Plugin::$instance->uploads_manager->extract_and_validate_zip( $zip_path, [ 'json', 'xml' ] );
601
602 if ( is_wp_error( $extraction_result ) ) {
603 if ( isset( $extraction_result->errors['zip_error'] ) ) {
604 throw new \Error( static::ZIP_ARCHIVE_ERROR_KEY );
605 }
606
607 throw new \Error( static::ZIP_FILE_ERROR_KEY );
608 }
609
610 return $extraction_result['extraction_directory'];
611 }
612
613 /**
614 * Get the manifest file from the extracted directory and adapt it if needed.
615 *
616 * @return string The manifest file content.
617 */
618 private function read_manifest_json() {
619 $manifest = Utils::read_json_file( $this->extracted_directory_path . 'manifest' );
620
621 if ( ! $manifest ) {
622 Plugin::$instance->logger->get_logger()->error( static::MANIFEST_ERROR_KEY );
623 throw new \Error( static::ZIP_FILE_ERROR_KEY );
624 }
625
626 $this->init_adapters( $manifest );
627
628 foreach ( $this->adapters as $adapter ) {
629 $manifest = $adapter->adapt_manifest( $manifest );
630 }
631
632 return $manifest;
633 }
634
635 /**
636 * Init the adapters and determine which ones to use.
637 *
638 * @param array $manifest_data The manifest file content.
639 */
640 private function init_adapters( array $manifest_data ) {
641 $this->adapters = [];
642
643 /** @var Base_Adapter[] $adapter_types */
644 $adapter_types = [ Envato::class, Kit_Library::class ];
645
646 foreach ( $adapter_types as $adapter_type ) {
647 if ( $adapter_type::is_compatibility_needed( $manifest_data, [ 'referrer' => $this->get_settings_referrer() ] ) ) {
648 $this->adapters[] = new $adapter_type( $this );
649 }
650 }
651 }
652
653 /**
654 * Get the site settings file from the extracted directory and adapt it if needed.
655 *
656 * @return string The site settings file content.
657 */
658 private function read_site_settings_json() {
659 $site_settings = Utils::read_json_file( $this->extracted_directory_path . 'site-settings' );
660
661 foreach ( $this->adapters as $adapter ) {
662 $site_settings = $adapter->adapt_site_settings( $site_settings, $this->manifest, $this->extracted_directory_path );
663 }
664
665 return $site_settings;
666 }
667
668 /**
669 * Get all the custom post types in the kit.
670 *
671 * @return array Custom post types names.
672 */
673 private function get_default_settings_custom_post_types() {
674 if ( empty( $this->manifest['custom-post-type-title'] ) ) {
675 return [];
676 }
677
678 $manifest_post_types = array_keys( $this->manifest['custom-post-type-title'] );
679
680 return array_diff( $manifest_post_types, Utils::get_builtin_wp_post_types() );
681 }
682
683 /**
684 * Get the default settings of elementor templates conditions to override.
685 *
686 * @return array
687 */
688 private function get_default_settings_conflicts() {
689 if ( empty( $this->manifest['templates'] ) ) {
690 return [];
691 }
692
693 return apply_filters( 'elementor/import/get_default_settings_conflicts', [], $this->manifest['templates'] );
694 }
695
696 /**
697 * Get the default settings of elementor templates conditions to override.
698 *
699 * @return array
700 */
701 private function get_default_settings_override_conditions() {
702 if ( empty( $this->settings_conflicts ) ) {
703 return [];
704 }
705
706 return array_keys( $this->settings_conflicts );
707 }
708
709 /**
710 * Get the default settings of the plugins that should be imported.
711 *
712 * @return array
713 */
714 private function get_default_settings_plugins() {
715 return ! empty( $this->manifest['plugins'] ) ? $this->manifest['plugins'] : [];
716 }
717
718 /**
719 * Get the default settings of which content types should be imported.
720 *
721 * @return array
722 */
723 private function get_default_settings_include() {
724 return [ 'templates', 'plugins', 'content', 'settings' ];
725 }
726
727 /**
728 * Get the data that requires updating/replacement when imported.
729 *
730 * @return array{post_ids: array, term_ids: array}
731 */
732 private function get_imported_data_replacements() : array {
733 return [
734 'post_ids' => Utils::map_old_new_post_ids( $this->imported_data ),
735 'term_ids' => Utils::map_old_new_term_ids( $this->imported_data ),
736 ];
737 }
738
739 /**
740 * Save the prevented elements on elementor post creation elements.
741 * Handle the replacement of all the dynamic content of the elements that probably have been changed during the import.
742 */
743 private function save_elements_of_imported_posts() {
744 $imported_data_replacements = $this->get_imported_data_replacements();
745
746 foreach ( $this->documents_data as $new_id => $data ) {
747 $document = Plugin::$instance->documents->get( $new_id );
748
749 if ( isset( $data['elements'] ) ) {
750 $data['elements'] = $document->on_import_update_dynamic_content( $data['elements'], $imported_data_replacements );
751 }
752
753 if ( isset( $data['settings'] ) ) {
754
755 if ( $document instanceof Kit ) {
756 // Without post_status certain tabs in the Kit will not save properly.
757 $data['settings']['post_status'] = get_post_status( $new_id );
758 }
759
760 $data['settings'] = $document->on_import_update_settings( $data['settings'], $imported_data_replacements );
761 }
762
763 $document->save( $data );
764 }
765 }
766
767 private function update_instance_data_in_import_session_option() {
768 $import_sessions = Utils::get_import_sessions();
769
770 $import_sessions[ $this->session_id ]['instance_data']['documents_data'] = $this->documents_data;
771 $import_sessions[ $this->session_id ]['instance_data']['imported_data'] = $this->imported_data;
772 $import_sessions[ $this->session_id ]['instance_data']['runners_import_metadata'] = $this->runners_import_metadata;
773
774 update_option( Module::OPTION_KEY_ELEMENTOR_IMPORT_SESSIONS, $import_sessions, false );
775 }
776
777 public function finalize_import_session_option() {
778 $import_sessions = Utils::get_import_sessions();
779
780 if ( ! isset( $import_sessions[ $this->session_id ] ) ) {
781 return;
782 }
783
784 unset( $import_sessions[ $this->session_id ]['instance_data'] );
785
786 $import_sessions[ $this->session_id ]['end_timestamp'] = current_time( 'timestamp' );
787 $import_sessions[ $this->session_id ]['runners'] = $this->runners_import_metadata;
788
789 update_option( Module::OPTION_KEY_ELEMENTOR_IMPORT_SESSIONS, $import_sessions, false );
790 }
791
792 /**
793 * Filter the php error args and return 408 status code if the error is a timeout.
794 *
795 * @param array $args
796 * @param array $error
797 * @return array
798 */
799 private function filter_php_error_args( $args, $error ) {
800 if ( strpos( $error['message'], 'Maximum execution time' ) !== false ) {
801 $args['response'] = 408;
802 }
803
804 return $args;
805 }
806 }
807