| @@ -5,9 +5,8 @@ | ||
| 5 | 5 | use Elementor\Core\Common\Modules\Ajax\Module as Ajax; |
| 6 | 6 | use Elementor\Core\Settings\Manager as SettingsManager; |
| 7 | 7 | use Elementor\Includes\TemplateLibrary\Data\Controller; |
| 8 | 8 | use Elementor\TemplateLibrary\Classes\Import_Images; |
| 9 | -use Elementor\Core\Utils\Template_Library_Import_Export_Utils; | |
| 10 | 9 | use Elementor\Plugin; |
| 11 | 10 | use Elementor\User; |
| 12 | 11 | use Elementor\Utils; |
| 13 | 12 | |
| @@ -26,9 +25,8 @@ | ||
| 26 | 25 | class Manager { |
| 27 | 26 | |
| 28 | 27 | const ERROR_TEMPLATE_SOURCE_NOT_FOUND = 'Template source not found.'; |
| 29 | 28 | 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 | 29 | |
| 32 | 30 | /** |
| 33 | 31 | * Registered template sources. |
| 34 | 32 | * |
| @@ -600,12 +598,8 @@ | ||
| 600 | 598 | * |
| 601 | 599 | * @return mixed Whether the export succeeded or failed. |
| 602 | 600 | */ |
| 603 | 601 | 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 | 602 | // If the template is a JSON file, allow uploading it. |
| 609 | 603 | add_filter( 'elementor/files/allow-file-type/json', [ $this, 'enable_json_template_upload' ] ); |
| 610 | 604 | add_filter( 'elementor/files/allow_unfiltered_upload', [ $this, 'enable_json_template_upload' ] ); |
| 611 | 605 | |
| @@ -615,15 +609,16 @@ | ||
| 615 | 609 | remove_filter( 'elementor/files/allow-file-type/json', [ $this, 'enable_json_template_upload' ] ); |
| 616 | 610 | remove_filter( 'elementor/files/allow_unfiltered_upload', [ $this, 'enable_json_template_upload' ] ); |
| 617 | 611 | |
| 618 | 612 | if ( is_wp_error( $upload_result ) ) { |
| 613 | + Plugin::$instance->uploads_manager->remove_file_or_dir( dirname( $upload_result['tmp_name'] ) ); | |
| 614 | + | |
| 619 | 615 | return $upload_result; |
| 620 | 616 | } |
| 621 | 617 | |
| 622 | 618 | $source = $this->get_source( $data['source'] ?? 'local' ); |
| 623 | 619 | |
| 624 | - $import_mode = $data['import_mode'] ?? 'match_site'; | |
| 625 | - $import_result = $source->import_template( $upload_result['name'], $upload_result['tmp_name'], $import_mode ); | |
| 620 | + $import_result = $source->import_template( $upload_result['name'], $upload_result['tmp_name'] ); | |
| 626 | 621 | |
| 627 | 622 | // Remove the temporary directory generated for the stream-uploaded file. |
| 628 | 623 | Plugin::$instance->uploads_manager->remove_file_or_dir( dirname( $upload_result['tmp_name'] ) ); |
| 629 | 624 | |
| @@ -683,103 +678,11 @@ | ||
| 683 | 678 | } |
| 684 | 679 | |
| 685 | 680 | $import_data = $document->get_import_data( [ 'content' => $elements ] ); |
| 686 | 681 | |
| 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 | - } ); | |
| 682 | + return $import_data['content']; | |
| 696 | 683 | } |
| 697 | 684 | |
| 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 | 685 | public function get_item_children( array $args ) { |
| 783 | 686 | $validate_args = $this->ensure_args( [ 'source', 'template_id' ], $args ); |
| 784 | 687 | |
| 785 | 688 | if ( is_wp_error( $validate_args ) ) { |
| @@ -904,12 +807,8 @@ | ||
| 904 | 807 | if ( ! User::is_current_user_can_edit_post_type( Source_Local::CPT ) ) { |
| 905 | 808 | throw new \Exception( 'Access denied.' ); |
| 906 | 809 | } |
| 907 | 810 | |
| 908 | - if ( 'import_template' === $ajax_request && ! User::is_current_user_can_upload_json() ) { | |
| 909 | - throw new \Exception( 'Access denied.' ); | |
| 910 | - } | |
| 911 | - | |
| 912 | 811 | if ( ! empty( $data['editor_post_id'] ) ) { |
| 913 | 812 | $editor_post_id = absint( $data['editor_post_id'] ); |
| 914 | 813 | |
| 915 | 814 | if ( ! get_post( $editor_post_id ) ) { |
| @@ -1030,9 +929,8 @@ | ||
| 1030 | 929 | 'bulk_copy_templates', |
| 1031 | 930 | 'bulk_undo_delete_items', |
| 1032 | 931 | 'get_templates_quota', |
| 1033 | 932 | 'template_screenshot_failed', |
| 1034 | - 'process_global_styles', | |
| 1035 | 933 | ]; |
| 1036 | 934 | |
| 1037 | 935 | foreach ( $library_ajax_requests as $ajax_request ) { |
| 1038 | 936 | $ajax->register_ajax_action( $ajax_request, function( $data ) use ( $ajax_request ) { |
| @@ -1165,15 +1063,15 @@ | ||
| 1165 | 1063 | |
| 1166 | 1064 | $bulk_args = $from_source->format_args_for_bulk_action( $args ); |
| 1167 | 1065 | |
| 1168 | 1066 | if ( $source->supports_quota() && ! $this->is_action_to_same_source( $args ) ) { |
| 1169 | - $quota_validation = $source->validate_quota( $bulk_args ); | |
| 1067 | + $is_quota_valid = $source->validate_quota( $bulk_args ); | |
| 1170 | 1068 | |
| 1171 | - if ( is_wp_error( $quota_validation ) ) { | |
| 1172 | - return $quota_validation; | |
| 1069 | + if ( is_wp_error( $is_quota_valid ) ) { | |
| 1070 | + return $is_quota_valid; | |
| 1173 | 1071 | } |
| 1174 | 1072 | |
| 1175 | - if ( false === $quota_validation ) { | |
| 1073 | + if ( ! $is_quota_valid ) { | |
| 1176 | 1074 | return new \WP_Error( 'quota_error', 'The moving failed because it will pass the maximum templates you can save.' ); |
| 1177 | 1075 | } |
| 1178 | 1076 | } |
| 1179 | 1077 | |
| @@ -1216,15 +1114,15 @@ | ||
| 1216 | 1114 | |
| 1217 | 1115 | $bulk_args = $from_source->format_args_for_bulk_action( $args ); |
| 1218 | 1116 | |
| 1219 | 1117 | if ( $source->supports_quota() && ! $this->is_action_to_same_source( $args ) ) { |
| 1220 | - $quota_validation = $source->validate_quota( $bulk_args ); | |
| 1118 | + $is_quota_valid = $source->validate_quota( $bulk_args ); | |
| 1221 | 1119 | |
| 1222 | - if ( is_wp_error( $quota_validation ) ) { | |
| 1223 | - return $quota_validation; | |
| 1120 | + if ( is_wp_error( $is_quota_valid ) ) { | |
| 1121 | + return $is_quota_valid; | |
| 1224 | 1122 | } |
| 1225 | 1123 | |
| 1226 | - if ( false === $quota_validation ) { | |
| 1124 | + if ( ! $is_quota_valid ) { | |
| 1227 | 1125 | return new \WP_Error( 'quota_error', 'The copying failed because it will pass the maximum templates you can save.' ); |
| 1228 | 1126 | } |
| 1229 | 1127 | } |
| 1230 | 1128 | |