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

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