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

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