Skip to content

Commit

Permalink
Deprecate gutenberg_add_global_styles_for_blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
aristath committed Mar 9, 2023
1 parent 0ccc227 commit af7feb3
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 47 deletions.
45 changes: 1 addition & 44 deletions lib/compat/wordpress-6.1/get-global-styles-and-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,48 +9,5 @@
* 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;
}

$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.
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.
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;
}
)
);
if ( isset( $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 );
}
}
}
// @TODO: Deprecate.
}
3 changes: 0 additions & 3 deletions lib/compat/wordpress-6.1/script-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ function gutenberg_enqueue_global_styles() {
wp_register_style( 'global-styles', false, array(), true, true );
wp_add_inline_style( 'global-styles', $stylesheet );
wp_enqueue_style( 'global-styles' );

// add each block as an inline css.
gutenberg_add_global_styles_for_blocks();
}

remove_action( 'wp_enqueue_scripts', 'wp_enqueue_global_styles' );
Expand Down
6 changes: 6 additions & 0 deletions lib/compat/wordpress-6.2/get-global-styles-and-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ function gutenberg_get_global_stylesheet( $types = array() ) {
}
$tree->add_stylesheet_to_rules_store( $types, $origins );
}

$block_nodes = $tree->get_styles_block_nodes();
foreach ( $block_nodes as $metadata ) {
$tree->add_styles_for_block_to_rules_store( $metadata );
}

$stylesheet = $tree->get_styles_from_rules_store();
if ( $can_use_cached ) {
wp_cache_set( $cache_key, $stylesheet, $cache_group );
Expand Down

0 comments on commit af7feb3

Please sign in to comment.