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

Plugin: Tweaks to accommodate latest changes in core for styles loading #31702

Merged
merged 1 commit into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ function gutenberg_register_core_block_styles( $block_name ) {
$editor_style_path = "build/block-library/blocks/$block_name/style-editor.css";

if ( file_exists( gutenberg_dir_path() . $style_path ) ) {
wp_deregister_style( "wp-block-{$block_name}" );
Copy link
Member Author

Choose a reason for hiding this comment

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

Deregister styles so they can be registered properly. This is needed in WP5.8, and irrelevant in previous versions.

Copy link
Member

@gziolo gziolo May 11, 2021

Choose a reason for hiding this comment

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

gutenberg_override_style should be a nice shortcut.

wp_register_style(
"wp-block-{$block_name}",
gutenberg_url( $style_path ),
Expand All @@ -183,6 +184,7 @@ function gutenberg_register_core_block_styles( $block_name ) {
}

if ( file_exists( gutenberg_dir_path() . $editor_style_path ) ) {
wp_deregister_style( "wp-block-{$block_name}-editor" );
wp_register_style(
"wp-block-{$block_name}-editor",
gutenberg_url( $editor_style_path ),
Expand All @@ -198,10 +200,17 @@ function gutenberg_register_core_block_styles( $block_name ) {
*
* Optimizes performance and sustainability of styles by inlining smaller stylesheets.
*
* @todo Remove this function when the minimum supported version is WordPress 5.8.
*
* @return void
*/
function gutenberg_maybe_inline_styles() {

// Early exit if the "wp_maybe_inline_styles" function exists.
if ( function_exists( 'wp_maybe_inline_styles' ) ) {
return;
}
aristath marked this conversation as resolved.
Show resolved Hide resolved

$total_inline_limit = 20000;
/**
* The maximum size of inlined styles in bytes.
Expand Down
29 changes: 28 additions & 1 deletion lib/compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,22 @@
/**
* Determine if the current theme needs to load separate block styles or not.
*
* @todo Remove this function when the minimum supported version is WordPress 5.8.
*
* @return bool
*/
function gutenberg_should_load_separate_block_assets() {
$load_separate_styles = gutenberg_is_fse_theme();
if ( function_exists( 'should_load_separate_core_block_assets' ) ) {
return should_load_separate_core_block_assets();
}

if ( is_admin() || is_feed() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) {
return false;
}

// The `separate_core_block_assets` filter was added in WP 5.8.
$load_separate_styles = apply_filters( 'separate_core_block_assets', gutenberg_is_fse_theme() );

/**
* Determine if separate styles will be loaded for blocks on-render or not.
*
Expand All @@ -25,6 +37,21 @@ function gutenberg_should_load_separate_block_assets() {
return apply_filters( 'load_separate_block_assets', $load_separate_styles );
}

/**
* Opt-in to separate styles loading for block themes in WordPress 5.8.
*
* @todo Remove this function when the minimum supported version is WordPress 5.8.
*/
add_filter(
'separate_core_block_assets',
function( $load_separate_styles ) {
if ( function_exists( 'gutenberg_is_fse_theme' ) && gutenberg_is_fse_theme() ) {
return true;
}
return $load_separate_styles;
}
);
aristath marked this conversation as resolved.
Show resolved Hide resolved

/**
* Remove the `wp_enqueue_registered_block_scripts_and_styles` hook if needed.
*
Expand Down