| @@ -10,11 +10,9 @@ | ||
| 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; | |
| 15 | -use Templately\Builder\TemplateLoader; | |
| 16 | -use Elementor\Core\Base\Elements_Iteration_Actions\Assets as Assets_Iteration_Action; | |
| 14 | +use ElementorPro\Plugin; | |
| 17 | 15 | |
| 18 | 16 | class LocationManager { |
| 19 | 17 | /** |
| 20 | 18 | * @var array<string, ThemeTemplate> |
| @@ -33,8 +31,12 @@ | ||
| 33 | 31 | |
| 34 | 32 | public function __construct( $builder ) { |
| 35 | 33 | $this->builder = $builder; |
| 36 | 34 | |
| 35 | + /** | |
| 36 | + * Don't know yet if we need it or not. | |
| 37 | + */ | |
| 38 | + $this->set_locations(); | |
| 37 | 39 | |
| 38 | 40 | /** |
| 39 | 41 | * Priority is 13, |
| 40 | 42 | * Because it should be run after elementor & woocommerce |
| @@ -44,22 +46,13 @@ | ||
| 44 | 46 | /** |
| 45 | 47 | * Priority is 7, |
| 46 | 48 | * Because it should run before elementor |
| 47 | 49 | */ |
| 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' ] ); | |
| 50 | + add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_styles' ], 7 ); | |
| 51 | + add_action( 'eb_frontend_assets', [ $this, 'enqueue_template_assets' ], 10, 2 ); | |
| 56 | 52 | } |
| 57 | 53 | |
| 58 | 54 | private function set_locations() { |
| 59 | - if(!empty($this->locations)) { | |
| 60 | - return; | |
| 61 | - } | |
| 62 | 55 | $this->locations = [ |
| 63 | 56 | 'header' => [ |
| 64 | 57 | 'label' => __( 'Header', 'templately' ), |
| 65 | 58 | 'multiple' => false |
| @@ -122,11 +115,8 @@ | ||
| 122 | 115 | $template = $templates_for_location[ $template_id ]; |
| 123 | 116 | $page_template = $template->get_meta( '_wp_page_template' ); |
| 124 | 117 | $platform = $template->get_platform(); |
| 125 | 118 | |
| 126 | - if( $platform === 'elementor' && !class_exists('Elementor\Plugin') ) { | |
| 127 | - return $template_path; | |
| 128 | - } | |
| 129 | 119 | if ( ! empty( $platform ) ) { |
| 130 | 120 | $page_template_module->set_platform( $platform ); |
| 131 | 121 | } |
| 132 | 122 | $path = $page_template_module->get_template_path( $page_template ); |
| @@ -179,15 +169,8 @@ | ||
| 179 | 169 | |
| 180 | 170 | if ( $document_page_template ) { |
| 181 | 171 | $page_template = $document_page_template; |
| 182 | 172 | } |
| 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 | 173 | } |
| 191 | 174 | } |
| 192 | 175 | $is_header_footer = 'header' === $location || 'footer' === $location; |
| 193 | 176 | if ( empty( $page_template ) && ! $is_header_footer ) { |
| @@ -244,13 +227,8 @@ | ||
| 244 | 227 | return $locations[ $location ] ?? []; |
| 245 | 228 | } |
| 246 | 229 | |
| 247 | 230 | public function get_locations(): array { |
| 248 | - /** | |
| 249 | - * Don't know yet if we need it or not. | |
| 250 | - */ | |
| 251 | - $this->set_locations(); | |
| 252 | - | |
| 253 | 231 | $this->register_locations(); |
| 254 | 232 | |
| 255 | 233 | return $this->locations; |
| 256 | 234 | } |
| @@ -294,12 +272,8 @@ | ||
| 294 | 272 | $this->skip_template_from_location( $location, $template_id ); |
| 295 | 273 | continue; |
| 296 | 274 | } |
| 297 | 275 | } |
| 298 | - | |
| 299 | - // Fire before printing to allow style preparation | |
| 300 | - do_action("templately_printed_location", $template_id, $location, $template); | |
| 301 | - | |
| 302 | 276 | $template->print_content(); |
| 303 | 277 | $this->did_locations[] = $location; |
| 304 | 278 | |
| 305 | 279 | $this->set_is_printed( $location, $template_id ); |
| @@ -307,25 +281,22 @@ | ||
| 307 | 281 | |
| 308 | 282 | return true; |
| 309 | 283 | } |
| 310 | 284 | |
| 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() { | |
| 285 | + public function enqueue_styles() { | |
| 286 | + | |
| 321 | 287 | if ( |
| 322 | - class_exists('ElementorPro\Modules\ThemeBuilder\Module') && | |
| 323 | - Module::is_preview() | |
| 288 | + $this->get_platform(get_the_ID()) !== 'elementor' || | |
| 289 | + ( | |
| 290 | + class_exists('ElementorPro\Modules\ThemeBuilder\Module') && | |
| 291 | + Module::is_preview() | |
| 292 | + ) | |
| 324 | 293 | ) { |
| 325 | 294 | return; |
| 326 | 295 | } |
| 327 | 296 | |
| 297 | + | |
| 298 | + | |
| 328 | 299 | $locations = $this->get_locations(); |
| 329 | 300 | |
| 330 | 301 | if ( empty( $locations ) ) { |
| 331 | 302 | return; |
| @@ -330,97 +301,53 @@ | ||
| 330 | 301 | if ( empty( $locations ) ) { |
| 331 | 302 | return; |
| 332 | 303 | } |
| 333 | 304 | |
| 305 | + // if ( ! empty( $this->current_page_template ) ) { | |
| 306 | + // $locations = $this->filter_page_template_locations( $locations ); | |
| 307 | + // } | |
| 308 | + | |
| 334 | 309 | if(class_exists('Elementor\Core\Files\CSS\Post')){ |
| 335 | 310 | $current_post_id = get_the_ID(); |
| 336 | 311 | |
| 312 | + /** @var Post_CSS[] $css_files */ | |
| 313 | + $css_files = []; | |
| 314 | + | |
| 337 | 315 | foreach ( $locations as $location => $settings ) { |
| 338 | 316 | $templates_for_location = $this->builder::$conditions_manager->get_templates_by_location( $location ); |
| 339 | 317 | |
| 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) | |
| 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) | |
| 350 | 321 | if ( $current_post_id !== $post_id ) { |
| 351 | 322 | $css_file = new Post_CSS( $post_id ); |
| 352 | - $css_file->enqueue(); | |
| 323 | + $css_files[] = $css_file; | |
| 353 | 324 | } |
| 354 | 325 | } |
| 355 | 326 | } |
| 356 | 327 | |
| 357 | - } | |
| 358 | - } | |
| 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(); | |
| 359 | 331 | |
| 360 | - private function iterate_elements( $post_id ) { | |
| 361 | - if ( ! class_exists( 'Elementor\Plugin' ) || ! class_exists( Assets_Iteration_Action::class ) ) { | |
| 362 | - return; | |
| 363 | - } | |
| 332 | + // Enqueue after the frontend styles to override them. | |
| 333 | + foreach ( $css_files as $css_file ) { | |
| 334 | + $css_file->enqueue(); | |
| 335 | + } | |
| 364 | 336 | |
| 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 ); | |
| 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' ] ); | |
| 394 | 342 | } |
| 395 | - | |
| 396 | - $elements_iteration_action->element_action( $element ); | |
| 397 | 343 | } |
| 398 | - | |
| 399 | - return $element_data; | |
| 400 | - } ); | |
| 401 | - | |
| 402 | - $elements_iteration_action->after_elements_iteration(); | |
| 344 | + } | |
| 403 | 345 | } |
| 404 | 346 | |
| 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 | - public function enqueue_template_assets() { | |
| 347 | + public function enqueue_template_assets( $path, $url ) { | |
| 421 | 348 | $using_templately_builder = get_query_var( 'using_templately_template' ); |
| 422 | - if ( ($using_templately_builder || TemplateLoader::is_header_footer()) && function_exists( 'templately' ) ) { | |
| 349 | + if ( $using_templately_builder && function_exists( 'templately' ) ) { | |
| 423 | 350 | $template_locations = [ 'header', 'footer', 'archive', 'single' ]; |
| 424 | 351 | foreach ( $template_locations as $location ) { |
| 425 | 352 | $template = templately()->theme_builder::$conditions_manager->get_templates_by_location( $location ); |
| 426 | 353 | if ( empty( $template ) ) { |
| @@ -428,73 +355,19 @@ | ||
| 428 | 355 | } |
| 429 | 356 | $template = array_pop( $template ); |
| 430 | 357 | if ( $template->platform == 'gutenberg' ) { |
| 431 | 358 | $template = is_array( $template ) ? array_pop( $template ) : $template; |
| 432 | - do_action("templately_printed_location", $template->get_main_id(), $location, $template); | |
| 433 | - } | |
| 434 | - } | |
| 435 | - } | |
| 436 | - } | |
| 437 | - | |
| 438 | - /** | |
| 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; | |
| 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 ) ); | |
| 481 | 363 | } |
| 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 ); | |
| 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 ); | |
| 496 | 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 ) ); | |
| 497 | 370 | } |
| 498 | 371 | } |
| 499 | 372 | } |
| 500 | 373 | } |
| @@ -499,55 +372,8 @@ | ||
| 499 | 372 | } |
| 500 | 373 | } |
| 501 | 374 | |
| 502 | 375 | /** |
| 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 | } |