| @@ -2,12 +2,15 @@ | ||
| 2 | 2 | namespace Elementor\TemplateLibrary; |
| 3 | 3 | |
| 4 | 4 | use Elementor\Api; |
| 5 | 5 | use Elementor\Core\Common\Modules\Ajax\Module as Ajax; |
| 6 | +use Elementor\Core\Isolation\Wordpress_Adapter; | |
| 7 | +use Elementor\Core\Isolation\Wordpress_Adapter_Interface; | |
| 8 | +use Elementor\Core\Isolation\Elementor_Adapter; | |
| 9 | +use Elementor\Core\Isolation\Elementor_Adapter_Interface; | |
| 6 | 10 | use Elementor\Core\Settings\Manager as SettingsManager; |
| 7 | 11 | use Elementor\Includes\TemplateLibrary\Data\Controller; |
| 8 | 12 | use Elementor\TemplateLibrary\Classes\Import_Images; |
| 9 | -use Elementor\Core\Utils\Template_Library_Import_Export_Utils; | |
| 10 | 13 | use Elementor\Plugin; |
| 11 | 14 | use Elementor\User; |
| 12 | 15 | use Elementor\Utils; |
| 13 | 16 | |
| @@ -24,12 +27,8 @@ | ||
| 24 | 27 | * @since 1.0.0 |
| 25 | 28 | */ |
| 26 | 29 | class Manager { |
| 27 | 30 | |
| 28 | - const ERROR_TEMPLATE_SOURCE_NOT_FOUND = 'Template source not found.'; | |
| 29 | - const ERROR_TEMPLATE_IDS_MISSING = 'Template IDs are missing.'; | |
| 30 | - const ERROR_JSON_UPLOAD_NOT_ALLOWED = 'Uploading JSON files is not allowed for this user.'; | |
| 31 | - | |
| 32 | 31 | /** |
| 33 | 32 | * Registered template sources. |
| 34 | 33 | * |
| 35 | 34 | * Holds a list of all the supported sources with their instances. |
| @@ -51,8 +50,18 @@ | ||
| 51 | 50 | */ |
| 52 | 51 | private $_import_images = null; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore |
| 53 | 52 | |
| 54 | 53 | /** |
| 54 | + * @var Wordpress_Adapter_Interface | |
| 55 | + */ | |
| 56 | + protected $wordpress_adapter = null; | |
| 57 | + | |
| 58 | + /** | |
| 59 | + * @var Elementor_Adapter_Interface | |
| 60 | + */ | |
| 61 | + protected $elementor_adapter = null; | |
| 62 | + | |
| 63 | + /** | |
| 55 | 64 | * Template library manager constructor. |
| 56 | 65 | * |
| 57 | 66 | * Initializing the template library manager by registering default template |
| 58 | 67 | * sources and initializing ajax calls. |
| @@ -94,8 +103,16 @@ | ||
| 94 | 103 | |
| 95 | 104 | return $this->_import_images; |
| 96 | 105 | } |
| 97 | 106 | |
| 107 | + public function set_wordpress_adapter( Wordpress_Adapter_Interface $wordpress_adapter ) { | |
| 108 | + $this->wordpress_adapter = $wordpress_adapter; | |
| 109 | + } | |
| 110 | + | |
| 111 | + public function set_elementor_adapter( Elementor_Adapter_Interface $elementor_adapter ): void { | |
| 112 | + $this->elementor_adapter = $elementor_adapter; | |
| 113 | + } | |
| 114 | + | |
| 98 | 115 | /** |
| 99 | 116 | * Register template source. |
| 100 | 117 | * |
| 101 | 118 | * Used to register new template sources displayed in the template library. |
| @@ -221,12 +238,10 @@ | ||
| 221 | 238 | * |
| 222 | 239 | * @return array Library data. |
| 223 | 240 | */ |
| 224 | 241 | public function get_library_data( array $args ) { |
| 225 | - $force_update = ! empty( $args['sync'] ); | |
| 242 | + $library_data = Api::get_library_data( ! empty( $args['sync'] ) ); | |
| 226 | 243 | |
| 227 | - $library_data = Api::get_library_data( $force_update ); | |
| 228 | - | |
| 229 | 244 | if ( empty( $library_data ) ) { |
| 230 | 245 | return $library_data; |
| 231 | 246 | } |
| 232 | 247 | |
| @@ -233,21 +248,14 @@ | ||
| 233 | 248 | // Ensure all document are registered. |
| 234 | 249 | Plugin::$instance->documents->get_document_types(); |
| 235 | 250 | |
| 236 | 251 | $filter_sources = ! empty( $args['filter_sources'] ) ? $args['filter_sources'] : []; |
| 252 | + $force_update = ! empty( $args['sync'] ); | |
| 237 | 253 | |
| 238 | - $full_library_data = [ | |
| 254 | + return [ | |
| 239 | 255 | 'templates' => $this->get_templates( $filter_sources, $force_update ), |
| 240 | 256 | 'config' => $library_data['types_data'], |
| 241 | 257 | ]; |
| 242 | - | |
| 243 | - /** | |
| 244 | - * Filter the full library data. | |
| 245 | - * | |
| 246 | - * @since 3.32.2 | |
| 247 | - * @param-out $full_library_data - 'templates' and 'config' data ('config' holds the list of categories). | |
| 248 | - */ | |
| 249 | - return apply_filters( 'elementor/library/full-data', $full_library_data ); | |
| 250 | 258 | } |
| 251 | 259 | |
| 252 | 260 | /** |
| 253 | 261 | * Save template. |
| @@ -283,9 +291,9 @@ | ||
| 283 | 291 | private function save_template_item( array $args ) { |
| 284 | 292 | $source = $this->get_source( $args['source'] ); |
| 285 | 293 | |
| 286 | 294 | if ( ! $source ) { |
| 287 | - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND ); | |
| 295 | + return new \WP_Error( 'template_error', 'Template source not found.' ); | |
| 288 | 296 | } |
| 289 | 297 | |
| 290 | 298 | $args['content'] = json_decode( $args['content'], true ); |
| 291 | 299 | |
| @@ -332,9 +340,9 @@ | ||
| 332 | 340 | |
| 333 | 341 | $source = $this->get_source( $args['source'] ); |
| 334 | 342 | |
| 335 | 343 | if ( ! $source ) { |
| 336 | - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND ); | |
| 344 | + return new \WP_Error( 'template_error', 'Template source not found.' ); | |
| 337 | 345 | } |
| 338 | 346 | |
| 339 | 347 | if ( $this->is_action_to_same_source( $args ) ) { |
| 340 | 348 | return $source->move_template_to_folder( $args ); |
| @@ -339,16 +347,16 @@ | ||
| 339 | 347 | if ( $this->is_action_to_same_source( $args ) ) { |
| 340 | 348 | return $source->move_template_to_folder( $args ); |
| 341 | 349 | } |
| 342 | 350 | |
| 343 | - $from_source = $this->get_source( $args['from_source'] ); | |
| 351 | + if ( 'local' === $args['from_source'] ) { | |
| 352 | + $args = $this->format_args_for_single_action_from_local_to_cloud( $args ); | |
| 353 | + } | |
| 344 | 354 | |
| 345 | - if ( ! $from_source ) { | |
| 346 | - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND ); | |
| 355 | + if ( 'cloud' === $args['from_source'] ) { | |
| 356 | + $args = $this->format_args_for_single_action_from_cloud_to_local( $args ); | |
| 347 | 357 | } |
| 348 | 358 | |
| 349 | - $args = $from_source->format_args_for_single_action( $args ); | |
| 350 | - | |
| 351 | 359 | $template_id = $source->save_item( $args ); |
| 352 | 360 | |
| 353 | 361 | if ( is_wp_error( $template_id ) ) { |
| 354 | 362 | return $template_id; |
| @@ -366,19 +374,19 @@ | ||
| 366 | 374 | |
| 367 | 375 | $source = $this->get_source( $args['source'][0] ); |
| 368 | 376 | |
| 369 | 377 | if ( ! $source ) { |
| 370 | - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND ); | |
| 378 | + return new \WP_Error( 'template_error', 'Template source not found.' ); | |
| 371 | 379 | } |
| 372 | 380 | |
| 373 | - $from_source = $this->get_source( $args['from_source'] ); | |
| 381 | + if ( 'local' === $args['from_source'] ) { | |
| 382 | + $args = $this->format_args_for_single_action_from_local_to_cloud( $args ); | |
| 383 | + } | |
| 374 | 384 | |
| 375 | - if ( ! $from_source ) { | |
| 376 | - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND ); | |
| 385 | + if ( 'cloud' === $args['from_source'] ) { | |
| 386 | + $args = $this->format_args_for_single_action_from_cloud_to_local( $args ); | |
| 377 | 387 | } |
| 378 | 388 | |
| 379 | - $args = $from_source->format_args_for_single_action( $args ); | |
| 380 | - | |
| 381 | 389 | $template_id = $source->save_item( $args ); |
| 382 | 390 | |
| 383 | 391 | if ( is_wp_error( $template_id ) ) { |
| 384 | 392 | return $template_id; |
| @@ -390,8 +398,53 @@ | ||
| 390 | 398 | private function is_action_to_same_source( $args ) { |
| 391 | 399 | return $args['source'] === $args['from_source']; |
| 392 | 400 | } |
| 393 | 401 | |
| 402 | + private function format_args_for_single_action_from_local_to_cloud( $args ) { | |
| 403 | + if ( ! $this->is_allowed_to_read_template( [ | |
| 404 | + 'source' => $args['from_source'], | |
| 405 | + 'template_id' => $args['from_template_id'], | |
| 406 | + ] ) ) { | |
| 407 | + return new \WP_Error( | |
| 408 | + 'template_error', | |
| 409 | + esc_html__( 'You do not have permission to access this template.', 'elementor' ) | |
| 410 | + ); | |
| 411 | + } | |
| 412 | + | |
| 413 | + $document = Plugin::$instance->documents->get( $args['from_template_id'] ); | |
| 414 | + | |
| 415 | + if ( ! $document ) { | |
| 416 | + return new \WP_Error( 'template_error', 'Document not found.' ); | |
| 417 | + } | |
| 418 | + | |
| 419 | + $args['content'] = $document->get_elements_data(); | |
| 420 | + | |
| 421 | + $page = SettingsManager::get_settings_managers( 'page' )->get_model( $args['from_template_id'] ); | |
| 422 | + $args['page_settings'] = $page->get_data( 'settings' ); | |
| 423 | + | |
| 424 | + return $args; | |
| 425 | + } | |
| 426 | + | |
| 427 | + private function format_args_for_single_action_from_cloud_to_local( $args ) { | |
| 428 | + $from_source = $this->get_source( $args['from_source'] ); | |
| 429 | + | |
| 430 | + if ( ! $from_source ) { | |
| 431 | + return new \WP_Error( 'template_error', 'Template source not found.' ); | |
| 432 | + } | |
| 433 | + | |
| 434 | + $data = $from_source->get_item( $args['from_template_id'] ); | |
| 435 | + | |
| 436 | + if ( is_wp_error( $data ) || empty( $data['content'] ) ) { | |
| 437 | + return new \WP_Error( 'template_error', 'Unable to format template args.' ); | |
| 438 | + } | |
| 439 | + | |
| 440 | + $decoded_data = json_decode( $data['content'], true ); | |
| 441 | + $args['content'] = $decoded_data['content']; | |
| 442 | + $args['page_settings'] = $decoded_data['page_settings']; | |
| 443 | + | |
| 444 | + return $args; | |
| 445 | + } | |
| 446 | + | |
| 394 | 447 | /** |
| 395 | 448 | * Update template. |
| 396 | 449 | * |
| 397 | 450 | * Update template on the database. |
| @@ -413,9 +466,9 @@ | ||
| 413 | 466 | |
| 414 | 467 | $source = $this->get_source( $template_data['source'] ); |
| 415 | 468 | |
| 416 | 469 | if ( ! $source ) { |
| 417 | - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND ); | |
| 470 | + return new \WP_Error( 'template_error', 'Template source not found.' ); | |
| 418 | 471 | } |
| 419 | 472 | |
| 420 | 473 | $template_data['content'] = json_decode( $template_data['content'], true ); |
| 421 | 474 | |
| @@ -437,9 +490,9 @@ | ||
| 437 | 490 | |
| 438 | 491 | $source = $this->get_source( $template_data['source'] ); |
| 439 | 492 | |
| 440 | 493 | if ( ! $source ) { |
| 441 | - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND ); | |
| 494 | + return new \WP_Error( 'template_error', 'Template source not found.' ); | |
| 442 | 495 | } |
| 443 | 496 | |
| 444 | 497 | $update = $source->update_item( $template_data ); |
| 445 | 498 | |
| @@ -492,15 +545,9 @@ | ||
| 492 | 545 | if ( is_wp_error( $validate_args ) ) { |
| 493 | 546 | return $validate_args; |
| 494 | 547 | } |
| 495 | 548 | |
| 496 | - $source = $this->get_source( $args['source'] ); | |
| 497 | - | |
| 498 | - if ( ! $source ) { | |
| 499 | - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND ); | |
| 500 | - } | |
| 501 | - | |
| 502 | - if ( method_exists( $source, 'is_allowed_to_read_template' ) && ! $source->is_allowed_to_read_template( $args ) ) { | |
| 549 | + if ( ! $this->is_allowed_to_read_template( $args ) ) { | |
| 503 | 550 | return new \WP_Error( |
| 504 | 551 | 'template_error', |
| 505 | 552 | esc_html__( 'You do not have permission to access this template.', 'elementor' ) |
| 506 | 553 | ); |
| @@ -509,8 +556,14 @@ | ||
| 509 | 556 | if ( isset( $args['edit_mode'] ) ) { |
| 510 | 557 | Plugin::$instance->editor->set_edit_mode( $args['edit_mode'] ); |
| 511 | 558 | } |
| 512 | 559 | |
| 560 | + $source = $this->get_source( $args['source'] ); | |
| 561 | + | |
| 562 | + if ( ! $source ) { | |
| 563 | + return new \WP_Error( 'template_error', 'Template source not found.' ); | |
| 564 | + } | |
| 565 | + | |
| 513 | 566 | do_action( 'elementor/template-library/before_get_source_data', $args, $source ); |
| 514 | 567 | |
| 515 | 568 | $data = $source->get_data( $args ); |
| 516 | 569 | |
| @@ -541,9 +594,9 @@ | ||
| 541 | 594 | |
| 542 | 595 | $source = $this->get_source( $args['source'] ); |
| 543 | 596 | |
| 544 | 597 | if ( ! $source ) { |
| 545 | - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND ); | |
| 598 | + return new \WP_Error( 'template_error', 'Template source not found.' ); | |
| 546 | 599 | } |
| 547 | 600 | |
| 548 | 601 | return $source->delete_template( $args['template_id'] ); |
| 549 | 602 | } |
| @@ -570,9 +623,9 @@ | ||
| 570 | 623 | |
| 571 | 624 | $source = $this->get_source( $args['source'] ); |
| 572 | 625 | |
| 573 | 626 | if ( ! $source ) { |
| 574 | - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND ); | |
| 627 | + return new \WP_Error( 'template_error', 'Template source not found' ); | |
| 575 | 628 | } |
| 576 | 629 | |
| 577 | 630 | return $source->export_template( $args['template_id'] ); |
| 578 | 631 | } |
| @@ -600,12 +653,8 @@ | ||
| 600 | 653 | * |
| 601 | 654 | * @return mixed Whether the export succeeded or failed. |
| 602 | 655 | */ |
| 603 | 656 | public function import_template( array $data ) { |
| 604 | - if ( ! User::is_current_user_can_upload_json() ) { | |
| 605 | - return new \WP_Error( 'template_error', self::ERROR_JSON_UPLOAD_NOT_ALLOWED ); | |
| 606 | - } | |
| 607 | - | |
| 608 | 657 | // If the template is a JSON file, allow uploading it. |
| 609 | 658 | add_filter( 'elementor/files/allow-file-type/json', [ $this, 'enable_json_template_upload' ] ); |
| 610 | 659 | add_filter( 'elementor/files/allow_unfiltered_upload', [ $this, 'enable_json_template_upload' ] ); |
| 611 | 660 | |
| @@ -615,15 +664,16 @@ | ||
| 615 | 664 | remove_filter( 'elementor/files/allow-file-type/json', [ $this, 'enable_json_template_upload' ] ); |
| 616 | 665 | remove_filter( 'elementor/files/allow_unfiltered_upload', [ $this, 'enable_json_template_upload' ] ); |
| 617 | 666 | |
| 618 | 667 | if ( is_wp_error( $upload_result ) ) { |
| 668 | + Plugin::$instance->uploads_manager->remove_file_or_dir( dirname( $upload_result['tmp_name'] ) ); | |
| 669 | + | |
| 619 | 670 | return $upload_result; |
| 620 | 671 | } |
| 621 | 672 | |
| 622 | 673 | $source = $this->get_source( $data['source'] ?? 'local' ); |
| 623 | 674 | |
| 624 | - $import_mode = $data['import_mode'] ?? 'match_site'; | |
| 625 | - $import_result = $source->import_template( $upload_result['name'], $upload_result['tmp_name'], $import_mode ); | |
| 675 | + $import_result = $source->import_template( $upload_result['name'], $upload_result['tmp_name'] ); | |
| 626 | 676 | |
| 627 | 677 | // Remove the temporary directory generated for the stream-uploaded file. |
| 628 | 678 | Plugin::$instance->uploads_manager->remove_file_or_dir( dirname( $upload_result['tmp_name'] ) ); |
| 629 | 679 | |
| @@ -683,103 +733,11 @@ | ||
| 683 | 733 | } |
| 684 | 734 | |
| 685 | 735 | $import_data = $document->get_import_data( [ 'content' => $elements ] ); |
| 686 | 736 | |
| 687 | - $content = $document::on_import_update_dynamic_content( | |
| 688 | - $import_data['content'], | |
| 689 | - [ 'post_ids' => [] ] | |
| 690 | - ); | |
| 691 | - | |
| 692 | - return Plugin::$instance->db->iterate_data( $content, function( $element_data ) { | |
| 693 | - unset( $element_data['htmlCache'] ); | |
| 694 | - return $element_data; | |
| 695 | - } ); | |
| 737 | + return $import_data['content']; | |
| 696 | 738 | } |
| 697 | 739 | |
| 698 | - public function process_global_styles( array $args ) { | |
| 699 | - $validate_args = $this->ensure_args( [ 'content', 'import_mode' ], $args ); | |
| 700 | - | |
| 701 | - if ( is_wp_error( $validate_args ) ) { | |
| 702 | - return $validate_args; | |
| 703 | - } | |
| 704 | - | |
| 705 | - $import_mode = Template_Library_Import_Export_Utils::sanitize_import_mode( $args['import_mode'] ); | |
| 706 | - | |
| 707 | - $content = $this->get_validated_json_arg( $args, 'content' ); | |
| 708 | - if ( is_wp_error( $content ) ) { | |
| 709 | - return $content; | |
| 710 | - } | |
| 711 | - | |
| 712 | - $global_classes = $this->get_validated_json_arg( $args, 'global_classes', true ); | |
| 713 | - if ( is_wp_error( $global_classes ) ) { | |
| 714 | - return $global_classes; | |
| 715 | - } | |
| 716 | - | |
| 717 | - $global_variables = $this->get_validated_json_arg( $args, 'global_variables', true ); | |
| 718 | - if ( is_wp_error( $global_variables ) ) { | |
| 719 | - return $global_variables; | |
| 720 | - } | |
| 721 | - | |
| 722 | - $data = [ | |
| 723 | - 'global_classes' => $global_classes, | |
| 724 | - 'global_variables' => $global_variables, | |
| 725 | - ]; | |
| 726 | - | |
| 727 | - $result = apply_filters( | |
| 728 | - 'elementor/template_library/import/process_content', | |
| 729 | - [ 'content' => $content ], | |
| 730 | - $import_mode, | |
| 731 | - $data, | |
| 732 | - null | |
| 733 | - ); | |
| 734 | - | |
| 735 | - $response = [ | |
| 736 | - 'content' => $result['content'], | |
| 737 | - ]; | |
| 738 | - | |
| 739 | - if ( ! empty( $result['updated_global_classes'] ) ) { | |
| 740 | - $response['updated_global_classes'] = $result['updated_global_classes']; | |
| 741 | - } | |
| 742 | - | |
| 743 | - if ( ! empty( $result['updated_global_variables'] ) ) { | |
| 744 | - $response['updated_global_variables'] = $result['updated_global_variables']; | |
| 745 | - } | |
| 746 | - | |
| 747 | - $classes_to_flatten = $result['classes_to_flatten'] ?? []; | |
| 748 | - $variables_to_flatten = $result['variables_to_flatten'] ?? []; | |
| 749 | - | |
| 750 | - if ( ! empty( $classes_to_flatten ) ) { | |
| 751 | - $response['flattened_classes_count'] = count( $classes_to_flatten ); | |
| 752 | - } | |
| 753 | - | |
| 754 | - if ( ! empty( $variables_to_flatten ) ) { | |
| 755 | - $response['flattened_variables_count'] = count( $variables_to_flatten ); | |
| 756 | - } | |
| 757 | - | |
| 758 | - return $response; | |
| 759 | - } | |
| 760 | - | |
| 761 | - private function get_validated_json_arg( array $args, string $key, bool $is_optional = false ) { | |
| 762 | - if ( ! array_key_exists( $key, $args ) || null === $args[ $key ] || '' === $args[ $key ] ) { | |
| 763 | - return $is_optional ? null : new \WP_Error( "invalid_$key", "Invalid $key." ); | |
| 764 | - } | |
| 765 | - | |
| 766 | - $value = $args[ $key ]; | |
| 767 | - if ( is_string( $value ) ) { | |
| 768 | - $decoded = json_decode( $value, true ); | |
| 769 | - if ( JSON_ERROR_NONE !== json_last_error() ) { | |
| 770 | - return new \WP_Error( "invalid_$key", "Invalid JSON $key." ); | |
| 771 | - } | |
| 772 | - return $decoded; | |
| 773 | - } | |
| 774 | - | |
| 775 | - if ( ! is_array( $value ) ) { | |
| 776 | - return new \WP_Error( "invalid_$key", "Invalid $key format." ); | |
| 777 | - } | |
| 778 | - | |
| 779 | - return $value; | |
| 780 | - } | |
| 781 | - | |
| 782 | 740 | public function get_item_children( array $args ) { |
| 783 | 741 | $validate_args = $this->ensure_args( [ 'source', 'template_id' ], $args ); |
| 784 | 742 | |
| 785 | 743 | if ( is_wp_error( $validate_args ) ) { |
| @@ -788,9 +746,9 @@ | ||
| 788 | 746 | |
| 789 | 747 | $source = $this->get_source( $args['source'] ); |
| 790 | 748 | |
| 791 | 749 | if ( ! $source ) { |
| 792 | - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND ); | |
| 750 | + return new \WP_Error( 'template_error', 'Template source not found.' ); | |
| 793 | 751 | } |
| 794 | 752 | |
| 795 | 753 | return $source->get_item_children( $args ); |
| 796 | 754 | } |
| @@ -804,9 +762,9 @@ | ||
| 804 | 762 | |
| 805 | 763 | $source = $this->get_source( $args['source'] ); |
| 806 | 764 | |
| 807 | 765 | if ( ! $source ) { |
| 808 | - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND ); | |
| 766 | + return new \WP_Error( 'template_error', 'Template source not found.' ); | |
| 809 | 767 | } |
| 810 | 768 | |
| 811 | 769 | return $source->search_templates( $args ); |
| 812 | 770 | } |
| @@ -820,9 +778,9 @@ | ||
| 820 | 778 | |
| 821 | 779 | $source = $this->get_source( $args['source'] ); |
| 822 | 780 | |
| 823 | 781 | if ( ! $source ) { |
| 824 | - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND ); | |
| 782 | + return new \WP_Error( 'template_error', 'Template source not found.' ); | |
| 825 | 783 | } |
| 826 | 784 | |
| 827 | 785 | return $source->get_items( $args ); |
| 828 | 786 | } |
| @@ -836,9 +794,9 @@ | ||
| 836 | 794 | |
| 837 | 795 | $source = $this->get_source( $args['source'] ); |
| 838 | 796 | |
| 839 | 797 | if ( ! $source ) { |
| 840 | - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND ); | |
| 798 | + return new \WP_Error( 'template_error', 'Template source not found.' ); | |
| 841 | 799 | } |
| 842 | 800 | |
| 843 | 801 | return $source->save_folder( $args ); |
| 844 | 802 | } |
| @@ -873,11 +831,14 @@ | ||
| 873 | 831 | private function register_default_sources() { |
| 874 | 832 | $sources = [ |
| 875 | 833 | 'local', |
| 876 | 834 | 'remote', |
| 877 | - 'cloud', | |
| 878 | 835 | ]; |
| 879 | 836 | |
| 837 | + if ( Plugin::$instance->experiments->is_feature_active( 'cloud-library' ) ) { | |
| 838 | + $sources[] = 'cloud'; | |
| 839 | + } | |
| 840 | + | |
| 880 | 841 | foreach ( $sources as $source_filename ) { |
| 881 | 842 | $class_name = ucwords( $source_filename ); |
| 882 | 843 | $class_name = str_replace( '-', '_', $class_name ); |
| 883 | 844 | |
| @@ -897,9 +858,9 @@ | ||
| 897 | 858 | * |
| 898 | 859 | * @param array $data |
| 899 | 860 | * |
| 900 | 861 | * @return mixed |
| 901 | - * @throws \Exception If current user has no permission or the post is not found. | |
| 862 | + * @throws \Exception If the user has no permission or the post is not found. | |
| 902 | 863 | */ |
| 903 | 864 | private function handle_ajax_request( $ajax_request, array $data ) { |
| 904 | 865 | if ( ! User::is_current_user_can_edit_post_type( Source_Local::CPT ) ) { |
| 905 | 866 | throw new \Exception( 'Access denied.' ); |
| @@ -904,12 +865,8 @@ | ||
| 904 | 865 | if ( ! User::is_current_user_can_edit_post_type( Source_Local::CPT ) ) { |
| 905 | 866 | throw new \Exception( 'Access denied.' ); |
| 906 | 867 | } |
| 907 | 868 | |
| 908 | - if ( 'import_template' === $ajax_request && ! User::is_current_user_can_upload_json() ) { | |
| 909 | - throw new \Exception( 'Access denied.' ); | |
| 910 | - } | |
| 911 | - | |
| 912 | 869 | if ( ! empty( $data['editor_post_id'] ) ) { |
| 913 | 870 | $editor_post_id = absint( $data['editor_post_id'] ); |
| 914 | 871 | |
| 915 | 872 | if ( ! get_post( $editor_post_id ) ) { |
| @@ -924,13 +881,13 @@ | ||
| 924 | 881 | if ( is_wp_error( $result ) ) { |
| 925 | 882 | throw new \Exception( esc_html( $result->get_error_message() ) ); |
| 926 | 883 | } |
| 927 | 884 | |
| 928 | - return $result; | |
| 885 | + return $result; // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped | |
| 929 | 886 | } |
| 930 | 887 | |
| 931 | 888 | /** |
| 932 | - * @throws \Exception If template import fails, file validation errors occur, or processing encounters issues. | |
| 889 | + * @throws \Exception | |
| 933 | 890 | */ |
| 934 | 891 | public function save_template_screenshot( $data ): string { |
| 935 | 892 | $validate_args = $this->ensure_args( [ 'template_id', 'screenshot' ], $data ); |
| 936 | 893 | |
| @@ -943,9 +900,9 @@ | ||
| 943 | 900 | return $this->get_source( 'cloud' )->save_item_preview( $data['template_id'], $raw_binary ); |
| 944 | 901 | } |
| 945 | 902 | |
| 946 | 903 | /** |
| 947 | - * @throws \Exception If template processing fails or data validation errors occur. | |
| 904 | + * @throws \Exception | |
| 948 | 905 | */ |
| 949 | 906 | public function template_screenshot_failed( $data ): string { |
| 950 | 907 | $validate_args = $this->ensure_args( [ 'template_id' ], $data ); |
| 951 | 908 | |
| @@ -965,13 +922,13 @@ | ||
| 965 | 922 | |
| 966 | 923 | $source = $this->get_source( $data['source'] ); |
| 967 | 924 | |
| 968 | 925 | if ( ! $source ) { |
| 969 | - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND ); | |
| 926 | + return new \WP_Error( 'template_error', 'Template source not found.' ); | |
| 970 | 927 | } |
| 971 | 928 | |
| 972 | 929 | if ( empty( $data['template_ids'] ) || ! is_array( $data['template_ids'] ) ) { |
| 973 | - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_IDS_MISSING ); | |
| 930 | + return new \WP_Error( 'template_error', 'Template IDs are missing.' ); | |
| 974 | 931 | } |
| 975 | 932 | |
| 976 | 933 | return $source->bulk_delete_items( $data['template_ids'] ); |
| 977 | 934 | } |
| @@ -985,13 +942,13 @@ | ||
| 985 | 942 | |
| 986 | 943 | $source = $this->get_source( $data['source'] ); |
| 987 | 944 | |
| 988 | 945 | if ( ! $source ) { |
| 989 | - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND ); | |
| 946 | + return new \WP_Error( 'template_error', 'Template source not found.' ); | |
| 990 | 947 | } |
| 991 | 948 | |
| 992 | 949 | if ( empty( $data['template_ids'] ) || ! is_array( $data['template_ids'] ) ) { |
| 993 | - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_IDS_MISSING ); | |
| 950 | + return new \WP_Error( 'template_error', 'Template IDs are missing.' ); | |
| 994 | 951 | } |
| 995 | 952 | |
| 996 | 953 | return $source->bulk_undo_delete_items( $data['template_ids'] ); |
| 997 | 954 | } |
| @@ -1030,9 +987,8 @@ | ||
| 1030 | 987 | 'bulk_copy_templates', |
| 1031 | 988 | 'bulk_undo_delete_items', |
| 1032 | 989 | 'get_templates_quota', |
| 1033 | 990 | 'template_screenshot_failed', |
| 1034 | - 'process_global_styles', | |
| 1035 | 991 | ]; |
| 1036 | 992 | |
| 1037 | 993 | foreach ( $library_ajax_requests as $ajax_request ) { |
| 1038 | 994 | $ajax->register_ajax_action( $ajax_request, function( $data ) use ( $ajax_request ) { |
| @@ -1133,8 +1089,46 @@ | ||
| 1133 | 1089 | |
| 1134 | 1090 | return true; |
| 1135 | 1091 | } |
| 1136 | 1092 | |
| 1093 | + private function is_allowed_to_read_template( array $args ): bool { | |
| 1094 | + if ( 'remote' === $args['source'] || 'cloud' === $args['source'] ) { | |
| 1095 | + return true; | |
| 1096 | + } | |
| 1097 | + | |
| 1098 | + if ( null === $this->wordpress_adapter ) { | |
| 1099 | + $this->set_wordpress_adapter( new WordPress_Adapter() ); | |
| 1100 | + } | |
| 1101 | + | |
| 1102 | + if ( ! $this->should_check_permissions( $args ) ) { | |
| 1103 | + return true; | |
| 1104 | + } | |
| 1105 | + | |
| 1106 | + $post_id = intval( $args['template_id'] ); | |
| 1107 | + $post_status = $this->wordpress_adapter->get_post_status( $post_id ); | |
| 1108 | + $is_private_or_non_published = ( 'private' === $post_status && ! $this->wordpress_adapter->current_user_can( 'read_private_posts', $post_id ) ) || ( 'publish' !== $post_status ); | |
| 1109 | + | |
| 1110 | + $can_read_template = $is_private_or_non_published || $this->wordpress_adapter->current_user_can( 'edit_post', $post_id ); | |
| 1111 | + | |
| 1112 | + return apply_filters( 'elementor/template-library/is_allowed_to_read_template', $can_read_template, $args ); | |
| 1113 | + } | |
| 1114 | + | |
| 1115 | + private function should_check_permissions( array $args ): bool { | |
| 1116 | + if ( null === $this->elementor_adapter ) { | |
| 1117 | + $this->set_elementor_adapter( new Elementor_Adapter() ); | |
| 1118 | + } | |
| 1119 | + | |
| 1120 | + // TODO: Remove $isWidgetTemplate in 3.28.0 as there is a Pro dependency | |
| 1121 | + $check_permissions = isset( $args['check_permissions'] ) && false === $args['check_permissions']; | |
| 1122 | + $is_widget_template = 'widget' === $this->elementor_adapter->get_template_type( $args['template_id'] ); | |
| 1123 | + | |
| 1124 | + if ( $check_permissions || $is_widget_template ) { | |
| 1125 | + return false; | |
| 1126 | + } | |
| 1127 | + | |
| 1128 | + return true; | |
| 1129 | + } | |
| 1130 | + | |
| 1137 | 1131 | public function bulk_move_templates( array $args ) { |
| 1138 | 1132 | $validate_args = $this->ensure_args( [ 'source', 'from_source', 'from_template_id' ], $args ); |
| 1139 | 1133 | |
| 1140 | 1134 | if ( is_wp_error( $validate_args ) ) { |
| @@ -1149,9 +1143,9 @@ | ||
| 1149 | 1143 | private function bulk_move_template_items( array $args ) { |
| 1150 | 1144 | $source = $this->get_source( $args['source'] ); |
| 1151 | 1145 | |
| 1152 | 1146 | if ( ! $source ) { |
| 1153 | - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND ); | |
| 1147 | + return new \WP_Error( 'template_error', 'Template source not found.' ); | |
| 1154 | 1148 | } |
| 1155 | 1149 | |
| 1156 | 1150 | if ( $this->is_action_to_same_source( $args ) ) { |
| 1157 | 1151 | return $source->move_bulk_templates_to_folder( $args ); |
| @@ -1156,24 +1150,20 @@ | ||
| 1156 | 1150 | if ( $this->is_action_to_same_source( $args ) ) { |
| 1157 | 1151 | return $source->move_bulk_templates_to_folder( $args ); |
| 1158 | 1152 | } |
| 1159 | 1153 | |
| 1160 | - $from_source = $this->get_source( $args['from_source'] ); | |
| 1154 | + $bulk_args = 'local' === $args['from_source'] | |
| 1155 | + ? $this->format_args_for_bulk_action_from_local( $args ) | |
| 1156 | + : $this->format_args_for_bulk_action_from_cloud( $args ); | |
| 1161 | 1157 | |
| 1162 | - if ( ! $from_source ) { | |
| 1163 | - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND ); | |
| 1164 | - } | |
| 1165 | - | |
| 1166 | - $bulk_args = $from_source->format_args_for_bulk_action( $args ); | |
| 1167 | - | |
| 1168 | 1158 | if ( $source->supports_quota() && ! $this->is_action_to_same_source( $args ) ) { |
| 1169 | - $quota_validation = $source->validate_quota( $bulk_args ); | |
| 1159 | + $is_quota_valid = $source->validate_quota( $bulk_args ); | |
| 1170 | 1160 | |
| 1171 | - if ( is_wp_error( $quota_validation ) ) { | |
| 1172 | - return $quota_validation; | |
| 1161 | + if ( is_wp_error( $is_quota_valid ) ) { | |
| 1162 | + return $is_quota_valid; | |
| 1173 | 1163 | } |
| 1174 | 1164 | |
| 1175 | - if ( false === $quota_validation ) { | |
| 1165 | + if ( ! $is_quota_valid ) { | |
| 1176 | 1166 | return new \WP_Error( 'quota_error', 'The moving failed because it will pass the maximum templates you can save.' ); |
| 1177 | 1167 | } |
| 1178 | 1168 | } |
| 1179 | 1169 | |
| @@ -1188,8 +1178,68 @@ | ||
| 1188 | 1178 | |
| 1189 | 1179 | return $bulk_save; |
| 1190 | 1180 | } |
| 1191 | 1181 | |
| 1182 | + private function format_args_for_bulk_action_from_local( $args ) { | |
| 1183 | + $bulk_args = []; | |
| 1184 | + | |
| 1185 | + foreach ( $args['from_template_id'] as $from_template_id ) { | |
| 1186 | + if ( ! $this->is_allowed_to_read_template( [ | |
| 1187 | + 'source' => $args['from_source'], | |
| 1188 | + 'template_id' => $from_template_id, | |
| 1189 | + ] ) ) { | |
| 1190 | + continue; | |
| 1191 | + } | |
| 1192 | + | |
| 1193 | + $document = Plugin::$instance->documents->get( $from_template_id ); | |
| 1194 | + | |
| 1195 | + if ( ! $document ) { | |
| 1196 | + continue; | |
| 1197 | + } | |
| 1198 | + | |
| 1199 | + $page = SettingsManager::get_settings_managers( 'page' )->get_model( $from_template_id ); | |
| 1200 | + | |
| 1201 | + $bulk_args[] = array_merge( | |
| 1202 | + $args, | |
| 1203 | + [ | |
| 1204 | + 'title' => $document->get_post()->post_title, | |
| 1205 | + 'type' => $document::get_type(), | |
| 1206 | + 'content' => $document->get_elements_data(), | |
| 1207 | + 'page_settings' => $page->get_data( 'settings' ), | |
| 1208 | + ] | |
| 1209 | + ); | |
| 1210 | + } | |
| 1211 | + | |
| 1212 | + return $bulk_args; | |
| 1213 | + } | |
| 1214 | + | |
| 1215 | + private function format_args_for_bulk_action_from_cloud( $args ) { | |
| 1216 | + $from_source = $this->get_source( $args['from_source'] ); | |
| 1217 | + | |
| 1218 | + if ( ! $from_source ) { | |
| 1219 | + return new \WP_Error( 'template_error', 'Template source not found.' ); | |
| 1220 | + } | |
| 1221 | + | |
| 1222 | + $templates = $from_source->get_bulk_items( $args ); | |
| 1223 | + $bulk_args = []; | |
| 1224 | + | |
| 1225 | + foreach ( $templates as $template ) { | |
| 1226 | + $content = json_decode( $template['content'], true ); | |
| 1227 | + | |
| 1228 | + $bulk_args[] = array_merge( | |
| 1229 | + $args, | |
| 1230 | + [ | |
| 1231 | + 'title' => $template['title'], | |
| 1232 | + 'type' => $template['type'], | |
| 1233 | + 'content' => $content['content'], | |
| 1234 | + 'page_settings' => $content['page_settings'], | |
| 1235 | + ] | |
| 1236 | + ); | |
| 1237 | + } | |
| 1238 | + | |
| 1239 | + return $bulk_args; | |
| 1240 | + } | |
| 1241 | + | |
| 1192 | 1242 | public function bulk_copy_templates( array $args ) { |
| 1193 | 1243 | $validate_args = $this->ensure_args( [ 'source', 'from_source', 'from_template_id' ], $args ); |
| 1194 | 1244 | |
| 1195 | 1245 | if ( is_wp_error( $validate_args ) ) { |
| @@ -1204,27 +1254,23 @@ | ||
| 1204 | 1254 | private function bulk_copy_template_items( array $args ) { |
| 1205 | 1255 | $source = $this->get_source( $args['source'] ); |
| 1206 | 1256 | |
| 1207 | 1257 | if ( ! $source ) { |
| 1208 | - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND ); | |
| 1258 | + return new \WP_Error( 'template_error', 'Template source not found.' ); | |
| 1209 | 1259 | } |
| 1210 | 1260 | |
| 1211 | - $from_source = $this->get_source( $args['from_source'] ); | |
| 1261 | + $bulk_args = 'local' === $args['from_source'] | |
| 1262 | + ? $this->format_args_for_bulk_action_from_local( $args ) | |
| 1263 | + : $this->format_args_for_bulk_action_from_cloud( $args ); | |
| 1212 | 1264 | |
| 1213 | - if ( ! $from_source ) { | |
| 1214 | - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND ); | |
| 1215 | - } | |
| 1216 | - | |
| 1217 | - $bulk_args = $from_source->format_args_for_bulk_action( $args ); | |
| 1218 | - | |
| 1219 | 1265 | if ( $source->supports_quota() && ! $this->is_action_to_same_source( $args ) ) { |
| 1220 | - $quota_validation = $source->validate_quota( $bulk_args ); | |
| 1266 | + $is_quota_valid = $source->validate_quota( $bulk_args ); | |
| 1221 | 1267 | |
| 1222 | - if ( is_wp_error( $quota_validation ) ) { | |
| 1223 | - return $quota_validation; | |
| 1268 | + if ( is_wp_error( $is_quota_valid ) ) { | |
| 1269 | + return $is_quota_valid; | |
| 1224 | 1270 | } |
| 1225 | 1271 | |
| 1226 | - if ( false === $quota_validation ) { | |
| 1272 | + if ( ! $is_quota_valid ) { | |
| 1227 | 1273 | return new \WP_Error( 'quota_error', 'The copying failed because it will pass the maximum templates you can save.' ); |
| 1228 | 1274 | } |
| 1229 | 1275 | } |
| 1230 | 1276 | |