From 7bee925319f4edee14bf86f19e40a0d68694c529 Mon Sep 17 00:00:00 2001 From: Tetsuaki Hamano Date: Sat, 10 Sep 2022 23:35:47 +0900 Subject: [PATCH 1/3] Add redeclaration check of `wp_add_global_styles_for_blocks` --- .../get-global-styles-and-settings.php | 70 ++++++++++--------- 1 file changed, 36 insertions(+), 34 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 32fde3442577b..ac40ff04903fe 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 @@ -5,44 +5,46 @@ * @package gutenberg */ -/** - * 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(); - $block_nodes = $tree->get_styles_block_nodes(); - foreach ( $block_nodes as $metadata ) { - $block_css = $tree->get_styles_for_block( $metadata ); +if ( ! function_exists( 'wp_add_global_styles_for_blocks' ) ) { + /** + * 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(); + $block_nodes = $tree->get_styles_block_nodes(); + foreach ( $block_nodes as $metadata ) { + $block_css = $tree->get_styles_for_block( $metadata ); - if ( ! wp_should_load_separate_core_block_assets() ) { - wp_add_inline_style( 'global-styles', $block_css ); - continue; - } + if ( ! wp_should_load_separate_core_block_assets() ) { + wp_add_inline_style( 'global-styles', $block_css ); + continue; + } - 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 ( 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 ( str_contains( $item, 'core/' ) ) { - return true; + // 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 ( str_contains( $item, 'core/' ) ) { + return true; + } + return false; } - return false; - } - ) - ); - if ( isset( $result[0] ) ) { - $block_name = str_replace( 'core/', '', $result[0] ); - wp_add_inline_style( 'wp-block-' . $block_name, $block_css ); + ) + ); + if ( isset( $result[0] ) ) { + $block_name = str_replace( 'core/', '', $result[0] ); + wp_add_inline_style( 'wp-block-' . $block_name, $block_css ); + } } } } From e74631d525c05bf68ff06d65dc4fac9d2b27c992 Mon Sep 17 00:00:00 2001 From: Tetsuaki Hamano Date: Sun, 11 Sep 2022 00:09:18 +0900 Subject: [PATCH 2/3] Add skip redirection --- lib/compat/wordpress-6.1/script-loader.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.1/script-loader.php b/lib/compat/wordpress-6.1/script-loader.php index 63c84f66075de..0aaf4103b1a6c 100644 --- a/lib/compat/wordpress-6.1/script-loader.php +++ b/lib/compat/wordpress-6.1/script-loader.php @@ -156,7 +156,9 @@ function gutenberg_enqueue_global_styles() { wp_enqueue_style( 'global-styles' ); // add each block as an inline css. - wp_add_global_styles_for_blocks(); + if ( ! function_exists( 'wp_add_global_styles_for_blocks' ) ) { + wp_add_global_styles_for_blocks(); + } } remove_action( 'wp_enqueue_scripts', 'wp_enqueue_global_styles' ); From 787f6cd57a24f4d18e547c33daec50a01248d5ed Mon Sep 17 00:00:00 2001 From: Tetsuaki Hamano Date: Mon, 12 Sep 2022 09:58:06 +0900 Subject: [PATCH 3/3] Use gutenberg_ prefix --- .../get-global-styles-and-settings.php | 70 +++++++++---------- lib/compat/wordpress-6.1/script-loader.php | 4 +- 2 files changed, 35 insertions(+), 39 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 ac40ff04903fe..c3b7f2b926e87 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 @@ -5,46 +5,44 @@ * @package gutenberg */ -if ( ! function_exists( 'wp_add_global_styles_for_blocks' ) ) { - /** - * 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(); - $block_nodes = $tree->get_styles_block_nodes(); - foreach ( $block_nodes as $metadata ) { - $block_css = $tree->get_styles_for_block( $metadata ); +/** + * Adds global style rules to the inline style for each block. + */ +function gutenberg_add_global_styles_for_blocks() { + $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 ); - if ( ! wp_should_load_separate_core_block_assets() ) { - wp_add_inline_style( 'global-styles', $block_css ); - continue; - } + if ( ! wp_should_load_separate_core_block_assets() ) { + wp_add_inline_style( 'global-styles', $block_css ); + continue; + } - 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 ( 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 ( str_contains( $item, 'core/' ) ) { - return true; - } - return false; + // 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 ( str_contains( $item, 'core/' ) ) { + return true; } - ) - ); - if ( isset( $result[0] ) ) { - $block_name = str_replace( 'core/', '', $result[0] ); - wp_add_inline_style( 'wp-block-' . $block_name, $block_css ); - } + return false; + } + ) + ); + if ( isset( $result[0] ) ) { + $block_name = str_replace( 'core/', '', $result[0] ); + wp_add_inline_style( 'wp-block-' . $block_name, $block_css ); } } } diff --git a/lib/compat/wordpress-6.1/script-loader.php b/lib/compat/wordpress-6.1/script-loader.php index 0aaf4103b1a6c..e6a42faf932f8 100644 --- a/lib/compat/wordpress-6.1/script-loader.php +++ b/lib/compat/wordpress-6.1/script-loader.php @@ -156,9 +156,7 @@ function gutenberg_enqueue_global_styles() { wp_enqueue_style( 'global-styles' ); // add each block as an inline css. - if ( ! function_exists( 'wp_add_global_styles_for_blocks' ) ) { - wp_add_global_styles_for_blocks(); - } + gutenberg_add_global_styles_for_blocks(); } remove_action( 'wp_enqueue_scripts', 'wp_enqueue_global_styles' );