PluginProbe
Elementor Website Builder – more than just a page builder / 4.0.7
Elementor Website Builder – more than just a page builder v4.0.7
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-library / connect / cloud-library.php

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

350 lines 9.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Modules\CloudLibrary\Connect;
3
4 use Elementor\Core\Common\Modules\Connect\Apps\Library;
5 use Elementor\Core\Utils\Exceptions;
6 use Elementor\Modules\CloudLibrary\Render_Mode_Preview;
7 use Elementor\TemplateLibrary\Source_Cloud;
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit; // Exit if accessed directly.
11 }
12
13 class Cloud_Library extends Library {
14 public function get_title(): string {
15 return esc_html__( 'Cloud Library', 'elementor' );
16 }
17
18 protected function get_api_url(): string {
19 return 'https://cloud-library.prod.builder.elementor.red/api/v1/cloud-library';
20 }
21
22 public function get_resources( $args = [] ): array {
23 $templates = [];
24
25 $endpoint = 'resources';
26
27 $query_string = http_build_query( [
28 'limit' => isset( $args['limit'] ) ? (int) $args['limit'] : null,
29 'offset' => isset( $args['offset'] ) ? (int) $args['offset'] : null,
30 'search' => isset( $args['search'] ) ? $args['search'] : null,
31 'parentId' => isset( $args['parentId'] ) ? $args['parentId'] : null,
32 'templateType' => isset( $args['templateType'] ) ? $args['templateType'] : null,
33 'orderBy' => isset( $args['orderby'] ) ? $args['orderby'] : null,
34 'order' => isset( $args['order'] ) ? strtoupper( $args['order'] ) : null,
35 ] );
36
37 $endpoint .= '?' . $query_string;
38
39 $cloud_templates = $this->http_request( 'GET', $endpoint, $args, [
40 'return_type' => static::HTTP_RETURN_TYPE_ARRAY,
41 ] );
42
43 if ( is_wp_error( $cloud_templates ) || ! is_array( $cloud_templates['data'] ) ) {
44 return $templates;
45 }
46
47 foreach ( $cloud_templates['data'] as $cloud_template ) {
48 $templates[] = $this->prepare_template( $cloud_template );
49 }
50
51 return [
52 'templates' => $templates,
53 'total' => $cloud_templates['total'],
54 ];
55 }
56
57 /**
58 * @return array|\WP_Error
59 */
60 public function get_resource( array $args ) {
61 return $this->http_request( 'GET', 'resources/' . $args['id'], $args, [
62 'return_type' => static::HTTP_RETURN_TYPE_ARRAY,
63 ] );
64 }
65
66 protected function prepare_template( array $template_data ): array {
67 $template = [
68 'template_id' => $template_data['id'],
69 'source' => 'cloud',
70 'type' => $template_data['templateType'],
71 'subType' => $template_data['type'],
72 'title' => $template_data['title'],
73 'status' => $template_data['status'],
74 'author' => $template_data['authorEmail'],
75 'human_date' => date_i18n( get_option( 'date_format' ), strtotime( $template_data['createdAt'] ) ),
76 'export_link' => $this->get_export_link( $template_data['id'] ),
77 'hasPageSettings' => $template_data['hasPageSettings'],
78 'parentId' => $template_data['parentId'],
79 'preview_url' => esc_url_raw( $template_data['previewUrl'] ?? '' ),
80 'generate_preview_url' => esc_url_raw( $this->generate_preview_url( $template_data ) ?? '' ),
81 ];
82
83 if ( ! empty( $template_data['content'] ) ) {
84 $template['content'] = $template_data['content'];
85 }
86
87 return $template;
88 }
89
90 private function generate_preview_url( $template_data ): ?string {
91 if ( ! empty( $template_data['previewUrl'] ) ||
92 Source_Cloud::FOLDER_RESOURCE_TYPE === $template_data['type'] ||
93 empty( $template_data['id'] )
94 ) {
95 return null;
96 }
97
98 $template_id = $template_data['id'];
99
100 $query_args = [
101 'render_mode_nonce' => wp_create_nonce( 'render_mode_' . $template_id ),
102 'template_id' => $template_id,
103 'render_mode' => Render_Mode_Preview::MODE,
104 ];
105
106 return set_url_scheme( add_query_arg( $query_args, site_url() ) );
107 }
108
109 private function get_export_link( $template_id ) {
110 return add_query_arg(
111 [
112 'action' => 'elementor_library_direct_actions',
113 'library_action' => 'export_template',
114 'source' => 'cloud',
115 '_nonce' => wp_create_nonce( 'elementor_ajax' ),
116 'template_id' => $template_id,
117 ],
118 admin_url( 'admin-ajax.php' )
119 );
120 }
121
122 public function post_resource( $data ): array {
123 $resource = [
124 'headers' => [
125 'Content-Type' => 'application/json',
126 ],
127 'body' => wp_json_encode( $data ),
128 ];
129
130 return $this->http_request( 'POST', 'resources', $resource, [
131 'return_type' => static::HTTP_RETURN_TYPE_ARRAY,
132 ] );
133 }
134
135 public function post_bulk_resources( $data ): array {
136 $resource = [
137 'headers' => [
138 'Content-Type' => 'application/json',
139 ],
140 'body' => wp_json_encode( $data ),
141 'timeout' => 120,
142 ];
143
144 return $this->http_request( 'POST', 'resources/bulk', $resource, [
145 'return_type' => static::HTTP_RETURN_TYPE_ARRAY,
146 ] );
147 }
148
149 public function delete_resource( $template_id ): bool {
150 $request = $this->http_request( 'DELETE', 'resources/' . $template_id );
151
152 if ( isset( $request->errors[204] ) && 'No Content' === $request->errors[204][0] ) {
153 return true;
154 }
155
156 if ( is_wp_error( $request ) ) {
157 return false;
158 }
159
160 return true;
161 }
162
163 public function update_resource( array $template_data ) {
164 $endpoint = 'resources/' . $template_data['id'];
165
166 $request = $this->http_request( 'PATCH', $endpoint, [ 'body' => $template_data ], [
167 'return_type' => static::HTTP_RETURN_TYPE_ARRAY,
168 ] );
169
170 if ( is_wp_error( $request ) ) {
171 return false;
172 }
173
174 return true;
175 }
176
177 public function update_resource_preview( $template_id, $file_data ) {
178 $endpoint = 'resources/' . $template_id . '/preview';
179
180 $boundary = wp_generate_password( 24, false );
181
182 $headers = [
183 'Content-Type' => 'multipart/form-data; boundary=' . $boundary,
184 ];
185
186 $body = $this->generate_multipart_payload( $file_data, $boundary, $template_id . '_preview.png' );
187
188 $payload = [
189 'headers' => $headers,
190 'body' => $body,
191 ];
192
193 $response = $this->http_request( 'PATCH', $endpoint, $payload, [
194 'return_type' => static::HTTP_RETURN_TYPE_ARRAY,
195 'timeout' => 120,
196 ]);
197
198 if ( is_wp_error( $response ) || empty( $response['preview_url'] ) ) {
199 $error_message = esc_html__( 'Failed to save preview.', 'elementor' );
200
201 throw new \Exception( $error_message, Exceptions::INTERNAL_SERVER_ERROR ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
202 }
203
204 return $response['preview_url'];
205 }
206
207 public function mark_preview_as_failed( $template_id, $error ) {
208 $endpoint = 'resources/' . $template_id . '/preview';
209
210 $payload = [
211 'body' => [
212 'error' => $error,
213 ],
214 ];
215
216 $response = $this->http_request( 'PATCH', $endpoint, $payload, [
217 'return_type' => static::HTTP_RETURN_TYPE_ARRAY,
218 ]);
219
220 if ( is_wp_error( $response ) ) {
221 $error_message = esc_html__( 'Failed to mark preview as failed.', 'elementor' );
222
223 throw new \Exception( $error_message, Exceptions::INTERNAL_SERVER_ERROR ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
224 }
225
226 return $response;
227 }
228
229 /**
230 * @param $file_data
231 * @param $boundary
232 * @param $file_name
233 * @return string
234 */
235 private function generate_multipart_payload( $file_data, $boundary, $file_name ): string {
236 $payload = '';
237
238 // Append the file
239 $payload .= "--{$boundary}\r\n";
240 $payload .= 'Content-Disposition: form-data; name="file"; filename="' . esc_attr( $file_name ) . "\"\r\n";
241 $payload .= "Content-Type: image/png\r\n\r\n";
242 $payload .= $file_data . "\r\n";
243 $payload .= "--{$boundary}--\r\n";
244
245 return $payload;
246 }
247
248 public function bulk_delete_resources( $template_ids ) {
249 $endpoint = 'resources/bulk';
250
251 $endpoint .= '?ids=' . implode( ',', $template_ids );
252
253 $response = $this->http_request( 'DELETE', $endpoint, [], [
254 'return_type' => static::HTTP_RETURN_TYPE_ARRAY,
255 ] );
256
257 if ( isset( $response->errors[204] ) ) {
258 return true;
259 }
260
261 if ( is_wp_error( $response ) ) {
262 return $response;
263 }
264
265 return true;
266 }
267
268 public function bulk_undo_delete_resources( $template_ids ) {
269 $endpoint = 'resources/bulk-delete/undo';
270
271 $body = wp_json_encode( [ 'ids' => $template_ids ] );
272
273 $request = [
274 'headers' => [
275 'Content-Type' => 'application/json',
276 ],
277 'body' => $body,
278 ];
279
280 $response = $this->http_request( 'POST', $endpoint, $request, [
281 'return_type' => static::HTTP_RETURN_TYPE_ARRAY,
282 ] );
283
284 if ( is_wp_error( $response ) ) {
285 return $response;
286 }
287
288 return true;
289 }
290
291 public function get_bulk_resources_with_content( $args = [] ): array {
292 $templates = [];
293
294 $endpoint = 'resources/bulk';
295
296 $query_string = http_build_query( [
297 'ids' => implode( ',', $args['from_template_id'] ),
298 ] );
299
300 $endpoint .= '?' . $query_string;
301
302 $cloud_templates = $this->http_request( 'GET', $endpoint, $args, [
303 'return_type' => static::HTTP_RETURN_TYPE_ARRAY,
304 ] );
305
306 if ( is_wp_error( $cloud_templates ) || ! is_array( $cloud_templates ) ) {
307 return $templates;
308 }
309
310 foreach ( $cloud_templates as $cloud_template ) {
311 $templates[] = $this->prepare_template( $cloud_template );
312 }
313
314 return $templates;
315 }
316
317 public function bulk_move_templates( array $template_data ) {
318 $endpoint = 'resources/move';
319 $args = [
320 'body' => wp_json_encode( $template_data ),
321 'headers' => [ 'Content-Type' => 'application/json' ],
322 ];
323
324 $request = $this->http_request( 'PATCH', $endpoint, $args, [
325 'return_type' => static::HTTP_RETURN_TYPE_ARRAY,
326 ] );
327
328 if ( is_wp_error( $request ) ) {
329 return false;
330 }
331
332 return true;
333 }
334
335 /**
336 * @return array|\WP_Error
337 */
338 public function get_quota() {
339 if ( ! $this->is_connected() ) {
340 return new \WP_Error( 'not_connected', esc_html__( 'Not connected', 'elementor' ) );
341 }
342
343 return $this->http_request( 'GET', 'quota', [], [
344 'return_type' => static::HTTP_RETURN_TYPE_ARRAY,
345 ] );
346 }
347
348 protected function init() {}
349 }
350