From 6aa111e44108f450e4da49a9c18d3d728011401c Mon Sep 17 00:00:00 2001 From: david-binda Date: Fri, 29 Oct 2021 16:18:31 +0000 Subject: [PATCH] Introduce a custom version of the enqueue_block_styles_assets function 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 #35912 and #35903 --- .../wordpress-5.9/default-theme-supports.php | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/lib/compat/wordpress-5.9/default-theme-supports.php b/lib/compat/wordpress-5.9/default-theme-supports.php index 84df5ca0d91c4..d381545139f8c 100644 --- a/lib/compat/wordpress-5.9/default-theme-supports.php +++ b/lib/compat/wordpress-5.9/default-theme-supports.php @@ -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 );