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