← All changes
|
app/modules/import-export-customization/processes/import.php
+33
-97
4.3.0-beta3
→
3.31.2
View file →
| @@ -4,14 +4,11 @@ | ||
| 4 | 4 | |
| 5 | 5 | use Elementor\App\Modules\ImportExportCustomization\Compatibility\Base_Adapter; |
| 6 | 6 | use Elementor\App\Modules\ImportExportCustomization\Compatibility\Envato; |
| 7 | 7 | use Elementor\App\Modules\ImportExportCustomization\Compatibility\Kit_Library; |
| 8 | -use Elementor\App\Modules\ImportExportCustomization\Compatibility\Customization; | |
| 9 | 8 | use Elementor\App\Modules\ImportExportCustomization\Utils; |
| 10 | 9 | use Elementor\Core\Base\Document; |
| 11 | 10 | use Elementor\Core\Kits\Documents\Kit; |
| 12 | -use Elementor\Modules\AtomicWidgets\Utils\Atomic_Prop_Remap; | |
| 13 | -use Elementor\Modules\Components\Module as Components_Module; | |
| 14 | 11 | use Elementor\Plugin; |
| 15 | 12 | |
| 16 | 13 | use Elementor\App\Modules\ImportExportCustomization\Runners\Import\Elementor_Content; |
| 17 | 14 | use Elementor\App\Modules\ImportExportCustomization\Runners\Import\Import_Runner_Base; |
| @@ -20,9 +17,8 @@ | ||
| 20 | 17 | use Elementor\App\Modules\ImportExportCustomization\Runners\Import\Taxonomies; |
| 21 | 18 | use Elementor\App\Modules\ImportExportCustomization\Runners\Import\Templates; |
| 22 | 19 | use Elementor\App\Modules\ImportExportCustomization\Runners\Import\Wp_Content; |
| 23 | 20 | use Elementor\App\Modules\ImportExportCustomization\Module; |
| 24 | -use Elementor\App\Modules\ImportExportCustomization\Runners\Import\Floating_Elements; | |
| 25 | 21 | |
| 26 | 22 | class Import { |
| 27 | 23 | const MANIFEST_ERROR_KEY = 'manifest-error'; |
| 28 | 24 | |
| @@ -127,15 +123,8 @@ | ||
| 127 | 123 | */ |
| 128 | 124 | private $settings_selected_plugins; |
| 129 | 125 | |
| 130 | 126 | /** |
| 131 | - * Customization settings for selective import. | |
| 132 | - * | |
| 133 | - * @var array | |
| 134 | - */ | |
| 135 | - private $settings_customization; | |
| 136 | - | |
| 137 | - /** | |
| 138 | 127 | * The imported data output. |
| 139 | 128 | * |
| 140 | 129 | * @var array |
| 141 | 130 | */ |
| @@ -149,11 +138,11 @@ | ||
| 149 | 138 | */ |
| 150 | 139 | private $runners_import_metadata = []; |
| 151 | 140 | |
| 152 | 141 | /** |
| 153 | - * @param string $path session_id | zip_file_path | |
| 154 | - * @param array $settings Use to determine which content to import. | |
| 155 | - * (e.g: include, selected_plugins, selected_cpt, selected_override_conditions, etc.) | |
| 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.) | |
| 156 | 145 | * @param array|null $old_instance An array of old instance parameters that will be used for creating new instance. |
| 157 | 146 | * We are using it for quick creation of the instance when the import process is being split into chunks. |
| 158 | 147 | * @throws \Exception If the import session does not exist. |
| 159 | 148 | */ |
| @@ -179,12 +168,11 @@ | ||
| 179 | 168 | $this->settings_referrer = ! empty( $settings['referrer'] ) ? $settings['referrer'] : 'local'; |
| 180 | 169 | $this->settings_include = ! empty( $settings['include'] ) ? $settings['include'] : null; |
| 181 | 170 | |
| 182 | 171 | // Using isset and not empty is important since empty array is valid option. |
| 183 | - $this->settings_selected_override_conditions = $settings['customization']['templates']['themeBuilder']['overrideConditions'] ?? null; | |
| 184 | - $this->settings_selected_custom_post_types = $settings['customization']['content']['customPostTypes'] ?? null; | |
| 172 | + $this->settings_selected_override_conditions = $settings['overrideConditions'] ?? null; | |
| 173 | + $this->settings_selected_custom_post_types = $settings['selectedCustomPostTypes'] ?? null; | |
| 185 | 174 | $this->settings_selected_plugins = $settings['plugins'] ?? null; |
| 186 | - $this->settings_customization = $settings['customization'] ?? null; | |
| 187 | 175 | |
| 188 | 176 | $this->manifest = $this->read_manifest_json(); |
| 189 | 177 | $this->site_settings = $this->read_site_settings_json(); |
| 190 | 178 | |
| @@ -213,9 +201,8 @@ | ||
| 213 | 201 | |
| 214 | 202 | $this->manifest = $instance_data['manifest']; |
| 215 | 203 | $this->site_settings = $instance_data['site_settings']; |
| 216 | 204 | |
| 217 | - $this->kit_id = $instance_data['kit_id'] ?? ''; | |
| 218 | 205 | $this->settings_include = $instance_data['settings_include']; |
| 219 | 206 | $this->settings_referrer = $instance_data['settings_referrer']; |
| 220 | 207 | $this->settings_conflicts = $instance_data['settings_conflicts']; |
| 221 | 208 | $this->settings_selected_override_conditions = $instance_data['settings_selected_override_conditions']; |
| @@ -220,9 +207,8 @@ | ||
| 220 | 207 | $this->settings_conflicts = $instance_data['settings_conflicts']; |
| 221 | 208 | $this->settings_selected_override_conditions = $instance_data['settings_selected_override_conditions']; |
| 222 | 209 | $this->settings_selected_custom_post_types = $instance_data['settings_selected_custom_post_types']; |
| 223 | 210 | $this->settings_selected_plugins = $instance_data['settings_selected_plugins']; |
| 224 | - $this->settings_customization = $instance_data['settings_customization']; | |
| 225 | 211 | |
| 226 | 212 | $this->documents_data = $instance_data['documents_data']; |
| 227 | 213 | $this->imported_data = $instance_data['imported_data']; |
| 228 | 214 | $this->runners_import_metadata = $instance_data['runners_import_metadata']; |
| @@ -264,9 +250,8 @@ | ||
| 264 | 250 | $this->register( new Templates() ); |
| 265 | 251 | $this->register( new Taxonomies() ); |
| 266 | 252 | $this->register( new Elementor_Content() ); |
| 267 | 253 | $this->register( new Wp_Content() ); |
| 268 | - $this->register( new Floating_Elements() ); | |
| 269 | 254 | } |
| 270 | 255 | |
| 271 | 256 | /** |
| 272 | 257 | * Set default settings for the import. |
| @@ -290,12 +275,8 @@ | ||
| 290 | 275 | |
| 291 | 276 | if ( ! is_array( $this->get_settings_selected_plugins() ) ) { |
| 292 | 277 | $this->settings_selected_plugins( $this->get_default_settings_plugins() ); |
| 293 | 278 | } |
| 294 | - | |
| 295 | - if ( ! is_array( $this->get_settings_customization() ) ) { | |
| 296 | - $this->settings_customization( $this->get_default_settings_customization() ); | |
| 297 | - } | |
| 298 | 279 | } |
| 299 | 280 | |
| 300 | 281 | /** |
| 301 | 282 | * Execute the import process. |
| @@ -308,9 +289,17 @@ | ||
| 308 | 289 | if ( empty( $this->runners ) ) { |
| 309 | 290 | throw new \Exception( 'Couldn’t execute the import process because no import runners have been specified. Try again by specifying import runners.' ); |
| 310 | 291 | } |
| 311 | 292 | |
| 312 | - $data = $this->build_runner_data(); | |
| 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 | + ]; | |
| 313 | 302 | |
| 314 | 303 | $this->init_import_session(); |
| 315 | 304 | |
| 316 | 305 | remove_filter( 'elementor/document/save/data', [ Plugin::$instance->modules_manager->get_modules( 'content-sanitizer' ), 'sanitize_content' ] ); |
| @@ -354,9 +343,17 @@ | ||
| 354 | 343 | if ( empty( $this->runners ) ) { |
| 355 | 344 | throw new \Exception( 'Couldn’t execute the import process because no import runners have been specified. Try again by specifying import runners.' ); |
| 356 | 345 | } |
| 357 | 346 | |
| 358 | - $data = $this->build_runner_data(); | |
| 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 | + ]; | |
| 359 | 356 | |
| 360 | 357 | add_filter( 'elementor/document/save/data', [ $this, 'prevent_saving_elements_on_post_creation' ], 10, 2 ); |
| 361 | 358 | |
| 362 | 359 | // Set the Request's state as an Elementor upload request, in order to support unfiltered file uploads. |
| @@ -390,9 +387,8 @@ | ||
| 390 | 387 | |
| 391 | 388 | return [ |
| 392 | 389 | 'status' => 'success', |
| 393 | 390 | 'runner' => $runner_name, |
| 394 | - 'imported_data' => $this->imported_data, | |
| 395 | 391 | ]; |
| 396 | 392 | } |
| 397 | 393 | |
| 398 | 394 | /** |
| @@ -401,18 +397,17 @@ | ||
| 401 | 397 | * @return void |
| 402 | 398 | */ |
| 403 | 399 | public function init_import_session( $save_instance_data = false ) { |
| 404 | 400 | $import_sessions = Utils::get_import_sessions( true ); |
| 405 | - $existing_session = $import_sessions[ $this->session_id ] ?? []; | |
| 406 | 401 | |
| 407 | 402 | $import_sessions[ $this->session_id ] = [ |
| 408 | 403 | 'session_id' => $this->session_id, |
| 409 | 404 | 'kit_title' => $this->manifest['title'] ?? '', |
| 410 | 405 | 'kit_name' => $this->manifest['name'] ?? '', |
| 411 | - 'kit_thumbnail' => $existing_session['kit_thumbnail'] ?? $this->get_kit_thumbnail(), | |
| 412 | - 'kit_source' => $existing_session['kit_source'] ?? $this->settings_referrer, | |
| 406 | + 'kit_thumbnail' => $this->get_kit_thumbnail(), | |
| 407 | + 'kit_source' => $this->settings_referrer, | |
| 413 | 408 | 'user_id' => get_current_user_id(), |
| 414 | - 'start_timestamp' => $existing_session['start_timestamp'] ?? current_time( 'timestamp' ), | |
| 409 | + 'start_timestamp' => current_time( 'timestamp' ), | |
| 415 | 410 | ]; |
| 416 | 411 | |
| 417 | 412 | if ( $save_instance_data ) { |
| 418 | 413 | $import_sessions[ $this->session_id ]['instance_data'] = [ |
| @@ -422,9 +417,8 @@ | ||
| 422 | 417 | |
| 423 | 418 | 'manifest' => $this->manifest, |
| 424 | 419 | 'site_settings' => $this->site_settings, |
| 425 | 420 | |
| 426 | - 'kit_id' => $this->kit_id, | |
| 427 | 421 | 'settings_include' => $this->settings_include, |
| 428 | 422 | 'settings_referrer' => $this->settings_referrer, |
| 429 | 423 | 'settings_conflicts' => $this->settings_conflicts, |
| 430 | 424 | 'settings_selected_override_conditions' => $this->settings_selected_override_conditions, |
| @@ -429,9 +423,8 @@ | ||
| 429 | 423 | 'settings_conflicts' => $this->settings_conflicts, |
| 430 | 424 | 'settings_selected_override_conditions' => $this->settings_selected_override_conditions, |
| 431 | 425 | 'settings_selected_custom_post_types' => $this->settings_selected_custom_post_types, |
| 432 | 426 | 'settings_selected_plugins' => $this->settings_selected_plugins, |
| 433 | - 'settings_customization' => $this->settings_customization, | |
| 434 | 427 | |
| 435 | 428 | 'documents_data' => $this->documents_data, |
| 436 | 429 | 'imported_data' => $this->imported_data, |
| 437 | 430 | 'runners_import_metadata' => $this->runners_import_metadata, |
| @@ -454,29 +447,11 @@ | ||
| 454 | 447 | return apply_filters( 'elementor/import/kit_thumbnail', '', $this->kit_id, $this->settings_referrer ); |
| 455 | 448 | } |
| 456 | 449 | |
| 457 | 450 | public function get_runners_name(): array { |
| 458 | - $data = $this->build_runner_data(); | |
| 459 | - | |
| 460 | - return array_keys( array_filter( | |
| 461 | - $this->runners, | |
| 462 | - fn( $runner ) => $runner->should_import( $data ) | |
| 463 | - ) ); | |
| 451 | + return array_keys( $this->runners ); | |
| 464 | 452 | } |
| 465 | 453 | |
| 466 | - private function build_runner_data(): array { | |
| 467 | - return [ | |
| 468 | - 'session_id' => $this->session_id, | |
| 469 | - 'include' => $this->settings_include, | |
| 470 | - 'manifest' => $this->manifest, | |
| 471 | - 'site_settings' => $this->site_settings, | |
| 472 | - 'selected_plugins' => $this->settings_selected_plugins, | |
| 473 | - 'customization' => $this->settings_customization, | |
| 474 | - 'extracted_directory_path' => $this->extracted_directory_path, | |
| 475 | - 'selected_custom_post_types' => $this->settings_selected_custom_post_types, | |
| 476 | - ]; | |
| 477 | - } | |
| 478 | - | |
| 479 | 454 | public function get_manifest() { |
| 480 | 455 | return $this->manifest; |
| 481 | 456 | } |
| 482 | 457 | |
| @@ -515,11 +490,8 @@ | ||
| 515 | 490 | |
| 516 | 491 | case 'plugins': |
| 517 | 492 | return $this->get_settings_selected_plugins(); |
| 518 | 493 | |
| 519 | - case 'customization': | |
| 520 | - return $this->get_settings_customization(); | |
| 521 | - | |
| 522 | 494 | default: |
| 523 | 495 | return []; |
| 524 | 496 | } |
| 525 | 497 | } |
| @@ -586,9 +558,9 @@ | ||
| 586 | 558 | |
| 587 | 559 | /** |
| 588 | 560 | * Prevent saving elements on elementor post creation. |
| 589 | 561 | * |
| 590 | - * @param array $data | |
| 562 | + * @param array $data | |
| 591 | 563 | * @param Document $document |
| 592 | 564 | * |
| 593 | 565 | * @return array |
| 594 | 566 | */ |
| @@ -611,9 +583,8 @@ | ||
| 611 | 583 | * Extract the zip file. |
| 612 | 584 | * |
| 613 | 585 | * @param string $zip_path The path to the zip file. |
| 614 | 586 | * @return string The extracted directory path. |
| 615 | - * @throws \Error If import process fails, file validation errors occur, or data corruption is detected. | |
| 616 | 587 | */ |
| 617 | 588 | private function extract_zip( $zip_path ) { |
| 618 | 589 | $extraction_result = Plugin::$instance->uploads_manager->extract_and_validate_zip( $zip_path, [ 'json', 'xml' ] ); |
| 619 | 590 | |
| @@ -631,9 +602,8 @@ | ||
| 631 | 602 | /** |
| 632 | 603 | * Get the manifest file from the extracted directory and adapt it if needed. |
| 633 | 604 | * |
| 634 | 605 | * @return string The manifest file content. |
| 635 | - * @throws \Error If import validation fails or processing errors occur. | |
| 636 | 606 | */ |
| 637 | 607 | private function read_manifest_json() { |
| 638 | 608 | $manifest = Utils::read_json_file( $this->extracted_directory_path . 'manifest' ); |
| 639 | 609 | |
| @@ -659,9 +629,9 @@ | ||
| 659 | 629 | private function init_adapters( array $manifest_data ) { |
| 660 | 630 | $this->adapters = []; |
| 661 | 631 | |
| 662 | 632 | /** @var Base_Adapter[] $adapter_types */ |
| 663 | - $adapter_types = [ Customization::class, Envato::class, Kit_Library::class ]; | |
| 633 | + $adapter_types = [ Envato::class, Kit_Library::class ]; | |
| 664 | 634 | |
| 665 | 635 | foreach ( $adapter_types as $adapter_type ) { |
| 666 | 636 | if ( $adapter_type::is_compatibility_needed( $manifest_data, [ 'referrer' => $this->get_settings_referrer() ] ) ) { |
| 667 | 637 | $this->adapters[] = new $adapter_type( $this ); |
| @@ -689,17 +659,15 @@ | ||
| 689 | 659 | * |
| 690 | 660 | * @return array Custom post types names. |
| 691 | 661 | */ |
| 692 | 662 | private function get_default_settings_custom_post_types() { |
| 693 | - $excluded = [ 'page', 'nav_menu_item' ]; | |
| 694 | - | |
| 695 | - if ( empty( $this->manifest['content']['post'] ?? [] ) && empty( $this->manifest['wp-content']['post'] ?? [] ) ) { | |
| 696 | - $excluded[] = 'post'; | |
| 663 | + if ( empty( $this->manifest['custom-post-type-title'] ) ) { | |
| 664 | + return []; | |
| 697 | 665 | } |
| 698 | 666 | |
| 699 | - $manifest_post_types = array_keys( $this->manifest['custom-post-type-title'] ?? [] ); | |
| 667 | + $manifest_post_types = array_keys( $this->manifest['custom-post-type-title'] ); | |
| 700 | 668 | |
| 701 | - return array_merge( $manifest_post_types, Utils::get_builtin_wp_post_types( $excluded ) ); | |
| 669 | + return array_diff( $manifest_post_types, Utils::get_builtin_wp_post_types() ); | |
| 702 | 670 | } |
| 703 | 671 | |
| 704 | 672 | /** |
| 705 | 673 | * Get the default settings of elementor templates conditions to override. |
| @@ -744,26 +712,8 @@ | ||
| 744 | 712 | private function get_default_settings_include() { |
| 745 | 713 | return [ 'templates', 'plugins', 'content', 'settings' ]; |
| 746 | 714 | } |
| 747 | 715 | |
| 748 | - public function settings_customization( $customization ) { | |
| 749 | - $this->settings_customization = $customization; | |
| 750 | - return $this; | |
| 751 | - } | |
| 752 | - | |
| 753 | - public function get_settings_customization() { | |
| 754 | - return $this->settings_customization; | |
| 755 | - } | |
| 756 | - | |
| 757 | - private function get_default_settings_customization() { | |
| 758 | - return [ | |
| 759 | - 'settings' => null, | |
| 760 | - 'templates' => null, | |
| 761 | - 'content' => null, | |
| 762 | - 'plugins' => null, | |
| 763 | - ]; | |
| 764 | - } | |
| 765 | - | |
| 766 | 716 | /** |
| 767 | 717 | * Get the data that requires updating/replacement when imported. |
| 768 | 718 | * |
| 769 | 719 | * @return array{post_ids: array, term_ids: array} |
| @@ -785,11 +735,8 @@ | ||
| 785 | 735 | foreach ( $this->documents_data as $new_id => $data ) { |
| 786 | 736 | $document = Plugin::$instance->documents->get( $new_id ); |
| 787 | 737 | |
| 788 | 738 | if ( isset( $data['elements'] ) ) { |
| 789 | - $data['elements'] = Components_Module::prepare_imported_elements( $data['elements'], $imported_data_replacements['post_ids'] ?? [] ); | |
| 790 | - $data['elements'] = Atomic_Prop_Remap::apply( $data['elements'], $imported_data_replacements ); | |
| 791 | - $this->merge_atomic_prop_remap_warnings( Atomic_Prop_Remap::consume_warnings() ); | |
| 792 | 739 | $data['elements'] = $document->on_import_update_dynamic_content( $data['elements'], $imported_data_replacements ); |
| 793 | 740 | } |
| 794 | 741 | |
| 795 | 742 | if ( isset( $data['settings'] ) ) { |
| @@ -803,19 +750,8 @@ | ||
| 803 | 750 | } |
| 804 | 751 | |
| 805 | 752 | $document->save( $data ); |
| 806 | 753 | } |
| 807 | - } | |
| 808 | - | |
| 809 | - private function merge_atomic_prop_remap_warnings( array $warnings ): void { | |
| 810 | - if ( empty( $warnings ) ) { | |
| 811 | - return; | |
| 812 | - } | |
| 813 | - | |
| 814 | - $existing = $this->runners_import_metadata['atomic_prop_remap']['warnings'] ?? []; | |
| 815 | - $this->runners_import_metadata['atomic_prop_remap'] = [ | |
| 816 | - 'warnings' => array_merge( $existing, $warnings ), | |
| 817 | - ]; | |
| 818 | 754 | } |
| 819 | 755 | |
| 820 | 756 | private function update_instance_data_in_import_session_option() { |
| 821 | 757 | $import_sessions = Utils::get_import_sessions(); |