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

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

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