PluginProbe
Elementor Website Builder – more than just a page builder / 3.31.4
Elementor Website Builder – more than just a page builder v3.31.4
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 / manager.php

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

1,296 lines 33.3 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\Api;
5 use Elementor\Core\Common\Modules\Ajax\Module as Ajax;
6 use Elementor\Core\Isolation\Wordpress_Adapter;
7 use Elementor\Core\Isolation\Wordpress_Adapter_Interface;
8 use Elementor\Core\Isolation\Elementor_Adapter;
9 use Elementor\Core\Isolation\Elementor_Adapter_Interface;
10 use Elementor\Core\Settings\Manager as SettingsManager;
11 use Elementor\Includes\TemplateLibrary\Data\Controller;
12 use Elementor\TemplateLibrary\Classes\Import_Images;
13 use Elementor\Plugin;
14 use Elementor\User;
15 use Elementor\Utils;
16
17 if ( ! defined( 'ABSPATH' ) ) {
18 exit; // Exit if accessed directly.
19 }
20
21 /**
22 * Elementor template library manager.
23 *
24 * Elementor template library manager handler class is responsible for
25 * initializing the template library.
26 *
27 * @since 1.0.0
28 */
29 class Manager {
30
31 /**
32 * Registered template sources.
33 *
34 * Holds a list of all the supported sources with their instances.
35 *
36 * @access protected
37 *
38 * @var Source_Base[]
39 */
40 protected $_registered_sources = []; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
41
42 /**
43 * Imported template images.
44 *
45 * Holds an instance of `Import_Images` class.
46 *
47 * @access private
48 *
49 * @var Import_Images
50 */
51 private $_import_images = null; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
52
53 /**
54 * @var Wordpress_Adapter_Interface
55 */
56 protected $wordpress_adapter = null;
57
58 /**
59 * @var Elementor_Adapter_Interface
60 */
61 protected $elementor_adapter = null;
62
63 /**
64 * Template library manager constructor.
65 *
66 * Initializing the template library manager by registering default template
67 * sources and initializing ajax calls.
68 *
69 * @since 1.0.0
70 * @access public
71 */
72 public function __construct() {
73 Plugin::$instance->data_manager_v2->register_controller( new Controller() );
74
75 $this->register_default_sources();
76
77 $this->add_actions();
78 }
79
80 /**
81 * @since 2.3.0
82 * @access public
83 */
84 public function add_actions() {
85 add_action( 'elementor/ajax/register_actions', [ $this, 'register_ajax_actions' ] );
86 add_action( 'wp_ajax_elementor_library_direct_actions', [ $this, 'handle_direct_actions' ] );
87 }
88
89 /**
90 * Get `Import_Images` instance.
91 *
92 * Retrieve the instance of the `Import_Images` class.
93 *
94 * @since 1.0.0
95 * @access public
96 *
97 * @return Import_Images Imported images instance.
98 */
99 public function get_import_images_instance() {
100 if ( null === $this->_import_images ) {
101 $this->_import_images = new Import_Images();
102 }
103
104 return $this->_import_images;
105 }
106
107 public function set_wordpress_adapter( Wordpress_Adapter_Interface $wordpress_adapter ) {
108 $this->wordpress_adapter = $wordpress_adapter;
109 }
110
111 public function set_elementor_adapter( Elementor_Adapter_Interface $elementor_adapter ): void {
112 $this->elementor_adapter = $elementor_adapter;
113 }
114
115 /**
116 * Register template source.
117 *
118 * Used to register new template sources displayed in the template library.
119 *
120 * @since 1.0.0
121 * @access public
122 *
123 * @param string $source_class The name of source class.
124 * @param array $args Optional. Class arguments. Default is an
125 * empty array.
126 *
127 * @return \WP_Error|true True if the source was registered, `WP_Error`
128 * otherwise.
129 */
130 public function register_source( $source_class, $args = [] ) {
131 if ( ! class_exists( $source_class ) ) {
132 return new \WP_Error( 'source_class_name_not_exists' );
133 }
134
135 $source_instance = new $source_class( $args );
136
137 if ( ! $source_instance instanceof Source_Base ) {
138 return new \WP_Error( 'wrong_instance_source' );
139 }
140
141 $source_id = $source_instance->get_id();
142
143 if ( isset( $this->_registered_sources[ $source_id ] ) ) {
144 return new \WP_Error( 'source_exists' );
145 }
146
147 $this->_registered_sources[ $source_id ] = $source_instance;
148
149 return true;
150 }
151
152 /**
153 * Unregister template source.
154 *
155 * Remove an existing template sources from the list of registered template
156 * sources.
157 *
158 * @since 1.0.0
159 * @deprecated 2.7.0
160 * @access public
161 *
162 * @param string $id The source ID.
163 *
164 * @return bool Whether the source was unregistered.
165 */
166 public function unregister_source( $id ) {
167 return true;
168 }
169
170 /**
171 * Get registered template sources.
172 *
173 * Retrieve registered template sources.
174 *
175 * @since 1.0.0
176 * @access public
177 *
178 * @return Source_Base[] Registered template sources.
179 */
180 public function get_registered_sources() {
181 return $this->_registered_sources;
182 }
183
184 /**
185 * Get template source.
186 *
187 * Retrieve single template sources for a given template ID.
188 *
189 * @since 1.0.0
190 * @access public
191 *
192 * @param string $id The source ID.
193 *
194 * @return false|Source_Base Template sources if one exist, False otherwise.
195 */
196 public function get_source( $id ) {
197 $sources = $this->get_registered_sources();
198
199 if ( ! isset( $sources[ $id ] ) ) {
200 return false;
201 }
202
203 return $sources[ $id ];
204 }
205
206 /**
207 * Get templates.
208 *
209 * Retrieve all the templates from all the registered sources.
210 *
211 * @param array $filter_sources
212 * @param bool $force_update
213 * @return array
214 */
215 public function get_templates( array $filter_sources = [], bool $force_update = false ): array {
216 $templates = [];
217
218 foreach ( $this->get_registered_sources() as $source ) {
219 if ( ! empty( $filter_sources ) && ! in_array( $source->get_id(), $filter_sources, true ) ) {
220 continue;
221 }
222
223 $templates = array_merge( $templates, $source->get_items( [ 'force_update' => $force_update ] ) );
224 }
225
226 return $templates;
227 }
228
229 /**
230 * Get library data.
231 *
232 * Retrieve the library data.
233 *
234 * @since 1.9.0
235 * @access public
236 *
237 * @param array $args Library arguments.
238 *
239 * @return array Library data.
240 */
241 public function get_library_data( array $args ) {
242 $library_data = Api::get_library_data( ! empty( $args['sync'] ) );
243
244 if ( empty( $library_data ) ) {
245 return $library_data;
246 }
247
248 // Ensure all document are registered.
249 Plugin::$instance->documents->get_document_types();
250
251 $filter_sources = ! empty( $args['filter_sources'] ) ? $args['filter_sources'] : [];
252 $force_update = ! empty( $args['sync'] );
253
254 return [
255 'templates' => $this->get_templates( $filter_sources, $force_update ),
256 'config' => $library_data['types_data'],
257 ];
258 }
259
260 /**
261 * Save template.
262 *
263 * Save new or update existing template on the database.
264 *
265 * @since 1.0.0
266 * @access public
267 *
268 * @param array $args Template arguments.
269 *
270 * @return \WP_Error|int The ID of the saved/updated template.
271 */
272 public function save_template( array $args ) {
273 $validate_args = $this->ensure_args( [ 'post_id', 'source', 'content', 'type' ], $args );
274
275 if ( is_wp_error( $validate_args ) ) {
276 return $validate_args;
277 }
278
279 $sources = (array) $args['source']; // BC
280 $results = [];
281
282 foreach ( $sources as $source ) {
283 $args_copy = $args;
284 $args_copy['source'] = $source;
285 $results[] = $this->save_template_item( $args_copy );
286 }
287
288 return 1 === count( $results ) ? $results[0] : $results;
289 }
290
291 private function save_template_item( array $args ) {
292 $source = $this->get_source( $args['source'] );
293
294 if ( ! $source ) {
295 return new \WP_Error( 'template_error', 'Template source not found.' );
296 }
297
298 $args['content'] = json_decode( $args['content'], true );
299
300 $page = SettingsManager::get_settings_managers( 'page' )->get_model( $args['post_id'] );
301
302 $args['page_settings'] = $page->get_data( 'settings' );
303
304 $template_id = $source->save_item( $args );
305
306 if ( is_wp_error( $template_id ) ) {
307 return $template_id;
308 }
309
310 return $source->get_item( $template_id );
311 }
312
313 public function move_template( array $args ) {
314 $validate_args = $this->ensure_args( [ 'source', 'from_source', 'from_template_id' ], $args );
315
316 if ( is_wp_error( $validate_args ) ) {
317 return $validate_args;
318 }
319
320 $args['source'] = $args['source'][0];
321
322 $result = $this->move_template_item( $args );
323
324 if ( ! $this->is_action_to_same_source( $args ) ) {
325 $this->delete_template( [
326 'source' => $args['from_source'],
327 'template_id' => $args['from_template_id'],
328 ] );
329 }
330
331 return $result;
332 }
333
334 private function move_template_item( array $args ) {
335 $validate_args = $this->ensure_args( [ 'source', 'from_source', 'from_template_id' ], $args );
336
337 if ( is_wp_error( $validate_args ) ) {
338 return $validate_args;
339 }
340
341 $source = $this->get_source( $args['source'] );
342
343 if ( ! $source ) {
344 return new \WP_Error( 'template_error', 'Template source not found.' );
345 }
346
347 if ( $this->is_action_to_same_source( $args ) ) {
348 return $source->move_template_to_folder( $args );
349 }
350
351 if ( 'local' === $args['from_source'] ) {
352 $args = $this->format_args_for_single_action_from_local_to_cloud( $args );
353 }
354
355 if ( 'cloud' === $args['from_source'] ) {
356 $args = $this->format_args_for_single_action_from_cloud_to_local( $args );
357 }
358
359 $template_id = $source->save_item( $args );
360
361 if ( is_wp_error( $template_id ) ) {
362 return $template_id;
363 }
364
365 return $source->get_item( $template_id );
366 }
367
368 public function copy_template( array $args ) {
369 $validate_args = $this->ensure_args( [ 'source', 'from_source', 'from_template_id' ], $args );
370
371 if ( is_wp_error( $validate_args ) ) {
372 return $validate_args;
373 }
374
375 $source = $this->get_source( $args['source'][0] );
376
377 if ( ! $source ) {
378 return new \WP_Error( 'template_error', 'Template source not found.' );
379 }
380
381 if ( 'local' === $args['from_source'] ) {
382 $args = $this->format_args_for_single_action_from_local_to_cloud( $args );
383 }
384
385 if ( 'cloud' === $args['from_source'] ) {
386 $args = $this->format_args_for_single_action_from_cloud_to_local( $args );
387 }
388
389 $template_id = $source->save_item( $args );
390
391 if ( is_wp_error( $template_id ) ) {
392 return $template_id;
393 }
394
395 return $source->get_item( $template_id );
396 }
397
398 private function is_action_to_same_source( $args ) {
399 return $args['source'] === $args['from_source'];
400 }
401
402 private function format_args_for_single_action_from_local_to_cloud( $args ) {
403 if ( ! $this->is_allowed_to_read_template( [
404 'source' => $args['from_source'],
405 'template_id' => $args['from_template_id'],
406 ] ) ) {
407 return new \WP_Error(
408 'template_error',
409 esc_html__( 'You do not have permission to access this template.', 'elementor' )
410 );
411 }
412
413 $document = Plugin::$instance->documents->get( $args['from_template_id'] );
414
415 if ( ! $document ) {
416 return new \WP_Error( 'template_error', 'Document not found.' );
417 }
418
419 $args['content'] = $document->get_elements_data();
420
421 $page = SettingsManager::get_settings_managers( 'page' )->get_model( $args['from_template_id'] );
422 $args['page_settings'] = $page->get_data( 'settings' );
423
424 return $args;
425 }
426
427 private function format_args_for_single_action_from_cloud_to_local( $args ) {
428 $from_source = $this->get_source( $args['from_source'] );
429
430 if ( ! $from_source ) {
431 return new \WP_Error( 'template_error', 'Template source not found.' );
432 }
433
434 $data = $from_source->get_item( $args['from_template_id'] );
435
436 if ( is_wp_error( $data ) || empty( $data['content'] ) ) {
437 return new \WP_Error( 'template_error', 'Unable to format template args.' );
438 }
439
440 $decoded_data = json_decode( $data['content'], true );
441 $args['content'] = $decoded_data['content'];
442 $args['page_settings'] = $decoded_data['page_settings'];
443
444 return $args;
445 }
446
447 /**
448 * Update template.
449 *
450 * Update template on the database.
451 *
452 * @since 1.0.0
453 * @access public
454 *
455 * @param array $template_data New template data.
456 *
457 * @return \WP_Error|Source_Base Template sources instance if the templates
458 * was updated, `WP_Error` otherwise.
459 */
460 public function update_template( array $template_data ) {
461 $validate_args = $this->ensure_args( [ 'source', 'content', 'type' ], $template_data );
462
463 if ( is_wp_error( $validate_args ) ) {
464 return $validate_args;
465 }
466
467 $source = $this->get_source( $template_data['source'] );
468
469 if ( ! $source ) {
470 return new \WP_Error( 'template_error', 'Template source not found.' );
471 }
472
473 $template_data['content'] = json_decode( $template_data['content'], true );
474
475 $update = $source->update_item( $template_data );
476
477 if ( is_wp_error( $update ) ) {
478 return $update;
479 }
480
481 return $source->get_item( $template_data['id'] );
482 }
483
484 public function rename_template( array $template_data ) {
485 $validate_args = $this->ensure_args( [ 'source', 'title', 'id' ], $template_data );
486
487 if ( is_wp_error( $validate_args ) ) {
488 return $validate_args;
489 }
490
491 $source = $this->get_source( $template_data['source'] );
492
493 if ( ! $source ) {
494 return new \WP_Error( 'template_error', 'Template source not found.' );
495 }
496
497 $update = $source->update_item( $template_data );
498
499 if ( is_wp_error( $update ) ) {
500 return $update;
501 }
502
503 return $source->get_item( $template_data['id'] );
504 }
505
506 /**
507 * Update templates.
508 *
509 * Update template on the database.
510 *
511 * @since 1.0.0
512 * @access public
513 *
514 * @param array $args Template arguments.
515 *
516 * @return \WP_Error|true True if templates updated, `WP_Error` otherwise.
517 */
518 public function update_templates( array $args ) {
519 foreach ( $args['templates'] as $template_data ) {
520 $result = $this->update_template( $template_data );
521
522 if ( is_wp_error( $result ) ) {
523 return $result;
524 }
525 }
526
527 return true;
528 }
529
530 /**
531 * Get template data.
532 *
533 * Retrieve the template data.
534 *
535 * @since 1.5.0
536 * @access public
537 *
538 * @param array $args Template arguments.
539 *
540 * @return \WP_Error|bool|array ??
541 */
542 public function get_template_data( array $args ) {
543 $validate_args = $this->ensure_args( [ 'source', 'template_id' ], $args );
544
545 if ( is_wp_error( $validate_args ) ) {
546 return $validate_args;
547 }
548
549 if ( ! $this->is_allowed_to_read_template( $args ) ) {
550 return new \WP_Error(
551 'template_error',
552 esc_html__( 'You do not have permission to access this template.', 'elementor' )
553 );
554 }
555
556 if ( isset( $args['edit_mode'] ) ) {
557 Plugin::$instance->editor->set_edit_mode( $args['edit_mode'] );
558 }
559
560 $source = $this->get_source( $args['source'] );
561
562 if ( ! $source ) {
563 return new \WP_Error( 'template_error', 'Template source not found.' );
564 }
565
566 do_action( 'elementor/template-library/before_get_source_data', $args, $source );
567
568 $data = $source->get_data( $args );
569
570 do_action( 'elementor/template-library/after_get_source_data', $args, $source );
571
572 return $data;
573 }
574
575 /**
576 * Delete template.
577 *
578 * Delete template from the database.
579 *
580 * @since 1.0.0
581 * @access public
582 *
583 * @param array $args Template arguments.
584 *
585 * @return \WP_Post|\WP_Error|false|null Post data on success, false or null
586 * or 'WP_Error' on failure.
587 */
588 public function delete_template( array $args ) {
589 $validate_args = $this->ensure_args( [ 'source', 'template_id' ], $args );
590
591 if ( is_wp_error( $validate_args ) ) {
592 return $validate_args;
593 }
594
595 $source = $this->get_source( $args['source'] );
596
597 if ( ! $source ) {
598 return new \WP_Error( 'template_error', 'Template source not found.' );
599 }
600
601 return $source->delete_template( $args['template_id'] );
602 }
603
604 /**
605 * Export template.
606 *
607 * Export template to a file after ensuring it is a valid Elementor template
608 * and checking user permissions for private posts.
609 *
610 * @since 1.0.0
611 * @access public
612 *
613 * @param array $args Template arguments.
614 *
615 * @return mixed Whether the export succeeded or failed.
616 */
617 public function export_template( array $args ) {
618 $validate_args = $this->ensure_args( [ 'source', 'template_id' ], $args );
619
620 if ( is_wp_error( $validate_args ) ) {
621 return $validate_args;
622 }
623
624 $source = $this->get_source( $args['source'] );
625
626 if ( ! $source ) {
627 return new \WP_Error( 'template_error', 'Template source not found' );
628 }
629
630 return $source->export_template( $args['template_id'] );
631 }
632
633 /**
634 * @since 2.3.0
635 * @access public
636 */
637 public function direct_import_template() {
638 /** @var Source_Local $source */
639 $source = $this->get_source( 'local' );
640 $file = Utils::get_super_global_value( $_FILES, 'file' ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
641 return $source->import_template( $file['name'], $file['tmp_name'] );
642 }
643
644 /**
645 * Import template.
646 *
647 * Import template from a file.
648 *
649 * @since 1.0.0
650 * @access public
651 *
652 * @param array $data
653 *
654 * @return mixed Whether the export succeeded or failed.
655 */
656 public function import_template( array $data ) {
657 // If the template is a JSON file, allow uploading it.
658 add_filter( 'elementor/files/allow-file-type/json', [ $this, 'enable_json_template_upload' ] );
659 add_filter( 'elementor/files/allow_unfiltered_upload', [ $this, 'enable_json_template_upload' ] );
660
661 // Imported templates can be either JSON files, or Zip files containing multiple JSON files
662 $upload_result = Plugin::$instance->uploads_manager->handle_elementor_upload( $data, [ 'zip', 'json' ] );
663
664 remove_filter( 'elementor/files/allow-file-type/json', [ $this, 'enable_json_template_upload' ] );
665 remove_filter( 'elementor/files/allow_unfiltered_upload', [ $this, 'enable_json_template_upload' ] );
666
667 if ( is_wp_error( $upload_result ) ) {
668 Plugin::$instance->uploads_manager->remove_file_or_dir( dirname( $upload_result['tmp_name'] ) );
669
670 return $upload_result;
671 }
672
673 $source = $this->get_source( $data['source'] ?? 'local' );
674
675 $import_result = $source->import_template( $upload_result['name'], $upload_result['tmp_name'] );
676
677 // Remove the temporary directory generated for the stream-uploaded file.
678 Plugin::$instance->uploads_manager->remove_file_or_dir( dirname( $upload_result['tmp_name'] ) );
679
680 return $import_result;
681 }
682
683 /**
684 * Enable JSON Template Upload
685 *
686 * Runs on the 'elementor/files/allow-file-type/json' Uploads Manager filter.
687 *
688 * @since 3.5.0
689 * @access public
690 *
691 * return bool
692 */
693 public function enable_json_template_upload() {
694 return true;
695 }
696
697 /**
698 * Mark template as favorite.
699 *
700 * Add the template to the user favorite templates.
701 *
702 * @since 1.9.0
703 * @access public
704 *
705 * @param array $args Template arguments.
706 *
707 * @return mixed Whether the template marked as favorite.
708 */
709 public function mark_template_as_favorite( $args ) {
710 $validate_args = $this->ensure_args( [ 'source', 'template_id', 'favorite' ], $args );
711
712 if ( is_wp_error( $validate_args ) ) {
713 return $validate_args;
714 }
715
716 $source = $this->get_source( $args['source'] );
717
718 return $source->mark_as_favorite( $args['template_id'], filter_var( $args['favorite'], FILTER_VALIDATE_BOOLEAN ) );
719 }
720
721 public function import_from_json( array $args ) {
722 $validate_args = $this->ensure_args( [ 'editor_post_id', 'elements' ], $args );
723
724 if ( is_wp_error( $validate_args ) ) {
725 return $validate_args;
726 }
727
728 $elements = json_decode( $args['elements'], true );
729
730 $document = Plugin::$instance->documents->get( $args['editor_post_id'] );
731 if ( ! $document ) {
732 return new \WP_Error( 'template_error', 'Document not found.' );
733 }
734
735 $import_data = $document->get_import_data( [ 'content' => $elements ] );
736
737 return $import_data['content'];
738 }
739
740 public function get_item_children( array $args ) {
741 $validate_args = $this->ensure_args( [ 'source', 'template_id' ], $args );
742
743 if ( is_wp_error( $validate_args ) ) {
744 return $validate_args;
745 }
746
747 $source = $this->get_source( $args['source'] );
748
749 if ( ! $source ) {
750 return new \WP_Error( 'template_error', 'Template source not found.' );
751 }
752
753 return $source->get_item_children( $args );
754 }
755
756 public function search_templates( array $args ) {
757 $validate_args = $this->ensure_args( [ 'source', 'search' ], $args );
758
759 if ( is_wp_error( $validate_args ) ) {
760 return $validate_args;
761 }
762
763 $source = $this->get_source( $args['source'] );
764
765 if ( ! $source ) {
766 return new \WP_Error( 'template_error', 'Template source not found.' );
767 }
768
769 return $source->search_templates( $args );
770 }
771
772 public function load_more_templates( array $args ) {
773 $validate_args = $this->ensure_args( [ 'source', 'offset' ], $args );
774
775 if ( is_wp_error( $validate_args ) ) {
776 return $validate_args;
777 }
778
779 $source = $this->get_source( $args['source'] );
780
781 if ( ! $source ) {
782 return new \WP_Error( 'template_error', 'Template source not found.' );
783 }
784
785 return $source->get_items( $args );
786 }
787
788 public function create_folder( array $args ) {
789 $validate_args = $this->ensure_args( [ 'source', 'title' ], $args );
790
791 if ( is_wp_error( $validate_args ) ) {
792 return $validate_args;
793 }
794
795 $source = $this->get_source( $args['source'] );
796
797 if ( ! $source ) {
798 return new \WP_Error( 'template_error', 'Template source not found.' );
799 }
800
801 return $source->save_folder( $args );
802 }
803
804 public function get_folders( array $args ) {
805 $validate_args = $this->ensure_args( [ 'source', 'offset' ], $args );
806
807 if ( is_wp_error( $validate_args ) ) {
808 return $validate_args;
809 }
810
811 $source = $this->get_source( $args['source'] );
812
813 if ( ! $source ) {
814 return new \WP_Error( 'template_error', 'Folder source not found.' );
815 }
816
817 $args['templateType'] = 'folder';
818
819 return $source->get_items( $args );
820 }
821
822 /**
823 * Register default template sources.
824 *
825 * Register the 'local' and 'remote' template sources that Elementor use by
826 * default.
827 *
828 * @since 1.0.0
829 * @access private
830 */
831 private function register_default_sources() {
832 $sources = [
833 'local',
834 'remote',
835 ];
836
837 if ( Plugin::$instance->experiments->is_feature_active( 'cloud-library' ) ) {
838 $sources[] = 'cloud';
839 }
840
841 foreach ( $sources as $source_filename ) {
842 $class_name = ucwords( $source_filename );
843 $class_name = str_replace( '-', '_', $class_name );
844
845 $this->register_source( __NAMESPACE__ . '\Source_' . $class_name );
846 }
847 }
848
849 /**
850 * Handle ajax request.
851 *
852 * Fire authenticated ajax actions for any given ajax request.
853 *
854 * @since 1.0.0
855 * @access private
856 *
857 * @param string $ajax_request Ajax request.
858 *
859 * @param array $data
860 *
861 * @return mixed
862 * @throws \Exception If the user has no permission or the post is not found.
863 */
864 private function handle_ajax_request( $ajax_request, array $data ) {
865 if ( ! User::is_current_user_can_edit_post_type( Source_Local::CPT ) ) {
866 throw new \Exception( 'Access denied.' );
867 }
868
869 if ( ! empty( $data['editor_post_id'] ) ) {
870 $editor_post_id = absint( $data['editor_post_id'] );
871
872 if ( ! get_post( $editor_post_id ) ) {
873 throw new \Exception( 'Post not found.' );
874 }
875
876 Plugin::$instance->db->switch_to_post( $editor_post_id );
877 }
878
879 $result = call_user_func( [ $this, $ajax_request ], $data );
880
881 if ( is_wp_error( $result ) ) {
882 throw new \Exception( esc_html( $result->get_error_message() ) );
883 }
884
885 return $result; // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
886 }
887
888 /**
889 * @throws \Exception
890 */
891 public function save_template_screenshot( $data ): string {
892 $validate_args = $this->ensure_args( [ 'template_id', 'screenshot' ], $data );
893
894 if ( is_wp_error( $validate_args ) ) {
895 return $validate_args;
896 }
897
898 $raw_binary = base64_decode( substr( $data['screenshot'], strlen( 'data:image/png;base64,' ) ) );
899
900 return $this->get_source( 'cloud' )->save_item_preview( $data['template_id'], $raw_binary );
901 }
902
903 /**
904 * @throws \Exception
905 */
906 public function template_screenshot_failed( $data ): string {
907 $validate_args = $this->ensure_args( [ 'template_id' ], $data );
908
909 if ( is_wp_error( $validate_args ) ) {
910 return $validate_args;
911 }
912
913 return $this->get_source( 'cloud' )->mark_preview_as_failed( $data['template_id'], $data['error'] );
914 }
915
916 public function bulk_delete_templates( $data ) {
917 $validate_args = $this->ensure_args( [ 'template_ids', 'source' ], $data );
918
919 if ( is_wp_error( $validate_args ) ) {
920 return $validate_args;
921 }
922
923 $source = $this->get_source( $data['source'] );
924
925 if ( ! $source ) {
926 return new \WP_Error( 'template_error', 'Template source not found.' );
927 }
928
929 if ( empty( $data['template_ids'] ) || ! is_array( $data['template_ids'] ) ) {
930 return new \WP_Error( 'template_error', 'Template IDs are missing.' );
931 }
932
933 return $source->bulk_delete_items( $data['template_ids'] );
934 }
935
936 public function bulk_undo_delete_items( $data ) {
937 $validate_args = $this->ensure_args( [ 'template_ids', 'source' ], $data );
938
939 if ( is_wp_error( $validate_args ) ) {
940 return $validate_args;
941 }
942
943 $source = $this->get_source( $data['source'] );
944
945 if ( ! $source ) {
946 return new \WP_Error( 'template_error', 'Template source not found.' );
947 }
948
949 if ( empty( $data['template_ids'] ) || ! is_array( $data['template_ids'] ) ) {
950 return new \WP_Error( 'template_error', 'Template IDs are missing.' );
951 }
952
953 return $source->bulk_undo_delete_items( $data['template_ids'] );
954 }
955
956 /**
957 * Init ajax calls.
958 *
959 * Initialize template library ajax calls for allowed ajax requests.
960 *
961 * @since 2.3.0
962 * @access public
963 *
964 * @param Ajax $ajax
965 */
966 public function register_ajax_actions( Ajax $ajax ) {
967 $library_ajax_requests = [
968 'get_library_data',
969 'get_template_data',
970 'save_template',
971 'update_templates',
972 'delete_template',
973 'import_template',
974 'mark_template_as_favorite',
975 'import_from_json',
976 'get_item_children',
977 'search_templates',
978 'rename_template',
979 'load_more_templates',
980 'create_folder',
981 'get_folders',
982 'save_template_screenshot',
983 'move_template',
984 'copy_template',
985 'bulk_move_templates',
986 'bulk_delete_templates',
987 'bulk_copy_templates',
988 'bulk_undo_delete_items',
989 'get_templates_quota',
990 'template_screenshot_failed',
991 ];
992
993 foreach ( $library_ajax_requests as $ajax_request ) {
994 $ajax->register_ajax_action( $ajax_request, function( $data ) use ( $ajax_request ) {
995 return $this->handle_ajax_request( $ajax_request, $data );
996 } );
997 }
998 }
999
1000 /**
1001 * @since 2.3.0
1002 * @access public
1003 */
1004 public function handle_direct_actions() {
1005 if ( ! User::is_current_user_can_edit_post_type( Source_Local::CPT ) ) {
1006 return;
1007 }
1008
1009 /** @var Ajax $ajax */
1010 $ajax = Plugin::$instance->common->get_component( 'ajax' );
1011
1012 if ( ! $ajax->verify_request_nonce() ) {
1013 $this->handle_direct_action_error( 'Access Denied' );
1014 }
1015
1016 $action = Utils::get_super_global_value( $_REQUEST, 'library_action' ); // phpcs:ignore -- Nonce already verified.
1017
1018 $whitelist_methods = [
1019 'export_template',
1020 'direct_import_template',
1021 ];
1022
1023 if ( 'direct_import_template' === $action && ! User::is_current_user_can_upload_json() ) {
1024 return;
1025 }
1026
1027 if ( in_array( $action, $whitelist_methods, true ) ) {
1028 $result = $this->$action( $_REQUEST ); // phpcs:ignore -- Nonce already verified.
1029 } else {
1030 $result = new \WP_Error( 'method_not_exists', 'Method Not exists' );
1031 }
1032
1033 if ( is_wp_error( $result ) ) {
1034 /** @var \WP_Error $result */
1035 $this->handle_direct_action_error( $result->get_error_message() . '.' );
1036 }
1037
1038 $callback = "on_{$action}_success";
1039
1040 if ( method_exists( $this, $callback ) ) {
1041 $this->$callback( $result );
1042 }
1043
1044 die;
1045 }
1046
1047 /**
1048 * On successful template import.
1049 *
1050 * Redirect the user to the template library after template import was
1051 * successful finished.
1052 *
1053 * @since 2.3.0
1054 * @access private
1055 */
1056 private function on_direct_import_template_success() {
1057 wp_safe_redirect( admin_url( Source_Local::ADMIN_MENU_SLUG ) );
1058 }
1059
1060 /**
1061 * @since 2.3.0
1062 * @access private
1063 */
1064 private function handle_direct_action_error( $message ) {
1065 _default_wp_die_handler( $message, 'Elementor Library' );
1066 }
1067
1068 /**
1069 * Ensure arguments exist.
1070 *
1071 * Checks whether the required arguments exist in the specified arguments.
1072 *
1073 * @since 1.0.0
1074 * @access private
1075 *
1076 * @param array $required_args Required arguments to check whether they
1077 * exist.
1078 * @param array $specified_args The list of all the specified arguments to
1079 * check against.
1080 *
1081 * @return \WP_Error|true True on success, 'WP_Error' otherwise.
1082 */
1083 private function ensure_args( array $required_args, array $specified_args ) {
1084 $not_specified_args = array_diff( $required_args, array_keys( $specified_args ) );
1085
1086 if ( $not_specified_args ) {
1087 return new \WP_Error( 'arguments_not_specified', sprintf( 'The required argument(s) "%s" not specified.', implode( ', ', $not_specified_args ) ) );
1088 }
1089
1090 return true;
1091 }
1092
1093 private function is_allowed_to_read_template( array $args ): bool {
1094 if ( 'remote' === $args['source'] || 'cloud' === $args['source'] ) {
1095 return true;
1096 }
1097
1098 if ( null === $this->wordpress_adapter ) {
1099 $this->set_wordpress_adapter( new WordPress_Adapter() );
1100 }
1101
1102 if ( ! $this->should_check_permissions( $args ) ) {
1103 return true;
1104 }
1105
1106 $post_id = intval( $args['template_id'] );
1107 $post_status = $this->wordpress_adapter->get_post_status( $post_id );
1108 $is_private_or_non_published = ( 'private' === $post_status && ! $this->wordpress_adapter->current_user_can( 'read_private_posts', $post_id ) ) || ( 'publish' !== $post_status );
1109
1110 $can_read_template = $is_private_or_non_published || $this->wordpress_adapter->current_user_can( 'edit_post', $post_id );
1111
1112 return apply_filters( 'elementor/template-library/is_allowed_to_read_template', $can_read_template, $args );
1113 }
1114
1115 private function should_check_permissions( array $args ): bool {
1116 if ( null === $this->elementor_adapter ) {
1117 $this->set_elementor_adapter( new Elementor_Adapter() );
1118 }
1119
1120 // TODO: Remove $isWidgetTemplate in 3.28.0 as there is a Pro dependency
1121 $check_permissions = isset( $args['check_permissions'] ) && false === $args['check_permissions'];
1122 $is_widget_template = 'widget' === $this->elementor_adapter->get_template_type( $args['template_id'] );
1123
1124 if ( $check_permissions || $is_widget_template ) {
1125 return false;
1126 }
1127
1128 return true;
1129 }
1130
1131 public function bulk_move_templates( array $args ) {
1132 $validate_args = $this->ensure_args( [ 'source', 'from_source', 'from_template_id' ], $args );
1133
1134 if ( is_wp_error( $validate_args ) ) {
1135 return $validate_args;
1136 }
1137
1138 $args['source'] = $args['source'][0];
1139
1140 return $this->bulk_move_template_items( $args );
1141 }
1142
1143 private function bulk_move_template_items( array $args ) {
1144 $source = $this->get_source( $args['source'] );
1145
1146 if ( ! $source ) {
1147 return new \WP_Error( 'template_error', 'Template source not found.' );
1148 }
1149
1150 if ( $this->is_action_to_same_source( $args ) ) {
1151 return $source->move_bulk_templates_to_folder( $args );
1152 }
1153
1154 $bulk_args = 'local' === $args['from_source']
1155 ? $this->format_args_for_bulk_action_from_local( $args )
1156 : $this->format_args_for_bulk_action_from_cloud( $args );
1157
1158 if ( $source->supports_quota() && ! $this->is_action_to_same_source( $args ) ) {
1159 $is_quota_valid = $source->validate_quota( $bulk_args );
1160
1161 if ( is_wp_error( $is_quota_valid ) ) {
1162 return $is_quota_valid;
1163 }
1164
1165 if ( ! $is_quota_valid ) {
1166 return new \WP_Error( 'quota_error', 'The moving failed because it will pass the maximum templates you can save.' );
1167 }
1168 }
1169
1170 $bulk_save = $source->save_bulk_items( $bulk_args );
1171
1172 if ( ! empty( $bulk_save ) ) {
1173 $this->bulk_delete_templates( [
1174 'template_ids' => $args['from_template_id'],
1175 'source' => $args['from_source'],
1176 ] );
1177 }
1178
1179 return $bulk_save;
1180 }
1181
1182 private function format_args_for_bulk_action_from_local( $args ) {
1183 $bulk_args = [];
1184
1185 foreach ( $args['from_template_id'] as $from_template_id ) {
1186 if ( ! $this->is_allowed_to_read_template( [
1187 'source' => $args['from_source'],
1188 'template_id' => $from_template_id,
1189 ] ) ) {
1190 continue;
1191 }
1192
1193 $document = Plugin::$instance->documents->get( $from_template_id );
1194
1195 if ( ! $document ) {
1196 continue;
1197 }
1198
1199 $page = SettingsManager::get_settings_managers( 'page' )->get_model( $from_template_id );
1200
1201 $bulk_args[] = array_merge(
1202 $args,
1203 [
1204 'title' => $document->get_post()->post_title,
1205 'type' => $document::get_type(),
1206 'content' => $document->get_elements_data(),
1207 'page_settings' => $page->get_data( 'settings' ),
1208 ]
1209 );
1210 }
1211
1212 return $bulk_args;
1213 }
1214
1215 private function format_args_for_bulk_action_from_cloud( $args ) {
1216 $from_source = $this->get_source( $args['from_source'] );
1217
1218 if ( ! $from_source ) {
1219 return new \WP_Error( 'template_error', 'Template source not found.' );
1220 }
1221
1222 $templates = $from_source->get_bulk_items( $args );
1223 $bulk_args = [];
1224
1225 foreach ( $templates as $template ) {
1226 $content = json_decode( $template['content'], true );
1227
1228 $bulk_args[] = array_merge(
1229 $args,
1230 [
1231 'title' => $template['title'],
1232 'type' => $template['type'],
1233 'content' => $content['content'],
1234 'page_settings' => $content['page_settings'],
1235 ]
1236 );
1237 }
1238
1239 return $bulk_args;
1240 }
1241
1242 public function bulk_copy_templates( array $args ) {
1243 $validate_args = $this->ensure_args( [ 'source', 'from_source', 'from_template_id' ], $args );
1244
1245 if ( is_wp_error( $validate_args ) ) {
1246 return $validate_args;
1247 }
1248
1249 $args['source'] = $args['source'][0];
1250
1251 return $this->bulk_copy_template_items( $args );
1252 }
1253
1254 private function bulk_copy_template_items( array $args ) {
1255 $source = $this->get_source( $args['source'] );
1256
1257 if ( ! $source ) {
1258 return new \WP_Error( 'template_error', 'Template source not found.' );
1259 }
1260
1261 $bulk_args = 'local' === $args['from_source']
1262 ? $this->format_args_for_bulk_action_from_local( $args )
1263 : $this->format_args_for_bulk_action_from_cloud( $args );
1264
1265 if ( $source->supports_quota() && ! $this->is_action_to_same_source( $args ) ) {
1266 $is_quota_valid = $source->validate_quota( $bulk_args );
1267
1268 if ( is_wp_error( $is_quota_valid ) ) {
1269 return $is_quota_valid;
1270 }
1271
1272 if ( ! $is_quota_valid ) {
1273 return new \WP_Error( 'quota_error', 'The copying failed because it will pass the maximum templates you can save.' );
1274 }
1275 }
1276
1277 return $source->save_bulk_items( $bulk_args );
1278 }
1279
1280 public function get_templates_quota( array $args ) {
1281 $validate_args = $this->ensure_args( [ 'source' ], $args );
1282
1283 if ( is_wp_error( $validate_args ) ) {
1284 return $validate_args;
1285 }
1286
1287 $source = $this->get_source( $args['source'] );
1288
1289 if ( ! $source ) {
1290 return new \WP_Error( 'template_error', 'Source not found.' );
1291 }
1292
1293 return $source->get_quota();
1294 }
1295 }
1296