Skip to content

Commit

Permalink
Script Loader: Fix missing block style loaded with style_handle
Browse files Browse the repository at this point in the history
  • Loading branch information
petitphp committed May 24, 2024
1 parent ce84e1f commit 630c92b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/wp-includes/default-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,7 @@
add_action( 'admin_enqueue_scripts', 'wp_common_block_scripts_and_styles' );
add_action( 'enqueue_block_assets', 'wp_enqueue_registered_block_scripts_and_styles' );
add_action( 'enqueue_block_assets', 'enqueue_block_styles_assets', 30 );
add_action( 'wp_loaded', 'enqueue_block_styles_handle_assets' );
/*
* `wp_enqueue_registered_block_scripts_and_styles` is bound to both
* `enqueue_block_editor_assets` and `enqueue_block_assets` hooks
Expand Down
62 changes: 43 additions & 19 deletions src/wp-includes/script-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2662,25 +2662,6 @@ function enqueue_block_styles_assets() {

foreach ( $block_styles as $block_name => $styles ) {
foreach ( $styles as $style_properties ) {
if ( isset( $style_properties['style_handle'] ) ) {

// If the site loads separate styles per-block, enqueue the stylesheet on render.
if ( wp_should_load_separate_core_block_assets() ) {
add_filter(
'render_block',
static function ( $html, $block ) use ( $block_name, $style_properties ) {
if ( $block['blockName'] === $block_name ) {
wp_enqueue_style( $style_properties['style_handle'] );
}
return $html;
},
10,
2
);
} else {
wp_enqueue_style( $style_properties['style_handle'] );
}
}
if ( isset( $style_properties['inline_style'] ) ) {

// Default to "wp-block-library".
Expand All @@ -2702,6 +2683,49 @@ static function ( $html, $block ) use ( $block_name, $style_properties ) {
}
}

/**
* Function responsible for enqueuing the CSS files required for block styles functionality on the editor and on the frontend.
*
* @since 6.6.0
*
* @global WP_Styles $wp_styles
*/
function enqueue_block_styles_handle_assets() {
global $wp_styles;

$block_styles = WP_Block_Styles_Registry::get_instance()->get_all_registered();
foreach ( $block_styles as $block_name => $styles ) {
foreach ( $styles as $style_properties ) {
if ( ! isset( $style_properties['style_handle'] ) || ! isset( $wp_styles->registered[ $style_properties['style_handle'] ] ) ) {
continue;
}

$handle = $style_properties['style_handle'];
$enqueue_callback = function () use ( $handle ) {
wp_enqueue_style( $handle );
};

if ( wp_should_load_separate_core_block_assets() ) {
add_filter(
'render_block',
static function ( $html, $block ) use ( $block_name, $enqueue_callback ) {
if ( $block['blockName'] === $block_name ) {
$enqueue_callback();
}

return $html;
},
10,
2
);
continue;
}

add_action( 'enqueue_block_assets', $enqueue_callback );
}
}
}

/**
* Function responsible for enqueuing the assets required for block styles functionality on the editor.
*
Expand Down

0 comments on commit 630c92b

Please sign in to comment.