| 1 |
<?php |
| 2 |
namespace Elementor\App\Modules\ImportExportCustomization\Data\Routes; |
| 3 |
|
| 4 |
use Elementor\Plugin; |
| 5 |
use Elementor\App\Modules\ImportExportCustomization\Data\Response; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; // Exit if accessed directly. |
| 9 |
} |
| 10 |
|
| 11 |
class Upload extends Base_Route { |
| 12 |
protected function get_route(): string { |
| 13 |
return 'upload'; |
| 14 |
} |
| 15 |
|
| 16 |
protected function get_method(): string { |
| 17 |
return \WP_REST_Server::CREATABLE; |
| 18 |
} |
| 19 |
|
| 20 |
private function format_url( string $url ): string { |
| 21 |
return wp_unslash( urldecode( $url ) ); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* @param $request \WP_REST_Request |
| 26 |
* @return \WP_REST_Response |
| 27 |
*/ |
| 28 |
protected function callback( $request ): \WP_REST_Response { |
| 29 |
try { |
| 30 |
$file_url = $request->get_param( 'file_url' ); |
| 31 |
$kit_id = $request->get_param( 'kit_id' ); |
| 32 |
$source = $request->get_param( 'source' ); |
| 33 |
$module = Plugin::$instance->app->get_component( 'import-export-customization' ); |
| 34 |
|
| 35 |
$is_import_from_library = ! empty( $file_url ); |
| 36 |
if ( $is_import_from_library ) { |
| 37 |
$file_url = $this->format_url( $file_url ); |
| 38 |
} |
| 39 |
|
| 40 |
if ( $is_import_from_library ) { |
| 41 |
if ( ! filter_var( $file_url, FILTER_VALIDATE_URL ) || 0 !== strpos( $file_url, 'http' ) ) { |
| 42 |
return Response::error( 'Invalid kit library URL.', 'invalid_kit_library_url' ); |
| 43 |
} |
| 44 |
|
| 45 |
$import_result = apply_filters( 'elementor/import/kit/result', [ 'file_url' => $file_url ] ); |
| 46 |
} elseif ( ! empty( $source ) ) { |
| 47 |
$import_result = apply_filters( 'elementor/import/kit/result/' . $source, [ |
| 48 |
'kit_id' => $kit_id, |
| 49 |
'source' => $source, |
| 50 |
] ); |
| 51 |
} else { |
| 52 |
$files = $request->get_file_params(); |
| 53 |
$file = $files['e_import_file'] ?? null; |
| 54 |
|
| 55 |
if ( empty( $file ) || empty( $file['tmp_name'] ) ) { |
| 56 |
return Response::error( 'No file uploaded or upload error occurred.', 'no_file_uploaded' ); |
| 57 |
} |
| 58 |
|
| 59 |
$import_result = [ |
| 60 |
'file_name' => $file['tmp_name'], |
| 61 |
'referrer' => $module::REFERRER_LOCAL, |
| 62 |
]; |
| 63 |
} |
| 64 |
|
| 65 |
Plugin::$instance->logger->get_logger()->info( 'Uploading Kit via REST API: ', [ |
| 66 |
'meta' => [ |
| 67 |
'kit_id' => $kit_id, |
| 68 |
'referrer' => $import_result['referrer'] ?? 'unknown', |
| 69 |
], |
| 70 |
] ); |
| 71 |
|
| 72 |
if ( is_wp_error( $import_result ) ) { |
| 73 |
return Response::error( $import_result->get_error_message(), 'upload_error' ); |
| 74 |
} |
| 75 |
|
| 76 |
$uploaded_kit = $module->upload_kit( $import_result['file_name'], $import_result['referrer'], $kit_id ); |
| 77 |
|
| 78 |
$result = [ |
| 79 |
'session' => $uploaded_kit['session'], |
| 80 |
'manifest' => $uploaded_kit['manifest'], |
| 81 |
]; |
| 82 |
|
| 83 |
if ( ! empty( $import_result['file_url'] ) ) { |
| 84 |
$result['file_url'] = $import_result['file_url']; |
| 85 |
} |
| 86 |
|
| 87 |
if ( ! empty( $import_result['kit'] ) ) { |
| 88 |
$result['uploaded_kit'] = $import_result['kit']; |
| 89 |
} |
| 90 |
|
| 91 |
if ( ! empty( $uploaded_kit['conflicts'] ) ) { |
| 92 |
$result['conflicts'] = $uploaded_kit['conflicts']; |
| 93 |
} |
| 94 |
|
| 95 |
// Clean up temporary files |
| 96 |
if ( $is_import_from_library || ! empty( $source ) ) { |
| 97 |
Plugin::$instance->uploads_manager->remove_file_or_dir( dirname( $import_result['file_name'] ) ); |
| 98 |
} |
| 99 |
|
| 100 |
return Response::success( $result ); |
| 101 |
|
| 102 |
} catch ( \Error $e ) { |
| 103 |
Plugin::$instance->logger->get_logger()->error( $e->getMessage(), [ |
| 104 |
'meta' => [ |
| 105 |
'trace' => $e->getTraceAsString(), |
| 106 |
], |
| 107 |
] ); |
| 108 |
|
| 109 |
return Response::error( $e->getMessage(), 'upload_error' ); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
protected function get_args(): array { |
| 114 |
return [ |
| 115 |
'file_url' => [ |
| 116 |
'type' => 'string', |
| 117 |
'description' => 'File URL for upload action', |
| 118 |
'required' => false, |
| 119 |
'validate_callback' => function ( $value ) { |
| 120 |
if ( empty( $value ) ) { |
| 121 |
return true; |
| 122 |
} |
| 123 |
|
| 124 |
return filter_var( $this->format_url( $value ), FILTER_VALIDATE_URL ); |
| 125 |
}, |
| 126 |
], |
| 127 |
'kit_id' => [ |
| 128 |
'type' => 'string', |
| 129 |
'description' => 'Kit ID for upload action', |
| 130 |
'required' => false, |
| 131 |
], |
| 132 |
'source' => [ |
| 133 |
'type' => 'string', |
| 134 |
'description' => 'Source for upload action', |
| 135 |
'required' => false, |
| 136 |
], |
| 137 |
]; |
| 138 |
} |
| 139 |
} |
| 140 |
|