Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce a custom version of the enqueue_block_styles_assets function #36089

Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 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,57 @@
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
*
* @see enqueue_block_styles_assets
*/
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 );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this only run if the WP version is lower than 5.8.2?
Since the patch has already been applied to core, it makes sense to only target previous versions.

Suggested change
remove_action( 'enqueue_block_assets', 'enqueue_block_styles_assets', 30 );
add_action( 'enqueue_block_assets', 'gutenberg_enqueue_block_styles_assets', 30 );
if ( version_compare( get_bloginfo( 'version' ), '5.8.2', '<' ) ) {
remove_action( 'enqueue_block_assets', 'enqueue_block_styles_assets', 30 );
add_action( 'enqueue_block_assets', 'gutenberg_enqueue_block_styles_assets', 30 );
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion @aristath ! You're right, there is no need to replace the callback by the patched version, if WordPress 5.8.2 is patched.

I have addressed that by the code you are suggesting here, and have also added an extra check for making sure the callback is only being replaced when it's actually hooked (eg.: in WordPress < 5.3 and possibly when removed by developer)