| 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\Utils as ElementorUtils; |
| 7 |
use Elementor\App\Modules\ImportExportCustomization\Module as ImportExportCustomizationModule; |
| 8 |
use Elementor\App\Modules\ImportExportCustomization\Processes\Import; |
| 9 |
use Elementor\App\Modules\ImportExportCustomization\Data\Routes\Traits\Handles_Quota_Errors; |
| 10 |
|
| 11 |
|
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; // Exit if accessed directly. |
| 14 |
} |
| 15 |
|
| 16 |
class Export extends Base_Route { |
| 17 |
use Handles_Quota_Errors; |
| 18 |
|
| 19 |
protected function get_route(): string { |
| 20 |
return 'export'; |
| 21 |
} |
| 22 |
|
| 23 |
protected function get_method(): string { |
| 24 |
return \WP_REST_Server::CREATABLE; |
| 25 |
} |
| 26 |
|
| 27 |
protected function callback( $request ): \WP_REST_Response { |
| 28 |
/** |
| 29 |
* @var $module ImportExportCustomizationModule |
| 30 |
*/ |
| 31 |
$module = Plugin::$instance->app->get_component( 'import-export-customization' ); |
| 32 |
|
| 33 |
try { |
| 34 |
$settings = [ |
| 35 |
'include' => $request->get_param( 'include' ), |
| 36 |
'kitInfo' => $request->get_param( 'kitInfo' ), |
| 37 |
'screenShotBlob' => $request->get_param( 'screenShotBlob' ), |
| 38 |
'customization' => $request->get_param( 'customization' ), |
| 39 |
'plugins' => $request->get_param( 'plugins' ), |
| 40 |
'selectedCustomPostTypes' => $request->get_param( 'selectedCustomPostTypes' ), |
| 41 |
]; |
| 42 |
|
| 43 |
$settings = array_filter( $settings ); |
| 44 |
|
| 45 |
$source = $settings['kitInfo']['source']; |
| 46 |
|
| 47 |
$export = $module->export_kit( $settings ); |
| 48 |
|
| 49 |
$file_name = $export['file_name']; |
| 50 |
$file_size = filesize( $file_name ); |
| 51 |
$file = ElementorUtils::file_get_contents( $file_name ); |
| 52 |
|
| 53 |
if ( ! $file ) { |
| 54 |
throw new \Error( Import::ZIP_FILE_ERROR_KEY ); |
| 55 |
} |
| 56 |
|
| 57 |
Plugin::$instance->uploads_manager->remove_file_or_dir( dirname( $file_name ) ); |
| 58 |
|
| 59 |
$result = apply_filters( |
| 60 |
'elementor/export/kit/export-result', |
| 61 |
[ |
| 62 |
'manifest' => $export['manifest'], |
| 63 |
'file' => base64_encode( $file ), |
| 64 |
'media_urls' => $export['media_urls'], |
| 65 |
], |
| 66 |
$source, |
| 67 |
$export, |
| 68 |
$settings, |
| 69 |
$file, |
| 70 |
$file_size, |
| 71 |
); |
| 72 |
|
| 73 |
if ( is_wp_error( $result ) ) { |
| 74 |
throw new \Error( $result->get_error_message() ); |
| 75 |
} |
| 76 |
|
| 77 |
return Response::success( $result ); |
| 78 |
|
| 79 |
} catch ( \Error |\Exception $e ) { |
| 80 |
Plugin::$instance->logger->get_logger()->error( $e->getMessage(), [ |
| 81 |
'meta' => [ |
| 82 |
'trace' => $e->getTraceAsString(), |
| 83 |
], |
| 84 |
] ); |
| 85 |
|
| 86 |
if ( $module->is_third_party_class( $e->getTrace()[0]['class'] ) ) { |
| 87 |
return Response::error( ImportExportCustomizationModule::THIRD_PARTY_ERROR, $e->getMessage() ); |
| 88 |
} |
| 89 |
|
| 90 |
if ( $this->is_quota_error( $e->getMessage() ) ) { |
| 91 |
$quota = null; |
| 92 |
$cloud_kit_library_app = $this->get_cloud_kit_library_app(); |
| 93 |
|
| 94 |
if ( $cloud_kit_library_app ) { |
| 95 |
try { |
| 96 |
$quota = $cloud_kit_library_app->get_quota(); |
| 97 |
} catch ( \Exception |\Error $quota_error ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch |
| 98 |
// Quota fetch failed, error message will use default value. |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
return $this->get_quota_error_response( $quota, $settings['kitInfo'] ?? [] ); |
| 103 |
} |
| 104 |
|
| 105 |
return Response::error( $e->getMessage(), 'export_error' ); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
protected function get_args(): array { |
| 110 |
return [ |
| 111 |
'include' => [ |
| 112 |
'type' => 'array', |
| 113 |
'description' => 'Content types to include in export', |
| 114 |
'required' => false, |
| 115 |
'default' => [ 'templates', 'content', 'settings', 'plugins' ], |
| 116 |
], |
| 117 |
'kitInfo' => [ |
| 118 |
'type' => 'object', |
| 119 |
'description' => 'Kit information', |
| 120 |
'required' => false, |
| 121 |
'default' => [ |
| 122 |
'title' => 'Elementor Website Template', |
| 123 |
'description' => '', |
| 124 |
'source' => 'local', |
| 125 |
], |
| 126 |
], |
| 127 |
'screenShotBlob' => [ |
| 128 |
'type' => [ 'string', 'null' ], |
| 129 |
'description' => 'Base64 encoded screenshot for cloud exports', |
| 130 |
'required' => false, |
| 131 |
'default' => null, |
| 132 |
], |
| 133 |
'customization' => [ |
| 134 |
'type' => 'object', |
| 135 |
'description' => 'Customization settings for selective export', |
| 136 |
'required' => false, |
| 137 |
'default' => null, |
| 138 |
'properties' => [ |
| 139 |
'settings' => [ |
| 140 |
'type' => [ 'object', 'null' ], |
| 141 |
'description' => 'Site settings customization', |
| 142 |
], |
| 143 |
'templates' => [ |
| 144 |
'type' => [ 'object', 'null' ], |
| 145 |
'description' => 'Templates customization', |
| 146 |
], |
| 147 |
'content' => [ |
| 148 |
'type' => [ 'object', 'null' ], |
| 149 |
'description' => 'Content customization', |
| 150 |
], |
| 151 |
'plugins' => [ |
| 152 |
'type' => [ 'object', 'null' ], |
| 153 |
'description' => 'Plugins customization', |
| 154 |
], |
| 155 |
], |
| 156 |
], |
| 157 |
'plugins' => [ |
| 158 |
'type' => 'array', |
| 159 |
'description' => 'Selected plugins to export', |
| 160 |
'required' => false, |
| 161 |
'default' => [], |
| 162 |
], |
| 163 |
'selectedCustomPostTypes' => [ |
| 164 |
'type' => 'array', |
| 165 |
'description' => 'Selected custom post types', |
| 166 |
'required' => false, |
| 167 |
'default' => [], |
| 168 |
], |
| 169 |
]; |
| 170 |
} |
| 171 |
} |
| 172 |
|