PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.1.11
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.1.11
3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.10 All 111 releases
templately / includes / Builder / PageTemplates.php

PageTemplates.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 3.1.11, at includes/Builder/PageTemplates.php

140 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Templately\Builder;
4
5 use Elementor\Plugin;
6
7 class PageTemplates {
8 const TEMPLATE_HEADER_FOOTER = 'templately_header_footer';
9 private $platform = '';
10 private $callback = null;
11 private $module = null;
12 private $templates;
13
14 public function __construct() {
15 add_filter( 'theme_page_templates', [ $this, 'add_new_template' ] );
16 add_filter( 'theme_post_templates', [ $this, 'add_new_template' ] );
17 add_filter( 'theme_templately_library_templates', [ $this, 'add_new_template' ] );
18
19 // Add a filter to the save post to inject out template into the page cache.
20 // add_filter( 'wp_insert_post_data', [ $this, 'register_templates' ] );
21
22 $this->templates = [
23 self::TEMPLATE_HEADER_FOOTER => __( 'Templately Gutenberg Template', 'templately' ),
24 ];
25 }
26
27 public function set_platform( $platform ): PageTemplates {
28 $this->platform = $platform;
29
30 if ( $this->platform === 'elementor' ) {
31 $this->module = Plugin::$instance->modules_manager->get_modules( 'page-templates' );
32 } elseif ( $this->platform === 'gutenberg' ) {
33 $this->module = new self();
34 }
35
36 return $this;
37 }
38
39 public function set_print_callback( $callback ) {
40 if ( $this->platform === 'elementor' ) {
41 $this->module->set_print_callback( $callback );
42
43 return;
44 }
45
46 $this->callback = $callback;
47 }
48
49 private function callback() {
50 while ( have_posts() ) {
51 the_post();
52 the_content();
53 }
54 }
55
56 public function print() {
57 if ( ! $this->callback ) {
58 $this->callback = [ $this, 'callback' ];
59 }
60
61 call_user_func( $this->callback );
62 }
63
64 /**
65 * Get page template path.
66 *
67 * Retrieve the path for any given page template.
68 *
69 * @param string $page_template The page template name.
70 *
71 * @return string Page template path.
72 * @since 2.0.0
73 * @access public
74 *
75 */
76 public function get_template_path( string $page_template ): string {
77 if ( $this->platform === 'elementor' ) {
78 return $this->module->get_template_path( $page_template );
79 }
80
81 $template_path = '';
82
83 $file = TEMPLATELY_PATH . 'views/';
84
85
86 switch ( $page_template ) {
87 case self::TEMPLATE_HEADER_FOOTER:
88 if ( $this->platform === 'gutenberg' ) {
89 $template_path = $file . 'templates/templately-gutenberg-template.php';
90 } else {
91 $template_path = $file . 'templates/header-footer.php';
92 }
93 break;
94 default:
95 break;
96 }
97
98 return $template_path;
99 }
100
101 public function get_header_footer_template(): string {
102 if ( $this->platform === 'elementor' ) {
103 return $this->module::TEMPLATE_HEADER_FOOTER;
104 }
105
106 return self::TEMPLATE_HEADER_FOOTER;
107 }
108
109 public function add_new_template( $posts_templates ) {
110 return array_merge( $posts_templates, $this->templates );
111 }
112
113 public function register_templates( $atts ) {
114
115 foreach ( [ 'page', 'post' ] as $type ) {
116 // Create the key used for the themes cache
117 $cache_key = $type . '_templates-' . md5( get_theme_root() . '/' . get_stylesheet() );
118
119 // Retrieve the cache list.
120 // If it doesn't exist, or it's empty prepare an array.
121 $templates = wp_get_theme()->{'get_' . $type . '_templates()'};
122 if ( empty( $templates ) ) {
123 $templates = [];
124 }
125
126 // New cache, therefore remove the old one.
127 wp_cache_delete( $cache_key, 'themes' );
128
129 // Now add our template to the list of templates by merging our templates.
130 // with the existing templates array from the cache.
131 $templates = array_merge( $templates, $this->templates );
132
133 // Add the modified cache to allow WordPress to pick it up for listing.
134 // available templates.
135 wp_cache_add( $cache_key, $templates, 'themes', 1800 );
136 }
137
138 return $atts;
139 }
140 }