← All changes
|
app/modules/import-export-customization/data/routes/upload.php
+3
-58
4.1.3
→
3.32.0-dev3
View file →
| @@ -1,8 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | namespace Elementor\App\Modules\ImportExportCustomization\Data\Routes; |
| 3 | 3 | |
| 4 | -use Elementor\App\Modules\ImportExportCustomization\Module as ImportExportCustomizationModule; | |
| 5 | 4 | use Elementor\Plugin; |
| 6 | 5 | use Elementor\App\Modules\ImportExportCustomization\Data\Response; |
| 7 | 6 | |
| 8 | 7 | if ( ! defined( 'ABSPATH' ) ) { |
| @@ -26,13 +25,8 @@ | ||
| 26 | 25 | * @param $request \WP_REST_Request |
| 27 | 26 | * @return \WP_REST_Response |
| 28 | 27 | */ |
| 29 | 28 | protected function callback( $request ): \WP_REST_Response { |
| 30 | - /** | |
| 31 | - * @var $module ImportExportCustomizationModule | |
| 32 | - */ | |
| 33 | - $module = Plugin::$instance->app->get_component( 'import-export-customization' ); | |
| 34 | - | |
| 35 | 29 | try { |
| 36 | 30 | $file_url = $request->get_param( 'file_url' ); |
| 37 | 31 | $kit_id = $request->get_param( 'kit_id' ); |
| 38 | 32 | $source = $request->get_param( 'source' ); |
| @@ -44,9 +38,9 @@ | ||
| 44 | 38 | } |
| 45 | 39 | |
| 46 | 40 | if ( $is_import_from_library ) { |
| 47 | 41 | if ( ! filter_var( $file_url, FILTER_VALIDATE_URL ) || 0 !== strpos( $file_url, 'http' ) ) { |
| 48 | - return Response::error( ImportExportCustomizationModule::KIT_LIBRARY_ERROR_KEY, 'Invalid kit library URL.' ); | |
| 42 | + return Response::error( 'Invalid kit library URL.', 'invalid_kit_library_url' ); | |
| 49 | 43 | } |
| 50 | 44 | |
| 51 | 45 | $import_result = apply_filters( 'elementor/import/kit/result', [ 'file_url' => $file_url ] ); |
| 52 | 46 | } elseif ( ! empty( $source ) ) { |
| @@ -58,9 +52,9 @@ | ||
| 58 | 52 | $files = $request->get_file_params(); |
| 59 | 53 | $file = $files['e_import_file'] ?? null; |
| 60 | 54 | |
| 61 | 55 | if ( empty( $file ) || empty( $file['tmp_name'] ) ) { |
| 62 | - return Response::error( 'no_file_uploaded', 'No file uploaded or upload error occurred.' ); | |
| 56 | + return Response::error( 'No file uploaded or upload error occurred.', 'no_file_uploaded' ); | |
| 63 | 57 | } |
| 64 | 58 | |
| 65 | 59 | $import_result = [ |
| 66 | 60 | 'file_name' => $file['tmp_name'], |
| @@ -78,12 +72,8 @@ | ||
| 78 | 72 | if ( is_wp_error( $import_result ) ) { |
| 79 | 73 | return Response::error( $import_result->get_error_message(), 'upload_error' ); |
| 80 | 74 | } |
| 81 | 75 | |
| 82 | - if ( ! empty( $import_result['media_file_name'] ) ) { | |
| 83 | - $this->setup_media_mapping( $import_result['media_file_name'] ); | |
| 84 | - } | |
| 85 | - | |
| 86 | 76 | $uploaded_kit = $module->upload_kit( $import_result['file_name'], $import_result['referrer'], $kit_id ); |
| 87 | 77 | |
| 88 | 78 | $result = [ |
| 89 | 79 | 'session' => $uploaded_kit['session'], |
| @@ -108,9 +98,9 @@ | ||
| 108 | 98 | } |
| 109 | 99 | |
| 110 | 100 | return Response::success( $result ); |
| 111 | 101 | |
| 112 | - } catch ( \Error | \Exception $e ) { | |
| 102 | + } catch ( \Error $e ) { | |
| 113 | 103 | Plugin::$instance->logger->get_logger()->error( $e->getMessage(), [ |
| 114 | 104 | 'meta' => [ |
| 115 | 105 | 'trace' => $e->getTraceAsString(), |
| 116 | 106 | ], |
| @@ -115,55 +105,10 @@ | ||
| 115 | 105 | 'trace' => $e->getTraceAsString(), |
| 116 | 106 | ], |
| 117 | 107 | ] ); |
| 118 | 108 | |
| 119 | - if ( $module->is_third_party_class( $e->getTrace()[0]['class'] ) ) { | |
| 120 | - return Response::error( ImportExportCustomizationModule::THIRD_PARTY_ERROR, $e->getMessage() ); | |
| 121 | - } | |
| 122 | - | |
| 123 | 109 | return Response::error( $e->getMessage(), 'upload_error' ); |
| 124 | 110 | } |
| 125 | - } | |
| 126 | - | |
| 127 | - private function setup_media_mapping( $media_zip_path ) { | |
| 128 | - \Elementor\TemplateLibrary\Classes\Media_Mapper::clear_mapping(); | |
| 129 | - | |
| 130 | - $media_dir = null; | |
| 131 | - | |
| 132 | - if ( file_exists( $media_zip_path ) ) { | |
| 133 | - $media_dir = $this->extract_media_zip( $media_zip_path ); | |
| 134 | - } | |
| 135 | - | |
| 136 | - if ( $media_dir && file_exists( $media_dir . '/media-mapping.json' ) ) { | |
| 137 | - $media_mapping = json_decode( file_get_contents( $media_dir . '/media-mapping.json' ), true ); | |
| 138 | - | |
| 139 | - \Elementor\TemplateLibrary\Classes\Media_Mapper::set_mapping( $media_mapping, $media_dir ); | |
| 140 | - } | |
| 141 | - | |
| 142 | - Plugin::$instance->uploads_manager->remove_file_or_dir( $media_zip_path ); | |
| 143 | - | |
| 144 | - return $media_dir; | |
| 145 | - } | |
| 146 | - | |
| 147 | - private function extract_media_zip( $zip_path ) { | |
| 148 | - if ( ! class_exists( '\ZipArchive' ) ) { | |
| 149 | - return null; | |
| 150 | - } | |
| 151 | - | |
| 152 | - $zip = new \ZipArchive(); | |
| 153 | - if ( $zip->open( $zip_path ) !== true ) { | |
| 154 | - return null; | |
| 155 | - } | |
| 156 | - | |
| 157 | - $media_dir = dirname( $zip_path ) . '/media'; | |
| 158 | - if ( ! $zip->extractTo( $media_dir ) ) { | |
| 159 | - $zip->close(); | |
| 160 | - return null; | |
| 161 | - } | |
| 162 | - | |
| 163 | - $zip->close(); | |
| 164 | - | |
| 165 | - return $media_dir; | |
| 166 | 111 | } |
| 167 | 112 | |
| 168 | 113 | protected function get_args(): array { |
| 169 | 114 | return [ |