PluginProbe
Elementor Website Builder – more than just a page builder / 3.32.5
Elementor Website Builder – more than just a page builder v3.32.5
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-customization / processes / import.php

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

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