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