PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.0-beta3
Elementor Website Builder – more than just a page builder v4.3.0-beta3
4.3.0-beta3 4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 All 452 releases
elementor / app / modules / import-export / processes / import.php

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

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