From d260161183f282a8976de3e31993a584be04201e Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Tue, 31 May 2022 15:44:03 +1200 Subject: [PATCH 1/2] Account for style block nodes that have no name, eg. block element style settings from theme.json --- .../get-global-styles-and-settings.php | 35 +++++++++++++++---- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/lib/compat/wordpress-6.1/get-global-styles-and-settings.php b/lib/compat/wordpress-6.1/get-global-styles-and-settings.php index 64d6a1050791b6..8bb1203e040a11 100644 --- a/lib/compat/wordpress-6.1/get-global-styles-and-settings.php +++ b/lib/compat/wordpress-6.1/get-global-styles-and-settings.php @@ -10,14 +10,35 @@ */ function wp_add_global_styles_for_blocks() { $tree = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data(); - // TODO some nodes dont have a name... $block_nodes = $tree->get_styles_block_nodes(); foreach ( $block_nodes as $metadata ) { - $block_css = $tree->get_styles_for_block( $metadata ); - $block_name = str_replace( 'core/', '', $metadata['name'] ); - // These block styles are added on block_render. - // This hooks inline CSS to them so that they are loaded conditionally - // based on whether or not the block is used on the page. - wp_add_inline_style( 'wp-block-' . $block_name, $block_css ); + $block_css = $tree->get_styles_for_block( $metadata ); + + if ( isset( $metadata['name'] ) ) { + $block_name = str_replace( 'core/', '', $metadata['name'] ); + // These block styles are added on block_render. + // This hooks inline CSS to them so that they are loaded conditionally + // based on whether or not the block is used on the page. + wp_add_inline_style( 'wp-block-' . $block_name, $block_css ); + } + + // The likes of block element styles from theme.json do not have $metadata['name'] set. + if ( ! isset( $metadata['name'] ) && ! empty( $metadata['path'] ) ) { + $result = array_values( + array_filter( + $metadata['path'], + function ( $item ) { + if ( strpos( $item, 'core/' ) !== false ) { + return true; + } + return false; + } + ) + ); + if ( isset( $result[0] ) ) { + $block_name = str_replace( 'core/', '', $result[0] ); + wp_add_inline_style( 'wp-block-' . $block_name, $block_css ); + } + } } } From e8ba9743a8cb043cd07a6a5715c92ac1b624ded8 Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Tue, 7 Jun 2022 12:57:12 +0100 Subject: [PATCH 2/2] lint fix --- lib/compat/wordpress-6.1/get-global-styles-and-settings.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.1/get-global-styles-and-settings.php b/lib/compat/wordpress-6.1/get-global-styles-and-settings.php index 8bb1203e040a11..dcb15c948316d2 100644 --- a/lib/compat/wordpress-6.1/get-global-styles-and-settings.php +++ b/lib/compat/wordpress-6.1/get-global-styles-and-settings.php @@ -9,7 +9,7 @@ * Adds global style rules to the inline style for each block. */ function wp_add_global_styles_for_blocks() { - $tree = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data(); + $tree = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data(); $block_nodes = $tree->get_styles_block_nodes(); foreach ( $block_nodes as $metadata ) { $block_css = $tree->get_styles_for_block( $metadata );