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

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

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