PluginProbe
Elementor Website Builder – more than just a page builder / 3.32.0-dev3
Elementor Website Builder – more than just a page builder v3.32.0-dev3
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 +222 -169 4.1.13.32.0-dev3 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.
@@ -233,20 +251,12 @@
233 251 Plugin::$instance->documents->get_document_types();
234 252
235 253 $filter_sources = ! empty( $args['filter_sources'] ) ? $args['filter_sources'] : [];
236 254
237 - $full_library_data = [
255 + return [
238 256 'templates' => $this->get_templates( $filter_sources, $force_update ),
239 257 'config' => $library_data['types_data'],
240 258 ];
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 259 }
250 260
251 261 /**
252 262 * Save template.
@@ -282,9 +292,9 @@
282 292 private function save_template_item( array $args ) {
283 293 $source = $this->get_source( $args['source'] );
284 294
285 295 if ( ! $source ) {
286 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
296 + return new \WP_Error( 'template_error', 'Template source not found.' );
287 297 }
288 298
289 299 $args['content'] = json_decode( $args['content'], true );
290 300
@@ -331,9 +341,9 @@
331 341
332 342 $source = $this->get_source( $args['source'] );
333 343
334 344 if ( ! $source ) {
335 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
345 + return new \WP_Error( 'template_error', 'Template source not found.' );
336 346 }
337 347
338 348 if ( $this->is_action_to_same_source( $args ) ) {
339 349 return $source->move_template_to_folder( $args );
@@ -338,16 +348,16 @@
338 348 if ( $this->is_action_to_same_source( $args ) ) {
339 349 return $source->move_template_to_folder( $args );
340 350 }
341 351
342 - $from_source = $this->get_source( $args['from_source'] );
352 + if ( 'local' === $args['from_source'] ) {
353 + $args = $this->format_args_for_single_action_from_local_to_cloud( $args );
354 + }
343 355
344 - if ( ! $from_source ) {
345 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
356 + if ( 'cloud' === $args['from_source'] ) {
357 + $args = $this->format_args_for_single_action_from_cloud_to_local( $args );
346 358 }
347 359
348 - $args = $from_source->format_args_for_single_action( $args );
349 -
350 360 $template_id = $source->save_item( $args );
351 361
352 362 if ( is_wp_error( $template_id ) ) {
353 363 return $template_id;
@@ -365,19 +375,19 @@
365 375
366 376 $source = $this->get_source( $args['source'][0] );
367 377
368 378 if ( ! $source ) {
369 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
379 + return new \WP_Error( 'template_error', 'Template source not found.' );
370 380 }
371 381
372 - $from_source = $this->get_source( $args['from_source'] );
382 + if ( 'local' === $args['from_source'] ) {
383 + $args = $this->format_args_for_single_action_from_local_to_cloud( $args );
384 + }
373 385
374 - if ( ! $from_source ) {
375 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
386 + if ( 'cloud' === $args['from_source'] ) {
387 + $args = $this->format_args_for_single_action_from_cloud_to_local( $args );
376 388 }
377 389
378 - $args = $from_source->format_args_for_single_action( $args );
379 -
380 390 $template_id = $source->save_item( $args );
381 391
382 392 if ( is_wp_error( $template_id ) ) {
383 393 return $template_id;
@@ -389,8 +399,53 @@
389 399 private function is_action_to_same_source( $args ) {
390 400 return $args['source'] === $args['from_source'];
391 401 }
392 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 +
393 448 /**
394 449 * Update template.
395 450 *
396 451 * Update template on the database.
@@ -412,9 +467,9 @@
412 467
413 468 $source = $this->get_source( $template_data['source'] );
414 469
415 470 if ( ! $source ) {
416 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
471 + return new \WP_Error( 'template_error', 'Template source not found.' );
417 472 }
418 473
419 474 $template_data['content'] = json_decode( $template_data['content'], true );
420 475
@@ -436,9 +491,9 @@
436 491
437 492 $source = $this->get_source( $template_data['source'] );
438 493
439 494 if ( ! $source ) {
440 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
495 + return new \WP_Error( 'template_error', 'Template source not found.' );
441 496 }
442 497
443 498 $update = $source->update_item( $template_data );
444 499
@@ -491,15 +546,9 @@
491 546 if ( is_wp_error( $validate_args ) ) {
492 547 return $validate_args;
493 548 }
494 549
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 ) ) {
550 + if ( ! $this->is_allowed_to_read_template( $args ) ) {
502 551 return new \WP_Error(
503 552 'template_error',
504 553 esc_html__( 'You do not have permission to access this template.', 'elementor' )
505 554 );
@@ -508,8 +557,14 @@
508 557 if ( isset( $args['edit_mode'] ) ) {
509 558 Plugin::$instance->editor->set_edit_mode( $args['edit_mode'] );
510 559 }
511 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 +
512 567 do_action( 'elementor/template-library/before_get_source_data', $args, $source );
513 568
514 569 $data = $source->get_data( $args );
515 570
@@ -540,9 +595,9 @@
540 595
541 596 $source = $this->get_source( $args['source'] );
542 597
543 598 if ( ! $source ) {
544 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
599 + return new \WP_Error( 'template_error', 'Template source not found.' );
545 600 }
546 601
547 602 return $source->delete_template( $args['template_id'] );
548 603 }
@@ -569,9 +624,9 @@
569 624
570 625 $source = $this->get_source( $args['source'] );
571 626
572 627 if ( ! $source ) {
573 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
628 + return new \WP_Error( 'template_error', 'Template source not found' );
574 629 }
575 630
576 631 return $source->export_template( $args['template_id'] );
577 632 }
@@ -610,15 +665,16 @@
610 665 remove_filter( 'elementor/files/allow-file-type/json', [ $this, 'enable_json_template_upload' ] );
611 666 remove_filter( 'elementor/files/allow_unfiltered_upload', [ $this, 'enable_json_template_upload' ] );
612 667
613 668 if ( is_wp_error( $upload_result ) ) {
669 + Plugin::$instance->uploads_manager->remove_file_or_dir( dirname( $upload_result['tmp_name'] ) );
670 +
614 671 return $upload_result;
615 672 }
616 673
617 674 $source = $this->get_source( $data['source'] ?? 'local' );
618 675
619 - $import_mode = $data['import_mode'] ?? 'match_site';
620 - $import_result = $source->import_template( $upload_result['name'], $upload_result['tmp_name'], $import_mode );
676 + $import_result = $source->import_template( $upload_result['name'], $upload_result['tmp_name'] );
621 677
622 678 // Remove the temporary directory generated for the stream-uploaded file.
623 679 Plugin::$instance->uploads_manager->remove_file_or_dir( dirname( $upload_result['tmp_name'] ) );
624 680
@@ -678,103 +734,11 @@
678 734 }
679 735
680 736 $import_data = $document->get_import_data( [ 'content' => $elements ] );
681 737
682 - $content = $document::on_import_update_dynamic_content(
683 - $import_data['content'],
684 - [ 'post_ids' => [] ]
685 - );
686 -
687 - return Plugin::$instance->db->iterate_data( $content, function( $element_data ) {
688 - unset( $element_data['htmlCache'] );
689 - return $element_data;
690 - } );
738 + return $import_data['content'];
691 739 }
692 740
693 - public function process_global_styles( array $args ) {
694 - $validate_args = $this->ensure_args( [ 'content', 'import_mode' ], $args );
695 -
696 - if ( is_wp_error( $validate_args ) ) {
697 - return $validate_args;
698 - }
699 -
700 - $import_mode = Template_Library_Import_Export_Utils::sanitize_import_mode( $args['import_mode'] );
701 -
702 - $content = $this->get_validated_json_arg( $args, 'content' );
703 - if ( is_wp_error( $content ) ) {
704 - return $content;
705 - }
706 -
707 - $global_classes = $this->get_validated_json_arg( $args, 'global_classes', true );
708 - if ( is_wp_error( $global_classes ) ) {
709 - return $global_classes;
710 - }
711 -
712 - $global_variables = $this->get_validated_json_arg( $args, 'global_variables', true );
713 - if ( is_wp_error( $global_variables ) ) {
714 - return $global_variables;
715 - }
716 -
717 - $data = [
718 - 'global_classes' => $global_classes,
719 - 'global_variables' => $global_variables,
720 - ];
721 -
722 - $result = apply_filters(
723 - 'elementor/template_library/import/process_content',
724 - [ 'content' => $content ],
725 - $import_mode,
726 - $data,
727 - null
728 - );
729 -
730 - $response = [
731 - 'content' => $result['content'],
732 - ];
733 -
734 - if ( ! empty( $result['updated_global_classes'] ) ) {
735 - $response['updated_global_classes'] = $result['updated_global_classes'];
736 - }
737 -
738 - if ( ! empty( $result['updated_global_variables'] ) ) {
739 - $response['updated_global_variables'] = $result['updated_global_variables'];
740 - }
741 -
742 - $classes_to_flatten = $result['classes_to_flatten'] ?? [];
743 - $variables_to_flatten = $result['variables_to_flatten'] ?? [];
744 -
745 - if ( ! empty( $classes_to_flatten ) ) {
746 - $response['flattened_classes_count'] = count( $classes_to_flatten );
747 - }
748 -
749 - if ( ! empty( $variables_to_flatten ) ) {
750 - $response['flattened_variables_count'] = count( $variables_to_flatten );
751 - }
752 -
753 - return $response;
754 - }
755 -
756 - private function get_validated_json_arg( array $args, string $key, bool $is_optional = false ) {
757 - if ( ! array_key_exists( $key, $args ) || null === $args[ $key ] || '' === $args[ $key ] ) {
758 - return $is_optional ? null : new \WP_Error( "invalid_$key", "Invalid $key." );
759 - }
760 -
761 - $value = $args[ $key ];
762 - if ( is_string( $value ) ) {
763 - $decoded = json_decode( $value, true );
764 - if ( JSON_ERROR_NONE !== json_last_error() ) {
765 - return new \WP_Error( "invalid_$key", "Invalid JSON $key." );
766 - }
767 - return $decoded;
768 - }
769 -
770 - if ( ! is_array( $value ) ) {
771 - return new \WP_Error( "invalid_$key", "Invalid $key format." );
772 - }
773 -
774 - return $value;
775 - }
776 -
777 741 public function get_item_children( array $args ) {
778 742 $validate_args = $this->ensure_args( [ 'source', 'template_id' ], $args );
779 743
780 744 if ( is_wp_error( $validate_args ) ) {
@@ -783,9 +747,9 @@
783 747
784 748 $source = $this->get_source( $args['source'] );
785 749
786 750 if ( ! $source ) {
787 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
751 + return new \WP_Error( 'template_error', 'Template source not found.' );
788 752 }
789 753
790 754 return $source->get_item_children( $args );
791 755 }
@@ -799,9 +763,9 @@
799 763
800 764 $source = $this->get_source( $args['source'] );
801 765
802 766 if ( ! $source ) {
803 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
767 + return new \WP_Error( 'template_error', 'Template source not found.' );
804 768 }
805 769
806 770 return $source->search_templates( $args );
807 771 }
@@ -815,9 +779,9 @@
815 779
816 780 $source = $this->get_source( $args['source'] );
817 781
818 782 if ( ! $source ) {
819 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
783 + return new \WP_Error( 'template_error', 'Template source not found.' );
820 784 }
821 785
822 786 return $source->get_items( $args );
823 787 }
@@ -831,9 +795,9 @@
831 795
832 796 $source = $this->get_source( $args['source'] );
833 797
834 798 if ( ! $source ) {
835 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
799 + return new \WP_Error( 'template_error', 'Template source not found.' );
836 800 }
837 801
838 802 return $source->save_folder( $args );
839 803 }
@@ -892,9 +856,9 @@
892 856 *
893 857 * @param array $data
894 858 *
895 859 * @return mixed
896 - * @throws \Exception If current user has no permission or the post is not found.
860 + * @throws \Exception If the user has no permission or the post is not found.
897 861 */
898 862 private function handle_ajax_request( $ajax_request, array $data ) {
899 863 if ( ! User::is_current_user_can_edit_post_type( Source_Local::CPT ) ) {
900 864 throw new \Exception( 'Access denied.' );
@@ -915,13 +879,13 @@
915 879 if ( is_wp_error( $result ) ) {
916 880 throw new \Exception( esc_html( $result->get_error_message() ) );
917 881 }
918 882
919 - return $result;
883 + return $result; // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
920 884 }
921 885
922 886 /**
923 - * @throws \Exception If template import fails, file validation errors occur, or processing encounters issues.
887 + * @throws \Exception
924 888 */
925 889 public function save_template_screenshot( $data ): string {
926 890 $validate_args = $this->ensure_args( [ 'template_id', 'screenshot' ], $data );
927 891
@@ -934,9 +898,9 @@
934 898 return $this->get_source( 'cloud' )->save_item_preview( $data['template_id'], $raw_binary );
935 899 }
936 900
937 901 /**
938 - * @throws \Exception If template processing fails or data validation errors occur.
902 + * @throws \Exception
939 903 */
940 904 public function template_screenshot_failed( $data ): string {
941 905 $validate_args = $this->ensure_args( [ 'template_id' ], $data );
942 906
@@ -956,13 +920,13 @@
956 920
957 921 $source = $this->get_source( $data['source'] );
958 922
959 923 if ( ! $source ) {
960 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
924 + return new \WP_Error( 'template_error', 'Template source not found.' );
961 925 }
962 926
963 927 if ( empty( $data['template_ids'] ) || ! is_array( $data['template_ids'] ) ) {
964 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_IDS_MISSING );
928 + return new \WP_Error( 'template_error', 'Template IDs are missing.' );
965 929 }
966 930
967 931 return $source->bulk_delete_items( $data['template_ids'] );
968 932 }
@@ -976,13 +940,13 @@
976 940
977 941 $source = $this->get_source( $data['source'] );
978 942
979 943 if ( ! $source ) {
980 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
944 + return new \WP_Error( 'template_error', 'Template source not found.' );
981 945 }
982 946
983 947 if ( empty( $data['template_ids'] ) || ! is_array( $data['template_ids'] ) ) {
984 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_IDS_MISSING );
948 + return new \WP_Error( 'template_error', 'Template IDs are missing.' );
985 949 }
986 950
987 951 return $source->bulk_undo_delete_items( $data['template_ids'] );
988 952 }
@@ -1021,9 +985,8 @@
1021 985 'bulk_copy_templates',
1022 986 'bulk_undo_delete_items',
1023 987 'get_templates_quota',
1024 988 'template_screenshot_failed',
1025 - 'process_global_styles',
1026 989 ];
1027 990
1028 991 foreach ( $library_ajax_requests as $ajax_request ) {
1029 992 $ajax->register_ajax_action( $ajax_request, function( $data ) use ( $ajax_request ) {
@@ -1124,8 +1087,46 @@
1124 1087
1125 1088 return true;
1126 1089 }
1127 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 +
1128 1129 public function bulk_move_templates( array $args ) {
1129 1130 $validate_args = $this->ensure_args( [ 'source', 'from_source', 'from_template_id' ], $args );
1130 1131
1131 1132 if ( is_wp_error( $validate_args ) ) {
@@ -1140,9 +1141,9 @@
1140 1141 private function bulk_move_template_items( array $args ) {
1141 1142 $source = $this->get_source( $args['source'] );
1142 1143
1143 1144 if ( ! $source ) {
1144 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
1145 + return new \WP_Error( 'template_error', 'Template source not found.' );
1145 1146 }
1146 1147
1147 1148 if ( $this->is_action_to_same_source( $args ) ) {
1148 1149 return $source->move_bulk_templates_to_folder( $args );
@@ -1147,24 +1148,20 @@
1147 1148 if ( $this->is_action_to_same_source( $args ) ) {
1148 1149 return $source->move_bulk_templates_to_folder( $args );
1149 1150 }
1150 1151
1151 - $from_source = $this->get_source( $args['from_source'] );
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 );
1152 1155
1153 - if ( ! $from_source ) {
1154 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
1155 - }
1156 -
1157 - $bulk_args = $from_source->format_args_for_bulk_action( $args );
1158 -
1159 1156 if ( $source->supports_quota() && ! $this->is_action_to_same_source( $args ) ) {
1160 - $quota_validation = $source->validate_quota( $bulk_args );
1157 + $is_quota_valid = $source->validate_quota( $bulk_args );
1161 1158
1162 - if ( is_wp_error( $quota_validation ) ) {
1163 - return $quota_validation;
1159 + if ( is_wp_error( $is_quota_valid ) ) {
1160 + return $is_quota_valid;
1164 1161 }
1165 1162
1166 - if ( false === $quota_validation ) {
1163 + if ( ! $is_quota_valid ) {
1167 1164 return new \WP_Error( 'quota_error', 'The moving failed because it will pass the maximum templates you can save.' );
1168 1165 }
1169 1166 }
1170 1167
@@ -1179,8 +1176,68 @@
1179 1176
1180 1177 return $bulk_save;
1181 1178 }
1182 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 +
1183 1240 public function bulk_copy_templates( array $args ) {
1184 1241 $validate_args = $this->ensure_args( [ 'source', 'from_source', 'from_template_id' ], $args );
1185 1242
1186 1243 if ( is_wp_error( $validate_args ) ) {
@@ -1195,27 +1252,23 @@
1195 1252 private function bulk_copy_template_items( array $args ) {
1196 1253 $source = $this->get_source( $args['source'] );
1197 1254
1198 1255 if ( ! $source ) {
1199 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
1256 + return new \WP_Error( 'template_error', 'Template source not found.' );
1200 1257 }
1201 1258
1202 - $from_source = $this->get_source( $args['from_source'] );
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 );
1203 1262
1204 - if ( ! $from_source ) {
1205 - return new \WP_Error( 'template_error', self::ERROR_TEMPLATE_SOURCE_NOT_FOUND );
1206 - }
1207 -
1208 - $bulk_args = $from_source->format_args_for_bulk_action( $args );
1209 -
1210 1263 if ( $source->supports_quota() && ! $this->is_action_to_same_source( $args ) ) {
1211 - $quota_validation = $source->validate_quota( $bulk_args );
1264 + $is_quota_valid = $source->validate_quota( $bulk_args );
1212 1265
1213 - if ( is_wp_error( $quota_validation ) ) {
1214 - return $quota_validation;
1266 + if ( is_wp_error( $is_quota_valid ) ) {
1267 + return $is_quota_valid;
1215 1268 }
1216 1269
1217 - if ( false === $quota_validation ) {
1270 + if ( ! $is_quota_valid ) {
1218 1271 return new \WP_Error( 'quota_error', 'The copying failed because it will pass the maximum templates you can save.' );
1219 1272 }
1220 1273 }
1221 1274