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

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