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

809 lines 20.7 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 $source = $this->get_source( $args['source'] );
280
281 if ( ! $source ) {
282 return new \WP_Error( 'template_error', 'Template source not found.' );
283 }
284
285 $args['content'] = json_decode( $args['content'], true );
286
287 $page = SettingsManager::get_settings_managers( 'page' )->get_model( $args['post_id'] );
288
289 $args['page_settings'] = $page->get_data( 'settings' );
290
291 $template_id = $source->save_item( $args );
292
293 if ( is_wp_error( $template_id ) ) {
294 return $template_id;
295 }
296
297 return $source->get_item( $template_id );
298 }
299
300 /**
301 * Update template.
302 *
303 * Update template on the database.
304 *
305 * @since 1.0.0
306 * @access public
307 *
308 * @param array $template_data New template data.
309 *
310 * @return \WP_Error|Source_Base Template sources instance if the templates
311 * was updated, `WP_Error` otherwise.
312 */
313 public function update_template( array $template_data ) {
314 $validate_args = $this->ensure_args( [ 'source', 'content', 'type' ], $template_data );
315
316 if ( is_wp_error( $validate_args ) ) {
317 return $validate_args;
318 }
319
320 $source = $this->get_source( $template_data['source'] );
321
322 if ( ! $source ) {
323 return new \WP_Error( 'template_error', 'Template source not found.' );
324 }
325
326 $template_data['content'] = json_decode( $template_data['content'], true );
327
328 $update = $source->update_item( $template_data );
329
330 if ( is_wp_error( $update ) ) {
331 return $update;
332 }
333
334 return $source->get_item( $template_data['id'] );
335 }
336
337 /**
338 * Update templates.
339 *
340 * Update template on the database.
341 *
342 * @since 1.0.0
343 * @access public
344 *
345 * @param array $args Template arguments.
346 *
347 * @return \WP_Error|true True if templates updated, `WP_Error` otherwise.
348 */
349 public function update_templates( array $args ) {
350 foreach ( $args['templates'] as $template_data ) {
351 $result = $this->update_template( $template_data );
352
353 if ( is_wp_error( $result ) ) {
354 return $result;
355 }
356 }
357
358 return true;
359 }
360
361 /**
362 * Get template data.
363 *
364 * Retrieve the template data.
365 *
366 * @since 1.5.0
367 * @access public
368 *
369 * @param array $args Template arguments.
370 *
371 * @return \WP_Error|bool|array ??
372 */
373 public function get_template_data( array $args ) {
374 $validate_args = $this->ensure_args( [ 'source', 'template_id' ], $args );
375
376 if ( is_wp_error( $validate_args ) ) {
377 return $validate_args;
378 }
379
380 if ( ! $this->is_allowed_to_read_template( $args ) ) {
381 return new \WP_Error(
382 'template_error',
383 esc_html__( 'You do not have permission to access this template.', 'elementor' )
384 );
385 }
386
387 if ( isset( $args['edit_mode'] ) ) {
388 Plugin::$instance->editor->set_edit_mode( $args['edit_mode'] );
389 }
390
391 $source = $this->get_source( $args['source'] );
392
393 if ( ! $source ) {
394 return new \WP_Error( 'template_error', 'Template source not found.' );
395 }
396
397 do_action( 'elementor/template-library/before_get_source_data', $args, $source );
398
399 $data = $source->get_data( $args );
400
401 do_action( 'elementor/template-library/after_get_source_data', $args, $source );
402
403 return $data;
404 }
405
406 /**
407 * Delete template.
408 *
409 * Delete template from the database.
410 *
411 * @since 1.0.0
412 * @access public
413 *
414 * @param array $args Template arguments.
415 *
416 * @return \WP_Post|\WP_Error|false|null Post data on success, false or null
417 * or 'WP_Error' on failure.
418 */
419 public function delete_template( array $args ) {
420 $validate_args = $this->ensure_args( [ 'source', 'template_id' ], $args );
421
422 if ( is_wp_error( $validate_args ) ) {
423 return $validate_args;
424 }
425
426 $source = $this->get_source( $args['source'] );
427
428 if ( ! $source ) {
429 return new \WP_Error( 'template_error', 'Template source not found.' );
430 }
431
432 return $source->delete_template( $args['template_id'] );
433 }
434
435 /**
436 * Export template.
437 *
438 * Export template to a file after ensuring it is a valid Elementor template
439 * and checking user permissions for private posts.
440 *
441 * @since 1.0.0
442 * @access public
443 *
444 * @param array $args Template arguments.
445 *
446 * @return mixed Whether the export succeeded or failed.
447 */
448 public function export_template( array $args ) {
449 $validate_args = $this->ensure_args( [ 'source', 'template_id' ], $args );
450
451 if ( is_wp_error( $validate_args ) ) {
452 return $validate_args;
453 }
454
455 $post_id = intval( $args['template_id'] );
456 $post_status = get_post_status( $post_id );
457
458 if ( get_post_type( $post_id ) !== Source_Local::CPT ) {
459 return new \WP_Error( 'template_error', esc_html__( 'Invalid template type or template does not exist.', 'elementor' ) );
460 }
461
462 if ( 'private' === $post_status && ! current_user_can( 'read_private_posts', $post_id ) ) {
463 return new \WP_Error( 'template_error', esc_html__( 'You do not have permission to access this template.', 'elementor' ) );
464 }
465
466 if ( 'publish' !== $post_status && ! current_user_can( 'edit_post', $post_id ) ) {
467 return new \WP_Error( 'template_error', esc_html__( 'You do not have permission to export this template.', 'elementor' ) );
468 }
469
470 $source = $this->get_source( $args['source'] );
471
472 if ( ! $source ) {
473 return new \WP_Error( 'template_error', 'Template source not found' );
474 }
475
476 return $source->export_template( $args['template_id'] );
477 }
478
479 /**
480 * @since 2.3.0
481 * @access public
482 */
483 public function direct_import_template() {
484 /** @var Source_Local $source */
485 $source = $this->get_source( 'local' );
486 $file = Utils::get_super_global_value( $_FILES, 'file' );
487 return $source->import_template( $file['name'], $file['tmp_name'] );
488 }
489
490 /**
491 * Import template.
492 *
493 * Import template from a file.
494 *
495 * @since 1.0.0
496 * @access public
497 *
498 * @param array $data
499 *
500 * @return mixed Whether the export succeeded or failed.
501 */
502 public function import_template( array $data ) {
503 // If the template is a JSON file, allow uploading it.
504 add_filter( 'elementor/files/allow-file-type/json', [ $this, 'enable_json_template_upload' ] );
505 add_filter( 'elementor/files/allow_unfiltered_upload', [ $this, 'enable_json_template_upload' ] );
506
507 // Imported templates can be either JSON files, or Zip files containing multiple JSON files
508 $upload_result = Plugin::$instance->uploads_manager->handle_elementor_upload( $data, [ 'zip', 'json' ] );
509
510 remove_filter( 'elementor/files/allow-file-type/json', [ $this, 'enable_json_template_upload' ] );
511 remove_filter( 'elementor/files/allow_unfiltered_upload', [ $this, 'enable_json_template_upload' ] );
512
513 if ( is_wp_error( $upload_result ) ) {
514 Plugin::$instance->uploads_manager->remove_file_or_dir( dirname( $upload_result['tmp_name'] ) );
515
516 return $upload_result;
517 }
518
519 /** @var Source_Local $source_local */
520 $source_local = $this->get_source( 'local' );
521
522 $import_result = $source_local->import_template( $upload_result['name'], $upload_result['tmp_name'] );
523
524 // Remove the temporary directory generated for the stream-uploaded file.
525 Plugin::$instance->uploads_manager->remove_file_or_dir( dirname( $upload_result['tmp_name'] ) );
526
527 return $import_result;
528 }
529
530 /**
531 * Enable JSON Template Upload
532 *
533 * Runs on the 'elementor/files/allow-file-type/json' Uploads Manager filter.
534 *
535 * @since 3.5.0
536 * @access public
537 *
538 * return bool
539 */
540 public function enable_json_template_upload() {
541 return true;
542 }
543
544 /**
545 * Mark template as favorite.
546 *
547 * Add the template to the user favorite templates.
548 *
549 * @since 1.9.0
550 * @access public
551 *
552 * @param array $args Template arguments.
553 *
554 * @return mixed Whether the template marked as favorite.
555 */
556 public function mark_template_as_favorite( $args ) {
557 $validate_args = $this->ensure_args( [ 'source', 'template_id', 'favorite' ], $args );
558
559 if ( is_wp_error( $validate_args ) ) {
560 return $validate_args;
561 }
562
563 $source = $this->get_source( $args['source'] );
564
565 return $source->mark_as_favorite( $args['template_id'], filter_var( $args['favorite'], FILTER_VALIDATE_BOOLEAN ) );
566 }
567
568 public function import_from_json( array $args ) {
569 $validate_args = $this->ensure_args( [ 'editor_post_id', 'elements' ], $args );
570
571 if ( is_wp_error( $validate_args ) ) {
572 return $validate_args;
573 }
574
575 $elements = json_decode( $args['elements'], true );
576
577 $document = Plugin::$instance->documents->get( $args['editor_post_id'] );
578 if ( ! $document ) {
579 return new \WP_Error( 'template_error', 'Document not found.' );
580 }
581
582 $import_data = $document->get_import_data( [ 'content' => $elements ] );
583
584 return $import_data['content'];
585 }
586
587 /**
588 * Register default template sources.
589 *
590 * Register the 'local' and 'remote' template sources that Elementor use by
591 * default.
592 *
593 * @since 1.0.0
594 * @access private
595 */
596 private function register_default_sources() {
597 $sources = [
598 'local',
599 'remote',
600 ];
601
602 foreach ( $sources as $source_filename ) {
603 $class_name = ucwords( $source_filename );
604 $class_name = str_replace( '-', '_', $class_name );
605
606 $this->register_source( __NAMESPACE__ . '\Source_' . $class_name );
607 }
608 }
609
610 /**
611 * Handle ajax request.
612 *
613 * Fire authenticated ajax actions for any given ajax request.
614 *
615 * @since 1.0.0
616 * @access private
617 *
618 * @param string $ajax_request Ajax request.
619 *
620 * @param array $data
621 *
622 * @return mixed
623 * @throws \Exception
624 */
625 private function handle_ajax_request( $ajax_request, array $data ) {
626 if ( ! User::is_current_user_can_edit_post_type( Source_Local::CPT ) ) {
627 throw new \Exception( 'Access denied.' );
628 }
629
630 if ( ! empty( $data['editor_post_id'] ) ) {
631 $editor_post_id = absint( $data['editor_post_id'] );
632
633 if ( ! get_post( $editor_post_id ) ) {
634 throw new \Exception( 'Post not found.' );
635 }
636
637 Plugin::$instance->db->switch_to_post( $editor_post_id );
638 }
639
640 $result = call_user_func( [ $this, $ajax_request ], $data );
641
642 if ( is_wp_error( $result ) ) {
643 throw new \Exception( $result->get_error_message() );
644 }
645
646 return $result;
647 }
648
649 /**
650 * Init ajax calls.
651 *
652 * Initialize template library ajax calls for allowed ajax requests.
653 *
654 * @since 2.3.0
655 * @access public
656 *
657 * @param Ajax $ajax
658 */
659 public function register_ajax_actions( Ajax $ajax ) {
660 $library_ajax_requests = [
661 'get_library_data',
662 'get_template_data',
663 'save_template',
664 'update_templates',
665 'delete_template',
666 'import_template',
667 'mark_template_as_favorite',
668 'import_from_json',
669 ];
670
671 foreach ( $library_ajax_requests as $ajax_request ) {
672 $ajax->register_ajax_action( $ajax_request, function( $data ) use ( $ajax_request ) {
673 return $this->handle_ajax_request( $ajax_request, $data );
674 } );
675 }
676 }
677
678 /**
679 * @since 2.3.0
680 * @access public
681 */
682 public function handle_direct_actions() {
683 if ( ! User::is_current_user_can_edit_post_type( Source_Local::CPT ) ) {
684 return;
685 }
686
687 /** @var Ajax $ajax */
688 $ajax = Plugin::$instance->common->get_component( 'ajax' );
689
690 if ( ! $ajax->verify_request_nonce() ) {
691 $this->handle_direct_action_error( 'Access Denied' );
692 }
693
694 $action = Utils::get_super_global_value( $_REQUEST, 'library_action' ); // phpcs:ignore -- Nonce already verified.
695
696 $whitelist_methods = [
697 'export_template',
698 'direct_import_template',
699 ];
700
701 if ( 'direct_import_template' === $action && ! User::is_current_user_can_upload_json() ) {
702 return;
703 }
704
705 if ( in_array( $action, $whitelist_methods, true ) ) {
706 $result = $this->$action( $_REQUEST ); // phpcs:ignore -- Nonce already verified.
707 } else {
708 $result = new \WP_Error( 'method_not_exists', 'Method Not exists' );
709 }
710
711 if ( is_wp_error( $result ) ) {
712 /** @var \WP_Error $result */
713 $this->handle_direct_action_error( $result->get_error_message() . '.' );
714 }
715
716 $callback = "on_{$action}_success";
717
718 if ( method_exists( $this, $callback ) ) {
719 $this->$callback( $result );
720 }
721
722 die;
723 }
724
725 /**
726 * On successful template import.
727 *
728 * Redirect the user to the template library after template import was
729 * successful finished.
730 *
731 * @since 2.3.0
732 * @access private
733 */
734 private function on_direct_import_template_success() {
735 wp_safe_redirect( admin_url( Source_Local::ADMIN_MENU_SLUG ) );
736 }
737
738 /**
739 * @since 2.3.0
740 * @access private
741 */
742 private function handle_direct_action_error( $message ) {
743 _default_wp_die_handler( $message, 'Elementor Library' );
744 }
745
746 /**
747 * Ensure arguments exist.
748 *
749 * Checks whether the required arguments exist in the specified arguments.
750 *
751 * @since 1.0.0
752 * @access private
753 *
754 * @param array $required_args Required arguments to check whether they
755 * exist.
756 * @param array $specified_args The list of all the specified arguments to
757 * check against.
758 *
759 * @return \WP_Error|true True on success, 'WP_Error' otherwise.
760 */
761 private function ensure_args( array $required_args, array $specified_args ) {
762 $not_specified_args = array_diff( $required_args, array_keys( $specified_args ) );
763
764 if ( $not_specified_args ) {
765 return new \WP_Error( 'arguments_not_specified', sprintf( 'The required argument(s) "%s" not specified.', implode( ', ', $not_specified_args ) ) );
766 }
767
768 return true;
769 }
770
771 private function is_allowed_to_read_template( array $args ): bool {
772 if ( 'remote' === $args['source'] ) {
773 return true;
774 }
775
776 if ( null === $this->wordpress_adapter ) {
777 $this->set_wordpress_adapter( new WordPress_Adapter() );
778 }
779
780 if ( ! $this->should_check_permissions( $args ) ) {
781 return true;
782 }
783
784 $post_id = intval( $args['template_id'] );
785 $post_status = $this->wordpress_adapter->get_post_status( $post_id );
786 $is_private_or_non_published = ( 'private' === $post_status && ! $this->wordpress_adapter->current_user_can( 'read_private_posts', $post_id ) ) || ( 'publish' !== $post_status );
787
788 $can_read_template = $is_private_or_non_published || $this->wordpress_adapter->current_user_can( 'edit_post', $post_id );
789
790 return apply_filters( 'elementor/template-library/is_allowed_to_read_template', $can_read_template, $args );
791 }
792
793 private function should_check_permissions( array $args ): bool {
794 if ( null === $this->elementor_adapter ) {
795 $this->set_elementor_adapter( new Elementor_Adapter() );
796 }
797
798 // TODO: Remove $isWidgetTemplate in 3.28.0 as there is a Pro dependency
799 $check_permissions = isset( $args['check_permissions'] ) && false === $args['check_permissions'];
800 $is_widget_template = 'widget' === $this->elementor_adapter->get_template_type( $args['template_id'] );
801
802 if ( $check_permissions || $is_widget_template ) {
803 return false;
804 }
805
806 return true;
807 }
808 }
809