| 1 |
<?php |
| 2 |
namespace Elementor\App\Modules\ImportExportCustomization\Data\Routes; |
| 3 |
|
| 4 |
use Elementor\Plugin; |
| 5 |
use Elementor\App\Modules\ImportExportCustomization\Data\Response; |
| 6 |
use Elementor\App\Modules\ImportExportCustomization\Module as ImportExportCustomizationModule; |
| 7 |
use Elementor\Modules\CloudKitLibrary\Module as CloudKitLibrary; |
| 8 |
use Elementor\Utils as ElementorUtils; |
| 9 |
use Elementor\App\Modules\ImportExportCustomization\Data\Routes\Traits\Handles_Quota_Errors; |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; // Exit if accessed directly. |
| 13 |
} |
| 14 |
|
| 15 |
class Process_Media extends Base_Route { |
| 16 |
use Handles_Quota_Errors; |
| 17 |
|
| 18 |
protected function get_route(): string { |
| 19 |
return 'process-media'; |
| 20 |
} |
| 21 |
|
| 22 |
protected function get_method(): string { |
| 23 |
return \WP_REST_Server::CREATABLE; |
| 24 |
} |
| 25 |
|
| 26 |
protected function callback( $request ): \WP_REST_Response { |
| 27 |
/** |
| 28 |
* @var $module ImportExportCustomizationModule |
| 29 |
*/ |
| 30 |
$module = Plugin::$instance->app->get_component( 'import-export-customization' ); |
| 31 |
|
| 32 |
$cloud_kit_library_app = $this->get_cloud_kit_library_app(); |
| 33 |
|
| 34 |
$media_urls = $request->get_param( 'media_urls' ); |
| 35 |
$kit = $request->get_param( 'kit' ); |
| 36 |
$quota = null; |
| 37 |
|
| 38 |
try { |
| 39 |
if ( empty( $media_urls ) || ! is_array( $media_urls ) ) { |
| 40 |
throw new \Error( 'Invalid media URLs provided' ); |
| 41 |
} |
| 42 |
|
| 43 |
$media_collector = new \Elementor\TemplateLibrary\Classes\Media_Collector(); |
| 44 |
$zip_path = $media_collector->process_media_collection( $media_urls ); |
| 45 |
|
| 46 |
if ( $cloud_kit_library_app ) { |
| 47 |
$quota = $cloud_kit_library_app->get_quota(); |
| 48 |
$cloud_kit_library_app->validate_storage_quota( filesize( $zip_path ), $quota ); |
| 49 |
} |
| 50 |
|
| 51 |
if ( ! $zip_path ) { |
| 52 |
throw new \Error( 'Failed to process media' ); |
| 53 |
} |
| 54 |
|
| 55 |
$zip_file = ElementorUtils::file_get_contents( $zip_path ); |
| 56 |
|
| 57 |
$upload_success = false; |
| 58 |
if ( $cloud_kit_library_app ) { |
| 59 |
$upload_success = $cloud_kit_library_app->upload_content_file( $kit['mediaUploadUrl'], $zip_file ); |
| 60 |
$cloud_kit_library_app->update_kit( $kit['id'], [ 'mediaFileId' => $upload_success ? $kit['mediaFileId'] : null ] ); |
| 61 |
} |
| 62 |
|
| 63 |
$media_collector->cleanup(); |
| 64 |
|
| 65 |
return Response::success( [ |
| 66 |
'success' => true, |
| 67 |
'message' => 'Media processed and uploaded successfully', |
| 68 |
] ); |
| 69 |
|
| 70 |
} catch ( \Error |\Exception $e ) { |
| 71 |
Plugin::$instance->logger->get_logger()->error( $e->getMessage(), [ |
| 72 |
'meta' => [ |
| 73 |
'trace' => $e->getTraceAsString(), |
| 74 |
], |
| 75 |
] ); |
| 76 |
|
| 77 |
if ( $cloud_kit_library_app ) { |
| 78 |
$cloud_kit_library_app->update_kit( $kit['id'], [ 'mediaFileId' => null ] ); |
| 79 |
} |
| 80 |
|
| 81 |
if ( $module->is_third_party_class( $e->getTrace()[0]['class'] ) ) { |
| 82 |
return Response::error( ImportExportCustomizationModule::THIRD_PARTY_ERROR, $e->getMessage() ); |
| 83 |
} |
| 84 |
|
| 85 |
if ( $this->is_quota_error( $e->getMessage() ) ) { |
| 86 |
return $this->get_quota_error_response( $quota, $kit ); |
| 87 |
} |
| 88 |
|
| 89 |
return Response::error( ImportExportCustomizationModule::MEDIA_PROCESSING_ERROR, $e->getMessage() ); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
protected function get_args(): array { |
| 94 |
return [ |
| 95 |
'media_urls' => [ |
| 96 |
'type' => 'array', |
| 97 |
'description' => 'Array of media URLs to process', |
| 98 |
'required' => true, |
| 99 |
'items' => [ |
| 100 |
'type' => 'string', |
| 101 |
], |
| 102 |
], |
| 103 |
]; |
| 104 |
} |
| 105 |
} |
| 106 |
|