PluginProbe
Elementor Website Builder – more than just a page builder / 3.28.0-dev2
Elementor Website Builder – more than just a page builder v3.28.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
← All changes | includes/template-library/manager.php +74 -435 4.2.43.28.0-dev2 View file →
@@ -2,12 +2,15 @@
2 2 namespace Elementor\TemplateLibrary;
3 3
4 4 use Elementor\Api;
5 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;
6 10 use Elementor\Core\Settings\Manager as SettingsManager;
7 11 use Elementor\Includes\TemplateLibrary\Data\Controller;
8 12 use Elementor\TemplateLibrary\Classes\Import_Images;
9 -use Elementor\Core\Utils\Template_Library_Import_Export_Utils;
10 13 use Elementor\Plugin;
11 14 use Elementor\User;
12 15 use Elementor\Utils;
13 16
@@ -24,12 +27,8 @@
24 27 * @since 1.0.0
25 28 */
26 29 class Manager {
27 30
28 - const ERROR_TEMPLATE_SOURCE_NOT_FOUND = 'Template source not found.';
29 - const ERROR_TEMPLATE_IDS_MISSING = 'Template IDs are missing.';
30 - const ERROR_JSON_UPLOAD_NOT_ALLOWED = 'Uploading JSON files is not allowed for this user.';
31 -
32 31 /**
33 32 * Registered template sources.
34 33 *
35 34 * Holds a list of all the supported sources with their instances.
@@ -51,8 +50,18 @@
51 50 */
52 51 private $_import_images = null; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
53 52
54 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 + /**
55 64 * Template library manager constructor.
56 65 *
57 66 * Initializing the template library manager by registering default template
58 67 * sources and initializing ajax calls.
@@ -94,8 +103,16 @@
94 103
95 104 return $this->_import_images;
96 105 }
97 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 +
98 115 /**
99 116 * Register template source.
100 117 *
101 118 * Used to register new template sources displayed in the template library.
@@ -221,12 +238,10 @@
221 238 *
222 239 * @return array Library data.
223 240 */
224 241 public function get_library_data( array $args ) {
225 - $force_update = ! empty( $args['sync'] );
242 + $library_data = Api::get_library_data( ! empty( $args['sync'] ) );
226 243
227 - $library_data = Api::get_library_data( $force_update );
228 -
229 244 if ( empty( $library_data ) ) {
230 245 return $library_data;
231 246 }
232 247
@@ -233,21 +248,14 @@
233 248 // Ensure all document are registered.
234 249 Plugin::$instance->documents->get_document_types();
235 250
236 251 $filter_sources = ! empty( $args['filter_sources'] ) ? $args['filter_sources'] : [];
252 + $force_update = ! empty( $args['sync'] );
237 253
238 - $full_library_data = [
254 + return [
239 255 'templates' => $this->get_templates( $filter_sources, $force_update ),
240 256 'config' => $library_data['types_data'],
241 257 ];
242 -
243 - /**
244 - * Filter the full library data.
245 - *
246 - * @since 3.32.2
247 - * @param-out $full_library_data - 'templates' and 'config' data ('config' holds the list of categories).
248 - */
249 - return apply_filters( 'elementor/library/full-data', $full_library_data );
250 258 }
251 259
252 260 /**
253 261 * Save template.
@@ -267,25 +275,12 @@
267 275 if ( is_wp_error( $validate_args ) ) {
268 276 return $validate_args;
269 277 }
270 278
271 - $sources = (array) $args['source']; // BC
272 - $results = [];
273 -
274 - foreach ( $sources as $source ) {
275 - $args_copy = $args;
276 - $args_copy['source'] = $source;
277 - $results[] = $this->save_template_item( $args_copy );
278 - }
279 -
280 - return 1 === count( $results ) ? $results[0] : $results;
281 - }
282 -
283 - private function save_template_item( array $args ) {
284 279 $source = $this->get_source( $args['source'] );
285 280
286 281 if ( ! $source ) {
287 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
282 + return new \WP_Error( 'template_error', 'Template source not found.' );
288 283 }
289 284
290 285 $args['content'] = json_decode( $args['content'], true );
291 286
@@ -301,97 +296,8 @@
301 296
302 297 return $source->get_item( $template_id );
303 298 }
304 299
305 - public function move_template( array $args ) {
306 - $validate_args = $this->ensure_args( [ 'source', 'from_source', 'from_template_id' ], $args );
307 -
308 - if ( is_wp_error( $validate_args ) ) {
309 - return $validate_args;
310 - }
311 -
312 - $args['source'] = $args['source'][0];
313 -
314 - $result = $this->move_template_item( $args );
315 -
316 - if ( ! $this->is_action_to_same_source( $args ) ) {
317 - $this->delete_template( [
318 - 'source' => $args['from_source'],
319 - 'template_id' => $args['from_template_id'],
320 - ] );
321 - }
322 -
323 - return $result;
324 - }
325 -
326 - private function move_template_item( array $args ) {
327 - $validate_args = $this->ensure_args( [ 'source', 'from_source', 'from_template_id' ], $args );
328 -
329 - if ( is_wp_error( $validate_args ) ) {
330 - return $validate_args;
331 - }
332 -
333 - $source = $this->get_source( $args['source'] );
334 -
335 - if ( ! $source ) {
336 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
337 - }
338 -
339 - if ( $this->is_action_to_same_source( $args ) ) {
340 - return $source->move_template_to_folder( $args );
341 - }
342 -
343 - $from_source = $this->get_source( $args['from_source'] );
344 -
345 - if ( ! $from_source ) {
346 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
347 - }
348 -
349 - $args = $from_source->format_args_for_single_action( $args );
350 -
351 - $template_id = $source->save_item( $args );
352 -
353 - if ( is_wp_error( $template_id ) ) {
354 - return $template_id;
355 - }
356 -
357 - return $source->get_item( $template_id );
358 - }
359 -
360 - public function copy_template( array $args ) {
361 - $validate_args = $this->ensure_args( [ 'source', 'from_source', 'from_template_id' ], $args );
362 -
363 - if ( is_wp_error( $validate_args ) ) {
364 - return $validate_args;
365 - }
366 -
367 - $source = $this->get_source( $args['source'][0] );
368 -
369 - if ( ! $source ) {
370 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
371 - }
372 -
373 - $from_source = $this->get_source( $args['from_source'] );
374 -
375 - if ( ! $from_source ) {
376 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
377 - }
378 -
379 - $args = $from_source->format_args_for_single_action( $args );
380 -
381 - $template_id = $source->save_item( $args );
382 -
383 - if ( is_wp_error( $template_id ) ) {
384 - return $template_id;
385 - }
386 -
387 - return $source->get_item( $template_id );
388 - }
389 -
390 - private function is_action_to_same_source( $args ) {
391 - return $args['source'] === $args['from_source'];
392 - }
393 -
394 300 /**
395 301 * Update template.
396 302 *
397 303 * Update template on the database.
@@ -413,9 +319,9 @@
413 319
414 320 $source = $this->get_source( $template_data['source'] );
415 321
416 322 if ( ! $source ) {
417 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
323 + return new \WP_Error( 'template_error', 'Template source not found.' );
418 324 }
419 325
420 326 $template_data['content'] = json_decode( $template_data['content'], true );
421 327
@@ -437,9 +343,9 @@
437 343
438 344 $source = $this->get_source( $template_data['source'] );
439 345
440 346 if ( ! $source ) {
441 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
347 + return new \WP_Error( 'template_error', 'Template source not found.' );
442 348 }
443 349
444 350 $update = $source->update_item( $template_data );
445 351
@@ -492,15 +398,9 @@
492 398 if ( is_wp_error( $validate_args ) ) {
493 399 return $validate_args;
494 400 }
495 401
496 - $source = $this->get_source( $args['source'] );
497 -
498 - if ( ! $source ) {
499 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
500 - }
501 -
502 - if ( method_exists( $source, 'is_allowed_to_read_template' ) && ! $source->is_allowed_to_read_template( $args ) ) {
402 + if ( ! $this->is_allowed_to_read_template( $args ) ) {
503 403 return new \WP_Error(
504 404 'template_error',
505 405 esc_html__( 'You do not have permission to access this template.', 'elementor' )
506 406 );
@@ -509,8 +409,14 @@
509 409 if ( isset( $args['edit_mode'] ) ) {
510 410 Plugin::$instance->editor->set_edit_mode( $args['edit_mode'] );
511 411 }
512 412
413 + $source = $this->get_source( $args['source'] );
414 +
415 + if ( ! $source ) {
416 + return new \WP_Error( 'template_error', 'Template source not found.' );
417 + }
418 +
513 419 do_action( 'elementor/template-library/before_get_source_data', $args, $source );
514 420
515 421 $data = $source->get_data( $args );
516 422
@@ -541,9 +447,9 @@
541 447
542 448 $source = $this->get_source( $args['source'] );
543 449
544 450 if ( ! $source ) {
545 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
451 + return new \WP_Error( 'template_error', 'Template source not found.' );
546 452 }
547 453
548 454 return $source->delete_template( $args['template_id'] );
549 455 }
@@ -570,9 +476,9 @@
570 476
571 477 $source = $this->get_source( $args['source'] );
572 478
573 479 if ( ! $source ) {
574 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
480 + return new \WP_Error( 'template_error', 'Template source not found' );
575 481 }
576 482
577 483 return $source->export_template( $args['template_id'] );
578 484 }
@@ -600,12 +506,8 @@
600 506 *
601 507 * @return mixed Whether the export succeeded or failed.
602 508 */
603 509 public function import_template( array $data ) {
604 - if ( ! User::is_current_user_can_upload_json() ) {
605 - return new \WP_Error( 'template_error', self::ERROR_JSON_UPLOAD_NOT_ALLOWED );
606 - }
607 -
608 510 // If the template is a JSON file, allow uploading it.
609 511 add_filter( 'elementor/files/allow-file-type/json', [ $this, 'enable_json_template_upload' ] );
610 512 add_filter( 'elementor/files/allow_unfiltered_upload', [ $this, 'enable_json_template_upload' ] );
611 513
@@ -615,15 +517,17 @@
615 517 remove_filter( 'elementor/files/allow-file-type/json', [ $this, 'enable_json_template_upload' ] );
616 518 remove_filter( 'elementor/files/allow_unfiltered_upload', [ $this, 'enable_json_template_upload' ] );
617 519
618 520 if ( is_wp_error( $upload_result ) ) {
521 + Plugin::$instance->uploads_manager->remove_file_or_dir( dirname( $upload_result['tmp_name'] ) );
522 +
619 523 return $upload_result;
620 524 }
621 525
622 - $source = $this->get_source( $data['source'] ?? 'local' );
526 + /** @var Source_Local $source_local */
527 + $source_local = $this->get_source( 'local' );
623 528
624 - $import_mode = $data['import_mode'] ?? 'match_site';
625 - $import_result = $source->import_template( $upload_result['name'], $upload_result['tmp_name'], $import_mode );
529 + $import_result = $source_local->import_template( $upload_result['name'], $upload_result['tmp_name'] );
626 530
627 531 // Remove the temporary directory generated for the stream-uploaded file.
628 532 Plugin::$instance->uploads_manager->remove_file_or_dir( dirname( $upload_result['tmp_name'] ) );
629 533
@@ -683,103 +587,11 @@
683 587 }
684 588
685 589 $import_data = $document->get_import_data( [ 'content' => $elements ] );
686 590
687 - $content = $document::on_import_update_dynamic_content(
688 - $import_data['content'],
689 - [ 'post_ids' => [] ]
690 - );
691 -
692 - return Plugin::$instance->db->iterate_data( $content, function( $element_data ) {
693 - unset( $element_data['htmlCache'] );
694 - return $element_data;
695 - } );
591 + return $import_data['content'];
696 592 }
697 593
698 - public function process_global_styles( array $args ) {
699 - $validate_args = $this->ensure_args( [ 'content', 'import_mode' ], $args );
700 -
701 - if ( is_wp_error( $validate_args ) ) {
702 - return $validate_args;
703 - }
704 -
705 - $import_mode = Template_Library_Import_Export_Utils::sanitize_import_mode( $args['import_mode'] );
706 -
707 - $content = $this->get_validated_json_arg( $args, 'content' );
708 - if ( is_wp_error( $content ) ) {
709 - return $content;
710 - }
711 -
712 - $global_classes = $this->get_validated_json_arg( $args, 'global_classes', true );
713 - if ( is_wp_error( $global_classes ) ) {
714 - return $global_classes;
715 - }
716 -
717 - $global_variables = $this->get_validated_json_arg( $args, 'global_variables', true );
718 - if ( is_wp_error( $global_variables ) ) {
719 - return $global_variables;
720 - }
721 -
722 - $data = [
723 - 'global_classes' => $global_classes,
724 - 'global_variables' => $global_variables,
725 - ];
726 -
727 - $result = apply_filters(
728 - 'elementor/template_library/import/process_content',
729 - [ 'content' => $content ],
730 - $import_mode,
731 - $data,
732 - null
733 - );
734 -
735 - $response = [
736 - 'content' => $result['content'],
737 - ];
738 -
739 - if ( ! empty( $result['updated_global_classes'] ) ) {
740 - $response['updated_global_classes'] = $result['updated_global_classes'];
741 - }
742 -
743 - if ( ! empty( $result['updated_global_variables'] ) ) {
744 - $response['updated_global_variables'] = $result['updated_global_variables'];
745 - }
746 -
747 - $classes_to_flatten = $result['classes_to_flatten'] ?? [];
748 - $variables_to_flatten = $result['variables_to_flatten'] ?? [];
749 -
750 - if ( ! empty( $classes_to_flatten ) ) {
751 - $response['flattened_classes_count'] = count( $classes_to_flatten );
752 - }
753 -
754 - if ( ! empty( $variables_to_flatten ) ) {
755 - $response['flattened_variables_count'] = count( $variables_to_flatten );
756 - }
757 -
758 - return $response;
759 - }
760 -
761 - private function get_validated_json_arg( array $args, string $key, bool $is_optional = false ) {
762 - if ( ! array_key_exists( $key, $args ) || null === $args[ $key ] || '' === $args[ $key ] ) {
763 - return $is_optional ? null : new \WP_Error( "invalid_$key", "Invalid $key." );
764 - }
765 -
766 - $value = $args[ $key ];
767 - if ( is_string( $value ) ) {
768 - $decoded = json_decode( $value, true );
769 - if ( JSON_ERROR_NONE !== json_last_error() ) {
770 - return new \WP_Error( "invalid_$key", "Invalid JSON $key." );
771 - }
772 - return $decoded;
773 - }
774 -
775 - if ( ! is_array( $value ) ) {
776 - return new \WP_Error( "invalid_$key", "Invalid $key format." );
777 - }
778 -
779 - return $value;
780 - }
781 -
782 594 public function get_item_children( array $args ) {
783 595 $validate_args = $this->ensure_args( [ 'source', 'template_id' ], $args );
784 596
785 597 if ( is_wp_error( $validate_args ) ) {
@@ -788,9 +600,9 @@
788 600
789 601 $source = $this->get_source( $args['source'] );
790 602
791 603 if ( ! $source ) {
792 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
604 + return new \WP_Error( 'template_error', 'Template source not found.' );
793 605 }
794 606
795 607 return $source->get_item_children( $args );
796 608 }
@@ -804,9 +616,9 @@
804 616
805 617 $source = $this->get_source( $args['source'] );
806 618
807 619 if ( ! $source ) {
808 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
620 + return new \WP_Error( 'template_error', 'Template source not found.' );
809 621 }
810 622
811 623 return $source->search_templates( $args );
812 624 }
@@ -820,9 +632,9 @@
820 632
821 633 $source = $this->get_source( $args['source'] );
822 634
823 635 if ( ! $source ) {
824 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
636 + return new \WP_Error( 'template_error', 'Template source not found.' );
825 637 }
826 638
827 639 return $source->get_items( $args );
828 640 }
@@ -836,32 +648,14 @@
836 648
837 649 $source = $this->get_source( $args['source'] );
838 650
839 651 if ( ! $source ) {
840 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
652 + return new \WP_Error( 'template_error', 'Template source not found.' );
841 653 }
842 654
843 655 return $source->save_folder( $args );
844 656 }
845 657
846 - public function get_folders( array $args ) {
847 - $validate_args = $this->ensure_args( [ 'source', 'offset' ], $args );
848 -
849 - if ( is_wp_error( $validate_args ) ) {
850 - return $validate_args;
851 - }
852 -
853 - $source = $this->get_source( $args['source'] );
854 -
855 - if ( ! $source ) {
856 - return new \WP_Error( 'template_error', 'Folder source not found.' );
857 - }
858 -
859 - $args['templateType'] = 'folder';
860 -
861 - return $source->get_items( $args );
862 - }
863 -
864 658 /**
865 659 * Register default template sources.
866 660 *
867 661 * Register the 'local' and 'remote' template sources that Elementor use by
@@ -873,11 +667,14 @@
873 667 private function register_default_sources() {
874 668 $sources = [
875 669 'local',
876 670 'remote',
877 - 'cloud',
878 671 ];
879 672
673 + if ( Plugin::$instance->experiments->is_feature_active( 'cloud-library' ) ) {
674 + $sources[] = 'cloud';
675 + }
676 +
880 677 foreach ( $sources as $source_filename ) {
881 678 $class_name = ucwords( $source_filename );
882 679 $class_name = str_replace( '-', '_', $class_name );
883 680
@@ -897,9 +694,9 @@
897 694 *
898 695 * @param array $data
899 696 *
900 697 * @return mixed
901 - * @throws \Exception If current user has no permission or the post is not found.
698 + * @throws \Exception If the user has no permission or the post is not found.
902 699 */
903 700 private function handle_ajax_request( $ajax_request, array $data ) {
904 701 if ( ! User::is_current_user_can_edit_post_type( Source_Local::CPT ) ) {
905 702 throw new \Exception( 'Access denied.' );
@@ -904,12 +701,8 @@
904 701 if ( ! User::is_current_user_can_edit_post_type( Source_Local::CPT ) ) {
905 702 throw new \Exception( 'Access denied.' );
906 703 }
907 704
908 - if ( 'import_template' === $ajax_request && ! User::is_current_user_can_upload_json() ) {
909 - throw new \Exception( 'Access denied.' );
910 - }
911 -
912 705 if ( ! empty( $data['editor_post_id'] ) ) {
913 706 $editor_post_id = absint( $data['editor_post_id'] );
914 707
915 708 if ( ! get_post( $editor_post_id ) ) {
@@ -924,80 +717,12 @@
924 717 if ( is_wp_error( $result ) ) {
925 718 throw new \Exception( esc_html( $result->get_error_message() ) );
926 719 }
927 720
928 - return $result;
721 + return $result; // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
929 722 }
930 723
931 724 /**
932 - * @throws \Exception If template import fails, file validation errors occur, or processing encounters issues.
933 - */
934 - public function save_template_screenshot( $data ): string {
935 - $validate_args = $this->ensure_args( [ 'template_id', 'screenshot' ], $data );
936 -
937 - if ( is_wp_error( $validate_args ) ) {
938 - return $validate_args;
939 - }
940 -
941 - $raw_binary = base64_decode( substr( $data['screenshot'], strlen( 'data:image/png;base64,' ) ) );
942 -
943 - return $this->get_source( 'cloud' )->save_item_preview( $data['template_id'], $raw_binary );
944 - }
945 -
946 - /**
947 - * @throws \Exception If template processing fails or data validation errors occur.
948 - */
949 - public function template_screenshot_failed( $data ): string {
950 - $validate_args = $this->ensure_args( [ 'template_id' ], $data );
951 -
952 - if ( is_wp_error( $validate_args ) ) {
953 - return $validate_args;
954 - }
955 -
956 - return $this->get_source( 'cloud' )->mark_preview_as_failed( $data['template_id'], $data['error'] );
957 - }
958 -
959 - public function bulk_delete_templates( $data ) {
960 - $validate_args = $this->ensure_args( [ 'template_ids', 'source' ], $data );
961 -
962 - if ( is_wp_error( $validate_args ) ) {
963 - return $validate_args;
964 - }
965 -
966 - $source = $this->get_source( $data['source'] );
967 -
968 - if ( ! $source ) {
969 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
970 - }
971 -
972 - if ( empty( $data['template_ids'] ) || ! is_array( $data['template_ids'] ) ) {
973 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_IDS_MISSING );
974 - }
975 -
976 - return $source->bulk_delete_items( $data['template_ids'] );
977 - }
978 -
979 - public function bulk_undo_delete_items( $data ) {
980 - $validate_args = $this->ensure_args( [ 'template_ids', 'source' ], $data );
981 -
982 - if ( is_wp_error( $validate_args ) ) {
983 - return $validate_args;
984 - }
985 -
986 - $source = $this->get_source( $data['source'] );
987 -
988 - if ( ! $source ) {
989 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
990 - }
991 -
992 - if ( empty( $data['template_ids'] ) || ! is_array( $data['template_ids'] ) ) {
993 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_IDS_MISSING );
994 - }
995 -
996 - return $source->bulk_undo_delete_items( $data['template_ids'] );
997 - }
998 -
999 - /**
1000 725 * Init ajax calls.
1001 726 *
1002 727 * Initialize template library ajax calls for allowed ajax requests.
1003 728 *
@@ -1020,19 +745,8 @@
1020 745 'search_templates',
1021 746 'rename_template',
1022 747 'load_more_templates',
1023 748 'create_folder',
1024 - 'get_folders',
1025 - 'save_template_screenshot',
1026 - 'move_template',
1027 - 'copy_template',
1028 - 'bulk_move_templates',
1029 - 'bulk_delete_templates',
1030 - 'bulk_copy_templates',
1031 - 'bulk_undo_delete_items',
1032 - 'get_templates_quota',
1033 - 'template_screenshot_failed',
1034 - 'process_global_styles',
1035 749 ];
1036 750
1037 751 foreach ( $library_ajax_requests as $ajax_request ) {
1038 752 $ajax->register_ajax_action( $ajax_request, function( $data ) use ( $ajax_request ) {
@@ -1133,117 +847,42 @@
1133 847
1134 848 return true;
1135 849 }
1136 850
1137 - public function bulk_move_templates( array $args ) {
1138 - $validate_args = $this->ensure_args( [ 'source', 'from_source', 'from_template_id' ], $args );
1139 -
1140 - if ( is_wp_error( $validate_args ) ) {
1141 - return $validate_args;
851 + private function is_allowed_to_read_template( array $args ): bool {
852 + if ( 'remote' === $args['source'] || 'cloud' === $args['source'] ) {
853 + return true;
1142 854 }
1143 855
1144 - $args['source'] = $args['source'][0];
1145 -
1146 - return $this->bulk_move_template_items( $args );
1147 - }
1148 -
1149 - private function bulk_move_template_items( array $args ) {
1150 - $source = $this->get_source( $args['source'] );
1151 -
1152 - if ( ! $source ) {
1153 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
856 + if ( null === $this->wordpress_adapter ) {
857 + $this->set_wordpress_adapter( new WordPress_Adapter() );
1154 858 }
1155 859
1156 - if ( $this->is_action_to_same_source( $args ) ) {
1157 - return $source->move_bulk_templates_to_folder( $args );
860 + if ( ! $this->should_check_permissions( $args ) ) {
861 + return true;
1158 862 }
1159 863
1160 - $from_source = $this->get_source( $args['from_source'] );
864 + $post_id = intval( $args['template_id'] );
865 + $post_status = $this->wordpress_adapter->get_post_status( $post_id );
866 + $is_private_or_non_published = ( 'private' === $post_status && ! $this->wordpress_adapter->current_user_can( 'read_private_posts', $post_id ) ) || ( 'publish' !== $post_status );
1161 867
1162 - if ( ! $from_source ) {
1163 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
1164 - }
868 + $can_read_template = $is_private_or_non_published || $this->wordpress_adapter->current_user_can( 'edit_post', $post_id );
1165 869
1166 - $bulk_args = $from_source->format_args_for_bulk_action( $args );
1167 -
1168 - if ( $source->supports_quota() && ! $this->is_action_to_same_source( $args ) ) {
1169 - $quota_validation = $source->validate_quota( $bulk_args );
1170 -
1171 - if ( is_wp_error( $quota_validation ) ) {
1172 - return $quota_validation;
1173 - }
1174 -
1175 - if ( false === $quota_validation ) {
1176 - return new \WP_Error( 'quota_error', 'The moving failed because it will pass the maximum templates you can save.' );
1177 - }
1178 - }
1179 -
1180 - $bulk_save = $source->save_bulk_items( $bulk_args );
1181 -
1182 - if ( ! empty( $bulk_save ) ) {
1183 - $this->bulk_delete_templates( [
1184 - 'template_ids' => $args['from_template_id'],
1185 - 'source' => $args['from_source'],
1186 - ] );
1187 - }
1188 -
1189 - return $bulk_save;
870 + return apply_filters( 'elementor/template-library/is_allowed_to_read_template', $can_read_template, $args );
1190 871 }
1191 872
1192 - public function bulk_copy_templates( array $args ) {
1193 - $validate_args = $this->ensure_args( [ 'source', 'from_source', 'from_template_id' ], $args );
1194 -
1195 - if ( is_wp_error( $validate_args ) ) {
1196 - return $validate_args;
873 + private function should_check_permissions( array $args ): bool {
874 + if ( null === $this->elementor_adapter ) {
875 + $this->set_elementor_adapter( new Elementor_Adapter() );
1197 876 }
1198 877
1199 - $args['source'] = $args['source'][0];
878 + // TODO: Remove $isWidgetTemplate in 3.28.0 as there is a Pro dependency
879 + $check_permissions = isset( $args['check_permissions'] ) && false === $args['check_permissions'];
880 + $is_widget_template = 'widget' === $this->elementor_adapter->get_template_type( $args['template_id'] );
1200 881
1201 - return $this->bulk_copy_template_items( $args );
1202 - }
1203 -
1204 - private function bulk_copy_template_items( array $args ) {
1205 - $source = $this->get_source( $args['source'] );
1206 -
1207 - if ( ! $source ) {
1208 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
882 + if ( $check_permissions || $is_widget_template ) {
883 + return false;
1209 884 }
1210 885
1211 - $from_source = $this->get_source( $args['from_source'] );
1212 -
1213 - if ( ! $from_source ) {
1214 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
1215 - }
1216 -
1217 - $bulk_args = $from_source->format_args_for_bulk_action( $args );
1218 -
1219 - if ( $source->supports_quota() && ! $this->is_action_to_same_source( $args ) ) {
1220 - $quota_validation = $source->validate_quota( $bulk_args );
1221 -
1222 - if ( is_wp_error( $quota_validation ) ) {
1223 - return $quota_validation;
1224 - }
1225 -
1226 - if ( false === $quota_validation ) {
1227 - return new \WP_Error( 'quota_error', 'The copying failed because it will pass the maximum templates you can save.' );
1228 - }
1229 - }
1230 -
1231 - return $source->save_bulk_items( $bulk_args );
1232 - }
1233 -
1234 - public function get_templates_quota( array $args ) {
1235 - $validate_args = $this->ensure_args( [ 'source' ], $args );
1236 -
1237 - if ( is_wp_error( $validate_args ) ) {
1238 - return $validate_args;
1239 - }
1240 -
1241 - $source = $this->get_source( $args['source'] );
1242 -
1243 - if ( ! $source ) {
1244 - return new \WP_Error( 'template_error', 'Source not found.' );
1245 - }
1246 -
1247 - return $source->get_quota();
886 + return true;
1248 887 }
1249 888 }