PluginProbe
Elementor Website Builder – more than just a page builder / 3.30.2
Elementor Website Builder – more than just a page builder v3.30.2
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 3.30.2, at includes/template-library/sources/cloud.php

479 lines 13.6 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 // After the upload complete, set the elementor upload state back to false
85 Plugin::$instance->uploads_manager->set_elementor_upload_state( false );
86
87 return $data;
88 }
89
90 public function delete_template( $template_id ) {
91 return $this->get_app()->delete_resource( $template_id );
92 }
93
94 public function save_item( $template_data ): int {
95 $app = $this->get_app();
96
97 $resource_data = $this->format_resource_item_for_create( $template_data );
98
99 $response = $app->post_resource( $resource_data );
100
101 return (int) $response['id'];
102 }
103
104 private function format_resource_item_for_create( $template_data ) {
105 return [
106 'title' => $template_data['title'] ?? esc_html__( '(no title)', 'elementor' ),
107 'type' => self::TEMPLATE_RESOURCE_TYPE,
108 'templateType' => $template_data['type'],
109 'parentId' => ! empty( $template_data['parentId'] ) ? (int) $template_data['parentId'] : null,
110 'content' => wp_json_encode( [
111 'content' => $template_data['content'],
112 'page_settings' => $template_data['page_settings'],
113 ] ),
114 'hasPageSettings' => ! empty( $template_data['page_settings'] ),
115 ];
116 }
117
118 public function save_folder( array $folder_data = [] ) {
119 $app = $this->get_app();
120
121 $resource_data = [
122 'title' => $folder_data['title'] ?? esc_html__( 'New Folder', 'elementor' ),
123 'type' => self::FOLDER_RESOURCE_TYPE,
124 'templateType' => 'folder',
125 'parentId' => null,
126 ];
127
128 $response = $app->post_resource( $resource_data );
129
130 return (int) $response['id'];
131 }
132
133 public function update_item( $template_data ) {
134 return $this->get_app()->update_resource( $template_data );
135 }
136
137 public function search_templates( array $args = [] ) {
138 return $this->get_app()->get_resources( $args );
139 }
140
141 public function export_template( $id ) {
142 $data = $this->get_app()->get_resource( [ 'id' => $id ] );
143
144 if ( is_wp_error( $data ) ) {
145 return new \WP_Error( 'export_template_error', 'An error has occured' );
146 }
147
148 if ( static::TEMPLATE_RESOURCE_TYPE === $data['type'] ) {
149 $this->handle_export_file( $data );
150 }
151
152 if ( static::FOLDER_RESOURCE_TYPE === $data['type'] ) {
153 $this->handle_export_folder( $id );
154 }
155 }
156
157 protected function handle_export_file( array $data ): void {
158 $file_data = $this->prepare_template_export( $data );
159
160 if ( is_wp_error( $file_data ) ) {
161 return;
162 }
163
164 $this->send_file_headers( $file_data['name'], strlen( $file_data['content'] ) );
165
166 $this->serve_file( $file_data['content'] );
167 }
168
169 protected function handle_export_folder( int $folder_id ) {
170 $data = $this->get_item_children( [ 'template_id' => $folder_id ] );
171
172 if ( empty( $data['templates'] ) ) {
173 throw new \Exception( 'Folder does not have any templates to export' );
174 }
175
176 $template_ids = array_map( fn( $template ) => $template['template_id'], $data['templates'] );
177
178 $this->export_multiple_templates( $template_ids );
179 }
180
181 private function prepare_template_export( $data ) {
182 if ( empty( $data['content'] ) ) {
183 throw new \Exception( 'Template data not found' );
184 }
185
186 $data['content'] = json_decode( $data['content'], true );
187
188 if ( empty( $data['content']['content'] ) ) {
189 throw new \Exception( 'The template is empty' );
190 }
191
192 $export_data = [
193 'content' => $data['content']['content'],
194 'page_settings' => $data['content']['page_settings'] ?? [],
195 'version' => DB::DB_VERSION,
196 'title' => $data['title'],
197 'type' => $data['templateType'],
198 ];
199
200 return [
201 'name' => 'elementor-' . $data['id'] . '-' . gmdate( 'Y-m-d' ) . '.json',
202 'content' => wp_json_encode( $export_data ),
203 ];
204 }
205
206 public function export_multiple_templates( array $template_ids ) {
207 $files = [];
208 $temp_path = Plugin::$instance->uploads_manager->create_unique_dir();
209
210 foreach ( $template_ids as $template_id ) {
211 $files[] = $this->get_file_item( $template_id, $temp_path );
212 }
213
214 if ( empty( $files ) ) {
215 throw new \Exception( 'There are no files to export (probably all the requested templates are empty).' );
216 }
217
218 list( $zip_archive_filename, $zip_complete_path ) = $this->handle_zip_file( $temp_path, $files );
219
220 $this->send_file_headers( $zip_archive_filename, $this->filesize( $zip_complete_path ) );
221
222 $this->serve_zip( $zip_complete_path );
223
224 Plugin::$instance->uploads_manager->remove_file_or_dir( $temp_path );
225 }
226
227 protected function handle_zip_file( string $temp_path, array $files ): array {
228 if ( ! class_exists( 'ZipArchive' ) ) {
229 throw new \Error( 'ZipArchive module missing' );
230 }
231
232 $zip_archive_filename = 'elementor-templates-' . gmdate( 'Y-m-d' ) . '.zip';
233
234 $zip_archive = new \ZipArchive();
235
236 $zip_complete_path = $temp_path . '/' . $zip_archive_filename;
237
238 $zip_archive->open( $zip_complete_path, \ZipArchive::CREATE );
239
240 foreach ( $files as $file ) {
241 $zip_archive->addFile( $file['path'], $file['name'] );
242 }
243
244 $zip_archive->close();
245
246 return [ $zip_archive_filename, $zip_complete_path ];
247 }
248
249 private function get_file_item( $template_id, string $temp_path ) {
250 $data = $this->get_app()->get_resource( [ 'id' => $template_id ] );
251 $file_data = $this->prepare_template_export( $data );
252
253 if ( is_wp_error( $file_data ) ) {
254 return;
255 }
256
257 $complete_path = $temp_path . $file_data['name'];
258
259 $put_contents = file_put_contents( $complete_path, $file_data['content'] );
260
261 if ( ! $put_contents ) {
262 throw new \Exception( sprintf( 'Cannot create file "%s".', esc_html( $file_data['name'] ) ) );
263 }
264
265 return [
266 'path' => $complete_path,
267 'name' => $file_data['name'],
268 ];
269 }
270
271 public function move_template_to_folder( array $args = [] ) {
272 $move_args = [
273 'title' => $args['title'],
274 'id' => $args['from_template_id'],
275 'parentId' => ! empty( $args['parentId'] ) ? (int) $args['parentId'] : '',
276 ];
277
278 return $this->update_item( $move_args );
279 }
280
281 public function move_bulk_templates_to_folder( array $args = [] ) {
282 $move_args = [
283 'ids' => $args['from_template_id'],
284 'parentId' => ! empty( $args['parentId'] ) ? (int) $args['parentId'] : null,
285 ];
286
287 return $this->get_app()->bulk_move_templates( $move_args );
288 }
289
290 public function save_item_preview( $template_id, $data ) {
291 return $this->get_app()->update_resource_preview( $template_id, $data );
292 }
293
294 public function mark_preview_as_failed( $template_id, $data ) {
295 return $this->get_app()->mark_preview_as_failed( $template_id, $data );
296 }
297
298 /**
299 * @param int $template_id
300 * @return Document|\WP_Error
301 * @throws \Exception
302 */
303 public function create_document_for_preview( int $template_id ) {
304 if ( ! current_user_can( 'edit_posts' ) ) {
305 return new \WP_Error( Exceptions::FORBIDDEN, esc_html__( 'You do not have permission to create preview documents.', 'elementor' ) );
306 }
307
308 $cloud_library_app = $this->get_app();
309
310 $template = $cloud_library_app->get_resource( [ 'id' => $template_id ] );
311
312 if ( is_wp_error( $template ) ) {
313 $error_message = $template->get_error_message();
314 throw new \Exception( esc_html( $error_message ), Exceptions::FORBIDDEN ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
315 }
316
317 $decoded_content = json_decode( $template['content'], true );
318
319 return $this->save_document_for_preview( [
320 'content' => $decoded_content['content'],
321 'page_settings' => $decoded_content['page_settings'],
322 ] );
323 }
324
325 protected function save_document_for_preview( $template_content ) {
326 $template_data = [
327 'title' => esc_html__( '(no title)', 'elementor' ),
328 'page_settings' => $template_content['page_settings'] ?? [],
329 'status' => 'draft',
330 'type' => 'container',
331 ];
332
333 $document = Plugin::$instance->documents->create(
334 Cloud_Template_Preview::TYPE,
335 [
336 'post_title' => $template_data['title'],
337 'post_status' => $template_data['status'],
338 ]
339 );
340
341 if ( is_wp_error( $document ) ) {
342 wp_die();
343 }
344
345 $template_data['content'] = $this->replace_elements_ids( $template_content['content'] );
346
347 $document->save( [
348 'elements' => $template_data['content'],
349 'settings' => $template_data['page_settings'],
350 ] );
351
352 do_action( 'elementor/template-library/after_save_template', $document->get_main_id(), $template_data );
353 do_action( 'elementor/template-library/after_update_template', $document->get_main_id(), $template_data );
354
355 return $document;
356 }
357
358 public function bulk_delete_items( array $template_ids ) {
359 return $this->get_app()->bulk_delete_resources( $template_ids );
360 }
361
362
363 public function bulk_undo_delete_items( array $template_ids ) {
364 return $this->get_app()->bulk_undo_delete_resources( $template_ids );
365 }
366
367 public function save_bulk_items( array $data = [] ) {
368 $items = [];
369
370 foreach ( $data as $template_data ) {
371 $items[] = $this->format_resource_item_for_create( $template_data );
372 }
373
374 return $this->get_app()->post_bulk_resources( $items );
375 }
376
377 public function get_bulk_items( array $args = [] ) {
378 return $this->get_app()->get_bulk_resources_with_content( $args );
379 }
380
381 public function get_quota() {
382 return $this->get_app()->get_quota();
383 }
384
385 public function import_template( $name, $path ) {
386 if ( empty( $path ) ) {
387 return new \WP_Error( 'file_error', 'Please upload a file to import' );
388 }
389
390 Plugin::$instance->uploads_manager->set_elementor_upload_state( true );
391
392 $items = [];
393
394 $quota = $this->get_quota();
395
396 if ( is_wp_error( $quota ) ) {
397 return $quota;
398 }
399
400 if ( $quota['currentUsage'] >= $quota['threshold'] ) {
401 return new \WP_Error( 'quota_error', 'The upload failed because you’ve saved the maximum templates already.' );
402 }
403
404 if ( 'zip' === pathinfo( $name, PATHINFO_EXTENSION ) ) {
405 $extracted_files = Plugin::$instance->uploads_manager->extract_and_validate_zip( $path, [ 'json' ] );
406
407 if ( is_wp_error( $extracted_files ) ) {
408 Plugin::$instance->uploads_manager->remove_file_or_dir( $extracted_files['extraction_directory'] );
409
410 return $extracted_files;
411 }
412
413 $items_to_save = [];
414
415 foreach ( $extracted_files['files'] as $file_path ) {
416 $prepared = $this->prepare_import_template_data( $file_path );
417
418 if ( is_wp_error( $prepared ) ) {
419 Plugin::$instance->uploads_manager->remove_file_or_dir( $extracted_files['extraction_directory'] );
420
421 return $prepared;
422 }
423
424 $items_to_save[] = $this->format_resource_item_for_create( $prepared );
425 }
426
427 $is_quota_valid = $this->validate_quota( $items_to_save );
428
429 if ( is_wp_error( $is_quota_valid ) ) {
430 return $is_quota_valid;
431 }
432
433 if ( ! $is_quota_valid ) {
434 return new \WP_Error( 'quota_error', 'The upload failed because it will pass the maximum templates you can save.' );
435 }
436
437 $items = $this->get_app()->post_bulk_resources( $items_to_save );
438
439 Plugin::$instance->uploads_manager->remove_file_or_dir( $extracted_files['extraction_directory'] );
440 } else {
441 $prepared = $this->prepare_import_template_data( $path );
442
443 if ( is_wp_error( $prepared ) ) {
444 return $prepared;
445 }
446
447 $is_quota_valid = $this->validate_quota( [ $prepared ] );
448
449 if ( is_wp_error( $is_quota_valid ) ) {
450 return $is_quota_valid;
451 }
452
453 if ( ! $is_quota_valid ) {
454 return new \WP_Error( 'quota_error', 'The upload failed because it will pass the maximum templates you can save.' );
455 }
456
457 $item = $this->get_app()->post_resource( $this->format_resource_item_for_create( $prepared ) );
458
459 if ( is_wp_error( $item ) ) {
460 return $item;
461 }
462
463 $items[] = $item;
464 }
465
466 return $items;
467 }
468
469 public function validate_quota( $items ) {
470 $quota = $this->get_quota();
471
472 if ( is_wp_error( $quota ) ) {
473 return $quota;
474 }
475
476 return $quota['currentUsage'] + count( $items ) <= $quota['threshold'];
477 }
478 }
479