← All changes
|
app/modules/import-export/module.php
+155
-134
4.3.0-beta3
→
3.30.0-beta1
View file →
| @@ -6,14 +6,14 @@ | ||
| 6 | 6 | use Elementor\App\Modules\ImportExport\Processes\Revert; |
| 7 | 7 | use Elementor\Core\Base\Module as BaseModule; |
| 8 | 8 | use Elementor\Core\Common\Modules\Ajax\Module as Ajax; |
| 9 | 9 | use Elementor\Core\Files\Uploads_Manager; |
| 10 | +use Elementor\App\Modules\KitLibrary\Module as KitLibrary; | |
| 10 | 11 | use Elementor\Modules\System_Info\Reporters\Server; |
| 11 | 12 | use Elementor\Plugin; |
| 12 | 13 | use Elementor\Tools; |
| 13 | 14 | use Elementor\Utils as ElementorUtils; |
| 14 | 15 | use Elementor\App\Modules\ImportExport\Utils as ImportExportUtils; |
| 15 | -use Elementor\Modules\CloudKitLibrary\Module as CloudKitLibrary; | |
| 16 | 16 | |
| 17 | 17 | if ( ! defined( 'ABSPATH' ) ) { |
| 18 | 18 | exit; // Exit if accessed directly. |
| 19 | 19 | } |
| @@ -117,12 +117,12 @@ | ||
| 117 | 117 | * Register the import/export tab in elementor tools. |
| 118 | 118 | */ |
| 119 | 119 | public function register_settings_tab( Tools $tools ) { |
| 120 | 120 | $tools->add_tab( 'import-export-kit', [ |
| 121 | - 'label' => esc_html__( 'Website Templates', 'elementor' ), | |
| 121 | + 'label' => esc_html__( 'Import / Export Kit', 'elementor' ), | |
| 122 | 122 | 'sections' => [ |
| 123 | 123 | 'intro' => [ |
| 124 | - 'label' => esc_html__( 'Website Templates', 'elementor' ), | |
| 124 | + 'label' => esc_html__( 'Template Kits', 'elementor' ), | |
| 125 | 125 | 'callback' => function() { |
| 126 | 126 | $this->render_import_export_tab_content(); |
| 127 | 127 | }, |
| 128 | 128 | 'fields' => [], |
| @@ -134,28 +134,43 @@ | ||
| 134 | 134 | /** |
| 135 | 135 | * Render the import/export tab content. |
| 136 | 136 | */ |
| 137 | 137 | private function render_import_export_tab_content() { |
| 138 | - $is_cloud_kits_available = CloudKitLibrary::get_app()->check_eligibility()['is_eligible']; | |
| 138 | + $intro_text_link = sprintf( '<a href="https://go.elementor.com/wp-dash-import-export-general/" target="_blank">%s</a>', esc_html__( 'Learn more', 'elementor' ) ); | |
| 139 | 139 | |
| 140 | + $intro_text = sprintf( | |
| 141 | + /* translators: 1: New line break, 2: Learn more link. */ | |
| 142 | + __( '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' ), | |
| 143 | + '<br>', | |
| 144 | + $intro_text_link | |
| 145 | + ); | |
| 146 | + | |
| 147 | + $is_cloud_kits_available = Plugin::$instance->experiments->is_feature_active( 'cloud-library' ) && KitLibrary::get_cloud_app()->is_eligible(); | |
| 148 | + | |
| 140 | 149 | $content_data = [ |
| 141 | 150 | 'export' => [ |
| 142 | - 'title' => esc_html__( 'Export this website', 'elementor' ), | |
| 151 | + 'title' => esc_html__( 'Export a Template Kit', 'elementor' ), | |
| 143 | 152 | 'button' => [ |
| 144 | 153 | 'url' => Plugin::$instance->app->get_base_url() . '#/export', |
| 145 | 154 | 'text' => esc_html__( 'Export', 'elementor' ), |
| 146 | - 'id' => 'elementor-import-export__export', | |
| 147 | 155 | ], |
| 148 | - 'description' => esc_html__( 'You can download this website as a .zip file, or upload it to the library.', 'elementor' ), | |
| 156 | + 'description' => esc_html__( 'Bundle your whole site - or just some of its elements - to be used for another website.', 'elementor' ), | |
| 157 | + 'link' => [ | |
| 158 | + 'url' => 'https://go.elementor.com/wp-dash-import-export-export-flow/', | |
| 159 | + 'text' => esc_html__( 'Learn More', 'elementor' ), | |
| 160 | + ], | |
| 149 | 161 | ], |
| 150 | 162 | 'import' => [ |
| 151 | - 'title' => esc_html__( 'Import website templates', 'elementor' ), | |
| 163 | + 'title' => esc_html__( 'Import a Template Kit', 'elementor' ), | |
| 152 | 164 | 'button' => [ |
| 153 | 165 | 'url' => Plugin::$instance->app->get_base_url() . '#/import', |
| 154 | - 'text' => esc_html__( 'Import', 'elementor' ), | |
| 155 | - 'id' => 'elementor-import-export__import', | |
| 166 | + 'text' => $is_cloud_kits_available ? esc_html__( 'Upload .zip file', 'elementor' ) : esc_html__( 'Import', 'elementor' ), | |
| 156 | 167 | ], |
| 157 | - 'description' => esc_html__( 'You can import design and settings from a .zip file or choose from the library.', 'elementor' ), | |
| 168 | + 'description' => esc_html__( 'Apply the design and settings of another site to this one.', 'elementor' ), | |
| 169 | + 'link' => [ | |
| 170 | + 'url' => 'https://go.elementor.com/wp-dash-import-export-import-flow/', | |
| 171 | + 'text' => esc_html__( 'Learn More', 'elementor' ), | |
| 172 | + ], | |
| 158 | 173 | ], |
| 159 | 174 | ]; |
| 160 | 175 | |
| 161 | 176 | if ( $is_cloud_kits_available ) { |
| @@ -160,10 +175,9 @@ | ||
| 160 | 175 | |
| 161 | 176 | if ( $is_cloud_kits_available ) { |
| 162 | 177 | $content_data['import']['button_secondary'] = [ |
| 163 | 178 | 'url' => Plugin::$instance->app->get_base_url() . '#/kit-library/cloud', |
| 164 | - 'text' => esc_html__( 'Import from library', 'elementor' ), | |
| 165 | - 'id' => 'elementor-import-export__import_from_library', | |
| 179 | + 'text' => esc_html__( 'Choose from Cloud Library', 'elementor' ), | |
| 166 | 180 | ]; |
| 167 | 181 | } |
| 168 | 182 | |
| 169 | 183 | $last_imported_kit = $this->revert->get_last_import_session(); |
| @@ -177,9 +191,8 @@ | ||
| 177 | 191 | |
| 178 | 192 | if ( $should_show_revert_section ) { |
| 179 | 193 | if ( ! empty( $penultimate_imported_kit ) ) { |
| 180 | 194 | $revert_text = sprintf( |
| 181 | - /* translators: 1: Last imported kit title, 2: Last imported kit date, 3: Line break <br>, 4: Penultimate imported kit title, 5: Penultimate imported kit date. */ | |
| 182 | 195 | 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' ), |
| 183 | 196 | ! empty( $last_imported_kit['kit_title'] ) ? $last_imported_kit['kit_title'] : esc_html__( 'imported kit', 'elementor' ), |
| 184 | 197 | gmdate( $date_format, $last_imported_kit['start_timestamp'] ), |
| 185 | 198 | '<br>', |
| @@ -187,9 +200,8 @@ | ||
| 187 | 200 | gmdate( $date_format, $penultimate_imported_kit['start_timestamp'] ) |
| 188 | 201 | ); |
| 189 | 202 | } else { |
| 190 | 203 | $revert_text = sprintf( |
| 191 | - /* translators: 1: Last imported kit title, 2: Last imported kit date, 3: Line break <br>. */ | |
| 192 | 204 | 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' ), |
| 193 | 205 | ! empty( $last_imported_kit['kit_title'] ) ? $last_imported_kit['kit_title'] : esc_html__( 'imported kit', 'elementor' ), |
| 194 | 206 | gmdate( $date_format, $last_imported_kit['start_timestamp'] ), |
| 195 | 207 | '<br>' |
| @@ -198,17 +210,9 @@ | ||
| 198 | 210 | } |
| 199 | 211 | ?> |
| 200 | 212 | |
| 201 | 213 | <div class="tab-import-export-kit__content"> |
| 202 | - <p class="tab-import-export-kit__info"> | |
| 203 | - <?php | |
| 204 | - printf( | |
| 205 | - '%1$s <a href="https://go.elementor.com/wp-dash-import-export-general/" target="_blank">%2$s</a>', | |
| 206 | - 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' ), | |
| 207 | - esc_html__( 'Learn more', 'elementor' ), | |
| 208 | - ); | |
| 209 | - ?> | |
| 210 | - </p> | |
| 214 | + <p class="tab-import-export-kit__info"><?php ElementorUtils::print_unescaped_internal_string( $intro_text ); ?></p> | |
| 211 | 215 | |
| 212 | 216 | <div class="tab-import-export-kit__wrapper"> |
| 213 | 217 | <?php foreach ( $content_data as $data ) { |
| 214 | 218 | $this->print_item_content( $data ); |
| @@ -225,9 +229,9 @@ | ||
| 225 | 229 | ]; |
| 226 | 230 | ?> |
| 227 | 231 | <div class="tab-import-export-kit__revert"> |
| 228 | 232 | <h2> |
| 229 | - <?php echo esc_html__( 'Remove the most recent Website Template', 'elementor' ); ?> | |
| 233 | + <?php ElementorUtils::print_unescaped_internal_string( esc_html__( 'Remove the most recent Kit', 'elementor' ) ); ?> | |
| 230 | 234 | </h2> |
| 231 | 235 | <p class="tab-import-export-kit__info"> |
| 232 | 236 | <?php ElementorUtils::print_unescaped_internal_string( $revert_text ); ?> |
| 233 | 237 | </p> |
| @@ -232,9 +236,9 @@ | ||
| 232 | 236 | <?php ElementorUtils::print_unescaped_internal_string( $revert_text ); ?> |
| 233 | 237 | </p> |
| 234 | 238 | <?php $this->render_last_kit_thumbnail( $last_imported_kit ); ?> |
| 235 | 239 | <a <?php ElementorUtils::print_html_attributes( $link_attributes ); ?> > |
| 236 | - <?php echo esc_html__( 'Remove Website Template', 'elementor' ); ?> | |
| 240 | + <?php ElementorUtils::print_unescaped_internal_string( esc_html__( 'Remove Kit', 'elementor' ) ); ?> | |
| 237 | 241 | </a> |
| 238 | 242 | </div> |
| 239 | 243 | <?php } ?> |
| 240 | 244 | </div> |
| @@ -241,30 +245,40 @@ | ||
| 241 | 245 | <?php |
| 242 | 246 | } |
| 243 | 247 | |
| 244 | 248 | private function print_item_content( $data ) { |
| 245 | - ?> | |
| 246 | - <div class="tab-import-export-kit__container"> | |
| 247 | - <div class="tab-import-export-kit__box"> | |
| 248 | - <h2><?php ElementorUtils::print_unescaped_internal_string( $data['title'] ); ?></h2> | |
| 249 | - </div> | |
| 250 | - <p class="description"><?php ElementorUtils::print_unescaped_internal_string( $data['description'] ); ?></p> | |
| 249 | + $is_cloud_kits_feature_active = Plugin::$instance->experiments->is_feature_active( 'cloud-library' ); | |
| 251 | 250 | |
| 252 | - <?php if ( ! empty( $data['link'] ) ) : ?> | |
| 251 | + if ( $is_cloud_kits_feature_active ) { ?> | |
| 252 | + <div class="tab-import-export-kit__container"> | |
| 253 | + <div class="tab-import-export-kit__box"> | |
| 254 | + <h2><?php ElementorUtils::print_unescaped_internal_string( $data['title'] ); ?></h2> | |
| 255 | + </div> | |
| 256 | + <p class="description"><?php ElementorUtils::print_unescaped_internal_string( $data['description'] ); ?></p> | |
| 253 | 257 | <a href="<?php ElementorUtils::print_unescaped_internal_string( $data['link']['url'] ); ?>" target="_blank"><?php ElementorUtils::print_unescaped_internal_string( $data['link']['text'] ); ?></a> |
| 254 | - <?php endif; ?> | |
| 255 | - <div class="tab-import-export-kit__box action-buttons"> | |
| 256 | - <?php if ( ! empty( $data['button_secondary'] ) ) : ?> | |
| 257 | - <a href="<?php ElementorUtils::print_unescaped_internal_string( $data['button_secondary']['url'] ); ?>" class="elementor-button e-btn-txt e-btn-txt-border"> | |
| 258 | - <?php ElementorUtils::print_unescaped_internal_string( $data['button_secondary']['text'] ); ?> | |
| 258 | + <div class="tab-import-export-kit__box action-buttons"> | |
| 259 | + <?php if ( ! empty( $data['button_secondary'] ) ) : ?> | |
| 260 | + <a href="<?php ElementorUtils::print_unescaped_internal_string( $data['button_secondary']['url'] ); ?>" class="elementor-button e-btn-txt e-btn-txt-border"> | |
| 261 | + <?php ElementorUtils::print_unescaped_internal_string( $data['button_secondary']['text'] ); ?> | |
| 262 | + </a> | |
| 263 | + <?php endif; ?> | |
| 264 | + <a href="<?php ElementorUtils::print_unescaped_internal_string( $data['button']['url'] ); ?>" class="elementor-button e-primary"> | |
| 265 | + <?php ElementorUtils::print_unescaped_internal_string( $data['button']['text'] ); ?> | |
| 259 | 266 | </a> |
| 260 | - <?php endif; ?> | |
| 261 | - <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"> | |
| 262 | - <?php ElementorUtils::print_unescaped_internal_string( $data['button']['text'] ); ?> | |
| 263 | - </a> | |
| 267 | + </div> | |
| 264 | 268 | </div> |
| 265 | - </div> | |
| 266 | - <?php | |
| 269 | + <?php } else { ?> | |
| 270 | + <div class="tab-import-export-kit__container"> | |
| 271 | + <div class="tab-import-export-kit__box"> | |
| 272 | + <h2><?php ElementorUtils::print_unescaped_internal_string( $data['title'] ); ?></h2> | |
| 273 | + <a href="<?php ElementorUtils::print_unescaped_internal_string( $data['button']['url'] ); ?>" class="elementor-button e-primary"> | |
| 274 | + <?php ElementorUtils::print_unescaped_internal_string( $data['button']['text'] ); ?> | |
| 275 | + </a> | |
| 276 | + </div> | |
| 277 | + <p><?php ElementorUtils::print_unescaped_internal_string( $data['description'] ); ?></p> | |
| 278 | + <a href="<?php ElementorUtils::print_unescaped_internal_string( $data['link']['url'] ); ?>" target="_blank"><?php ElementorUtils::print_unescaped_internal_string( $data['link']['text'] ); ?></a> | |
| 279 | + </div> | |
| 280 | + <?php } | |
| 267 | 281 | } |
| 268 | 282 | |
| 269 | 283 | private function get_revert_href(): string { |
| 270 | 284 | $admin_post_url = admin_url( 'admin-post.php?action=elementor_revert_kit' ); |
| @@ -326,19 +340,15 @@ | ||
| 326 | 340 | * so it will be available to use in different places such as: WP_Cli, Pro, etc. |
| 327 | 341 | * |
| 328 | 342 | * @param string $file Path to the file. |
| 329 | 343 | * @param string $referrer Referrer of the file 'local' or 'kit-library'. |
| 330 | - * @param string $kit_id | |
| 331 | 344 | * @return array |
| 332 | - * @throws \Exception If export validation fails or processing errors occur. | |
| 345 | + * @throws \Exception | |
| 333 | 346 | */ |
| 334 | - public function upload_kit( $file, $referrer, $kit_id = null ) { | |
| 347 | + public function upload_kit( $file, $referrer ) { | |
| 335 | 348 | $this->ensure_writing_permissions(); |
| 336 | 349 | |
| 337 | - $this->import = new Import( $file, [ | |
| 338 | - 'referrer' => $referrer, | |
| 339 | - 'id' => $kit_id, | |
| 340 | - ] ); | |
| 350 | + $this->import = new Import( $file, [ 'referrer' => $referrer ] ); | |
| 341 | 351 | |
| 342 | 352 | return [ |
| 343 | 353 | 'session' => $this->import->get_session_id(), |
| 344 | 354 | 'manifest' => $this->import->get_manifest(), |
| @@ -356,13 +366,13 @@ | ||
| 356 | 366 | * Assigning the Import process to the 'import' property, |
| 357 | 367 | * so it will be available to use in different places such as: WP_Cli, Pro, etc. |
| 358 | 368 | * |
| 359 | 369 | * @param string $path Path to the file or session_id. |
| 360 | - * @param array $settings Settings the import use to determine which content to import. | |
| 361 | - * (e.g: include, selected_plugins, selected_cpt, selected_override_conditions, etc.) | |
| 362 | - * @param bool $split_to_chunks Determine if the import process should be split into chunks. | |
| 370 | + * @param array $settings Settings the import use to determine which content to import. | |
| 371 | + * (e.g: include, selected_plugins, selected_cpt, selected_override_conditions, etc.) | |
| 372 | + * @param bool $split_to_chunks Determine if the import process should be split into chunks. | |
| 363 | 373 | * @return array |
| 364 | - * @throws \Exception If export configuration is invalid or processing fails. | |
| 374 | + * @throws \Exception | |
| 365 | 375 | */ |
| 366 | 376 | public function import_kit( string $path, array $settings, bool $split_to_chunks = false ): array { |
| 367 | 377 | $this->ensure_writing_permissions(); |
| 368 | 378 | $this->ensure_DOMDocument_exists(); |
| @@ -393,9 +403,9 @@ | ||
| 393 | 403 | * |
| 394 | 404 | * @return array Two types of response. |
| 395 | 405 | * 1. The status and the runner name. |
| 396 | 406 | * 2. The imported data. (Only if the runner is the last one in the import process) |
| 397 | - * @throws \Exception If import configuration is invalid or processing fails. | |
| 407 | + * @throws \Exception | |
| 398 | 408 | */ |
| 399 | 409 | public function import_kit_by_runner( string $session_id, string $runner_name ): array { |
| 400 | 410 | // Check session_id |
| 401 | 411 | $this->import = Import::from_session( $session_id ); |
| @@ -418,9 +428,9 @@ | ||
| 418 | 428 | * |
| 419 | 429 | * @param array $settings Settings the export use to determine which content to export. |
| 420 | 430 | * (e.g: include, kit_info, selected_plugins, selected_cpt, etc.) |
| 421 | 431 | * @return array |
| 422 | - * @throws \Exception If import/export process fails or validation errors occur. | |
| 432 | + * @throws \Exception | |
| 423 | 433 | */ |
| 424 | 434 | public function export_kit( array $settings ) { |
| 425 | 435 | $this->ensure_writing_permissions(); |
| 426 | 436 | |
| @@ -474,13 +484,11 @@ | ||
| 474 | 484 | add_action( 'admin_post_elementor_revert_kit', [ $this, 'handle_revert_last_imported_kit' ] ); |
| 475 | 485 | |
| 476 | 486 | add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] ); |
| 477 | 487 | |
| 478 | - if ( ! Plugin::$instance->experiments->is_feature_active( 'import-export-customization' ) ) { | |
| 479 | - $page_id = Tools::PAGE_ID; | |
| 488 | + $page_id = Tools::PAGE_ID; | |
| 480 | 489 | |
| 481 | - add_action( "elementor/admin/after_create_settings/{$page_id}", [ $this, 'register_settings_tab' ] ); | |
| 482 | - } | |
| 490 | + add_action( "elementor/admin/after_create_settings/{$page_id}", [ $this, 'register_settings_tab' ] ); | |
| 483 | 491 | |
| 484 | 492 | // TODO 18/04/2023 : This needs to be moved to the runner itself after https://elementor.atlassian.net/browse/HTS-434 is done. |
| 485 | 493 | if ( self::IMPORT_PLUGINS_ACTION === ElementorUtils::get_super_global_value( $_SERVER, 'HTTP_X_ELEMENTOR_ACTION' ) ) { |
| 486 | 494 | add_filter( 'woocommerce_create_pages', [ $this, 'empty_pages' ], 10, 0 ); |
| @@ -485,20 +493,8 @@ | ||
| 485 | 493 | if ( self::IMPORT_PLUGINS_ACTION === ElementorUtils::get_super_global_value( $_SERVER, 'HTTP_X_ELEMENTOR_ACTION' ) ) { |
| 486 | 494 | add_filter( 'woocommerce_create_pages', [ $this, 'empty_pages' ], 10, 0 ); |
| 487 | 495 | } |
| 488 | 496 | // TODO ^^^ |
| 489 | - | |
| 490 | - add_filter( 'elementor/import/kit/result', function( $result ) { | |
| 491 | - if ( ! empty( $result['file_url'] ) ) { | |
| 492 | - return [ | |
| 493 | - 'file_name' => $this->get_remote_kit_zip( $result['file_url'] ), | |
| 494 | - 'referrer' => static::REFERRER_KIT_LIBRARY, | |
| 495 | - 'file_url' => $result['file_url'], | |
| 496 | - ]; | |
| 497 | - } | |
| 498 | - | |
| 499 | - return $result; | |
| 500 | - } ); | |
| 501 | 497 | } |
| 502 | 498 | |
| 503 | 499 | /** |
| 504 | 500 | * Prevent the creation of the default WooCommerce pages (Cart, Checkout, etc.) |
| @@ -616,67 +612,43 @@ | ||
| 616 | 612 | } |
| 617 | 613 | |
| 618 | 614 | /** |
| 619 | 615 | * Handle upload kit ajax request. |
| 620 | - * | |
| 621 | - * @throws \Error If operation validation fails or processing errors occur. | |
| 622 | 616 | */ |
| 623 | 617 | private function handle_upload_kit() { |
| 624 | 618 | // PHPCS - A URL that should contain special chars (auth headers information). |
| 625 | - $file_url = isset( $_POST['e_import_file'] ) | |
| 626 | - // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 627 | - ? wp_unslash( $_POST['e_import_file'] ) | |
| 619 | + $file_url = isset( $_POST['e_import_file'] ) // phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| 620 | + ? wp_unslash( $_POST['e_import_file'] ) // phpcs:ignore | |
| 628 | 621 | : ''; |
| 629 | - | |
| 630 | - // PHPCS - Already validated in caller function | |
| 631 | 622 | $kit_id = ElementorUtils::get_super_global_value( $_POST, 'kit_id' ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 632 | - $source = ElementorUtils::get_super_global_value( $_POST, 'source' ); // phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| 633 | 623 | |
| 634 | 624 | $is_import_from_library = ! empty( $file_url ); |
| 625 | + $is_import_from_cloud = isset( $_POST['source'] ) && self::REFERRER_CLOUD === ElementorUtils::get_super_global_value( $_POST, 'source' ); // phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| 635 | 626 | |
| 636 | - if ( $is_import_from_library ) { | |
| 637 | - if ( | |
| 638 | - ! wp_verify_nonce( ElementorUtils::get_super_global_value( $_POST, 'e_kit_library_nonce' ), 'kit-library-import' ) | |
| 639 | - ) { | |
| 640 | - throw new \Error( 'Invalid kit library nonce.' ); | |
| 641 | - } | |
| 642 | - | |
| 643 | - if ( ! filter_var( $file_url, FILTER_VALIDATE_URL ) || 0 !== strpos( $file_url, 'http' ) ) { | |
| 644 | - throw new \Error( static::KIT_LIBRARY_ERROR_KEY ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped | |
| 645 | - } | |
| 646 | - | |
| 647 | - $import_result = apply_filters( 'elementor/import/kit/result', [ 'file_url' => $file_url ] ); | |
| 648 | - } elseif ( ! empty( $source ) ) { | |
| 649 | - $import_result = apply_filters( 'elementor/import/kit/result/' . $source, [ | |
| 650 | - 'kit_id' => $kit_id, | |
| 651 | - 'source' => $source, | |
| 652 | - ] ); | |
| 627 | + if ( $is_import_from_cloud ) { | |
| 628 | + $result = $this->handle_import_kit_from_cloud( $kit_id ); | |
| 629 | + $file_url = $result['file_url']; | |
| 630 | + } elseif ( $is_import_from_library ) { | |
| 631 | + $result = $this->handle_import_kit_from_library( $file_url ); | |
| 653 | 632 | } else { |
| 654 | - $import_result = [ | |
| 655 | - 'file_name' => ElementorUtils::get_super_global_value( $_FILES, 'e_import_file' )['tmp_name'], | |
| 656 | - 'referrer' => static::REFERRER_LOCAL, | |
| 657 | - ]; | |
| 633 | + $result = $this->handle_import_kit_from_upload(); | |
| 658 | 634 | } |
| 659 | 635 | |
| 660 | 636 | Plugin::$instance->logger->get_logger()->info( 'Uploading Kit: ', [ |
| 661 | 637 | 'meta' => [ |
| 662 | - 'kit_id' => $kit_id, | |
| 663 | - 'referrer' => $import_result['referrer'], | |
| 638 | + 'kit_id' => ElementorUtils::get_super_global_value( $_POST, 'kit_id' ), // phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| 639 | + 'referrer' => $result['referrer'], | |
| 664 | 640 | ], |
| 665 | 641 | ] ); |
| 666 | 642 | |
| 667 | - if ( is_wp_error( $import_result ) ) { | |
| 668 | - wp_send_json_error( $import_result->get_error_message() ); | |
| 669 | - } | |
| 643 | + $uploaded_kit = $this->upload_kit( $result['file_name'], $result['referrer'] ); | |
| 670 | 644 | |
| 671 | - $uploaded_kit = $this->upload_kit( $import_result['file_name'], $import_result['referrer'], $kit_id ); | |
| 672 | - | |
| 673 | 645 | $session_dir = $uploaded_kit['session']; |
| 674 | 646 | $manifest = $uploaded_kit['manifest']; |
| 675 | 647 | $conflicts = $uploaded_kit['conflicts']; |
| 676 | 648 | |
| 677 | - if ( $is_import_from_library || ! empty( $source ) ) { | |
| 678 | - Plugin::$instance->uploads_manager->remove_file_or_dir( dirname( $import_result['file_name'] ) ); | |
| 649 | + if ( $is_import_from_cloud || $is_import_from_library ) { | |
| 650 | + Plugin::$instance->uploads_manager->remove_file_or_dir( dirname( $result['file_name'] ) ); | |
| 679 | 651 | } |
| 680 | 652 | |
| 681 | 653 | if ( isset( $manifest['plugins'] ) && ! current_user_can( 'install_plugins' ) ) { |
| 682 | 654 | throw new \Error( static::PLUGIN_PERMISSIONS_ERROR_KEY ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| @@ -684,13 +656,12 @@ | ||
| 684 | 656 | |
| 685 | 657 | $result = [ |
| 686 | 658 | 'session' => $session_dir, |
| 687 | 659 | 'manifest' => $manifest, |
| 688 | - 'file_url' => $import_result['file_url'], | |
| 689 | 660 | ]; |
| 690 | 661 | |
| 691 | - if ( ! empty( $import_result['kit'] ) ) { | |
| 692 | - $result['uploaded_kit'] = $import_result['kit']; | |
| 662 | + if ( isset( $file_url ) ) { | |
| 663 | + $result['file_url'] = $file_url; | |
| 693 | 664 | } |
| 694 | 665 | |
| 695 | 666 | if ( ! empty( $conflicts ) ) { |
| 696 | 667 | $result['conflicts'] = $conflicts; |
| @@ -702,8 +673,52 @@ | ||
| 702 | 673 | |
| 703 | 674 | wp_send_json_success( $result ); |
| 704 | 675 | } |
| 705 | 676 | |
| 677 | + protected function handle_import_kit_from_library( $file_url ) { | |
| 678 | + if ( | |
| 679 | + ! wp_verify_nonce( ElementorUtils::get_super_global_value( $_POST, 'e_kit_library_nonce' ), 'kit-library-import' ) | |
| 680 | + ) { | |
| 681 | + throw new \Error( 'Invalid kit library nonce.' ); | |
| 682 | + } | |
| 683 | + | |
| 684 | + if ( ! filter_var( $file_url, FILTER_VALIDATE_URL ) || 0 !== strpos( $file_url, 'http' ) ) { | |
| 685 | + throw new \Error( static::KIT_LIBRARY_ERROR_KEY ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped | |
| 686 | + } | |
| 687 | + | |
| 688 | + return [ | |
| 689 | + 'file_name' => $this->get_remote_kit_zip( $file_url ), | |
| 690 | + 'referrer' => static::REFERRER_KIT_LIBRARY, | |
| 691 | + ]; | |
| 692 | + } | |
| 693 | + | |
| 694 | + protected function handle_import_kit_from_upload() { | |
| 695 | + return [ | |
| 696 | + // PHPCS - Already validated in caller function. | |
| 697 | + 'file_name' => ElementorUtils::get_super_global_value( $_FILES, 'e_import_file' )['tmp_name'], // phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| 698 | + 'referrer' => static::REFERRER_LOCAL, | |
| 699 | + ]; | |
| 700 | + } | |
| 701 | + | |
| 702 | + protected function handle_import_kit_from_cloud( $kit_id ) { | |
| 703 | + $kit = KitLibrary::get_cloud_app()->get_kit( [ | |
| 704 | + 'id' => $kit_id, | |
| 705 | + ] ); | |
| 706 | + | |
| 707 | + if ( is_wp_error( $kit ) ) { | |
| 708 | + wp_send_json_error( $kit->get_error_message() ); | |
| 709 | + } | |
| 710 | + | |
| 711 | + if ( empty( $kit['downloadUrl'] ) ) { | |
| 712 | + throw new \Error( static::KIT_LIBRARY_ERROR_KEY ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped | |
| 713 | + } | |
| 714 | + | |
| 715 | + return [ | |
| 716 | + 'file_name' => $this->get_remote_kit_zip( $kit['downloadUrl'] ), | |
| 717 | + 'referrer' => static::REFERRER_CLOUD, | |
| 718 | + 'file_url' => $kit['downloadUrl'], | |
| 719 | + ]; | |
| 720 | + } | |
| 706 | 721 | protected function get_remote_kit_zip( $url ) { |
| 707 | 722 | $remote_zip_request = wp_safe_remote_get( $url ); |
| 708 | 723 | |
| 709 | 724 | if ( is_wp_error( $remote_zip_request ) ) { |
| @@ -770,10 +785,8 @@ | ||
| 770 | 785 | } |
| 771 | 786 | |
| 772 | 787 | /** |
| 773 | 788 | * Handle export kit ajax request. |
| 774 | - * | |
| 775 | - * @throws \Error If cleanup process fails or file system errors occur. | |
| 776 | 789 | */ |
| 777 | 790 | private function handle_export_kit() { |
| 778 | 791 | // PHPCS - Already validated in caller function |
| 779 | 792 | $settings = json_decode( ElementorUtils::get_super_global_value( $_POST, 'data' ), true ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| @@ -781,9 +794,8 @@ | ||
| 781 | 794 | |
| 782 | 795 | $export = $this->export_kit( $settings ); |
| 783 | 796 | |
| 784 | 797 | $file_name = $export['file_name']; |
| 785 | - $file_size = filesize( $file_name ); | |
| 786 | 798 | $file = ElementorUtils::file_get_contents( $file_name ); |
| 787 | 799 | |
| 788 | 800 | if ( ! $file ) { |
| 789 | 801 | throw new \Error( 'Could not read the exported file.' ); |
| @@ -790,23 +802,32 @@ | ||
| 790 | 802 | } |
| 791 | 803 | |
| 792 | 804 | Plugin::$instance->uploads_manager->remove_file_or_dir( dirname( $file_name ) ); |
| 793 | 805 | |
| 794 | - $result = apply_filters( | |
| 795 | - 'elementor/export/kit/export-result', | |
| 796 | - [ | |
| 797 | - 'manifest' => $export['manifest'], | |
| 798 | - 'file' => base64_encode( $file ), | |
| 799 | - ], | |
| 800 | - $source, | |
| 801 | - $export, | |
| 802 | - $settings, | |
| 803 | - $file, | |
| 804 | - $file_size, | |
| 805 | - ); | |
| 806 | + $result = [ | |
| 807 | + 'manifest' => $export['manifest'], | |
| 808 | + ]; | |
| 806 | 809 | |
| 807 | - if ( is_wp_error( $result ) ) { | |
| 808 | - wp_send_json_error( $result ); | |
| 810 | + if ( self::EXPORT_SOURCE_CLOUD === $source ) { | |
| 811 | + $raw_screen_shot = base64_decode( substr( $settings['screenShotBlob'], strlen( 'data:image/png;base64,' ) ) ); | |
| 812 | + $title = $export['manifest']['title']; | |
| 813 | + $description = $export['manifest']['description']; | |
| 814 | + | |
| 815 | + $kit = KitLibrary::get_cloud_app()->create_kit( | |
| 816 | + $title, | |
| 817 | + $description, | |
| 818 | + $file, | |
| 819 | + $raw_screen_shot, | |
| 820 | + $settings['include'], | |
| 821 | + ); | |
| 822 | + | |
| 823 | + if ( is_wp_error( $kit ) ) { | |
| 824 | + wp_send_json_error( $kit ); | |
| 825 | + } | |
| 826 | + | |
| 827 | + $result['kit'] = $kit; | |
| 828 | + } else { | |
| 829 | + $result['file'] = base64_encode( $file ); | |
| 809 | 830 | } |
| 810 | 831 | |
| 811 | 832 | wp_send_json_success( $result ); |
| 812 | 833 | } |
| @@ -975,13 +996,13 @@ | ||
| 975 | 996 | return $document ? $document->get_edit_url() : ''; |
| 976 | 997 | } |
| 977 | 998 | |
| 978 | 999 | /** |
| 979 | - * @param string $class_name | |
| 1000 | + * @param string $class | |
| 980 | 1001 | * |
| 981 | 1002 | * @return bool |
| 982 | 1003 | */ |
| 983 | - public function is_third_party_class( $class_name ) { | |
| 1004 | + public function is_third_party_class( $class ) { | |
| 984 | 1005 | $allowed_classes = [ |
| 985 | 1006 | 'Elementor\\', |
| 986 | 1007 | 'ElementorPro\\', |
| 987 | 1008 | 'WP_', |
| @@ -988,9 +1009,9 @@ | ||
| 988 | 1009 | 'wp_', |
| 989 | 1010 | ]; |
| 990 | 1011 | |
| 991 | 1012 | foreach ( $allowed_classes as $allowed_class ) { |
| 992 | - if ( str_starts_with( $class_name, $allowed_class ) ) { | |
| 1013 | + if ( str_starts_with( $class, $allowed_class ) ) { | |
| 993 | 1014 | return false; |
| 994 | 1015 | } |
| 995 | 1016 | } |
| 996 | 1017 | |