PluginProbe
Elementor Website Builder – more than just a page builder / 3.33.0-dev1
Elementor Website Builder – more than just a page builder v3.33.0-dev1
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / app / modules / import-export-customization / module.php

module.php in Elementor Website Builder – more than just a page builder 3.33.0-dev1, at app/modules/import-export-customization/module.php

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