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