-
Notifications
You must be signed in to change notification settings - Fork 36
Enqueued assets for block template #2729
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
291d2a3
1b26459
2a086d0
77eefc3
aad2f02
0bd41ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -386,7 +386,7 @@ function ( $content ) { | |||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if ( function_exists( 'get_block_templates' ) && function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() && current_theme_supports( 'block-templates' ) ) { | ||||||||||||||||||||||||||||||||
| if ( function_exists( 'get_block_templates' ) && ( current_theme_supports( 'block-templates' ) || current_theme_supports( 'block-template-parts' ) ) ) { | ||||||||||||||||||||||||||||||||
| $this->enqueue_dependencies( 'block-templates' ); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
@@ -406,25 +406,42 @@ public function enqueue_dependencies( $post = null ) { | |||||||||||||||||||||||||||||||
| } elseif ( 'block-templates' === $post ) { | ||||||||||||||||||||||||||||||||
| global $_wp_current_template_content; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| $slugs = array(); | ||||||||||||||||||||||||||||||||
| $template_blocks = parse_blocks( $_wp_current_template_content ); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| foreach ( $template_blocks as $template_block ) { | ||||||||||||||||||||||||||||||||
| if ( 'core/template-part' === $template_block['blockName'] ) { | ||||||||||||||||||||||||||||||||
| $slugs[] = $template_block['attrs']['slug']; | ||||||||||||||||||||||||||||||||
| $content = ''; | ||||||||||||||||||||||||||||||||
| $slugs = array(); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // If we have template content (full block templates), extract template part slugs. | ||||||||||||||||||||||||||||||||
| if ( ! empty( $_wp_current_template_content ) ) { | ||||||||||||||||||||||||||||||||
| $template_blocks = parse_blocks( $_wp_current_template_content ); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| foreach ( $template_blocks as $template_block ) { | ||||||||||||||||||||||||||||||||
| if ( 'core/template-part' === $template_block['blockName'] && isset( $template_block['attrs']['slug'] ) ) { | ||||||||||||||||||||||||||||||||
| $slugs[] = $template_block['attrs']['slug']; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| $templates_parts = get_block_templates( array( 'slugs__in' => $slugs ), 'wp_template_part' ); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| foreach ( $templates_parts as $templates_part ) { | ||||||||||||||||||||||||||||||||
| if ( ! empty( $templates_part->content ) && ! empty( $templates_part->slug ) && in_array( $templates_part->slug, $slugs ) ) { | ||||||||||||||||||||||||||||||||
| $content .= $templates_part->content; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // Get the specific template parts referenced in the template. | ||||||||||||||||||||||||||||||||
| $templates_parts = get_block_templates( array( 'slug__in' => $slugs ), 'wp_template_part' ); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| foreach ( $templates_parts as $templates_part ) { | ||||||||||||||||||||||||||||||||
| if ( ! empty( $templates_part->content ) && ! empty( $templates_part->slug ) && in_array( $templates_part->slug, $slugs ) ) { | ||||||||||||||||||||||||||||||||
| $content .= $templates_part->content; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| $content .= $_wp_current_template_content; | ||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||
| // Fallback for classic themes with block-template-parts only. | ||||||||||||||||||||||||||||||||
| // Get all template parts since we can't determine which ones are used. | ||||||||||||||||||||||||||||||||
| $templates_parts = get_block_templates( array(), 'wp_template_part' ); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| foreach ( $templates_parts as $templates_part ) { | ||||||||||||||||||||||||||||||||
| if ( ! empty( $templates_part->content ) ) { | ||||||||||||||||||||||||||||||||
| $content .= $templates_part->content; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
Comment on lines
+433
to
441
|
||||||||||||||||||||||||||||||||
| // Fallback for classic themes with block-template-parts only. | |
| // Get all template parts since we can't determine which ones are used. | |
| $templates_parts = get_block_templates( array(), 'wp_template_part' ); | |
| foreach ( $templates_parts as $templates_part ) { | |
| if ( ! empty( $templates_part->content ) ) { | |
| $content .= $templates_part->content; | |
| } | |
| } | |
| // Fallback when the current template content is not available. | |
| // We cannot reliably determine which template parts are used here. | |
| // To avoid loading and parsing all template parts (which can have | |
| // significant performance implications on large sites), we skip | |
| // template-part-based dependency detection in this scenario. | |
| $content = ''; |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -577,32 +577,47 @@ public function enqueue_widgets_css() { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @access public | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public function enqueue_fse_css() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( ! ( function_exists( 'get_block_templates' ) && function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() && current_theme_supports( 'block-templates' ) ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( ! ( function_exists( 'get_block_templates' ) && ( current_theme_supports( 'block-templates' ) || current_theme_supports( 'block-template-parts' ) ) ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| global $_wp_current_template_content; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $content = ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $slugs = array(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $template_blocks = parse_blocks( $_wp_current_template_content ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $content = ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $slugs = array(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| foreach ( $template_blocks as $template_block ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( 'core/template-part' === $template_block['blockName'] ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $slugs[] = $template_block['attrs']['slug']; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // If we have template content (full block templates), extract template part slugs. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( ! empty( $_wp_current_template_content ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $template_blocks = parse_blocks( $_wp_current_template_content ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| foreach ( $template_blocks as $template_block ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( 'core/template-part' === $template_block['blockName'] && isset( $template_block['attrs']['slug'] ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $slugs[] = $template_block['attrs']['slug']; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $templates_parts = get_block_templates( array( 'slugs__in' => $slugs ), 'wp_template_part' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| foreach ( $templates_parts as $templates_part ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( ! empty( $templates_part->content ) && ! empty( $templates_part->slug ) && in_array( $templates_part->slug, $slugs ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $content .= $templates_part->content; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Get the specific template parts referenced in the template. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $templates_parts = get_block_templates( array( 'slug__in' => $slugs ), 'wp_template_part' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $templates_parts = get_block_templates( array( 'slug__in' => $slugs ), 'wp_template_part' ); | |
| $templates_parts = get_block_templates( array( 'slugs__in' => $slugs ), 'wp_template_part' ); |
Copilot
AI
Jan 19, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Loading all template parts when $_wp_current_template_content is empty could have performance implications, especially on sites with many template parts. Consider documenting this as a known limitation or exploring alternative approaches to determine which template parts are actually being used.
| // Fallback for classic themes with block-template-parts only. | |
| // Get all template parts since we can't determine which ones are used. | |
| $templates_parts = get_block_templates( array(), 'wp_template_part' ); | |
| foreach ( $templates_parts as $templates_part ) { | |
| if ( ! empty( $templates_part->content ) ) { | |
| $content .= $templates_part->content; | |
| /** | |
| * Fallback for classic themes with block-template-parts only. | |
| * | |
| * Note: This fallback loads all registered template parts when | |
| * $_wp_current_template_content is empty, because there is no reliable | |
| * way to detect which ones are actually referenced. On sites with many | |
| * template parts this may have performance implications, since all of | |
| * them are queried and parsed in order to extract CSS. | |
| * | |
| * Developers can disable this behavior by filtering | |
| * 'themeisle_gutenbergblocks_load_all_template_parts' and returning | |
| * false when they do not need CSS to be generated from template parts | |
| * in this fallback scenario. | |
| */ | |
| $load_all_template_parts = apply_filters( 'themeisle_gutenbergblocks_load_all_template_parts', true ); | |
| if ( $load_all_template_parts ) { | |
| // Get all template parts since we can't determine which ones are used. | |
| $templates_parts = get_block_templates( array(), 'wp_template_part' ); | |
| foreach ( $templates_parts as $templates_part ) { | |
| if ( ! empty( $templates_part->content ) ) { | |
| $content .= $templates_part->content; | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The parameter name should be 'slugs__in' (with an 's'), not 'slug__in'. This is inconsistent with the existing usage in the codebase (see inc/class-blocks-css.php line 134) and will cause the slug filtering to not work correctly.