PluginProbe
Elementor Website Builder – more than just a page builder / 3.9.2
Elementor Website Builder – more than just a page builder v3.9.2
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/sources/local.php +207 -503 4.2.43.9.2 View file →
@@ -1,12 +1,14 @@
1 1 <?php
2 2 namespace Elementor\TemplateLibrary;
3 3
4 +use Elementor\Core\Admin\Menu\Admin_Menu_Manager;
4 5 use Elementor\Core\Base\Document;
5 6 use Elementor\Core\Editor\Editor;
6 7 use Elementor\Core\Utils\Collection;
7 8 use Elementor\DB;
8 9 use Elementor\Core\Settings\Manager as SettingsManager;
10 +use Elementor\Core\Settings\Page\Model;
9 11 use Elementor\Includes\TemplateLibrary\Sources\AdminMenuItems\Add_New_Template_Menu_Item;
10 12 use Elementor\Includes\TemplateLibrary\Sources\AdminMenuItems\Saved_Templates_Menu_Item;
11 13 use Elementor\Includes\TemplateLibrary\Sources\AdminMenuItems\Templates_Categories_Menu_Item;
12 14 use Elementor\Modules\Library\Documents\Library_Document;
@@ -11,15 +13,8 @@
11 13 use Elementor\Includes\TemplateLibrary\Sources\AdminMenuItems\Templates_Categories_Menu_Item;
12 14 use Elementor\Modules\Library\Documents\Library_Document;
13 15 use Elementor\Plugin;
14 16 use Elementor\Utils;
15 -use Elementor\User;
16 -use Elementor\Core\Isolation\Wordpress_Adapter;
17 -use Elementor\Core\Isolation\Wordpress_Adapter_Interface;
18 -use Elementor\Core\Isolation\Elementor_Adapter_Interface;
19 -use Elementor\Modules\EditorOne\Classes\Menu_Data_Provider;
20 -use Elementor\Includes\TemplateLibrary\Sources\AdminMenuItems\Editor_One_Saved_Templates_Menu;
21 -use Elementor\Includes\TemplateLibrary\Sources\AdminMenuItems\Editor_One_Templates_Menu;
22 17
23 18 if ( ! defined( 'ABSPATH' ) ) {
24 19 exit; // Exit if accessed directly.
25 20 }
@@ -50,10 +45,9 @@
50 45 const TAXONOMY_CATEGORY_SLUG = 'elementor_library_category';
51 46
52 47 /**
53 48 * Elementor template-library meta key.
54 - *
55 - * @deprecated 2.3.0 Use `Elementor\Core\Base\Document::TYPE_META_KEY` const instead.
49 + * @deprecated 2.3.0 Use \Elementor\Core\Base\Document::TYPE_META_KEY instead
56 50 */
57 51 const TYPE_META_KEY = '_elementor_template_type';
58 52
59 53 /**
@@ -95,18 +89,8 @@
95 89 */
96 90 private $post_type_object;
97 91
98 92 /**
99 - * @var Wordpress_Adapter_Interface
100 - */
101 - protected $wordpress_adapter = null;
102 -
103 - /**
104 - * @var Elementor_Adapter_Interface
105 - */
106 - protected $elementor_adapter = null;
107 -
108 - /**
109 93 * @since 2.3.0
110 94 * @access public
111 95 * @static
112 96 * @return array
@@ -236,21 +220,29 @@
236 220 * @since 1.0.0
237 221 * @access public
238 222 */
239 223 public function register_data() {
224 + $admin_menu_rearrangement_active = Plugin::$instance->experiments->is_feature_active( 'admin_menu_rearrangement' );
225 +
226 + if ( $admin_menu_rearrangement_active ) {
227 + $name = esc_html_x( 'Templates', 'Template Library', 'elementor' );
228 + } else {
229 + $name = esc_html_x( 'My Templates', 'Template Library', 'elementor' );
230 + }
231 +
240 232 $labels = [
241 - 'name' => esc_html_x( 'My Templates', 'Template Library', 'elementor' ),
233 + 'name' => $name,
242 234 'singular_name' => esc_html_x( 'Template', 'Template Library', 'elementor' ),
243 - 'add_new' => esc_html__( 'Add New Template', 'elementor' ),
244 - 'add_new_item' => esc_html__( 'Add New Template', 'elementor' ),
245 - 'edit_item' => esc_html__( 'Edit Template', 'elementor' ),
246 - 'new_item' => esc_html__( 'New Template', 'elementor' ),
247 - 'all_items' => esc_html__( 'All Templates', 'elementor' ),
248 - 'view_item' => esc_html__( 'View Template', 'elementor' ),
249 - 'search_items' => esc_html__( 'Search Template', 'elementor' ),
250 - 'not_found' => esc_html__( 'No Templates found', 'elementor' ),
251 - 'not_found_in_trash' => esc_html__( 'No Templates found in Trash', 'elementor' ),
252 - 'parent_item_colon' => esc_html__( 'Parent Template:', 'elementor' ),
235 + 'add_new' => esc_html_x( 'Add New', 'Template Library', 'elementor' ),
236 + 'add_new_item' => esc_html_x( 'Add New Template', 'Template Library', 'elementor' ),
237 + 'edit_item' => esc_html_x( 'Edit Template', 'Template Library', 'elementor' ),
238 + 'new_item' => esc_html_x( 'New Template', 'Template Library', 'elementor' ),
239 + 'all_items' => esc_html_x( 'All Templates', 'Template Library', 'elementor' ),
240 + 'view_item' => esc_html_x( 'View Template', 'Template Library', 'elementor' ),
241 + 'search_items' => esc_html_x( 'Search Template', 'Template Library', 'elementor' ),
242 + 'not_found' => esc_html_x( 'No Templates found', 'Template Library', 'elementor' ),
243 + 'not_found_in_trash' => esc_html_x( 'No Templates found in Trash', 'Template Library', 'elementor' ),
244 + 'parent_item_colon' => '',
253 245 'menu_name' => esc_html_x( 'Templates', 'Template Library', 'elementor' ),
254 246 ];
255 247
256 248 $args = [
@@ -258,19 +250,16 @@
258 250 'public' => true,
259 251 'rewrite' => false,
260 252 'menu_icon' => 'dashicons-admin-page',
261 253 'show_ui' => true,
262 - 'show_in_menu' => true,
254 + 'show_in_menu' => ! $admin_menu_rearrangement_active,
263 255 'show_in_nav_menus' => false,
264 256 'exclude_from_search' => true,
265 257 'capability_type' => 'post',
266 258 'hierarchical' => false,
267 - 'supports' => [ 'title', 'thumbnail', 'author', 'elementor', 'custom-fields' ],
268 - 'show_in_rest' => true,
259 + 'supports' => [ 'title', 'thumbnail', 'author', 'elementor' ],
269 260 ];
270 261
271 - $this->avoid_rest_access_for_non_admins();
272 -
273 262 /**
274 263 * Register template library post type args.
275 264 *
276 265 * Filters the post type arguments when registering elementor template library post type.
@@ -351,14 +340,79 @@
351 340
352 341 register_taxonomy( self::TAXONOMY_CATEGORY_SLUG, self::CPT, $args );
353 342 }
354 343
344 + /**
345 + * Remove Add New item from admin menu.
346 + *
347 + * Fired by `admin_menu` action.
348 + *
349 + * @since 2.4.0
350 + * @access public
351 + */
352 + private function admin_menu_reorder( Admin_Menu_Manager $admin_menu ) {
353 + global $submenu;
355 354
356 - private function register_editor_one_menu( Menu_Data_Provider $menu_data_provider ) {
357 - $menu_data_provider->register_menu( new Editor_One_Templates_Menu() );
358 - $menu_data_provider->register_menu( new Editor_One_Saved_Templates_Menu() );
355 + if ( ! isset( $submenu[ static::ADMIN_MENU_SLUG ] ) ) {
356 + return;
357 + }
358 +
359 + remove_submenu_page( static::ADMIN_MENU_SLUG, static::ADMIN_MENU_SLUG );
360 +
361 + $add_new_slug = 'post-new.php?post_type=' . static::CPT;
362 + $category_slug = 'edit-tags.php?taxonomy=' . static::TAXONOMY_CATEGORY_SLUG . '&amp;post_type=' . static::CPT;
363 +
364 + $library_submenu = new Collection( $submenu[ static::ADMIN_MENU_SLUG ] );
365 +
366 + $add_new_item = $library_submenu->find( function ( $item ) use ( $add_new_slug ) {
367 + return $add_new_slug === $item[2];
368 + } );
369 +
370 + $categories_item = $library_submenu->find( function ( $item ) use ( $category_slug ) {
371 + return $category_slug === $item[2];
372 + } );
373 +
374 + if ( $add_new_item ) {
375 + remove_submenu_page( static::ADMIN_MENU_SLUG, $add_new_slug );
376 +
377 + $admin_menu->register( admin_url( static::ADMIN_MENU_SLUG . '#add_new' ), new Add_New_Template_Menu_Item() );
378 + }
379 +
380 + if ( $categories_item ) {
381 + remove_submenu_page( static::ADMIN_MENU_SLUG, $category_slug );
382 +
383 + $admin_menu->register( $category_slug, new Templates_Categories_Menu_Item() );
384 + }
359 385 }
360 386
387 + /**
388 + * Add a `current` CSS class to the `Saved Templates` submenu page when it's active.
389 + * It should work by default, but since we interfere with WordPress' builtin CPT menus it doesn't work properly.
390 + *
391 + * @return void
392 + */
393 + private function admin_menu_set_current() {
394 + global $submenu;
395 +
396 + if ( $this->is_current_screen() ) {
397 + $library_submenu = &$submenu[ static::ADMIN_MENU_SLUG ];
398 + $library_title = $this->get_library_title();
399 +
400 + foreach ( $library_submenu as &$item ) {
401 + if ( $library_title === $item[0] ) {
402 + if ( ! isset( $item[4] ) ) {
403 + $item[4] = '';
404 + }
405 + $item[4] .= ' current';
406 + }
407 + }
408 + }
409 + }
410 +
411 + private function register_admin_menu( Admin_Menu_Manager $admin_menu ) {
412 + $admin_menu->register( static::get_admin_url( true ), new Saved_Templates_Menu_Item() );
413 + }
414 +
361 415 public function admin_title( $admin_title, $title ) {
362 416 $library_title = $this->get_library_title();
363 417
364 418 if ( $library_title ) {
@@ -368,54 +422,15 @@
368 422 return $admin_title;
369 423 }
370 424
371 425 public function replace_admin_heading() {
372 - if ( ! $this->is_current_screen() ) {
373 - return;
374 - }
375 -
376 - global $post_type_object;
377 -
378 426 $library_title = $this->get_library_title();
379 427
380 428 if ( $library_title ) {
429 + global $post_type_object;
430 +
381 431 $post_type_object->labels->name = $library_title;
382 432 }
383 -
384 - $labels = $this->get_current_template_labels();
385 - $template_singular = $labels['singular'];
386 - $template_plural = $labels['plural'];
387 -
388 - if ( ! $template_plural && $library_title ) {
389 - $template_plural = $library_title;
390 - }
391 -
392 - if ( $template_singular ) {
393 - $labels = $post_type_object->labels;
394 - $labels->name = $template_plural;
395 - $labels->singular_name = $template_singular;
396 - /* translators: %s: Template label (plural). */
397 - $labels->all_items = sprintf( esc_html__( 'All %s', 'elementor' ), $template_plural );
398 - /* translators: %s: Template label (singular). */
399 - $labels->add_new = sprintf( esc_html__( 'Add New %s', 'elementor' ), $template_singular );
400 - $labels->add_new_item = $labels->add_new;
401 - /* translators: %s: Template label (singular). */
402 - $labels->edit_item = sprintf( esc_html__( 'Edit %s', 'elementor' ), $template_singular );
403 - /* translators: %s: Template label (singular). */
404 - $labels->new_item = sprintf( esc_html__( 'New %s', 'elementor' ), $template_singular );
405 - /* translators: %s: Template label (singular). */
406 - $labels->view_item = sprintf( esc_html__( 'View %s', 'elementor' ), $template_singular );
407 - /* translators: %s: Template label (plural). */
408 - $labels->search_items = sprintf( esc_html__( 'Search %s', 'elementor' ), $template_plural );
409 - /* translators: %s: Template label (plural). */
410 - $labels->not_found = sprintf( esc_html__( 'No %s found', 'elementor' ), $template_plural );
411 - /* translators: %s: Template label (plural). */
412 - $labels->not_found_in_trash = sprintf( esc_html__( 'No %s found in Trash', 'elementor' ), $template_plural );
413 - /* translators: %s: Template label (singular). */
414 - $labels->parent_item_colon = sprintf( esc_html__( 'Parent %s:', 'elementor' ), $template_singular );
415 - $labels->menu_name = $template_plural;
416 - $post_type_object->labels = $labels;
417 - }
418 433 }
419 434
420 435 /**
421 436 * Get local templates.
@@ -486,24 +501,13 @@
486 501
487 502 $defaults = [
488 503 'title' => esc_html__( '(no title)', 'elementor' ),
489 504 'page_settings' => [],
505 + 'status' => current_user_can( 'publish_posts' ) ? 'publish' : 'pending',
490 506 ];
491 507
492 508 $template_data = wp_parse_args( $template_data, $defaults );
493 - $template_data['status'] = current_user_can( 'publish_posts' ) ? 'publish' : 'pending';
494 509
495 - // BC: Allow importing any template type when using CLI
496 - // to support users that rely on this mechanism.
497 - $should_check_template_type = ! $this->is_wp_cli();
498 -
499 - if (
500 - $should_check_template_type &&
501 - ! $this->is_valid_template_type( $template_data['type'] )
502 - ) {
503 - return new \WP_Error( 'invalid_template_type', esc_html__( 'Invalid template type.', 'elementor' ) );
504 - }
505 -
506 510 $document = Plugin::$instance->documents->create(
507 511 $template_data['type'],
508 512 [
509 513 'post_title' => $template_data['title'],
@@ -555,35 +559,8 @@
555 559
556 560 return $template_id;
557 561 }
558 562
559 - protected function is_valid_template_type( $type ) {
560 - $document_class = Plugin::$instance->documents->get_document_type( $type, false );
561 -
562 - if ( ! $document_class ) {
563 - return false;
564 - }
565 -
566 - $cpt = $document_class::get_property( 'cpt' );
567 -
568 - if ( ! $cpt || ! is_array( $cpt ) || 1 !== count( $cpt ) ) {
569 - return false;
570 - }
571 -
572 - $is_valid_template_type = in_array( static::CPT, $cpt, true );
573 -
574 - return apply_filters(
575 - 'elementor/template_library/sources/local/is_valid_template_type',
576 - $is_valid_template_type,
577 - $cpt,
578 - );
579 - }
580 -
581 - /** For testing purposes only, in order to be able to mock the `WP_CLI` constant. */
582 - protected function is_wp_cli() {
583 - return Utils::is_wp_cli();
584 - }
585 -
586 563 /**
587 564 * Update local template.
588 565 *
589 566 * Update template on the database.
@@ -605,20 +582,12 @@
605 582 if ( ! $document ) {
606 583 return new \WP_Error( 'save_error', esc_html__( 'Template not exist.', 'elementor' ) );
607 584 }
608 585
609 - $save_data = [];
586 + $document->save( [
587 + 'elements' => $new_data['content'],
588 + ] );
610 589
611 - if ( isset( $new_data['title'] ) ) {
612 - $save_data['post_title'] = $new_data['title'];
613 - }
614 -
615 - if ( isset( $new_data['content'] ) ) {
616 - $save_data['elements'] = $new_data['content'];
617 - }
618 -
619 - $document->save( $save_data );
620 -
621 590 /**
622 591 * After template library update.
623 592 *
624 593 * Fires after Elementor template library was updated.
@@ -715,22 +684,8 @@
715 684 }
716 685
717 686 if ( ! empty( $content ) ) {
718 687 $content = $this->replace_elements_ids( $content );
719 -
720 - /**
721 - * Filters the template content data.
722 - *
723 - * This filter allows third-party plugins to modify template content
724 - * when loaded via get_template_data(), making the behavior consistent
725 - * with Frontend::get_builder_content().
726 - *
727 - * @since 3.34.0
728 - *
729 - * @param array $content The template content data.
730 - * @param int $template_id The template ID.
731 - */
732 - $content = apply_filters( 'elementor/frontend/builder_content_data', $content, $template_id );
733 688 }
734 689 }
735 690
736 691 $data = [
@@ -742,12 +697,8 @@
742 697
743 698 $data['page_settings'] = $page->get_data( 'settings' );
744 699 }
745 700
746 - if ( ! empty( $content ) ) {
747 - $this->attach_global_styles_to_data( $data, $content, $template_id );
748 - }
749 -
750 701 return $data;
751 702 }
752 703
753 704 /**
@@ -770,22 +721,8 @@
770 721
771 722 return wp_delete_post( $template_id, true );
772 723 }
773 724
774 - public function bulk_delete_items( array $template_ids ) {
775 - foreach ( $template_ids as $template_id ) {
776 - if ( ! current_user_can( $this->post_type_object->cap->delete_post, $template_id ) ) {
777 - return new \WP_Error( 'template_error', esc_html__( 'Access denied.', 'elementor' ) );
778 - }
779 - }
780 -
781 - foreach ( $template_ids as $template_id ) {
782 - wp_delete_post( $template_id, true );
783 - }
784 -
785 - return true;
786 - }
787 -
788 725 /**
789 726 * Export local template.
790 727 *
791 728 * Export template to a file.
@@ -797,14 +734,8 @@
797 734 *
798 735 * @return \WP_Error WordPress error if template export failed.
799 736 */
800 737 public function export_template( $template_id ) {
801 - $permissions_error = $this->validate_template_export_permissions( $template_id );
802 -
803 - if ( is_wp_error( $permissions_error ) ) {
804 - return $permissions_error;
805 - }
806 -
807 738 $file_data = $this->prepare_template_export( $template_id );
808 739
809 740 if ( is_wp_error( $file_data ) ) {
810 741 return $file_data;
@@ -811,29 +742,18 @@
811 742 }
812 743
813 744 $this->send_file_headers( $file_data['name'], strlen( $file_data['content'] ) );
814 745
815 - $this->serve_file( $file_data['content'] );
746 + // Clear buffering just in case.
747 + @ob_end_clean();
816 748
817 - die;
818 - }
749 + flush();
819 750
820 - private function validate_template_export_permissions( $template_id ) {
821 - $post_id = intval( $template_id );
822 - if ( get_post_type( $post_id ) !== self::CPT ) {
823 - return new \WP_Error( 'template_error', esc_html__( 'Invalid template type or template does not exist.', 'elementor' ) );
824 - }
751 + // Output file contents.
752 + // PHPCS - Export widget json
753 + echo $file_data['content']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
825 754
826 - $post_status = get_post_status( $post_id );
827 - if ( 'private' === $post_status && ! current_user_can( 'read_private_posts', $post_id ) ) {
828 - return new \WP_Error( 'template_error', esc_html__( 'You do not have permission to access this template.', 'elementor' ) );
829 - }
830 -
831 - if ( 'publish' !== $post_status && ! current_user_can( 'edit_post', $post_id ) ) {
832 - return new \WP_Error( 'template_error', esc_html__( 'You do not have permission to export this template.', 'elementor' ) );
833 - }
834 -
835 - return null;
755 + die;
836 756 }
837 757
838 758 /**
839 759 * Export multiple local templates.
@@ -901,12 +821,14 @@
901 821 foreach ( $files as $file ) {
902 822 unlink( $file['path'] );
903 823 }
904 824
905 - $this->send_file_headers( $zip_archive_filename, $this->filesize( $zip_complete_path ) );
825 + $this->send_file_headers( $zip_archive_filename, filesize( $zip_complete_path ) );
906 826
907 - $this->serve_zip( $zip_complete_path );
827 + @ob_end_flush();
908 828
829 + @readfile( $zip_complete_path );
830 +
909 831 unlink( $zip_complete_path );
910 832
911 833 die;
912 834 }
@@ -918,13 +840,13 @@
918 840 *
919 841 * @since 1.0.0
920 842 * @access public
921 843 *
922 - * @param string $name - The file name.
923 - * @param string $path - The file path.
844 + * @param string $name - The file name
845 + * @param string $path - The file path
924 846 * @return \WP_Error|array An array of items on success, 'WP_Error' on failure.
925 847 */
926 - public function import_template( $name, $path, $import_mode = 'match_site' ) {
848 + public function import_template( $name, $path ) {
927 849 if ( empty( $path ) ) {
928 850 return new \WP_Error( 'file_error', 'Please upload a file to import' );
929 851 }
930 852
@@ -944,18 +866,15 @@
944 866 return $extracted_files;
945 867 }
946 868
947 869 foreach ( $extracted_files['files'] as $file_path ) {
948 - // Skip macOS metadata files and folders
949 - if ( false !== strpos( $file_path, '__MACOSX' ) || '.' === basename( $file_path )[0] ) {
950 - continue;
951 - }
870 + $import_result = $this->import_single_template( $file_path );
952 871
953 - $import_result = $this->import_single_template( $file_path, $import_mode );
872 + if ( is_wp_error( $import_result ) ) {
873 + // Delete the temporary extraction directory, since it's now not necessary.
874 + Plugin::$instance->uploads_manager->remove_file_or_dir( $extracted_files['extraction_directory'] );
954 875
955 - if ( is_wp_error( $import_result ) ) {
956 - // Skip failed files
957 - continue;
876 + return $import_result;
958 877 }
959 878
960 879 $items[] = $import_result;
961 880 }
@@ -963,9 +882,9 @@
963 882 // Delete the temporary extraction directory, since it's now not necessary.
964 883 Plugin::$instance->uploads_manager->remove_file_or_dir( $extracted_files['extraction_directory'] );
965 884 } else {
966 885 // If the import file is a single JSON file
967 - $import_result = $this->import_single_template( $path, $import_mode );
886 + $import_result = $this->import_single_template( $path );
968 887
969 888 if ( is_wp_error( $import_result ) ) {
970 889 return $import_result;
971 890 }
@@ -993,17 +912,9 @@
993 912 */
994 913 public function post_row_actions( $actions, \WP_Post $post ) {
995 914 if ( self::is_base_templates_screen() ) {
996 915 if ( $this->is_template_supports_export( $post->ID ) ) {
997 - $template_labels = $this->get_template_labels_by_type( self::get_template_type( $post->ID ) );
998 - $export_label = esc_html__( 'Export Template', 'elementor' );
999 -
1000 - if ( $template_labels['singular'] ) {
1001 - /* translators: %s: Template label (singular). */
1002 - $export_label = sprintf( esc_html__( 'Export %s', 'elementor' ), $template_labels['singular'] );
1003 - }
1004 -
1005 - $actions['export-template'] = sprintf( '<a href="%1$s">%2$s</a>', $this->get_export_link( $post->ID ), $export_label );
916 + $actions['export-template'] = sprintf( '<a href="%1$s">%2$s</a>', $this->get_export_link( $post->ID ), esc_html__( 'Export Template', 'elementor' ) );
1006 917 }
1007 918 }
1008 919
1009 920 return $actions;
@@ -1021,36 +932,19 @@
1021 932 * @since 1.0.0
1022 933 * @access public
1023 934 */
1024 935 public function admin_import_template_form() {
1025 - if ( ! self::is_base_templates_screen() || ! User::is_current_user_can_upload_json() ) {
936 + if ( ! self::is_base_templates_screen() ) {
1026 937 return;
1027 938 }
1028 939
1029 940 /** @var \Elementor\Core\Common\Modules\Ajax\Module $ajax */
1030 941 $ajax = Plugin::$instance->common->get_component( 'ajax' );
1031 - $template_labels = $this->get_current_template_labels();
1032 - $import_label = esc_html__( 'Import Templates', 'elementor' );
1033 - $description = esc_html__( 'Choose an Elementor template JSON file or a .zip archive of Elementor templates, and add them to the list of templates available in your library.', 'elementor' );
1034 -
1035 - if ( $template_labels['plural'] ) {
1036 - /* translators: %s: Template label (plural). */
1037 - $import_label = sprintf( esc_html__( 'Import %s', 'elementor' ), $template_labels['plural'] );
1038 - }
1039 -
1040 - if ( $template_labels['singular'] && $template_labels['plural'] ) {
1041 - $description = sprintf(
1042 - /* translators: 1: Template label (singular). 2: Template label (plural). */
1043 - esc_html__( 'Choose an Elementor %1$s JSON file or a .zip archive of Elementor %2$s, and add them to the list of %2$s available in your library.', 'elementor' ),
1044 - $template_labels['singular'],
1045 - $template_labels['plural']
1046 - );
1047 - }
1048 942 ?>
1049 943 <div id="elementor-hidden-area">
1050 - <a id="elementor-import-template-trigger" class="page-title-action button button-secondary"><?php echo esc_html( $import_label ); ?></a>
944 + <a id="elementor-import-template-trigger" class="page-title-action"><?php echo esc_html__( 'Import Templates', 'elementor' ); ?></a>
1051 945 <div id="elementor-import-template-area">
1052 - <div id="elementor-import-template-title"><?php echo esc_html( $description ); ?></div>
946 + <div id="elementor-import-template-title"><?php echo esc_html__( 'Choose an Elementor template JSON file or a .zip archive of Elementor templates, and add them to the list of templates available in your library.', 'elementor' ); ?></div>
1053 947 <form id="elementor-import-template-form" method="post" action="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>" enctype="multipart/form-data">
1054 948 <input type="hidden" name="action" value="elementor_library_direct_actions">
1055 949 <input type="hidden" name="library_action" value="direct_import_template">
1056 950 <input type="hidden" name="_nonce" value="<?php Utils::print_unescaped_internal_string( $ajax->create_nonce() ); ?>">
@@ -1081,27 +975,12 @@
1081 975 die;
1082 976 }
1083 977 }
1084 978
1085 - public function redirect_categories_page_to_saved_templates_page() {
1086 - // phpcs:ignore WordPress.Security.NonceVerification.Recommended
1087 - $taxonomy = sanitize_key( wp_unslash( $_GET['taxonomy'] ?? '' ) );
1088 - // phpcs:ignore WordPress.Security.NonceVerification.Recommended
1089 - $post_type = sanitize_key( wp_unslash( $_GET['post_type'] ?? '' ) );
1090 - $is_categories_page = 'edit-tags.php' === $GLOBALS['pagenow']
1091 - && self::TAXONOMY_CATEGORY_SLUG === $taxonomy
1092 - && self::CPT === $post_type;
1093 -
1094 - if ( $is_categories_page ) {
1095 - wp_safe_redirect( admin_url( 'edit.php?post_type=' . self::CPT . '&tabs_group=library' ) );
1096 - die;
1097 - }
1098 - }
1099 -
1100 979 /**
1101 980 * Is template library supports export.
1102 981 *
1103 - * Whether the template library supports export.
982 + * whether the template library supports export.
1104 983 *
1105 984 * Template saved by the user locally on his site, support export by default
1106 985 * but this can be changed using a filter.
1107 986 *
@@ -1292,13 +1171,17 @@
1292 1171 *
1293 1172 * @return array An updated array of available list table views.
1294 1173 */
1295 1174 public function admin_print_tabs( $views ) {
1296 - //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce is not required to retrieve the value.
1297 - $current_type = Utils::get_super_global_value( $_REQUEST, self::TAXONOMY_TYPE_SLUG ) ?? '';
1298 - $active_class = $current_type ? '' : ' nav-tab-active';
1175 + $current_type = '';
1176 + $active_class = ' nav-tab-active';
1299 1177 $current_tabs_group = $this->get_current_tab_group();
1300 1178
1179 + if ( ! empty( $_REQUEST[ self::TAXONOMY_TYPE_SLUG ] ) ) {
1180 + $current_type = $_REQUEST[ self::TAXONOMY_TYPE_SLUG ];
1181 + $active_class = '';
1182 + }
1183 +
1301 1184 $url_args = [
1302 1185 'post_type' => self::CPT,
1303 1186 'tabs_group' => $current_tabs_group,
1304 1187 ];
@@ -1360,9 +1243,9 @@
1360 1243 * @since 2.0.0
1361 1244 * @access public
1362 1245 *
1363 1246 * @param string $which The location of the extra table nav markup: 'top' or 'bottom'.
1364 - * @param array $args
1247 + * @param array $args
1365 1248 */
1366 1249 public function maybe_render_blank_state( $which, array $args = [] ) {
1367 1250 global $post_type;
1368 1251
@@ -1378,9 +1261,8 @@
1378 1261 global $wp_list_table;
1379 1262
1380 1263 $total_items = $wp_list_table->get_pagination_arg( 'total_items' );
1381 1264
1382 - //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce is not required to retrieve the value.
1383 1265 if ( ! empty( $total_items ) || ! empty( $_REQUEST['s'] ) ) {
1384 1266 return;
1385 1267 }
1386 1268
@@ -1400,9 +1282,9 @@
1400 1282 // TODO: This code maybe unreachable see if above `if ( empty( $document_types[ $current_type ] ) )`.
1401 1283 if ( empty( $current_type ) ) {
1402 1284 $counts = (array) wp_count_posts( self::CPT );
1403 1285 unset( $counts['auto-draft'] );
1404 - $count = array_sum( $counts );
1286 + $count = array_sum( $counts );
1405 1287
1406 1288 if ( 0 < $count ) {
1407 1289 return;
1408 1290 }
@@ -1416,24 +1298,14 @@
1416 1298 }
1417 1299
1418 1300 private function render_blank_state( $current_type, array $args = [] ) {
1419 1301 $current_type_label = $this->get_template_label_by_type( $current_type );
1420 - $type_labels = $this->get_template_labels_by_type( $current_type );
1421 - $description = esc_html__( 'Add templates and reuse them across your website. Easily export and import them to any other project, for an optimized workflow.', 'elementor' );
1422 -
1423 - if ( $type_labels['plural'] ) {
1424 - $description = sprintf(
1425 - /* translators: %s: Template label (plural). */
1426 - esc_html__( 'Add %s and reuse them across your website. Easily export and import them to any other project, for an optimized workflow.', 'elementor' ),
1427 - $type_labels['plural']
1428 - );
1429 - }
1430 1302 $inline_style = '#posts-filter .wp-list-table, #posts-filter .tablenav.top, .tablenav.bottom .actions, .wrap .subsubsub { display:none;}';
1431 1303
1432 1304 $args = wp_parse_args( $args, [
1433 1305 'additional_inline_style' => '',
1434 1306 'href' => '',
1435 - 'description' => $description,
1307 + 'description' => esc_html__( 'Add templates and reuse them across your website. Easily export and import them to any other project, for an optimized workflow.', 'elementor' ),
1436 1308 ] );
1437 1309 $inline_style .= $args['additional_inline_style'];
1438 1310 ?>
1439 1311 <style type="text/css"><?php Utils::print_unescaped_internal_string( $inline_style ); ?></style>
@@ -1453,11 +1325,11 @@
1453 1325 *
1454 1326 * @since 3.1.0
1455 1327 * @access public
1456 1328 *
1457 - * @param string $current_type_label The Entity title.
1458 - * @param string $href The URL for the 'Add New' button.
1459 - * @param string $description The sub title describing the Entity (Post Type, Taxonomy, etc.).
1329 + * @param string $current_type_label The Entity title
1330 + * @param string $href The URL for the 'Add New' button
1331 + * @param string $description The sub title describing the Entity (Post Type, Taxonomy, etc.)
1460 1332 */
1461 1333 public function print_blank_state_template( $current_type_label, $href, $description ) {
1462 1334 ?>
1463 1335 <div class="elementor-blank_state">
@@ -1468,9 +1340,9 @@
1468 1340 printf( esc_html__( 'Create Your First %s', 'elementor' ), esc_html( $current_type_label ) );
1469 1341 ?>
1470 1342 </h3>
1471 1343 <p><?php echo wp_kses_post( $description ); ?></p>
1472 - <a id="elementor-template-library-add-new" class="elementor-button e-primary" href="<?php echo esc_url( $href ); ?>">
1344 + <a id="elementor-template-library-add-new" class="elementor-button elementor-button-success" href="<?php echo esc_url( $href ); ?>">
1473 1345 <?php
1474 1346 /* translators: %s: Template type label. */
1475 1347 printf( esc_html__( 'Add New %s', 'elementor' ), esc_html( $current_type_label ) );
1476 1348 ?>
@@ -1478,17 +1350,8 @@
1478 1350 </div>
1479 1351 <?php
1480 1352 }
1481 1353
1482 - /**
1483 - * Add filter by category.
1484 - *
1485 - * In the templates library, add a filter by Elementor library category.
1486 - *
1487 - * @access public
1488 - *
1489 - * @param string $post_type The post type slug.
1490 - */
1491 1354 public function add_filter_by_category( $post_type ) {
1492 1355 if ( self::CPT !== $post_type ) {
1493 1356 return;
1494 1357 }
@@ -1493,9 +1356,10 @@
1493 1356 return;
1494 1357 }
1495 1358
1496 1359 $all_items = get_taxonomy( self::TAXONOMY_CATEGORY_SLUG )->labels->all_items;
1497 - $dropdown_options = [
1360 +
1361 + $dropdown_options = array(
1498 1362 'show_option_all' => $all_items,
1499 1363 'show_option_none' => $all_items,
1500 1364 'hide_empty' => 0,
1501 1365 'hierarchical' => 1,
@@ -1503,17 +1367,11 @@
1503 1367 'orderby' => 'name',
1504 1368 'value_field' => 'slug',
1505 1369 'taxonomy' => self::TAXONOMY_CATEGORY_SLUG,
1506 1370 'name' => self::TAXONOMY_CATEGORY_SLUG,
1507 - //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce is not required to retrieve the value.
1508 - 'selected' => Utils::get_super_global_value( $_GET, self::TAXONOMY_CATEGORY_SLUG ) ?? '',
1509 - ];
1510 -
1511 - printf(
1512 - '<label class="screen-reader-text" for="%1$s">%2$s</label>',
1513 - esc_attr( self::TAXONOMY_CATEGORY_SLUG ),
1514 - esc_html_x( 'Filter by category', 'Template Library', 'elementor' )
1371 + 'selected' => empty( $_GET[ self::TAXONOMY_CATEGORY_SLUG ] ) ? '' : $_GET[ self::TAXONOMY_CATEGORY_SLUG ],
1515 1372 );
1373 + echo '<label class="screen-reader-text" for="cat">' . esc_html_x( 'Filter by category', 'Template Library', 'elementor' ) . '</label>';
1516 1374 wp_dropdown_categories( $dropdown_options );
1517 1375 }
1518 1376
1519 1377 /**
@@ -1528,26 +1386,48 @@
1528 1386 *
1529 1387 * @return \WP_Error|int|array Local template array, or template ID, or
1530 1388 * `WP_Error`.
1531 1389 */
1532 - private function import_single_template( $file_path, $import_mode = 'match_site' ) {
1533 - $data = $this->prepare_import_template_data( $file_path, $import_mode );
1390 + private function import_single_template( $file_path ) {
1391 + $data = json_decode( Utils::file_get_contents( $file_path ), true );
1534 1392
1535 - if ( is_wp_error( $data ) ) {
1536 - return $data;
1393 + if ( empty( $data ) ) {
1394 + return new \WP_Error( 'file_error', 'Invalid File' );
1537 1395 }
1538 1396
1539 - if ( empty( $data ) ) {
1540 - return new \WP_Error( 'file_error', 'Invalid File' );
1397 + $content = $data['content'];
1398 +
1399 + if ( ! is_array( $content ) ) {
1400 + return new \WP_Error( 'file_error', 'Invalid Content In File' );
1541 1401 }
1542 1402
1403 + $content = $this->process_export_import_content( $content, 'on_import' );
1404 +
1405 + $page_settings = [];
1406 +
1407 + if ( ! empty( $data['page_settings'] ) ) {
1408 + $page = new Model( [
1409 + 'id' => 0,
1410 + 'settings' => $data['page_settings'],
1411 + ] );
1412 +
1413 + $page_settings_data = $this->process_element_export_import_content( $page, 'on_import' );
1414 +
1415 + if ( ! empty( $page_settings_data['settings'] ) ) {
1416 + $page_settings = $page_settings_data['settings'];
1417 + }
1418 + }
1419 +
1543 1420 $template_id = $this->save_item( [
1544 - 'content' => $data['content'],
1421 + 'content' => $content,
1545 1422 'title' => $data['title'],
1546 1423 'type' => $data['type'],
1547 - 'page_settings' => $data['page_settings'],
1424 + 'page_settings' => $page_settings,
1548 1425 ] );
1549 1426
1427 + // Remove the temporary file, now that we're done with it.
1428 + Plugin::$instance->uploads_manager->remove_file_or_dir( $file_path );
1429 +
1550 1430 if ( is_wp_error( $template_id ) ) {
1551 1431 return $template_id;
1552 1432 }
1553 1433
@@ -1574,15 +1454,10 @@
1574 1454 if ( empty( $template_data['content'] ) ) {
1575 1455 return new \WP_Error( 'empty_template', 'The template is empty' );
1576 1456 }
1577 1457
1578 - $content = apply_filters(
1579 - 'elementor/template_library/sources/local/export/elements',
1580 - $template_data['content']
1581 - );
1582 -
1583 1458 $export_data = [
1584 - 'content' => $content,
1459 + 'content' => $template_data['content'],
1585 1460 'page_settings' => $template_data['settings'],
1586 1461 'version' => DB::DB_VERSION,
1587 1462 'title' => $document->get_main_post()->post_title,
1588 1463 'type' => self::get_template_type( $template_id ),
@@ -1587,10 +1462,8 @@
1587 1462 'title' => $document->get_main_post()->post_title,
1588 1463 'type' => self::get_template_type( $template_id ),
1589 1464 ];
1590 1465
1591 - $this->attach_global_styles_to_data( $export_data, $content, $template_id );
1592 -
1593 1466 return [
1594 1467 'name' => 'elementor-' . $template_id . '-' . gmdate( 'Y-m-d' ) . '.json',
1595 1468 'content' => wp_json_encode( $export_data ),
1596 1469 ];
@@ -1596,8 +1469,28 @@
1596 1469 ];
1597 1470 }
1598 1471
1599 1472 /**
1473 + * Send file headers.
1474 + *
1475 + * Set the file header when export template data to a file.
1476 + *
1477 + * @since 1.6.0
1478 + * @access private
1479 + *
1480 + * @param string $file_name File name.
1481 + * @param int $file_size File size.
1482 + */
1483 + private function send_file_headers( $file_name, $file_size ) {
1484 + header( 'Content-Type: application/octet-stream' );
1485 + header( 'Content-Disposition: attachment; filename=' . $file_name );
1486 + header( 'Expires: 0' );
1487 + header( 'Cache-Control: must-revalidate' );
1488 + header( 'Pragma: public' );
1489 + header( 'Content-Length: ' . $file_size );
1490 + }
1491 +
1492 + /**
1600 1493 * Get template label by type.
1601 1494 *
1602 1495 * Retrieve the template label for any given template type.
1603 1496 *
@@ -1631,68 +1524,8 @@
1631 1524
1632 1525 return $template_label;
1633 1526 }
1634 1527
1635 - private function get_template_labels_by_type( $template_type ) {
1636 - $labels = [
1637 - 'singular' => '',
1638 - 'plural' => '',
1639 - ];
1640 -
1641 - if ( ! $template_type ) {
1642 - return $labels;
1643 - }
1644 -
1645 - $document_types = Plugin::instance()->documents->get_document_types();
1646 -
1647 - if ( isset( $document_types[ $template_type ] ) ) {
1648 - $doc_type = $document_types[ $template_type ];
1649 - $labels['singular'] = call_user_func( [ $doc_type, 'get_title' ] );
1650 - $labels['plural'] = call_user_func( [ $doc_type, 'get_plural_title' ] );
1651 - } else {
1652 - $labels['singular'] = $this->get_template_label_by_type( $template_type );
1653 - }
1654 -
1655 - if ( ! $labels['plural'] ) {
1656 - $labels['plural'] = $labels['singular'];
1657 - }
1658 -
1659 - return $labels;
1660 - }
1661 -
1662 - private function get_current_template_labels() {
1663 - $labels = [
1664 - 'singular' => '',
1665 - 'plural' => '',
1666 - ];
1667 -
1668 - $template_type = Utils::get_super_global_value( $_GET, self::TAXONOMY_TYPE_SLUG );
1669 -
1670 - if ( $template_type ) {
1671 - return $this->get_template_labels_by_type( $template_type );
1672 - }
1673 -
1674 - $current_tabs_group = $this->get_current_tab_group();
1675 -
1676 - if ( $current_tabs_group ) {
1677 - $doc_types = Plugin::$instance->documents->get_document_types( [
1678 - 'admin_tab_group' => $current_tabs_group,
1679 - ], 'and' );
1680 -
1681 - if ( 1 === count( $doc_types ) ) {
1682 - $doc_type = reset( $doc_types );
1683 - $labels['singular'] = call_user_func( [ $doc_type, 'get_title' ] );
1684 - $labels['plural'] = call_user_func( [ $doc_type, 'get_plural_title' ] );
1685 - }
1686 - }
1687 -
1688 - if ( ! $labels['plural'] ) {
1689 - $labels['plural'] = $labels['singular'];
1690 - }
1691 -
1692 - return $labels;
1693 - }
1694 -
1695 1528 /**
1696 1529 * Filter template types in admin query.
1697 1530 *
1698 1531 * Update the template types in the main admin query.
@@ -1736,32 +1569,20 @@
1736 1569 * @access private
1737 1570 */
1738 1571 private function add_actions() {
1739 1572 if ( is_admin() ) {
1740 - add_action( 'admin_init', [ $this, 'redirect_categories_page_to_saved_templates_page' ] );
1573 + add_action( 'elementor/admin/menu/register', function ( Admin_Menu_Manager $admin_menu ) {
1574 + $this->register_admin_menu( $admin_menu );
1575 + }, static::ADMIN_MENU_PRIORITY );
1741 1576
1742 - add_action( 'elementor/editor-one/menu/register', function ( Menu_Data_Provider $menu_data_provider ) {
1743 - $this->register_editor_one_menu( $menu_data_provider );
1744 - } );
1577 + add_action( 'elementor/admin/menu/register', function ( Admin_Menu_Manager $admin_menu ) {
1578 + $this->admin_menu_reorder( $admin_menu );
1579 + }, 800 );
1745 1580
1746 - add_action( 'elementor/editor-one/menu/excluded_level4_slugs', function ( array $excluded_slugs ): array {
1747 - $excluded_slugs[] = 'edit.php?post_type=elementor_library#add_new';
1748 - $excluded_slugs[] = 'edit-tags.php?taxonomy=elementor_library_category&amp;post_type=elementor_library';
1749 - return $excluded_slugs;
1581 + add_action( 'elementor/admin/menu/after_register', function () {
1582 + $this->admin_menu_set_current();
1750 1583 } );
1751 1584
1752 - add_filter( 'elementor/editor-one/menu/elementor_post_types', function ( array $elementor_post_types ): array {
1753 - $elementor_post_types[ self::CPT ] = [];
1754 -
1755 - return $elementor_post_types;
1756 - } );
1757 -
1758 - add_filter( 'elementor/editor-one/admin-edit-post-types', function ( array $post_types ): array {
1759 - $post_types[] = self::CPT;
1760 -
1761 - return $post_types;
1762 - } );
1763 -
1764 1585 add_filter( 'admin_title', [ $this, 'admin_title' ], 10, 2 );
1765 1586 add_action( 'all_admin_notices', [ $this, 'replace_admin_heading' ] );
1766 1587 add_filter( 'post_row_actions', [ $this, 'post_row_actions' ], 10, 2 );
1767 1588 add_action( 'admin_footer', [ $this, 'admin_import_template_form' ] );
@@ -1787,10 +1608,8 @@
1787 1608 // Show blank state.
1788 1609 add_action( 'manage_posts_extra_tablenav', [ $this, 'maybe_render_blank_state' ] );
1789 1610 }
1790 1611
1791 - add_action( 'elementor/document/after_save', [ $this, 'on_template_update' ], 10, 2 );
1792 -
1793 1612 add_action( 'template_redirect', [ $this, 'block_template_frontend' ] );
1794 1613
1795 1614 // Remove elementor library templates from WP Sitemap
1796 1615 add_filter(
@@ -1800,17 +1619,8 @@
1800 1619 }
1801 1620 );
1802 1621 }
1803 1622
1804 - public function on_template_update( \Elementor\Core\Base\Document $document, array $data ) {
1805 - if ( ! empty( $data['post_title'] ) ) {
1806 - wp_update_post( [
1807 - 'ID' => $document->get_main_id(),
1808 - 'post_title' => $data['post_title'],
1809 - ] );
1810 - }
1811 - }
1812 -
1813 1623 /**
1814 1624 * @since 2.0.6
1815 1625 * @access public
1816 1626 */
@@ -1841,18 +1651,20 @@
1841 1651
1842 1652 return $posts_columns;
1843 1653 }
1844 1654
1845 - public function get_current_tab_group() {
1846 - $current_tabs_group = Utils::get_super_global_value( $_GET, 'tabs_group' ) ?? '';
1847 - $type_slug = Utils::get_super_global_value( $_GET, self::TAXONOMY_TYPE_SLUG );
1655 + public function get_current_tab_group( $default = '' ) {
1656 + $current_tabs_group = $default;
1848 1657
1849 - if ( $type_slug ) {
1850 - $doc_type = Plugin::$instance->documents->get_document_type( $type_slug, '' );
1658 + if ( ! empty( $_REQUEST[ self::TAXONOMY_TYPE_SLUG ] ) ) {
1659 + $doc_type = Plugin::$instance->documents->get_document_type( $_REQUEST[ self::TAXONOMY_TYPE_SLUG ], '' );
1851 1660 if ( $doc_type ) {
1852 1661 $current_tabs_group = $doc_type::get_property( 'admin_tab_group' );
1853 1662 }
1663 + } elseif ( ! empty( $_REQUEST['tabs_group'] ) ) {
1664 + $current_tabs_group = $_REQUEST['tabs_group'];
1854 1665 }
1666 +
1855 1667 return $current_tabs_group;
1856 1668 }
1857 1669
1858 1670 private function get_library_title() {
@@ -1893,35 +1705,8 @@
1893 1705
1894 1706 return $post_types;
1895 1707 }
1896 1708
1897 - private function avoid_rest_access_for_non_admins(): void {
1898 - add_filter( 'rest_pre_dispatch', function ( $value, \WP_REST_Server $server, \WP_REST_Request $request ) {
1899 - if ( strpos( $request->get_route(), self::CPT ) !== false ) {
1900 - if ( ! current_user_can( 'manage_options' ) ) {
1901 - return new \WP_Error( 'rest_forbidden', esc_html__( 'Sorry, you are not allowed to do that.', 'elementor' ), [ 'status' => rest_authorization_required_code() ] );
1902 - }
1903 - }
1904 -
1905 - return $value;
1906 - }, 10, 3 );
1907 - }
1908 -
1909 - public function save_bulk_items( array $args = [] ) {
1910 - $items = [];
1911 -
1912 - foreach ( $args as $item ) {
1913 - $items[] = $this->save_item( [
1914 - 'content' => $item['content'],
1915 - 'title' => $item['title'],
1916 - 'type' => $item['type'],
1917 - 'page_settings' => $item['page_settings'],
1918 - ] );
1919 - }
1920 -
1921 - return $items;
1922 - }
1923 -
1924 1709 /**
1925 1710 * Template library local source constructor.
1926 1711 *
1927 1712 * Initializing the template library local source base by registering custom
@@ -1933,87 +1718,6 @@
1933 1718 public function __construct() {
1934 1719 parent::__construct();
1935 1720
1936 1721 $this->add_actions();
1937 - }
1938 -
1939 - public function format_args_for_bulk_action( $args ) {
1940 - $bulk_args = [];
1941 -
1942 - foreach ( $args['from_template_id'] as $from_template_id ) {
1943 - if ( ! $this->is_allowed_to_read_template( [
1944 - 'template_id' => $from_template_id,
1945 - ] ) ) {
1946 - continue;
1947 - }
1948 -
1949 - $document = Plugin::$instance->documents->get( $from_template_id );
1950 -
1951 - if ( ! $document ) {
1952 - continue;
1953 - }
1954 -
1955 - $page = SettingsManager::get_settings_managers( 'page' )->get_model( $from_template_id );
1956 -
1957 - $bulk_args[] = array_merge(
1958 - $args,
1959 - [
1960 - 'title' => $document->get_post()->post_title,
1961 - 'type' => $document::get_type(),
1962 - 'content' => $document->get_elements_data(),
1963 - 'page_settings' => $page->get_data( 'settings' ),
1964 - ]
1965 - );
1966 - }
1967 -
1968 - return $bulk_args;
1969 - }
1970 -
1971 - public function format_args_for_single_action( $args ) {
1972 - if ( ! $this->is_allowed_to_read_template( [
1973 - 'template_id' => $args['from_template_id'],
1974 - ] ) ) {
1975 - return new \WP_Error(
1976 - 'template_error',
1977 - esc_html__( 'You do not have permission to access this template.', 'elementor' )
1978 - );
1979 - }
1980 -
1981 - $document = Plugin::$instance->documents->get( $args['from_template_id'] );
1982 -
1983 - if ( ! $document ) {
1984 - return new \WP_Error( 'template_error', 'Document not found.' );
1985 - }
1986 -
1987 - $args['content'] = $document->get_elements_data();
1988 -
1989 - $page = SettingsManager::get_settings_managers( 'page' )->get_model( $args['from_template_id'] );
1990 - $args['page_settings'] = $page->get_data( 'settings' );
1991 -
1992 - return $args;
1993 - }
1994 -
1995 - public function is_allowed_to_read_template( array $args ): bool {
1996 - if ( null === $this->wordpress_adapter ) {
1997 - $this->set_wordpress_adapter( new WordPress_Adapter() );
1998 - }
1999 -
2000 - $post_id = intval( $args['template_id'] );
2001 - $post_status = $this->wordpress_adapter->get_post_status( $post_id );
2002 -
2003 - if ( 'publish' === $post_status && ! post_password_required( $post_id ) ) {
2004 - return true;
2005 - }
2006 -
2007 - $can_read_template = $this->wordpress_adapter->current_user_can( 'edit_post', $post_id );
2008 -
2009 - return apply_filters( 'elementor/template-library/is_allowed_to_read_template', $can_read_template, $args );
2010 - }
2011 -
2012 - public function set_wordpress_adapter( Wordpress_Adapter_Interface $wordpress_adapter ) {
2013 - $this->wordpress_adapter = $wordpress_adapter;
2014 - }
2015 -
2016 - public function set_elementor_adapter( Elementor_Adapter_Interface $elementor_adapter ): void {
2017 - $this->elementor_adapter = $elementor_adapter;
2018 1722 }
2019 1723 }