PluginProbe
Elementor Website Builder – more than just a page builder / 3.0.7
Elementor Website Builder – more than just a page builder v3.0.7
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 +301 -652 4.3.0-beta3 → 3.0.7 View file →
@@ -2,24 +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;
12 9 use Elementor\Modules\Library\Documents\Library_Document;
13 10 use Elementor\Plugin;
14 11 use Elementor\Utils;
15 -use Elementor\User;
16 -use Elementor\Core\Isolation\Wordpress_Adapter;
17 -use Elementor\Core\Isolation\Wordpress_Adapter_Interface;
18 -use Elementor\Core\Isolation\Elementor_Adapter_Interface;
19 -use Elementor\Modules\EditorOne\Classes\Menu_Data_Provider;
20 -use Elementor\Includes\TemplateLibrary\Sources\AdminMenuItems\Editor_One_Saved_Templates_Menu;
21 -use Elementor\Includes\TemplateLibrary\Sources\AdminMenuItems\Editor_One_Templates_Menu;
22 12
23 13 if ( ! defined( 'ABSPATH' ) ) {
24 14 exit; // Exit if accessed directly.
25 15 }
@@ -50,10 +40,9 @@
50 40 const TAXONOMY_CATEGORY_SLUG = 'elementor_library_category';
51 41
52 42 /**
53 43 * Elementor template-library meta key.
54 - *
55 - * @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
56 45 */
57 46 const TYPE_META_KEY = '_elementor_template_type';
58 47
59 48 /**
@@ -67,10 +56,8 @@
67 56 const BULK_EXPORT_ACTION = 'elementor_export_multiple_templates';
68 57
69 58 const ADMIN_MENU_SLUG = 'edit.php?post_type=elementor_library';
70 59
71 - const ADMIN_MENU_PRIORITY = 10;
72 -
73 60 const ADMIN_SCREEN_ID = 'edit-elementor_library';
74 61
75 62 /**
76 63 * Template types.
@@ -95,18 +82,8 @@
95 82 */
96 83 private $post_type_object;
97 84
98 85 /**
99 - * @var Wordpress_Adapter_Interface
100 - */
101 - protected $wordpress_adapter = null;
102 -
103 - /**
104 - * @var Elementor_Adapter_Interface
105 - */
106 - protected $elementor_adapter = null;
107 -
108 - /**
109 86 * @since 2.3.0
110 87 * @access public
111 88 * @static
112 89 * @return array
@@ -219,9 +196,9 @@
219 196 *
220 197 * @return string The local template title.
221 198 */
222 199 public function get_title() {
223 - return esc_html__( 'Local', 'elementor' );
200 + return __( 'Local', 'elementor' );
224 201 }
225 202
226 203 /**
227 204 * Register local template data.
@@ -237,21 +214,21 @@
237 214 * @access public
238 215 */
239 216 public function register_data() {
240 217 $labels = [
241 - 'name' => esc_html_x( 'My Templates', 'Template Library', 'elementor' ),
242 - 'singular_name' => esc_html_x( 'Template', 'Template Library', 'elementor' ),
243 - 'add_new' => esc_html__( 'Add New Template', 'elementor' ),
244 - 'add_new_item' => esc_html__( 'Add New Template', 'elementor' ),
245 - 'edit_item' => esc_html__( 'Edit Template', 'elementor' ),
246 - 'new_item' => esc_html__( 'New Template', 'elementor' ),
247 - 'all_items' => esc_html__( 'All Templates', 'elementor' ),
248 - 'view_item' => esc_html__( 'View Template', 'elementor' ),
249 - 'search_items' => esc_html__( 'Search Template', 'elementor' ),
250 - 'not_found' => esc_html__( 'No Templates found', 'elementor' ),
251 - 'not_found_in_trash' => esc_html__( 'No Templates found in Trash', 'elementor' ),
252 - 'parent_item_colon' => esc_html__( 'Parent Template:', 'elementor' ),
253 - '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' ),
254 231 ];
255 232
256 233 $args = [
257 234 'labels' => $labels,
@@ -263,14 +240,11 @@
263 240 'show_in_nav_menus' => false,
264 241 'exclude_from_search' => true,
265 242 'capability_type' => 'post',
266 243 'hierarchical' => false,
267 - 'supports' => [ 'title', 'thumbnail', 'author', 'elementor', 'custom-fields' ],
268 - 'show_in_rest' => true,
244 + 'supports' => [ 'title', 'thumbnail', 'author', 'elementor' ],
269 245 ];
270 246
271 - $this->avoid_rest_access_for_non_admins();
272 -
273 247 /**
274 248 * Register template library post type args.
275 249 *
276 250 * Filters the post type arguments when registering elementor template library post type.
@@ -290,9 +264,9 @@
290 264 'show_admin_column' => true,
291 265 'query_var' => is_admin(),
292 266 'rewrite' => false,
293 267 'public' => false,
294 - 'label' => esc_html_x( 'Type', 'Template Library', 'elementor' ),
268 + 'label' => _x( 'Type', 'Template Library', 'elementor' ),
295 269 ];
296 270
297 271 /**
298 272 * Register template library taxonomy args.
@@ -304,24 +278,11 @@
304 278 * @param array $args Arguments for registering a taxonomy.
305 279 */
306 280 $args = apply_filters( 'elementor/template_library/sources/local/register_taxonomy_args', $args );
307 281
308 - $cpts_to_associate = [ self::CPT ];
282 + register_taxonomy( self::TAXONOMY_TYPE_SLUG, self::CPT, $args );
309 283
310 284 /**
311 - * Custom post types to associate.
312 - *
313 - * Filters the list of custom post types when registering elementor template library taxonomy.
314 - *
315 - * @since 1.0.0
316 - *
317 - * @param array $cpts_to_associate Custom post types. Default is `elementor_library` post type.
318 - */
319 - $cpts_to_associate = apply_filters( 'elementor/template_library/sources/local/register_taxonomy_cpts', $cpts_to_associate );
320 -
321 - register_taxonomy( self::TAXONOMY_TYPE_SLUG, $cpts_to_associate, $args );
322 -
323 - /**
324 285 * Categories
325 286 */
326 287 $args = [
327 288 'hierarchical' => true,
@@ -331,11 +292,11 @@
331 292 'query_var' => is_admin(),
332 293 'rewrite' => false,
333 294 'public' => false,
334 295 'labels' => [
335 - 'name' => esc_html_x( 'Categories', 'Template Library', 'elementor' ),
336 - 'singular_name' => esc_html_x( 'Category', 'Template Library', 'elementor' ),
337 - 'all_items' => esc_html_x( 'All Categories', 'Template Library', 'elementor' ),
296 + 'name' => _x( 'Categories', 'Template Library', 'elementor' ),
297 + 'singular_name' => _x( 'Category', 'Template Library', 'elementor' ),
298 + 'all_items' => _x( 'All Categories', 'Template Library', 'elementor' ),
338 299 ],
339 300 ];
340 301
341 302 /**
@@ -351,14 +312,58 @@
351 312
352 313 register_taxonomy( self::TAXONOMY_CATEGORY_SLUG, self::CPT, $args );
353 314 }
354 315
316 + /**
317 + * Remove Add New item from admin menu.
318 + *
319 + * Fired by `admin_menu` action.
320 + *
321 + * @since 2.4.0
322 + * @access public
323 + */
324 + public function admin_menu_reorder() {
325 + global $submenu;
355 326
356 - private function register_editor_one_menu( Menu_Data_Provider $menu_data_provider ) {
357 - $menu_data_provider->register_menu( new Editor_One_Templates_Menu() );
358 - $menu_data_provider->register_menu( new Editor_One_Saved_Templates_Menu() );
327 + if ( ! isset( $submenu[ self::ADMIN_MENU_SLUG ] ) ) {
328 + return;
329 + }
330 + $library_submenu = &$submenu[ self::ADMIN_MENU_SLUG ];
331 +
332 + // Remove 'All Templates' menu.
333 + unset( $library_submenu[5] );
334 +
335 + // If current use can 'Add New' - move the menu to end, and add the '#add_new' anchor.
336 + if ( isset( $library_submenu[10][2] ) ) {
337 + $library_submenu[700] = $library_submenu[10];
338 + unset( $library_submenu[10] );
339 + $library_submenu[700][2] = admin_url( self::ADMIN_MENU_SLUG . '#add_new' );
340 + }
341 +
342 + // Move the 'Categories' menu to end.
343 + if ( isset( $library_submenu[15] ) ) {
344 + $library_submenu[800] = $library_submenu[15];
345 + unset( $library_submenu[15] );
346 + }
347 +
348 + if ( $this->is_current_screen() ) {
349 + $library_title = $this->get_library_title();
350 +
351 + foreach ( $library_submenu as &$item ) {
352 + if ( $library_title === $item[0] ) {
353 + if ( ! isset( $item[4] ) ) {
354 + $item[4] = '';
355 + }
356 + $item[4] .= ' current';
357 + }
358 + }
359 + }
359 360 }
360 361
362 + public function admin_menu() {
363 + add_submenu_page( self::ADMIN_MENU_SLUG, '', __( 'Saved Templates', 'elementor' ), Editor::EDITING_CAPABILITY, self::get_admin_url( true ) );
364 + }
365 +
361 366 public function admin_title( $admin_title, $title ) {
362 367 $library_title = $this->get_library_title();
363 368
364 369 if ( $library_title ) {
@@ -368,54 +373,15 @@
368 373 return $admin_title;
369 374 }
370 375
371 376 public function replace_admin_heading() {
372 - if ( ! $this->is_current_screen() ) {
373 - return;
374 - }
375 -
376 - global $post_type_object;
377 -
378 377 $library_title = $this->get_library_title();
379 378
380 379 if ( $library_title ) {
380 + global $post_type_object;
381 +
381 382 $post_type_object->labels->name = $library_title;
382 383 }
383 -
384 - $labels = $this->get_current_template_labels();
385 - $template_singular = $labels['singular'];
386 - $template_plural = $labels['plural'];
387 -
388 - if ( ! $template_plural && $library_title ) {
389 - $template_plural = $library_title;
390 - }
391 -
392 - if ( $template_singular ) {
393 - $labels = $post_type_object->labels;
394 - $labels->name = $template_plural;
395 - $labels->singular_name = $template_singular;
396 - /* translators: %s: Template label (plural). */
397 - $labels->all_items = sprintf( esc_html__( 'All %s', 'elementor' ), $template_plural );
398 - /* translators: %s: Template label (singular). */
399 - $labels->add_new = sprintf( esc_html__( 'Add New %s', 'elementor' ), $template_singular );
400 - $labels->add_new_item = $labels->add_new;
401 - /* translators: %s: Template label (singular). */
402 - $labels->edit_item = sprintf( esc_html__( 'Edit %s', 'elementor' ), $template_singular );
403 - /* translators: %s: Template label (singular). */
404 - $labels->new_item = sprintf( esc_html__( 'New %s', 'elementor' ), $template_singular );
405 - /* translators: %s: Template label (singular). */
406 - $labels->view_item = sprintf( esc_html__( 'View %s', 'elementor' ), $template_singular );
407 - /* translators: %s: Template label (plural). */
408 - $labels->search_items = sprintf( esc_html__( 'Search %s', 'elementor' ), $template_plural );
409 - /* translators: %s: Template label (plural). */
410 - $labels->not_found = sprintf( esc_html__( 'No %s found', 'elementor' ), $template_plural );
411 - /* translators: %s: Template label (plural). */
412 - $labels->not_found_in_trash = sprintf( esc_html__( 'No %s found in Trash', 'elementor' ), $template_plural );
413 - /* translators: %s: Template label (singular). */
414 - $labels->parent_item_colon = sprintf( esc_html__( 'Parent %s:', 'elementor' ), $template_singular );
415 - $labels->menu_name = $template_plural;
416 - $post_type_object->labels = $labels;
417 - }
418 384 }
419 385
420 386 /**
421 387 * Get local templates.
@@ -480,35 +446,25 @@
480 446 * @return \WP_Error|int The ID of the saved/updated template, `WP_Error` otherwise.
481 447 */
482 448 public function save_item( $template_data ) {
483 449 if ( ! current_user_can( $this->post_type_object->cap->edit_posts ) ) {
484 - return new \WP_Error( 'save_error', esc_html__( 'Access denied.', 'elementor' ) );
450 + return new \WP_Error( 'save_error', __( 'Access denied.', 'elementor' ) );
485 451 }
486 452
487 453 $defaults = [
488 - 'title' => esc_html__( '(no title)', 'elementor' ),
454 + 'title' => __( '(no title)', 'elementor' ),
489 455 'page_settings' => [],
456 + 'status' => current_user_can( 'publish_posts' ) ? 'publish' : 'pending',
490 457 ];
491 458
492 459 $template_data = wp_parse_args( $template_data, $defaults );
493 - $template_data['status'] = current_user_can( 'publish_posts' ) ? 'publish' : 'pending';
494 460
495 - // BC: Allow importing any template type when using CLI
496 - // to support users that rely on this mechanism.
497 - $should_check_template_type = ! $this->is_wp_cli();
498 -
499 - if (
500 - $should_check_template_type &&
501 - ! $this->is_valid_template_type( $template_data['type'] )
502 - ) {
503 - return new \WP_Error( 'invalid_template_type', esc_html__( 'Invalid template type.', 'elementor' ) );
504 - }
505 -
506 461 $document = Plugin::$instance->documents->create(
507 462 $template_data['type'],
508 463 [
509 464 'post_title' => $template_data['title'],
510 465 'post_status' => $template_data['status'],
466 + 'post_type' => self::CPT,
511 467 ]
512 468 );
513 469
514 470 if ( is_wp_error( $document ) ) {
@@ -555,35 +511,8 @@
555 511
556 512 return $template_id;
557 513 }
558 514
559 - protected function is_valid_template_type( $type ) {
560 - $document_class = Plugin::$instance->documents->get_document_type( $type, false );
561 -
562 - if ( ! $document_class ) {
563 - return false;
564 - }
565 -
566 - $cpt = $document_class::get_property( 'cpt' );
567 -
568 - if ( ! $cpt || ! is_array( $cpt ) || 1 !== count( $cpt ) ) {
569 - return false;
570 - }
571 -
572 - $is_valid_template_type = in_array( static::CPT, $cpt, true );
573 -
574 - return apply_filters(
575 - 'elementor/template_library/sources/local/is_valid_template_type',
576 - $is_valid_template_type,
577 - $cpt,
578 - );
579 - }
580 -
581 - /** For testing purposes only, in order to be able to mock the `WP_CLI` constant. */
582 - protected function is_wp_cli() {
583 - return Utils::is_wp_cli();
584 - }
585 -
586 515 /**
587 516 * Update local template.
588 517 *
589 518 * Update template on the database.
@@ -596,29 +525,21 @@
596 525 * @return \WP_Error|true True if template updated, `WP_Error` otherwise.
597 526 */
598 527 public function update_item( $new_data ) {
599 528 if ( ! current_user_can( $this->post_type_object->cap->edit_post, $new_data['id'] ) ) {
600 - return new \WP_Error( 'save_error', esc_html__( 'Access denied.', 'elementor' ) );
529 + return new \WP_Error( 'save_error', __( 'Access denied.', 'elementor' ) );
601 530 }
602 531
603 532 $document = Plugin::$instance->documents->get( $new_data['id'] );
604 533
605 534 if ( ! $document ) {
606 - return new \WP_Error( 'save_error', esc_html__( 'Template not exist.', 'elementor' ) );
535 + return new \WP_Error( 'save_error', __( 'Template not exist.', 'elementor' ) );
607 536 }
608 537
609 - $save_data = [];
538 + $document->save( [
539 + 'elements' => $new_data['content'],
540 + ] );
610 541
611 - if ( isset( $new_data['title'] ) ) {
612 - $save_data['post_title'] = $new_data['title'];
613 - }
614 -
615 - if ( isset( $new_data['content'] ) ) {
616 - $save_data['elements'] = $new_data['content'];
617 - }
618 -
619 - $document->save( $save_data );
620 -
621 542 /**
622 543 * After template library update.
623 544 *
624 545 * Fires after Elementor template library was updated.
@@ -700,38 +621,22 @@
700 621 *
701 622 * @return array Local template data.
702 623 */
703 624 public function get_data( array $args ) {
625 + $db = Plugin::$instance->db;
626 +
704 627 $template_id = $args['template_id'];
705 628
706 - $document = Plugin::$instance->documents->get( $template_id );
707 - $content = [];
629 + // TODO: Validate the data (in JS too!).
630 + if ( ! empty( $args['display'] ) ) {
631 + $content = $db->get_builder( $template_id );
632 + } else {
633 + $document = Plugin::$instance->documents->get( $template_id );
634 + $content = $document ? $document->get_elements_data() : [];
635 + }
708 636
709 - if ( $document ) {
710 - // TODO: Validate the data (in JS too!).
711 - if ( ! empty( $args['display'] ) ) {
712 - $content = $document->get_elements_raw_data( null, true );
713 - } else {
714 - $content = $document->get_elements_data();
715 - }
716 -
717 - if ( ! empty( $content ) ) {
718 - $content = $this->replace_elements_ids( $content );
719 -
720 - /**
721 - * Filters the template content data.
722 - *
723 - * This filter allows third-party plugins to modify template content
724 - * when loaded via get_template_data(), making the behavior consistent
725 - * with Frontend::get_builder_content().
726 - *
727 - * @since 3.34.0
728 - *
729 - * @param array $content The template content data.
730 - * @param int $template_id The template ID.
731 - */
732 - $content = apply_filters( 'elementor/frontend/builder_content_data', $content, $template_id );
733 - }
637 + if ( ! empty( $content ) ) {
638 + $content = $this->replace_elements_ids( $content );
734 639 }
735 640
736 641 $data = [
737 642 'content' => $content,
@@ -742,12 +647,8 @@
742 647
743 648 $data['page_settings'] = $page->get_data( 'settings' );
744 649 }
745 650
746 - if ( ! empty( $content ) ) {
747 - $this->attach_global_styles_to_data( $data, $content, $template_id );
748 - }
749 -
750 651 return $data;
751 652 }
752 653
753 654 /**
@@ -764,28 +665,14 @@
764 665 * or 'WP_Error' on failure.
765 666 */
766 667 public function delete_template( $template_id ) {
767 668 if ( ! current_user_can( $this->post_type_object->cap->delete_post, $template_id ) ) {
768 - return new \WP_Error( 'template_error', esc_html__( 'Access denied.', 'elementor' ) );
669 + return new \WP_Error( 'template_error', __( 'Access denied.', 'elementor' ) );
769 670 }
770 671
771 672 return wp_delete_post( $template_id, true );
772 673 }
773 674
774 - public function bulk_delete_items( array $template_ids ) {
775 - foreach ( $template_ids as $template_id ) {
776 - if ( ! current_user_can( $this->post_type_object->cap->delete_post, $template_id ) ) {
777 - return new \WP_Error( 'template_error', esc_html__( 'Access denied.', 'elementor' ) );
778 - }
779 - }
780 -
781 - foreach ( $template_ids as $template_id ) {
782 - wp_delete_post( $template_id, true );
783 - }
784 -
785 - return true;
786 - }
787 -
788 675 /**
789 676 * Export local template.
790 677 *
791 678 * Export template to a file.
@@ -797,14 +684,8 @@
797 684 *
798 685 * @return \WP_Error WordPress error if template export failed.
799 686 */
800 687 public function export_template( $template_id ) {
801 - $permissions_error = $this->validate_template_export_permissions( $template_id );
802 -
803 - if ( is_wp_error( $permissions_error ) ) {
804 - return $permissions_error;
805 - }
806 -
807 688 $file_data = $this->prepare_template_export( $template_id );
808 689
809 690 if ( is_wp_error( $file_data ) ) {
810 691 return $file_data;
@@ -811,29 +692,17 @@
811 692 }
812 693
813 694 $this->send_file_headers( $file_data['name'], strlen( $file_data['content'] ) );
814 695
815 - $this->serve_file( $file_data['content'] );
696 + // Clear buffering just in case.
697 + @ob_end_clean();
816 698
817 - die;
818 - }
699 + flush();
819 700
820 - private function validate_template_export_permissions( $template_id ) {
821 - $post_id = intval( $template_id );
822 - if ( get_post_type( $post_id ) !== self::CPT ) {
823 - return new \WP_Error( 'template_error', esc_html__( 'Invalid template type or template does not exist.', 'elementor' ) );
824 - }
701 + // Output file contents.
702 + echo $file_data['content'];
825 703
826 - $post_status = get_post_status( $post_id );
827 - if ( 'private' === $post_status && ! current_user_can( 'read_private_posts', $post_id ) ) {
828 - return new \WP_Error( 'template_error', esc_html__( 'You do not have permission to access this template.', 'elementor' ) );
829 - }
830 -
831 - if ( 'publish' !== $post_status && ! current_user_can( 'edit_post', $post_id ) ) {
832 - return new \WP_Error( 'template_error', esc_html__( 'You do not have permission to export this template.', 'elementor' ) );
833 - }
834 -
835 - return null;
704 + die;
836 705 }
837 706
838 707 /**
839 708 * Export multiple local templates.
@@ -901,12 +770,14 @@
901 770 foreach ( $files as $file ) {
902 771 unlink( $file['path'] );
903 772 }
904 773
905 - $this->send_file_headers( $zip_archive_filename, $this->filesize( $zip_complete_path ) );
774 + $this->send_file_headers( $zip_archive_filename, filesize( $zip_complete_path ) );
906 775
907 - $this->serve_zip( $zip_complete_path );
776 + @ob_end_flush();
908 777
778 + @readfile( $zip_complete_path );
779 +
909 780 unlink( $zip_complete_path );
910 781
911 782 die;
912 783 }
@@ -911,8 +782,46 @@
911 782 die;
912 783 }
913 784
914 785 /**
786 + * Find temporary files.
787 + *
788 + * Recursively finds a list of temporary files from the extracted zip file.
789 + *
790 + * Example return data:
791 + *
792 + * [
793 + * 0 => '/www/wp-content/uploads/elementor/tmp/5eb3a7a411d44/templates/block-2-col-marble-title.json',
794 + * 1 => '/www/wp-content/uploads/elementor/tmp/5eb3a7a411d44/templates/block-2-col-text-and-photo.json',
795 + * ]
796 + *
797 + * @since 2.9.8
798 + * @access private
799 + *
800 + * @param string $temp_path - The temporary file path to scan for template files
801 + *
802 + * @return array An array of temporary files on the filesystem
803 + */
804 + private function find_temp_files( $temp_path ) {
805 +
806 + $file_names = [];
807 +
808 + $possible_file_names = array_diff( scandir( $temp_path ), [ '.', '..' ] );
809 +
810 + // Find nested files in the unzipped path. This happens for example when the user imports a Template Kit.
811 + foreach ( $possible_file_names as $possible_file_name ) {
812 + $full_possible_file_name = $temp_path . '/' . $possible_file_name;
813 + if ( is_dir( $full_possible_file_name ) ) {
814 + $file_names = $file_names + $this->find_temp_files( $full_possible_file_name );
815 + } else {
816 + $file_names[] = $full_possible_file_name;
817 + }
818 + }
819 +
820 + return $file_names;
821 + }
822 +
823 + /**
915 824 * Import local template.
916 825 *
917 826 * Import template from a file.
918 827 *
@@ -918,54 +827,70 @@
918 827 *
919 828 * @since 1.0.0
920 829 * @access public
921 830 *
922 - * @param string $name - The file name.
923 - * @param string $path - The file path.
831 + * @param string $name - The file name
832 + * @param string $path - The file path
833 + *
924 834 * @return \WP_Error|array An array of items on success, 'WP_Error' on failure.
925 835 */
926 - public function import_template( $name, $path, $import_mode = 'match_site' ) {
836 + public function import_template( $name, $path ) {
927 837 if ( empty( $path ) ) {
928 838 return new \WP_Error( 'file_error', 'Please upload a file to import' );
929 839 }
930 840
931 - // Set the Request's state as an Elementor upload request, in order to support unfiltered file uploads.
932 - Plugin::$instance->uploads_manager->set_elementor_upload_state( true );
841 + $items = [];
933 842
934 - $items = [];
843 + $file_extension = pathinfo( $name, PATHINFO_EXTENSION );
935 844
936 - // If the import file is a Zip file with potentially multiple JSON files
937 - if ( 'zip' === pathinfo( $name, PATHINFO_EXTENSION ) ) {
938 - $extracted_files = Plugin::$instance->uploads_manager->extract_and_validate_zip( $path, [ 'json' ] );
845 + if ( 'zip' === $file_extension ) {
846 + if ( ! class_exists( '\ZipArchive' ) ) {
847 + return new \WP_Error( 'zip_error', 'PHP Zip extension not loaded' );
848 + }
939 849
940 - if ( is_wp_error( $extracted_files ) ) {
941 - // Delete the temporary extraction directory, since it's now not necessary.
942 - Plugin::$instance->uploads_manager->remove_file_or_dir( $extracted_files['extraction_directory'] );
850 + $zip = new \ZipArchive();
943 851
944 - return $extracted_files;
852 + $wp_upload_dir = wp_upload_dir();
853 +
854 + $temp_path = $wp_upload_dir['basedir'] . '/' . self::TEMP_FILES_DIR . '/' . uniqid();
855 +
856 + $zip->open( $path );
857 +
858 + $valid_entries = [];
859 +
860 + // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
861 + for ( $i = 0; $i < $zip->numFiles; $i++ ) {
862 + $zipped_file_name = $zip->getNameIndex( $i );
863 + $zipped_extension = pathinfo( $zipped_file_name, PATHINFO_EXTENSION );
864 + // Template Kit zip files contain a `manifest.json` file, this is not a valid Elementor template so ensure we skip it.
865 + if ( 'json' === $zipped_extension && 'manifest.json' !== $zipped_file_name ) {
866 + $valid_entries[] = $zipped_file_name;
867 + }
945 868 }
946 869
947 - foreach ( $extracted_files['files'] as $file_path ) {
948 - // Skip macOS metadata files and folders
949 - if ( false !== strpos( $file_path, '__MACOSX' ) || '.' === basename( $file_path )[0] ) {
950 - continue;
951 - }
870 + if ( ! empty( $valid_entries ) ) {
871 + $zip->extractTo( $temp_path, $valid_entries );
872 + }
952 873
953 - $import_result = $this->import_single_template( $file_path, $import_mode );
874 + $zip->close();
954 875
876 + $file_names = $this->find_temp_files( $temp_path );
877 +
878 + foreach ( $file_names as $full_file_name ) {
879 + $import_result = $this->import_single_template( $full_file_name );
880 +
881 + unlink( $full_file_name );
882 +
955 883 if ( is_wp_error( $import_result ) ) {
956 - // Skip failed files
957 - continue;
884 + return $import_result;
958 885 }
959 886
960 887 $items[] = $import_result;
961 888 }
962 889
963 - // Delete the temporary extraction directory, since it's now not necessary.
964 - Plugin::$instance->uploads_manager->remove_file_or_dir( $extracted_files['extraction_directory'] );
890 + rmdir( $temp_path );
965 891 } else {
966 - // If the import file is a single JSON file
967 - $import_result = $this->import_single_template( $path, $import_mode );
892 + $import_result = $this->import_single_template( $path );
968 893
969 894 if ( is_wp_error( $import_result ) ) {
970 895 return $import_result;
971 896 }
@@ -993,17 +918,9 @@
993 918 */
994 919 public function post_row_actions( $actions, \WP_Post $post ) {
995 920 if ( self::is_base_templates_screen() ) {
996 921 if ( $this->is_template_supports_export( $post->ID ) ) {
997 - $template_labels = $this->get_template_labels_by_type( self::get_template_type( $post->ID ) );
998 - $export_label = esc_html__( 'Export Template', 'elementor' );
999 -
1000 - if ( $template_labels['singular'] ) {
1001 - /* translators: %s: Template label (singular). */
1002 - $export_label = sprintf( esc_html__( 'Export %s', 'elementor' ), $template_labels['singular'] );
1003 - }
1004 -
1005 - $actions['export-template'] = sprintf( '<a href="%1$s">%2$s</a>', $this->get_export_link( $post->ID ), $export_label );
922 + $actions['export-template'] = sprintf( '<a href="%1$s">%2$s</a>', $this->get_export_link( $post->ID ), __( 'Export Template', 'elementor' ) );
1006 923 }
1007 924 }
1008 925
1009 926 return $actions;
@@ -1021,43 +938,26 @@
1021 938 * @since 1.0.0
1022 939 * @access public
1023 940 */
1024 941 public function admin_import_template_form() {
1025 - if ( ! self::is_base_templates_screen() || ! User::is_current_user_can_upload_json() ) {
942 + if ( ! self::is_base_templates_screen() ) {
1026 943 return;
1027 944 }
1028 945
1029 946 /** @var \Elementor\Core\Common\Modules\Ajax\Module $ajax */
1030 947 $ajax = Plugin::$instance->common->get_component( 'ajax' );
1031 - $template_labels = $this->get_current_template_labels();
1032 - $import_label = esc_html__( 'Import Templates', 'elementor' );
1033 - $description = esc_html__( 'Choose an Elementor template JSON file or a .zip archive of Elementor templates, and add them to the list of templates available in your library.', 'elementor' );
1034 -
1035 - if ( $template_labels['plural'] ) {
1036 - /* translators: %s: Template label (plural). */
1037 - $import_label = sprintf( esc_html__( 'Import %s', 'elementor' ), $template_labels['plural'] );
1038 - }
1039 -
1040 - if ( $template_labels['singular'] && $template_labels['plural'] ) {
1041 - $description = sprintf(
1042 - /* translators: 1: Template label (singular). 2: Template label (plural). */
1043 - esc_html__( 'Choose an Elementor %1$s JSON file or a .zip archive of Elementor %2$s, and add them to the list of %2$s available in your library.', 'elementor' ),
1044 - $template_labels['singular'],
1045 - $template_labels['plural']
1046 - );
1047 - }
1048 948 ?>
1049 949 <div id="elementor-hidden-area">
1050 - <a id="elementor-import-template-trigger" class="page-title-action button button-secondary"><?php echo esc_html( $import_label ); ?></a>
950 + <a id="elementor-import-template-trigger" class="page-title-action"><?php echo __( 'Import Templates', 'elementor' ); ?></a>
1051 951 <div id="elementor-import-template-area">
1052 - <div id="elementor-import-template-title"><?php echo esc_html( $description ); ?></div>
1053 - <form id="elementor-import-template-form" method="post" action="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>" enctype="multipart/form-data">
952 + <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>
953 + <form id="elementor-import-template-form" method="post" action="<?php echo admin_url( 'admin-ajax.php' ); ?>" enctype="multipart/form-data">
1054 954 <input type="hidden" name="action" value="elementor_library_direct_actions">
1055 955 <input type="hidden" name="library_action" value="direct_import_template">
1056 - <input type="hidden" name="_nonce" value="<?php Utils::print_unescaped_internal_string( $ajax->create_nonce() ); ?>">
956 + <input type="hidden" name="_nonce" value="<?php echo $ajax->create_nonce(); ?>">
1057 957 <fieldset id="elementor-import-template-form-inputs">
1058 958 <input type="file" name="file" accept=".json,application/json,.zip,application/octet-stream,application/zip,application/x-zip,application/x-zip-compressed" required>
1059 - <input id="e-import-template-action" type="submit" class="button" value="<?php echo esc_attr__( 'Import Now', 'elementor' ); ?>">
959 + <input type="submit" class="button" value="<?php echo esc_attr__( 'Import Now', 'elementor' ); ?>">
1060 960 </fieldset>
1061 961 </form>
1062 962 </div>
1063 963 </div>
@@ -1081,27 +981,12 @@
1081 981 die;
1082 982 }
1083 983 }
1084 984
1085 - public function redirect_categories_page_to_saved_templates_page() {
1086 - // phpcs:ignore WordPress.Security.NonceVerification.Recommended
1087 - $taxonomy = sanitize_key( wp_unslash( $_GET['taxonomy'] ?? '' ) );
1088 - // phpcs:ignore WordPress.Security.NonceVerification.Recommended
1089 - $post_type = sanitize_key( wp_unslash( $_GET['post_type'] ?? '' ) );
1090 - $is_categories_page = 'edit-tags.php' === $GLOBALS['pagenow']
1091 - && self::TAXONOMY_CATEGORY_SLUG === $taxonomy
1092 - && self::CPT === $post_type;
1093 -
1094 - if ( $is_categories_page ) {
1095 - wp_safe_redirect( admin_url( 'edit.php?post_type=' . self::CPT . '&tabs_group=library' ) );
1096 - die;
1097 - }
1098 - }
1099 -
1100 985 /**
1101 986 * Is template library supports export.
1102 987 *
1103 - * Whether the template library supports export.
988 + * whether the template library supports export.
1104 989 *
1105 990 * Template saved by the user locally on his site, support export by default
1106 991 * but this can be changed using a filter.
1107 992 *
@@ -1248,9 +1133,9 @@
1248 1133 *
1249 1134 * @return array An array of the available bulk actions.
1250 1135 */
1251 1136 public function admin_add_bulk_export_action( $actions ) {
1252 - $actions[ self::BULK_EXPORT_ACTION ] = esc_html__( 'Export', 'elementor' );
1137 + $actions[ self::BULK_EXPORT_ACTION ] = __( 'Export', 'elementor' );
1253 1138
1254 1139 return $actions;
1255 1140 }
1256 1141
@@ -1266,10 +1151,8 @@
1266 1151 *
1267 1152 * @param string $redirect_to The redirect URL.
1268 1153 * @param string $action The action being taken.
1269 1154 * @param array $post_ids The items to take the action on.
1270 - *
1271 - * @return string The redirect URL, unchanged when the action is not handled here.
1272 1155 */
1273 1156 public function admin_export_multiple_templates( $redirect_to, $action, $post_ids ) {
1274 1157 if ( self::BULK_EXPORT_ACTION === $action ) {
1275 1158 $result = $this->export_multiple_templates( $post_ids );
@@ -1274,13 +1157,10 @@
1274 1157 if ( self::BULK_EXPORT_ACTION === $action ) {
1275 1158 $result = $this->export_multiple_templates( $post_ids );
1276 1159
1277 1160 // If you reach this line, the export failed
1278 - // PHPCS - Not user input.
1279 - wp_die( $result->get_error_message() ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1161 + wp_die( $result->get_error_message() );
1280 1162 }
1281 -
1282 - return $redirect_to;
1283 1163 }
1284 1164
1285 1165 /**
1286 1166 * Print admin tabs.
@@ -1296,13 +1176,17 @@
1296 1176 *
1297 1177 * @return array An updated array of available list table views.
1298 1178 */
1299 1179 public function admin_print_tabs( $views ) {
1300 - //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce is not required to retrieve the value.
1301 - $current_type = Utils::get_super_global_value( $_REQUEST, self::TAXONOMY_TYPE_SLUG ) ?? '';
1302 - $active_class = $current_type ? '' : ' nav-tab-active';
1180 + $current_type = '';
1181 + $active_class = ' nav-tab-active';
1303 1182 $current_tabs_group = $this->get_current_tab_group();
1304 1183
1184 + if ( ! empty( $_REQUEST[ self::TAXONOMY_TYPE_SLUG ] ) ) {
1185 + $current_type = $_REQUEST[ self::TAXONOMY_TYPE_SLUG ];
1186 + $active_class = '';
1187 + }
1188 +
1305 1189 $url_args = [
1306 1190 'post_type' => self::CPT,
1307 1191 'tabs_group' => $current_tabs_group,
1308 1192 ];
@@ -1326,15 +1210,15 @@
1326 1210 }
1327 1211
1328 1212 ?>
1329 1213 <div id="elementor-template-library-tabs-wrapper" class="nav-tab-wrapper">
1330 - <a class="nav-tab<?php echo esc_attr( $active_class ); ?>" href="<?php echo esc_url( $baseurl ); ?>">
1214 + <a class="nav-tab<?php echo $active_class; ?>" href="<?php echo $baseurl; ?>">
1331 1215 <?php
1332 1216 $all_title = $this->get_library_title();
1333 1217 if ( ! $all_title ) {
1334 - $all_title = esc_html__( 'All', 'elementor' );
1218 + $all_title = __( 'All', 'elementor' );
1335 1219 }
1336 - Utils::print_unescaped_internal_string( $all_title ); ?>
1220 + echo $all_title; ?>
1337 1221 </a>
1338 1222 <?php
1339 1223 foreach ( $doc_types as $type => $class_name ) :
1340 1224 $active_class = '';
@@ -1342,11 +1226,12 @@
1342 1226 if ( $current_type === $type ) {
1343 1227 $active_class = ' nav-tab-active';
1344 1228 }
1345 1229
1346 - $type_url = esc_url( add_query_arg( self::TAXONOMY_TYPE_SLUG, $type, $baseurl ) );
1230 + $type_url = add_query_arg( self::TAXONOMY_TYPE_SLUG, $type, $baseurl );
1347 1231 $type_label = $this->get_template_label_by_type( $type );
1348 - Utils::print_unescaped_internal_string( "<a class='nav-tab{$active_class}' href='{$type_url}'>{$type_label}</a>" );
1232 +
1233 + echo "<a class='nav-tab{$active_class}' href='{$type_url}'>{$type_label}</a>";
1349 1234 endforeach;
1350 1235 ?>
1351 1236 </div>
1352 1237 <?php
@@ -1364,19 +1249,13 @@
1364 1249 * @since 2.0.0
1365 1250 * @access public
1366 1251 *
1367 1252 * @param string $which The location of the extra table nav markup: 'top' or 'bottom'.
1368 - * @param array $args
1369 1253 */
1370 - public function maybe_render_blank_state( $which, array $args = [] ) {
1254 + public function maybe_render_blank_state( $which ) {
1371 1255 global $post_type;
1372 1256
1373 - $args = wp_parse_args( $args, [
1374 - 'cpt' => self::CPT,
1375 - 'post_type' => get_query_var( 'elementor_library_type' ),
1376 - ] );
1377 -
1378 - if ( $args['cpt'] !== $post_type || 'bottom' !== $which ) {
1257 + if ( self::CPT !== $post_type || 'bottom' !== $which ) {
1379 1258 return;
1380 1259 }
1381 1260
1382 1261 global $wp_list_table;
@@ -1382,15 +1261,16 @@
1382 1261 global $wp_list_table;
1383 1262
1384 1263 $total_items = $wp_list_table->get_pagination_arg( 'total_items' );
1385 1264
1386 - //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce is not required to retrieve the value.
1387 1265 if ( ! empty( $total_items ) || ! empty( $_REQUEST['s'] ) ) {
1388 1266 return;
1389 1267 }
1390 1268
1391 - $current_type = $args['post_type'];
1269 + $inline_style = '#posts-filter .wp-list-table, #posts-filter .tablenav.top, .tablenav.bottom .actions, .wrap .subsubsub { display:none;}';
1392 1270
1271 + $current_type = get_query_var( 'elementor_library_type' );
1272 +
1393 1273 $document_types = Plugin::instance()->documents->get_document_types();
1394 1274
1395 1275 if ( empty( $document_types[ $current_type ] ) ) {
1396 1276 return;
@@ -1400,13 +1280,12 @@
1400 1280 if ( 'widget' === $current_type ) {
1401 1281 return;
1402 1282 }
1403 1283
1404 - // TODO: This code maybe unreachable see if above `if ( empty( $document_types[ $current_type ] ) )`.
1405 1284 if ( empty( $current_type ) ) {
1406 1285 $counts = (array) wp_count_posts( self::CPT );
1407 1286 unset( $counts['auto-draft'] );
1408 - $count = array_sum( $counts );
1287 + $count = array_sum( $counts );
1409 1288
1410 1289 if ( 0 < $count ) {
1411 1290 return;
1412 1291 }
@@ -1412,87 +1291,35 @@
1412 1291 }
1413 1292
1414 1293 $current_type = 'template';
1415 1294
1416 - $args['additional_inline_style'] = '#elementor-template-library-tabs-wrapper {display: none;}';
1295 + $inline_style .= '#elementor-template-library-tabs-wrapper {display: none;}';
1417 1296 }
1418 1297
1419 - $this->render_blank_state( $current_type, $args );
1420 - }
1421 -
1422 - private function render_blank_state( $current_type, array $args = [] ) {
1423 1298 $current_type_label = $this->get_template_label_by_type( $current_type );
1424 - $type_labels = $this->get_template_labels_by_type( $current_type );
1425 - $description = esc_html__( 'Add templates and reuse them across your website. Easily export and import them to any other project, for an optimized workflow.', 'elementor' );
1426 -
1427 - if ( $type_labels['plural'] ) {
1428 - $description = sprintf(
1429 - /* translators: %s: Template label (plural). */
1430 - esc_html__( 'Add %s and reuse them across your website. Easily export and import them to any other project, for an optimized workflow.', 'elementor' ),
1431 - $type_labels['plural']
1432 - );
1433 - }
1434 - $inline_style = '#posts-filter .wp-list-table, #posts-filter .tablenav.top, .tablenav.bottom .actions, .wrap .subsubsub { display:none;}';
1435 -
1436 - $args = wp_parse_args( $args, [
1437 - 'additional_inline_style' => '',
1438 - 'href' => '',
1439 - 'description' => $description,
1440 - ] );
1441 - $inline_style .= $args['additional_inline_style'];
1442 1299 ?>
1443 - <style type="text/css"><?php Utils::print_unescaped_internal_string( $inline_style ); ?></style>
1300 + <style type="text/css"><?php echo $inline_style; ?></style>
1444 1301 <div class="elementor-template_library-blank_state">
1445 - <?php $this->print_blank_state_template( $current_type_label, $args['href'], $args['description'] ); ?>
1446 - </div>
1447 - <?php
1448 - }
1449 -
1450 - /**
1451 - * Print Blank State Template
1452 - *
1453 - * When the an entity (CPT, Taxonomy...etc) has no saved items, print a blank admin page offering
1454 - * to create the very first item.
1455 - *
1456 - * This method is public because it needs to be accessed from outside the Source_Local
1457 - *
1458 - * @since 3.1.0
1459 - * @access public
1460 - *
1461 - * @param string $current_type_label The Entity title.
1462 - * @param string $href The URL for the 'Add New' button.
1463 - * @param string $description The sub title describing the Entity (Post Type, Taxonomy, etc.).
1464 - */
1465 - public function print_blank_state_template( $current_type_label, $href, $description ) {
1466 - ?>
1467 1302 <div class="elementor-blank_state">
1468 1303 <i class="eicon-folder"></i>
1469 - <h3>
1304 + <h2>
1470 1305 <?php
1471 1306 /* translators: %s: Template type label. */
1472 - printf( esc_html__( 'Create Your First %s', 'elementor' ), esc_html( $current_type_label ) );
1307 + printf( __( 'Create Your First %s', 'elementor' ), $current_type_label );
1473 1308 ?>
1474 - </h3>
1475 - <p><?php echo wp_kses_post( $description ); ?></p>
1476 - <a id="elementor-template-library-add-new" class="elementor-button e-primary" href="<?php echo esc_url( $href ); ?>">
1309 + </h2>
1310 + <p><?php echo __( 'Add templates and reuse them across your website. Easily export and import them to any other project, for an optimized workflow.', 'elementor' ); ?></p>
1311 + <a id="elementor-template-library-add-new" class="elementor-button elementor-button-success" href="<?php esc_url( Utils::get_pro_link( 'https://elementor.com/pro/?utm_source=wp-custom-fonts&utm_campaign=gopro&utm_medium=wp-dash' ) ); ?>">
1477 1312 <?php
1478 1313 /* translators: %s: Template type label. */
1479 - printf( esc_html__( 'Add New %s', 'elementor' ), esc_html( $current_type_label ) );
1314 + printf( __( 'Add New %s', 'elementor' ), $current_type_label );
1480 1315 ?>
1481 1316 </a>
1482 1317 </div>
1318 + </div>
1483 1319 <?php
1484 1320 }
1485 1321
1486 - /**
1487 - * Add filter by category.
1488 - *
1489 - * In the templates library, add a filter by Elementor library category.
1490 - *
1491 - * @access public
1492 - *
1493 - * @param string $post_type The post type slug.
1494 - */
1495 1322 public function add_filter_by_category( $post_type ) {
1496 1323 if ( self::CPT !== $post_type ) {
1497 1324 return;
1498 1325 }
@@ -1497,9 +1324,10 @@
1497 1324 return;
1498 1325 }
1499 1326
1500 1327 $all_items = get_taxonomy( self::TAXONOMY_CATEGORY_SLUG )->labels->all_items;
1501 - $dropdown_options = [
1328 +
1329 + $dropdown_options = array(
1502 1330 'show_option_all' => $all_items,
1503 1331 'show_option_none' => $all_items,
1504 1332 'hide_empty' => 0,
1505 1333 'hierarchical' => 1,
@@ -1507,17 +1335,12 @@
1507 1335 'orderby' => 'name',
1508 1336 'value_field' => 'slug',
1509 1337 'taxonomy' => self::TAXONOMY_CATEGORY_SLUG,
1510 1338 'name' => self::TAXONOMY_CATEGORY_SLUG,
1511 - //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce is not required to retrieve the value.
1512 - 'selected' => Utils::get_super_global_value( $_GET, self::TAXONOMY_CATEGORY_SLUG ) ?? '',
1513 - ];
1339 + 'selected' => empty( $_GET[ self::TAXONOMY_CATEGORY_SLUG ] ) ? '' : $_GET[ self::TAXONOMY_CATEGORY_SLUG ],
1340 + );
1514 1341
1515 - printf(
1516 - '<label class="screen-reader-text" for="%1$s">%2$s</label>',
1517 - esc_attr( self::TAXONOMY_CATEGORY_SLUG ),
1518 - esc_html_x( 'Filter by category', 'Template Library', 'elementor' )
1519 - );
1342 + echo '<label class="screen-reader-text" for="cat">' . _x( 'Filter by category', 'Template Library', 'elementor' ) . '</label>';
1520 1343 wp_dropdown_categories( $dropdown_options );
1521 1344 }
1522 1345
1523 1346 /**
@@ -1527,29 +1350,48 @@
1527 1350 *
1528 1351 * @since 1.6.0
1529 1352 * @access private
1530 1353 *
1531 - * @param string $file_path File name.
1354 + * @param string $file_name File name.
1532 1355 *
1533 1356 * @return \WP_Error|int|array Local template array, or template ID, or
1534 1357 * `WP_Error`.
1535 1358 */
1536 - private function import_single_template( $file_path, $import_mode = 'match_site' ) {
1537 - $data = $this->prepare_import_template_data( $file_path, $import_mode );
1359 + private function import_single_template( $file_name ) {
1360 + $data = json_decode( file_get_contents( $file_name ), true );
1538 1361
1539 - if ( is_wp_error( $data ) ) {
1540 - return $data;
1362 + if ( empty( $data ) ) {
1363 + return new \WP_Error( 'file_error', 'Invalid File' );
1541 1364 }
1542 1365
1543 - if ( empty( $data ) ) {
1544 - return new \WP_Error( 'file_error', 'Invalid File' );
1366 + $content = $data['content'];
1367 +
1368 + if ( ! is_array( $content ) ) {
1369 + return new \WP_Error( 'file_error', 'Invalid Content In File' );
1545 1370 }
1546 1371
1372 + $content = $this->process_export_import_content( $content, 'on_import' );
1373 +
1374 + $page_settings = [];
1375 +
1376 + if ( ! empty( $data['page_settings'] ) ) {
1377 + $page = new Model( [
1378 + 'id' => 0,
1379 + 'settings' => $data['page_settings'],
1380 + ] );
1381 +
1382 + $page_settings_data = $this->process_element_export_import_content( $page, 'on_import' );
1383 +
1384 + if ( ! empty( $page_settings_data['settings'] ) ) {
1385 + $page_settings = $page_settings_data['settings'];
1386 + }
1387 + }
1388 +
1547 1389 $template_id = $this->save_item( [
1548 - 'content' => $data['content'],
1390 + 'content' => $content,
1549 1391 'title' => $data['title'],
1550 1392 'type' => $data['type'],
1551 - 'page_settings' => $data['page_settings'],
1393 + 'page_settings' => $page_settings,
1552 1394 ] );
1553 1395
1554 1396 if ( is_wp_error( $template_id ) ) {
1555 1397 return $template_id;
@@ -1570,30 +1412,35 @@
1570 1412 *
1571 1413 * @return \WP_Error|array Exported template data.
1572 1414 */
1573 1415 private function prepare_template_export( $template_id ) {
1574 - $document = Plugin::$instance->documents->get( $template_id );
1416 + $template_data = $this->get_data( [
1417 + 'template_id' => $template_id,
1418 + ] );
1575 1419
1576 - $template_data = $document->get_export_data();
1577 -
1578 1420 if ( empty( $template_data['content'] ) ) {
1579 1421 return new \WP_Error( 'empty_template', 'The template is empty' );
1580 1422 }
1581 1423
1582 - $content = apply_filters(
1583 - 'elementor/template_library/sources/local/export/elements',
1584 - $template_data['content']
1585 - );
1424 + $template_data['content'] = $this->process_export_import_content( $template_data['content'], 'on_export' );
1586 1425
1426 + if ( get_post_meta( $template_id, '_elementor_page_settings', true ) ) {
1427 + $page = SettingsManager::get_settings_managers( 'page' )->get_model( $template_id );
1428 +
1429 + $page_settings_data = $this->process_element_export_import_content( $page, 'on_export' );
1430 +
1431 + if ( ! empty( $page_settings_data['settings'] ) ) {
1432 + $template_data['page_settings'] = $page_settings_data['settings'];
1433 + }
1434 + }
1435 +
1587 1436 $export_data = [
1588 - 'content' => $content,
1589 - 'page_settings' => $template_data['settings'],
1590 1437 'version' => DB::DB_VERSION,
1591 - 'title' => $document->get_main_post()->post_title,
1438 + 'title' => get_the_title( $template_id ),
1592 1439 'type' => self::get_template_type( $template_id ),
1593 1440 ];
1594 1441
1595 - $this->attach_global_styles_to_data( $export_data, $content, $template_id );
1442 + $export_data += $template_data;
1596 1443
1597 1444 return [
1598 1445 'name' => 'elementor-' . $template_id . '-' . gmdate( 'Y-m-d' ) . '.json',
1599 1446 'content' => wp_json_encode( $export_data ),
@@ -1600,8 +1447,28 @@
1600 1447 ];
1601 1448 }
1602 1449
1603 1450 /**
1451 + * Send file headers.
1452 + *
1453 + * Set the file header when export template data to a file.
1454 + *
1455 + * @since 1.6.0
1456 + * @access private
1457 + *
1458 + * @param string $file_name File name.
1459 + * @param int $file_size File size.
1460 + */
1461 + private function send_file_headers( $file_name, $file_size ) {
1462 + header( 'Content-Type: application/octet-stream' );
1463 + header( 'Content-Disposition: attachment; filename=' . $file_name );
1464 + header( 'Expires: 0' );
1465 + header( 'Cache-Control: must-revalidate' );
1466 + header( 'Pragma: public' );
1467 + header( 'Content-Length: ' . $file_size );
1468 + }
1469 +
1470 + /**
1604 1471 * Get template label by type.
1605 1472 *
1606 1473 * Retrieve the template label for any given template type.
1607 1474 *
@@ -1635,68 +1502,8 @@
1635 1502
1636 1503 return $template_label;
1637 1504 }
1638 1505
1639 - private function get_template_labels_by_type( $template_type ) {
1640 - $labels = [
1641 - 'singular' => '',
1642 - 'plural' => '',
1643 - ];
1644 -
1645 - if ( ! $template_type ) {
1646 - return $labels;
1647 - }
1648 -
1649 - $document_types = Plugin::instance()->documents->get_document_types();
1650 -
1651 - if ( isset( $document_types[ $template_type ] ) ) {
1652 - $doc_type = $document_types[ $template_type ];
1653 - $labels['singular'] = call_user_func( [ $doc_type, 'get_title' ] );
1654 - $labels['plural'] = call_user_func( [ $doc_type, 'get_plural_title' ] );
1655 - } else {
1656 - $labels['singular'] = $this->get_template_label_by_type( $template_type );
1657 - }
1658 -
1659 - if ( ! $labels['plural'] ) {
1660 - $labels['plural'] = $labels['singular'];
1661 - }
1662 -
1663 - return $labels;
1664 - }
1665 -
1666 - private function get_current_template_labels() {
1667 - $labels = [
1668 - 'singular' => '',
1669 - 'plural' => '',
1670 - ];
1671 -
1672 - $template_type = Utils::get_super_global_value( $_GET, self::TAXONOMY_TYPE_SLUG );
1673 -
1674 - if ( $template_type ) {
1675 - return $this->get_template_labels_by_type( $template_type );
1676 - }
1677 -
1678 - $current_tabs_group = $this->get_current_tab_group();
1679 -
1680 - if ( $current_tabs_group ) {
1681 - $doc_types = Plugin::$instance->documents->get_document_types( [
1682 - 'admin_tab_group' => $current_tabs_group,
1683 - ], 'and' );
1684 -
1685 - if ( 1 === count( $doc_types ) ) {
1686 - $doc_type = reset( $doc_types );
1687 - $labels['singular'] = call_user_func( [ $doc_type, 'get_title' ] );
1688 - $labels['plural'] = call_user_func( [ $doc_type, 'get_plural_title' ] );
1689 - }
1690 - }
1691 -
1692 - if ( ! $labels['plural'] ) {
1693 - $labels['plural'] = $labels['singular'];
1694 - }
1695 -
1696 - return $labels;
1697 - }
1698 -
1699 1506 /**
1700 1507 * Filter template types in admin query.
1701 1508 *
1702 1509 * Update the template types in the main admin query.
@@ -1740,32 +1547,10 @@
1740 1547 * @access private
1741 1548 */
1742 1549 private function add_actions() {
1743 1550 if ( is_admin() ) {
1744 - add_action( 'admin_init', [ $this, 'redirect_categories_page_to_saved_templates_page' ] );
1745 -
1746 - add_action( 'elementor/editor-one/menu/register', function ( Menu_Data_Provider $menu_data_provider ) {
1747 - $this->register_editor_one_menu( $menu_data_provider );
1748 - } );
1749 -
1750 - add_action( 'elementor/editor-one/menu/excluded_level4_slugs', function ( array $excluded_slugs ): array {
1751 - $excluded_slugs[] = 'edit.php?post_type=elementor_library#add_new';
1752 - $excluded_slugs[] = 'edit-tags.php?taxonomy=elementor_library_category&amp;post_type=elementor_library';
1753 - return $excluded_slugs;
1754 - } );
1755 -
1756 - add_filter( 'elementor/editor-one/menu/elementor_post_types', function ( array $elementor_post_types ): array {
1757 - $elementor_post_types[ self::CPT ] = [];
1758 -
1759 - return $elementor_post_types;
1760 - } );
1761 -
1762 - add_filter( 'elementor/editor-one/admin-edit-post-types', function ( array $post_types ): array {
1763 - $post_types[] = self::CPT;
1764 -
1765 - return $post_types;
1766 - } );
1767 -
1551 + add_action( 'admin_menu', [ $this, 'admin_menu' ] );
1552 + add_action( 'admin_menu', [ $this, 'admin_menu_reorder' ], 800 );
1768 1553 add_filter( 'admin_title', [ $this, 'admin_title' ], 10, 2 );
1769 1554 add_action( 'all_admin_notices', [ $this, 'replace_admin_heading' ] );
1770 1555 add_filter( 'post_row_actions', [ $this, 'post_row_actions' ], 10, 2 );
1771 1556 add_action( 'admin_footer', [ $this, 'admin_import_template_form' ] );
@@ -1791,30 +1576,11 @@
1791 1576 // Show blank state.
1792 1577 add_action( 'manage_posts_extra_tablenav', [ $this, 'maybe_render_blank_state' ] );
1793 1578 }
1794 1579
1795 - add_action( 'elementor/document/after_save', [ $this, 'on_template_update' ], 10, 2 );
1796 -
1797 1580 add_action( 'template_redirect', [ $this, 'block_template_frontend' ] );
1798 -
1799 - // Remove elementor library templates from WP Sitemap
1800 - add_filter(
1801 - 'wp_sitemaps_post_types',
1802 - function( $post_types ) {
1803 - return $this->remove_elementor_cpt_from_sitemap( $post_types );
1804 - }
1805 - );
1806 1581 }
1807 1582
1808 - public function on_template_update( \Elementor\Core\Base\Document $document, array $data ) {
1809 - if ( ! empty( $data['post_title'] ) ) {
1810 - wp_update_post( [
1811 - 'ID' => $document->get_main_id(),
1812 - 'post_title' => $data['post_title'],
1813 - ] );
1814 - }
1815 - }
1816 -
1817 1583 /**
1818 1584 * @since 2.0.6
1819 1585 * @access public
1820 1586 */
@@ -1839,24 +1605,26 @@
1839 1605
1840 1606 $offset = 2;
1841 1607
1842 1608 $posts_columns = array_slice( $posts_columns, 0, $offset, true ) + [
1843 - 'elementor_library_type' => esc_html__( 'Type', 'elementor' ),
1609 + 'elementor_library_type' => __( 'Type', 'elementor' ),
1844 1610 ] + array_slice( $posts_columns, $offset, null, true );
1845 1611
1846 1612 return $posts_columns;
1847 1613 }
1848 1614
1849 - public function get_current_tab_group() {
1850 - $current_tabs_group = Utils::get_super_global_value( $_GET, 'tabs_group' ) ?? '';
1851 - $type_slug = Utils::get_super_global_value( $_GET, self::TAXONOMY_TYPE_SLUG );
1615 + public function get_current_tab_group( $default = '' ) {
1616 + $current_tabs_group = $default;
1852 1617
1853 - if ( $type_slug ) {
1854 - $doc_type = Plugin::$instance->documents->get_document_type( $type_slug, '' );
1618 + if ( ! empty( $_REQUEST[ self::TAXONOMY_TYPE_SLUG ] ) ) {
1619 + $doc_type = Plugin::$instance->documents->get_document_type( $_REQUEST[ self::TAXONOMY_TYPE_SLUG ], '' );
1855 1620 if ( $doc_type ) {
1856 1621 $current_tabs_group = $doc_type::get_property( 'admin_tab_group' );
1857 1622 }
1623 + } elseif ( ! empty( $_REQUEST['tabs_group'] ) ) {
1624 + $current_tabs_group = $_REQUEST['tabs_group'];
1858 1625 }
1626 +
1859 1627 return $current_tabs_group;
1860 1628 }
1861 1629
1862 1630 private function get_library_title() {
@@ -1866,11 +1634,11 @@
1866 1634 $current_tab_group = $this->get_current_tab_group();
1867 1635
1868 1636 if ( $current_tab_group ) {
1869 1637 $titles = [
1870 - 'library' => esc_html__( 'Saved Templates', 'elementor' ),
1871 - 'theme' => esc_html__( 'Theme Builder', 'elementor' ),
1872 - 'popup' => esc_html__( 'Popups', 'elementor' ),
1638 + 'library' => __( 'Saved Templates', 'elementor' ),
1639 + 'theme' => __( 'Theme Builder', 'elementor' ),
1640 + 'popup' => __( 'Popups', 'elementor' ),
1873 1641 ];
1874 1642
1875 1643 if ( ! empty( $titles[ $current_tab_group ] ) ) {
1876 1644 $title = $titles[ $current_tab_group ];
@@ -1887,46 +1655,8 @@
1887 1655 return 'edit.php' === $pagenow && self::CPT === $typenow;
1888 1656 }
1889 1657
1890 1658 /**
1891 - * @param array $post_types
1892 - *
1893 - * @return array
1894 - */
1895 - private function remove_elementor_cpt_from_sitemap( array $post_types ) {
1896 - unset( $post_types[ self::CPT ] );
1897 -
1898 - return $post_types;
1899 - }
1900 -
1901 - private function avoid_rest_access_for_non_admins(): void {
1902 - add_filter( 'rest_pre_dispatch', function ( $value, \WP_REST_Server $server, \WP_REST_Request $request ) {
1903 - if ( strpos( $request->get_route(), self::CPT ) !== false ) {
1904 - if ( ! current_user_can( 'manage_options' ) ) {
1905 - return new \WP_Error( 'rest_forbidden', esc_html__( 'Sorry, you are not allowed to do that.', 'elementor' ), [ 'status' => rest_authorization_required_code() ] );
1906 - }
1907 - }
1908 -
1909 - return $value;
1910 - }, 10, 3 );
1911 - }
1912 -
1913 - public function save_bulk_items( array $args = [] ) {
1914 - $items = [];
1915 -
1916 - foreach ( $args as $item ) {
1917 - $items[] = $this->save_item( [
1918 - 'content' => $item['content'],
1919 - 'title' => $item['title'],
1920 - 'type' => $item['type'],
1921 - 'page_settings' => $item['page_settings'],
1922 - ] );
1923 - }
1924 -
1925 - return $items;
1926 - }
1927 -
1928 - /**
1929 1659 * Template library local source constructor.
1930 1660 *
1931 1661 * Initializing the template library local source base by registering custom
1932 1662 * template data and running custom actions.
@@ -1937,87 +1667,6 @@
1937 1667 public function __construct() {
1938 1668 parent::__construct();
1939 1669
1940 1670 $this->add_actions();
1941 - }
1942 -
1943 - public function format_args_for_bulk_action( $args ) {
1944 - $bulk_args = [];
1945 -
1946 - foreach ( $args['from_template_id'] as $from_template_id ) {
1947 - if ( ! $this->is_allowed_to_read_template( [
1948 - 'template_id' => $from_template_id,
1949 - ] ) ) {
1950 - continue;
1951 - }
1952 -
1953 - $document = Plugin::$instance->documents->get( $from_template_id );
1954 -
1955 - if ( ! $document ) {
1956 - continue;
1957 - }
1958 -
1959 - $page = SettingsManager::get_settings_managers( 'page' )->get_model( $from_template_id );
1960 -
1961 - $bulk_args[] = array_merge(
1962 - $args,
1963 - [
1964 - 'title' => $document->get_post()->post_title,
1965 - 'type' => $document::get_type(),
1966 - 'content' => $document->get_elements_data(),
1967 - 'page_settings' => $page->get_data( 'settings' ),
1968 - ]
1969 - );
1970 - }
1971 -
1972 - return $bulk_args;
1973 - }
1974 -
1975 - public function format_args_for_single_action( $args ) {
1976 - if ( ! $this->is_allowed_to_read_template( [
1977 - 'template_id' => $args['from_template_id'],
1978 - ] ) ) {
1979 - return new \WP_Error(
1980 - 'template_error',
1981 - esc_html__( 'You do not have permission to access this template.', 'elementor' )
1982 - );
1983 - }
1984 -
1985 - $document = Plugin::$instance->documents->get( $args['from_template_id'] );
1986 -
1987 - if ( ! $document ) {
1988 - return new \WP_Error( 'template_error', 'Document not found.' );
1989 - }
1990 -
1991 - $args['content'] = $document->get_elements_data();
1992 -
1993 - $page = SettingsManager::get_settings_managers( 'page' )->get_model( $args['from_template_id'] );
1994 - $args['page_settings'] = $page->get_data( 'settings' );
1995 -
1996 - return $args;
1997 - }
1998 -
1999 - public function is_allowed_to_read_template( array $args ): bool {
2000 - if ( null === $this->wordpress_adapter ) {
2001 - $this->set_wordpress_adapter( new WordPress_Adapter() );
2002 - }
2003 -
2004 - $post_id = intval( $args['template_id'] );
2005 - $post_status = $this->wordpress_adapter->get_post_status( $post_id );
2006 -
2007 - if ( 'publish' === $post_status && ! post_password_required( $post_id ) ) {
2008 - return true;
2009 - }
2010 -
2011 - $can_read_template = $this->wordpress_adapter->current_user_can( 'edit_post', $post_id );
2012 -
2013 - return apply_filters( 'elementor/template-library/is_allowed_to_read_template', $can_read_template, $args );
2014 - }
2015 -
2016 - public function set_wordpress_adapter( Wordpress_Adapter_Interface $wordpress_adapter ) {
2017 - $this->wordpress_adapter = $wordpress_adapter;
2018 - }
2019 -
2020 - public function set_elementor_adapter( Elementor_Adapter_Interface $elementor_adapter ): void {
2021 - $this->elementor_adapter = $elementor_adapter;
2022 1671 }
2023 1672 }