PluginProbe
Elementor Website Builder – more than just a page builder / 4.1.0
Elementor Website Builder – more than just a page builder v4.1.0
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 / includes / template-library / sources / cloud.php

cloud.php in Elementor Website Builder – more than just a page builder 4.1.0, at includes/template-library/sources/cloud.php

543 lines 15.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\TemplateLibrary;
3
4 use Elementor\Core\Base\Document;
5 use Elementor\Core\Utils\Exceptions;
6 use Elementor\Modules\CloudLibrary\Connect\Cloud_Library;
7 use Elementor\Modules\CloudLibrary\Documents\Cloud_Template_Preview;
8 use Elementor\Plugin;
9 use Elementor\DB;
10
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit; // Exit if accessed directly.
13 }
14
15 class Source_Cloud extends Source_Base {
16 const FOLDER_RESOURCE_TYPE = 'FOLDER';
17 const TEMPLATE_RESOURCE_TYPE = 'TEMPLATE';
18
19 protected function get_app(): Cloud_Library {
20 $cloud_library_app = Plugin::$instance->common->get_component( 'connect' )->get_app( 'cloud-library' );
21
22 if ( ! $cloud_library_app ) {
23 $error_message = esc_html__( 'Cloud-Library is not instantiated.', 'elementor' );
24
25 throw new \Exception( $error_message, Exceptions::FORBIDDEN ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
26 }
27
28 return $cloud_library_app;
29 }
30
31 public function get_id(): string {
32 return 'cloud';
33 }
34
35 public function get_title(): string {
36 return esc_html__( 'Cloud Library', 'elementor' );
37 }
38
39 public function register_data() {}
40
41 public function supports_quota(): bool {
42 return true;
43 }
44
45 public function get_items( $args = [] ) {
46 return $this->get_app()->get_resources( $args );
47 }
48
49 public function get_item_children( array $args = [] ) {
50 return $this->get_app()->get_resources( [ 'parentId' => $args['template_id'] ] );
51 }
52
53 public function get_item( $id ) {
54 return $this->get_app()->get_resource( [ 'id' => $id ] );
55 }
56
57 public function get_data( array $args ) {
58 $data = $this->get_app()->get_resource( [ 'id' => $args['template_id'] ] );
59
60 if ( is_wp_error( $data ) || empty( $data['content'] ) ) {
61 return $data;
62 }
63
64 $decoded_data = json_decode( $data['content'], true );
65 $data['content'] = $decoded_data['content'];
66
67 Plugin::$instance->uploads_manager->set_elementor_upload_state( true );
68
69 $data['content'] = $this->replace_elements_ids( $data['content'] );
70 $data['content'] = $this->process_export_import_content( $data['content'], 'on_import' );
71
72 if ( ! empty( $args['editor_post_id'] ) ) {
73 $post_id = $args['editor_post_id'];
74 $document = Plugin::$instance->documents->get( $post_id );
75 if ( $document ) {
76 $data['content'] = $document->get_elements_raw_data( $data['content'], true );
77 }
78 }
79
80 if ( ! empty( $args['with_page_settings'] ) ) {
81 $data['page_settings'] = $decoded_data['page_settings'];
82 }
83
84 $snapshots = apply_filters(
85 'elementor/template_library/get_data/extract_snapshots',
86 [],
87 $decoded_data,
88 $data
89 );
90
91 static::attach_global_styles_to_data_static( $data, $snapshots );
92
93 // After the upload complete, set the elementor upload state back to false
94 Plugin::$instance->uploads_manager->set_elementor_upload_state( false );
95
96 return $data;
97 }
98
99 public function delete_template( $template_id ) {
100 return $this->get_app()->delete_resource( $template_id );
101 }
102
103 public function save_item( $template_data ): int {
104 $app = $this->get_app();
105
106 $resource_data = $this->format_resource_item_for_create( $template_data );
107
108 $response = $app->post_resource( $resource_data );
109
110 return (int) $response['id'];
111 }
112
113 private function format_resource_item_for_create( $template_data ) {
114 $content_payload = [
115 'content' => $template_data['content'],
116 'page_settings' => $template_data['page_settings'],
117 ];
118
119 if ( is_array( $template_data['content'] ?? null ) ) {
120 $this->attach_global_styles_to_data( $content_payload, $template_data['content'], 0 );
121 }
122
123 return [
124 'title' => $template_data['title'] ?? esc_html__( '(no title)', 'elementor' ),
125 'type' => self::TEMPLATE_RESOURCE_TYPE,
126 'templateType' => $template_data['type'],
127 'parentId' => ! empty( $template_data['parentId'] ) ? (int) $template_data['parentId'] : null,
128 'content' => wp_json_encode( $content_payload ),
129 'hasPageSettings' => ! empty( $template_data['page_settings'] ),
130 ];
131 }
132
133 public function save_folder( array $folder_data = [] ) {
134 $app = $this->get_app();
135
136 $resource_data = [
137 'title' => $folder_data['title'] ?? esc_html__( 'New Folder', 'elementor' ),
138 'type' => self::FOLDER_RESOURCE_TYPE,
139 'templateType' => 'folder',
140 'parentId' => null,
141 ];
142
143 $response = $app->post_resource( $resource_data );
144
145 return (int) $response['id'];
146 }
147
148 public function update_item( $template_data ) {
149 return $this->get_app()->update_resource( $template_data );
150 }
151
152 public function search_templates( array $args = [] ) {
153 return $this->get_app()->get_resources( $args );
154 }
155
156 public function export_template( $id ) {
157 $data = $this->get_app()->get_resource( [ 'id' => $id ] );
158
159 if ( is_wp_error( $data ) ) {
160 return new \WP_Error( 'export_template_error', 'An error has occurred' );
161 }
162
163 if ( static::TEMPLATE_RESOURCE_TYPE === $data['type'] ) {
164 $this->handle_export_file( $data );
165 }
166
167 if ( static::FOLDER_RESOURCE_TYPE === $data['type'] ) {
168 $this->handle_export_folder( $id );
169 }
170 }
171
172 protected function handle_export_file( array $data ): void {
173 $file_data = $this->prepare_template_export( $data );
174
175 if ( is_wp_error( $file_data ) ) {
176 return;
177 }
178
179 $this->send_file_headers( $file_data['name'], strlen( $file_data['content'] ) );
180
181 $this->serve_file( $file_data['content'] );
182 }
183
184 protected function handle_export_folder( int $folder_id ) {
185 $data = $this->get_item_children( [ 'template_id' => $folder_id ] );
186
187 if ( empty( $data['templates'] ) ) {
188 throw new \Exception( 'Folder does not have any templates to export' );
189 }
190
191 $template_ids = array_map( fn( $template ) => $template['template_id'], $data['templates'] );
192
193 $this->export_multiple_templates( $template_ids );
194 }
195
196 private function prepare_template_export( $data ) {
197 if ( empty( $data['content'] ) ) {
198 throw new \Exception( 'Template data not found' );
199 }
200
201 $data['content'] = json_decode( $data['content'], true );
202
203 if ( empty( $data['content']['content'] ) ) {
204 throw new \Exception( 'The template is empty' );
205 }
206
207 $export_data = [
208 'content' => $data['content']['content'],
209 'page_settings' => $data['content']['page_settings'] ?? [],
210 'version' => DB::DB_VERSION,
211 'title' => $data['title'],
212 'type' => $data['templateType'],
213 ];
214
215 $this->attach_global_styles_to_data(
216 $export_data,
217 $export_data['content'],
218 (int) ( $data['id'] ?? 0 ),
219 [
220 'global_classes' => $data['content']['global_classes'] ?? null,
221 'global_variables' => $data['content']['global_variables'] ?? null,
222 ]
223 );
224
225 return [
226 'name' => 'elementor-' . $data['id'] . '-' . gmdate( 'Y-m-d' ) . '.json',
227 'content' => wp_json_encode( $export_data ),
228 ];
229 }
230
231 public function export_multiple_templates( array $template_ids ) {
232 $files = [];
233 $temp_path = Plugin::$instance->uploads_manager->create_unique_dir();
234
235 foreach ( $template_ids as $template_id ) {
236 $files[] = $this->get_file_item( $template_id, $temp_path );
237 }
238
239 if ( empty( $files ) ) {
240 throw new \Exception( 'There are no files to export (probably all the requested templates are empty).' );
241 }
242
243 list( $zip_archive_filename, $zip_complete_path ) = $this->handle_zip_file( $temp_path, $files );
244
245 $this->send_file_headers( $zip_archive_filename, $this->filesize( $zip_complete_path ) );
246
247 $this->serve_zip( $zip_complete_path );
248
249 Plugin::$instance->uploads_manager->remove_file_or_dir( $temp_path );
250 }
251
252 protected function handle_zip_file( string $temp_path, array $files ): array {
253 if ( ! class_exists( 'ZipArchive' ) ) {
254 throw new \Error( 'ZipArchive module missing' );
255 }
256
257 $zip_archive_filename = 'elementor-templates-' . gmdate( 'Y-m-d' ) . '.zip';
258
259 $zip_archive = new \ZipArchive();
260
261 $zip_complete_path = $temp_path . '/' . $zip_archive_filename;
262
263 $zip_archive->open( $zip_complete_path, \ZipArchive::CREATE );
264
265 foreach ( $files as $file ) {
266 $zip_archive->addFile( $file['path'], $file['name'] );
267 }
268
269 $zip_archive->close();
270
271 return [ $zip_archive_filename, $zip_complete_path ];
272 }
273
274 private function get_file_item( $template_id, string $temp_path ) {
275 $data = $this->get_app()->get_resource( [ 'id' => $template_id ] );
276 $file_data = $this->prepare_template_export( $data );
277
278 if ( is_wp_error( $file_data ) ) {
279 return;
280 }
281
282 $complete_path = $temp_path . $file_data['name'];
283
284 $put_contents = file_put_contents( $complete_path, $file_data['content'] );
285
286 if ( ! $put_contents ) {
287 throw new \Exception( sprintf( 'Cannot create file "%s".', esc_html( $file_data['name'] ) ) );
288 }
289
290 return [
291 'path' => $complete_path,
292 'name' => $file_data['name'],
293 ];
294 }
295
296 public function move_template_to_folder( array $args = [] ) {
297 $move_args = [
298 'title' => $args['title'],
299 'id' => $args['from_template_id'],
300 'parentId' => ! empty( $args['parentId'] ) ? (int) $args['parentId'] : '',
301 ];
302
303 return $this->update_item( $move_args );
304 }
305
306 public function move_bulk_templates_to_folder( array $args = [] ) {
307 $move_args = [
308 'ids' => $args['from_template_id'],
309 'parentId' => ! empty( $args['parentId'] ) ? (int) $args['parentId'] : null,
310 ];
311
312 return $this->get_app()->bulk_move_templates( $move_args );
313 }
314
315 public function save_item_preview( $template_id, $data ) {
316 return $this->get_app()->update_resource_preview( $template_id, $data );
317 }
318
319 public function mark_preview_as_failed( $template_id, $data ) {
320 return $this->get_app()->mark_preview_as_failed( $template_id, $data );
321 }
322
323 /**
324 * @param int $template_id
325 * @return Document|\WP_Error
326 * @throws \Exception If the user has no permission or the post is not found.
327 */
328 public function create_document_for_preview( int $template_id ) {
329 if ( ! current_user_can( 'edit_posts' ) ) {
330 return new \WP_Error( Exceptions::FORBIDDEN, esc_html__( 'You do not have permission to create preview documents.', 'elementor' ) );
331 }
332
333 $cloud_library_app = $this->get_app();
334
335 $template = $cloud_library_app->get_resource( [ 'id' => $template_id ] );
336
337 if ( is_wp_error( $template ) ) {
338 $error_message = $template->get_error_message();
339 throw new \Exception( esc_html( $error_message ), Exceptions::FORBIDDEN ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
340 }
341
342 $decoded_content = json_decode( $template['content'], true );
343
344 return $this->save_document_for_preview( [
345 'content' => $decoded_content['content'],
346 'page_settings' => $decoded_content['page_settings'],
347 ] );
348 }
349
350 protected function save_document_for_preview( $template_content ) {
351 $template_data = [
352 'title' => esc_html__( '(no title)', 'elementor' ),
353 'page_settings' => $template_content['page_settings'] ?? [],
354 'status' => 'draft',
355 'type' => 'container',
356 ];
357
358 $document = Plugin::$instance->documents->create(
359 Cloud_Template_Preview::TYPE,
360 [
361 'post_title' => $template_data['title'],
362 'post_status' => $template_data['status'],
363 ]
364 );
365
366 if ( is_wp_error( $document ) ) {
367 wp_die();
368 }
369
370 $template_data['content'] = $this->replace_elements_ids( $template_content['content'] );
371
372 $document->save( [
373 'elements' => $template_data['content'],
374 'settings' => $template_data['page_settings'],
375 ] );
376
377 do_action( 'elementor/template-library/after_save_template', $document->get_main_id(), $template_data );
378 do_action( 'elementor/template-library/after_update_template', $document->get_main_id(), $template_data );
379
380 return $document;
381 }
382
383 public function bulk_delete_items( array $template_ids ) {
384 return $this->get_app()->bulk_delete_resources( $template_ids );
385 }
386
387
388 public function bulk_undo_delete_items( array $template_ids ) {
389 return $this->get_app()->bulk_undo_delete_resources( $template_ids );
390 }
391
392 public function save_bulk_items( array $data = [] ) {
393 $items = [];
394
395 foreach ( $data as $template_data ) {
396 $items[] = $this->format_resource_item_for_create( $template_data );
397 }
398
399 return $this->get_app()->post_bulk_resources( $items );
400 }
401
402 public function get_bulk_items( array $args = [] ) {
403 return $this->get_app()->get_bulk_resources_with_content( $args );
404 }
405
406 public function get_quota() {
407 return $this->get_app()->get_quota();
408 }
409
410 public function import_template( $name, $path, $import_mode = 'match_site' ) {
411 if ( empty( $path ) ) {
412 return new \WP_Error( 'file_error', 'Please upload a file to import' );
413 }
414
415 Plugin::$instance->uploads_manager->set_elementor_upload_state( true );
416
417 $items = [];
418
419 $quota = $this->get_quota();
420
421 if ( is_wp_error( $quota ) ) {
422 return $quota;
423 }
424
425 if ( $quota['currentUsage'] >= $quota['threshold'] ) {
426 return new \WP_Error( 'quota_error', 'The upload failed because you’ve saved the maximum templates already.' );
427 }
428
429 if ( 'zip' === pathinfo( $name, PATHINFO_EXTENSION ) ) {
430 $extracted_files = Plugin::$instance->uploads_manager->extract_and_validate_zip( $path, [ 'json' ] );
431
432 if ( is_wp_error( $extracted_files ) ) {
433 Plugin::$instance->uploads_manager->remove_file_or_dir( $extracted_files['extraction_directory'] );
434
435 return $extracted_files;
436 }
437
438 $items_to_save = [];
439
440 foreach ( $extracted_files['files'] as $file_path ) {
441 // Skip macOS metadata files and folders
442 if ( false !== strpos( $file_path, '__MACOSX' ) || '.' === basename( $file_path )[0] ) {
443 continue;
444 }
445
446 $prepared = $this->prepare_import_template_data( $file_path, $import_mode );
447
448 if ( is_wp_error( $prepared ) ) {
449 // Skip failed templates
450 continue;
451 }
452
453 $items_to_save[] = $this->format_resource_item_for_create( $prepared );
454 }
455
456 $is_quota_valid = $this->validate_quota( $items_to_save );
457
458 if ( is_wp_error( $is_quota_valid ) ) {
459 return $is_quota_valid;
460 }
461
462 if ( ! $is_quota_valid ) {
463 return new \WP_Error( 'quota_error', 'The upload failed because it will pass the maximum templates you can save.' );
464 }
465
466 $items = $this->get_app()->post_bulk_resources( $items_to_save );
467
468 Plugin::$instance->uploads_manager->remove_file_or_dir( $extracted_files['extraction_directory'] );
469 } else {
470 $prepared = $this->prepare_import_template_data( $path, $import_mode );
471
472 if ( is_wp_error( $prepared ) ) {
473 return $prepared;
474 }
475
476 $is_quota_valid = $this->validate_quota( [ $prepared ] );
477
478 if ( is_wp_error( $is_quota_valid ) ) {
479 return $is_quota_valid;
480 }
481
482 if ( ! $is_quota_valid ) {
483 return new \WP_Error( 'quota_error', 'The upload failed because it will pass the maximum templates you can save.' );
484 }
485
486 $item = $this->get_app()->post_resource( $this->format_resource_item_for_create( $prepared ) );
487
488 if ( is_wp_error( $item ) ) {
489 return $item;
490 }
491
492 $items[] = $item;
493 }
494
495 return $items;
496 }
497
498 public function validate_quota( $items ) {
499 $quota = $this->get_quota();
500
501 if ( is_wp_error( $quota ) ) {
502 return $quota;
503 }
504
505 return $quota['currentUsage'] + count( $items ) <= $quota['threshold'];
506 }
507
508 public function format_args_for_bulk_action( $args ) {
509 $templates = $this->get_bulk_items( $args );
510 $bulk_args = [];
511
512 foreach ( $templates as $template ) {
513 $content = json_decode( $template['content'], true );
514
515 $bulk_args[] = array_merge(
516 $args,
517 [
518 'title' => $template['title'],
519 'type' => $template['type'],
520 'content' => $content['content'],
521 'page_settings' => $content['page_settings'],
522 ]
523 );
524 }
525
526 return $bulk_args;
527 }
528
529 public function format_args_for_single_action( $args ) {
530 $data = $this->get_item( $args['from_template_id'] );
531
532 if ( is_wp_error( $data ) || empty( $data['content'] ) ) {
533 return new \WP_Error( 'template_error', 'Unable to format template args.' );
534 }
535
536 $decoded_data = json_decode( $data['content'], true );
537 $args['content'] = $decoded_data['content'];
538 $args['page_settings'] = $decoded_data['page_settings'];
539
540 return $args;
541 }
542 }
543