PluginProbe
Elementor Website Builder – more than just a page builder / 3.32.0-dev2
Elementor Website Builder – more than just a page builder v3.32.0-dev2
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.32.0-dev2, at includes/template-library/manager.php

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