PluginProbe
Elementor Website Builder – more than just a page builder / 3.13.3
Elementor Website Builder – more than just a page builder v3.13.3
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.3, at app/modules/import-export/module.php

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