PluginProbe
Elementor Website Builder – more than just a page builder / 3.25.7
Elementor Website Builder – more than just a page builder v3.25.7
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / includes / template-library / sources / local.php

local.php in Elementor Website Builder – more than just a page builder 3.25.7, at includes/template-library/sources/local.php

1,764 lines 48.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\TemplateLibrary;
3
4 use Elementor\Core\Admin\Menu\Admin_Menu_Manager;
5 use Elementor\Core\Base\Document;
6 use Elementor\Core\Editor\Editor;
7 use Elementor\Core\Utils\Collection;
8 use Elementor\DB;
9 use Elementor\Core\Settings\Manager as SettingsManager;
10 use Elementor\Core\Settings\Page\Model;
11 use Elementor\Includes\TemplateLibrary\Sources\AdminMenuItems\Add_New_Template_Menu_Item;
12 use Elementor\Includes\TemplateLibrary\Sources\AdminMenuItems\Saved_Templates_Menu_Item;
13 use Elementor\Includes\TemplateLibrary\Sources\AdminMenuItems\Templates_Categories_Menu_Item;
14 use Elementor\Modules\Library\Documents\Library_Document;
15 use Elementor\Plugin;
16 use Elementor\Utils;
17 use Elementor\User;
18
19 if ( ! defined( 'ABSPATH' ) ) {
20 exit; // Exit if accessed directly.
21 }
22
23 /**
24 * Elementor template library local source.
25 *
26 * Elementor template library local source handler class is responsible for
27 * handling local Elementor templates saved by the user locally on his site.
28 *
29 * @since 1.0.0
30 */
31 class Source_Local extends Source_Base {
32
33 /**
34 * Elementor template-library post-type slug.
35 */
36 const CPT = 'elementor_library';
37
38 /**
39 * Elementor template-library taxonomy slug.
40 */
41 const TAXONOMY_TYPE_SLUG = 'elementor_library_type';
42
43 /**
44 * Elementor template-library category slug.
45 */
46 const TAXONOMY_CATEGORY_SLUG = 'elementor_library_category';
47
48 /**
49 * Elementor template-library meta key.
50 *
51 * @deprecated 2.3.0 Use `Elementor\Core\Base\Document::TYPE_META_KEY` const instead.
52 */
53 const TYPE_META_KEY = '_elementor_template_type';
54
55 /**
56 * Elementor template-library temporary files folder.
57 */
58 const TEMP_FILES_DIR = 'elementor/tmp';
59
60 /**
61 * Elementor template-library bulk export action name.
62 */
63 const BULK_EXPORT_ACTION = 'elementor_export_multiple_templates';
64
65 const ADMIN_MENU_SLUG = 'edit.php?post_type=elementor_library';
66
67 const ADMIN_MENU_PRIORITY = 10;
68
69 const ADMIN_SCREEN_ID = 'edit-elementor_library';
70
71 /**
72 * Template types.
73 *
74 * Holds the list of supported template types that can be displayed.
75 *
76 * @access private
77 * @static
78 *
79 * @var array
80 */
81 private static $template_types = [];
82
83 /**
84 * Post type object.
85 *
86 * Holds the post type object of the current post.
87 *
88 * @access private
89 *
90 * @var \WP_Post_Type
91 */
92 private $post_type_object;
93
94 /**
95 * @since 2.3.0
96 * @access public
97 * @static
98 * @return array
99 */
100 public static function get_template_types() {
101 return self::$template_types;
102 }
103
104 /**
105 * Get local template type.
106 *
107 * Retrieve the template type from the post meta.
108 *
109 * @since 1.0.0
110 * @access public
111 * @static
112 *
113 * @param int $template_id The template ID.
114 *
115 * @return mixed The value of meta data field.
116 */
117 public static function get_template_type( $template_id ) {
118 return get_post_meta( $template_id, Document::TYPE_META_KEY, true );
119 }
120
121 /**
122 * Is base templates screen.
123 *
124 * Whether the current screen base is edit and the post type is template.
125 *
126 * @since 1.0.0
127 * @access public
128 * @static
129 *
130 * @return bool True on base templates screen, False otherwise.
131 */
132 public static function is_base_templates_screen() {
133 global $current_screen;
134
135 if ( ! $current_screen ) {
136 return false;
137 }
138
139 return 'edit' === $current_screen->base && self::CPT === $current_screen->post_type;
140 }
141
142 /**
143 * Add template type.
144 *
145 * Register new template type to the list of supported local template types.
146 *
147 * @since 1.0.3
148 * @access public
149 * @static
150 *
151 * @param string $type Template type.
152 */
153 public static function add_template_type( $type ) {
154 self::$template_types[ $type ] = $type;
155 }
156
157 /**
158 * Remove template type.
159 *
160 * Remove existing template type from the list of supported local template
161 * types.
162 *
163 * @since 1.8.0
164 * @access public
165 * @static
166 *
167 * @param string $type Template type.
168 */
169 public static function remove_template_type( $type ) {
170 if ( isset( self::$template_types[ $type ] ) ) {
171 unset( self::$template_types[ $type ] );
172 }
173 }
174
175 public static function get_admin_url( $relative = false ) {
176 $base_url = self::ADMIN_MENU_SLUG;
177 if ( ! $relative ) {
178 $base_url = admin_url( $base_url );
179 }
180
181 return add_query_arg( 'tabs_group', 'library', $base_url );
182 }
183
184 /**
185 * Get local template ID.
186 *
187 * Retrieve the local template ID.
188 *
189 * @since 1.0.0
190 * @access public
191 *
192 * @return string The local template ID.
193 */
194 public function get_id() {
195 return 'local';
196 }
197
198 /**
199 * Get local template title.
200 *
201 * Retrieve the local template title.
202 *
203 * @since 1.0.0
204 * @access public
205 *
206 * @return string The local template title.
207 */
208 public function get_title() {
209 return esc_html__( 'Local', 'elementor' );
210 }
211
212 /**
213 * Register local template data.
214 *
215 * Used to register custom template data like a post type, a taxonomy or any
216 * other data.
217 *
218 * The local template class registers a new `elementor_library` post type
219 * and an `elementor_library_type` taxonomy. They are used to store data for
220 * local templates saved by the user on his site.
221 *
222 * @since 1.0.0
223 * @access public
224 */
225 public function register_data() {
226 $labels = [
227 'name' => esc_html_x( 'My Templates', 'Template Library', 'elementor' ),
228 'singular_name' => esc_html_x( 'Template', 'Template Library', 'elementor' ),
229 'add_new' => esc_html__( 'Add New Template', 'elementor' ),
230 'add_new_item' => esc_html__( 'Add New Template', 'elementor' ),
231 'edit_item' => esc_html__( 'Edit Template', 'elementor' ),
232 'new_item' => esc_html__( 'New Template', 'elementor' ),
233 'all_items' => esc_html__( 'All Templates', 'elementor' ),
234 'view_item' => esc_html__( 'View Template', 'elementor' ),
235 'search_items' => esc_html__( 'Search Template', 'elementor' ),
236 'not_found' => esc_html__( 'No Templates found', 'elementor' ),
237 'not_found_in_trash' => esc_html__( 'No Templates found in Trash', 'elementor' ),
238 'parent_item_colon' => esc_html__( 'Parent Template:', 'elementor' ),
239 'menu_name' => esc_html_x( 'Templates', 'Template Library', 'elementor' ),
240 ];
241
242 $args = [
243 'labels' => $labels,
244 'public' => true,
245 'rewrite' => false,
246 'menu_icon' => 'dashicons-admin-page',
247 'show_ui' => true,
248 'show_in_menu' => true,
249 'show_in_nav_menus' => false,
250 'exclude_from_search' => true,
251 'capability_type' => 'post',
252 'hierarchical' => false,
253 'supports' => [ 'title', 'thumbnail', 'author', 'elementor' ],
254 ];
255
256 /**
257 * Register template library post type args.
258 *
259 * Filters the post type arguments when registering elementor template library post type.
260 *
261 * @since 1.0.0
262 *
263 * @param array $args Arguments for registering a post type.
264 */
265 $args = apply_filters( 'elementor/template_library/sources/local/register_post_type_args', $args );
266
267 $this->post_type_object = register_post_type( self::CPT, $args );
268
269 $args = [
270 'hierarchical' => false,
271 'show_ui' => false,
272 'show_in_nav_menus' => false,
273 'show_admin_column' => true,
274 'query_var' => is_admin(),
275 'rewrite' => false,
276 'public' => false,
277 'label' => esc_html_x( 'Type', 'Template Library', 'elementor' ),
278 ];
279
280 /**
281 * Register template library taxonomy args.
282 *
283 * Filters the taxonomy arguments when registering elementor template library taxonomy.
284 *
285 * @since 1.0.0
286 *
287 * @param array $args Arguments for registering a taxonomy.
288 */
289 $args = apply_filters( 'elementor/template_library/sources/local/register_taxonomy_args', $args );
290
291 $cpts_to_associate = [ self::CPT ];
292
293 /**
294 * Custom post types to associate.
295 *
296 * Filters the list of custom post types when registering elementor template library taxonomy.
297 *
298 * @since 1.0.0
299 *
300 * @param array $cpts_to_associate Custom post types. Default is `elementor_library` post type.
301 */
302 $cpts_to_associate = apply_filters( 'elementor/template_library/sources/local/register_taxonomy_cpts', $cpts_to_associate );
303
304 register_taxonomy( self::TAXONOMY_TYPE_SLUG, $cpts_to_associate, $args );
305
306 /**
307 * Categories
308 */
309 $args = [
310 'hierarchical' => true,
311 'show_ui' => true,
312 'show_in_nav_menus' => false,
313 'show_admin_column' => true,
314 'query_var' => is_admin(),
315 'rewrite' => false,
316 'public' => false,
317 'labels' => [
318 'name' => esc_html_x( 'Categories', 'Template Library', 'elementor' ),
319 'singular_name' => esc_html_x( 'Category', 'Template Library', 'elementor' ),
320 'all_items' => esc_html_x( 'All Categories', 'Template Library', 'elementor' ),
321 ],
322 ];
323
324 /**
325 * Register template library category args.
326 *
327 * Filters the category arguments when registering elementor template library category.
328 *
329 * @since 2.4.0
330 *
331 * @param array $args Arguments for registering a category.
332 */
333 $args = apply_filters( 'elementor/template_library/sources/local/register_category_args', $args );
334
335 register_taxonomy( self::TAXONOMY_CATEGORY_SLUG, self::CPT, $args );
336 }
337
338 /**
339 * Remove Add New item from admin menu.
340 *
341 * Fired by `admin_menu` action.
342 *
343 * @since 2.4.0
344 * @access public
345 */
346 private function admin_menu_reorder( Admin_Menu_Manager $admin_menu ) {
347 global $submenu;
348
349 if ( ! isset( $submenu[ static::ADMIN_MENU_SLUG ] ) ) {
350 return;
351 }
352
353 remove_submenu_page( static::ADMIN_MENU_SLUG, static::ADMIN_MENU_SLUG );
354
355 $add_new_slug = 'post-new.php?post_type=' . static::CPT;
356 $category_slug = 'edit-tags.php?taxonomy=' . static::TAXONOMY_CATEGORY_SLUG . '&amp;post_type=' . static::CPT;
357
358 $library_submenu = new Collection( $submenu[ static::ADMIN_MENU_SLUG ] );
359
360 $add_new_item = $library_submenu->find( function ( $item ) use ( $add_new_slug ) {
361 return $add_new_slug === $item[2];
362 } );
363
364 $categories_item = $library_submenu->find( function ( $item ) use ( $category_slug ) {
365 return $category_slug === $item[2];
366 } );
367
368 if ( $add_new_item ) {
369 remove_submenu_page( static::ADMIN_MENU_SLUG, $add_new_slug );
370
371 $admin_menu->register( admin_url( static::ADMIN_MENU_SLUG . '#add_new' ), new Add_New_Template_Menu_Item() );
372 }
373
374 if ( $categories_item ) {
375 remove_submenu_page( static::ADMIN_MENU_SLUG, $category_slug );
376
377 $admin_menu->register( $category_slug, new Templates_Categories_Menu_Item() );
378 }
379 }
380
381 /**
382 * Add a `current` CSS class to the `Saved Templates` submenu page when it's active.
383 * It should work by default, but since we interfere with WordPress' builtin CPT menus it doesn't work properly.
384 *
385 * @return void
386 */
387 private function admin_menu_set_current() {
388 global $submenu;
389
390 if ( $this->is_current_screen() ) {
391 $library_submenu = &$submenu[ static::ADMIN_MENU_SLUG ];
392 $library_title = $this->get_library_title();
393
394 foreach ( $library_submenu as &$item ) {
395 if ( $library_title === $item[0] ) {
396 if ( ! isset( $item[4] ) ) {
397 $item[4] = '';
398 }
399 $item[4] .= ' current';
400 }
401 }
402 }
403 }
404
405 private function register_admin_menu( Admin_Menu_Manager $admin_menu ) {
406 $admin_menu->register( static::get_admin_url( true ), new Saved_Templates_Menu_Item() );
407 }
408
409 public function admin_title( $admin_title, $title ) {
410 $library_title = $this->get_library_title();
411
412 if ( $library_title ) {
413 $admin_title = str_replace( $title, $library_title, $admin_title );
414 }
415
416 return $admin_title;
417 }
418
419 public function replace_admin_heading() {
420 $library_title = $this->get_library_title();
421
422 if ( $library_title ) {
423 global $post_type_object;
424
425 $post_type_object->labels->name = $library_title;
426 }
427 }
428
429 /**
430 * Get local templates.
431 *
432 * Retrieve local templates saved by the user on his site.
433 *
434 * @since 1.0.0
435 * @access public
436 *
437 * @param array $args Optional. Filter templates based on a set of
438 * arguments. Default is an empty array.
439 *
440 * @return array Local templates.
441 */
442 public function get_items( $args = [] ) {
443 $template_types = array_values( self::$template_types );
444
445 if ( ! empty( $args['type'] ) ) {
446 $template_types = $args['type'];
447 unset( $args['type'] );
448 }
449
450 $defaults = [
451 'post_type' => self::CPT,
452 'post_status' => 'publish',
453 'posts_per_page' => -1,
454 'orderby' => 'title',
455 'order' => 'ASC',
456 'meta_query' => [
457 [
458 'key' => Document::TYPE_META_KEY,
459 'value' => $template_types,
460 ],
461 ],
462 ];
463
464 $query_args = wp_parse_args( $args, $defaults );
465
466 $templates_query = new \WP_Query( $query_args );
467
468 $templates = [];
469
470 if ( $templates_query->have_posts() ) {
471 foreach ( $templates_query->get_posts() as $post ) {
472 $templates[] = $this->get_item( $post->ID );
473 }
474 }
475
476 return $templates;
477 }
478
479 /**
480 * Save local template.
481 *
482 * Save new or update existing template on the database.
483 *
484 * @since 1.0.0
485 * @access public
486 *
487 * @param array $template_data Local template data.
488 *
489 * @return \WP_Error|int The ID of the saved/updated template, `WP_Error` otherwise.
490 */
491 public function save_item( $template_data ) {
492 if ( ! current_user_can( $this->post_type_object->cap->edit_posts ) ) {
493 return new \WP_Error( 'save_error', esc_html__( 'Access denied.', 'elementor' ) );
494 }
495
496 $defaults = [
497 'title' => esc_html__( '(no title)', 'elementor' ),
498 'page_settings' => [],
499 ];
500
501 $template_data = wp_parse_args( $template_data, $defaults );
502 $template_data['status'] = current_user_can( 'publish_posts' ) ? 'publish' : 'pending';
503
504 // BC: Allow importing any template type when using CLI
505 // to support users that rely on this mechanism.
506 $should_check_template_type = ! $this->is_wp_cli();
507
508 if (
509 $should_check_template_type &&
510 ! $this->is_valid_template_type( $template_data['type'] )
511 ) {
512 return new \WP_Error( 'invalid_template_type', esc_html__( 'Invalid template type.', 'elementor' ) );
513 }
514
515 $document = Plugin::$instance->documents->create(
516 $template_data['type'],
517 [
518 'post_title' => $template_data['title'],
519 'post_status' => $template_data['status'],
520 ]
521 );
522
523 if ( is_wp_error( $document ) ) {
524 /**
525 * @var \WP_Error $document
526 */
527 return $document;
528 }
529
530 if ( ! empty( $template_data['content'] ) ) {
531 $template_data['content'] = $this->replace_elements_ids( $template_data['content'] );
532 }
533
534 $document->save( [
535 'elements' => $template_data['content'],
536 'settings' => $template_data['page_settings'],
537 ] );
538
539 $template_id = $document->get_main_id();
540
541 /**
542 * After template library save.
543 *
544 * Fires after Elementor template library was saved.
545 *
546 * @since 1.0.1
547 *
548 * @param int $template_id The ID of the template.
549 * @param array $template_data The template data.
550 */
551 do_action( 'elementor/template-library/after_save_template', $template_id, $template_data );
552
553 /**
554 * After template library update.
555 *
556 * Fires after Elementor template library was updated.
557 *
558 * @since 1.0.1
559 *
560 * @param int $template_id The ID of the template.
561 * @param array $template_data The template data.
562 */
563 do_action( 'elementor/template-library/after_update_template', $template_id, $template_data );
564
565 return $template_id;
566 }
567
568 protected function is_valid_template_type( $type ) {
569 $document_class = Plugin::$instance->documents->get_document_type( $type, false );
570
571 if ( ! $document_class ) {
572 return false;
573 }
574
575 $cpt = $document_class::get_property( 'cpt' );
576
577 if ( ! $cpt || ! is_array( $cpt ) || 1 !== count( $cpt ) ) {
578 return false;
579 }
580
581 $is_valid_template_type = in_array( static::CPT, $cpt, true );
582
583 return apply_filters(
584 'elementor/template_library/sources/local/is_valid_template_type',
585 $is_valid_template_type,
586 $cpt,
587 );
588 }
589
590 // For testing purposes only, in order to be able to mock the `WP_CLI` constant.
591 protected function is_wp_cli() {
592 return Utils::is_wp_cli();
593 }
594
595 /**
596 * Update local template.
597 *
598 * Update template on the database.
599 *
600 * @since 1.0.0
601 * @access public
602 *
603 * @param array $new_data New template data.
604 *
605 * @return \WP_Error|true True if template updated, `WP_Error` otherwise.
606 */
607 public function update_item( $new_data ) {
608 if ( ! current_user_can( $this->post_type_object->cap->edit_post, $new_data['id'] ) ) {
609 return new \WP_Error( 'save_error', esc_html__( 'Access denied.', 'elementor' ) );
610 }
611
612 $document = Plugin::$instance->documents->get( $new_data['id'] );
613
614 if ( ! $document ) {
615 return new \WP_Error( 'save_error', esc_html__( 'Template not exist.', 'elementor' ) );
616 }
617
618 $document->save( [
619 'elements' => $new_data['content'],
620 ] );
621
622 /**
623 * After template library update.
624 *
625 * Fires after Elementor template library was updated.
626 *
627 * @since 1.0.0
628 *
629 * @param int $new_data_id The ID of the new template.
630 * @param array $new_data The new template data.
631 */
632 do_action( 'elementor/template-library/after_update_template', $new_data['id'], $new_data );
633
634 return true;
635 }
636
637 /**
638 * Get local template.
639 *
640 * Retrieve a single local template saved by the user on his site.
641 *
642 * @since 1.0.0
643 * @access public
644 *
645 * @param int $template_id The template ID.
646 *
647 * @return array Local template.
648 */
649 public function get_item( $template_id ) {
650 $post = get_post( $template_id );
651
652 $user = get_user_by( 'id', $post->post_author );
653
654 $page = SettingsManager::get_settings_managers( 'page' )->get_model( $template_id );
655
656 $page_settings = $page->get_data( 'settings' );
657
658 $date = strtotime( $post->post_date );
659
660 $data = [
661 'template_id' => $post->ID,
662 'source' => $this->get_id(),
663 'type' => self::get_template_type( $post->ID ),
664 'title' => $post->post_title,
665 'thumbnail' => get_the_post_thumbnail_url( $post ),
666 'date' => $date,
667 'human_date' => date_i18n( get_option( 'date_format' ), $date ),
668 'human_modified_date' => date_i18n( get_option( 'date_format' ), strtotime( $post->post_modified ) ),
669 'author' => $user->display_name,
670 'status' => $post->post_status,
671 'hasPageSettings' => ! empty( $page_settings ),
672 'tags' => [],
673 'export_link' => $this->get_export_link( $template_id ),
674 'url' => get_permalink( $post->ID ),
675 ];
676
677 /**
678 * Get template library template.
679 *
680 * Filters the template data when retrieving a single template from the
681 * template library.
682 *
683 * @since 1.0.0
684 *
685 * @param array $data Template data.
686 */
687 $data = apply_filters( 'elementor/template-library/get_template', $data );
688
689 return $data;
690 }
691
692 /**
693 * Get template data.
694 *
695 * Retrieve the data of a single local template saved by the user on his site.
696 *
697 * @since 1.5.0
698 * @access public
699 *
700 * @param array $args Custom template arguments.
701 *
702 * @return array Local template data.
703 */
704 public function get_data( array $args ) {
705 $template_id = $args['template_id'];
706
707 $document = Plugin::$instance->documents->get( $template_id );
708 $content = [];
709
710 if ( $document ) {
711 // TODO: Validate the data (in JS too!).
712 if ( ! empty( $args['display'] ) ) {
713 $content = $document->get_elements_raw_data( null, true );
714 } else {
715 $content = $document->get_elements_data();
716 }
717
718 if ( ! empty( $content ) ) {
719 $content = $this->replace_elements_ids( $content );
720 }
721 }
722
723 $data = [
724 'content' => $content,
725 ];
726
727 if ( ! empty( $args['with_page_settings'] ) ) {
728 $page = SettingsManager::get_settings_managers( 'page' )->get_model( $args['template_id'] );
729
730 $data['page_settings'] = $page->get_data( 'settings' );
731 }
732
733 return $data;
734 }
735
736 /**
737 * Delete local template.
738 *
739 * Delete template from the database.
740 *
741 * @since 1.0.0
742 * @access public
743 *
744 * @param int $template_id The template ID.
745 *
746 * @return \WP_Post|\WP_Error|false|null Post data on success, false or null
747 * or 'WP_Error' on failure.
748 */
749 public function delete_template( $template_id ) {
750 if ( ! current_user_can( $this->post_type_object->cap->delete_post, $template_id ) ) {
751 return new \WP_Error( 'template_error', esc_html__( 'Access denied.', 'elementor' ) );
752 }
753
754 return wp_delete_post( $template_id, true );
755 }
756
757 /**
758 * Export local template.
759 *
760 * Export template to a file.
761 *
762 * @since 1.0.0
763 * @access public
764 *
765 * @param int $template_id The template ID.
766 *
767 * @return \WP_Error WordPress error if template export failed.
768 */
769 public function export_template( $template_id ) {
770 $file_data = $this->prepare_template_export( $template_id );
771
772 if ( is_wp_error( $file_data ) ) {
773 return $file_data;
774 }
775
776 $this->send_file_headers( $file_data['name'], strlen( $file_data['content'] ) );
777
778 // Clear buffering just in case.
779 @ob_end_clean();
780
781 flush();
782
783 // Output file contents.
784 // PHPCS - Export widget json
785 echo $file_data['content']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
786
787 die;
788 }
789
790 /**
791 * Export multiple local templates.
792 *
793 * Export multiple template to a ZIP file.
794 *
795 * @since 1.6.0
796 * @access public
797 *
798 * @param array $template_ids An array of template IDs.
799 *
800 * @return \WP_Error WordPress error if export failed.
801 */
802 public function export_multiple_templates( array $template_ids ) {
803 $files = [];
804
805 $wp_upload_dir = wp_upload_dir();
806
807 $temp_path = $wp_upload_dir['basedir'] . '/' . self::TEMP_FILES_DIR;
808
809 // Create temp path if it doesn't exist
810 wp_mkdir_p( $temp_path );
811
812 // Create all json files
813 foreach ( $template_ids as $template_id ) {
814 $file_data = $this->prepare_template_export( $template_id );
815
816 if ( is_wp_error( $file_data ) ) {
817 continue;
818 }
819
820 $complete_path = $temp_path . '/' . $file_data['name'];
821
822 $put_contents = file_put_contents( $complete_path, $file_data['content'] );
823
824 if ( ! $put_contents ) {
825 return new \WP_Error( '404', sprintf( 'Cannot create file "%s".', $file_data['name'] ) );
826 }
827
828 $files[] = [
829 'path' => $complete_path,
830 'name' => $file_data['name'],
831 ];
832 }
833
834 if ( ! $files ) {
835 return new \WP_Error( 'empty_files', 'There is no files to export (probably all the requested templates are empty).' );
836 }
837
838 // Create temporary .zip file
839 $zip_archive_filename = 'elementor-templates-' . gmdate( 'Y-m-d' ) . '.zip';
840
841 $zip_archive = new \ZipArchive();
842
843 $zip_complete_path = $temp_path . '/' . $zip_archive_filename;
844
845 $zip_archive->open( $zip_complete_path, \ZipArchive::CREATE );
846
847 foreach ( $files as $file ) {
848 $zip_archive->addFile( $file['path'], $file['name'] );
849 }
850
851 $zip_archive->close();
852
853 foreach ( $files as $file ) {
854 unlink( $file['path'] );
855 }
856
857 $this->send_file_headers( $zip_archive_filename, filesize( $zip_complete_path ) );
858
859 @ob_end_flush();
860
861 @readfile( $zip_complete_path );
862
863 unlink( $zip_complete_path );
864
865 die;
866 }
867
868 /**
869 * Import local template.
870 *
871 * Import template from a file.
872 *
873 * @since 1.0.0
874 * @access public
875 *
876 * @param string $name - The file name
877 * @param string $path - The file path
878 * @return \WP_Error|array An array of items on success, 'WP_Error' on failure.
879 */
880 public function import_template( $name, $path ) {
881 if ( empty( $path ) ) {
882 return new \WP_Error( 'file_error', 'Please upload a file to import' );
883 }
884
885 // Set the Request's state as an Elementor upload request, in order to support unfiltered file uploads.
886 Plugin::$instance->uploads_manager->set_elementor_upload_state( true );
887
888 $items = [];
889
890 // If the import file is a Zip file with potentially multiple JSON files
891 if ( 'zip' === pathinfo( $name, PATHINFO_EXTENSION ) ) {
892 $extracted_files = Plugin::$instance->uploads_manager->extract_and_validate_zip( $path, [ 'json' ] );
893
894 if ( is_wp_error( $extracted_files ) ) {
895 // Delete the temporary extraction directory, since it's now not necessary.
896 Plugin::$instance->uploads_manager->remove_file_or_dir( $extracted_files['extraction_directory'] );
897
898 return $extracted_files;
899 }
900
901 foreach ( $extracted_files['files'] as $file_path ) {
902 $import_result = $this->import_single_template( $file_path );
903
904 if ( is_wp_error( $import_result ) ) {
905 // Delete the temporary extraction directory, since it's now not necessary.
906 Plugin::$instance->uploads_manager->remove_file_or_dir( $extracted_files['extraction_directory'] );
907
908 return $import_result;
909 }
910
911 $items[] = $import_result;
912 }
913
914 // Delete the temporary extraction directory, since it's now not necessary.
915 Plugin::$instance->uploads_manager->remove_file_or_dir( $extracted_files['extraction_directory'] );
916 } else {
917 // If the import file is a single JSON file
918 $import_result = $this->import_single_template( $path );
919
920 if ( is_wp_error( $import_result ) ) {
921 return $import_result;
922 }
923
924 $items[] = $import_result;
925 }
926
927 return $items;
928 }
929
930 /**
931 * Post row actions.
932 *
933 * Add an export link to the template library action links table list.
934 *
935 * Fired by `post_row_actions` filter.
936 *
937 * @since 1.0.0
938 * @access public
939 *
940 * @param array $actions An array of row action links.
941 * @param \WP_Post $post The post object.
942 *
943 * @return array An updated array of row action links.
944 */
945 public function post_row_actions( $actions, \WP_Post $post ) {
946 if ( self::is_base_templates_screen() ) {
947 if ( $this->is_template_supports_export( $post->ID ) ) {
948 $actions['export-template'] = sprintf( '<a href="%1$s">%2$s</a>', $this->get_export_link( $post->ID ), esc_html__( 'Export Template', 'elementor' ) );
949 }
950 }
951
952 return $actions;
953 }
954
955 /**
956 * Admin import template form.
957 *
958 * The import form displayed in "My Library" screen in WordPress dashboard.
959 *
960 * The form allows the user to import template in json/zip format to the site.
961 *
962 * Fired by `admin_footer` action.
963 *
964 * @since 1.0.0
965 * @access public
966 */
967 public function admin_import_template_form() {
968 if ( ! self::is_base_templates_screen() || ! User::is_current_user_can_upload_json() ) {
969 return;
970 }
971
972 /** @var \Elementor\Core\Common\Modules\Ajax\Module $ajax */
973 $ajax = Plugin::$instance->common->get_component( 'ajax' );
974 ?>
975 <div id="elementor-hidden-area">
976 <a id="elementor-import-template-trigger" class="page-title-action"><?php echo esc_html__( 'Import Templates', 'elementor' ); ?></a>
977 <div id="elementor-import-template-area">
978 <div id="elementor-import-template-title"><?php echo esc_html__( 'Choose an Elementor template JSON file or a .zip archive of Elementor templates, and add them to the list of templates available in your library.', 'elementor' ); ?></div>
979 <form id="elementor-import-template-form" method="post" action="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>" enctype="multipart/form-data">
980 <input type="hidden" name="action" value="elementor_library_direct_actions">
981 <input type="hidden" name="library_action" value="direct_import_template">
982 <input type="hidden" name="_nonce" value="<?php Utils::print_unescaped_internal_string( $ajax->create_nonce() ); ?>">
983 <fieldset id="elementor-import-template-form-inputs">
984 <input type="file" name="file" accept=".json,application/json,.zip,application/octet-stream,application/zip,application/x-zip,application/x-zip-compressed" required>
985 <input id="e-import-template-action" type="submit" class="button" value="<?php echo esc_attr__( 'Import Now', 'elementor' ); ?>">
986 </fieldset>
987 </form>
988 </div>
989 </div>
990 <?php
991 }
992
993 /**
994 * Block template frontend
995 *
996 * Don't display the single view of the template library post type in the
997 * frontend, for users that don't have the proper permissions.
998 *
999 * Fired by `template_redirect` action.
1000 *
1001 * @since 1.0.0
1002 * @access public
1003 */
1004 public function block_template_frontend() {
1005 if ( is_singular( self::CPT ) && ! current_user_can( Editor::EDITING_CAPABILITY ) ) {
1006 wp_safe_redirect( site_url(), 301 );
1007 die;
1008 }
1009 }
1010
1011 /**
1012 * Is template library supports export.
1013 *
1014 * whether the template library supports export.
1015 *
1016 * Template saved by the user locally on his site, support export by default
1017 * but this can be changed using a filter.
1018 *
1019 * @since 1.0.0
1020 * @access public
1021 *
1022 * @param int $template_id The template ID.
1023 *
1024 * @return bool Whether the template library supports export.
1025 */
1026 public function is_template_supports_export( $template_id ) {
1027 $export_support = true;
1028
1029 /**
1030 * Is template library supports export.
1031 *
1032 * Filters whether the template library supports export.
1033 *
1034 * @since 1.0.0
1035 *
1036 * @param bool $export_support Whether the template library supports export.
1037 * Default is true.
1038 * @param int $template_id Post ID.
1039 */
1040 $export_support = apply_filters( 'elementor/template_library/is_template_supports_export', $export_support, $template_id );
1041
1042 return $export_support;
1043 }
1044
1045 /**
1046 * Remove Elementor post state.
1047 *
1048 * Remove the 'elementor' post state from the display states of the post.
1049 *
1050 * Used to remove the 'elementor' post state from the template library items.
1051 *
1052 * Fired by `display_post_states` filter.
1053 *
1054 * @since 1.8.0
1055 * @access public
1056 *
1057 * @param array $post_states An array of post display states.
1058 * @param \WP_Post $post The current post object.
1059 *
1060 * @return array Updated array of post display states.
1061 */
1062 public function remove_elementor_post_state_from_library( $post_states, $post ) {
1063 if ( self::CPT === $post->post_type && isset( $post_states['elementor'] ) ) {
1064 unset( $post_states['elementor'] );
1065 }
1066 return $post_states;
1067 }
1068
1069 /**
1070 * Get template export link.
1071 *
1072 * Retrieve the link used to export a single template based on the template
1073 * ID.
1074 *
1075 * @since 2.0.0
1076 * @access private
1077 *
1078 * @param int $template_id The template ID.
1079 *
1080 * @return string Template export URL.
1081 */
1082 private function get_export_link( $template_id ) {
1083 // TODO: BC since 2.3.0 - Use `$ajax->create_nonce()`
1084 /** @var \Elementor\Core\Common\Modules\Ajax\Module $ajax */
1085 // $ajax = Plugin::$instance->common->get_component( 'ajax' );
1086
1087 return add_query_arg(
1088 [
1089 'action' => 'elementor_library_direct_actions',
1090 'library_action' => 'export_template',
1091 'source' => $this->get_id(),
1092 '_nonce' => wp_create_nonce( 'elementor_ajax' ),
1093 'template_id' => $template_id,
1094 ],
1095 admin_url( 'admin-ajax.php' )
1096 );
1097 }
1098
1099 /**
1100 * On template save.
1101 *
1102 * Run this method when template is being saved.
1103 *
1104 * Fired by `save_post` action.
1105 *
1106 * @since 1.0.1
1107 * @access public
1108 *
1109 * @param int $post_id Post ID.
1110 * @param \WP_Post $post The current post object.
1111 */
1112 public function on_save_post( $post_id, \WP_Post $post ) {
1113 if ( self::CPT !== $post->post_type ) {
1114 return;
1115 }
1116
1117 if ( self::get_template_type( $post_id ) ) { // It's already with a type
1118 return;
1119 }
1120
1121 // Don't save type on import, the importer will do it.
1122 if ( did_action( 'import_start' ) ) {
1123 return;
1124 }
1125
1126 $this->save_item_type( $post_id, 'page' );
1127 }
1128
1129 /**
1130 * Save item type.
1131 *
1132 * When saving/updating templates, this method is used to update the post
1133 * meta data and the taxonomy.
1134 *
1135 * @since 1.0.1
1136 * @access private
1137 *
1138 * @param int $post_id Post ID.
1139 * @param string $type Item type.
1140 */
1141 private function save_item_type( $post_id, $type ) {
1142 update_post_meta( $post_id, Document::TYPE_META_KEY, $type );
1143
1144 wp_set_object_terms( $post_id, $type, self::TAXONOMY_TYPE_SLUG );
1145 }
1146
1147 /**
1148 * Bulk export action.
1149 *
1150 * Adds an 'Export' action to the Bulk Actions drop-down in the template
1151 * library.
1152 *
1153 * Fired by `bulk_actions-edit-elementor_library` filter.
1154 *
1155 * @since 1.6.0
1156 * @access public
1157 *
1158 * @param array $actions An array of the available bulk actions.
1159 *
1160 * @return array An array of the available bulk actions.
1161 */
1162 public function admin_add_bulk_export_action( $actions ) {
1163 $actions[ self::BULK_EXPORT_ACTION ] = esc_html__( 'Export', 'elementor' );
1164
1165 return $actions;
1166 }
1167
1168 /**
1169 * Add bulk export action.
1170 *
1171 * Handles the template library bulk export action.
1172 *
1173 * Fired by `handle_bulk_actions-edit-elementor_library` filter.
1174 *
1175 * @since 1.6.0
1176 * @access public
1177 *
1178 * @param string $redirect_to The redirect URL.
1179 * @param string $action The action being taken.
1180 * @param array $post_ids The items to take the action on.
1181 */
1182 public function admin_export_multiple_templates( $redirect_to, $action, $post_ids ) {
1183 if ( self::BULK_EXPORT_ACTION === $action ) {
1184 $result = $this->export_multiple_templates( $post_ids );
1185
1186 // If you reach this line, the export failed
1187 // PHPCS - Not user input.
1188 wp_die( $result->get_error_message() ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1189 }
1190 }
1191
1192 /**
1193 * Print admin tabs.
1194 *
1195 * Used to output the template library tabs with their labels.
1196 *
1197 * Fired by `views_edit-elementor_library` filter.
1198 *
1199 * @since 2.0.0
1200 * @access public
1201 *
1202 * @param array $views An array of available list table views.
1203 *
1204 * @return array An updated array of available list table views.
1205 */
1206 public function admin_print_tabs( $views ) {
1207 //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce is not required to retrieve the value.
1208 $current_type = Utils::get_super_global_value( $_REQUEST, self::TAXONOMY_TYPE_SLUG ) ?? '';
1209 $active_class = $current_type ? '' : ' nav-tab-active';
1210 $current_tabs_group = $this->get_current_tab_group();
1211
1212 $url_args = [
1213 'post_type' => self::CPT,
1214 'tabs_group' => $current_tabs_group,
1215 ];
1216
1217 $baseurl = add_query_arg( $url_args, admin_url( 'edit.php' ) );
1218
1219 $filter = [
1220 'admin_tab_group' => $current_tabs_group,
1221 ];
1222 $operator = 'and';
1223
1224 if ( empty( $current_tabs_group ) ) {
1225 // Don't include 'not-supported' or other templates that don't set their `admin_tab_group`.
1226 $operator = 'NOT';
1227 }
1228
1229 $doc_types = Plugin::$instance->documents->get_document_types( $filter, $operator );
1230
1231 if ( 1 >= count( $doc_types ) ) {
1232 return $views;
1233 }
1234
1235 ?>
1236 <div id="elementor-template-library-tabs-wrapper" class="nav-tab-wrapper">
1237 <a class="nav-tab<?php echo esc_attr( $active_class ); ?>" href="<?php echo esc_url( $baseurl ); ?>">
1238 <?php
1239 $all_title = $this->get_library_title();
1240 if ( ! $all_title ) {
1241 $all_title = esc_html__( 'All', 'elementor' );
1242 }
1243 Utils::print_unescaped_internal_string( $all_title ); ?>
1244 </a>
1245 <?php
1246 foreach ( $doc_types as $type => $class_name ) :
1247 $active_class = '';
1248
1249 if ( $current_type === $type ) {
1250 $active_class = ' nav-tab-active';
1251 }
1252
1253 $type_url = esc_url( add_query_arg( self::TAXONOMY_TYPE_SLUG, $type, $baseurl ) );
1254 $type_label = $this->get_template_label_by_type( $type );
1255 Utils::print_unescaped_internal_string( "<a class='nav-tab{$active_class}' href='{$type_url}'>{$type_label}</a>" );
1256 endforeach;
1257 ?>
1258 </div>
1259 <?php
1260 return $views;
1261 }
1262
1263 /**
1264 * Maybe render blank state.
1265 *
1266 * When the template library has no saved templates, display a blank admin page offering
1267 * to create the very first template.
1268 *
1269 * Fired by `manage_posts_extra_tablenav` action.
1270 *
1271 * @since 2.0.0
1272 * @access public
1273 *
1274 * @param string $which The location of the extra table nav markup: 'top' or 'bottom'.
1275 * @param array $args
1276 */
1277 public function maybe_render_blank_state( $which, array $args = [] ) {
1278 global $post_type;
1279
1280 $args = wp_parse_args( $args, [
1281 'cpt' => self::CPT,
1282 'post_type' => get_query_var( 'elementor_library_type' ),
1283 ] );
1284
1285 if ( $args['cpt'] !== $post_type || 'bottom' !== $which ) {
1286 return;
1287 }
1288
1289 global $wp_list_table;
1290
1291 $total_items = $wp_list_table->get_pagination_arg( 'total_items' );
1292
1293 //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce is not required to retrieve the value.
1294 if ( ! empty( $total_items ) || ! empty( $_REQUEST['s'] ) ) {
1295 return;
1296 }
1297
1298 $current_type = $args['post_type'];
1299
1300 $document_types = Plugin::instance()->documents->get_document_types();
1301
1302 if ( empty( $document_types[ $current_type ] ) ) {
1303 return;
1304 }
1305
1306 // TODO: Better way to exclude widget type.
1307 if ( 'widget' === $current_type ) {
1308 return;
1309 }
1310
1311 // TODO: This code maybe unreachable see if above `if ( empty( $document_types[ $current_type ] ) )`.
1312 if ( empty( $current_type ) ) {
1313 $counts = (array) wp_count_posts( self::CPT );
1314 unset( $counts['auto-draft'] );
1315 $count = array_sum( $counts );
1316
1317 if ( 0 < $count ) {
1318 return;
1319 }
1320
1321 $current_type = 'template';
1322
1323 $args['additional_inline_style'] = '#elementor-template-library-tabs-wrapper {display: none;}';
1324 }
1325
1326 $this->render_blank_state( $current_type, $args );
1327 }
1328
1329 private function render_blank_state( $current_type, array $args = [] ) {
1330 $current_type_label = $this->get_template_label_by_type( $current_type );
1331 $inline_style = '#posts-filter .wp-list-table, #posts-filter .tablenav.top, .tablenav.bottom .actions, .wrap .subsubsub { display:none;}';
1332
1333 $args = wp_parse_args( $args, [
1334 'additional_inline_style' => '',
1335 'href' => '',
1336 'description' => esc_html__( 'Add templates and reuse them across your website. Easily export and import them to any other project, for an optimized workflow.', 'elementor' ),
1337 ] );
1338 $inline_style .= $args['additional_inline_style'];
1339 ?>
1340 <style type="text/css"><?php Utils::print_unescaped_internal_string( $inline_style ); ?></style>
1341 <div class="elementor-template_library-blank_state">
1342 <?php $this->print_blank_state_template( $current_type_label, $args['href'], $args['description'] ); ?>
1343 </div>
1344 <?php
1345 }
1346
1347 /**
1348 * Print Blank State Template
1349 *
1350 * When the an entity (CPT, Taxonomy...etc) has no saved items, print a blank admin page offering
1351 * to create the very first item.
1352 *
1353 * This method is public because it needs to be accessed from outside the Source_Local
1354 *
1355 * @since 3.1.0
1356 * @access public
1357 *
1358 * @param string $current_type_label The Entity title
1359 * @param string $href The URL for the 'Add New' button
1360 * @param string $description The sub title describing the Entity (Post Type, Taxonomy, etc.)
1361 */
1362 public function print_blank_state_template( $current_type_label, $href, $description ) {
1363 ?>
1364 <div class="elementor-blank_state">
1365 <i class="eicon-folder"></i>
1366 <h3>
1367 <?php
1368 /* translators: %s: Template type label. */
1369 printf( esc_html__( 'Create Your First %s', 'elementor' ), esc_html( $current_type_label ) );
1370 ?>
1371 </h3>
1372 <p><?php echo wp_kses_post( $description ); ?></p>
1373 <a id="elementor-template-library-add-new" class="elementor-button e-primary" href="<?php echo esc_url( $href ); ?>">
1374 <?php
1375 /* translators: %s: Template type label. */
1376 printf( esc_html__( 'Add New %s', 'elementor' ), esc_html( $current_type_label ) );
1377 ?>
1378 </a>
1379 </div>
1380 <?php
1381 }
1382
1383 /**
1384 * Add filter by category.
1385 *
1386 * In the templates library, add a filter by Elementor library category.
1387 *
1388 * @access public
1389 *
1390 * @param string $post_type The post type slug.
1391 */
1392 public function add_filter_by_category( $post_type ) {
1393 if ( self::CPT !== $post_type ) {
1394 return;
1395 }
1396
1397 $all_items = get_taxonomy( self::TAXONOMY_CATEGORY_SLUG )->labels->all_items;
1398 $dropdown_options = array(
1399 'show_option_all' => $all_items,
1400 'show_option_none' => $all_items,
1401 'hide_empty' => 0,
1402 'hierarchical' => 1,
1403 'show_count' => 0,
1404 'orderby' => 'name',
1405 'value_field' => 'slug',
1406 'taxonomy' => self::TAXONOMY_CATEGORY_SLUG,
1407 'name' => self::TAXONOMY_CATEGORY_SLUG,
1408 //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce is not required to retrieve the value.
1409 'selected' => Utils::get_super_global_value( $_GET, self::TAXONOMY_CATEGORY_SLUG ) ?? '',
1410 );
1411
1412 printf(
1413 '<label class="screen-reader-text" for="%1$s">%2$s</label>',
1414 esc_attr( self::TAXONOMY_CATEGORY_SLUG ),
1415 esc_html_x( 'Filter by category', 'Template Library', 'elementor' )
1416 );
1417 wp_dropdown_categories( $dropdown_options );
1418 }
1419
1420 /**
1421 * Import single template.
1422 *
1423 * Import template from a file to the database.
1424 *
1425 * @since 1.6.0
1426 * @access private
1427 *
1428 * @param string $file_path File name.
1429 *
1430 * @return \WP_Error|int|array Local template array, or template ID, or
1431 * `WP_Error`.
1432 */
1433 private function import_single_template( $file_path ) {
1434 $data = json_decode( Utils::file_get_contents( $file_path ), true );
1435
1436 if ( empty( $data ) ) {
1437 return new \WP_Error( 'file_error', 'Invalid File' );
1438 }
1439
1440 $content = $data['content'];
1441
1442 if ( ! is_array( $content ) ) {
1443 return new \WP_Error( 'file_error', 'Invalid Content In File' );
1444 }
1445
1446 $content = $this->process_export_import_content( $content, 'on_import' );
1447
1448 $page_settings = [];
1449
1450 if ( ! empty( $data['page_settings'] ) ) {
1451 $page = new Model( [
1452 'id' => 0,
1453 'settings' => $data['page_settings'],
1454 ] );
1455
1456 $page_settings_data = $this->process_element_export_import_content( $page, 'on_import' );
1457
1458 if ( ! empty( $page_settings_data['settings'] ) ) {
1459 $page_settings = $page_settings_data['settings'];
1460 }
1461 }
1462
1463 $template_id = $this->save_item( [
1464 'content' => $content,
1465 'title' => $data['title'],
1466 'type' => $data['type'],
1467 'page_settings' => $page_settings,
1468 ] );
1469
1470 if ( is_wp_error( $template_id ) ) {
1471 return $template_id;
1472 }
1473
1474 return $this->get_item( $template_id );
1475 }
1476
1477 /**
1478 * Prepare template to export.
1479 *
1480 * Retrieve the relevant template data and return them as an array.
1481 *
1482 * @since 1.6.0
1483 * @access private
1484 *
1485 * @param int $template_id The template ID.
1486 *
1487 * @return \WP_Error|array Exported template data.
1488 */
1489 private function prepare_template_export( $template_id ) {
1490 $document = Plugin::$instance->documents->get( $template_id );
1491
1492 $template_data = $document->get_export_data();
1493
1494 if ( empty( $template_data['content'] ) ) {
1495 return new \WP_Error( 'empty_template', 'The template is empty' );
1496 }
1497
1498 $export_data = [
1499 'content' => $template_data['content'],
1500 'page_settings' => $template_data['settings'],
1501 'version' => DB::DB_VERSION,
1502 'title' => $document->get_main_post()->post_title,
1503 'type' => self::get_template_type( $template_id ),
1504 ];
1505
1506 return [
1507 'name' => 'elementor-' . $template_id . '-' . gmdate( 'Y-m-d' ) . '.json',
1508 'content' => wp_json_encode( $export_data ),
1509 ];
1510 }
1511
1512 /**
1513 * Send file headers.
1514 *
1515 * Set the file header when export template data to a file.
1516 *
1517 * @since 1.6.0
1518 * @access private
1519 *
1520 * @param string $file_name File name.
1521 * @param int $file_size File size.
1522 */
1523 private function send_file_headers( $file_name, $file_size ) {
1524 header( 'Content-Type: application/octet-stream' );
1525 header( 'Content-Disposition: attachment; filename=' . $file_name );
1526 header( 'Expires: 0' );
1527 header( 'Cache-Control: must-revalidate' );
1528 header( 'Pragma: public' );
1529 header( 'Content-Length: ' . $file_size );
1530 }
1531
1532 /**
1533 * Get template label by type.
1534 *
1535 * Retrieve the template label for any given template type.
1536 *
1537 * @since 2.0.0
1538 * @access private
1539 *
1540 * @param string $template_type Template type.
1541 *
1542 * @return string Template label.
1543 */
1544 private function get_template_label_by_type( $template_type ) {
1545 $document_types = Plugin::instance()->documents->get_document_types();
1546
1547 if ( isset( $document_types[ $template_type ] ) ) {
1548 $template_label = call_user_func( [ $document_types[ $template_type ], 'get_title' ] );
1549 } else {
1550 $template_label = ucwords( str_replace( [ '_', '-' ], ' ', $template_type ) );
1551 }
1552
1553 /**
1554 * Template label by template type.
1555 *
1556 * Filters the template label by template type in the template library .
1557 *
1558 * @since 2.0.0
1559 *
1560 * @param string $template_label Template label.
1561 * @param string $template_type Template type.
1562 */
1563 $template_label = apply_filters( 'elementor/template-library/get_template_label_by_type', $template_label, $template_type );
1564
1565 return $template_label;
1566 }
1567
1568 /**
1569 * Filter template types in admin query.
1570 *
1571 * Update the template types in the main admin query.
1572 *
1573 * Fired by `parse_query` action.
1574 *
1575 * @since 2.4.0
1576 * @access public
1577 *
1578 * @param \WP_Query $query The `WP_Query` instance.
1579 */
1580 public function admin_query_filter_types( \WP_Query $query ) {
1581 if ( ! $this->is_current_screen() || ! empty( $query->query_vars['meta_key'] ) ) {
1582 return;
1583 }
1584
1585 $current_tabs_group = $this->get_current_tab_group();
1586
1587 if ( isset( $query->query_vars[ self::TAXONOMY_CATEGORY_SLUG ] ) && '-1' === $query->query_vars[ self::TAXONOMY_CATEGORY_SLUG ] ) {
1588 unset( $query->query_vars[ self::TAXONOMY_CATEGORY_SLUG ] );
1589 }
1590
1591 if ( empty( $current_tabs_group ) ) {
1592 return;
1593 }
1594
1595 $doc_types = Plugin::$instance->documents->get_document_types( [
1596 'admin_tab_group' => $current_tabs_group,
1597 ] );
1598
1599 $query->query_vars['meta_key'] = Document::TYPE_META_KEY;
1600 $query->query_vars['meta_value'] = array_keys( $doc_types );
1601 }
1602
1603 /**
1604 * Add template library actions.
1605 *
1606 * Register filters and actions for the template library.
1607 *
1608 * @since 2.0.0
1609 * @access private
1610 */
1611 private function add_actions() {
1612 if ( is_admin() ) {
1613 add_action( 'elementor/admin/menu/register', function ( Admin_Menu_Manager $admin_menu ) {
1614 $this->register_admin_menu( $admin_menu );
1615 }, static::ADMIN_MENU_PRIORITY );
1616
1617 add_action( 'elementor/admin/menu/register', function ( Admin_Menu_Manager $admin_menu ) {
1618 $this->admin_menu_reorder( $admin_menu );
1619 }, 800 );
1620
1621 add_action( 'elementor/admin/menu/after_register', function () {
1622 $this->admin_menu_set_current();
1623 } );
1624
1625 add_filter( 'admin_title', [ $this, 'admin_title' ], 10, 2 );
1626 add_action( 'all_admin_notices', [ $this, 'replace_admin_heading' ] );
1627 add_filter( 'post_row_actions', [ $this, 'post_row_actions' ], 10, 2 );
1628 add_action( 'admin_footer', [ $this, 'admin_import_template_form' ] );
1629 add_action( 'save_post', [ $this, 'on_save_post' ], 10, 2 );
1630 add_filter( 'display_post_states', [ $this, 'remove_elementor_post_state_from_library' ], 11, 2 );
1631
1632 add_action( 'parse_query', [ $this, 'admin_query_filter_types' ] );
1633
1634 // Template filter by category.
1635 add_action( 'restrict_manage_posts', [ $this, 'add_filter_by_category' ] );
1636
1637 // Template type column.
1638 add_action( 'manage_' . self::CPT . '_posts_columns', [ $this, 'admin_columns_headers' ] );
1639 add_action( 'manage_' . self::CPT . '_posts_custom_column', [ $this, 'admin_columns_content' ], 10, 2 );
1640
1641 // Template library bulk actions.
1642 add_filter( 'bulk_actions-edit-elementor_library', [ $this, 'admin_add_bulk_export_action' ] );
1643 add_filter( 'handle_bulk_actions-edit-elementor_library', [ $this, 'admin_export_multiple_templates' ], 10, 3 );
1644
1645 // Print template library tabs.
1646 add_filter( 'views_edit-' . self::CPT, [ $this, 'admin_print_tabs' ] );
1647
1648 // Show blank state.
1649 add_action( 'manage_posts_extra_tablenav', [ $this, 'maybe_render_blank_state' ] );
1650 }
1651
1652 add_action( 'template_redirect', [ $this, 'block_template_frontend' ] );
1653
1654 // Remove elementor library templates from WP Sitemap
1655 add_filter(
1656 'wp_sitemaps_post_types',
1657 function( $post_types ) {
1658 return $this->remove_elementor_cpt_from_sitemap( $post_types );
1659 }
1660 );
1661 }
1662
1663 /**
1664 * @since 2.0.6
1665 * @access public
1666 */
1667 public function admin_columns_content( $column_name, $post_id ) {
1668 if ( 'elementor_library_type' === $column_name ) {
1669 /** @var Document $document */
1670 $document = Plugin::$instance->documents->get( $post_id );
1671
1672 if ( $document && $document instanceof Library_Document ) {
1673 $document->print_admin_column_type();
1674 }
1675 }
1676 }
1677
1678 /**
1679 * @since 2.0.6
1680 * @access public
1681 */
1682 public function admin_columns_headers( $posts_columns ) {
1683 // Replace original column that bind to the taxonomy - with another column.
1684 unset( $posts_columns['taxonomy-elementor_library_type'] );
1685
1686 $offset = 2;
1687
1688 $posts_columns = array_slice( $posts_columns, 0, $offset, true ) + [
1689 'elementor_library_type' => esc_html__( 'Type', 'elementor' ),
1690 ] + array_slice( $posts_columns, $offset, null, true );
1691
1692 return $posts_columns;
1693 }
1694
1695 public function get_current_tab_group( $default = '' ) {
1696 //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce verification is not required here.
1697 $current_tabs_group = Utils::get_super_global_value( $_REQUEST, 'tabs_group' ) ?? '';
1698 //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce verification is not required here.
1699 $type_slug = Utils::get_super_global_value( $_REQUEST, self::TAXONOMY_TYPE_SLUG );
1700
1701 if ( $type_slug ) {
1702 $doc_type = Plugin::$instance->documents->get_document_type( $type_slug, '' );
1703 if ( $doc_type ) {
1704 $current_tabs_group = $doc_type::get_property( 'admin_tab_group' );
1705 }
1706 }
1707 return $current_tabs_group;
1708 }
1709
1710 private function get_library_title() {
1711 $title = '';
1712
1713 if ( $this->is_current_screen() ) {
1714 $current_tab_group = $this->get_current_tab_group();
1715
1716 if ( $current_tab_group ) {
1717 $titles = [
1718 'library' => esc_html__( 'Saved Templates', 'elementor' ),
1719 'theme' => esc_html__( 'Theme Builder', 'elementor' ),
1720 'popup' => esc_html__( 'Popups', 'elementor' ),
1721 ];
1722
1723 if ( ! empty( $titles[ $current_tab_group ] ) ) {
1724 $title = $titles[ $current_tab_group ];
1725 }
1726 }
1727 }
1728
1729 return $title;
1730 }
1731
1732 private function is_current_screen() {
1733 global $pagenow, $typenow;
1734
1735 return 'edit.php' === $pagenow && self::CPT === $typenow;
1736 }
1737
1738 /**
1739 * @param array $post_types
1740 *
1741 * @return array
1742 */
1743 private function remove_elementor_cpt_from_sitemap( array $post_types ) {
1744 unset( $post_types[ self::CPT ] );
1745
1746 return $post_types;
1747 }
1748
1749 /**
1750 * Template library local source constructor.
1751 *
1752 * Initializing the template library local source base by registering custom
1753 * template data and running custom actions.
1754 *
1755 * @since 1.0.0
1756 * @access public
1757 */
1758 public function __construct() {
1759 parent::__construct();
1760
1761 $this->add_actions();
1762 }
1763 }
1764