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-beta3 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 All 452 releases
← All changes | includes/template-library/manager.php +73 -417 4.0.73.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,11 +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 -
31 31 /**
32 32 * Registered template sources.
33 33 *
34 34 * Holds a list of all the supported sources with their instances.
@@ -50,8 +50,18 @@
50 50 */
51 51 private $_import_images = null; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
52 52
53 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 + /**
54 64 * Template library manager constructor.
55 65 *
56 66 * Initializing the template library manager by registering default template
57 67 * sources and initializing ajax calls.
@@ -93,8 +103,16 @@
93 103
94 104 return $this->_import_images;
95 105 }
96 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 +
97 115 /**
98 116 * Register template source.
99 117 *
100 118 * Used to register new template sources displayed in the template library.
@@ -220,12 +238,10 @@
220 238 *
221 239 * @return array Library data.
222 240 */
223 241 public function get_library_data( array $args ) {
224 - $force_update = ! empty( $args['sync'] );
242 + $library_data = Api::get_library_data( ! empty( $args['sync'] ) );
225 243
226 - $library_data = Api::get_library_data( $force_update );
227 -
228 244 if ( empty( $library_data ) ) {
229 245 return $library_data;
230 246 }
231 247
@@ -232,21 +248,14 @@
232 248 // Ensure all document are registered.
233 249 Plugin::$instance->documents->get_document_types();
234 250
235 251 $filter_sources = ! empty( $args['filter_sources'] ) ? $args['filter_sources'] : [];
252 + $force_update = ! empty( $args['sync'] );
236 253
237 - $full_library_data = [
254 + return [
238 255 'templates' => $this->get_templates( $filter_sources, $force_update ),
239 256 'config' => $library_data['types_data'],
240 257 ];
241 -
242 - /**
243 - * Filter the full library data.
244 - *
245 - * @since 3.32.2
246 - * @param-out $full_library_data - 'templates' and 'config' data ('config' holds the list of categories).
247 - */
248 - return apply_filters( 'elementor/library/full-data', $full_library_data );
249 258 }
250 259
251 260 /**
252 261 * Save template.
@@ -266,25 +275,12 @@
266 275 if ( is_wp_error( $validate_args ) ) {
267 276 return $validate_args;
268 277 }
269 278
270 - $sources = (array) $args['source']; // BC
271 - $results = [];
272 -
273 - foreach ( $sources as $source ) {
274 - $args_copy = $args;
275 - $args_copy['source'] = $source;
276 - $results[] = $this->save_template_item( $args_copy );
277 - }
278 -
279 - return 1 === count( $results ) ? $results[0] : $results;
280 - }
281 -
282 - private function save_template_item( array $args ) {
283 279 $source = $this->get_source( $args['source'] );
284 280
285 281 if ( ! $source ) {
286 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
282 + return new \WP_Error( 'template_error', 'Template source not found.' );
287 283 }
288 284
289 285 $args['content'] = json_decode( $args['content'], true );
290 286
@@ -300,97 +296,8 @@
300 296
301 297 return $source->get_item( $template_id );
302 298 }
303 299
304 - public function move_template( array $args ) {
305 - $validate_args = $this->ensure_args( [ 'source', 'from_source', 'from_template_id' ], $args );
306 -
307 - if ( is_wp_error( $validate_args ) ) {
308 - return $validate_args;
309 - }
310 -
311 - $args['source'] = $args['source'][0];
312 -
313 - $result = $this->move_template_item( $args );
314 -
315 - if ( ! $this->is_action_to_same_source( $args ) ) {
316 - $this->delete_template( [
317 - 'source' => $args['from_source'],
318 - 'template_id' => $args['from_template_id'],
319 - ] );
320 - }
321 -
322 - return $result;
323 - }
324 -
325 - private function move_template_item( array $args ) {
326 - $validate_args = $this->ensure_args( [ 'source', 'from_source', 'from_template_id' ], $args );
327 -
328 - if ( is_wp_error( $validate_args ) ) {
329 - return $validate_args;
330 - }
331 -
332 - $source = $this->get_source( $args['source'] );
333 -
334 - if ( ! $source ) {
335 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
336 - }
337 -
338 - if ( $this->is_action_to_same_source( $args ) ) {
339 - return $source->move_template_to_folder( $args );
340 - }
341 -
342 - $from_source = $this->get_source( $args['from_source'] );
343 -
344 - if ( ! $from_source ) {
345 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
346 - }
347 -
348 - $args = $from_source->format_args_for_single_action( $args );
349 -
350 - $template_id = $source->save_item( $args );
351 -
352 - if ( is_wp_error( $template_id ) ) {
353 - return $template_id;
354 - }
355 -
356 - return $source->get_item( $template_id );
357 - }
358 -
359 - public function copy_template( array $args ) {
360 - $validate_args = $this->ensure_args( [ 'source', 'from_source', 'from_template_id' ], $args );
361 -
362 - if ( is_wp_error( $validate_args ) ) {
363 - return $validate_args;
364 - }
365 -
366 - $source = $this->get_source( $args['source'][0] );
367 -
368 - if ( ! $source ) {
369 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
370 - }
371 -
372 - $from_source = $this->get_source( $args['from_source'] );
373 -
374 - if ( ! $from_source ) {
375 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
376 - }
377 -
378 - $args = $from_source->format_args_for_single_action( $args );
379 -
380 - $template_id = $source->save_item( $args );
381 -
382 - if ( is_wp_error( $template_id ) ) {
383 - return $template_id;
384 - }
385 -
386 - return $source->get_item( $template_id );
387 - }
388 -
389 - private function is_action_to_same_source( $args ) {
390 - return $args['source'] === $args['from_source'];
391 - }
392 -
393 300 /**
394 301 * Update template.
395 302 *
396 303 * Update template on the database.
@@ -412,9 +319,9 @@
412 319
413 320 $source = $this->get_source( $template_data['source'] );
414 321
415 322 if ( ! $source ) {
416 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
323 + return new \WP_Error( 'template_error', 'Template source not found.' );
417 324 }
418 325
419 326 $template_data['content'] = json_decode( $template_data['content'], true );
420 327
@@ -436,9 +343,9 @@
436 343
437 344 $source = $this->get_source( $template_data['source'] );
438 345
439 346 if ( ! $source ) {
440 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
347 + return new \WP_Error( 'template_error', 'Template source not found.' );
441 348 }
442 349
443 350 $update = $source->update_item( $template_data );
444 351
@@ -491,15 +398,9 @@
491 398 if ( is_wp_error( $validate_args ) ) {
492 399 return $validate_args;
493 400 }
494 401
495 - $source = $this->get_source( $args['source'] );
496 -
497 - if ( ! $source ) {
498 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
499 - }
500 -
501 - 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 ) ) {
502 403 return new \WP_Error(
503 404 'template_error',
504 405 esc_html__( 'You do not have permission to access this template.', 'elementor' )
505 406 );
@@ -508,8 +409,14 @@
508 409 if ( isset( $args['edit_mode'] ) ) {
509 410 Plugin::$instance->editor->set_edit_mode( $args['edit_mode'] );
510 411 }
511 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 +
512 419 do_action( 'elementor/template-library/before_get_source_data', $args, $source );
513 420
514 421 $data = $source->get_data( $args );
515 422
@@ -540,9 +447,9 @@
540 447
541 448 $source = $this->get_source( $args['source'] );
542 449
543 450 if ( ! $source ) {
544 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
451 + return new \WP_Error( 'template_error', 'Template source not found.' );
545 452 }
546 453
547 454 return $source->delete_template( $args['template_id'] );
548 455 }
@@ -569,9 +476,9 @@
569 476
570 477 $source = $this->get_source( $args['source'] );
571 478
572 479 if ( ! $source ) {
573 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
480 + return new \WP_Error( 'template_error', 'Template source not found' );
574 481 }
575 482
576 483 return $source->export_template( $args['template_id'] );
577 484 }
@@ -610,15 +517,17 @@
610 517 remove_filter( 'elementor/files/allow-file-type/json', [ $this, 'enable_json_template_upload' ] );
611 518 remove_filter( 'elementor/files/allow_unfiltered_upload', [ $this, 'enable_json_template_upload' ] );
612 519
613 520 if ( is_wp_error( $upload_result ) ) {
521 + Plugin::$instance->uploads_manager->remove_file_or_dir( dirname( $upload_result['tmp_name'] ) );
522 +
614 523 return $upload_result;
615 524 }
616 525
617 - $source = $this->get_source( $data['source'] ?? 'local' );
526 + /** @var Source_Local $source_local */
527 + $source_local = $this->get_source( 'local' );
618 528
619 - $import_mode = $data['import_mode'] ?? 'match_site';
620 - $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'] );
621 530
622 531 // Remove the temporary directory generated for the stream-uploaded file.
623 532 Plugin::$instance->uploads_manager->remove_file_or_dir( dirname( $upload_result['tmp_name'] ) );
624 533
@@ -681,92 +590,8 @@
681 590
682 591 return $import_data['content'];
683 592 }
684 593
685 - public function process_global_styles( array $args ) {
686 - $validate_args = $this->ensure_args( [ 'content', 'import_mode' ], $args );
687 -
688 - if ( is_wp_error( $validate_args ) ) {
689 - return $validate_args;
690 - }
691 -
692 - $import_mode = Template_Library_Import_Export_Utils::sanitize_import_mode( $args['import_mode'] );
693 -
694 - $content = $this->get_validated_json_arg( $args, 'content' );
695 - if ( is_wp_error( $content ) ) {
696 - return $content;
697 - }
698 -
699 - $global_classes = $this->get_validated_json_arg( $args, 'global_classes', true );
700 - if ( is_wp_error( $global_classes ) ) {
701 - return $global_classes;
702 - }
703 -
704 - $global_variables = $this->get_validated_json_arg( $args, 'global_variables', true );
705 - if ( is_wp_error( $global_variables ) ) {
706 - return $global_variables;
707 - }
708 -
709 - $data = [
710 - 'global_classes' => $global_classes,
711 - 'global_variables' => $global_variables,
712 - ];
713 -
714 - $result = apply_filters(
715 - 'elementor/template_library/import/process_content',
716 - [ 'content' => $content ],
717 - $import_mode,
718 - $data,
719 - null
720 - );
721 -
722 - $response = [
723 - 'content' => $result['content'],
724 - ];
725 -
726 - if ( ! empty( $result['updated_global_classes'] ) ) {
727 - $response['updated_global_classes'] = $result['updated_global_classes'];
728 - }
729 -
730 - if ( ! empty( $result['updated_global_variables'] ) ) {
731 - $response['updated_global_variables'] = $result['updated_global_variables'];
732 - }
733 -
734 - $classes_to_flatten = $result['classes_to_flatten'] ?? [];
735 - $variables_to_flatten = $result['variables_to_flatten'] ?? [];
736 -
737 - if ( ! empty( $classes_to_flatten ) ) {
738 - $response['flattened_classes_count'] = count( $classes_to_flatten );
739 - }
740 -
741 - if ( ! empty( $variables_to_flatten ) ) {
742 - $response['flattened_variables_count'] = count( $variables_to_flatten );
743 - }
744 -
745 - return $response;
746 - }
747 -
748 - private function get_validated_json_arg( array $args, string $key, bool $is_optional = false ) {
749 - if ( ! array_key_exists( $key, $args ) || null === $args[ $key ] || '' === $args[ $key ] ) {
750 - return $is_optional ? null : new \WP_Error( "invalid_$key", "Invalid $key." );
751 - }
752 -
753 - $value = $args[ $key ];
754 - if ( is_string( $value ) ) {
755 - $decoded = json_decode( $value, true );
756 - if ( JSON_ERROR_NONE !== json_last_error() ) {
757 - return new \WP_Error( "invalid_$key", "Invalid JSON $key." );
758 - }
759 - return $decoded;
760 - }
761 -
762 - if ( ! is_array( $value ) ) {
763 - return new \WP_Error( "invalid_$key", "Invalid $key format." );
764 - }
765 -
766 - return $value;
767 - }
768 -
769 594 public function get_item_children( array $args ) {
770 595 $validate_args = $this->ensure_args( [ 'source', 'template_id' ], $args );
771 596
772 597 if ( is_wp_error( $validate_args ) ) {
@@ -775,9 +600,9 @@
775 600
776 601 $source = $this->get_source( $args['source'] );
777 602
778 603 if ( ! $source ) {
779 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
604 + return new \WP_Error( 'template_error', 'Template source not found.' );
780 605 }
781 606
782 607 return $source->get_item_children( $args );
783 608 }
@@ -791,9 +616,9 @@
791 616
792 617 $source = $this->get_source( $args['source'] );
793 618
794 619 if ( ! $source ) {
795 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
620 + return new \WP_Error( 'template_error', 'Template source not found.' );
796 621 }
797 622
798 623 return $source->search_templates( $args );
799 624 }
@@ -807,9 +632,9 @@
807 632
808 633 $source = $this->get_source( $args['source'] );
809 634
810 635 if ( ! $source ) {
811 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
636 + return new \WP_Error( 'template_error', 'Template source not found.' );
812 637 }
813 638
814 639 return $source->get_items( $args );
815 640 }
@@ -823,32 +648,14 @@
823 648
824 649 $source = $this->get_source( $args['source'] );
825 650
826 651 if ( ! $source ) {
827 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
652 + return new \WP_Error( 'template_error', 'Template source not found.' );
828 653 }
829 654
830 655 return $source->save_folder( $args );
831 656 }
832 657
833 - public function get_folders( array $args ) {
834 - $validate_args = $this->ensure_args( [ 'source', 'offset' ], $args );
835 -
836 - if ( is_wp_error( $validate_args ) ) {
837 - return $validate_args;
838 - }
839 -
840 - $source = $this->get_source( $args['source'] );
841 -
842 - if ( ! $source ) {
843 - return new \WP_Error( 'template_error', 'Folder source not found.' );
844 - }
845 -
846 - $args['templateType'] = 'folder';
847 -
848 - return $source->get_items( $args );
849 - }
850 -
851 658 /**
852 659 * Register default template sources.
853 660 *
854 661 * Register the 'local' and 'remote' template sources that Elementor use by
@@ -860,11 +667,14 @@
860 667 private function register_default_sources() {
861 668 $sources = [
862 669 'local',
863 670 'remote',
864 - 'cloud',
865 671 ];
866 672
673 + if ( Plugin::$instance->experiments->is_feature_active( 'cloud-library' ) ) {
674 + $sources[] = 'cloud';
675 + }
676 +
867 677 foreach ( $sources as $source_filename ) {
868 678 $class_name = ucwords( $source_filename );
869 679 $class_name = str_replace( '-', '_', $class_name );
870 680
@@ -884,9 +694,9 @@
884 694 *
885 695 * @param array $data
886 696 *
887 697 * @return mixed
888 - * @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.
889 699 */
890 700 private function handle_ajax_request( $ajax_request, array $data ) {
891 701 if ( ! User::is_current_user_can_edit_post_type( Source_Local::CPT ) ) {
892 702 throw new \Exception( 'Access denied.' );
@@ -907,80 +717,12 @@
907 717 if ( is_wp_error( $result ) ) {
908 718 throw new \Exception( esc_html( $result->get_error_message() ) );
909 719 }
910 720
911 - return $result;
721 + return $result; // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
912 722 }
913 723
914 724 /**
915 - * @throws \Exception If template import fails, file validation errors occur, or processing encounters issues.
916 - */
917 - public function save_template_screenshot( $data ): string {
918 - $validate_args = $this->ensure_args( [ 'template_id', 'screenshot' ], $data );
919 -
920 - if ( is_wp_error( $validate_args ) ) {
921 - return $validate_args;
922 - }
923 -
924 - $raw_binary = base64_decode( substr( $data['screenshot'], strlen( 'data:image/png;base64,' ) ) );
925 -
926 - return $this->get_source( 'cloud' )->save_item_preview( $data['template_id'], $raw_binary );
927 - }
928 -
929 - /**
930 - * @throws \Exception If template processing fails or data validation errors occur.
931 - */
932 - public function template_screenshot_failed( $data ): string {
933 - $validate_args = $this->ensure_args( [ 'template_id' ], $data );
934 -
935 - if ( is_wp_error( $validate_args ) ) {
936 - return $validate_args;
937 - }
938 -
939 - return $this->get_source( 'cloud' )->mark_preview_as_failed( $data['template_id'], $data['error'] );
940 - }
941 -
942 - public function bulk_delete_templates( $data ) {
943 - $validate_args = $this->ensure_args( [ 'template_ids', 'source' ], $data );
944 -
945 - if ( is_wp_error( $validate_args ) ) {
946 - return $validate_args;
947 - }
948 -
949 - $source = $this->get_source( $data['source'] );
950 -
951 - if ( ! $source ) {
952 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
953 - }
954 -
955 - if ( empty( $data['template_ids'] ) || ! is_array( $data['template_ids'] ) ) {
956 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_IDS_MISSING );
957 - }
958 -
959 - return $source->bulk_delete_items( $data['template_ids'] );
960 - }
961 -
962 - public function bulk_undo_delete_items( $data ) {
963 - $validate_args = $this->ensure_args( [ 'template_ids', 'source' ], $data );
964 -
965 - if ( is_wp_error( $validate_args ) ) {
966 - return $validate_args;
967 - }
968 -
969 - $source = $this->get_source( $data['source'] );
970 -
971 - if ( ! $source ) {
972 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
973 - }
974 -
975 - if ( empty( $data['template_ids'] ) || ! is_array( $data['template_ids'] ) ) {
976 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_IDS_MISSING );
977 - }
978 -
979 - return $source->bulk_undo_delete_items( $data['template_ids'] );
980 - }
981 -
982 - /**
983 725 * Init ajax calls.
984 726 *
985 727 * Initialize template library ajax calls for allowed ajax requests.
986 728 *
@@ -1003,19 +745,8 @@
1003 745 'search_templates',
1004 746 'rename_template',
1005 747 'load_more_templates',
1006 748 'create_folder',
1007 - 'get_folders',
1008 - 'save_template_screenshot',
1009 - 'move_template',
1010 - 'copy_template',
1011 - 'bulk_move_templates',
1012 - 'bulk_delete_templates',
1013 - 'bulk_copy_templates',
1014 - 'bulk_undo_delete_items',
1015 - 'get_templates_quota',
1016 - 'template_screenshot_failed',
1017 - 'process_global_styles',
1018 749 ];
1019 750
1020 751 foreach ( $library_ajax_requests as $ajax_request ) {
1021 752 $ajax->register_ajax_action( $ajax_request, function( $data ) use ( $ajax_request ) {
@@ -1116,117 +847,42 @@
1116 847
1117 848 return true;
1118 849 }
1119 850
1120 - public function bulk_move_templates( array $args ) {
1121 - $validate_args = $this->ensure_args( [ 'source', 'from_source', 'from_template_id' ], $args );
1122 -
1123 - if ( is_wp_error( $validate_args ) ) {
1124 - 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;
1125 854 }
1126 855
1127 - $args['source'] = $args['source'][0];
1128 -
1129 - return $this->bulk_move_template_items( $args );
1130 - }
1131 -
1132 - private function bulk_move_template_items( array $args ) {
1133 - $source = $this->get_source( $args['source'] );
1134 -
1135 - if ( ! $source ) {
1136 - 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() );
1137 858 }
1138 859
1139 - if ( $this->is_action_to_same_source( $args ) ) {
1140 - return $source->move_bulk_templates_to_folder( $args );
860 + if ( ! $this->should_check_permissions( $args ) ) {
861 + return true;
1141 862 }
1142 863
1143 - $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 );
1144 867
1145 - if ( ! $from_source ) {
1146 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
1147 - }
868 + $can_read_template = $is_private_or_non_published || $this->wordpress_adapter->current_user_can( 'edit_post', $post_id );
1148 869
1149 - $bulk_args = $from_source->format_args_for_bulk_action( $args );
1150 -
1151 - if ( $source->supports_quota() && ! $this->is_action_to_same_source( $args ) ) {
1152 - $quota_validation = $source->validate_quota( $bulk_args );
1153 -
1154 - if ( is_wp_error( $quota_validation ) ) {
1155 - return $quota_validation;
1156 - }
1157 -
1158 - if ( false === $quota_validation ) {
1159 - return new \WP_Error( 'quota_error', 'The moving failed because it will pass the maximum templates you can save.' );
1160 - }
1161 - }
1162 -
1163 - $bulk_save = $source->save_bulk_items( $bulk_args );
1164 -
1165 - if ( ! empty( $bulk_save ) ) {
1166 - $this->bulk_delete_templates( [
1167 - 'template_ids' => $args['from_template_id'],
1168 - 'source' => $args['from_source'],
1169 - ] );
1170 - }
1171 -
1172 - return $bulk_save;
870 + return apply_filters( 'elementor/template-library/is_allowed_to_read_template', $can_read_template, $args );
1173 871 }
1174 872
1175 - public function bulk_copy_templates( array $args ) {
1176 - $validate_args = $this->ensure_args( [ 'source', 'from_source', 'from_template_id' ], $args );
1177 -
1178 - if ( is_wp_error( $validate_args ) ) {
1179 - 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() );
1180 876 }
1181 877
1182 - $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'] );
1183 881
1184 - return $this->bulk_copy_template_items( $args );
1185 - }
1186 -
1187 - private function bulk_copy_template_items( array $args ) {
1188 - $source = $this->get_source( $args['source'] );
1189 -
1190 - if ( ! $source ) {
1191 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
882 + if ( $check_permissions || $is_widget_template ) {
883 + return false;
1192 884 }
1193 885
1194 - $from_source = $this->get_source( $args['from_source'] );
1195 -
1196 - if ( ! $from_source ) {
1197 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
1198 - }
1199 -
1200 - $bulk_args = $from_source->format_args_for_bulk_action( $args );
1201 -
1202 - if ( $source->supports_quota() && ! $this->is_action_to_same_source( $args ) ) {
1203 - $quota_validation = $source->validate_quota( $bulk_args );
1204 -
1205 - if ( is_wp_error( $quota_validation ) ) {
1206 - return $quota_validation;
1207 - }
1208 -
1209 - if ( false === $quota_validation ) {
1210 - return new \WP_Error( 'quota_error', 'The copying failed because it will pass the maximum templates you can save.' );
1211 - }
1212 - }
1213 -
1214 - return $source->save_bulk_items( $bulk_args );
1215 - }
1216 -
1217 - public function get_templates_quota( array $args ) {
1218 - $validate_args = $this->ensure_args( [ 'source' ], $args );
1219 -
1220 - if ( is_wp_error( $validate_args ) ) {
1221 - return $validate_args;
1222 - }
1223 -
1224 - $source = $this->get_source( $args['source'] );
1225 -
1226 - if ( ! $source ) {
1227 - return new \WP_Error( 'template_error', 'Source not found.' );
1228 - }
1229 -
1230 - return $source->get_quota();
886 + return true;
1231 887 }
1232 888 }