Skip to content

Commit 6338a29

Browse files
author
Mohamed Khaled
committed
Extend conditional loading of block global styles to third-party blocks
This extends the existing conditional loading optimization for block-specific global styles from core blocks to include third-party blocks, improving performance by only loading styles for blocks actually present on the page. - Implements unified handle generation for both core and third-party blocks - Follows WordPress handle pattern: wp-block-{namespace}-{blockname} - Maintains consistent fallback behavior for edge cases - Addresses TODO comment from changeset [59823] in #61965 - Performance impact: Reduces CSS payload for sites using block plugins selectively - Fixed PHPCS whitespace violations Trac ticket: https://core.trac.wordpress.org/ticket/63805
1 parent 7a680be commit 6338a29

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

src/wp-includes/global-styles-and-settings.php

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -312,17 +312,32 @@ function wp_add_global_styles_for_blocks() {
312312
* Block-specific global styles should be attached to the global-styles handle, but
313313
* only for blocks on the page, thus we check if the block's handle is in the queue
314314
* before adding the inline style.
315-
* This conditional loading only applies to core blocks.
316-
* TODO: Explore how this could be expanded to third-party blocks as well.
315+
* This conditional loading applies to both core and third-party blocks.
317316
*/
318317
if ( isset( $metadata['name'] ) ) {
318+
$block_handle = null;
319+
319320
if ( str_starts_with( $metadata['name'], 'core/' ) ) {
320321
$block_name = str_replace( 'core/', '', $metadata['name'] );
321322
$block_handle = 'wp-block-' . $block_name;
322-
if ( in_array( $block_handle, $wp_styles->queue, true ) ) {
323-
wp_add_inline_style( $stylesheet_handle, $block_css );
324-
}
325323
} else {
324+
/*
325+
* For third-party blocks, generate the expected handle based on the block name.
326+
* Third-party blocks typically use the pattern 'namespace/block-name' and
327+
* WordPress generates handles in the format 'wp-block-namespace-block-name'.
328+
*/
329+
$block_name_parts = explode( '/', $metadata['name'] );
330+
if ( count( $block_name_parts ) === 2 ) {
331+
$namespace = $block_name_parts[0];
332+
$name = $block_name_parts[1];
333+
$block_handle = 'wp-block-' . $namespace . '-' . $name;
334+
}
335+
}
336+
337+
if ( $block_handle && in_array( $block_handle, $wp_styles->queue, true ) ) {
338+
wp_add_inline_style( $stylesheet_handle, $block_css );
339+
} elseif ( ! $block_handle ) {
340+
// Fallback for blocks with unexpected naming patterns.
326341
wp_add_inline_style( $stylesheet_handle, $block_css );
327342
}
328343
}
@@ -331,13 +346,25 @@ function wp_add_global_styles_for_blocks() {
331346
if ( ! isset( $metadata['name'] ) && ! empty( $metadata['path'] ) ) {
332347
$block_name = wp_get_block_name_from_theme_json_path( $metadata['path'] );
333348
if ( $block_name ) {
349+
$block_handle = null;
350+
334351
if ( str_starts_with( $block_name, 'core/' ) ) {
335352
$block_name = str_replace( 'core/', '', $block_name );
336353
$block_handle = 'wp-block-' . $block_name;
337-
if ( in_array( $block_handle, $wp_styles->queue, true ) ) {
338-
wp_add_inline_style( $stylesheet_handle, $block_css );
339-
}
340354
} else {
355+
// Apply the same third-party block handle generation logic.
356+
$block_name_parts = explode( '/', $block_name );
357+
if ( count( $block_name_parts ) === 2 ) {
358+
$namespace = $block_name_parts[0];
359+
$name = $block_name_parts[1];
360+
$block_handle = 'wp-block-' . $namespace . '-' . $name;
361+
}
362+
}
363+
364+
if ( $block_handle && in_array( $block_handle, $wp_styles->queue, true ) ) {
365+
wp_add_inline_style( $stylesheet_handle, $block_css );
366+
} elseif ( ! $block_handle ) {
367+
// Fallback for blocks with unexpected naming patterns.
341368
wp_add_inline_style( $stylesheet_handle, $block_css );
342369
}
343370
}

0 commit comments

Comments
 (0)