Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport: Gutenberg PR #44363 > Add missing blocks origin to theme.json #3319

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/wp-includes/class-wp-theme-json-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,12 +295,10 @@ public static function get_block_data() {
*
* @param WP_Theme_JSON_Data Class to access and update the underlying data.
*/
$theme_json = apply_filters( 'theme_json_blocks', new WP_Theme_JSON_Data( $config, 'core' ) );
$theme_json = apply_filters( 'theme_json_blocks', new WP_Theme_JSON_Data( $config, 'blocks' ) );
$config = $theme_json->get_data();

// Core here means it's the lower level part of the styles chain.
// It can be a core or a third-party block.
return new WP_Theme_JSON( $config, 'core' );
return new WP_Theme_JSON( $config, 'blocks' );
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/wp-includes/class-wp-theme-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ class WP_Theme_JSON {
* The sources of data this object can represent.
*
* @since 5.8.0
* @since 6.1.0 Added 'blocks'.
* @var string[]
*/
const VALID_ORIGINS = array(
'default',
'blocks',
'theme',
'custom',
);
Expand Down
23 changes: 20 additions & 3 deletions src/wp-includes/global-styles-and-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,21 @@ function wp_get_global_stylesheet( $types = array() ) {
}

/*
* If variables are part of the stylesheet,
* we add them for all origins (default, theme, user).
* If variables are part of the stylesheet, then add them.
* This is so themes without a theme.json still work as before 5.9:
* they can override the default presets.
* See https://core.trac.wordpress.org/ticket/54782
*/
$styles_variables = '';
if ( in_array( 'variables', $types, true ) ) {
$styles_variables = $tree->get_stylesheet( array( 'variables' ) );
/*
* Only use the default, theme, and custom origins. Why?
* Because styles for `blocks` origin are added at a later phase
* (i.e. in the render cycle). Here, only the ones in use are rendered.
* @see wp_add_global_styles_for_blocks
*/
$origins = array( 'default', 'theme', 'custom' );
$styles_variables = $tree->get_stylesheet( array( 'variables' ), $origins );
$types = array_diff( $types, array( 'variables' ) );
}

Expand All @@ -133,6 +139,12 @@ function wp_get_global_stylesheet( $types = array() ) {
*/
$styles_rest = '';
if ( ! empty( $types ) ) {
/*
* Only use the default, theme, and custom origins. Why?
* Because styles for `blocks` origin are added at a later phase
* (i.e. in the render cycle). Here, only the ones in use are rendered.
* @see wp_add_global_styles_for_blocks
*/
$origins = array( 'default', 'theme', 'custom' );
if ( ! $supports_theme_json ) {
$origins = array( 'default' );
Expand Down Expand Up @@ -204,6 +216,11 @@ function wp_add_global_styles_for_blocks() {
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 ( isset( $metadata['name'] ) ) {
$block_name = str_replace( 'core/', '', $metadata['name'] );
/*
Expand Down
12 changes: 6 additions & 6 deletions src/wp-includes/script-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2409,14 +2409,11 @@ function wp_enqueue_global_styles() {
}

/*
* If we are loading CSS for each block separately, then we can load the theme.json CSS conditionally.
* If loading the CSS for each block separately, then load the theme.json CSS conditionally.
* This removes the CSS from the global-styles stylesheet and adds it to the inline CSS for each block.
* This filter must be registered before calling wp_get_global_stylesheet();
*/
if ( $separate_assets ) {
add_filter( 'theme_json_get_style_nodes', 'wp_filter_out_block_nodes' );
// Add each block as an inline css.
wp_add_global_styles_for_blocks();
}
add_filter( 'theme_json_get_style_nodes', 'wp_filter_out_block_nodes' );

$stylesheet = wp_get_global_stylesheet();

Expand All @@ -2427,6 +2424,9 @@ function wp_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.
wp_add_global_styles_for_blocks();
}

/**
Expand Down