PluginProbe
Elementor Website Builder – more than just a page builder / 3.29.1
Elementor Website Builder – more than just a page builder v3.29.1
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 3.29.1, at modules/cloud-library/connect/cloud-library.php

344 lines 9.0 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 ];
142
143 return $this->http_request( 'POST', 'resources/bulk', $resource, [
144 'return_type' => static::HTTP_RETURN_TYPE_ARRAY,
145 ] );
146 }
147
148 public function delete_resource( $template_id ): bool {
149 $request = $this->http_request( 'DELETE', 'resources/' . $template_id );
150
151 if ( isset( $request->errors[204] ) && 'No Content' === $request->errors[204][0] ) {
152 return true;
153 }
154
155 if ( is_wp_error( $request ) ) {
156 return false;
157 }
158
159 return true;
160 }
161
162 public function update_resource( array $template_data ) {
163 $endpoint = 'resources/' . $template_data['id'];
164
165 $request = $this->http_request( 'PATCH', $endpoint, [ 'body' => $template_data ], [
166 'return_type' => static::HTTP_RETURN_TYPE_ARRAY,
167 ] );
168
169 if ( is_wp_error( $request ) ) {
170 return false;
171 }
172
173 return true;
174 }
175
176 public function update_resource_preview( $template_id, $file_data ) {
177 $endpoint = 'resources/' . $template_id . '/preview';
178
179 $boundary = wp_generate_password( 24, false );
180
181 $headers = [
182 'Content-Type' => 'multipart/form-data; boundary=' . $boundary,
183 ];
184
185 $body = $this->generate_multipart_payload( $file_data, $boundary, $template_id . '_preview.png' );
186
187 $payload = [
188 'headers' => $headers,
189 'body' => $body,
190 ];
191
192 $response = $this->http_request( 'PATCH', $endpoint, $payload, [
193 'return_type' => static::HTTP_RETURN_TYPE_ARRAY,
194 ]);
195
196 if ( is_wp_error( $response ) || empty( $response['preview_url'] ) ) {
197 $error_message = esc_html__( 'Failed to save preview.', 'elementor' );
198
199 throw new \Exception( $error_message, Exceptions::INTERNAL_SERVER_ERROR ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
200 }
201
202 return $response['preview_url'];
203 }
204
205 public function mark_preview_as_failed( $template_id, $error ) {
206 $endpoint = 'resources/' . $template_id . '/preview';
207
208 $payload = [
209 'body' => [
210 'error' => $error,
211 ],
212 ];
213
214 $response = $this->http_request( 'PATCH', $endpoint, $payload, [
215 'return_type' => static::HTTP_RETURN_TYPE_ARRAY,
216 ]);
217
218 if ( is_wp_error( $response ) ) {
219 $error_message = esc_html__( 'Failed to mark preview as failed.', 'elementor' );
220
221 throw new \Exception( $error_message, Exceptions::INTERNAL_SERVER_ERROR ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
222 }
223
224 return $response;
225 }
226
227 /**
228 * @param $file_data
229 * @param $boundary
230 * @param $file_name
231 * @return string
232 */
233 private function generate_multipart_payload( $file_data, $boundary, $file_name ): string {
234 $payload = '';
235
236 // Append the file
237 $payload .= "--{$boundary}\r\n";
238 $payload .= 'Content-Disposition: form-data; name="file"; filename="' . esc_attr( $file_name ) . "\"\r\n";
239 $payload .= "Content-Type: image/png\r\n\r\n";
240 $payload .= $file_data . "\r\n";
241 $payload .= "--{$boundary}--\r\n";
242
243 return $payload;
244 }
245
246 public function bulk_delete_resources( $template_ids ) {
247 $endpoint = 'resources/bulk';
248
249 $endpoint .= '?ids=' . implode( ',', $template_ids );
250
251 $response = $this->http_request( 'DELETE', $endpoint, [], [
252 'return_type' => static::HTTP_RETURN_TYPE_ARRAY,
253 ] );
254
255 if ( isset( $response->errors[204] ) ) {
256 return true;
257 }
258
259 if ( is_wp_error( $response ) ) {
260 return $response;
261 }
262
263 return true;
264 }
265
266 public function bulk_undo_delete_resources( $template_ids ) {
267 $endpoint = 'resources/bulk-delete/undo';
268
269 $body = wp_json_encode( [ 'ids' => $template_ids ] );
270
271 $request = [
272 'headers' => [
273 'Content-Type' => 'application/json',
274 ],
275 'body' => $body,
276 ];
277
278 $response = $this->http_request( 'POST', $endpoint, $request, [
279 'return_type' => static::HTTP_RETURN_TYPE_ARRAY,
280 ] );
281
282 if ( is_wp_error( $response ) ) {
283 return $response;
284 }
285
286 return true;
287 }
288
289 public function get_bulk_resources_with_content( $args = [] ): array {
290 $templates = [];
291
292 $endpoint = 'resources/bulk';
293
294 $query_string = http_build_query( [
295 'ids' => implode( ',', $args['from_template_id'] ),
296 ] );
297
298 $endpoint .= '?' . $query_string;
299
300 $cloud_templates = $this->http_request( 'GET', $endpoint, $args, [
301 'return_type' => static::HTTP_RETURN_TYPE_ARRAY,
302 ] );
303
304 if ( is_wp_error( $cloud_templates ) || ! is_array( $cloud_templates ) ) {
305 return $templates;
306 }
307
308 foreach ( $cloud_templates as $cloud_template ) {
309 $templates[] = $this->prepare_template( $cloud_template );
310 }
311
312 return $templates;
313 }
314
315 public function bulk_move_templates( array $template_data ) {
316 $endpoint = 'resources/move';
317 $args = [
318 'body' => wp_json_encode( $template_data ),
319 'headers' => [ 'Content-Type' => 'application/json' ],
320 ];
321
322 $request = $this->http_request( 'PATCH', $endpoint, $args, [
323 'return_type' => static::HTTP_RETURN_TYPE_ARRAY,
324 ] );
325
326 if ( is_wp_error( $request ) ) {
327 return false;
328 }
329
330 return true;
331 }
332
333 /**
334 * @return array|\WP_Error
335 */
336 public function get_quota() {
337 return $this->http_request( 'GET', 'quota', [], [
338 'return_type' => static::HTTP_RETURN_TYPE_ARRAY,
339 ] );
340 }
341
342 protected function init() {}
343 }
344