Skip to content

Commit

Permalink
Introduce a custom version of the enqueue_block_styles_assets function
Browse files Browse the repository at this point in the history
The WordPress `enqueue_block_styles_assets` function contains a bug
which may lead to fatal errors for full site editing enabled themes
which register custom styles and are missing a HTML template for
request (for instance 404.html).

The bug was patched in WordPress core ( see
https://core.trac.wordpress.org/ticket/54323 ), but since the Gutenberg
11.8.0 introduces the following line:

```
add_filter( 'should_load_separate_core_block_assets', '__return_true' );
```

it also should patch the issue, in order to prevent fatal errors for the
themes matching the above mentioned criteria.

See WordPress#35912 and WordPress#35903
  • Loading branch information
david-binda committed Oct 29, 2021
1 parent f7ef4f3 commit 6aa111e
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions lib/compat/wordpress-5.9/default-theme-supports.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,55 @@
add_theme_support( 'automatic-feed-links' );
add_filter( 'should_load_separate_core_block_assets', '__return_true' );
}

/**
* Overrides the WordPress enqueue_block_styles_assets function, which contains
* a bug eventually leading to a fatal error for full site editing enabled themes
* which do register custom block styles and don't have a template for requested
* template (eg.: 404)
*
* More details on the bug can be found in: https://core.trac.wordpress.org/ticket/54323
*/
function gutenberg_enqueue_block_styles_assets() {
$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'] ) ) {

// 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',
function( $html ) use ( $style_properties ) {
wp_enqueue_style( $style_properties['style_handle'] );
return $html;
}
);
} else {
wp_enqueue_style( $style_properties['style_handle'] );
}
}
if ( isset( $style_properties['inline_style'] ) ) {

// Default to "wp-block-library".
$handle = 'wp-block-library';

// If the site loads separate styles per-block, check if the block has a stylesheet registered.
if ( wp_should_load_separate_core_block_assets() ) {
$block_stylesheet_handle = generate_block_asset_handle( $block_name, 'style' );
global $wp_styles;
if ( isset( $wp_styles->registered[ $block_stylesheet_handle ] ) ) {
$handle = $block_stylesheet_handle;
}
}

// Add inline styles to the calculated handle.
wp_add_inline_style( $handle, $style_properties['inline_style'] );
}
}
}
}

remove_action( 'enqueue_block_assets', 'enqueue_block_styles_assets', 30 );
add_action( 'enqueue_block_assets', 'gutenberg_enqueue_block_styles_assets', 30 );

0 comments on commit 6aa111e

Please sign in to comment.