PluginProbe
Elementor Website Builder – more than just a page builder / 3.1.3
Elementor Website Builder – more than just a page builder v3.1.3
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 +289 -610 4.1.0-beta3 → 3.1.3 View file →
@@ -2,25 +2,14 @@
2 2 namespace Elementor\TemplateLibrary;
3 3
4 4 use Elementor\Core\Base\Document;
5 5 use Elementor\Core\Editor\Editor;
6 -use Elementor\Core\Utils\Collection;
7 6 use Elementor\DB;
8 7 use Elementor\Core\Settings\Manager as SettingsManager;
9 -use Elementor\Includes\TemplateLibrary\Sources\AdminMenuItems\Add_New_Template_Menu_Item;
10 -use Elementor\Includes\TemplateLibrary\Sources\AdminMenuItems\Saved_Templates_Menu_Item;
11 -use Elementor\Includes\TemplateLibrary\Sources\AdminMenuItems\Templates_Categories_Menu_Item;
8 +use Elementor\Core\Settings\Page\Model;
9 +use Elementor\Modules\LandingPages\Module as Landing_Page_Module;
12 10 use Elementor\Modules\Library\Documents\Library_Document;
13 11 use Elementor\Plugin;
14 -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 12
24 13 if ( ! defined( 'ABSPATH' ) ) {
25 14 exit; // Exit if accessed directly.
26 15 }
@@ -51,10 +40,9 @@
51 40 const TAXONOMY_CATEGORY_SLUG = 'elementor_library_category';
52 41
53 42 /**
54 43 * Elementor template-library meta key.
55 - *
56 - * @deprecated 2.3.0 Use `Elementor\Core\Base\Document::TYPE_META_KEY` const instead.
44 + * @deprecated 2.3.0 Use \Elementor\Core\Base\Document::TYPE_META_KEY instead
57 45 */
58 46 const TYPE_META_KEY = '_elementor_template_type';
59 47
60 48 /**
@@ -68,10 +56,8 @@
68 56 const BULK_EXPORT_ACTION = 'elementor_export_multiple_templates';
69 57
70 58 const ADMIN_MENU_SLUG = 'edit.php?post_type=elementor_library';
71 59
72 - const ADMIN_MENU_PRIORITY = 10;
73 -
74 60 const ADMIN_SCREEN_ID = 'edit-elementor_library';
75 61
76 62 /**
77 63 * Template types.
@@ -96,18 +82,8 @@
96 82 */
97 83 private $post_type_object;
98 84
99 85 /**
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 86 * @since 2.3.0
111 87 * @access public
112 88 * @static
113 89 * @return array
@@ -220,9 +196,9 @@
220 196 *
221 197 * @return string The local template title.
222 198 */
223 199 public function get_title() {
224 - return esc_html__( 'Local', 'elementor' );
200 + return __( 'Local', 'elementor' );
225 201 }
226 202
227 203 /**
228 204 * Register local template data.
@@ -238,21 +214,21 @@
238 214 * @access public
239 215 */
240 216 public function register_data() {
241 217 $labels = [
242 - 'name' => esc_html_x( 'My Templates', 'Template Library', 'elementor' ),
243 - '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' ),
254 - 'menu_name' => esc_html_x( 'Templates', 'Template Library', 'elementor' ),
218 + 'name' => _x( 'My Templates', 'Template Library', 'elementor' ),
219 + 'singular_name' => _x( 'Template', 'Template Library', 'elementor' ),
220 + 'add_new' => _x( 'Add New', 'Template Library', 'elementor' ),
221 + 'add_new_item' => _x( 'Add New Template', 'Template Library', 'elementor' ),
222 + 'edit_item' => _x( 'Edit Template', 'Template Library', 'elementor' ),
223 + 'new_item' => _x( 'New Template', 'Template Library', 'elementor' ),
224 + 'all_items' => _x( 'All Templates', 'Template Library', 'elementor' ),
225 + 'view_item' => _x( 'View Template', 'Template Library', 'elementor' ),
226 + 'search_items' => _x( 'Search Template', 'Template Library', 'elementor' ),
227 + 'not_found' => _x( 'No Templates found', 'Template Library', 'elementor' ),
228 + 'not_found_in_trash' => _x( 'No Templates found in Trash', 'Template Library', 'elementor' ),
229 + 'parent_item_colon' => '',
230 + 'menu_name' => _x( 'Templates', 'Template Library', 'elementor' ),
255 231 ];
256 232
257 233 $args = [
258 234 'labels' => $labels,
@@ -264,14 +240,11 @@
264 240 'show_in_nav_menus' => false,
265 241 'exclude_from_search' => true,
266 242 'capability_type' => 'post',
267 243 'hierarchical' => false,
268 - 'supports' => [ 'title', 'thumbnail', 'author', 'elementor', 'custom-fields' ],
269 - 'show_in_rest' => true,
244 + 'supports' => [ 'title', 'thumbnail', 'author', 'elementor' ],
270 245 ];
271 246
272 - $this->avoid_rest_access_for_non_admins();
273 -
274 247 /**
275 248 * Register template library post type args.
276 249 *
277 250 * Filters the post type arguments when registering elementor template library post type.
@@ -291,9 +264,9 @@
291 264 'show_admin_column' => true,
292 265 'query_var' => is_admin(),
293 266 'rewrite' => false,
294 267 'public' => false,
295 - 'label' => esc_html_x( 'Type', 'Template Library', 'elementor' ),
268 + 'label' => _x( 'Type', 'Template Library', 'elementor' ),
296 269 ];
297 270
298 271 /**
299 272 * Register template library taxonomy args.
@@ -305,21 +278,10 @@
305 278 * @param array $args Arguments for registering a taxonomy.
306 279 */
307 280 $args = apply_filters( 'elementor/template_library/sources/local/register_taxonomy_args', $args );
308 281
309 - $cpts_to_associate = [ self::CPT ];
282 + $cpts_to_associate = apply_filters( 'elementor/template_library/sources/local/register_taxonomy_cpts', [ self::CPT ] );
310 283
311 - /**
312 - * Custom post types to associate.
313 - *
314 - * Filters the list of custom post types when registering elementor template library taxonomy.
315 - *
316 - * @since 1.0.0
317 - *
318 - * @param array $cpts_to_associate Custom post types. Default is `elementor_library` post type.
319 - */
320 - $cpts_to_associate = apply_filters( 'elementor/template_library/sources/local/register_taxonomy_cpts', $cpts_to_associate );
321 -
322 284 register_taxonomy( self::TAXONOMY_TYPE_SLUG, $cpts_to_associate, $args );
323 285
324 286 /**
325 287 * Categories
@@ -332,11 +294,11 @@
332 294 'query_var' => is_admin(),
333 295 'rewrite' => false,
334 296 'public' => false,
335 297 'labels' => [
336 - 'name' => esc_html_x( 'Categories', 'Template Library', 'elementor' ),
337 - 'singular_name' => esc_html_x( 'Category', 'Template Library', 'elementor' ),
338 - 'all_items' => esc_html_x( 'All Categories', 'Template Library', 'elementor' ),
298 + 'name' => _x( 'Categories', 'Template Library', 'elementor' ),
299 + 'singular_name' => _x( 'Category', 'Template Library', 'elementor' ),
300 + 'all_items' => _x( 'All Categories', 'Template Library', 'elementor' ),
339 301 ],
340 302 ];
341 303
342 304 /**
@@ -352,14 +314,58 @@
352 314
353 315 register_taxonomy( self::TAXONOMY_CATEGORY_SLUG, self::CPT, $args );
354 316 }
355 317
318 + /**
319 + * Remove Add New item from admin menu.
320 + *
321 + * Fired by `admin_menu` action.
322 + *
323 + * @since 2.4.0
324 + * @access public
325 + */
326 + public function admin_menu_reorder() {
327 + global $submenu;
356 328
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() );
329 + if ( ! isset( $submenu[ self::ADMIN_MENU_SLUG ] ) ) {
330 + return;
331 + }
332 + $library_submenu = &$submenu[ self::ADMIN_MENU_SLUG ];
333 +
334 + // Remove 'All Templates' menu.
335 + unset( $library_submenu[5] );
336 +
337 + // If current use can 'Add New' - move the menu to end, and add the '#add_new' anchor.
338 + if ( isset( $library_submenu[10][2] ) ) {
339 + $library_submenu[700] = $library_submenu[10];
340 + unset( $library_submenu[10] );
341 + $library_submenu[700][2] = admin_url( self::ADMIN_MENU_SLUG . '#add_new' );
342 + }
343 +
344 + // Move the 'Categories' menu to end.
345 + if ( isset( $library_submenu[15] ) ) {
346 + $library_submenu[800] = $library_submenu[15];
347 + unset( $library_submenu[15] );
348 + }
349 +
350 + if ( $this->is_current_screen() ) {
351 + $library_title = $this->get_library_title();
352 +
353 + foreach ( $library_submenu as &$item ) {
354 + if ( $library_title === $item[0] ) {
355 + if ( ! isset( $item[4] ) ) {
356 + $item[4] = '';
357 + }
358 + $item[4] .= ' current';
359 + }
360 + }
361 + }
360 362 }
361 363
364 + public function admin_menu() {
365 + add_submenu_page( self::ADMIN_MENU_SLUG, '', __( 'Saved Templates', 'elementor' ), Editor::EDITING_CAPABILITY, self::get_admin_url( true ) );
366 + }
367 +
362 368 public function admin_title( $admin_title, $title ) {
363 369 $library_title = $this->get_library_title();
364 370
365 371 if ( $library_title ) {
@@ -369,54 +375,15 @@
369 375 return $admin_title;
370 376 }
371 377
372 378 public function replace_admin_heading() {
373 - if ( ! $this->is_current_screen() ) {
374 - return;
375 - }
376 -
377 - global $post_type_object;
378 -
379 379 $library_title = $this->get_library_title();
380 380
381 381 if ( $library_title ) {
382 + global $post_type_object;
383 +
382 384 $post_type_object->labels->name = $library_title;
383 385 }
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 386 }
420 387
421 388 /**
422 389 * Get local templates.
@@ -481,35 +448,25 @@
481 448 * @return \WP_Error|int The ID of the saved/updated template, `WP_Error` otherwise.
482 449 */
483 450 public function save_item( $template_data ) {
484 451 if ( ! current_user_can( $this->post_type_object->cap->edit_posts ) ) {
485 - return new \WP_Error( 'save_error', esc_html__( 'Access denied.', 'elementor' ) );
452 + return new \WP_Error( 'save_error', __( 'Access denied.', 'elementor' ) );
486 453 }
487 454
488 455 $defaults = [
489 - 'title' => esc_html__( '(no title)', 'elementor' ),
456 + 'title' => __( '(no title)', 'elementor' ),
490 457 'page_settings' => [],
458 + 'status' => current_user_can( 'publish_posts' ) ? 'publish' : 'pending',
491 459 ];
492 460
493 461 $template_data = wp_parse_args( $template_data, $defaults );
494 - $template_data['status'] = current_user_can( 'publish_posts' ) ? 'publish' : 'pending';
495 462
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 463 $document = Plugin::$instance->documents->create(
508 464 $template_data['type'],
509 465 [
510 466 'post_title' => $template_data['title'],
511 467 'post_status' => $template_data['status'],
468 + 'post_type' => self::CPT,
512 469 ]
513 470 );
514 471
515 472 if ( is_wp_error( $document ) ) {
@@ -556,35 +513,8 @@
556 513
557 514 return $template_id;
558 515 }
559 516
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 517 /**
588 518 * Update local template.
589 519 *
590 520 * Update template on the database.
@@ -597,29 +527,21 @@
597 527 * @return \WP_Error|true True if template updated, `WP_Error` otherwise.
598 528 */
599 529 public function update_item( $new_data ) {
600 530 if ( ! current_user_can( $this->post_type_object->cap->edit_post, $new_data['id'] ) ) {
601 - return new \WP_Error( 'save_error', esc_html__( 'Access denied.', 'elementor' ) );
531 + return new \WP_Error( 'save_error', __( 'Access denied.', 'elementor' ) );
602 532 }
603 533
604 534 $document = Plugin::$instance->documents->get( $new_data['id'] );
605 535
606 536 if ( ! $document ) {
607 - return new \WP_Error( 'save_error', esc_html__( 'Template not exist.', 'elementor' ) );
537 + return new \WP_Error( 'save_error', __( 'Template not exist.', 'elementor' ) );
608 538 }
609 539
610 - $save_data = [];
540 + $document->save( [
541 + 'elements' => $new_data['content'],
542 + ] );
611 543
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 544 /**
623 545 * After template library update.
624 546 *
625 547 * Fires after Elementor template library was updated.
@@ -716,22 +638,8 @@
716 638 }
717 639
718 640 if ( ! empty( $content ) ) {
719 641 $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 642 }
735 643 }
736 644
737 645 $data = [
@@ -743,12 +651,8 @@
743 651
744 652 $data['page_settings'] = $page->get_data( 'settings' );
745 653 }
746 654
747 - if ( ! empty( $content ) ) {
748 - $this->attach_global_styles_to_data( $data, $content, $template_id );
749 - }
750 -
751 655 return $data;
752 656 }
753 657
754 658 /**
@@ -765,28 +669,14 @@
765 669 * or 'WP_Error' on failure.
766 670 */
767 671 public function delete_template( $template_id ) {
768 672 if ( ! current_user_can( $this->post_type_object->cap->delete_post, $template_id ) ) {
769 - return new \WP_Error( 'template_error', esc_html__( 'Access denied.', 'elementor' ) );
673 + return new \WP_Error( 'template_error', __( 'Access denied.', 'elementor' ) );
770 674 }
771 675
772 676 return wp_delete_post( $template_id, true );
773 677 }
774 678
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 679 /**
790 680 * Export local template.
791 681 *
792 682 * Export template to a file.
@@ -798,14 +688,8 @@
798 688 *
799 689 * @return \WP_Error WordPress error if template export failed.
800 690 */
801 691 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 692 $file_data = $this->prepare_template_export( $template_id );
809 693
810 694 if ( is_wp_error( $file_data ) ) {
811 695 return $file_data;
@@ -812,29 +696,17 @@
812 696 }
813 697
814 698 $this->send_file_headers( $file_data['name'], strlen( $file_data['content'] ) );
815 699
816 - $this->serve_file( $file_data['content'] );
700 + // Clear buffering just in case.
701 + @ob_end_clean();
817 702
818 - die;
819 - }
703 + flush();
820 704
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 - }
705 + // Output file contents.
706 + echo $file_data['content'];
826 707
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;
708 + die;
837 709 }
838 710
839 711 /**
840 712 * Export multiple local templates.
@@ -902,12 +774,14 @@
902 774 foreach ( $files as $file ) {
903 775 unlink( $file['path'] );
904 776 }
905 777
906 - $this->send_file_headers( $zip_archive_filename, $this->filesize( $zip_complete_path ) );
778 + $this->send_file_headers( $zip_archive_filename, filesize( $zip_complete_path ) );
907 779
908 - $this->serve_zip( $zip_complete_path );
780 + @ob_end_flush();
909 781
782 + @readfile( $zip_complete_path );
783 +
910 784 unlink( $zip_complete_path );
911 785
912 786 die;
913 787 }
@@ -912,8 +786,46 @@
912 786 die;
913 787 }
914 788
915 789 /**
790 + * Find temporary files.
791 + *
792 + * Recursively finds a list of temporary files from the extracted zip file.
793 + *
794 + * Example return data:
795 + *
796 + * [
797 + * 0 => '/www/wp-content/uploads/elementor/tmp/5eb3a7a411d44/templates/block-2-col-marble-title.json',
798 + * 1 => '/www/wp-content/uploads/elementor/tmp/5eb3a7a411d44/templates/block-2-col-text-and-photo.json',
799 + * ]
800 + *
801 + * @since 2.9.8
802 + * @access private
803 + *
804 + * @param string $temp_path - The temporary file path to scan for template files
805 + *
806 + * @return array An array of temporary files on the filesystem
807 + */
808 + private function find_temp_files( $temp_path ) {
809 +
810 + $file_names = [];
811 +
812 + $possible_file_names = array_diff( scandir( $temp_path ), [ '.', '..' ] );
813 +
814 + // Find nested files in the unzipped path. This happens for example when the user imports a Template Kit.
815 + foreach ( $possible_file_names as $possible_file_name ) {
816 + $full_possible_file_name = $temp_path . '/' . $possible_file_name;
817 + if ( is_dir( $full_possible_file_name ) ) {
818 + $file_names = $file_names + $this->find_temp_files( $full_possible_file_name );
819 + } else {
820 + $file_names[] = $full_possible_file_name;
821 + }
822 + }
823 +
824 + return $file_names;
825 + }
826 +
827 + /**
916 828 * Import local template.
917 829 *
918 830 * Import template from a file.
919 831 *
@@ -919,54 +831,70 @@
919 831 *
920 832 * @since 1.0.0
921 833 * @access public
922 834 *
923 - * @param string $name - The file name.
924 - * @param string $path - The file path.
835 + * @param string $name - The file name
836 + * @param string $path - The file path
837 + *
925 838 * @return \WP_Error|array An array of items on success, 'WP_Error' on failure.
926 839 */
927 - public function import_template( $name, $path, $import_mode = 'match_site' ) {
840 + public function import_template( $name, $path ) {
928 841 if ( empty( $path ) ) {
929 842 return new \WP_Error( 'file_error', 'Please upload a file to import' );
930 843 }
931 844
932 - // Set the Request's state as an Elementor upload request, in order to support unfiltered file uploads.
933 - Plugin::$instance->uploads_manager->set_elementor_upload_state( true );
845 + $items = [];
934 846
935 - $items = [];
847 + $file_extension = pathinfo( $name, PATHINFO_EXTENSION );
936 848
937 - // If the import file is a Zip file with potentially multiple JSON files
938 - if ( 'zip' === pathinfo( $name, PATHINFO_EXTENSION ) ) {
939 - $extracted_files = Plugin::$instance->uploads_manager->extract_and_validate_zip( $path, [ 'json' ] );
849 + if ( 'zip' === $file_extension ) {
850 + if ( ! class_exists( '\ZipArchive' ) ) {
851 + return new \WP_Error( 'zip_error', 'PHP Zip extension not loaded' );
852 + }
940 853
941 - if ( is_wp_error( $extracted_files ) ) {
942 - // Delete the temporary extraction directory, since it's now not necessary.
943 - Plugin::$instance->uploads_manager->remove_file_or_dir( $extracted_files['extraction_directory'] );
854 + $zip = new \ZipArchive();
944 855
945 - return $extracted_files;
856 + $wp_upload_dir = wp_upload_dir();
857 +
858 + $temp_path = $wp_upload_dir['basedir'] . '/' . self::TEMP_FILES_DIR . '/' . uniqid();
859 +
860 + $zip->open( $path );
861 +
862 + $valid_entries = [];
863 +
864 + // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
865 + for ( $i = 0; $i < $zip->numFiles; $i++ ) {
866 + $zipped_file_name = $zip->getNameIndex( $i );
867 + $zipped_extension = pathinfo( $zipped_file_name, PATHINFO_EXTENSION );
868 + // Template Kit zip files contain a `manifest.json` file, this is not a valid Elementor template so ensure we skip it.
869 + if ( 'json' === $zipped_extension && 'manifest.json' !== $zipped_file_name ) {
870 + $valid_entries[] = $zipped_file_name;
871 + }
946 872 }
947 873
948 - 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 - }
874 + if ( ! empty( $valid_entries ) ) {
875 + $zip->extractTo( $temp_path, $valid_entries );
876 + }
953 877
954 - $import_result = $this->import_single_template( $file_path, $import_mode );
878 + $zip->close();
955 879
880 + $file_names = $this->find_temp_files( $temp_path );
881 +
882 + foreach ( $file_names as $full_file_name ) {
883 + $import_result = $this->import_single_template( $full_file_name );
884 +
885 + unlink( $full_file_name );
886 +
956 887 if ( is_wp_error( $import_result ) ) {
957 - // Skip failed files
958 - continue;
888 + return $import_result;
959 889 }
960 890
961 891 $items[] = $import_result;
962 892 }
963 893
964 - // Delete the temporary extraction directory, since it's now not necessary.
965 - Plugin::$instance->uploads_manager->remove_file_or_dir( $extracted_files['extraction_directory'] );
894 + rmdir( $temp_path );
966 895 } else {
967 - // If the import file is a single JSON file
968 - $import_result = $this->import_single_template( $path, $import_mode );
896 + $import_result = $this->import_single_template( $path );
969 897
970 898 if ( is_wp_error( $import_result ) ) {
971 899 return $import_result;
972 900 }
@@ -994,17 +922,9 @@
994 922 */
995 923 public function post_row_actions( $actions, \WP_Post $post ) {
996 924 if ( self::is_base_templates_screen() ) {
997 925 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 );
926 + $actions['export-template'] = sprintf( '<a href="%1$s">%2$s</a>', $this->get_export_link( $post->ID ), __( 'Export Template', 'elementor' ) );
1007 927 }
1008 928 }
1009 929
1010 930 return $actions;
@@ -1022,43 +942,26 @@
1022 942 * @since 1.0.0
1023 943 * @access public
1024 944 */
1025 945 public function admin_import_template_form() {
1026 - if ( ! self::is_base_templates_screen() || ! User::is_current_user_can_upload_json() ) {
946 + if ( ! self::is_base_templates_screen() ) {
1027 947 return;
1028 948 }
1029 949
1030 950 /** @var \Elementor\Core\Common\Modules\Ajax\Module $ajax */
1031 951 $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 952 ?>
1050 953 <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>
954 + <a id="elementor-import-template-trigger" class="page-title-action"><?php echo __( 'Import Templates', 'elementor' ); ?></a>
1052 955 <div id="elementor-import-template-area">
1053 - <div id="elementor-import-template-title"><?php echo esc_html( $description ); ?></div>
1054 - <form id="elementor-import-template-form" method="post" action="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>" enctype="multipart/form-data">
956 + <div id="elementor-import-template-title"><?php echo __( '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>
957 + <form id="elementor-import-template-form" method="post" action="<?php echo admin_url( 'admin-ajax.php' ); ?>" enctype="multipart/form-data">
1055 958 <input type="hidden" name="action" value="elementor_library_direct_actions">
1056 959 <input type="hidden" name="library_action" value="direct_import_template">
1057 - <input type="hidden" name="_nonce" value="<?php Utils::print_unescaped_internal_string( $ajax->create_nonce() ); ?>">
960 + <input type="hidden" name="_nonce" value="<?php echo $ajax->create_nonce(); ?>">
1058 961 <fieldset id="elementor-import-template-form-inputs">
1059 962 <input type="file" name="file" accept=".json,application/json,.zip,application/octet-stream,application/zip,application/x-zip,application/x-zip-compressed" required>
1060 - <input id="e-import-template-action" type="submit" class="button" value="<?php echo esc_attr__( 'Import Now', 'elementor' ); ?>">
963 + <input type="submit" class="button" value="<?php echo esc_attr__( 'Import Now', 'elementor' ); ?>">
1061 964 </fieldset>
1062 965 </form>
1063 966 </div>
1064 967 </div>
@@ -1082,27 +985,12 @@
1082 985 die;
1083 986 }
1084 987 }
1085 988
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 989 /**
1102 990 * Is template library supports export.
1103 991 *
1104 - * Whether the template library supports export.
992 + * whether the template library supports export.
1105 993 *
1106 994 * Template saved by the user locally on his site, support export by default
1107 995 * but this can be changed using a filter.
1108 996 *
@@ -1249,9 +1137,9 @@
1249 1137 *
1250 1138 * @return array An array of the available bulk actions.
1251 1139 */
1252 1140 public function admin_add_bulk_export_action( $actions ) {
1253 - $actions[ self::BULK_EXPORT_ACTION ] = esc_html__( 'Export', 'elementor' );
1141 + $actions[ self::BULK_EXPORT_ACTION ] = __( 'Export', 'elementor' );
1254 1142
1255 1143 return $actions;
1256 1144 }
1257 1145
@@ -1273,10 +1161,9 @@
1273 1161 if ( self::BULK_EXPORT_ACTION === $action ) {
1274 1162 $result = $this->export_multiple_templates( $post_ids );
1275 1163
1276 1164 // If you reach this line, the export failed
1277 - // PHPCS - Not user input.
1278 - wp_die( $result->get_error_message() ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1165 + wp_die( $result->get_error_message() );
1279 1166 }
1280 1167 }
1281 1168
1282 1169 /**
@@ -1293,13 +1180,17 @@
1293 1180 *
1294 1181 * @return array An updated array of available list table views.
1295 1182 */
1296 1183 public function admin_print_tabs( $views ) {
1297 - //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce is not required to retrieve the value.
1298 - $current_type = Utils::get_super_global_value( $_REQUEST, self::TAXONOMY_TYPE_SLUG ) ?? '';
1299 - $active_class = $current_type ? '' : ' nav-tab-active';
1184 + $current_type = '';
1185 + $active_class = ' nav-tab-active';
1300 1186 $current_tabs_group = $this->get_current_tab_group();
1301 1187
1188 + if ( ! empty( $_REQUEST[ self::TAXONOMY_TYPE_SLUG ] ) ) {
1189 + $current_type = $_REQUEST[ self::TAXONOMY_TYPE_SLUG ];
1190 + $active_class = '';
1191 + }
1192 +
1302 1193 $url_args = [
1303 1194 'post_type' => self::CPT,
1304 1195 'tabs_group' => $current_tabs_group,
1305 1196 ];
@@ -1323,15 +1214,15 @@
1323 1214 }
1324 1215
1325 1216 ?>
1326 1217 <div id="elementor-template-library-tabs-wrapper" class="nav-tab-wrapper">
1327 - <a class="nav-tab<?php echo esc_attr( $active_class ); ?>" href="<?php echo esc_url( $baseurl ); ?>">
1218 + <a class="nav-tab<?php echo $active_class; ?>" href="<?php echo $baseurl; ?>">
1328 1219 <?php
1329 1220 $all_title = $this->get_library_title();
1330 1221 if ( ! $all_title ) {
1331 - $all_title = esc_html__( 'All', 'elementor' );
1222 + $all_title = __( 'All', 'elementor' );
1332 1223 }
1333 - Utils::print_unescaped_internal_string( $all_title ); ?>
1224 + echo $all_title; ?>
1334 1225 </a>
1335 1226 <?php
1336 1227 foreach ( $doc_types as $type => $class_name ) :
1337 1228 $active_class = '';
@@ -1339,11 +1230,12 @@
1339 1230 if ( $current_type === $type ) {
1340 1231 $active_class = ' nav-tab-active';
1341 1232 }
1342 1233
1343 - $type_url = esc_url( add_query_arg( self::TAXONOMY_TYPE_SLUG, $type, $baseurl ) );
1234 + $type_url = add_query_arg( self::TAXONOMY_TYPE_SLUG, $type, $baseurl );
1344 1235 $type_label = $this->get_template_label_by_type( $type );
1345 - Utils::print_unescaped_internal_string( "<a class='nav-tab{$active_class}' href='{$type_url}'>{$type_label}</a>" );
1236 +
1237 + echo "<a class='nav-tab{$active_class}' href='{$type_url}'>{$type_label}</a>";
1346 1238 endforeach;
1347 1239 ?>
1348 1240 </div>
1349 1241 <?php
@@ -1361,9 +1253,9 @@
1361 1253 * @since 2.0.0
1362 1254 * @access public
1363 1255 *
1364 1256 * @param string $which The location of the extra table nav markup: 'top' or 'bottom'.
1365 - * @param array $args
1257 + * @param array $args
1366 1258 */
1367 1259 public function maybe_render_blank_state( $which, array $args = [] ) {
1368 1260 global $post_type;
1369 1261
@@ -1379,9 +1271,8 @@
1379 1271 global $wp_list_table;
1380 1272
1381 1273 $total_items = $wp_list_table->get_pagination_arg( 'total_items' );
1382 1274
1383 - //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce is not required to retrieve the value.
1384 1275 if ( ! empty( $total_items ) || ! empty( $_REQUEST['s'] ) ) {
1385 1276 return;
1386 1277 }
1387 1278
@@ -1401,9 +1292,9 @@
1401 1292 // TODO: This code maybe unreachable see if above `if ( empty( $document_types[ $current_type ] ) )`.
1402 1293 if ( empty( $current_type ) ) {
1403 1294 $counts = (array) wp_count_posts( self::CPT );
1404 1295 unset( $counts['auto-draft'] );
1405 - $count = array_sum( $counts );
1296 + $count = array_sum( $counts );
1406 1297
1407 1298 if ( 0 < $count ) {
1408 1299 return;
1409 1300 }
@@ -1417,28 +1308,18 @@
1417 1308 }
1418 1309
1419 1310 private function render_blank_state( $current_type, array $args = [] ) {
1420 1311 $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 1312 $inline_style = '#posts-filter .wp-list-table, #posts-filter .tablenav.top, .tablenav.bottom .actions, .wrap .subsubsub { display:none;}';
1432 1313
1433 1314 $args = wp_parse_args( $args, [
1434 1315 'additional_inline_style' => '',
1435 1316 'href' => '',
1436 - 'description' => $description,
1317 + 'description' => __( 'Add templates and reuse them across your website. Easily export and import them to any other project, for an optimized workflow.', 'elementor' ),
1437 1318 ] );
1438 1319 $inline_style .= $args['additional_inline_style'];
1439 1320 ?>
1440 - <style type="text/css"><?php Utils::print_unescaped_internal_string( $inline_style ); ?></style>
1321 + <style type="text/css"><?php echo $inline_style; ?></style>
1441 1322 <div class="elementor-template_library-blank_state">
1442 1323 <?php $this->print_blank_state_template( $current_type_label, $args['href'], $args['description'] ); ?>
1443 1324 </div>
1444 1325 <?php
@@ -1454,27 +1335,27 @@
1454 1335 *
1455 1336 * @since 3.1.0
1456 1337 * @access public
1457 1338 *
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.).
1339 + * @param string $current_type_label The Entity title
1340 + * @param string $href The URL for the 'Add New' button
1341 + * @param string $description The sub title describing the Entity (Post Type, Taxonomy, etc.)
1461 1342 */
1462 1343 public function print_blank_state_template( $current_type_label, $href, $description ) {
1463 1344 ?>
1464 1345 <div class="elementor-blank_state">
1465 1346 <i class="eicon-folder"></i>
1466 - <h3>
1347 + <h2>
1467 1348 <?php
1468 1349 /* translators: %s: Template type label. */
1469 - printf( esc_html__( 'Create Your First %s', 'elementor' ), esc_html( $current_type_label ) );
1350 + printf( __( 'Create Your First %s', 'elementor' ), $current_type_label );
1470 1351 ?>
1471 - </h3>
1472 - <p><?php echo wp_kses_post( $description ); ?></p>
1473 - <a id="elementor-template-library-add-new" class="elementor-button e-primary" href="<?php echo esc_url( $href ); ?>">
1352 + </h2>
1353 + <p><?php echo $description; ?></p>
1354 + <a id="elementor-template-library-add-new" class="elementor-button elementor-button-success" href="<?php echo $href; ?>">
1474 1355 <?php
1475 1356 /* translators: %s: Template type label. */
1476 - printf( esc_html__( 'Add New %s', 'elementor' ), esc_html( $current_type_label ) );
1357 + printf( __( 'Add New %s', 'elementor' ), $current_type_label );
1477 1358 ?>
1478 1359 </a>
1479 1360 </div>
1480 1361 <?php
@@ -1479,17 +1360,8 @@
1479 1360 </div>
1480 1361 <?php
1481 1362 }
1482 1363
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 1364 public function add_filter_by_category( $post_type ) {
1493 1365 if ( self::CPT !== $post_type ) {
1494 1366 return;
1495 1367 }
@@ -1494,9 +1366,10 @@
1494 1366 return;
1495 1367 }
1496 1368
1497 1369 $all_items = get_taxonomy( self::TAXONOMY_CATEGORY_SLUG )->labels->all_items;
1498 - $dropdown_options = [
1370 +
1371 + $dropdown_options = array(
1499 1372 'show_option_all' => $all_items,
1500 1373 'show_option_none' => $all_items,
1501 1374 'hide_empty' => 0,
1502 1375 'hierarchical' => 1,
@@ -1504,17 +1377,12 @@
1504 1377 'orderby' => 'name',
1505 1378 'value_field' => 'slug',
1506 1379 'taxonomy' => self::TAXONOMY_CATEGORY_SLUG,
1507 1380 'name' => self::TAXONOMY_CATEGORY_SLUG,
1508 - //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce is not required to retrieve the value.
1509 - 'selected' => Utils::get_super_global_value( $_GET, self::TAXONOMY_CATEGORY_SLUG ) ?? '',
1510 - ];
1381 + 'selected' => empty( $_GET[ self::TAXONOMY_CATEGORY_SLUG ] ) ? '' : $_GET[ self::TAXONOMY_CATEGORY_SLUG ],
1382 + );
1511 1383
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 - );
1384 + echo '<label class="screen-reader-text" for="cat">' . _x( 'Filter by category', 'Template Library', 'elementor' ) . '</label>';
1517 1385 wp_dropdown_categories( $dropdown_options );
1518 1386 }
1519 1387
1520 1388 /**
@@ -1524,29 +1392,48 @@
1524 1392 *
1525 1393 * @since 1.6.0
1526 1394 * @access private
1527 1395 *
1528 - * @param string $file_path File name.
1396 + * @param string $file_name File name.
1529 1397 *
1530 1398 * @return \WP_Error|int|array Local template array, or template ID, or
1531 1399 * `WP_Error`.
1532 1400 */
1533 - private function import_single_template( $file_path, $import_mode = 'match_site' ) {
1534 - $data = $this->prepare_import_template_data( $file_path, $import_mode );
1401 + private function import_single_template( $file_name ) {
1402 + $data = json_decode( file_get_contents( $file_name ), true );
1535 1403
1536 - if ( is_wp_error( $data ) ) {
1537 - return $data;
1404 + if ( empty( $data ) ) {
1405 + return new \WP_Error( 'file_error', 'Invalid File' );
1538 1406 }
1539 1407
1540 - if ( empty( $data ) ) {
1541 - return new \WP_Error( 'file_error', 'Invalid File' );
1408 + $content = $data['content'];
1409 +
1410 + if ( ! is_array( $content ) ) {
1411 + return new \WP_Error( 'file_error', 'Invalid Content In File' );
1542 1412 }
1543 1413
1414 + $content = $this->process_export_import_content( $content, 'on_import' );
1415 +
1416 + $page_settings = [];
1417 +
1418 + if ( ! empty( $data['page_settings'] ) ) {
1419 + $page = new Model( [
1420 + 'id' => 0,
1421 + 'settings' => $data['page_settings'],
1422 + ] );
1423 +
1424 + $page_settings_data = $this->process_element_export_import_content( $page, 'on_import' );
1425 +
1426 + if ( ! empty( $page_settings_data['settings'] ) ) {
1427 + $page_settings = $page_settings_data['settings'];
1428 + }
1429 + }
1430 +
1544 1431 $template_id = $this->save_item( [
1545 - 'content' => $data['content'],
1432 + 'content' => $content,
1546 1433 'title' => $data['title'],
1547 1434 'type' => $data['type'],
1548 - 'page_settings' => $data['page_settings'],
1435 + 'page_settings' => $page_settings,
1549 1436 ] );
1550 1437
1551 1438 if ( is_wp_error( $template_id ) ) {
1552 1439 return $template_id;
@@ -1567,30 +1454,35 @@
1567 1454 *
1568 1455 * @return \WP_Error|array Exported template data.
1569 1456 */
1570 1457 private function prepare_template_export( $template_id ) {
1571 - $document = Plugin::$instance->documents->get( $template_id );
1458 + $template_data = $this->get_data( [
1459 + 'template_id' => $template_id,
1460 + ] );
1572 1461
1573 - $template_data = $document->get_export_data();
1574 -
1575 1462 if ( empty( $template_data['content'] ) ) {
1576 1463 return new \WP_Error( 'empty_template', 'The template is empty' );
1577 1464 }
1578 1465
1579 - $content = apply_filters(
1580 - 'elementor/template_library/sources/local/export/elements',
1581 - $template_data['content']
1582 - );
1466 + $template_data['content'] = $this->process_export_import_content( $template_data['content'], 'on_export' );
1583 1467
1468 + if ( get_post_meta( $template_id, '_elementor_page_settings', true ) ) {
1469 + $page = SettingsManager::get_settings_managers( 'page' )->get_model( $template_id );
1470 +
1471 + $page_settings_data = $this->process_element_export_import_content( $page, 'on_export' );
1472 +
1473 + if ( ! empty( $page_settings_data['settings'] ) ) {
1474 + $template_data['page_settings'] = $page_settings_data['settings'];
1475 + }
1476 + }
1477 +
1584 1478 $export_data = [
1585 - 'content' => $content,
1586 - 'page_settings' => $template_data['settings'],
1587 1479 'version' => DB::DB_VERSION,
1588 - 'title' => $document->get_main_post()->post_title,
1480 + 'title' => get_the_title( $template_id ),
1589 1481 'type' => self::get_template_type( $template_id ),
1590 1482 ];
1591 1483
1592 - $this->attach_global_styles_to_data( $export_data, $content, $template_id );
1484 + $export_data += $template_data;
1593 1485
1594 1486 return [
1595 1487 'name' => 'elementor-' . $template_id . '-' . gmdate( 'Y-m-d' ) . '.json',
1596 1488 'content' => wp_json_encode( $export_data ),
@@ -1597,8 +1489,28 @@
1597 1489 ];
1598 1490 }
1599 1491
1600 1492 /**
1493 + * Send file headers.
1494 + *
1495 + * Set the file header when export template data to a file.
1496 + *
1497 + * @since 1.6.0
1498 + * @access private
1499 + *
1500 + * @param string $file_name File name.
1501 + * @param int $file_size File size.
1502 + */
1503 + private function send_file_headers( $file_name, $file_size ) {
1504 + header( 'Content-Type: application/octet-stream' );
1505 + header( 'Content-Disposition: attachment; filename=' . $file_name );
1506 + header( 'Expires: 0' );
1507 + header( 'Cache-Control: must-revalidate' );
1508 + header( 'Pragma: public' );
1509 + header( 'Content-Length: ' . $file_size );
1510 + }
1511 +
1512 + /**
1601 1513 * Get template label by type.
1602 1514 *
1603 1515 * Retrieve the template label for any given template type.
1604 1516 *
@@ -1632,68 +1544,8 @@
1632 1544
1633 1545 return $template_label;
1634 1546 }
1635 1547
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 1548 /**
1697 1549 * Filter template types in admin query.
1698 1550 *
1699 1551 * Update the template types in the main admin query.
@@ -1737,32 +1589,10 @@
1737 1589 * @access private
1738 1590 */
1739 1591 private function add_actions() {
1740 1592 if ( is_admin() ) {
1741 - add_action( 'admin_init', [ $this, 'redirect_categories_page_to_saved_templates_page' ] );
1742 -
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 - } );
1746 -
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;
1751 - } );
1752 -
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 -
1593 + add_action( 'admin_menu', [ $this, 'admin_menu' ] );
1594 + add_action( 'admin_menu', [ $this, 'admin_menu_reorder' ], 800 );
1765 1595 add_filter( 'admin_title', [ $this, 'admin_title' ], 10, 2 );
1766 1596 add_action( 'all_admin_notices', [ $this, 'replace_admin_heading' ] );
1767 1597 add_filter( 'post_row_actions', [ $this, 'post_row_actions' ], 10, 2 );
1768 1598 add_action( 'admin_footer', [ $this, 'admin_import_template_form' ] );
@@ -1788,30 +1618,11 @@
1788 1618 // Show blank state.
1789 1619 add_action( 'manage_posts_extra_tablenav', [ $this, 'maybe_render_blank_state' ] );
1790 1620 }
1791 1621
1792 - add_action( 'elementor/document/after_save', [ $this, 'on_template_update' ], 10, 2 );
1793 -
1794 1622 add_action( 'template_redirect', [ $this, 'block_template_frontend' ] );
1795 -
1796 - // Remove elementor library templates from WP Sitemap
1797 - add_filter(
1798 - 'wp_sitemaps_post_types',
1799 - function( $post_types ) {
1800 - return $this->remove_elementor_cpt_from_sitemap( $post_types );
1801 - }
1802 - );
1803 1623 }
1804 1624
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 1625 /**
1815 1626 * @since 2.0.6
1816 1627 * @access public
1817 1628 */
@@ -1836,24 +1647,26 @@
1836 1647
1837 1648 $offset = 2;
1838 1649
1839 1650 $posts_columns = array_slice( $posts_columns, 0, $offset, true ) + [
1840 - 'elementor_library_type' => esc_html__( 'Type', 'elementor' ),
1651 + 'elementor_library_type' => __( 'Type', 'elementor' ),
1841 1652 ] + array_slice( $posts_columns, $offset, null, true );
1842 1653
1843 1654 return $posts_columns;
1844 1655 }
1845 1656
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 );
1657 + public function get_current_tab_group( $default = '' ) {
1658 + $current_tabs_group = $default;
1849 1659
1850 - if ( $type_slug ) {
1851 - $doc_type = Plugin::$instance->documents->get_document_type( $type_slug, '' );
1660 + if ( ! empty( $_REQUEST[ self::TAXONOMY_TYPE_SLUG ] ) ) {
1661 + $doc_type = Plugin::$instance->documents->get_document_type( $_REQUEST[ self::TAXONOMY_TYPE_SLUG ], '' );
1852 1662 if ( $doc_type ) {
1853 1663 $current_tabs_group = $doc_type::get_property( 'admin_tab_group' );
1854 1664 }
1665 + } elseif ( ! empty( $_REQUEST['tabs_group'] ) ) {
1666 + $current_tabs_group = $_REQUEST['tabs_group'];
1855 1667 }
1668 +
1856 1669 return $current_tabs_group;
1857 1670 }
1858 1671
1859 1672 private function get_library_title() {
@@ -1863,11 +1676,11 @@
1863 1676 $current_tab_group = $this->get_current_tab_group();
1864 1677
1865 1678 if ( $current_tab_group ) {
1866 1679 $titles = [
1867 - 'library' => esc_html__( 'Saved Templates', 'elementor' ),
1868 - 'theme' => esc_html__( 'Theme Builder', 'elementor' ),
1869 - 'popup' => esc_html__( 'Popups', 'elementor' ),
1680 + 'library' => __( 'Saved Templates', 'elementor' ),
1681 + 'theme' => __( 'Theme Builder', 'elementor' ),
1682 + 'popup' => __( 'Popups', 'elementor' ),
1870 1683 ];
1871 1684
1872 1685 if ( ! empty( $titles[ $current_tab_group ] ) ) {
1873 1686 $title = $titles[ $current_tab_group ];
@@ -1884,46 +1697,8 @@
1884 1697 return 'edit.php' === $pagenow && self::CPT === $typenow;
1885 1698 }
1886 1699
1887 1700 /**
1888 - * @param array $post_types
1889 - *
1890 - * @return array
1891 - */
1892 - private function remove_elementor_cpt_from_sitemap( array $post_types ) {
1893 - unset( $post_types[ self::CPT ] );
1894 -
1895 - return $post_types;
1896 - }
1897 -
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 - /**
1926 1701 * Template library local source constructor.
1927 1702 *
1928 1703 * Initializing the template library local source base by registering custom
1929 1704 * template data and running custom actions.
@@ -1934,102 +1709,6 @@
1934 1709 public function __construct() {
1935 1710 parent::__construct();
1936 1711
1937 1712 $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 1713 }
2035 1714 }