← All changes
|
app/modules/import-export/processes/import.php
+50
-316
4.3.0-beta3
→
3.9.2
View file →
| @@ -6,11 +6,8 @@ | ||
| 6 | 6 | use Elementor\App\Modules\ImportExport\Compatibility\Envato; |
| 7 | 7 | use Elementor\App\Modules\ImportExport\Compatibility\Kit_Library; |
| 8 | 8 | use Elementor\App\Modules\ImportExport\Utils; |
| 9 | 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 | 10 | use Elementor\Plugin; |
| 14 | 11 | |
| 15 | 12 | use Elementor\App\Modules\ImportExport\Runners\Import\Elementor_Content; |
| 16 | 13 | use Elementor\App\Modules\ImportExport\Runners\Import\Import_Runner_Base; |
| @@ -23,11 +20,11 @@ | ||
| 23 | 20 | |
| 24 | 21 | class Import { |
| 25 | 22 | const MANIFEST_ERROR_KEY = 'manifest-error'; |
| 26 | 23 | |
| 27 | - const ZIP_FILE_ERROR_KEY = 'invalid-zip-file'; | |
| 24 | + const SESSION_DOES_NOT_EXITS_ERROR = 'session-does-not-exits-error'; | |
| 28 | 25 | |
| 29 | - const ZIP_ARCHIVE_ERROR_KEY = 'zip-archive-module-missing'; | |
| 26 | + const ZIP_FILE_ERROR_KEY = 'zip-file-error'; | |
| 30 | 27 | |
| 31 | 28 | /** |
| 32 | 29 | * @var Import_Runner_Base[] |
| 33 | 30 | */ |
| @@ -41,15 +38,8 @@ | ||
| 41 | 38 | */ |
| 42 | 39 | private $session_id; |
| 43 | 40 | |
| 44 | 41 | /** |
| 45 | - * The Kit ID. | |
| 46 | - * | |
| 47 | - * @var string | |
| 48 | - */ | |
| 49 | - private $kit_id; | |
| 50 | - | |
| 51 | - /** | |
| 52 | 42 | * Adapter for the kit compatibility. |
| 53 | 43 | * |
| 54 | 44 | * @var Base_Adapter[] |
| 55 | 45 | */ |
| @@ -55,13 +45,13 @@ | ||
| 55 | 45 | */ |
| 56 | 46 | private $adapters; |
| 57 | 47 | |
| 58 | 48 | /** |
| 59 | - * Document's data (elements and settings) that was imported during the process. | |
| 49 | + * Document's elements that imported during the process. | |
| 60 | 50 | * |
| 61 | - * @var array { [document_id] => { "elements": array , "settings": array } } | |
| 51 | + * @var array | |
| 62 | 52 | */ |
| 63 | - private $documents_data = []; | |
| 53 | + private $documents_elements = []; | |
| 64 | 54 | |
| 65 | 55 | /** |
| 66 | 56 | * Path to the extracted kit files. |
| 67 | 57 | * |
| @@ -140,105 +130,43 @@ | ||
| 140 | 130 | */ |
| 141 | 131 | private $runners_import_metadata = []; |
| 142 | 132 | |
| 143 | 133 | /** |
| 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. | |
| 134 | + * @param $path string session_id | zip_file_path | |
| 135 | + * @param $settings array Use to determine which content to import. | |
| 136 | + * (e.g: include, selected_plugins, selected_cpt, selected_override_conditions, etc.) | |
| 137 | + * @throws \Exception | |
| 151 | 138 | */ |
| 152 | - public function __construct( string $path, array $settings = [], array $old_instance = null ) { | |
| 153 | - if ( ! empty( $old_instance ) ) { | |
| 154 | - $this->set_import_object( $old_instance ); | |
| 139 | + public function __construct( string $path, array $settings = [] ) { | |
| 140 | + if ( is_file( $path ) ) { | |
| 141 | + $this->extracted_directory_path = $this->extract_zip( $path ); | |
| 155 | 142 | } 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 ); | |
| 143 | + $elementor_tmp_directory = Plugin::$instance->uploads_manager->get_temp_dir(); | |
| 144 | + $path = $elementor_tmp_directory . basename( $path ); | |
| 161 | 145 | |
| 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 . '/'; | |
| 146 | + if ( ! is_dir( $path ) ) { | |
| 147 | + throw new \Exception( static::SESSION_DOES_NOT_EXITS_ERROR ); | |
| 167 | 148 | } |
| 168 | 149 | |
| 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(); | |
| 150 | + $this->extracted_directory_path = $path . '/'; | |
| 183 | 151 | } |
| 184 | 152 | |
| 185 | - add_filter( 'wp_php_error_args', function ( $args, $error ) { | |
| 186 | - return $this->filter_php_error_args( $args, $error ); | |
| 187 | - }, 10, 2 ); | |
| 188 | - } | |
| 153 | + $this->session_id = basename( $this->extracted_directory_path ); | |
| 154 | + $this->settings_referrer = ! empty( $settings['referrer'] ) ? $settings['referrer'] : 'local'; | |
| 155 | + $this->settings_include = ! empty( $settings['include'] ) ? $settings['include'] : null; | |
| 189 | 156 | |
| 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']; | |
| 157 | + // Using isset and not empty is important since empty array is valid option. | |
| 158 | + $this->settings_selected_override_conditions = $settings['overrideConditions'] ?? null; | |
| 159 | + $this->settings_selected_custom_post_types = $settings['selectedCustomPostTypes'] ?? null; | |
| 160 | + $this->settings_selected_plugins = $settings['plugins'] ?? null; | |
| 198 | 161 | |
| 199 | - $instance_data = $instance['instance_data']; | |
| 162 | + $this->manifest = $this->read_manifest_json(); | |
| 163 | + $this->site_settings = $this->read_site_settings_json(); | |
| 200 | 164 | |
| 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']; | |
| 165 | + $this->set_default_settings(); | |
| 218 | 166 | } |
| 219 | 167 | |
| 220 | 168 | /** |
| 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 | 169 | * Register a runner. |
| 242 | 170 | * Be aware that the runner will be executed in the order of registration, the order is crucial for the import process. |
| 243 | 171 | * |
| 244 | 172 | * @param Import_Runner_Base $runner_instance |
| @@ -284,14 +212,15 @@ | ||
| 284 | 212 | /** |
| 285 | 213 | * Execute the import process. |
| 286 | 214 | * |
| 287 | 215 | * @return array The imported data output. |
| 288 | - * | |
| 289 | - * @throws \Exception If no import runners have been specified. | |
| 216 | + * @throws \Exception | |
| 290 | 217 | */ |
| 291 | 218 | public function run() { |
| 219 | + $start_time = current_time( 'timestamp' ); | |
| 220 | + | |
| 292 | 221 | 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.' ); | |
| 222 | + throw new \Exception( 'Please specify import runners.' ); | |
| 294 | 223 | } |
| 295 | 224 | |
| 296 | 225 | $data = [ |
| 297 | 226 | 'session_id' => $this->session_id, |
| @@ -302,11 +231,8 @@ | ||
| 302 | 231 | 'extracted_directory_path' => $this->extracted_directory_path, |
| 303 | 232 | 'selected_custom_post_types' => $this->settings_selected_custom_post_types, |
| 304 | 233 | ]; |
| 305 | 234 | |
| 306 | - $this->init_import_session(); | |
| 307 | - | |
| 308 | - remove_filter( 'elementor/document/save/data', [ Plugin::$instance->modules_manager->get_modules( 'content-sanitizer' ), 'sanitize_content' ] ); | |
| 309 | 235 | add_filter( 'elementor/document/save/data', [ $this, 'prevent_saving_elements_on_post_creation' ], 10, 2 ); |
| 310 | 236 | |
| 311 | 237 | // Set the Request's state as an Elementor upload request, in order to support unfiltered file uploads. |
| 312 | 238 | Plugin::$instance->uploads_manager->set_elementor_upload_state( true ); |
| @@ -324,9 +250,9 @@ | ||
| 324 | 250 | Plugin::$instance->uploads_manager->set_elementor_upload_state( false ); |
| 325 | 251 | |
| 326 | 252 | remove_filter( 'elementor/document/save/data', [ $this, 'prevent_saving_elements_on_post_creation' ], 10 ); |
| 327 | 253 | |
| 328 | - $this->finalize_import_session_option(); | |
| 254 | + $this->add_import_session_option( $start_time ); | |
| 329 | 255 | |
| 330 | 256 | $this->save_elements_of_imported_posts(); |
| 331 | 257 | |
| 332 | 258 | Plugin::$instance->uploads_manager->remove_file_or_dir( $this->extracted_directory_path ); |
| @@ -332,129 +258,8 @@ | ||
| 332 | 258 | Plugin::$instance->uploads_manager->remove_file_or_dir( $this->extracted_directory_path ); |
| 333 | 259 | return $this->imported_data; |
| 334 | 260 | } |
| 335 | 261 | |
| 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 | 262 | public function get_manifest() { |
| 458 | 263 | return $this->manifest; |
| 459 | 264 | } |
| 460 | 265 | |
| @@ -469,12 +274,8 @@ | ||
| 469 | 274 | public function get_adapters() { |
| 470 | 275 | return $this->adapters; |
| 471 | 276 | } |
| 472 | 277 | |
| 473 | - public function get_imported_data() { | |
| 474 | - return $this->imported_data; | |
| 475 | - } | |
| 476 | - | |
| 477 | 278 | /** |
| 478 | 279 | * Get settings by key. |
| 479 | 280 | * Used for backward compatibility. |
| 480 | 281 | * |
| @@ -561,9 +362,9 @@ | ||
| 561 | 362 | |
| 562 | 363 | /** |
| 563 | 364 | * Prevent saving elements on elementor post creation. |
| 564 | 365 | * |
| 565 | - * @param array $data | |
| 366 | + * @param array $data | |
| 566 | 367 | * @param Document $document |
| 567 | 368 | * |
| 568 | 369 | * @return array |
| 569 | 370 | */ |
| @@ -568,18 +369,13 @@ | ||
| 568 | 369 | * @return array |
| 569 | 370 | */ |
| 570 | 371 | public function prevent_saving_elements_on_post_creation( array $data, Document $document ) { |
| 571 | 372 | if ( isset( $data['elements'] ) ) { |
| 572 | - $this->documents_data[ $document->get_main_id() ] = [ 'elements' => $data['elements'] ]; | |
| 373 | + $this->documents_elements[ $document->get_main_id() ] = $data['elements']; | |
| 573 | 374 | |
| 574 | 375 | $data['elements'] = []; |
| 575 | 376 | } |
| 576 | 377 | |
| 577 | - if ( isset( $data['settings'] ) ) { | |
| 578 | - $this->documents_data[ $document->get_main_id() ]['settings'] = $data['settings']; | |
| 579 | - | |
| 580 | - } | |
| 581 | - | |
| 582 | 378 | return $data; |
| 583 | 379 | } |
| 584 | 380 | |
| 585 | 381 | /** |
| @@ -586,19 +382,14 @@ | ||
| 586 | 382 | * Extract the zip file. |
| 587 | 383 | * |
| 588 | 384 | * @param string $zip_path The path to the zip file. |
| 589 | 385 | * @return string The extracted directory path. |
| 590 | - * @throws \Error If import process fails, file validation errors occur, or data corruption is detected. | |
| 591 | 386 | */ |
| 592 | 387 | private function extract_zip( $zip_path ) { |
| 593 | 388 | $extraction_result = Plugin::$instance->uploads_manager->extract_and_validate_zip( $zip_path, [ 'json', 'xml' ] ); |
| 594 | 389 | |
| 595 | 390 | 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 | |
| 391 | + throw new \Error( static::ZIP_FILE_ERROR_KEY ); | |
| 601 | 392 | } |
| 602 | 393 | |
| 603 | 394 | return $extraction_result['extraction_directory']; |
| 604 | 395 | } |
| @@ -606,17 +397,14 @@ | ||
| 606 | 397 | /** |
| 607 | 398 | * Get the manifest file from the extracted directory and adapt it if needed. |
| 608 | 399 | * |
| 609 | 400 | * @return string The manifest file content. |
| 610 | - * | |
| 611 | - * @throws \Error If import validation fails or processing errors occur. | |
| 612 | 401 | */ |
| 613 | 402 | private function read_manifest_json() { |
| 614 | 403 | $manifest = Utils::read_json_file( $this->extracted_directory_path . 'manifest' ); |
| 615 | 404 | |
| 616 | 405 | 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 | |
| 406 | + throw new \Error( static::MANIFEST_ERROR_KEY ); | |
| 619 | 407 | } |
| 620 | 408 | |
| 621 | 409 | $this->init_adapters( $manifest ); |
| 622 | 410 | |
| @@ -723,9 +511,9 @@ | ||
| 723 | 511 | * Get the data that requires updating/replacement when imported. |
| 724 | 512 | * |
| 725 | 513 | * @return array{post_ids: array, term_ids: array} |
| 726 | 514 | */ |
| 727 | - private function get_imported_data_replacements(): array { | |
| 515 | + private function get_imported_data_replacements() : array { | |
| 728 | 516 | return [ |
| 729 | 517 | 'post_ids' => Utils::map_old_new_post_ids( $this->imported_data ), |
| 730 | 518 | 'term_ids' => Utils::map_old_new_term_ids( $this->imported_data ), |
| 731 | 519 | ]; |
| @@ -735,81 +523,27 @@ | ||
| 735 | 523 | * Save the prevented elements on elementor post creation elements. |
| 736 | 524 | * Handle the replacement of all the dynamic content of the elements that probably have been changed during the import. |
| 737 | 525 | */ |
| 738 | 526 | 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 ) { | |
| 527 | + foreach ( $this->documents_elements as $new_id => $document_elements ) { | |
| 742 | 528 | $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 ); | |
| 529 | + $updated_elements = $document->on_import_update_dynamic_content( $document_elements, $this->get_imported_data_replacements() ); | |
| 530 | + $document->save( [ 'elements' => $updated_elements ] ); | |
| 762 | 531 | } |
| 763 | 532 | } |
| 764 | 533 | |
| 765 | - private function merge_atomic_prop_remap_warnings( array $warnings ): void { | |
| 766 | - if ( empty( $warnings ) ) { | |
| 767 | - return; | |
| 768 | - } | |
| 534 | + private function add_import_session_option( $start_time ) { | |
| 535 | + $import_sessions = get_option( Module::OPTION_KEY_ELEMENTOR_IMPORT_SESSIONS ); | |
| 769 | 536 | |
| 770 | - $existing = $this->runners_import_metadata['atomic_prop_remap']['warnings'] ?? []; | |
| 771 | - $this->runners_import_metadata['atomic_prop_remap'] = [ | |
| 772 | - 'warnings' => array_merge( $existing, $warnings ), | |
| 537 | + $import_sessions[ time() ] = [ | |
| 538 | + 'session_id' => $this->session_id, | |
| 539 | + 'kit_name' => $this->manifest['name'], | |
| 540 | + 'kit_source' => $this->settings_referrer, | |
| 541 | + 'user_id' => get_current_user_id(), | |
| 542 | + 'start_timestamp' => $start_time, | |
| 543 | + 'end_timestamp' => current_time( 'timestamp' ), | |
| 544 | + 'runners' => $this->runners_import_metadata, | |
| 773 | 545 | ]; |
| 774 | - } | |
| 775 | 546 | |
| 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 | 547 | 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 | 548 | } |
| 815 | 549 | } |