PluginProbe
Elementor Website Builder – more than just a page builder / 3.13.3
Elementor Website Builder – more than just a page builder v3.13.3
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.13.3, at includes/template-library/sources/local.php

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