| @@ -10,11 +10,10 @@ | ||
| 10 | 10 | use Templately\Builder\Types\BaseTemplate; |
| 11 | 11 | use Templately\Builder\Types\ThemeTemplate; |
| 12 | 12 | use ElementorPro\Modules\ThemeBuilder\Module; |
| 13 | 13 | use Elementor\Core\Files\CSS\Post as Post_CSS; |
| 14 | -use Elementor\Plugin; | |
| 14 | +use ElementorPro\Plugin; | |
| 15 | 15 | use Templately\Builder\TemplateLoader; |
| 16 | -use Elementor\Core\Base\Elements_Iteration_Actions\Assets as Assets_Iteration_Action; | |
| 17 | 16 | |
| 18 | 17 | class LocationManager { |
| 19 | 18 | /** |
| 20 | 19 | * @var array<string, ThemeTemplate> |
| @@ -44,16 +43,10 @@ | ||
| 44 | 43 | /** |
| 45 | 44 | * Priority is 7, |
| 46 | 45 | * Because it should run before elementor |
| 47 | 46 | */ |
| 48 | - add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_elementor_styles' ], 7 ); | |
| 49 | - add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_template_assets' ], 8 ); | |
| 50 | - add_action( 'wp_enqueue_scripts', [ $this, 'preload_gutenberg_template_styles' ], 9 ); | |
| 51 | - | |
| 52 | - // Cache clearing hooks | |
| 53 | - add_action( 'save_post', [ $this, 'clear_header_styles_cache' ] ); | |
| 54 | - add_action( 'delete_post', [ $this, 'clear_header_styles_cache' ] ); | |
| 55 | - add_action( 'trash_post', [ $this, 'clear_header_styles_cache' ] ); | |
| 47 | + add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_styles' ], 7 ); | |
| 48 | + add_action( 'wp_enqueue_scripts', [$this, 'enqueue_template_assets'], 8 ); | |
| 56 | 49 | } |
| 57 | 50 | |
| 58 | 51 | private function set_locations() { |
| 59 | 52 | if(!empty($this->locations)) { |
| @@ -179,15 +172,8 @@ | ||
| 179 | 172 | |
| 180 | 173 | if ( $document_page_template ) { |
| 181 | 174 | $page_template = $document_page_template; |
| 182 | 175 | } |
| 183 | - | |
| 184 | - if(!empty($template_id) && !empty($template) && $template->get_type() == "course_archive"){ | |
| 185 | - // $__post = $GLOBALS['post']; | |
| 186 | - // learndash_course_grid_load_resources(); | |
| 187 | - // $GLOBALS['post'] = $__post; | |
| 188 | - $GLOBALS['post'] = get_post($template_id); | |
| 189 | - } | |
| 190 | 176 | } |
| 191 | 177 | } |
| 192 | 178 | $is_header_footer = 'header' === $location || 'footer' === $location; |
| 193 | 179 | if ( empty( $page_template ) && ! $is_header_footer ) { |
| @@ -294,38 +280,33 @@ | ||
| 294 | 280 | $this->skip_template_from_location( $location, $template_id ); |
| 295 | 281 | continue; |
| 296 | 282 | } |
| 297 | 283 | } |
| 298 | - | |
| 299 | - // Fire before printing to allow style preparation | |
| 300 | - do_action("templately_printed_location", $template_id, $location, $template); | |
| 301 | - | |
| 302 | 284 | $template->print_content(); |
| 303 | 285 | $this->did_locations[] = $location; |
| 304 | 286 | |
| 305 | 287 | $this->set_is_printed( $location, $template_id ); |
| 288 | + | |
| 289 | + do_action("templately_printed_location", $template_id, $location, $template); | |
| 306 | 290 | } |
| 307 | 291 | |
| 308 | 292 | return true; |
| 309 | 293 | } |
| 310 | 294 | |
| 311 | - /** | |
| 312 | - * Enqueue CSS styles for Elementor-based templates. | |
| 313 | - * | |
| 314 | - * Uses Elementor's Post_CSS class to enqueue styles for all locations | |
| 315 | - * (header, footer, archive, single). Only runs for Elementor platform. | |
| 316 | - * | |
| 317 | - * @since 3.0.0 | |
| 318 | - * @return void | |
| 319 | - */ | |
| 320 | - public function enqueue_elementor_styles() { | |
| 295 | + public function enqueue_styles() { | |
| 296 | + | |
| 321 | 297 | if ( |
| 322 | - class_exists('ElementorPro\Modules\ThemeBuilder\Module') && | |
| 323 | - Module::is_preview() | |
| 298 | + $this->get_platform(get_the_ID()) !== 'elementor' || | |
| 299 | + ( | |
| 300 | + class_exists('ElementorPro\Modules\ThemeBuilder\Module') && | |
| 301 | + Module::is_preview() | |
| 302 | + ) | |
| 324 | 303 | ) { |
| 325 | 304 | return; |
| 326 | 305 | } |
| 327 | 306 | |
| 307 | + | |
| 308 | + | |
| 328 | 309 | $locations = $this->get_locations(); |
| 329 | 310 | |
| 330 | 311 | if ( empty( $locations ) ) { |
| 331 | 312 | return; |
| @@ -330,94 +311,50 @@ | ||
| 330 | 311 | if ( empty( $locations ) ) { |
| 331 | 312 | return; |
| 332 | 313 | } |
| 333 | 314 | |
| 315 | + // if ( ! empty( $this->current_page_template ) ) { | |
| 316 | + // $locations = $this->filter_page_template_locations( $locations ); | |
| 317 | + // } | |
| 318 | + | |
| 334 | 319 | if(class_exists('Elementor\Core\Files\CSS\Post')){ |
| 335 | 320 | $current_post_id = get_the_ID(); |
| 336 | 321 | |
| 322 | + /** @var Post_CSS[] $css_files */ | |
| 323 | + $css_files = []; | |
| 324 | + | |
| 337 | 325 | foreach ( $locations as $location => $settings ) { |
| 338 | 326 | $templates_for_location = $this->builder::$conditions_manager->get_templates_by_location( $location ); |
| 339 | 327 | |
| 340 | - foreach ( $templates_for_location as $template ) { | |
| 341 | - if ( ! $template->is_elementor_template() ) { | |
| 342 | - continue; | |
| 343 | - } | |
| 344 | - | |
| 345 | - $this->iterate_elements($template->get_main_id()); | |
| 346 | - | |
| 347 | - $post_id = $template->get_main_id(); | |
| 348 | - | |
| 349 | - // Don't enqueue current post here (let the preview/frontend components to handle it) | |
| 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) | |
| 350 | 331 | if ( $current_post_id !== $post_id ) { |
| 351 | 332 | $css_file = new Post_CSS( $post_id ); |
| 352 | - $css_file->enqueue(); | |
| 333 | + $css_files[] = $css_file; | |
| 353 | 334 | } |
| 354 | 335 | } |
| 355 | 336 | } |
| 356 | 337 | |
| 357 | - } | |
| 358 | - } | |
| 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(); | |
| 359 | 341 | |
| 360 | - private function iterate_elements( $post_id ) { | |
| 361 | - if ( ! class_exists( 'Elementor\Plugin' ) || ! class_exists( Assets_Iteration_Action::class ) ) { | |
| 362 | - return; | |
| 363 | - } | |
| 342 | + // Enqueue after the frontend styles to override them. | |
| 343 | + foreach ( $css_files as $css_file ) { | |
| 344 | + $css_file->enqueue(); | |
| 345 | + } | |
| 364 | 346 | |
| 365 | - $plugin = Plugin::instance(); | |
| 366 | - if ( ! $plugin || ! isset( $plugin->documents ) || ! isset( $plugin->db ) || ! isset( $plugin->elements_manager ) ) { | |
| 367 | - return; | |
| 368 | - } | |
| 369 | - | |
| 370 | - $document = $plugin->documents->get( $post_id ); | |
| 371 | - if ( ! $document ) { | |
| 372 | - return; | |
| 373 | - } | |
| 374 | - | |
| 375 | - $elements = $document->get_elements_data(); | |
| 376 | - if ( empty( $elements ) ) { | |
| 377 | - return; | |
| 378 | - } | |
| 379 | - | |
| 380 | - $unique_page_elements = []; | |
| 381 | - $elements_iteration_action = new Assets_Iteration_Action( $document ); | |
| 382 | - $elements_iteration_action->set_mode( 'render' ); | |
| 383 | - | |
| 384 | - $plugin->db->iterate_data( $elements, function( array $element_data ) use ( &$unique_page_elements, $elements_iteration_action, $plugin ) { | |
| 385 | - $element_type = 'widget' === $element_data['elType'] ? $element_data['widgetType'] : $element_data['elType']; | |
| 386 | - | |
| 387 | - $element = $plugin->elements_manager->create_element_instance( $element_data ); | |
| 388 | - | |
| 389 | - if ( $element ) { | |
| 390 | - if ( ! in_array( $element_type, $unique_page_elements, true ) ) { | |
| 391 | - $unique_page_elements[] = $element_type; | |
| 392 | - | |
| 393 | - $elements_iteration_action->unique_element_action( $element ); | |
| 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' ] ); | |
| 394 | 352 | } |
| 395 | - | |
| 396 | - $elements_iteration_action->element_action( $element ); | |
| 397 | 353 | } |
| 398 | - | |
| 399 | - return $element_data; | |
| 400 | - } ); | |
| 401 | - | |
| 402 | - $elements_iteration_action->after_elements_iteration(); | |
| 354 | + } | |
| 403 | 355 | } |
| 404 | 356 | |
| 405 | - /** | |
| 406 | - * Fire action for Essential Blocks to write CSS files. | |
| 407 | - * | |
| 408 | - * Iterates all Gutenberg template locations and fires the | |
| 409 | - * templately_printed_location action. | |
| 410 | - * | |
| 411 | - * Note: This action only triggers Essential Blocks to generate the CSS file | |
| 412 | - * and add the template ID to its processing list. It does NOT enqueue the | |
| 413 | - * CSS file immediately. The actual enqueue happens later in Essential Blocks' | |
| 414 | - * `enqueue_frontend_assets` method (hooked to wp_enqueue_scripts at priority 10), | |
| 415 | - * which relies on this action having already fired (at priority 8). | |
| 416 | - * | |
| 417 | - * @since 3.0.0 | |
| 418 | - * @return void | |
| 419 | - */ | |
| 420 | 357 | public function enqueue_template_assets() { |
| 421 | 358 | $using_templately_builder = get_query_var( 'using_templately_template' ); |
| 422 | 359 | if ( ($using_templately_builder || TemplateLoader::is_header_footer()) && function_exists( 'templately' ) ) { |
| 423 | 360 | $template_locations = [ 'header', 'footer', 'archive', 'single' ]; |
| @@ -435,119 +372,8 @@ | ||
| 435 | 372 | } |
| 436 | 373 | } |
| 437 | 374 | |
| 438 | 375 | /** |
| 439 | - * Pre-parse Gutenberg template blocks to enqueue styles in head. | |
| 440 | - * | |
| 441 | - * Iterates all template locations, parses block content, and triggers | |
| 442 | - * block style registration BEFORE wp_head() outputs styles. This ensures | |
| 443 | - * all block styles are properly enqueued rather than printed inline. | |
| 444 | - * | |
| 445 | - * @since 3.0.0 | |
| 446 | - * @return void | |
| 447 | - */ | |
| 448 | - public function preload_gutenberg_template_styles() { | |
| 449 | - if ( ! function_exists( 'templately' ) || ! function_exists( 'parse_blocks' ) ) { | |
| 450 | - return; | |
| 451 | - } | |
| 452 | - | |
| 453 | - $template_locations = [ 'header' ]; | |
| 454 | - | |
| 455 | - foreach ( $template_locations as $location ) { | |
| 456 | - $templates = templately()->theme_builder::$conditions_manager->get_templates_by_location( $location ); | |
| 457 | - | |
| 458 | - if ( empty( $templates ) ) { | |
| 459 | - continue; | |
| 460 | - } | |
| 461 | - | |
| 462 | - foreach ( $templates as $template ) { | |
| 463 | - if ( $template->platform !== 'gutenberg' ) { | |
| 464 | - continue; | |
| 465 | - } | |
| 466 | - | |
| 467 | - $post_id = $template->get_main_id(); | |
| 468 | - $post = get_post( $post_id ); | |
| 469 | - | |
| 470 | - if ( ! $post ) { | |
| 471 | - continue; | |
| 472 | - } | |
| 473 | - | |
| 474 | - // Check for cached styles | |
| 475 | - $transient_key = 'templately_header_styles_' . $post_id; | |
| 476 | - $style_handles = get_transient( $transient_key ); | |
| 477 | - | |
| 478 | - if ( false === $style_handles ) { | |
| 479 | - if ( empty( $post->post_content ) ) { | |
| 480 | - continue; | |
| 481 | - } | |
| 482 | - | |
| 483 | - // Parse blocks and extract styles | |
| 484 | - $blocks = parse_blocks( $post->post_content ); | |
| 485 | - $style_handles = []; | |
| 486 | - $this->get_block_styles_recursive( $blocks, $style_handles ); | |
| 487 | - | |
| 488 | - // Cache the handles for a long time (e.g., 1 month) | |
| 489 | - set_transient( $transient_key, $style_handles, MONTH_IN_SECONDS ); | |
| 490 | - } | |
| 491 | - | |
| 492 | - // Enqueue the styles | |
| 493 | - if ( ! empty( $style_handles ) ) { | |
| 494 | - foreach ( $style_handles as $handle ) { | |
| 495 | - wp_enqueue_style( $handle ); | |
| 496 | - } | |
| 497 | - } | |
| 498 | - } | |
| 499 | - } | |
| 500 | - } | |
| 501 | - | |
| 502 | - /** | |
| 503 | - * Recursively extract style handles for all blocks. | |
| 504 | - * | |
| 505 | - * @since 3.0.0 | |
| 506 | - * @param array $blocks Parsed blocks array. | |
| 507 | - * @param array $handles Reference to array of handles to populate. | |
| 508 | - * @return void | |
| 509 | - */ | |
| 510 | - private function get_block_styles_recursive( array $blocks, array &$handles ) { | |
| 511 | - if ( ! class_exists( 'WP_Block_Type_Registry' ) ) { | |
| 512 | - return; | |
| 513 | - } | |
| 514 | - | |
| 515 | - foreach ( $blocks as $block ) { | |
| 516 | - if ( ! empty( $block['blockName'] ) ) { | |
| 517 | - // Get registered block type | |
| 518 | - $block_type = \WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); | |
| 519 | - | |
| 520 | - if ( $block_type ) { | |
| 521 | - // Enqueue style handles (WP 5.9+) | |
| 522 | - if ( ! empty( $block_type->style_handles ) ) { | |
| 523 | - foreach ( $block_type->style_handles as $handle ) { | |
| 524 | - if ( ! in_array( $handle, $handles ) ) { | |
| 525 | - $handles[] = $handle; | |
| 526 | - } | |
| 527 | - } | |
| 528 | - } | |
| 529 | - // Legacy style property support | |
| 530 | - if ( ! empty( $block_type->style ) ) { | |
| 531 | - $styles = is_array( $block_type->style ) ? $block_type->style : [ $block_type->style ]; | |
| 532 | - foreach ( $styles as $style ) { | |
| 533 | - if ( ! in_array( $style, $handles ) ) { | |
| 534 | - $handles[] = $style; | |
| 535 | - } | |
| 536 | - } | |
| 537 | - } | |
| 538 | - } | |
| 539 | - } | |
| 540 | - | |
| 541 | - // Process inner blocks recursively | |
| 542 | - if ( ! empty( $block['innerBlocks'] ) ) { | |
| 543 | - $this->get_block_styles_recursive( $block['innerBlocks'], $handles ); | |
| 544 | - } | |
| 545 | - } | |
| 546 | - } | |
| 547 | - | |
| 548 | - | |
| 549 | - /** | |
| 550 | 376 | * @param string $location |
| 551 | 377 | * @param integer $template_id |
| 552 | 378 | */ |
| 553 | 379 | public function add_template_to_location( string $location, int $template_id ) { |
| @@ -586,19 +412,6 @@ | ||
| 586 | 412 | } |
| 587 | 413 | |
| 588 | 414 | $this->locations_printed[ $location ][ $template_id ] = $template_id; |
| 589 | 415 | $this->remove_template_from_location( $location, $template_id ); |
| 590 | - } | |
| 591 | - | |
| 592 | - /** | |
| 593 | - * Clear the header styles cache for a specific post. | |
| 594 | - * | |
| 595 | - * @param int $post_id Post ID. | |
| 596 | - * @return void | |
| 597 | - */ | |
| 598 | - public function clear_header_styles_cache( $post_id ) { | |
| 599 | - // Only clear if it might be a header template. | |
| 600 | - // Since we don't strictly know if it IS a header without checking metadata (which might be what's changing), | |
| 601 | - // we just clear the specific transient for this ID. It's harmless if the transient doesn't exist. | |
| 602 | - delete_transient( 'templately_header_styles_' . $post_id ); | |
| 603 | 416 | } |
| 604 | 417 | } |