← All changes
|
modules/cloud-kit-library/connect/cloud-kits.php
+13
-60
4.1.0-dev2
→
3.32.0-dev3
View file →
| @@ -10,15 +10,10 @@ | ||
| 10 | 10 | |
| 11 | 11 | class Cloud_Kits extends Library { |
| 12 | 12 | const THRESHOLD_UNLIMITED = -1; |
| 13 | 13 | const FAILED_TO_FETCH_QUOTA_KEY = 'failed-to-fetch-quota'; |
| 14 | - | |
| 15 | - const FAILED_TO_UPLOAD_KIT = 'cloud-upload-failed'; | |
| 16 | - | |
| 17 | 14 | const INSUFFICIENT_QUOTA_KEY = 'insufficient-quota'; |
| 18 | 15 | |
| 19 | - const INSUFFICIENT_STORAGE_QUOTA = 'insufficient-storage-quota'; | |
| 20 | - | |
| 21 | 16 | public function get_title() { |
| 22 | 17 | return esc_html__( 'Cloud Kits', 'elementor' ); |
| 23 | 18 | } |
| 24 | 19 | |
| @@ -38,18 +33,16 @@ | ||
| 38 | 33 | /** |
| 39 | 34 | * @return array|\WP_Error |
| 40 | 35 | */ |
| 41 | 36 | public function get_quota() { |
| 42 | - if ( ! $this->is_connected() ) { | |
| 43 | - return new \WP_Error( 'not_connected', esc_html__( 'Not connected', 'elementor' ) ); | |
| 44 | - } | |
| 45 | - | |
| 46 | 37 | return $this->http_request( 'GET', 'quota/kits', [], [ |
| 47 | 38 | 'return_type' => static::HTTP_RETURN_TYPE_ARRAY, |
| 48 | 39 | ] ); |
| 49 | 40 | } |
| 50 | 41 | |
| 51 | - public function validate_quota( $quota ) { | |
| 42 | + public function validate_quota() { | |
| 43 | + $quota = $this->get_quota(); | |
| 44 | + | |
| 52 | 45 | if ( is_wp_error( $quota ) ) { |
| 53 | 46 | throw new \Error( static::FAILED_TO_FETCH_QUOTA_KEY ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 54 | 47 | } |
| 55 | 48 | |
| @@ -60,24 +53,8 @@ | ||
| 60 | 53 | throw new \Error( static::INSUFFICIENT_QUOTA_KEY ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 61 | 54 | } |
| 62 | 55 | } |
| 63 | 56 | |
| 64 | - public function validate_storage_quota( $intended_usage, $quota ) { | |
| 65 | - if ( is_wp_error( $quota ) ) { | |
| 66 | - throw new \Error( static::FAILED_TO_FETCH_QUOTA_KEY ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped | |
| 67 | - } | |
| 68 | - | |
| 69 | - if ( empty( $quota['storage']['currentUsage'] ) ) { | |
| 70 | - return; | |
| 71 | - } | |
| 72 | - | |
| 73 | - $has_quota = $quota['storage']['currentUsage'] + $intended_usage < $quota['storage']['threshold']; | |
| 74 | - | |
| 75 | - if ( ! $has_quota ) { | |
| 76 | - throw new \Error( static::INSUFFICIENT_STORAGE_QUOTA ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped | |
| 77 | - } | |
| 78 | - } | |
| 79 | - | |
| 80 | 57 | public function check_eligibility() { |
| 81 | 58 | $quota = $this->get_quota(); |
| 82 | 59 | |
| 83 | 60 | if ( is_wp_error( $quota ) ) { |
| @@ -92,12 +69,10 @@ | ||
| 92 | 69 | 'subscription_id' => ! empty( $quota['subscriptionId'] ) ? $quota['subscriptionId'] : '', |
| 93 | 70 | ]; |
| 94 | 71 | } |
| 95 | 72 | |
| 96 | - public function create_kit( $title, $description, $content_file_data, $preview_file_data, array $includes, string $media_format = 'link', $file_size = 0 ) { | |
| 97 | - $quota = $this->get_quota(); | |
| 98 | - $this->validate_quota( $quota ); | |
| 99 | - $this->validate_storage_quota( $file_size, $quota ); | |
| 73 | + public function create_kit( $title, $description, $content_file_data, $preview_file_data, array $includes ) { | |
| 74 | + $this->validate_quota(); | |
| 100 | 75 | |
| 101 | 76 | $endpoint = 'kits'; |
| 102 | 77 | |
| 103 | 78 | $boundary = wp_generate_password( 24, false ); |
| @@ -110,9 +85,8 @@ | ||
| 110 | 85 | [ |
| 111 | 86 | 'title' => $title, |
| 112 | 87 | 'description' => $description, |
| 113 | 88 | 'includes' => wp_json_encode( $includes ), |
| 114 | - 'mediaFormat' => $media_format, | |
| 115 | 89 | ], |
| 116 | 90 | [ |
| 117 | 91 | 'previewFile' => [ |
| 118 | 92 | 'filename' => 'preview.png', |
| @@ -132,15 +106,17 @@ | ||
| 132 | 106 | $response = $this->http_request( 'POST', $endpoint, $payload, [ |
| 133 | 107 | 'return_type' => static::HTTP_RETURN_TYPE_ARRAY, |
| 134 | 108 | ] ); |
| 135 | 109 | |
| 136 | - if ( is_wp_error( $response ) || empty( $response['id'] ) ) { | |
| 137 | - throw new \Exception( static::FAILED_TO_UPLOAD_KIT, Exceptions::INTERNAL_SERVER_ERROR ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped | |
| 110 | + if ( empty( $response['id'] ) ) { | |
| 111 | + $error_message = esc_html__( 'Failed to create kit: Invalid response', 'elementor' ); | |
| 112 | + throw new \Exception( $error_message, Exceptions::INTERNAL_SERVER_ERROR ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped | |
| 138 | 113 | } |
| 139 | 114 | |
| 140 | 115 | if ( empty( $response['uploadUrl'] ) ) { |
| 141 | 116 | $this->delete_kit( $response['id'] ); |
| 142 | - throw new \Exception( static::FAILED_TO_UPLOAD_KIT, Exceptions::INTERNAL_SERVER_ERROR ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped | |
| 117 | + $error_message = esc_html__( 'Failed to create kit: No upload URL provided', 'elementor' ); | |
| 118 | + throw new \Exception( $error_message, Exceptions::INTERNAL_SERVER_ERROR ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped | |
| 143 | 119 | } |
| 144 | 120 | |
| 145 | 121 | $upload_success = $this->upload_content_file( $response['uploadUrl'], $content_file_data ); |
| 146 | 122 | |
| @@ -145,15 +121,16 @@ | ||
| 145 | 121 | $upload_success = $this->upload_content_file( $response['uploadUrl'], $content_file_data ); |
| 146 | 122 | |
| 147 | 123 | if ( ! $upload_success ) { |
| 148 | 124 | $this->delete_kit( $response['id'] ); |
| 149 | - throw new \Exception( static::FAILED_TO_UPLOAD_KIT, Exceptions::INTERNAL_SERVER_ERROR ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped | |
| 125 | + $error_message = esc_html__( 'Failed to create kit: Content upload failed', 'elementor' ); | |
| 126 | + throw new \Exception( $error_message, Exceptions::INTERNAL_SERVER_ERROR ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped | |
| 150 | 127 | } |
| 151 | 128 | |
| 152 | 129 | return $response; |
| 153 | 130 | } |
| 154 | 131 | |
| 155 | - public function upload_content_file( $upload_url, $content_file_data ) { | |
| 132 | + private function upload_content_file( $upload_url, $content_file_data ) { | |
| 156 | 133 | $upload_response = wp_remote_request( $upload_url, [ |
| 157 | 134 | 'method' => 'PUT', |
| 158 | 135 | 'body' => $content_file_data, |
| 159 | 136 | 'headers' => [ |
| @@ -211,32 +188,8 @@ | ||
| 211 | 188 | |
| 212 | 189 | $body .= "--{$boundary}--{$eol}"; |
| 213 | 190 | |
| 214 | 191 | return $body; |
| 215 | - } | |
| 216 | - | |
| 217 | - public function update_kit( $id, array $kit_data ) { | |
| 218 | - $endpoint = 'kits/' . $id; | |
| 219 | - | |
| 220 | - $request = $this->http_request( | |
| 221 | - 'PATCH', | |
| 222 | - $endpoint, | |
| 223 | - [ | |
| 224 | - 'body' => wp_json_encode( $kit_data ), | |
| 225 | - 'headers' => [ | |
| 226 | - 'Content-Type' => 'application/json', | |
| 227 | - ], | |
| 228 | - ], | |
| 229 | - [ | |
| 230 | - 'return_type' => static::HTTP_RETURN_TYPE_ARRAY, | |
| 231 | - ], | |
| 232 | - ); | |
| 233 | - | |
| 234 | - if ( is_wp_error( $request ) ) { | |
| 235 | - return false; | |
| 236 | - } | |
| 237 | - | |
| 238 | - return true; | |
| 239 | 192 | } |
| 240 | 193 | |
| 241 | 194 | protected function init() {} |
| 242 | 195 | } |