PluginProbe
Elementor Website Builder – more than just a page builder / 3.35.9
Elementor Website Builder – more than just a page builder v3.35.9
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / modules / cloud-kit-library / connect / cloud-kits.php

cloud-kits.php in Elementor Website Builder – more than just a page builder 3.35.9, at modules/cloud-kit-library/connect/cloud-kits.php

243 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Modules\CloudKitLibrary\Connect;
3
4 use Elementor\Core\Common\Modules\Connect\Apps\Library;
5 use Elementor\Core\Utils\Exceptions;
6
7 if ( ! defined( 'ABSPATH' ) ) {
8 exit; // Exit if accessed directly.
9 }
10
11 class Cloud_Kits extends Library {
12 const THRESHOLD_UNLIMITED = -1;
13 const FAILED_TO_FETCH_QUOTA_KEY = 'failed-to-fetch-quota';
14
15 const FAILED_TO_UPLOAD_KIT = 'cloud-upload-failed';
16
17 const INSUFFICIENT_QUOTA_KEY = 'insufficient-quota';
18
19 const INSUFFICIENT_STORAGE_QUOTA = 'insufficient-storage-quota';
20
21 public function get_title() {
22 return esc_html__( 'Cloud Kits', 'elementor' );
23 }
24
25 protected function get_api_url(): string {
26 return 'https://cloud-library.prod.builder.elementor.red/api/v1/cloud-library';
27 }
28
29 /**
30 * @return array|\WP_Error
31 */
32 public function get_all( $args = [] ) {
33 return $this->http_request( 'GET', 'kits', [], [
34 'return_type' => static::HTTP_RETURN_TYPE_ARRAY,
35 ] );
36 }
37
38 /**
39 * @return array|\WP_Error
40 */
41 public function get_quota() {
42 if ( ! $this->is_connected() ) {
43 return new \WP_Error( 'not_connected', esc_html__( 'Not connected', 'elementor' ) );
44 }
45
46 return $this->http_request( 'GET', 'quota/kits', [], [
47 'return_type' => static::HTTP_RETURN_TYPE_ARRAY,
48 ] );
49 }
50
51 public function validate_quota( $quota ) {
52 if ( is_wp_error( $quota ) ) {
53 throw new \Error( static::FAILED_TO_FETCH_QUOTA_KEY ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
54 }
55
56 $is_unlimited = self::THRESHOLD_UNLIMITED === $quota['threshold'];
57 $has_quota = $quota['currentUsage'] < $quota['threshold'];
58
59 if ( ! $is_unlimited && ! $has_quota ) {
60 throw new \Error( static::INSUFFICIENT_QUOTA_KEY ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
61 }
62 }
63
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 public function check_eligibility() {
81 $quota = $this->get_quota();
82
83 if ( is_wp_error( $quota ) ) {
84 return [
85 'is_eligible' => false,
86 'subscription_id' => '',
87 ];
88 }
89
90 return [
91 'is_eligible' => isset( $quota['threshold'] ) && 0 !== $quota['threshold'],
92 'subscription_id' => ! empty( $quota['subscriptionId'] ) ? $quota['subscriptionId'] : '',
93 ];
94 }
95
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 );
100
101 $endpoint = 'kits';
102
103 $boundary = wp_generate_password( 24, false );
104
105 $headers = [
106 'Content-Type' => 'multipart/form-data; boundary=' . $boundary,
107 ];
108
109 $body = $this->create_multipart_body(
110 [
111 'title' => $title,
112 'description' => $description,
113 'includes' => wp_json_encode( $includes ),
114 'mediaFormat' => $media_format,
115 ],
116 [
117 'previewFile' => [
118 'filename' => 'preview.png',
119 'content' => $preview_file_data,
120 'content_type' => 'image/png',
121 ],
122 ],
123 $boundary
124 );
125
126 $payload = [
127 'headers' => $headers,
128 'body' => $body,
129 'timeout' => 120,
130 ];
131
132 $response = $this->http_request( 'POST', $endpoint, $payload, [
133 'return_type' => static::HTTP_RETURN_TYPE_ARRAY,
134 ] );
135
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
138 }
139
140 if ( empty( $response['uploadUrl'] ) ) {
141 $this->delete_kit( $response['id'] );
142 throw new \Exception( static::FAILED_TO_UPLOAD_KIT, Exceptions::INTERNAL_SERVER_ERROR ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
143 }
144
145 $upload_success = $this->upload_content_file( $response['uploadUrl'], $content_file_data );
146
147 if ( ! $upload_success ) {
148 $this->delete_kit( $response['id'] );
149 throw new \Exception( static::FAILED_TO_UPLOAD_KIT, Exceptions::INTERNAL_SERVER_ERROR ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
150 }
151
152 return $response;
153 }
154
155 public function upload_content_file( $upload_url, $content_file_data ) {
156 $upload_response = wp_remote_request( $upload_url, [
157 'method' => 'PUT',
158 'body' => $content_file_data,
159 'headers' => [
160 'Content-Type' => 'application/zip',
161 'Content-Length' => strlen( $content_file_data ),
162 ],
163 'timeout' => 120,
164 ] );
165
166 if ( is_wp_error( $upload_response ) ) {
167 return false;
168 }
169
170 $response_code = wp_remote_retrieve_response_code( $upload_response );
171
172 return $response_code >= 200 && $response_code < 300;
173 }
174
175 public function get_kit( array $args ) {
176 $args = array_merge_recursive( $args, [
177 'timeout' => 60, // just in case if zip is big
178 ] );
179
180 return $this->http_request( 'GET', 'kits/' . $args['id'], $args, [
181 'return_type' => static::HTTP_RETURN_TYPE_ARRAY,
182 ] );
183 }
184
185 public function delete_kit( int $id ) {
186 return $this->http_request( 'DELETE', 'kits/' . $id, [], [
187 'return_type' => static::HTTP_RETURN_TYPE_ARRAY,
188 ] );
189 }
190
191 private function create_multipart_body( $fields, $files, $boundary ): string {
192 $eol = "\r\n";
193 $body = '';
194
195 foreach ( $fields as $name => $value ) {
196 $body .= "--{$boundary}{$eol}";
197 $body .= "Content-Disposition: form-data; name=\"{$name}\"{$eol}{$eol}";
198 $body .= "{$value}{$eol}";
199 }
200
201 foreach ( $files as $name => $file ) {
202 $filename = basename( $file['filename'] );
203 $content_type = $file['content_type'];
204 $content = $file['content'];
205
206 $body .= "--{$boundary}{$eol}";
207 $body .= "Content-Disposition: form-data; name=\"{$name}\"; filename=\"{$filename}\"{$eol}";
208 $body .= "Content-Type: {$content_type}{$eol}{$eol}";
209 $body .= $content . $eol;
210 }
211
212 $body .= "--{$boundary}--{$eol}";
213
214 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 }
240
241 protected function init() {}
242 }
243