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

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