Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Fix: load blocks style separately for classic themes #10758

Merged
merged 10 commits into from
Sep 8, 2023
30 changes: 30 additions & 0 deletions src/BlockTypes/AbstractBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,36 @@ protected function register_block_type() {
}

$metadata_path = $this->asset_api->get_block_metadata_path( $this->block_name );

/**
* We want to load blocks style separatedly, always, for every themes.
* When the core assets are loaded separately, other blocks' styles get
* enqueue separately too. Thus we only need to handle the remaining
* case.
*/
dinhtungdu marked this conversation as resolved.
Show resolved Hide resolved
if (
! wc_current_theme_is_fse_theme() &&
$block_settings['style'] &&
(
! function_exists( 'wp_should_load_separate_core_block_assets' ) ||
! wp_should_load_separate_core_block_assets()
)
) {
$style_handles = $block_settings['style'];
$block_settings['style'] = null;
add_filter(
'render_block',
function( $html, $block ) use ( $block_name, $style_handles ) {
if ( $block['blockName'] === $this->get_block_type() ) {
array_map( 'wp_enqueue_style', $style_handles );
}
return $html;
},
10,
2
);
}

// Prefer to register with metadata if the path is set in the block's class.
if ( ! empty( $metadata_path ) ) {
register_block_type_from_metadata(
Expand Down
Loading