PluginProbe
Elementor Website Builder – more than just a page builder / 3.17.2
Elementor Website Builder – more than just a page builder v3.17.2
4.3.1 4.3.0 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 All 454 releases
← All changes | includes/template-library/sources/local.php +193 -451 4.3.0-beta2 → 3.17.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 }
@@ -95,18 +90,8 @@
95 90 */
96 91 private $post_type_object;
97 92
98 93 /**
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 94 * @since 2.3.0
110 95 * @access public
111 96 * @static
112 97 * @return array
@@ -236,21 +221,29 @@
236 221 * @since 1.0.0
237 222 * @access public
238 223 */
239 224 public function register_data() {
225 + $admin_menu_rearrangement_active = Plugin::$instance->experiments->is_feature_active( 'admin_menu_rearrangement' );
226 +
227 + if ( $admin_menu_rearrangement_active ) {
228 + $name = esc_html_x( 'Templates', 'Template Library', 'elementor' );
229 + } else {
230 + $name = esc_html_x( 'My Templates', 'Template Library', 'elementor' );
231 + }
232 +
240 233 $labels = [
241 - 'name' => esc_html_x( 'My Templates', 'Template Library', 'elementor' ),
234 + 'name' => $name,
242 235 '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' ),
236 + 'add_new' => esc_html_x( 'Add New', 'Template Library', 'elementor' ),
237 + 'add_new_item' => esc_html_x( 'Add New Template', 'Template Library', 'elementor' ),
238 + 'edit_item' => esc_html_x( 'Edit Template', 'Template Library', 'elementor' ),
239 + 'new_item' => esc_html_x( 'New Template', 'Template Library', 'elementor' ),
240 + 'all_items' => esc_html_x( 'All Templates', 'Template Library', 'elementor' ),
241 + 'view_item' => esc_html_x( 'View Template', 'Template Library', 'elementor' ),
242 + 'search_items' => esc_html_x( 'Search Template', 'Template Library', 'elementor' ),
243 + 'not_found' => esc_html_x( 'No Templates found', 'Template Library', 'elementor' ),
244 + 'not_found_in_trash' => esc_html_x( 'No Templates found in Trash', 'Template Library', 'elementor' ),
245 + 'parent_item_colon' => '',
253 246 'menu_name' => esc_html_x( 'Templates', 'Template Library', 'elementor' ),
254 247 ];
255 248
256 249 $args = [
@@ -258,19 +251,16 @@
258 251 'public' => true,
259 252 'rewrite' => false,
260 253 'menu_icon' => 'dashicons-admin-page',
261 254 'show_ui' => true,
262 - 'show_in_menu' => true,
255 + 'show_in_menu' => ! $admin_menu_rearrangement_active,
263 256 'show_in_nav_menus' => false,
264 257 'exclude_from_search' => true,
265 258 'capability_type' => 'post',
266 259 'hierarchical' => false,
267 - 'supports' => [ 'title', 'thumbnail', 'author', 'elementor', 'custom-fields' ],
268 - 'show_in_rest' => true,
260 + 'supports' => [ 'title', 'thumbnail', 'author', 'elementor' ],
269 261 ];
270 262
271 - $this->avoid_rest_access_for_non_admins();
272 -
273 263 /**
274 264 * Register template library post type args.
275 265 *
276 266 * Filters the post type arguments when registering elementor template library post type.
@@ -351,14 +341,79 @@
351 341
352 342 register_taxonomy( self::TAXONOMY_CATEGORY_SLUG, self::CPT, $args );
353 343 }
354 344
345 + /**
346 + * Remove Add New item from admin menu.
347 + *
348 + * Fired by `admin_menu` action.
349 + *
350 + * @since 2.4.0
351 + * @access public
352 + */
353 + private function admin_menu_reorder( Admin_Menu_Manager $admin_menu ) {
354 + global $submenu;
355 355
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() );
356 + if ( ! isset( $submenu[ static::ADMIN_MENU_SLUG ] ) ) {
357 + return;
358 + }
359 +
360 + remove_submenu_page( static::ADMIN_MENU_SLUG, static::ADMIN_MENU_SLUG );
361 +
362 + $add_new_slug = 'post-new.php?post_type=' . static::CPT;
363 + $category_slug = 'edit-tags.php?taxonomy=' . static::TAXONOMY_CATEGORY_SLUG . '&amp;post_type=' . static::CPT;
364 +
365 + $library_submenu = new Collection( $submenu[ static::ADMIN_MENU_SLUG ] );
366 +
367 + $add_new_item = $library_submenu->find( function ( $item ) use ( $add_new_slug ) {
368 + return $add_new_slug === $item[2];
369 + } );
370 +
371 + $categories_item = $library_submenu->find( function ( $item ) use ( $category_slug ) {
372 + return $category_slug === $item[2];
373 + } );
374 +
375 + if ( $add_new_item ) {
376 + remove_submenu_page( static::ADMIN_MENU_SLUG, $add_new_slug );
377 +
378 + $admin_menu->register( admin_url( static::ADMIN_MENU_SLUG . '#add_new' ), new Add_New_Template_Menu_Item() );
379 + }
380 +
381 + if ( $categories_item ) {
382 + remove_submenu_page( static::ADMIN_MENU_SLUG, $category_slug );
383 +
384 + $admin_menu->register( $category_slug, new Templates_Categories_Menu_Item() );
385 + }
359 386 }
360 387
388 + /**
389 + * Add a `current` CSS class to the `Saved Templates` submenu page when it's active.
390 + * It should work by default, but since we interfere with WordPress' builtin CPT menus it doesn't work properly.
391 + *
392 + * @return void
393 + */
394 + private function admin_menu_set_current() {
395 + global $submenu;
396 +
397 + if ( $this->is_current_screen() ) {
398 + $library_submenu = &$submenu[ static::ADMIN_MENU_SLUG ];
399 + $library_title = $this->get_library_title();
400 +
401 + foreach ( $library_submenu as &$item ) {
402 + if ( $library_title === $item[0] ) {
403 + if ( ! isset( $item[4] ) ) {
404 + $item[4] = '';
405 + }
406 + $item[4] .= ' current';
407 + }
408 + }
409 + }
410 + }
411 +
412 + private function register_admin_menu( Admin_Menu_Manager $admin_menu ) {
413 + $admin_menu->register( static::get_admin_url( true ), new Saved_Templates_Menu_Item() );
414 + }
415 +
361 416 public function admin_title( $admin_title, $title ) {
362 417 $library_title = $this->get_library_title();
363 418
364 419 if ( $library_title ) {
@@ -368,54 +423,15 @@
368 423 return $admin_title;
369 424 }
370 425
371 426 public function replace_admin_heading() {
372 - if ( ! $this->is_current_screen() ) {
373 - return;
374 - }
375 -
376 - global $post_type_object;
377 -
378 427 $library_title = $this->get_library_title();
379 428
380 429 if ( $library_title ) {
430 + global $post_type_object;
431 +
381 432 $post_type_object->labels->name = $library_title;
382 433 }
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 434 }
419 435
420 436 /**
421 437 * Get local templates.
@@ -568,18 +584,12 @@
568 584 if ( ! $cpt || ! is_array( $cpt ) || 1 !== count( $cpt ) ) {
569 585 return false;
570 586 }
571 587
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 - );
588 + return in_array( static::CPT, $cpt, true );
579 589 }
580 590
581 - /** For testing purposes only, in order to be able to mock the `WP_CLI` constant. */
591 + // For testing purposes only, in order to be able to mock the `WP_CLI` constant.
582 592 protected function is_wp_cli() {
583 593 return Utils::is_wp_cli();
584 594 }
585 595
@@ -605,20 +615,12 @@
605 615 if ( ! $document ) {
606 616 return new \WP_Error( 'save_error', esc_html__( 'Template not exist.', 'elementor' ) );
607 617 }
608 618
609 - $save_data = [];
619 + $document->save( [
620 + 'elements' => $new_data['content'],
621 + ] );
610 622
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 623 /**
622 624 * After template library update.
623 625 *
624 626 * Fires after Elementor template library was updated.
@@ -715,22 +717,8 @@
715 717 }
716 718
717 719 if ( ! empty( $content ) ) {
718 720 $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 721 }
734 722 }
735 723
736 724 $data = [
@@ -742,12 +730,8 @@
742 730
743 731 $data['page_settings'] = $page->get_data( 'settings' );
744 732 }
745 733
746 - if ( ! empty( $content ) ) {
747 - $this->attach_global_styles_to_data( $data, $content, $template_id );
748 - }
749 -
750 734 return $data;
751 735 }
752 736
753 737 /**
@@ -770,22 +754,8 @@
770 754
771 755 return wp_delete_post( $template_id, true );
772 756 }
773 757
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 758 /**
789 759 * Export local template.
790 760 *
791 761 * Export template to a file.
@@ -797,14 +767,8 @@
797 767 *
798 768 * @return \WP_Error WordPress error if template export failed.
799 769 */
800 770 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 771 $file_data = $this->prepare_template_export( $template_id );
808 772
809 773 if ( is_wp_error( $file_data ) ) {
810 774 return $file_data;
@@ -811,29 +775,18 @@
811 775 }
812 776
813 777 $this->send_file_headers( $file_data['name'], strlen( $file_data['content'] ) );
814 778
815 - $this->serve_file( $file_data['content'] );
779 + // Clear buffering just in case.
780 + @ob_end_clean();
816 781
817 - die;
818 - }
782 + flush();
819 783
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 - }
784 + // Output file contents.
785 + // PHPCS - Export widget json
786 + echo $file_data['content']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
825 787
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;
788 + die;
836 789 }
837 790
838 791 /**
839 792 * Export multiple local templates.
@@ -901,12 +854,14 @@
901 854 foreach ( $files as $file ) {
902 855 unlink( $file['path'] );
903 856 }
904 857
905 - $this->send_file_headers( $zip_archive_filename, $this->filesize( $zip_complete_path ) );
858 + $this->send_file_headers( $zip_archive_filename, filesize( $zip_complete_path ) );
906 859
907 - $this->serve_zip( $zip_complete_path );
860 + @ob_end_flush();
908 861
862 + @readfile( $zip_complete_path );
863 +
909 864 unlink( $zip_complete_path );
910 865
911 866 die;
912 867 }
@@ -918,13 +873,13 @@
918 873 *
919 874 * @since 1.0.0
920 875 * @access public
921 876 *
922 - * @param string $name - The file name.
923 - * @param string $path - The file path.
877 + * @param string $name - The file name
878 + * @param string $path - The file path
924 879 * @return \WP_Error|array An array of items on success, 'WP_Error' on failure.
925 880 */
926 - public function import_template( $name, $path, $import_mode = 'match_site' ) {
881 + public function import_template( $name, $path ) {
927 882 if ( empty( $path ) ) {
928 883 return new \WP_Error( 'file_error', 'Please upload a file to import' );
929 884 }
930 885
@@ -944,18 +899,15 @@
944 899 return $extracted_files;
945 900 }
946 901
947 902 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 - }
903 + $import_result = $this->import_single_template( $file_path );
952 904
953 - $import_result = $this->import_single_template( $file_path, $import_mode );
905 + if ( is_wp_error( $import_result ) ) {
906 + // Delete the temporary extraction directory, since it's now not necessary.
907 + Plugin::$instance->uploads_manager->remove_file_or_dir( $extracted_files['extraction_directory'] );
954 908
955 - if ( is_wp_error( $import_result ) ) {
956 - // Skip failed files
957 - continue;
909 + return $import_result;
958 910 }
959 911
960 912 $items[] = $import_result;
961 913 }
@@ -963,9 +915,9 @@
963 915 // Delete the temporary extraction directory, since it's now not necessary.
964 916 Plugin::$instance->uploads_manager->remove_file_or_dir( $extracted_files['extraction_directory'] );
965 917 } else {
966 918 // If the import file is a single JSON file
967 - $import_result = $this->import_single_template( $path, $import_mode );
919 + $import_result = $this->import_single_template( $path );
968 920
969 921 if ( is_wp_error( $import_result ) ) {
970 922 return $import_result;
971 923 }
@@ -993,17 +945,9 @@
993 945 */
994 946 public function post_row_actions( $actions, \WP_Post $post ) {
995 947 if ( self::is_base_templates_screen() ) {
996 948 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 );
949 + $actions['export-template'] = sprintf( '<a href="%1$s">%2$s</a>', $this->get_export_link( $post->ID ), esc_html__( 'Export Template', 'elementor' ) );
1006 950 }
1007 951 }
1008 952
1009 953 return $actions;
@@ -1021,36 +965,19 @@
1021 965 * @since 1.0.0
1022 966 * @access public
1023 967 */
1024 968 public function admin_import_template_form() {
1025 - if ( ! self::is_base_templates_screen() || ! User::is_current_user_can_upload_json() ) {
969 + if ( ! self::is_base_templates_screen() ) {
1026 970 return;
1027 971 }
1028 972
1029 973 /** @var \Elementor\Core\Common\Modules\Ajax\Module $ajax */
1030 974 $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 975 ?>
1049 976 <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>
977 + <a id="elementor-import-template-trigger" class="page-title-action"><?php echo esc_html__( 'Import Templates', 'elementor' ); ?></a>
1051 978 <div id="elementor-import-template-area">
1052 - <div id="elementor-import-template-title"><?php echo esc_html( $description ); ?></div>
979 + <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 980 <form id="elementor-import-template-form" method="post" action="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>" enctype="multipart/form-data">
1054 981 <input type="hidden" name="action" value="elementor_library_direct_actions">
1055 982 <input type="hidden" name="library_action" value="direct_import_template">
1056 983 <input type="hidden" name="_nonce" value="<?php Utils::print_unescaped_internal_string( $ajax->create_nonce() ); ?>">
@@ -1081,27 +1008,12 @@
1081 1008 die;
1082 1009 }
1083 1010 }
1084 1011
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 1012 /**
1101 1013 * Is template library supports export.
1102 1014 *
1103 - * Whether the template library supports export.
1015 + * whether the template library supports export.
1104 1016 *
1105 1017 * Template saved by the user locally on his site, support export by default
1106 1018 * but this can be changed using a filter.
1107 1019 *
@@ -1266,10 +1178,8 @@
1266 1178 *
1267 1179 * @param string $redirect_to The redirect URL.
1268 1180 * @param string $action The action being taken.
1269 1181 * @param array $post_ids The items to take the action on.
1270 - *
1271 - * @return string The redirect URL, unchanged when the action is not handled here.
1272 1182 */
1273 1183 public function admin_export_multiple_templates( $redirect_to, $action, $post_ids ) {
1274 1184 if ( self::BULK_EXPORT_ACTION === $action ) {
1275 1185 $result = $this->export_multiple_templates( $post_ids );
@@ -1277,10 +1187,8 @@
1277 1187 // If you reach this line, the export failed
1278 1188 // PHPCS - Not user input.
1279 1189 wp_die( $result->get_error_message() ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1280 1190 }
1281 -
1282 - return $redirect_to;
1283 1191 }
1284 1192
1285 1193 /**
1286 1194 * Print admin tabs.
@@ -1364,9 +1272,9 @@
1364 1272 * @since 2.0.0
1365 1273 * @access public
1366 1274 *
1367 1275 * @param string $which The location of the extra table nav markup: 'top' or 'bottom'.
1368 - * @param array $args
1276 + * @param array $args
1369 1277 */
1370 1278 public function maybe_render_blank_state( $which, array $args = [] ) {
1371 1279 global $post_type;
1372 1280
@@ -1404,9 +1312,9 @@
1404 1312 // TODO: This code maybe unreachable see if above `if ( empty( $document_types[ $current_type ] ) )`.
1405 1313 if ( empty( $current_type ) ) {
1406 1314 $counts = (array) wp_count_posts( self::CPT );
1407 1315 unset( $counts['auto-draft'] );
1408 - $count = array_sum( $counts );
1316 + $count = array_sum( $counts );
1409 1317
1410 1318 if ( 0 < $count ) {
1411 1319 return;
1412 1320 }
@@ -1420,24 +1328,14 @@
1420 1328 }
1421 1329
1422 1330 private function render_blank_state( $current_type, array $args = [] ) {
1423 1331 $current_type_label = $this->get_template_label_by_type( $current_type );
1424 - $type_labels = $this->get_template_labels_by_type( $current_type );
1425 - $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' );
1426 -
1427 - if ( $type_labels['plural'] ) {
1428 - $description = sprintf(
1429 - /* translators: %s: Template label (plural). */
1430 - esc_html__( 'Add %s and reuse them across your website. Easily export and import them to any other project, for an optimized workflow.', 'elementor' ),
1431 - $type_labels['plural']
1432 - );
1433 - }
1434 1332 $inline_style = '#posts-filter .wp-list-table, #posts-filter .tablenav.top, .tablenav.bottom .actions, .wrap .subsubsub { display:none;}';
1435 1333
1436 1334 $args = wp_parse_args( $args, [
1437 1335 'additional_inline_style' => '',
1438 1336 'href' => '',
1439 - 'description' => $description,
1337 + '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' ),
1440 1338 ] );
1441 1339 $inline_style .= $args['additional_inline_style'];
1442 1340 ?>
1443 1341 <style type="text/css"><?php Utils::print_unescaped_internal_string( $inline_style ); ?></style>
@@ -1457,11 +1355,11 @@
1457 1355 *
1458 1356 * @since 3.1.0
1459 1357 * @access public
1460 1358 *
1461 - * @param string $current_type_label The Entity title.
1462 - * @param string $href The URL for the 'Add New' button.
1463 - * @param string $description The sub title describing the Entity (Post Type, Taxonomy, etc.).
1359 + * @param string $current_type_label The Entity title
1360 + * @param string $href The URL for the 'Add New' button
1361 + * @param string $description The sub title describing the Entity (Post Type, Taxonomy, etc.)
1464 1362 */
1465 1363 public function print_blank_state_template( $current_type_label, $href, $description ) {
1466 1364 ?>
1467 1365 <div class="elementor-blank_state">
@@ -1497,9 +1395,9 @@
1497 1395 return;
1498 1396 }
1499 1397
1500 1398 $all_items = get_taxonomy( self::TAXONOMY_CATEGORY_SLUG )->labels->all_items;
1501 - $dropdown_options = [
1399 + $dropdown_options = array(
1502 1400 'show_option_all' => $all_items,
1503 1401 'show_option_none' => $all_items,
1504 1402 'hide_empty' => 0,
1505 1403 'hierarchical' => 1,
@@ -1509,11 +1407,12 @@
1509 1407 'taxonomy' => self::TAXONOMY_CATEGORY_SLUG,
1510 1408 'name' => self::TAXONOMY_CATEGORY_SLUG,
1511 1409 //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce is not required to retrieve the value.
1512 1410 'selected' => Utils::get_super_global_value( $_GET, self::TAXONOMY_CATEGORY_SLUG ) ?? '',
1513 - ];
1411 + );
1514 1412
1515 1413 printf(
1414 + /* translators: 1: Taxonomy slug, 2: Label text. */
1516 1415 '<label class="screen-reader-text" for="%1$s">%2$s</label>',
1517 1416 esc_attr( self::TAXONOMY_CATEGORY_SLUG ),
1518 1417 esc_html_x( 'Filter by category', 'Template Library', 'elementor' )
1519 1418 );
@@ -1532,24 +1431,43 @@
1532 1431 *
1533 1432 * @return \WP_Error|int|array Local template array, or template ID, or
1534 1433 * `WP_Error`.
1535 1434 */
1536 - private function import_single_template( $file_path, $import_mode = 'match_site' ) {
1537 - $data = $this->prepare_import_template_data( $file_path, $import_mode );
1435 + private function import_single_template( $file_path ) {
1436 + $data = json_decode( Utils::file_get_contents( $file_path ), true );
1538 1437
1539 - if ( is_wp_error( $data ) ) {
1540 - return $data;
1438 + if ( empty( $data ) ) {
1439 + return new \WP_Error( 'file_error', 'Invalid File' );
1541 1440 }
1542 1441
1543 - if ( empty( $data ) ) {
1544 - return new \WP_Error( 'file_error', 'Invalid File' );
1442 + $content = $data['content'];
1443 +
1444 + if ( ! is_array( $content ) ) {
1445 + return new \WP_Error( 'file_error', 'Invalid Content In File' );
1545 1446 }
1546 1447
1448 + $content = $this->process_export_import_content( $content, 'on_import' );
1449 +
1450 + $page_settings = [];
1451 +
1452 + if ( ! empty( $data['page_settings'] ) ) {
1453 + $page = new Model( [
1454 + 'id' => 0,
1455 + 'settings' => $data['page_settings'],
1456 + ] );
1457 +
1458 + $page_settings_data = $this->process_element_export_import_content( $page, 'on_import' );
1459 +
1460 + if ( ! empty( $page_settings_data['settings'] ) ) {
1461 + $page_settings = $page_settings_data['settings'];
1462 + }
1463 + }
1464 +
1547 1465 $template_id = $this->save_item( [
1548 - 'content' => $data['content'],
1466 + 'content' => $content,
1549 1467 'title' => $data['title'],
1550 1468 'type' => $data['type'],
1551 - 'page_settings' => $data['page_settings'],
1469 + 'page_settings' => $page_settings,
1552 1470 ] );
1553 1471
1554 1472 if ( is_wp_error( $template_id ) ) {
1555 1473 return $template_id;
@@ -1578,15 +1496,10 @@
1578 1496 if ( empty( $template_data['content'] ) ) {
1579 1497 return new \WP_Error( 'empty_template', 'The template is empty' );
1580 1498 }
1581 1499
1582 - $content = apply_filters(
1583 - 'elementor/template_library/sources/local/export/elements',
1584 - $template_data['content']
1585 - );
1586 -
1587 1500 $export_data = [
1588 - 'content' => $content,
1501 + 'content' => $template_data['content'],
1589 1502 'page_settings' => $template_data['settings'],
1590 1503 'version' => DB::DB_VERSION,
1591 1504 'title' => $document->get_main_post()->post_title,
1592 1505 'type' => self::get_template_type( $template_id ),
@@ -1591,10 +1504,8 @@
1591 1504 'title' => $document->get_main_post()->post_title,
1592 1505 'type' => self::get_template_type( $template_id ),
1593 1506 ];
1594 1507
1595 - $this->attach_global_styles_to_data( $export_data, $content, $template_id );
1596 -
1597 1508 return [
1598 1509 'name' => 'elementor-' . $template_id . '-' . gmdate( 'Y-m-d' ) . '.json',
1599 1510 'content' => wp_json_encode( $export_data ),
1600 1511 ];
@@ -1600,8 +1511,28 @@
1600 1511 ];
1601 1512 }
1602 1513
1603 1514 /**
1515 + * Send file headers.
1516 + *
1517 + * Set the file header when export template data to a file.
1518 + *
1519 + * @since 1.6.0
1520 + * @access private
1521 + *
1522 + * @param string $file_name File name.
1523 + * @param int $file_size File size.
1524 + */
1525 + private function send_file_headers( $file_name, $file_size ) {
1526 + header( 'Content-Type: application/octet-stream' );
1527 + header( 'Content-Disposition: attachment; filename=' . $file_name );
1528 + header( 'Expires: 0' );
1529 + header( 'Cache-Control: must-revalidate' );
1530 + header( 'Pragma: public' );
1531 + header( 'Content-Length: ' . $file_size );
1532 + }
1533 +
1534 + /**
1604 1535 * Get template label by type.
1605 1536 *
1606 1537 * Retrieve the template label for any given template type.
1607 1538 *
@@ -1635,68 +1566,8 @@
1635 1566
1636 1567 return $template_label;
1637 1568 }
1638 1569
1639 - private function get_template_labels_by_type( $template_type ) {
1640 - $labels = [
1641 - 'singular' => '',
1642 - 'plural' => '',
1643 - ];
1644 -
1645 - if ( ! $template_type ) {
1646 - return $labels;
1647 - }
1648 -
1649 - $document_types = Plugin::instance()->documents->get_document_types();
1650 -
1651 - if ( isset( $document_types[ $template_type ] ) ) {
1652 - $doc_type = $document_types[ $template_type ];
1653 - $labels['singular'] = call_user_func( [ $doc_type, 'get_title' ] );
1654 - $labels['plural'] = call_user_func( [ $doc_type, 'get_plural_title' ] );
1655 - } else {
1656 - $labels['singular'] = $this->get_template_label_by_type( $template_type );
1657 - }
1658 -
1659 - if ( ! $labels['plural'] ) {
1660 - $labels['plural'] = $labels['singular'];
1661 - }
1662 -
1663 - return $labels;
1664 - }
1665 -
1666 - private function get_current_template_labels() {
1667 - $labels = [
1668 - 'singular' => '',
1669 - 'plural' => '',
1670 - ];
1671 -
1672 - $template_type = Utils::get_super_global_value( $_GET, self::TAXONOMY_TYPE_SLUG );
1673 -
1674 - if ( $template_type ) {
1675 - return $this->get_template_labels_by_type( $template_type );
1676 - }
1677 -
1678 - $current_tabs_group = $this->get_current_tab_group();
1679 -
1680 - if ( $current_tabs_group ) {
1681 - $doc_types = Plugin::$instance->documents->get_document_types( [
1682 - 'admin_tab_group' => $current_tabs_group,
1683 - ], 'and' );
1684 -
1685 - if ( 1 === count( $doc_types ) ) {
1686 - $doc_type = reset( $doc_types );
1687 - $labels['singular'] = call_user_func( [ $doc_type, 'get_title' ] );
1688 - $labels['plural'] = call_user_func( [ $doc_type, 'get_plural_title' ] );
1689 - }
1690 - }
1691 -
1692 - if ( ! $labels['plural'] ) {
1693 - $labels['plural'] = $labels['singular'];
1694 - }
1695 -
1696 - return $labels;
1697 - }
1698 -
1699 1570 /**
1700 1571 * Filter template types in admin query.
1701 1572 *
1702 1573 * Update the template types in the main admin query.
@@ -1740,32 +1611,20 @@
1740 1611 * @access private
1741 1612 */
1742 1613 private function add_actions() {
1743 1614 if ( is_admin() ) {
1744 - add_action( 'admin_init', [ $this, 'redirect_categories_page_to_saved_templates_page' ] );
1615 + add_action( 'elementor/admin/menu/register', function ( Admin_Menu_Manager $admin_menu ) {
1616 + $this->register_admin_menu( $admin_menu );
1617 + }, static::ADMIN_MENU_PRIORITY );
1745 1618
1746 - add_action( 'elementor/editor-one/menu/register', function ( Menu_Data_Provider $menu_data_provider ) {
1747 - $this->register_editor_one_menu( $menu_data_provider );
1748 - } );
1619 + add_action( 'elementor/admin/menu/register', function ( Admin_Menu_Manager $admin_menu ) {
1620 + $this->admin_menu_reorder( $admin_menu );
1621 + }, 800 );
1749 1622
1750 - add_action( 'elementor/editor-one/menu/excluded_level4_slugs', function ( array $excluded_slugs ): array {
1751 - $excluded_slugs[] = 'edit.php?post_type=elementor_library#add_new';
1752 - $excluded_slugs[] = 'edit-tags.php?taxonomy=elementor_library_category&amp;post_type=elementor_library';
1753 - return $excluded_slugs;
1623 + add_action( 'elementor/admin/menu/after_register', function () {
1624 + $this->admin_menu_set_current();
1754 1625 } );
1755 1626
1756 - add_filter( 'elementor/editor-one/menu/elementor_post_types', function ( array $elementor_post_types ): array {
1757 - $elementor_post_types[ self::CPT ] = [];
1758 -
1759 - return $elementor_post_types;
1760 - } );
1761 -
1762 - add_filter( 'elementor/editor-one/admin-edit-post-types', function ( array $post_types ): array {
1763 - $post_types[] = self::CPT;
1764 -
1765 - return $post_types;
1766 - } );
1767 -
1768 1627 add_filter( 'admin_title', [ $this, 'admin_title' ], 10, 2 );
1769 1628 add_action( 'all_admin_notices', [ $this, 'replace_admin_heading' ] );
1770 1629 add_filter( 'post_row_actions', [ $this, 'post_row_actions' ], 10, 2 );
1771 1630 add_action( 'admin_footer', [ $this, 'admin_import_template_form' ] );
@@ -1791,10 +1650,8 @@
1791 1650 // Show blank state.
1792 1651 add_action( 'manage_posts_extra_tablenav', [ $this, 'maybe_render_blank_state' ] );
1793 1652 }
1794 1653
1795 - add_action( 'elementor/document/after_save', [ $this, 'on_template_update' ], 10, 2 );
1796 -
1797 1654 add_action( 'template_redirect', [ $this, 'block_template_frontend' ] );
1798 1655
1799 1656 // Remove elementor library templates from WP Sitemap
1800 1657 add_filter(
@@ -1804,17 +1661,8 @@
1804 1661 }
1805 1662 );
1806 1663 }
1807 1664
1808 - public function on_template_update( \Elementor\Core\Base\Document $document, array $data ) {
1809 - if ( ! empty( $data['post_title'] ) ) {
1810 - wp_update_post( [
1811 - 'ID' => $document->get_main_id(),
1812 - 'post_title' => $data['post_title'],
1813 - ] );
1814 - }
1815 - }
1816 -
1817 1665 /**
1818 1666 * @since 2.0.6
1819 1667 * @access public
1820 1668 */
@@ -1845,11 +1693,13 @@
1845 1693
1846 1694 return $posts_columns;
1847 1695 }
1848 1696
1849 - public function get_current_tab_group() {
1850 - $current_tabs_group = Utils::get_super_global_value( $_GET, 'tabs_group' ) ?? '';
1851 - $type_slug = Utils::get_super_global_value( $_GET, self::TAXONOMY_TYPE_SLUG );
1697 + public function get_current_tab_group( $default = '' ) {
1698 + //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce verification is not required here.
1699 + $current_tabs_group = Utils::get_super_global_value( $_REQUEST, 'tabs_group' ) ?? '';
1700 + //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce verification is not required here.
1701 + $type_slug = Utils::get_super_global_value( $_REQUEST, self::TAXONOMY_TYPE_SLUG );
1852 1702
1853 1703 if ( $type_slug ) {
1854 1704 $doc_type = Plugin::$instance->documents->get_document_type( $type_slug, '' );
1855 1705 if ( $doc_type ) {
@@ -1897,35 +1747,8 @@
1897 1747
1898 1748 return $post_types;
1899 1749 }
1900 1750
1901 - private function avoid_rest_access_for_non_admins(): void {
1902 - add_filter( 'rest_pre_dispatch', function ( $value, \WP_REST_Server $server, \WP_REST_Request $request ) {
1903 - if ( strpos( $request->get_route(), self::CPT ) !== false ) {
1904 - if ( ! current_user_can( 'manage_options' ) ) {
1905 - return new \WP_Error( 'rest_forbidden', esc_html__( 'Sorry, you are not allowed to do that.', 'elementor' ), [ 'status' => rest_authorization_required_code() ] );
1906 - }
1907 - }
1908 -
1909 - return $value;
1910 - }, 10, 3 );
1911 - }
1912 -
1913 - public function save_bulk_items( array $args = [] ) {
1914 - $items = [];
1915 -
1916 - foreach ( $args as $item ) {
1917 - $items[] = $this->save_item( [
1918 - 'content' => $item['content'],
1919 - 'title' => $item['title'],
1920 - 'type' => $item['type'],
1921 - 'page_settings' => $item['page_settings'],
1922 - ] );
1923 - }
1924 -
1925 - return $items;
1926 - }
1927 -
1928 1751 /**
1929 1752 * Template library local source constructor.
1930 1753 *
1931 1754 * Initializing the template library local source base by registering custom
@@ -1937,87 +1760,6 @@
1937 1760 public function __construct() {
1938 1761 parent::__construct();
1939 1762
1940 1763 $this->add_actions();
1941 - }
1942 -
1943 - public function format_args_for_bulk_action( $args ) {
1944 - $bulk_args = [];
1945 -
1946 - foreach ( $args['from_template_id'] as $from_template_id ) {
1947 - if ( ! $this->is_allowed_to_read_template( [
1948 - 'template_id' => $from_template_id,
1949 - ] ) ) {
1950 - continue;
1951 - }
1952 -
1953 - $document = Plugin::$instance->documents->get( $from_template_id );
1954 -
1955 - if ( ! $document ) {
1956 - continue;
1957 - }
1958 -
1959 - $page = SettingsManager::get_settings_managers( 'page' )->get_model( $from_template_id );
1960 -
1961 - $bulk_args[] = array_merge(
1962 - $args,
1963 - [
1964 - 'title' => $document->get_post()->post_title,
1965 - 'type' => $document::get_type(),
1966 - 'content' => $document->get_elements_data(),
1967 - 'page_settings' => $page->get_data( 'settings' ),
1968 - ]
1969 - );
1970 - }
1971 -
1972 - return $bulk_args;
1973 - }
1974 -
1975 - public function format_args_for_single_action( $args ) {
1976 - if ( ! $this->is_allowed_to_read_template( [
1977 - 'template_id' => $args['from_template_id'],
1978 - ] ) ) {
1979 - return new \WP_Error(
1980 - 'template_error',
1981 - esc_html__( 'You do not have permission to access this template.', 'elementor' )
1982 - );
1983 - }
1984 -
1985 - $document = Plugin::$instance->documents->get( $args['from_template_id'] );
1986 -
1987 - if ( ! $document ) {
1988 - return new \WP_Error( 'template_error', 'Document not found.' );
1989 - }
1990 -
1991 - $args['content'] = $document->get_elements_data();
1992 -
1993 - $page = SettingsManager::get_settings_managers( 'page' )->get_model( $args['from_template_id'] );
1994 - $args['page_settings'] = $page->get_data( 'settings' );
1995 -
1996 - return $args;
1997 - }
1998 -
1999 - public function is_allowed_to_read_template( array $args ): bool {
2000 - if ( null === $this->wordpress_adapter ) {
2001 - $this->set_wordpress_adapter( new WordPress_Adapter() );
2002 - }
2003 -
2004 - $post_id = intval( $args['template_id'] );
2005 - $post_status = $this->wordpress_adapter->get_post_status( $post_id );
2006 -
2007 - if ( 'publish' === $post_status && ! post_password_required( $post_id ) ) {
2008 - return true;
2009 - }
2010 -
2011 - $can_read_template = $this->wordpress_adapter->current_user_can( 'edit_post', $post_id );
2012 -
2013 - return apply_filters( 'elementor/template-library/is_allowed_to_read_template', $can_read_template, $args );
2014 - }
2015 -
2016 - public function set_wordpress_adapter( Wordpress_Adapter_Interface $wordpress_adapter ) {
2017 - $this->wordpress_adapter = $wordpress_adapter;
2018 - }
2019 -
2020 - public function set_elementor_adapter( Elementor_Adapter_Interface $elementor_adapter ): void {
2021 - $this->elementor_adapter = $elementor_adapter;
2022 1764 }
2023 1765 }