PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.3.3
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.3.3
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 / Managers / LocationManager.php

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

417 lines 11.8 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\Managers;
4
5 use EbStyleHandler;
6 use EssentialBlocks\Modules\StyleHandler;
7 use Templately\Builder\PageTemplates;
8 use Templately\Builder\Source;
9 use Templately\Builder\ThemeBuilder;
10 use Templately\Builder\Types\BaseTemplate;
11 use Templately\Builder\Types\ThemeTemplate;
12 use ElementorPro\Modules\ThemeBuilder\Module;
13 use Elementor\Core\Files\CSS\Post as Post_CSS;
14 use ElementorPro\Plugin;
15 use Templately\Builder\TemplateLoader;
16
17 class LocationManager {
18 /**
19 * @var array<string, ThemeTemplate>
20 */
21 public $locations_queue = [];
22 public $locations_skipped = [];
23 public $locations_printed = [];
24 public $did_locations = [];
25
26 protected $locations = [];
27
28 /**
29 * @var ThemeBuilder
30 */
31 protected $builder;
32
33 public function __construct( $builder ) {
34 $this->builder = $builder;
35
36
37 /**
38 * Priority is 13,
39 * Because it should be run after elementor & woocommerce
40 */
41 add_filter( 'template_include', [ $this, 'template_include' ], 13 );
42
43 /**
44 * Priority is 7,
45 * Because it should run before elementor
46 */
47 add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_styles' ], 7 );
48 add_action( 'wp_enqueue_scripts', [$this, 'enqueue_template_assets'], 8 );
49 }
50
51 private function set_locations() {
52 if(!empty($this->locations)) {
53 return;
54 }
55 $this->locations = [
56 'header' => [
57 'label' => __( 'Header', 'templately' ),
58 'multiple' => false
59 ],
60 'footer' => [
61 'label' => __( 'Footer', 'templately' ),
62 'multiple' => false
63 ],
64 'archive' => [
65 'label' => __( 'Archive', 'templately' ),
66 'multiple' => false
67 ],
68 'single' => [
69 'label' => __( 'Single', 'templately' ),
70 'multiple' => false
71 ]
72 ];
73 }
74
75 public function template_include( $template_path ) {
76 $location = '';
77 $page_template_module = $this->get_template_module();
78
79 /**
80 * Return if it is Elementor Template.
81 * This should be elementor's responsibility.
82 */
83
84 if ( get_post_type( get_the_ID() ) === 'elementor_library' ) {
85 return $template_path;
86 }
87 else if( $this->get_platform(get_the_ID()) === 'elementor' && !class_exists('Elementor\Plugin') ) {
88 return $template_path;
89 }
90
91 if ( is_singular() ) {
92 /**
93 * @var BaseTemplate $template
94 */
95 $template = $this->builder::$templates_manager->get( get_the_ID() );
96
97 if ( $template && $template->get_property( 'support_wp_page_templates' ) ) {
98 $page_template_module->set_platform( $template->get_platform() );
99 $wp_page_template = $template->get_meta( '_wp_page_template' );
100
101 $_custom_template_path = $page_template_module->get_template_path( $wp_page_template );
102
103
104 if ( empty( $_custom_template_path ) ) {
105 $location = 'single';
106
107 $templates_for_location = $this->builder::$conditions_manager->get_templates_by_location( $location );
108
109 if ( empty( $templates_for_location ) ) {
110 return $template_path;
111 }
112
113 $template_id = key( $templates_for_location );
114
115 $template = $templates_for_location[ $template_id ];
116 $page_template = $template->get_meta( '_wp_page_template' );
117 $platform = $template->get_platform();
118
119 if( $platform === 'elementor' && !class_exists('Elementor\Plugin') ) {
120 return $template_path;
121 }
122 if ( ! empty( $platform ) ) {
123 $page_template_module->set_platform( $platform );
124 }
125 $path = $page_template_module->get_template_path( $page_template );
126 $page_template_module->set_print_callback( function () use ( $location ) {
127 $this->do_location( $location );
128 } );
129 set_query_var( 'using_templately_template', 1 );
130
131 return $path;
132 }
133
134 if ( $wp_page_template && $wp_page_template !== 'default' ) {
135 set_query_var( 'using_templately_template', 1 );
136
137 return $_custom_template_path;
138 }
139 }
140 } else {
141 $template = false;
142 }
143
144 if ( $template instanceof ThemeTemplate ) {
145 $location = $template->get_location();
146 } elseif ( function_exists( 'is_shop' ) && is_shop() ) {
147 $location = 'archive';
148 } elseif ( is_archive() || is_tax() || is_home() || is_search() ) {
149 $location = 'archive';
150 } elseif ( is_singular() || is_404() ) {
151 $location = 'single';
152 }
153
154 if ( $location ) {
155 $templates_for_location = $this->builder::$conditions_manager->get_templates_by_location( $location );
156
157 if ( empty( $templates_for_location ) ) {
158 set_query_var( 'using_templately_template', 1 );
159
160 return $template_path;
161 }
162
163 if ( 'single' === $location || 'archive' === $location ) {
164 $template_id = key( $templates_for_location );
165 $template = $templates_for_location[ $template_id ];
166 $document_page_template = $template->get_meta( '_wp_page_template' );
167 $platform = $template->get_platform();
168
169 if ( ! empty( $platform ) ) {
170 $page_template_module->set_platform( $platform );
171 }
172
173 if ( $document_page_template ) {
174 $page_template = $document_page_template;
175 }
176 }
177 }
178 $is_header_footer = 'header' === $location || 'footer' === $location;
179 if ( empty( $page_template ) && ! $is_header_footer ) {
180 $page_template = $page_template_module->get_header_footer_template();
181 }
182
183 if ( ! empty( $page_template ) ) {
184 $path = $page_template_module->get_template_path( $page_template );
185
186 if ( $path ) {
187 $page_template_module->set_print_callback( function () use ( $location ) {
188 $this->do_location( $location );
189 } );
190 set_query_var( 'using_templately_template', 1 );
191 $template_path = $path;
192 }
193 }
194
195 return $template_path;
196 }
197
198 private function get_platform($post_id) {
199 $post_type = get_post_type( get_the_ID() );
200 if ( $post_type == Source::CPT ) {
201 $platform = get_post_meta( $post_id, Source::PLATFORM_META_KEY, true );
202 } elseif ( get_post_meta( $post_id, '_elementor_edit_mode', true ) == 'builder' || $post_type == 'elementor_library' ) {
203 $platform = 'elementor';
204 } else {
205 $platform = 'gutenberg';
206 }
207 return $platform;
208 }
209
210 /**
211 * Get page templating modules and set platform if needed.
212 *
213 * @param string $platform
214 *
215 * @return PageTemplates
216 */
217 private function get_template_module( string $platform = '' ): PageTemplates {
218 $module = templately()->theme_builder::$page_template_module;
219
220 if ( ! empty( $platform ) ) {
221 $module = $module->set_platform( $platform );
222 }
223
224 return $module;
225 }
226
227 public function get_location( $location ) {
228 $locations = $this->get_locations();
229
230 return $locations[ $location ] ?? [];
231 }
232
233 public function get_locations(): array {
234 /**
235 * Don't know yet if we need it or not.
236 */
237 $this->set_locations();
238
239 $this->register_locations();
240
241 return $this->locations;
242 }
243
244 public function register_locations() {
245 if ( ! did_action( 'templately_locations' ) ) {
246 do_action( 'templately_locations', $this );
247 }
248 }
249
250 /**
251 * Getting the Idea From Elementor itself.
252 *
253 * @param $location
254 *
255 * @return bool
256 */
257 public function do_location( $location ): bool {
258 $templates_for_location = $this->builder::$conditions_manager->get_templates_by_location( $location );
259
260 foreach ( $templates_for_location as $template_id => $template ) {
261 $this->add_template_to_location( $location, $template_id );
262 }
263
264 if ( empty( $this->locations_queue[ $location ] ) ) {
265 return false;
266 }
267
268 while ( ! empty( $this->locations_queue[ $location ] ) ) {
269 $template_id = key( $this->locations_queue[ $location ] );
270 $template = $this->builder->get_template( $template_id );
271
272 if ( ! $template || $this->is_printed( $location, $template_id ) ) {
273 $this->skip_template_from_location( $location, $template_id );
274 continue;
275 }
276
277 if ( empty( $documents_by_conditions[ $template_id ] ) ) {
278 $post_status = get_post_status( $template_id );
279 if ( 'publish' !== $post_status ) {
280 $this->skip_template_from_location( $location, $template_id );
281 continue;
282 }
283 }
284 $template->print_content();
285 $this->did_locations[] = $location;
286
287 $this->set_is_printed( $location, $template_id );
288
289 do_action("templately_printed_location", $template_id, $location, $template);
290 }
291
292 return true;
293 }
294
295 public function enqueue_styles() {
296
297 if (
298 $this->get_platform(get_the_ID()) !== 'elementor' ||
299 (
300 class_exists('ElementorPro\Modules\ThemeBuilder\Module') &&
301 Module::is_preview()
302 )
303 ) {
304 return;
305 }
306
307
308
309 $locations = $this->get_locations();
310
311 if ( empty( $locations ) ) {
312 return;
313 }
314
315 // if ( ! empty( $this->current_page_template ) ) {
316 // $locations = $this->filter_page_template_locations( $locations );
317 // }
318
319 if(class_exists('Elementor\Core\Files\CSS\Post')){
320 $current_post_id = get_the_ID();
321
322 /** @var Post_CSS[] $css_files */
323 $css_files = [];
324
325 foreach ( $locations as $location => $settings ) {
326 $templates_for_location = $this->builder::$conditions_manager->get_templates_by_location( $location );
327
328 foreach ( $templates_for_location as $document ) {
329 $post_id = $document->get_main_id();
330 // Don't enqueue current post here (let the preview/frontend components to handle it)
331 if ( $current_post_id !== $post_id ) {
332 $css_file = new Post_CSS( $post_id );
333 $css_files[] = $css_file;
334 }
335 }
336 }
337
338 if ( ! empty( $css_files ) ) {
339 // Enqueue the frontend styles manually also for pages that don't built with Elementor.
340 // Plugin::elementor()->frontend->enqueue_styles();
341
342 // Enqueue after the frontend styles to override them.
343 foreach ( $css_files as $css_file ) {
344 $css_file->enqueue();
345 }
346
347 if(class_exists('ElementorPro\Plugin')){
348 /** @var \ElementorPro\Modules\ThemeBuilder\Module $theme_builder */
349 $theme_builder = Plugin::instance()->modules_manager->get_modules( 'theme-builder' );
350 $location_manager = $theme_builder->get_locations_manager();
351 remove_action( 'wp_enqueue_scripts', [ $location_manager, 'enqueue_styles' ] );
352 }
353 }
354 }
355 }
356
357 public function enqueue_template_assets() {
358 $using_templately_builder = get_query_var( 'using_templately_template' );
359 if ( ($using_templately_builder || TemplateLoader::is_header_footer()) && function_exists( 'templately' ) ) {
360 $template_locations = [ 'header', 'footer', 'archive', 'single' ];
361 foreach ( $template_locations as $location ) {
362 $template = templately()->theme_builder::$conditions_manager->get_templates_by_location( $location );
363 if ( empty( $template ) ) {
364 continue;
365 }
366 $template = array_pop( $template );
367 if ( $template->platform == 'gutenberg' ) {
368 $template = is_array( $template ) ? array_pop( $template ) : $template;
369 do_action("templately_printed_location", $template->get_main_id(), $location, $template);
370 }
371 }
372 }
373 }
374
375 /**
376 * @param string $location
377 * @param integer $template_id
378 */
379 public function add_template_to_location( string $location, int $template_id ) {
380 if ( isset( $this->locations_skipped[ $location ][ $template_id ] ) ) {
381 return;
382 }
383
384 if ( ! isset( $this->locations_queue[ $location ] ) ) {
385 $this->locations_queue[ $location ] = [];
386 }
387
388 $this->locations_queue[ $location ][ $template_id ] = $template_id;
389 }
390
391 public function is_printed( $location, $template_id ): bool {
392 return isset( $this->locations_printed[ $location ][ $template_id ] );
393 }
394
395 public function skip_template_from_location( $location, $template_id ) {
396 $this->remove_template_from_location( $location, $template_id );
397
398 if ( ! isset( $this->locations_skipped[ $location ] ) ) {
399 $this->locations_skipped[ $location ] = [];
400 }
401
402 $this->locations_skipped[ $location ][ $template_id ] = $template_id;
403 }
404
405 public function remove_template_from_location( $location, $template_id ) {
406 unset( $this->locations_queue[ $location ][ $template_id ] );
407 }
408
409 public function set_is_printed( $location, $template_id ) {
410 if ( ! isset( $this->locations_printed[ $location ] ) ) {
411 $this->locations_printed[ $location ] = [];
412 }
413
414 $this->locations_printed[ $location ][ $template_id ] = $template_id;
415 $this->remove_template_from_location( $location, $template_id );
416 }
417 }