| 1 |
<?php |
| 2 |
namespace Elementor\App\Modules\ImportExportCustomization; |
| 3 |
|
| 4 |
use Elementor\App\Modules\ImportExportCustomization\Processes\Export; |
| 5 |
use Elementor\App\Modules\ImportExportCustomization\Processes\Import; |
| 6 |
use Elementor\App\Modules\ImportExportCustomization\Processes\Revert; |
| 7 |
use Elementor\Core\Base\Module as BaseModule; |
| 8 |
use Elementor\Core\Files\Uploads_Manager; |
| 9 |
use Elementor\Modules\CloudKitLibrary\Module as CloudKitLibrary; |
| 10 |
use Elementor\Modules\System_Info\Reporters\Server; |
| 11 |
use Elementor\Plugin; |
| 12 |
use Elementor\Tools; |
| 13 |
use Elementor\Utils as ElementorUtils; |
| 14 |
use Elementor\App\Modules\ImportExportCustomization\Utils as ImportExportUtils; |
| 15 |
use Elementor\App\Modules\ImportExportCustomization\Data\Controller; |
| 16 |
use Elementor\Core\Settings\Manager as SettingsManager; |
| 17 |
|
| 18 |
if ( ! defined( 'ABSPATH' ) ) { |
| 19 |
exit; // Exit if accessed directly. |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Import Export Module |
| 24 |
* |
| 25 |
* Responsible for initializing Elementor App functionality |
| 26 |
*/ |
| 27 |
class Module extends BaseModule { |
| 28 |
const FORMAT_VERSION = '2.0'; |
| 29 |
|
| 30 |
const REFERRER_KIT_LIBRARY = 'kit-library'; |
| 31 |
|
| 32 |
const REFERRER_LOCAL = 'local'; |
| 33 |
|
| 34 |
const REFERRER_CLOUD = 'cloud'; |
| 35 |
|
| 36 |
const PLUGIN_PERMISSIONS_ERROR_KEY = 'plugin-installation-permissions-error'; |
| 37 |
|
| 38 |
const KIT_LIBRARY_ERROR_KEY = 'invalid-kit-library-zip-error'; |
| 39 |
|
| 40 |
const NO_WRITE_PERMISSIONS_KEY = 'no-write-permissions'; |
| 41 |
|
| 42 |
const THIRD_PARTY_ERROR = 'third-party-error'; |
| 43 |
|
| 44 |
const DOMDOCUMENT_MISSING = 'domdocument-missing'; |
| 45 |
|
| 46 |
const OPTION_KEY_ELEMENTOR_IMPORT_SESSIONS = 'elementor_import_sessions'; |
| 47 |
|
| 48 |
const OPTION_KEY_ELEMENTOR_REVERT_SESSIONS = 'elementor_revert_sessions'; |
| 49 |
|
| 50 |
const META_KEY_ELEMENTOR_IMPORT_SESSION_ID = '_elementor_import_session_id'; |
| 51 |
|
| 52 |
const META_KEY_ELEMENTOR_EDIT_MODE = '_elementor_edit_mode'; |
| 53 |
const IMPORT_PLUGINS_ACTION = 'import-plugins'; |
| 54 |
const EXPORT_SOURCE_CLOUD = 'cloud'; |
| 55 |
const EXPORT_SOURCE_FILE = 'file'; |
| 56 |
|
| 57 |
/** |
| 58 |
* Assigning the export process to a property, so we can use the process from outside the class. |
| 59 |
* |
| 60 |
* @var Export |
| 61 |
*/ |
| 62 |
public $export; |
| 63 |
|
| 64 |
/** |
| 65 |
* Assigning the import process to a property, so we can use the process from outside the class. |
| 66 |
* |
| 67 |
* @var Import |
| 68 |
*/ |
| 69 |
public $import; |
| 70 |
|
| 71 |
/** |
| 72 |
* Assigning the revert process to a property, so we can use the process from outside the class. |
| 73 |
* |
| 74 |
* @var Revert |
| 75 |
*/ |
| 76 |
public $revert; |
| 77 |
|
| 78 |
/** |
| 79 |
* Get name. |
| 80 |
* |
| 81 |
* @access public |
| 82 |
* |
| 83 |
* @return string |
| 84 |
*/ |
| 85 |
public function get_name() { |
| 86 |
return 'import-export-customization'; |
| 87 |
} |
| 88 |
|
| 89 |
public function __construct() { |
| 90 |
$this->register_actions(); |
| 91 |
|
| 92 |
Controller::register_hooks(); |
| 93 |
|
| 94 |
if ( ElementorUtils::is_wp_cli() ) { |
| 95 |
\WP_CLI::add_command( 'elementor kit', WP_CLI::class ); |
| 96 |
} |
| 97 |
|
| 98 |
( new Usage() )->register(); |
| 99 |
|
| 100 |
$this->revert = new Revert(); |
| 101 |
} |
| 102 |
|
| 103 |
public function get_init_settings() { |
| 104 |
if ( ! Plugin::$instance->app->is_current() ) { |
| 105 |
return []; |
| 106 |
} |
| 107 |
|
| 108 |
return $this->get_config_data(); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Register the import/export tab in elementor tools. |
| 113 |
*/ |
| 114 |
public function register_settings_tab( Tools $tools ) { |
| 115 |
$tools->add_tab( 'import-export-kit', [ |
| 116 |
'label' => esc_html__( 'Website Templates', 'elementor' ), |
| 117 |
'sections' => [ |
| 118 |
'intro' => [ |
| 119 |
'label' => esc_html__( 'Website Templates', 'elementor' ), |
| 120 |
'callback' => function() { |
| 121 |
$this->render_import_export_tab_content(); |
| 122 |
}, |
| 123 |
'fields' => [], |
| 124 |
], |
| 125 |
], |
| 126 |
] ); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Render the import/export tab content. |
| 131 |
*/ |
| 132 |
private function render_import_export_tab_content() { |
| 133 |
$is_cloud_kits_available = Plugin::$instance->experiments->is_feature_active( 'cloud-library' ) && CloudKitLibrary::get_app()->is_eligible(); |
| 134 |
|
| 135 |
$content_data = [ |
| 136 |
'export' => [ |
| 137 |
'title' => esc_html__( 'Export this website', 'elementor' ), |
| 138 |
'button' => [ |
| 139 |
'url' => Plugin::$instance->app->get_base_url() . '#/export', |
| 140 |
'text' => esc_html__( 'Export', 'elementor' ), |
| 141 |
], |
| 142 |
'description' => esc_html__( 'You can download this website as a .zip file, or upload it to the library.', 'elementor' ), |
| 143 |
], |
| 144 |
'import' => [ |
| 145 |
'title' => esc_html__( 'Apply a Website Template', 'elementor' ), |
| 146 |
'button' => [ |
| 147 |
'url' => Plugin::$instance->app->get_base_url() . '#/import', |
| 148 |
'text' => $is_cloud_kits_available ? esc_html__( 'Upload .zip file', 'elementor' ) : esc_html__( 'Import', 'elementor' ), |
| 149 |
], |
| 150 |
'description' => esc_html__( 'You can import design and settings from a .zip file or choose from the library.', 'elementor' ), |
| 151 |
], |
| 152 |
]; |
| 153 |
|
| 154 |
if ( $is_cloud_kits_available ) { |
| 155 |
$content_data['import']['button_secondary'] = [ |
| 156 |
'url' => Plugin::$instance->app->get_base_url() . '#/kit-library/cloud', |
| 157 |
'text' => esc_html__( 'Open the Library', 'elementor' ), |
| 158 |
]; |
| 159 |
} |
| 160 |
|
| 161 |
$last_imported_kit = $this->revert->get_last_import_session(); |
| 162 |
$penultimate_imported_kit = $this->revert->get_penultimate_import_session(); |
| 163 |
|
| 164 |
$user_date_format = get_option( 'date_format' ); |
| 165 |
$user_time_format = get_option( 'time_format' ); |
| 166 |
$date_format = $user_date_format . ' ' . $user_time_format; |
| 167 |
|
| 168 |
$should_show_revert_section = $this->should_show_revert_section( $last_imported_kit ); |
| 169 |
|
| 170 |
if ( $should_show_revert_section ) { |
| 171 |
if ( ! empty( $penultimate_imported_kit ) ) { |
| 172 |
$revert_text = sprintf( |
| 173 |
esc_html__( 'Remove all the content and site settings that came with "%1$s" on %2$s %3$s and revert to the site setting that came with "%4$s" on %5$s.', 'elementor' ), |
| 174 |
! empty( $last_imported_kit['kit_title'] ) ? $last_imported_kit['kit_title'] : esc_html__( 'imported kit', 'elementor' ), |
| 175 |
gmdate( $date_format, $last_imported_kit['start_timestamp'] ), |
| 176 |
'<br>', |
| 177 |
! empty( $penultimate_imported_kit['kit_title'] ) ? $penultimate_imported_kit['kit_title'] : esc_html__( 'imported kit', 'elementor' ), |
| 178 |
gmdate( $date_format, $penultimate_imported_kit['start_timestamp'] ) |
| 179 |
); |
| 180 |
} else { |
| 181 |
$revert_text = sprintf( |
| 182 |
esc_html__( 'Remove all the content and site settings that came with "%1$s" on %2$s.%3$s Your original site settings will be restored.', 'elementor' ), |
| 183 |
! empty( $last_imported_kit['kit_title'] ) ? $last_imported_kit['kit_title'] : esc_html__( 'imported kit', 'elementor' ), |
| 184 |
gmdate( $date_format, $last_imported_kit['start_timestamp'] ), |
| 185 |
'<br>' |
| 186 |
); |
| 187 |
} |
| 188 |
} |
| 189 |
?> |
| 190 |
|
| 191 |
<div class="tab-import-export-kit__content"> |
| 192 |
<p class="tab-import-export-kit__info"> |
| 193 |
<?php |
| 194 |
printf( |
| 195 |
'%1$s <a href="https://go.elementor.com/wp-dash-import-export-general/" target="_blank">%2$s</a>', |
| 196 |
esc_html__( 'Here’s where you can export this website as a .zip file, upload it to the cloud, or start the process of applying an existing template to your site.', 'elementor' ), |
| 197 |
esc_html__( 'Learn more', 'elementor' ), |
| 198 |
); |
| 199 |
?> |
| 200 |
</p> |
| 201 |
|
| 202 |
<div class="tab-import-export-kit__wrapper"> |
| 203 |
<?php foreach ( $content_data as $data ) { |
| 204 |
$this->print_item_content( $data ); |
| 205 |
} ?> |
| 206 |
</div> |
| 207 |
|
| 208 |
<?php |
| 209 |
if ( $should_show_revert_section ) { |
| 210 |
|
| 211 |
$link_attributes = [ |
| 212 |
'href' => $this->get_revert_href(), |
| 213 |
'id' => 'elementor-import-export__revert_kit', |
| 214 |
'class' => 'button', |
| 215 |
]; |
| 216 |
?> |
| 217 |
<div class="tab-import-export-kit__revert"> |
| 218 |
<h2> |
| 219 |
<?php echo esc_html__( 'Remove the most recent Website Template', 'elementor' ); ?> |
| 220 |
</h2> |
| 221 |
<p class="tab-import-export-kit__info"> |
| 222 |
<?php ElementorUtils::print_unescaped_internal_string( $revert_text ); ?> |
| 223 |
</p> |
| 224 |
<?php $this->render_last_kit_thumbnail( $last_imported_kit ); ?> |
| 225 |
<a <?php ElementorUtils::print_html_attributes( $link_attributes ); ?> > |
| 226 |
<?php echo esc_html__( 'Remove Website Template', 'elementor' ); ?> |
| 227 |
</a> |
| 228 |
</div> |
| 229 |
<?php } ?> |
| 230 |
</div> |
| 231 |
<?php |
| 232 |
} |
| 233 |
|
| 234 |
private function print_item_content( $data ) { |
| 235 |
$is_cloud_kits_feature_active = Plugin::$instance->experiments->is_feature_active( 'cloud-library' ); |
| 236 |
|
| 237 |
if ( $is_cloud_kits_feature_active ) { ?> |
| 238 |
<div class="tab-import-export-kit__container"> |
| 239 |
<div class="tab-import-export-kit__box"> |
| 240 |
<h2><?php ElementorUtils::print_unescaped_internal_string( $data['title'] ); ?></h2> |
| 241 |
</div> |
| 242 |
<p class="description"><?php ElementorUtils::print_unescaped_internal_string( $data['description'] ); ?></p> |
| 243 |
|
| 244 |
<?php if ( ! empty( $data['link'] ) ) : ?> |
| 245 |
<a href="<?php ElementorUtils::print_unescaped_internal_string( $data['link']['url'] ); ?>" target="_blank"><?php ElementorUtils::print_unescaped_internal_string( $data['link']['text'] ); ?></a> |
| 246 |
<?php endif; ?> |
| 247 |
<div class="tab-import-export-kit__box action-buttons"> |
| 248 |
<?php if ( ! empty( $data['button_secondary'] ) ) : ?> |
| 249 |
<a href="<?php ElementorUtils::print_unescaped_internal_string( $data['button_secondary']['url'] ); ?>" class="elementor-button e-btn-txt e-btn-txt-border"> |
| 250 |
<?php ElementorUtils::print_unescaped_internal_string( $data['button_secondary']['text'] ); ?> |
| 251 |
</a> |
| 252 |
<?php endif; ?> |
| 253 |
<a href="<?php ElementorUtils::print_unescaped_internal_string( $data['button']['url'] ); ?>" class="elementor-button e-primary"> |
| 254 |
<?php ElementorUtils::print_unescaped_internal_string( $data['button']['text'] ); ?> |
| 255 |
</a> |
| 256 |
</div> |
| 257 |
</div> |
| 258 |
<?php } else { ?> |
| 259 |
<div class="tab-import-export-kit__container"> |
| 260 |
<div class="tab-import-export-kit__box"> |
| 261 |
<h2><?php ElementorUtils::print_unescaped_internal_string( $data['title'] ); ?></h2> |
| 262 |
<a href="<?php ElementorUtils::print_unescaped_internal_string( $data['button']['url'] ); ?>" class="elementor-button e-primary"> |
| 263 |
<?php ElementorUtils::print_unescaped_internal_string( $data['button']['text'] ); ?> |
| 264 |
</a> |
| 265 |
</div> |
| 266 |
<p><?php ElementorUtils::print_unescaped_internal_string( $data['description'] ); ?></p> |
| 267 |
<a href="<?php ElementorUtils::print_unescaped_internal_string( $data['link']['url'] ); ?>" target="_blank"><?php ElementorUtils::print_unescaped_internal_string( $data['link']['text'] ); ?></a> |
| 268 |
</div> |
| 269 |
<?php } |
| 270 |
} |
| 271 |
|
| 272 |
private function get_revert_href(): string { |
| 273 |
$admin_post_url = admin_url( 'admin-post.php?action=elementor_revert_kit' ); |
| 274 |
$nonced_admin_post_url = wp_nonce_url( $admin_post_url, 'elementor_revert_kit' ); |
| 275 |
return $this->maybe_add_referrer_param( $nonced_admin_post_url ); |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Checks if referred by a kit and adds the referrer ID to the href |
| 280 |
* |
| 281 |
* @param string $href |
| 282 |
* |
| 283 |
* @return string |
| 284 |
*/ |
| 285 |
private function maybe_add_referrer_param( string $href ): string { |
| 286 |
$param_name = 'referrer_kit'; |
| 287 |
|
| 288 |
if ( empty( $_GET[ $param_name ] ) ) { |
| 289 |
return $href; |
| 290 |
} |
| 291 |
|
| 292 |
return add_query_arg( $param_name, sanitize_key( $_GET[ $param_name ] ), $href ); |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* Render the last kit thumbnail if exists |
| 297 |
* |
| 298 |
* @param $last_imported_kit |
| 299 |
* |
| 300 |
* @return void |
| 301 |
*/ |
| 302 |
private function render_last_kit_thumbnail( $last_imported_kit ) { |
| 303 |
if ( empty( $last_imported_kit['kit_thumbnail'] ) ) { |
| 304 |
return; |
| 305 |
} |
| 306 |
|
| 307 |
?> |
| 308 |
<div class="tab-import-export-kit__kit-item-row"> |
| 309 |
<article class="tab-import-export-kit__kit-item"> |
| 310 |
<header> |
| 311 |
<h3> |
| 312 |
<?php echo esc_html( $last_imported_kit['kit_title'] ); ?> |
| 313 |
</h3> |
| 314 |
</header> |
| 315 |
<img |
| 316 |
src="<?php echo esc_url( $last_imported_kit['kit_thumbnail'] ); ?>" |
| 317 |
alt="<?php echo esc_attr( $last_imported_kit['kit_title'] ); ?>" |
| 318 |
loading="lazy" |
| 319 |
> |
| 320 |
</article> |
| 321 |
</div> |
| 322 |
<?php |
| 323 |
} |
| 324 |
|
| 325 |
/** |
| 326 |
* Upload a kit zip file and get the kit data. |
| 327 |
* |
| 328 |
* Assigning the Import process to the 'import' property, |
| 329 |
* so it will be available to use in different places such as: WP_Cli, Pro, etc. |
| 330 |
* |
| 331 |
* @param string $file Path to the file. |
| 332 |
* @param string $referrer Referrer of the file 'local' or 'kit-library'. |
| 333 |
* @param string $kit_id |
| 334 |
* @return array |
| 335 |
* @throws \Exception |
| 336 |
*/ |
| 337 |
public function upload_kit( $file, $referrer, $kit_id = null ) { |
| 338 |
$this->ensure_writing_permissions(); |
| 339 |
|
| 340 |
$this->import = new Import( $file, [ |
| 341 |
'referrer' => $referrer, |
| 342 |
'id' => $kit_id, |
| 343 |
] ); |
| 344 |
|
| 345 |
return [ |
| 346 |
'session' => $this->import->get_session_id(), |
| 347 |
'manifest' => $this->import->get_manifest(), |
| 348 |
'conflicts' => $this->import->get_settings_conflicts(), |
| 349 |
]; |
| 350 |
} |
| 351 |
|
| 352 |
/** |
| 353 |
* Import a kit by session_id. |
| 354 |
* Upload and import a kit by kit zip file. |
| 355 |
* |
| 356 |
* If the split_to_chunks flag is true, the process won't start |
| 357 |
* It will initialize the import process and return the session_id and the runners. |
| 358 |
* |
| 359 |
* Assigning the Import process to the 'import' property, |
| 360 |
* so it will be available to use in different places such as: WP_Cli, Pro, etc. |
| 361 |
* |
| 362 |
* @param string $path Path to the file or session_id. |
| 363 |
* @param array $settings Settings the import use to determine which content to import. |
| 364 |
* (e.g: include, selected_plugins, selected_cpt, selected_override_conditions, etc.) |
| 365 |
* @param bool $split_to_chunks Determine if the import process should be split into chunks. |
| 366 |
* @return array |
| 367 |
* @throws \Exception |
| 368 |
*/ |
| 369 |
public function import_kit( string $path, array $settings, bool $split_to_chunks = false ): array { |
| 370 |
$this->ensure_writing_permissions(); |
| 371 |
$this->ensure_DOMDocument_exists(); |
| 372 |
|
| 373 |
$this->import = new Import( $path, $settings ); |
| 374 |
$this->import->register_default_runners(); |
| 375 |
|
| 376 |
remove_filter( 'elementor/document/save/data', [ Plugin::$instance->modules_manager->get_modules( 'content-sanitizer' ), 'sanitize_content' ] ); |
| 377 |
do_action( 'elementor/import-export-customization/import-kit', $this->import ); |
| 378 |
|
| 379 |
if ( $split_to_chunks ) { |
| 380 |
$this->import->init_import_session( true ); |
| 381 |
|
| 382 |
return [ |
| 383 |
'session' => $this->import->get_session_id(), |
| 384 |
'runners' => $this->import->get_runners_name(), |
| 385 |
]; |
| 386 |
} |
| 387 |
|
| 388 |
return $this->import->run(); |
| 389 |
} |
| 390 |
|
| 391 |
/** |
| 392 |
* Resuming import process by re-creating the import instance and running the specific runner. |
| 393 |
* |
| 394 |
* @param string $session_id The id off the import session. |
| 395 |
* @param string $runner_name The specific runner that we want to run. |
| 396 |
* |
| 397 |
* @return array Two types of response. |
| 398 |
* 1. The status and the runner name. |
| 399 |
* 2. The imported data. (Only if the runner is the last one in the import process) |
| 400 |
* @throws \Exception |
| 401 |
*/ |
| 402 |
public function import_kit_by_runner( string $session_id, string $runner_name ): array { |
| 403 |
// Check session_id |
| 404 |
$this->import = Import::from_session( $session_id ); |
| 405 |
$runners = $this->import->get_runners_name(); |
| 406 |
|
| 407 |
$run = $this->import->run_runner( $runner_name ); |
| 408 |
|
| 409 |
if ( end( $runners ) === $run['runner'] ) { |
| 410 |
return $this->import->get_imported_data(); |
| 411 |
} |
| 412 |
|
| 413 |
return $run; |
| 414 |
} |
| 415 |
|
| 416 |
/** |
| 417 |
* Export a kit. |
| 418 |
* |
| 419 |
* Assigning the Export process to the 'export' property, |
| 420 |
* so it will be available to use in different places such as: WP_Cli, Pro, etc. |
| 421 |
* |
| 422 |
* @param array $settings Settings the export use to determine which content to export. |
| 423 |
* (e.g: include, kit_info, selected_plugins, selected_cpt, etc.) |
| 424 |
* @return array |
| 425 |
* @throws \Exception |
| 426 |
*/ |
| 427 |
public function export_kit( array $settings ) { |
| 428 |
$this->ensure_writing_permissions(); |
| 429 |
|
| 430 |
$this->export = new Export( $settings ); |
| 431 |
$this->export->register_default_runners(); |
| 432 |
|
| 433 |
do_action( 'elementor/import-export-customization/export-kit', $this->export ); |
| 434 |
|
| 435 |
return $this->export->run(); |
| 436 |
} |
| 437 |
|
| 438 |
/** |
| 439 |
* Handle revert kit ajax request. |
| 440 |
*/ |
| 441 |
public function revert_last_imported_kit() { |
| 442 |
$this->revert = new Revert(); |
| 443 |
$this->revert->register_default_runners(); |
| 444 |
|
| 445 |
do_action( 'elementor/import-export-customization/revert-kit', $this->revert ); |
| 446 |
|
| 447 |
$this->revert->run(); |
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
/** |
| 452 |
* Handle revert last imported kit ajax request. |
| 453 |
*/ |
| 454 |
public function handle_revert_last_imported_kit() { |
| 455 |
check_admin_referer( 'elementor_revert_kit' ); |
| 456 |
|
| 457 |
$this->revert_last_imported_kit(); |
| 458 |
|
| 459 |
wp_safe_redirect( admin_url( 'admin.php?page=' . Tools::PAGE_ID . '#tab-import-export-kit' ) ); |
| 460 |
die; |
| 461 |
} |
| 462 |
|
| 463 |
/** |
| 464 |
* Register appropriate actions. |
| 465 |
*/ |
| 466 |
private function register_actions() { |
| 467 |
add_action( 'admin_post_elementor_revert_kit', [ $this, 'handle_revert_last_imported_kit' ] ); |
| 468 |
|
| 469 |
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] ); |
| 470 |
|
| 471 |
$page_id = Tools::PAGE_ID; |
| 472 |
|
| 473 |
add_action( "elementor/admin/after_create_settings/{$page_id}", [ $this, 'register_settings_tab' ] ); |
| 474 |
|
| 475 |
// TODO 18/04/2023 : This needs to be moved to the runner itself after https://elementor.atlassian.net/browse/HTS-434 is done. |
| 476 |
if ( self::IMPORT_PLUGINS_ACTION === ElementorUtils::get_super_global_value( $_SERVER, 'HTTP_X_ELEMENTOR_ACTION' ) ) { |
| 477 |
add_filter( 'woocommerce_create_pages', [ $this, 'empty_pages' ], 10, 0 ); |
| 478 |
} |
| 479 |
// TODO ^^^ |
| 480 |
|
| 481 |
add_filter( 'elementor/import/kit/result', function( $result ) { |
| 482 |
if ( ! empty( $result['file_url'] ) ) { |
| 483 |
return [ |
| 484 |
'file_name' => $this->get_remote_kit_zip( $result['file_url'] ), |
| 485 |
'referrer' => static::REFERRER_KIT_LIBRARY, |
| 486 |
'file_url' => $result['file_url'], |
| 487 |
]; |
| 488 |
} |
| 489 |
|
| 490 |
return $result; |
| 491 |
} ); |
| 492 |
} |
| 493 |
|
| 494 |
/** |
| 495 |
* Prevent the creation of the default WooCommerce pages (Cart, Checkout, etc.) |
| 496 |
* |
| 497 |
* TODO 18/04/2023 : This needs to be moved to the runner itself after https://elementor.atlassian.net/browse/HTS-434 is done. |
| 498 |
* |
| 499 |
* @return array |
| 500 |
*/ |
| 501 |
public function empty_pages(): array { |
| 502 |
return []; |
| 503 |
} |
| 504 |
|
| 505 |
private function ensure_writing_permissions() { |
| 506 |
$server = new Server(); |
| 507 |
|
| 508 |
$paths_to_check = [ |
| 509 |
Server::KEY_PATH_WP_CONTENT_DIR => $server->get_system_path( Server::KEY_PATH_WP_CONTENT_DIR ), |
| 510 |
Server::KEY_PATH_UPLOADS_DIR => $server->get_system_path( Server::KEY_PATH_UPLOADS_DIR ), |
| 511 |
Server::KEY_PATH_ELEMENTOR_UPLOADS_DIR => $server->get_system_path( Server::KEY_PATH_ELEMENTOR_UPLOADS_DIR ), |
| 512 |
]; |
| 513 |
|
| 514 |
$permissions = $server->get_paths_permissions( $paths_to_check ); |
| 515 |
|
| 516 |
// WP Content dir has to be exists and writable. |
| 517 |
if ( ! $permissions[ Server::KEY_PATH_WP_CONTENT_DIR ]['write'] ) { |
| 518 |
throw new \Error( self::NO_WRITE_PERMISSIONS_KEY ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 519 |
} |
| 520 |
|
| 521 |
// WP Uploads dir has to be exists and writable. |
| 522 |
if ( ! $permissions[ Server::KEY_PATH_UPLOADS_DIR ]['write'] ) { |
| 523 |
throw new \Error( self::NO_WRITE_PERMISSIONS_KEY ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 524 |
} |
| 525 |
|
| 526 |
// Elementor uploads dir permissions is divided to 2 cases: |
| 527 |
// 1. If the dir exists, it has to be writable. |
| 528 |
// 2. If the dir doesn't exist, the parent dir has to be writable (wp uploads dir), so we can create it. |
| 529 |
if ( $permissions[ Server::KEY_PATH_ELEMENTOR_UPLOADS_DIR ]['exists'] && ! $permissions[ Server::KEY_PATH_ELEMENTOR_UPLOADS_DIR ]['write'] ) { |
| 530 |
throw new \Error( self::NO_WRITE_PERMISSIONS_KEY ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
private function ensure_DOMDocument_exists() { |
| 535 |
if ( ! class_exists( 'DOMDocument' ) ) { |
| 536 |
throw new \Error( self::DOMDOCUMENT_MISSING ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
/** |
| 541 |
* Enqueue admin scripts |
| 542 |
*/ |
| 543 |
public function enqueue_scripts() { |
| 544 |
wp_enqueue_script( |
| 545 |
'elementor-import-export-admin', |
| 546 |
$this->get_js_assets_url( 'import-export-admin' ), |
| 547 |
[ 'elementor-common' ], |
| 548 |
ELEMENTOR_VERSION, |
| 549 |
true |
| 550 |
); |
| 551 |
|
| 552 |
wp_localize_script( |
| 553 |
'elementor-import-export-admin', |
| 554 |
'elementorImportExport', |
| 555 |
[ |
| 556 |
'lastImportedSession' => $this->revert->get_last_import_session(), |
| 557 |
'appUrl' => Plugin::$instance->app->get_base_url() . '#/kit-library', |
| 558 |
] |
| 559 |
); |
| 560 |
} |
| 561 |
|
| 562 |
protected function get_remote_kit_zip( $url ) { |
| 563 |
$remote_zip_request = wp_safe_remote_get( $url ); |
| 564 |
|
| 565 |
if ( is_wp_error( $remote_zip_request ) ) { |
| 566 |
Plugin::$instance->logger->get_logger()->error( $remote_zip_request->get_error_message() ); |
| 567 |
throw new \Error( static::KIT_LIBRARY_ERROR_KEY ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 568 |
} |
| 569 |
|
| 570 |
if ( 200 !== $remote_zip_request['response']['code'] ) { |
| 571 |
Plugin::$instance->logger->get_logger()->error( $remote_zip_request['response']['message'] ); |
| 572 |
throw new \Error( static::KIT_LIBRARY_ERROR_KEY ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 573 |
} |
| 574 |
|
| 575 |
return Plugin::$instance->uploads_manager->create_temp_file( $remote_zip_request['body'], 'kit.zip' ); |
| 576 |
} |
| 577 |
|
| 578 |
/** |
| 579 |
* Get config data that will be exposed to the frontend. |
| 580 |
*/ |
| 581 |
private function get_config_data() { |
| 582 |
$export_nonce = wp_create_nonce( 'elementor_export' ); |
| 583 |
$export_url = add_query_arg( [ '_nonce' => $export_nonce ], Plugin::$instance->app->get_base_url() ); |
| 584 |
|
| 585 |
return [ |
| 586 |
'exportURL' => $export_url, |
| 587 |
'summaryTitles' => $this->get_summary_titles(), |
| 588 |
'builtinWpPostTypes' => ImportExportUtils::get_builtin_wp_post_types(), |
| 589 |
'elementorPostTypes' => ImportExportUtils::get_elementor_post_types(), |
| 590 |
'isUnfilteredFilesEnabled' => Uploads_Manager::are_unfiltered_uploads_enabled(), |
| 591 |
'elementorHomePageUrl' => $this->get_elementor_home_page_url(), |
| 592 |
'recentlyEditedElementorPageUrl' => $this->get_recently_edited_elementor_page_url(), |
| 593 |
'tools_url' => Tools::get_url(), |
| 594 |
'importSessions' => Revert::get_import_sessions(), |
| 595 |
'lastImportedSession' => $this->revert->get_last_import_session(), |
| 596 |
'kitPreviewNonce' => wp_create_nonce( 'kit_thumbnail' ), |
| 597 |
'restApiBaseUrl' => Controller::get_base_url(), |
| 598 |
'uiTheme' => $this->get_elementor_ui_theme_preference(), |
| 599 |
]; |
| 600 |
} |
| 601 |
|
| 602 |
private function get_elementor_ui_theme_preference() { |
| 603 |
$editor_preferences = SettingsManager::get_settings_managers( 'editorPreferences' ); |
| 604 |
|
| 605 |
return $editor_preferences->get_model()->get_settings( 'ui_theme' ); |
| 606 |
} |
| 607 |
|
| 608 |
/** |
| 609 |
* Get labels of Elementor document types, Elementor Post types, WordPress Post types and Custom Post types. |
| 610 |
*/ |
| 611 |
private function get_summary_titles() { |
| 612 |
$summary_titles = []; |
| 613 |
|
| 614 |
$document_types = Plugin::$instance->documents->get_document_types(); |
| 615 |
|
| 616 |
foreach ( $document_types as $name => $document_type ) { |
| 617 |
$summary_titles['templates'][ $name ] = [ |
| 618 |
'single' => $document_type::get_title(), |
| 619 |
'plural' => $document_type::get_plural_title(), |
| 620 |
]; |
| 621 |
} |
| 622 |
|
| 623 |
$elementor_post_types = ImportExportUtils::get_elementor_post_types(); |
| 624 |
$wp_builtin_post_types = ImportExportUtils::get_builtin_wp_post_types(); |
| 625 |
$post_types = array_merge( $elementor_post_types, $wp_builtin_post_types ); |
| 626 |
|
| 627 |
foreach ( $post_types as $post_type ) { |
| 628 |
$post_type_object = get_post_type_object( $post_type ); |
| 629 |
|
| 630 |
$summary_titles['content'][ $post_type ] = [ |
| 631 |
'single' => $post_type_object->labels->singular_name ?? '', |
| 632 |
'plural' => $post_type_object->label ?? '', |
| 633 |
]; |
| 634 |
} |
| 635 |
|
| 636 |
$custom_post_types = ImportExportUtils::get_registered_cpt_names(); |
| 637 |
if ( ! empty( $custom_post_types ) ) { |
| 638 |
foreach ( $custom_post_types as $custom_post_type ) { |
| 639 |
|
| 640 |
$custom_post_types_object = get_post_type_object( $custom_post_type ); |
| 641 |
// CPT data appears in two arrays: |
| 642 |
// 1. content object: in order to show the export summary when completed in getLabel function |
| 643 |
$summary_titles['content'][ $custom_post_type ] = [ |
| 644 |
'single' => $custom_post_types_object->labels->singular_name ?? '', |
| 645 |
'plural' => $custom_post_types_object->label ?? '', |
| 646 |
]; |
| 647 |
|
| 648 |
// 2. customPostTypes object: in order to actually export the data |
| 649 |
$summary_titles['content']['customPostTypes'][ $custom_post_type ] = [ |
| 650 |
'single' => $custom_post_types_object->labels->singular_name ?? '', |
| 651 |
'plural' => $custom_post_types_object->label ?? '', |
| 652 |
]; |
| 653 |
} |
| 654 |
} |
| 655 |
|
| 656 |
$active_kit = Plugin::$instance->kits_manager->get_active_kit(); |
| 657 |
|
| 658 |
foreach ( $active_kit->get_tabs() as $key => $tab ) { |
| 659 |
$summary_titles['site-settings'][ $key ] = $tab->get_title(); |
| 660 |
} |
| 661 |
|
| 662 |
return $summary_titles; |
| 663 |
} |
| 664 |
|
| 665 |
public function should_show_revert_section( $last_imported_kit ) { |
| 666 |
if ( empty( $last_imported_kit ) ) { |
| 667 |
return false; |
| 668 |
} |
| 669 |
|
| 670 |
// TODO: BC - remove in the future |
| 671 |
// The 'templates' runner was in core and moved to the Pro plugin. (Part of it still exits in the Core for BC) |
| 672 |
// The runner that is in the core version is missing the revert functionality, |
| 673 |
// therefore we shouldn't display the revert section if the import process done with the core version. |
| 674 |
$is_import_templates_ran = isset( $last_imported_kit['runners']['templates'] ); |
| 675 |
if ( $this->has_pro() && $is_import_templates_ran ) { |
| 676 |
$has_imported_templates = ! empty( $last_imported_kit['runners']['templates'] ); |
| 677 |
|
| 678 |
return $has_imported_templates; |
| 679 |
} |
| 680 |
|
| 681 |
return true; |
| 682 |
} |
| 683 |
|
| 684 |
public function has_pro(): bool { |
| 685 |
return ElementorUtils::has_pro(); |
| 686 |
} |
| 687 |
|
| 688 |
private function get_elementor_editor_home_page_url() { |
| 689 |
if ( 'page' !== get_option( 'show_on_front' ) ) { |
| 690 |
return ''; |
| 691 |
} |
| 692 |
|
| 693 |
$frontpage_id = get_option( 'page_on_front' ); |
| 694 |
|
| 695 |
return $this->get_elementor_editor_page_url( $frontpage_id ); |
| 696 |
} |
| 697 |
|
| 698 |
private function get_elementor_home_page_url() { |
| 699 |
if ( 'page' !== get_option( 'show_on_front' ) ) { |
| 700 |
return ''; |
| 701 |
} |
| 702 |
|
| 703 |
$frontpage_id = get_option( 'page_on_front' ); |
| 704 |
|
| 705 |
return $this->get_elementor_page_url( $frontpage_id ); |
| 706 |
} |
| 707 |
|
| 708 |
private function get_recently_edited_elementor_page_url() { |
| 709 |
$query = ElementorUtils::get_recently_edited_posts_query( [ 'posts_per_page' => 1 ] ); |
| 710 |
|
| 711 |
if ( ! isset( $query->post ) ) { |
| 712 |
return ''; |
| 713 |
} |
| 714 |
|
| 715 |
return $this->get_elementor_page_url( $query->post->ID ); |
| 716 |
} |
| 717 |
|
| 718 |
private function get_recently_edited_elementor_editor_page_url() { |
| 719 |
$query = ElementorUtils::get_recently_edited_posts_query( [ 'posts_per_page' => 1 ] ); |
| 720 |
|
| 721 |
if ( ! isset( $query->post ) ) { |
| 722 |
return ''; |
| 723 |
} |
| 724 |
|
| 725 |
return $this->get_elementor_editor_page_url( $query->post->ID ); |
| 726 |
} |
| 727 |
|
| 728 |
private function get_elementor_document( $page_id ) { |
| 729 |
$document = Plugin::$instance->documents->get( $page_id ); |
| 730 |
|
| 731 |
if ( ! $document || ! $document->is_built_with_elementor() ) { |
| 732 |
return false; |
| 733 |
} |
| 734 |
|
| 735 |
return $document; |
| 736 |
} |
| 737 |
|
| 738 |
private function get_elementor_page_url( $page_id ) { |
| 739 |
$document = $this->get_elementor_document( $page_id ); |
| 740 |
|
| 741 |
return $document ? $document->get_preview_url() : ''; |
| 742 |
} |
| 743 |
|
| 744 |
private function get_elementor_editor_page_url( $page_id ) { |
| 745 |
$document = $this->get_elementor_document( $page_id ); |
| 746 |
|
| 747 |
return $document ? $document->get_edit_url() : ''; |
| 748 |
} |
| 749 |
|
| 750 |
/** |
| 751 |
* @param string $class |
| 752 |
* |
| 753 |
* @return bool |
| 754 |
*/ |
| 755 |
public function is_third_party_class( $class ) { |
| 756 |
$allowed_classes = [ |
| 757 |
'Elementor\\', |
| 758 |
'ElementorPro\\', |
| 759 |
'WP_', |
| 760 |
'wp_', |
| 761 |
]; |
| 762 |
|
| 763 |
foreach ( $allowed_classes as $allowed_class ) { |
| 764 |
if ( str_starts_with( $class, $allowed_class ) ) { |
| 765 |
return false; |
| 766 |
} |
| 767 |
} |
| 768 |
|
| 769 |
return true; |
| 770 |
} |
| 771 |
} |
| 772 |
|