PluginProbe
Elementor Website Builder – more than just a page builder / 3.24.8
Elementor Website Builder – more than just a page builder v3.24.8
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 +170 -426 4.2.0-dev23.24.8 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;
@@ -12,14 +14,8 @@
12 14 use Elementor\Modules\Library\Documents\Library_Document;
13 15 use Elementor\Plugin;
14 16 use Elementor\Utils;
15 17 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 18
23 19 if ( ! defined( 'ABSPATH' ) ) {
24 20 exit; // Exit if accessed directly.
25 21 }
@@ -95,18 +91,8 @@
95 91 */
96 92 private $post_type_object;
97 93
98 94 /**
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 95 * @since 2.3.0
110 96 * @access public
111 97 * @static
112 98 * @return array
@@ -263,14 +249,11 @@
263 249 'show_in_nav_menus' => false,
264 250 'exclude_from_search' => true,
265 251 'capability_type' => 'post',
266 252 'hierarchical' => false,
267 - 'supports' => [ 'title', 'thumbnail', 'author', 'elementor', 'custom-fields' ],
268 - 'show_in_rest' => true,
253 + 'supports' => [ 'title', 'thumbnail', 'author', 'elementor' ],
269 254 ];
270 255
271 - $this->avoid_rest_access_for_non_admins();
272 -
273 256 /**
274 257 * Register template library post type args.
275 258 *
276 259 * Filters the post type arguments when registering elementor template library post type.
@@ -351,14 +334,79 @@
351 334
352 335 register_taxonomy( self::TAXONOMY_CATEGORY_SLUG, self::CPT, $args );
353 336 }
354 337
338 + /**
339 + * Remove Add New item from admin menu.
340 + *
341 + * Fired by `admin_menu` action.
342 + *
343 + * @since 2.4.0
344 + * @access public
345 + */
346 + private function admin_menu_reorder( Admin_Menu_Manager $admin_menu ) {
347 + global $submenu;
355 348
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() );
349 + if ( ! isset( $submenu[ static::ADMIN_MENU_SLUG ] ) ) {
350 + return;
351 + }
352 +
353 + remove_submenu_page( static::ADMIN_MENU_SLUG, static::ADMIN_MENU_SLUG );
354 +
355 + $add_new_slug = 'post-new.php?post_type=' . static::CPT;
356 + $category_slug = 'edit-tags.php?taxonomy=' . static::TAXONOMY_CATEGORY_SLUG . '&amp;post_type=' . static::CPT;
357 +
358 + $library_submenu = new Collection( $submenu[ static::ADMIN_MENU_SLUG ] );
359 +
360 + $add_new_item = $library_submenu->find( function ( $item ) use ( $add_new_slug ) {
361 + return $add_new_slug === $item[2];
362 + } );
363 +
364 + $categories_item = $library_submenu->find( function ( $item ) use ( $category_slug ) {
365 + return $category_slug === $item[2];
366 + } );
367 +
368 + if ( $add_new_item ) {
369 + remove_submenu_page( static::ADMIN_MENU_SLUG, $add_new_slug );
370 +
371 + $admin_menu->register( admin_url( static::ADMIN_MENU_SLUG . '#add_new' ), new Add_New_Template_Menu_Item() );
372 + }
373 +
374 + if ( $categories_item ) {
375 + remove_submenu_page( static::ADMIN_MENU_SLUG, $category_slug );
376 +
377 + $admin_menu->register( $category_slug, new Templates_Categories_Menu_Item() );
378 + }
359 379 }
360 380
381 + /**
382 + * Add a `current` CSS class to the `Saved Templates` submenu page when it's active.
383 + * It should work by default, but since we interfere with WordPress' builtin CPT menus it doesn't work properly.
384 + *
385 + * @return void
386 + */
387 + private function admin_menu_set_current() {
388 + global $submenu;
389 +
390 + if ( $this->is_current_screen() ) {
391 + $library_submenu = &$submenu[ static::ADMIN_MENU_SLUG ];
392 + $library_title = $this->get_library_title();
393 +
394 + foreach ( $library_submenu as &$item ) {
395 + if ( $library_title === $item[0] ) {
396 + if ( ! isset( $item[4] ) ) {
397 + $item[4] = '';
398 + }
399 + $item[4] .= ' current';
400 + }
401 + }
402 + }
403 + }
404 +
405 + private function register_admin_menu( Admin_Menu_Manager $admin_menu ) {
406 + $admin_menu->register( static::get_admin_url( true ), new Saved_Templates_Menu_Item() );
407 + }
408 +
361 409 public function admin_title( $admin_title, $title ) {
362 410 $library_title = $this->get_library_title();
363 411
364 412 if ( $library_title ) {
@@ -368,54 +416,15 @@
368 416 return $admin_title;
369 417 }
370 418
371 419 public function replace_admin_heading() {
372 - if ( ! $this->is_current_screen() ) {
373 - return;
374 - }
375 -
376 - global $post_type_object;
377 -
378 420 $library_title = $this->get_library_title();
379 421
380 422 if ( $library_title ) {
423 + global $post_type_object;
424 +
381 425 $post_type_object->labels->name = $library_title;
382 426 }
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 427 }
419 428
420 429 /**
421 430 * Get local templates.
@@ -577,9 +586,9 @@
577 586 $cpt,
578 587 );
579 588 }
580 589
581 - /** For testing purposes only, in order to be able to mock the `WP_CLI` constant. */
590 + // For testing purposes only, in order to be able to mock the `WP_CLI` constant.
582 591 protected function is_wp_cli() {
583 592 return Utils::is_wp_cli();
584 593 }
585 594
@@ -605,20 +614,12 @@
605 614 if ( ! $document ) {
606 615 return new \WP_Error( 'save_error', esc_html__( 'Template not exist.', 'elementor' ) );
607 616 }
608 617
609 - $save_data = [];
618 + $document->save( [
619 + 'elements' => $new_data['content'],
620 + ] );
610 621
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 622 /**
622 623 * After template library update.
623 624 *
624 625 * Fires after Elementor template library was updated.
@@ -715,22 +716,8 @@
715 716 }
716 717
717 718 if ( ! empty( $content ) ) {
718 719 $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 720 }
734 721 }
735 722
736 723 $data = [
@@ -742,12 +729,8 @@
742 729
743 730 $data['page_settings'] = $page->get_data( 'settings' );
744 731 }
745 732
746 - if ( ! empty( $content ) ) {
747 - $this->attach_global_styles_to_data( $data, $content, $template_id );
748 - }
749 -
750 733 return $data;
751 734 }
752 735
753 736 /**
@@ -770,22 +753,8 @@
770 753
771 754 return wp_delete_post( $template_id, true );
772 755 }
773 756
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 757 /**
789 758 * Export local template.
790 759 *
791 760 * Export template to a file.
@@ -797,14 +766,8 @@
797 766 *
798 767 * @return \WP_Error WordPress error if template export failed.
799 768 */
800 769 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 770 $file_data = $this->prepare_template_export( $template_id );
808 771
809 772 if ( is_wp_error( $file_data ) ) {
810 773 return $file_data;
@@ -811,29 +774,18 @@
811 774 }
812 775
813 776 $this->send_file_headers( $file_data['name'], strlen( $file_data['content'] ) );
814 777
815 - $this->serve_file( $file_data['content'] );
778 + // Clear buffering just in case.
779 + @ob_end_clean();
816 780
817 - die;
818 - }
781 + flush();
819 782
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 - }
783 + // Output file contents.
784 + // PHPCS - Export widget json
785 + echo $file_data['content']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
825 786
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;
787 + die;
836 788 }
837 789
838 790 /**
839 791 * Export multiple local templates.
@@ -901,12 +853,14 @@
901 853 foreach ( $files as $file ) {
902 854 unlink( $file['path'] );
903 855 }
904 856
905 - $this->send_file_headers( $zip_archive_filename, $this->filesize( $zip_complete_path ) );
857 + $this->send_file_headers( $zip_archive_filename, filesize( $zip_complete_path ) );
906 858
907 - $this->serve_zip( $zip_complete_path );
859 + @ob_end_flush();
908 860
861 + @readfile( $zip_complete_path );
862 +
909 863 unlink( $zip_complete_path );
910 864
911 865 die;
912 866 }
@@ -918,13 +872,13 @@
918 872 *
919 873 * @since 1.0.0
920 874 * @access public
921 875 *
922 - * @param string $name - The file name.
923 - * @param string $path - The file path.
876 + * @param string $name - The file name
877 + * @param string $path - The file path
924 878 * @return \WP_Error|array An array of items on success, 'WP_Error' on failure.
925 879 */
926 - public function import_template( $name, $path, $import_mode = 'match_site' ) {
880 + public function import_template( $name, $path ) {
927 881 if ( empty( $path ) ) {
928 882 return new \WP_Error( 'file_error', 'Please upload a file to import' );
929 883 }
930 884
@@ -944,18 +898,15 @@
944 898 return $extracted_files;
945 899 }
946 900
947 901 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 - }
902 + $import_result = $this->import_single_template( $file_path );
952 903
953 - $import_result = $this->import_single_template( $file_path, $import_mode );
904 + if ( is_wp_error( $import_result ) ) {
905 + // Delete the temporary extraction directory, since it's now not necessary.
906 + Plugin::$instance->uploads_manager->remove_file_or_dir( $extracted_files['extraction_directory'] );
954 907
955 - if ( is_wp_error( $import_result ) ) {
956 - // Skip failed files
957 - continue;
908 + return $import_result;
958 909 }
959 910
960 911 $items[] = $import_result;
961 912 }
@@ -963,9 +914,9 @@
963 914 // Delete the temporary extraction directory, since it's now not necessary.
964 915 Plugin::$instance->uploads_manager->remove_file_or_dir( $extracted_files['extraction_directory'] );
965 916 } else {
966 917 // If the import file is a single JSON file
967 - $import_result = $this->import_single_template( $path, $import_mode );
918 + $import_result = $this->import_single_template( $path );
968 919
969 920 if ( is_wp_error( $import_result ) ) {
970 921 return $import_result;
971 922 }
@@ -993,17 +944,9 @@
993 944 */
994 945 public function post_row_actions( $actions, \WP_Post $post ) {
995 946 if ( self::is_base_templates_screen() ) {
996 947 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 );
948 + $actions['export-template'] = sprintf( '<a href="%1$s">%2$s</a>', $this->get_export_link( $post->ID ), esc_html__( 'Export Template', 'elementor' ) );
1006 949 }
1007 950 }
1008 951
1009 952 return $actions;
@@ -1027,30 +970,13 @@
1027 970 }
1028 971
1029 972 /** @var \Elementor\Core\Common\Modules\Ajax\Module $ajax */
1030 973 $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 974 ?>
1049 975 <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>
976 + <a id="elementor-import-template-trigger" class="page-title-action"><?php echo esc_html__( 'Import Templates', 'elementor' ); ?></a>
1051 977 <div id="elementor-import-template-area">
1052 - <div id="elementor-import-template-title"><?php echo esc_html( $description ); ?></div>
978 + <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 979 <form id="elementor-import-template-form" method="post" action="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>" enctype="multipart/form-data">
1054 980 <input type="hidden" name="action" value="elementor_library_direct_actions">
1055 981 <input type="hidden" name="library_action" value="direct_import_template">
1056 982 <input type="hidden" name="_nonce" value="<?php Utils::print_unescaped_internal_string( $ajax->create_nonce() ); ?>">
@@ -1081,27 +1007,12 @@
1081 1007 die;
1082 1008 }
1083 1009 }
1084 1010
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 1011 /**
1101 1012 * Is template library supports export.
1102 1013 *
1103 - * Whether the template library supports export.
1014 + * whether the template library supports export.
1104 1015 *
1105 1016 * Template saved by the user locally on his site, support export by default
1106 1017 * but this can be changed using a filter.
1107 1018 *
@@ -1360,9 +1271,9 @@
1360 1271 * @since 2.0.0
1361 1272 * @access public
1362 1273 *
1363 1274 * @param string $which The location of the extra table nav markup: 'top' or 'bottom'.
1364 - * @param array $args
1275 + * @param array $args
1365 1276 */
1366 1277 public function maybe_render_blank_state( $which, array $args = [] ) {
1367 1278 global $post_type;
1368 1279
@@ -1400,9 +1311,9 @@
1400 1311 // TODO: This code maybe unreachable see if above `if ( empty( $document_types[ $current_type ] ) )`.
1401 1312 if ( empty( $current_type ) ) {
1402 1313 $counts = (array) wp_count_posts( self::CPT );
1403 1314 unset( $counts['auto-draft'] );
1404 - $count = array_sum( $counts );
1315 + $count = array_sum( $counts );
1405 1316
1406 1317 if ( 0 < $count ) {
1407 1318 return;
1408 1319 }
@@ -1416,24 +1327,14 @@
1416 1327 }
1417 1328
1418 1329 private function render_blank_state( $current_type, array $args = [] ) {
1419 1330 $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 1331 $inline_style = '#posts-filter .wp-list-table, #posts-filter .tablenav.top, .tablenav.bottom .actions, .wrap .subsubsub { display:none;}';
1431 1332
1432 1333 $args = wp_parse_args( $args, [
1433 1334 'additional_inline_style' => '',
1434 1335 'href' => '',
1435 - 'description' => $description,
1336 + '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 1337 ] );
1437 1338 $inline_style .= $args['additional_inline_style'];
1438 1339 ?>
1439 1340 <style type="text/css"><?php Utils::print_unescaped_internal_string( $inline_style ); ?></style>
@@ -1453,11 +1354,11 @@
1453 1354 *
1454 1355 * @since 3.1.0
1455 1356 * @access public
1456 1357 *
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.).
1358 + * @param string $current_type_label The Entity title
1359 + * @param string $href The URL for the 'Add New' button
1360 + * @param string $description The sub title describing the Entity (Post Type, Taxonomy, etc.)
1460 1361 */
1461 1362 public function print_blank_state_template( $current_type_label, $href, $description ) {
1462 1363 ?>
1463 1364 <div class="elementor-blank_state">
@@ -1493,9 +1394,9 @@
1493 1394 return;
1494 1395 }
1495 1396
1496 1397 $all_items = get_taxonomy( self::TAXONOMY_CATEGORY_SLUG )->labels->all_items;
1497 - $dropdown_options = [
1398 + $dropdown_options = array(
1498 1399 'show_option_all' => $all_items,
1499 1400 'show_option_none' => $all_items,
1500 1401 'hide_empty' => 0,
1501 1402 'hierarchical' => 1,
@@ -1505,9 +1406,9 @@
1505 1406 'taxonomy' => self::TAXONOMY_CATEGORY_SLUG,
1506 1407 'name' => self::TAXONOMY_CATEGORY_SLUG,
1507 1408 //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce is not required to retrieve the value.
1508 1409 'selected' => Utils::get_super_global_value( $_GET, self::TAXONOMY_CATEGORY_SLUG ) ?? '',
1509 - ];
1410 + );
1510 1411
1511 1412 printf(
1512 1413 '<label class="screen-reader-text" for="%1$s">%2$s</label>',
1513 1414 esc_attr( self::TAXONOMY_CATEGORY_SLUG ),
@@ -1528,24 +1429,43 @@
1528 1429 *
1529 1430 * @return \WP_Error|int|array Local template array, or template ID, or
1530 1431 * `WP_Error`.
1531 1432 */
1532 - private function import_single_template( $file_path, $import_mode = 'match_site' ) {
1533 - $data = $this->prepare_import_template_data( $file_path, $import_mode );
1433 + private function import_single_template( $file_path ) {
1434 + $data = json_decode( Utils::file_get_contents( $file_path ), true );
1534 1435
1535 - if ( is_wp_error( $data ) ) {
1536 - return $data;
1436 + if ( empty( $data ) ) {
1437 + return new \WP_Error( 'file_error', 'Invalid File' );
1537 1438 }
1538 1439
1539 - if ( empty( $data ) ) {
1540 - return new \WP_Error( 'file_error', 'Invalid File' );
1440 + $content = $data['content'];
1441 +
1442 + if ( ! is_array( $content ) ) {
1443 + return new \WP_Error( 'file_error', 'Invalid Content In File' );
1541 1444 }
1542 1445
1446 + $content = $this->process_export_import_content( $content, 'on_import' );
1447 +
1448 + $page_settings = [];
1449 +
1450 + if ( ! empty( $data['page_settings'] ) ) {
1451 + $page = new Model( [
1452 + 'id' => 0,
1453 + 'settings' => $data['page_settings'],
1454 + ] );
1455 +
1456 + $page_settings_data = $this->process_element_export_import_content( $page, 'on_import' );
1457 +
1458 + if ( ! empty( $page_settings_data['settings'] ) ) {
1459 + $page_settings = $page_settings_data['settings'];
1460 + }
1461 + }
1462 +
1543 1463 $template_id = $this->save_item( [
1544 - 'content' => $data['content'],
1464 + 'content' => $content,
1545 1465 'title' => $data['title'],
1546 1466 'type' => $data['type'],
1547 - 'page_settings' => $data['page_settings'],
1467 + 'page_settings' => $page_settings,
1548 1468 ] );
1549 1469
1550 1470 if ( is_wp_error( $template_id ) ) {
1551 1471 return $template_id;
@@ -1574,15 +1494,10 @@
1574 1494 if ( empty( $template_data['content'] ) ) {
1575 1495 return new \WP_Error( 'empty_template', 'The template is empty' );
1576 1496 }
1577 1497
1578 - $content = apply_filters(
1579 - 'elementor/template_library/sources/local/export/elements',
1580 - $template_data['content']
1581 - );
1582 -
1583 1498 $export_data = [
1584 - 'content' => $content,
1499 + 'content' => $template_data['content'],
1585 1500 'page_settings' => $template_data['settings'],
1586 1501 'version' => DB::DB_VERSION,
1587 1502 'title' => $document->get_main_post()->post_title,
1588 1503 'type' => self::get_template_type( $template_id ),
@@ -1587,10 +1502,8 @@
1587 1502 'title' => $document->get_main_post()->post_title,
1588 1503 'type' => self::get_template_type( $template_id ),
1589 1504 ];
1590 1505
1591 - $this->attach_global_styles_to_data( $export_data, $content, $template_id );
1592 -
1593 1506 return [
1594 1507 'name' => 'elementor-' . $template_id . '-' . gmdate( 'Y-m-d' ) . '.json',
1595 1508 'content' => wp_json_encode( $export_data ),
1596 1509 ];
@@ -1596,8 +1509,28 @@
1596 1509 ];
1597 1510 }
1598 1511
1599 1512 /**
1513 + * Send file headers.
1514 + *
1515 + * Set the file header when export template data to a file.
1516 + *
1517 + * @since 1.6.0
1518 + * @access private
1519 + *
1520 + * @param string $file_name File name.
1521 + * @param int $file_size File size.
1522 + */
1523 + private function send_file_headers( $file_name, $file_size ) {
1524 + header( 'Content-Type: application/octet-stream' );
1525 + header( 'Content-Disposition: attachment; filename=' . $file_name );
1526 + header( 'Expires: 0' );
1527 + header( 'Cache-Control: must-revalidate' );
1528 + header( 'Pragma: public' );
1529 + header( 'Content-Length: ' . $file_size );
1530 + }
1531 +
1532 + /**
1600 1533 * Get template label by type.
1601 1534 *
1602 1535 * Retrieve the template label for any given template type.
1603 1536 *
@@ -1631,68 +1564,8 @@
1631 1564
1632 1565 return $template_label;
1633 1566 }
1634 1567
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 1568 /**
1696 1569 * Filter template types in admin query.
1697 1570 *
1698 1571 * Update the template types in the main admin query.
@@ -1736,32 +1609,20 @@
1736 1609 * @access private
1737 1610 */
1738 1611 private function add_actions() {
1739 1612 if ( is_admin() ) {
1740 - add_action( 'admin_init', [ $this, 'redirect_categories_page_to_saved_templates_page' ] );
1613 + add_action( 'elementor/admin/menu/register', function ( Admin_Menu_Manager $admin_menu ) {
1614 + $this->register_admin_menu( $admin_menu );
1615 + }, static::ADMIN_MENU_PRIORITY );
1741 1616
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 - } );
1617 + add_action( 'elementor/admin/menu/register', function ( Admin_Menu_Manager $admin_menu ) {
1618 + $this->admin_menu_reorder( $admin_menu );
1619 + }, 800 );
1745 1620
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;
1621 + add_action( 'elementor/admin/menu/after_register', function () {
1622 + $this->admin_menu_set_current();
1750 1623 } );
1751 1624
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 1625 add_filter( 'admin_title', [ $this, 'admin_title' ], 10, 2 );
1765 1626 add_action( 'all_admin_notices', [ $this, 'replace_admin_heading' ] );
1766 1627 add_filter( 'post_row_actions', [ $this, 'post_row_actions' ], 10, 2 );
1767 1628 add_action( 'admin_footer', [ $this, 'admin_import_template_form' ] );
@@ -1787,10 +1648,8 @@
1787 1648 // Show blank state.
1788 1649 add_action( 'manage_posts_extra_tablenav', [ $this, 'maybe_render_blank_state' ] );
1789 1650 }
1790 1651
1791 - add_action( 'elementor/document/after_save', [ $this, 'on_template_update' ], 10, 2 );
1792 -
1793 1652 add_action( 'template_redirect', [ $this, 'block_template_frontend' ] );
1794 1653
1795 1654 // Remove elementor library templates from WP Sitemap
1796 1655 add_filter(
@@ -1800,17 +1659,8 @@
1800 1659 }
1801 1660 );
1802 1661 }
1803 1662
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 1663 /**
1814 1664 * @since 2.0.6
1815 1665 * @access public
1816 1666 */
@@ -1841,11 +1691,13 @@
1841 1691
1842 1692 return $posts_columns;
1843 1693 }
1844 1694
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 );
1695 + public function get_current_tab_group( $default = '' ) {
1696 + //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce verification is not required here.
1697 + $current_tabs_group = Utils::get_super_global_value( $_REQUEST, 'tabs_group' ) ?? '';
1698 + //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce verification is not required here.
1699 + $type_slug = Utils::get_super_global_value( $_REQUEST, self::TAXONOMY_TYPE_SLUG );
1848 1700
1849 1701 if ( $type_slug ) {
1850 1702 $doc_type = Plugin::$instance->documents->get_document_type( $type_slug, '' );
1851 1703 if ( $doc_type ) {
@@ -1893,35 +1745,8 @@
1893 1745
1894 1746 return $post_types;
1895 1747 }
1896 1748
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 1749 /**
1925 1750 * Template library local source constructor.
1926 1751 *
1927 1752 * Initializing the template library local source base by registering custom
@@ -1933,87 +1758,6 @@
1933 1758 public function __construct() {
1934 1759 parent::__construct();
1935 1760
1936 1761 $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 1762 }
2019 1763 }