From 618e7b6f4f47cdf36c1eaeed47462f03ab165891 Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Thu, 8 Sep 2022 16:35:05 +0100 Subject: [PATCH 1/5] Global Styles: Add support for third party blocks --- .../wordpress-6.1/get-global-styles-and-settings.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 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 c3b7f2b926e879..6b7dae985b7361 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 @@ -20,11 +20,16 @@ function gutenberg_add_global_styles_for_blocks() { } 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 ); + if ( 0 === strpos( $metadata['name'], 'core/' ) ) { + $block_name = str_replace( 'core/', '', $metadata['name'] ); + wp_add_inline_style( 'wp-block-' . $block_name, $block_css ); + } else { + $block_name = str_replace( '/', '-block-', $metadata['name'] ); + wp_add_inline_style( $block_name, $block_css ); + } } // The likes of block element styles from theme.json do not have $metadata['name'] set. From b8d7df5045dba0150f844d0929bdc52c9f42558d Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Fri, 9 Sep 2022 09:26:42 +0100 Subject: [PATCH 2/5] Append style to the global styles stylesheet as we can't rely on third party blocks having predictable file names --- .../wordpress-6.1/get-global-styles-and-settings.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 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 6b7dae985b7361..92643b81d98bea 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 @@ -27,8 +27,7 @@ function gutenberg_add_global_styles_for_blocks() { $block_name = str_replace( 'core/', '', $metadata['name'] ); wp_add_inline_style( 'wp-block-' . $block_name, $block_css ); } else { - $block_name = str_replace( '/', '-block-', $metadata['name'] ); - wp_add_inline_style( $block_name, $block_css ); + wp_add_inline_style( 'global-styles', $block_css ); } } @@ -46,8 +45,12 @@ function ( $item ) { ) ); if ( isset( $result[0] ) ) { - $block_name = str_replace( 'core/', '', $result[0] ); - wp_add_inline_style( 'wp-block-' . $block_name, $block_css ); + if ( 0 === strpos( $metadata['name'], 'core/' ) ) { + $block_name = str_replace( 'core/', '', $result[0] ); + wp_add_inline_style( 'wp-block-' . $block_name, $block_css ); + } else { + wp_add_inline_style( 'global-styles', $block_css ); + } } } } From 1ac1ed806fbdea832d30d3b352f431d6f390d42d Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Fri, 9 Sep 2022 11:29:45 +0100 Subject: [PATCH 3/5] Update lib/compat/wordpress-6.1/get-global-styles-and-settings.php Co-authored-by: Ari Stathopoulos --- .../wordpress-6.1/get-global-styles-and-settings.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 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 92643b81d98bea..d3fb219db661e5 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 @@ -23,12 +23,12 @@ function gutenberg_add_global_styles_for_blocks() { // 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. - if ( 0 === strpos( $metadata['name'], 'core/' ) ) { - $block_name = str_replace( 'core/', '', $metadata['name'] ); - wp_add_inline_style( 'wp-block-' . $block_name, $block_css ); - } else { - wp_add_inline_style( 'global-styles', $block_css ); + $stylesheet_handle = 'global-styles'; + if ( str_starts_with( $metadata['name'], 'core/' ) ) { + $block_name = str_replace( 'core/', '', $metadata['name'] ); + $stylesheet_handle = 'wp-block-' . $block_name; } + wp_add_inline_style( $stylesheet_handle, $block_css ); } // The likes of block element styles from theme.json do not have $metadata['name'] set. From 3a45ac351a3a738658a1f9486c5aecb1ceab29ca Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Fri, 9 Sep 2022 12:20:14 +0100 Subject: [PATCH 4/5] refactor for readability --- .../wordpress-6.1/get-global-styles-and-settings.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 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 d3fb219db661e5..e636637ae2b022 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 @@ -19,11 +19,11 @@ function gutenberg_add_global_styles_for_blocks() { continue; } + $stylesheet_handle = 'global-styles'; if ( isset( $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. - $stylesheet_handle = 'global-styles'; if ( str_starts_with( $metadata['name'], 'core/' ) ) { $block_name = str_replace( 'core/', '', $metadata['name'] ); $stylesheet_handle = 'wp-block-' . $block_name; @@ -45,12 +45,11 @@ function ( $item ) { ) ); if ( isset( $result[0] ) ) { - if ( 0 === strpos( $metadata['name'], 'core/' ) ) { + if ( str_starts_with( $metadata['name'], 'core/' ) ) { $block_name = str_replace( 'core/', '', $result[0] ); - wp_add_inline_style( 'wp-block-' . $block_name, $block_css ); - } else { - wp_add_inline_style( 'global-styles', $block_css ); + $stylesheet_handle = 'wp-block-' . $block_name; } + wp_add_inline_style( $stylesheet_handle, $block_css ); } } } From fd600490cbfa08a6e7a7f943a03de08b3609806d Mon Sep 17 00:00:00 2001 From: Alex Lende Date: Tue, 13 Sep 2022 13:26:24 -0700 Subject: [PATCH 5/5] Fix undefined array key "name" warning --- lib/compat/wordpress-6.1/get-global-styles-and-settings.php | 4 ++-- 1 file changed, 2 insertions(+), 2 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 e636637ae2b022..ccccdad9a814b9 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 @@ -45,8 +45,8 @@ function ( $item ) { ) ); if ( isset( $result[0] ) ) { - if ( str_starts_with( $metadata['name'], 'core/' ) ) { - $block_name = str_replace( 'core/', '', $result[0] ); + if ( str_starts_with( $result[0], 'core/' ) ) { + $block_name = str_replace( 'core/', '', $result[0] ); $stylesheet_handle = 'wp-block-' . $block_name; } wp_add_inline_style( $stylesheet_handle, $block_css );