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

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