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