PluginProbe
Elementor Website Builder – more than just a page builder / 3.13.2
Elementor Website Builder – more than just a page builder v3.13.2
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 / module.php

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

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