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

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