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

Update custom CSS handling to be consistent with block global styles. #6750

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
2 changes: 1 addition & 1 deletion src/wp-includes/block-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ function get_block_editor_settings( array $custom_settings, $block_editor_contex
* entered by users does not break other global styles.
*/
$global_styles[] = array(
'css' => wp_get_global_styles_custom_css(),
'css' => wp_get_global_stylesheet( array( 'custom-css' ) ),
'__unstableType' => 'user',
'isGlobalStyles' => true,
);
Expand Down
11 changes: 10 additions & 1 deletion src/wp-includes/class-wp-theme-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -1423,6 +1423,12 @@ public function get_stylesheet( $types = array( 'variables', 'styles', 'presets'
$stylesheet .= $this->get_preset_classes( $setting_nodes, $origins );
}

// Load the custom CSS last so it has the highest specificity.
if ( in_array( 'custom-css', $types, true ) ) {
// Add the global styles root CSS.
$stylesheet .= _wp_array_get( $this->theme_json, array( 'styles', 'css' ) );
}

return $stylesheet;
}

Expand Down Expand Up @@ -1467,10 +1473,12 @@ protected function process_blocks_custom_css( $css, $selector ) {
* Returns the global styles custom CSS.
*
* @since 6.2.0
* @deprecated 6.7.0 Use {@see 'get_stylesheet'} instead.
*
* @return string The global styles custom CSS.
*/
public function get_custom_css() {
_deprecated_function( __METHOD__, '6.7.0', 'get_stylesheet' );
// Add the global styles root CSS.
$stylesheet = isset( $this->theme_json['styles']['css'] ) ? $this->theme_json['styles']['css'] : '';

Expand Down Expand Up @@ -2692,6 +2700,7 @@ private static function get_block_nodes( $theme_json, $selectors = array(), $opt
'duotone' => $duotone_selector,
'features' => $feature_selectors,
'variations' => $variation_selectors,
'css' => $selector,
);

if ( isset( $theme_json['styles']['blocks'][ $name ]['elements'] ) ) {
Expand Down Expand Up @@ -2908,7 +2917,7 @@ static function ( $pseudo_selector ) use ( $selector ) {
}
}

// 7. Generate and append any custom CSS rules pertaining to nested block style variations.
// 7. Generate and append any custom CSS rules.
if ( isset( $node['css'] ) && ! $is_root_selector ) {
$block_rules .= $this->process_blocks_custom_css( $node['css'], $selector );
}
Expand Down
3 changes: 0 additions & 3 deletions src/wp-includes/default-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -600,9 +600,6 @@
add_action( 'wp_enqueue_scripts', 'wp_enqueue_global_styles' );
add_action( 'wp_footer', 'wp_enqueue_global_styles', 1 );

// Global styles custom CSS.
add_action( 'wp_enqueue_scripts', 'wp_enqueue_global_styles_custom_css' );

// Block supports, and other styles parsed and stored in the Style Engine.
add_action( 'wp_enqueue_scripts', 'wp_enqueue_stored_styles' );
add_action( 'wp_footer', 'wp_enqueue_stored_styles', 1 );
Expand Down
77 changes: 77 additions & 0 deletions src/wp-includes/deprecated.php
Original file line number Diff line number Diff line change
Expand Up @@ -6310,3 +6310,80 @@ function wp_interactivity_process_directives_of_interactive_blocks( array $parse
_deprecated_function( __FUNCTION__, '6.6.0' );
return $parsed_block;
}

/**
* Gets the global styles custom CSS from theme.json.
*
* @since 6.2.0
* @deprecated 6.7.0 Use {@see 'wp_get_global_stylesheet'} instead.
*
* @return string The global styles custom CSS.
*/
function wp_get_global_styles_custom_css() {
_deprecated_function( __FUNCTION__, '6.7.0', 'wp_get_global_stylesheet' );
if ( ! wp_theme_has_theme_json() ) {
return '';
}
/*
* Ignore cache when the development mode is set to 'theme', so it doesn't interfere with the theme
* developer's workflow.
*/
$can_use_cached = ! wp_is_development_mode( 'theme' );

/*
* By using the 'theme_json' group, this data is marked to be non-persistent across requests.
* @see `wp_cache_add_non_persistent_groups()`.
*
* The rationale for this is to make sure derived data from theme.json
* is always fresh from the potential modifications done via hooks
* that can use dynamic data (modify the stylesheet depending on some option,
* settings depending on user permissions, etc.).
* See some of the existing hooks to modify theme.json behavior:
* @see https://make.wordpress.org/core/2022/10/10/filters-for-theme-json-data/
*
* A different alternative considered was to invalidate the cache upon certain
* events such as options add/update/delete, user meta, etc.
* It was judged not enough, hence this approach.
* @see https://github.com/WordPress/gutenberg/pull/45372
*/
$cache_key = 'wp_get_global_styles_custom_css';
$cache_group = 'theme_json';
if ( $can_use_cached ) {
$cached = wp_cache_get( $cache_key, $cache_group );
if ( $cached ) {
return $cached;
}
}

$tree = WP_Theme_JSON_Resolver::get_merged_data();
$stylesheet = $tree->get_custom_css();

if ( $can_use_cached ) {
wp_cache_set( $cache_key, $stylesheet, $cache_group );
}

return $stylesheet;
}

/**
* Enqueues the global styles custom css defined via theme.json.
*
* @since 6.2.0
* @deprecated 6.7.0 Use {@see 'wp_enqueue_global_styles'} instead.
*/
function wp_enqueue_global_styles_custom_css() {
_deprecated_function( __FUNCTION__, '6.7.0', 'wp_enqueue_global_styles' );
if ( ! wp_is_block_theme() ) {
return;
}

// Don't enqueue Customizer's custom CSS separately.
remove_action( 'wp_head', 'wp_custom_css_cb', 101 );

$custom_css = wp_get_custom_css();
$custom_css .= wp_get_global_styles_custom_css();

if ( ! empty( $custom_css ) ) {
wp_add_inline_style( 'global-styles', $custom_css );
}
}
52 changes: 0 additions & 52 deletions src/wp-includes/global-styles-and-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,58 +243,6 @@ function wp_get_global_stylesheet( $types = array() ) {
return $stylesheet;
}

/**
* Gets the global styles custom CSS from theme.json.
*
* @since 6.2.0
*
* @return string The global styles custom CSS.
*/
function wp_get_global_styles_custom_css() {
if ( ! wp_theme_has_theme_json() ) {
return '';
}
/*
* Ignore cache when the development mode is set to 'theme', so it doesn't interfere with the theme
* developer's workflow.
*/
$can_use_cached = ! wp_is_development_mode( 'theme' );

/*
* By using the 'theme_json' group, this data is marked to be non-persistent across requests.
* @see `wp_cache_add_non_persistent_groups()`.
*
* The rationale for this is to make sure derived data from theme.json
* is always fresh from the potential modifications done via hooks
* that can use dynamic data (modify the stylesheet depending on some option,
* settings depending on user permissions, etc.).
* See some of the existing hooks to modify theme.json behavior:
* @see https://make.wordpress.org/core/2022/10/10/filters-for-theme-json-data/
*
* A different alternative considered was to invalidate the cache upon certain
* events such as options add/update/delete, user meta, etc.
* It was judged not enough, hence this approach.
* @see https://github.com/WordPress/gutenberg/pull/45372
*/
$cache_key = 'wp_get_global_styles_custom_css';
$cache_group = 'theme_json';
if ( $can_use_cached ) {
$cached = wp_cache_get( $cache_key, $cache_group );
if ( $cached ) {
return $cached;
}
}

$tree = WP_Theme_JSON_Resolver::get_merged_data();
$stylesheet = $tree->get_custom_css();

if ( $can_use_cached ) {
wp_cache_set( $cache_key, $stylesheet, $cache_group );
}

return $stylesheet;
}

/**
* Adds global style rules to the inline style for each block.
*
Expand Down
35 changes: 14 additions & 21 deletions src/wp-includes/script-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2504,6 +2504,20 @@ function wp_enqueue_global_styles() {

$stylesheet = wp_get_global_stylesheet();

if ( $is_block_theme ) {
/*
* Dequeue the Customizer's custom CSS
* and add it before the global styles custom CSS.
*/
remove_action( 'wp_head', 'wp_custom_css_cb', 101 );
// Get the custom CSS from the Customizer and add it to the global stylesheet.
$custom_css = wp_get_custom_css();
$stylesheet .= $custom_css;

// Add the global styles custom CSS at the end.
$stylesheet .= wp_get_global_stylesheet( array( 'custom-css' ) );
}

if ( empty( $stylesheet ) ) {
return;
}
Expand All @@ -2516,27 +2530,6 @@ function wp_enqueue_global_styles() {
wp_add_global_styles_for_blocks();
}

/**
* Enqueues the global styles custom css defined via theme.json.
*
* @since 6.2.0
*/
function wp_enqueue_global_styles_custom_css() {
if ( ! wp_is_block_theme() ) {
return;
}

// Don't enqueue Customizer's custom CSS separately.
remove_action( 'wp_head', 'wp_custom_css_cb', 101 );

$custom_css = wp_get_custom_css();
$custom_css .= wp_get_global_styles_custom_css();

if ( ! empty( $custom_css ) ) {
wp_add_inline_style( 'global-styles', $custom_css );
}
}

/**
* Checks if the editor scripts and styles for all registered block types
* should be enqueued on the current screen.
Expand Down
40 changes: 34 additions & 6 deletions tests/phpunit/tests/theme/wpThemeJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -5035,15 +5035,34 @@ public function test_get_top_level_background_image_styles() {
}

/**
* @ticket 57536
* @ticket 61165
* Tests that base custom CSS is generated correctly.
*
* @ticket 61395
*/
public function test_get_stylesheet_handles_base_custom_css() {
$theme_json = new WP_Theme_JSON(
array(
'version' => WP_Theme_JSON::LATEST_SCHEMA,
'styles' => array(
'css' => 'body {color:purple;}',
),
)
);

$custom_css = 'body {color:purple;}';
$this->assertSame( $custom_css, $theme_json->get_stylesheet( array( 'custom-css' ) ) );
}

/**
* Tests that block custom CSS is generated correctly.
*
* @ticket 61395
*/
public function test_get_custom_css_handles_global_custom_css() {
public function test_get_styles_for_block_handles_block_custom_css() {
$theme_json = new WP_Theme_JSON(
array(
'version' => WP_Theme_JSON::LATEST_SCHEMA,
'styles' => array(
'css' => 'body {color:purple;}',
'blocks' => array(
'core/paragraph' => array(
'css' => 'color:red;',
Expand All @@ -5053,8 +5072,17 @@ public function test_get_custom_css_handles_global_custom_css() {
)
);

$custom_css = 'body {color:purple;}:root :where(p){color:red;}';
$this->assertSame( $custom_css, $theme_json->get_custom_css() );
$paragraph_node = array(
'name' => 'core/paragraph',
'path' => array( 'styles', 'blocks', 'core/paragraph' ),
'selector' => 'p',
'selectors' => array(
'root' => 'p',
),
);

$custom_css = ':root :where(p){color:red;}';
$this->assertSame( $custom_css, $theme_json->get_styles_for_block( $paragraph_node ) );
}

/**
Expand Down
Loading