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

417 lines 12.2 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
16 class LocationManager {
17 /**
18 * @var array<string, ThemeTemplate>
19 */
20 public $locations_queue = [];
21 public $locations_skipped = [];
22 public $locations_printed = [];
23 public $did_locations = [];
24
25 protected $locations = [];
26
27 /**
28 * @var ThemeBuilder
29 */
30 protected $builder;
31
32 public function __construct( $builder ) {
33 $this->builder = $builder;
34
35 /**
36 * Don't know yet if we need it or not.
37 */
38 $this->set_locations();
39
40 /**
41 * Priority is 13,
42 * Because it should be run after elementor & woocommerce
43 */
44 add_filter( 'template_include', [ $this, 'template_include' ], 13 );
45
46 /**
47 * Priority is 7,
48 * Because it should run before elementor
49 */
50 add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_styles' ], 7 );
51 add_action( 'eb_frontend_assets', [ $this, 'enqueue_template_assets' ], 10, 2 );
52 }
53
54 private function set_locations() {
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 ( ! empty( $platform ) ) {
120 $page_template_module->set_platform( $platform );
121 }
122 $path = $page_template_module->get_template_path( $page_template );
123 $page_template_module->set_print_callback( function () use ( $location ) {
124 $this->do_location( $location );
125 } );
126 set_query_var( 'using_templately_template', 1 );
127
128 return $path;
129 }
130
131 if ( $wp_page_template && $wp_page_template !== 'default' ) {
132 set_query_var( 'using_templately_template', 1 );
133
134 return $_custom_template_path;
135 }
136 }
137 } else {
138 $template = false;
139 }
140
141 if ( $template instanceof ThemeTemplate ) {
142 $location = $template->get_location();
143 } elseif ( function_exists( 'is_shop' ) && is_shop() ) {
144 $location = 'archive';
145 } elseif ( is_archive() || is_tax() || is_home() || is_search() ) {
146 $location = 'archive';
147 } elseif ( is_singular() || is_404() ) {
148 $location = 'single';
149 }
150
151 if ( $location ) {
152 $templates_for_location = $this->builder::$conditions_manager->get_templates_by_location( $location );
153
154 if ( empty( $templates_for_location ) ) {
155 set_query_var( 'using_templately_template', 1 );
156
157 return $template_path;
158 }
159
160 if ( 'single' === $location || 'archive' === $location ) {
161 $template_id = key( $templates_for_location );
162 $template = $templates_for_location[ $template_id ];
163 $document_page_template = $template->get_meta( '_wp_page_template' );
164 $platform = $template->get_platform();
165
166 if ( ! empty( $platform ) ) {
167 $page_template_module->set_platform( $platform );
168 }
169
170 if ( $document_page_template ) {
171 $page_template = $document_page_template;
172 }
173 }
174 }
175 $is_header_footer = 'header' === $location || 'footer' === $location;
176 if ( empty( $page_template ) && ! $is_header_footer ) {
177 $page_template = $page_template_module->get_header_footer_template();
178 }
179
180 if ( ! empty( $page_template ) ) {
181 $path = $page_template_module->get_template_path( $page_template );
182
183 if ( $path ) {
184 $page_template_module->set_print_callback( function () use ( $location ) {
185 $this->do_location( $location );
186 } );
187 set_query_var( 'using_templately_template', 1 );
188 $template_path = $path;
189 }
190 }
191
192 return $template_path;
193 }
194
195 private function get_platform($post_id) {
196 $post_type = get_post_type( get_the_ID() );
197 if ( $post_type == Source::CPT ) {
198 $platform = get_post_meta( $post_id, Source::PLATFORM_META_KEY, true );
199 } elseif ( get_post_meta( $post_id, '_elementor_edit_mode', true ) == 'builder' || $post_type == 'elementor_library' ) {
200 $platform = 'elementor';
201 } else {
202 $platform = 'gutenberg';
203 }
204 return $platform;
205 }
206
207 /**
208 * Get page templating modules and set platform if needed.
209 *
210 * @param string $platform
211 *
212 * @return PageTemplates
213 */
214 private function get_template_module( string $platform = '' ): PageTemplates {
215 $module = templately()->theme_builder::$page_template_module;
216
217 if ( ! empty( $platform ) ) {
218 $module = $module->set_platform( $platform );
219 }
220
221 return $module;
222 }
223
224 public function get_location( $location ) {
225 $locations = $this->get_locations();
226
227 return $locations[ $location ] ?? [];
228 }
229
230 public function get_locations(): array {
231 $this->register_locations();
232
233 return $this->locations;
234 }
235
236 public function register_locations() {
237 if ( ! did_action( 'templately_locations' ) ) {
238 do_action( 'templately_locations', $this );
239 }
240 }
241
242 /**
243 * Getting the Idea From Elementor itself.
244 *
245 * @param $location
246 *
247 * @return bool
248 */
249 public function do_location( $location ): bool {
250 $templates_for_location = $this->builder::$conditions_manager->get_templates_by_location( $location );
251
252 foreach ( $templates_for_location as $template_id => $template ) {
253 $this->add_template_to_location( $location, $template_id );
254 }
255
256 if ( empty( $this->locations_queue[ $location ] ) ) {
257 return false;
258 }
259
260 while ( ! empty( $this->locations_queue[ $location ] ) ) {
261 $template_id = key( $this->locations_queue[ $location ] );
262 $template = $this->builder->get_template( $template_id );
263
264 if ( ! $template || $this->is_printed( $location, $template_id ) ) {
265 $this->skip_template_from_location( $location, $template_id );
266 continue;
267 }
268
269 if ( empty( $documents_by_conditions[ $template_id ] ) ) {
270 $post_status = get_post_status( $template_id );
271 if ( 'publish' !== $post_status ) {
272 $this->skip_template_from_location( $location, $template_id );
273 continue;
274 }
275 }
276 $template->print_content();
277 $this->did_locations[] = $location;
278
279 $this->set_is_printed( $location, $template_id );
280 }
281
282 return true;
283 }
284
285 public function enqueue_styles() {
286
287 if (
288 $this->get_platform(get_the_ID()) !== 'elementor' ||
289 (
290 class_exists('ElementorPro\Modules\ThemeBuilder\Module') &&
291 Module::is_preview()
292 )
293 ) {
294 return;
295 }
296
297
298
299 $locations = $this->get_locations();
300
301 if ( empty( $locations ) ) {
302 return;
303 }
304
305 // if ( ! empty( $this->current_page_template ) ) {
306 // $locations = $this->filter_page_template_locations( $locations );
307 // }
308
309 if(class_exists('Elementor\Core\Files\CSS\Post')){
310 $current_post_id = get_the_ID();
311
312 /** @var Post_CSS[] $css_files */
313 $css_files = [];
314
315 foreach ( $locations as $location => $settings ) {
316 $templates_for_location = $this->builder::$conditions_manager->get_templates_by_location( $location );
317
318 foreach ( $templates_for_location as $document ) {
319 $post_id = $document->get_main_id();
320 // Don't enqueue current post here (let the preview/frontend components to handle it)
321 if ( $current_post_id !== $post_id ) {
322 $css_file = new Post_CSS( $post_id );
323 $css_files[] = $css_file;
324 }
325 }
326 }
327
328 if ( ! empty( $css_files ) ) {
329 // Enqueue the frontend styles manually also for pages that don't built with Elementor.
330 // Plugin::elementor()->frontend->enqueue_styles();
331
332 // Enqueue after the frontend styles to override them.
333 foreach ( $css_files as $css_file ) {
334 $css_file->enqueue();
335 }
336
337 if(class_exists('ElementorPro\Plugin')){
338 /** @var \ElementorPro\Modules\ThemeBuilder\Module $theme_builder */
339 $theme_builder = Plugin::instance()->modules_manager->get_modules( 'theme-builder' );
340 $location_manager = $theme_builder->get_locations_manager();
341 remove_action( 'wp_enqueue_scripts', [ $location_manager, 'enqueue_styles' ] );
342 }
343 }
344 }
345 }
346
347 public function enqueue_template_assets( $path, $url ) {
348 $using_templately_builder = get_query_var( 'using_templately_template' );
349 if ( $using_templately_builder && function_exists( 'templately' ) ) {
350 $template_locations = [ 'header', 'footer', 'archive', 'single' ];
351 foreach ( $template_locations as $location ) {
352 $template = templately()->theme_builder::$conditions_manager->get_templates_by_location( $location );
353 if ( empty( $template ) ) {
354 continue;
355 }
356 $template = array_pop( $template );
357 if ( $template->platform == 'gutenberg' ) {
358 $template = is_array( $template ) ? array_pop( $template ) : $template;
359 if ( class_exists('EbStyleHandler') && ! file_exists( $path . 'eb-style-' . $template->get_main_id() . '.min.css' ) ) {
360 $st = EbStyleHandler::init();
361 $post = get_post( $template->get_main_id() );
362 $st->eb_write_css_from_content( $post, $post->ID, parse_blocks( $post->post_content ) );
363 }
364 else if ( class_exists('EssentialBlocks\Modules\StyleHandler') && ! file_exists( $path . 'eb-style-' . $template->get_main_id() . '.min.css' ) ) {
365 $st = StyleHandler::init();
366 $post = get_post( $template->get_main_id() );
367 $st->on_save_post( $template->get_main_id(), $post, true );
368 }
369 wp_enqueue_style( 'templately-' . $location . '-' . $template->get_main_id(), $url . 'eb-style-' . $template->get_main_id() . '.min.css', [], substr( md5( microtime( true ) ), 0, 10 ) );
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 }