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