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

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