From fb481b7e32917d50021332d6e713642993ffaac0 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Tue, 8 Jun 2021 14:03:22 +0300 Subject: [PATCH] Allow themes to add inline styles for all blocks when using lazy styles loading (#32275) * register empty styles for blocks that don't have any * add style & editorStyle in block.json where missing * only do this if the file doesn't exist * meh, missed the 2nd arg here * Use a filter instead of modifying each block.json file. * Use the register_block_type_args filter * Move filter to compat files --- lib/blocks.php | 5 ++++- lib/compat/wordpress-5.8/block-editor.php | 24 +++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/blocks.php b/lib/blocks.php index 470148668e892..68d3404a589e9 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -199,6 +199,8 @@ function gutenberg_register_core_block_styles( $block_name ) { // Add a reference to the stylesheet's path to allow calculations for inlining styles in `wp_head`. wp_style_add_data( "wp-block-{$block_name}", 'path', gutenberg_dir_path() . $style_path ); + } else { + wp_register_style( "wp-block-{$block_name}", false ); } if ( file_exists( gutenberg_dir_path() . $editor_style_path ) ) { @@ -210,6 +212,8 @@ function gutenberg_register_core_block_styles( $block_name ) { filemtime( gutenberg_dir_path() . $editor_style_path ) ); wp_style_add_data( "wp-block-{$block_name}-editor", 'rtl', 'replace' ); + } else { + wp_register_style( "wp-block-{$block_name}-editor", false ); } } @@ -466,5 +470,4 @@ function gutenberg_migrate_old_typography_shape( $metadata ) { return $metadata; } - add_filter( 'block_type_metadata', 'gutenberg_migrate_old_typography_shape' ); diff --git a/lib/compat/wordpress-5.8/block-editor.php b/lib/compat/wordpress-5.8/block-editor.php index f7efc7f1ebf6d..983889dd3fbcf 100644 --- a/lib/compat/wordpress-5.8/block-editor.php +++ b/lib/compat/wordpress-5.8/block-editor.php @@ -371,3 +371,27 @@ function gutenberg_block_editor_rest_api_preload( array $preload_paths, $block_e 'after' ); } + +/** + * Filters the arguments for registering a block type. + * + * @todo Remove from the Gutenberg plugin when WordPress 5.8 is the minimum required version. + * + * @param array $args Array of arguments for registering a block type. + * + * @return array Returns the $metadata with any missing `style` and `editorStyle` added. + */ +function gutenberg_add_missing_styles_to_core_block_json( $args ) { + if ( ! empty( $args['name'] ) && 0 === strpos( $args['name'], 'core/' ) ) { + $block_name = str_replace( 'core/', '', $args['name'] ); + + if ( ! isset( $args['style'] ) ) { + $args['style'] = "wp-block-$block_name"; + } + if ( ! isset( $args['editor_style'] ) ) { + $args['editor_style'] = "wp-block-$block_name-editor"; + } + } + return $args; +} +add_filter( 'register_block_type_args', 'gutenberg_add_missing_styles_to_core_block_json' );