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

424 lines 12.0 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 if(!empty($template_id) && !empty($template) && $template->get_type() == "course_archive"){
178 // $__post = $GLOBALS['post'];
179 // learndash_course_grid_load_resources();
180 // $GLOBALS['post'] = $__post;
181 $GLOBALS['post'] = get_post($template_id);
182 }
183 }
184 }
185 $is_header_footer = 'header' === $location || 'footer' === $location;
186 if ( empty( $page_template ) && ! $is_header_footer ) {
187 $page_template = $page_template_module->get_header_footer_template();
188 }
189
190 if ( ! empty( $page_template ) ) {
191 $path = $page_template_module->get_template_path( $page_template );
192
193 if ( $path ) {
194 $page_template_module->set_print_callback( function () use ( $location ) {
195 $this->do_location( $location );
196 } );
197 set_query_var( 'using_templately_template', 1 );
198 $template_path = $path;
199 }
200 }
201
202 return $template_path;
203 }
204
205 private function get_platform($post_id) {
206 $post_type = get_post_type( get_the_ID() );
207 if ( $post_type == Source::CPT ) {
208 $platform = get_post_meta( $post_id, Source::PLATFORM_META_KEY, true );
209 } elseif ( get_post_meta( $post_id, '_elementor_edit_mode', true ) == 'builder' || $post_type == 'elementor_library' ) {
210 $platform = 'elementor';
211 } else {
212 $platform = 'gutenberg';
213 }
214 return $platform;
215 }
216
217 /**
218 * Get page templating modules and set platform if needed.
219 *
220 * @param string $platform
221 *
222 * @return PageTemplates
223 */
224 private function get_template_module( string $platform = '' ): PageTemplates {
225 $module = templately()->theme_builder::$page_template_module;
226
227 if ( ! empty( $platform ) ) {
228 $module = $module->set_platform( $platform );
229 }
230
231 return $module;
232 }
233
234 public function get_location( $location ) {
235 $locations = $this->get_locations();
236
237 return $locations[ $location ] ?? [];
238 }
239
240 public function get_locations(): array {
241 /**
242 * Don't know yet if we need it or not.
243 */
244 $this->set_locations();
245
246 $this->register_locations();
247
248 return $this->locations;
249 }
250
251 public function register_locations() {
252 if ( ! did_action( 'templately_locations' ) ) {
253 do_action( 'templately_locations', $this );
254 }
255 }
256
257 /**
258 * Getting the Idea From Elementor itself.
259 *
260 * @param $location
261 *
262 * @return bool
263 */
264 public function do_location( $location ): bool {
265 $templates_for_location = $this->builder::$conditions_manager->get_templates_by_location( $location );
266
267 foreach ( $templates_for_location as $template_id => $template ) {
268 $this->add_template_to_location( $location, $template_id );
269 }
270
271 if ( empty( $this->locations_queue[ $location ] ) ) {
272 return false;
273 }
274
275 while ( ! empty( $this->locations_queue[ $location ] ) ) {
276 $template_id = key( $this->locations_queue[ $location ] );
277 $template = $this->builder->get_template( $template_id );
278
279 if ( ! $template || $this->is_printed( $location, $template_id ) ) {
280 $this->skip_template_from_location( $location, $template_id );
281 continue;
282 }
283
284 if ( empty( $documents_by_conditions[ $template_id ] ) ) {
285 $post_status = get_post_status( $template_id );
286 if ( 'publish' !== $post_status ) {
287 $this->skip_template_from_location( $location, $template_id );
288 continue;
289 }
290 }
291 $template->print_content();
292 $this->did_locations[] = $location;
293
294 $this->set_is_printed( $location, $template_id );
295
296 do_action("templately_printed_location", $template_id, $location, $template);
297 }
298
299 return true;
300 }
301
302 public function enqueue_styles() {
303
304 if (
305 $this->get_platform(get_the_ID()) !== 'elementor' ||
306 (
307 class_exists('ElementorPro\Modules\ThemeBuilder\Module') &&
308 Module::is_preview()
309 )
310 ) {
311 return;
312 }
313
314
315
316 $locations = $this->get_locations();
317
318 if ( empty( $locations ) ) {
319 return;
320 }
321
322 // if ( ! empty( $this->current_page_template ) ) {
323 // $locations = $this->filter_page_template_locations( $locations );
324 // }
325
326 if(class_exists('Elementor\Core\Files\CSS\Post')){
327 $current_post_id = get_the_ID();
328
329 /** @var Post_CSS[] $css_files */
330 $css_files = [];
331
332 foreach ( $locations as $location => $settings ) {
333 $templates_for_location = $this->builder::$conditions_manager->get_templates_by_location( $location );
334
335 foreach ( $templates_for_location as $document ) {
336 $post_id = $document->get_main_id();
337 // Don't enqueue current post here (let the preview/frontend components to handle it)
338 if ( $current_post_id !== $post_id ) {
339 $css_file = new Post_CSS( $post_id );
340 $css_files[] = $css_file;
341 }
342 }
343 }
344
345 if ( ! empty( $css_files ) ) {
346 // Enqueue the frontend styles manually also for pages that don't built with Elementor.
347 // Plugin::elementor()->frontend->enqueue_styles();
348
349 // Enqueue after the frontend styles to override them.
350 foreach ( $css_files as $css_file ) {
351 $css_file->enqueue();
352 }
353
354 if(class_exists('ElementorPro\Plugin')){
355 /** @var \ElementorPro\Modules\ThemeBuilder\Module $theme_builder */
356 $theme_builder = Plugin::instance()->modules_manager->get_modules( 'theme-builder' );
357 $location_manager = $theme_builder->get_locations_manager();
358 remove_action( 'wp_enqueue_scripts', [ $location_manager, 'enqueue_styles' ] );
359 }
360 }
361 }
362 }
363
364 public function enqueue_template_assets() {
365 $using_templately_builder = get_query_var( 'using_templately_template' );
366 if ( ($using_templately_builder || TemplateLoader::is_header_footer()) && function_exists( 'templately' ) ) {
367 $template_locations = [ 'header', 'footer', 'archive', 'single' ];
368 foreach ( $template_locations as $location ) {
369 $template = templately()->theme_builder::$conditions_manager->get_templates_by_location( $location );
370 if ( empty( $template ) ) {
371 continue;
372 }
373 $template = array_pop( $template );
374 if ( $template->platform == 'gutenberg' ) {
375 $template = is_array( $template ) ? array_pop( $template ) : $template;
376 do_action("templately_printed_location", $template->get_main_id(), $location, $template);
377 }
378 }
379 }
380 }
381
382 /**
383 * @param string $location
384 * @param integer $template_id
385 */
386 public function add_template_to_location( string $location, int $template_id ) {
387 if ( isset( $this->locations_skipped[ $location ][ $template_id ] ) ) {
388 return;
389 }
390
391 if ( ! isset( $this->locations_queue[ $location ] ) ) {
392 $this->locations_queue[ $location ] = [];
393 }
394
395 $this->locations_queue[ $location ][ $template_id ] = $template_id;
396 }
397
398 public function is_printed( $location, $template_id ): bool {
399 return isset( $this->locations_printed[ $location ][ $template_id ] );
400 }
401
402 public function skip_template_from_location( $location, $template_id ) {
403 $this->remove_template_from_location( $location, $template_id );
404
405 if ( ! isset( $this->locations_skipped[ $location ] ) ) {
406 $this->locations_skipped[ $location ] = [];
407 }
408
409 $this->locations_skipped[ $location ][ $template_id ] = $template_id;
410 }
411
412 public function remove_template_from_location( $location, $template_id ) {
413 unset( $this->locations_queue[ $location ][ $template_id ] );
414 }
415
416 public function set_is_printed( $location, $template_id ) {
417 if ( ! isset( $this->locations_printed[ $location ] ) ) {
418 $this->locations_printed[ $location ] = [];
419 }
420
421 $this->locations_printed[ $location ][ $template_id ] = $template_id;
422 $this->remove_template_from_location( $location, $template_id );
423 }
424 }