Repository navigation
WP_Theme_JSON: deprecate to_ruleset() and replace with Style Engine compile_css() #73117
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
Changes from all commits
9ff2c21
089d8b7
c07041d
bec015a
8f7e507
7457dc9
51016f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1812,10 +1812,15 @@ | |
| preg_match( $layout_selector_pattern, $spacing_rule['selector'] ) && | ||
| ! empty( $spacing_rule['rules'] ) | ||
| ) { | ||
| // Iterate over each of the styling rules and substitute non-string values such as `null` with the real `blockGap` value. | ||
| foreach ( $spacing_rule['rules'] as $css_property => $css_value ) { | ||
| $current_css_value = is_string( $css_value ) ? $css_value : $block_gap_value; | ||
| if ( static::is_safe_css_declaration( $css_property, $current_css_value ) ) { | ||
| // Iterate over each of the styling rules and substitute non-string values such as `null` with the real `blockGap` value. | ||
| foreach ( $spacing_rule['rules'] as $css_property => $css_value ) { | ||
| $current_css_value = is_string( $css_value ) ? $css_value : $block_gap_value; | ||
| // Boolean blockGap (a feature flag, not a CSS value) must be cast to string | ||
| // to match PHP's implicit coercion: true => '1', false => ''. | ||
| if ( is_bool( $current_css_value ) ) { | ||
| $current_css_value = $current_css_value ? '1' : ''; | ||
| } | ||
| if ( static::is_safe_css_declaration( $css_property, $current_css_value ) ) { | ||
| $declarations[] = array( | ||
| 'name' => $css_property, | ||
| 'value' => $current_css_value, | ||
|
|
@@ -1841,7 +1846,7 @@ | |
| $spacing_rule['selector'] | ||
| ); | ||
| } | ||
| $block_rules .= static::to_ruleset( $layout_selector, $declarations ); | ||
| $block_rules .= WP_Style_Engine_Gutenberg::compile_css( $declarations, $layout_selector ); | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -1873,14 +1878,14 @@ | |
| $selector, | ||
| $class_name | ||
| ); | ||
| $block_rules .= static::to_ruleset( | ||
| $layout_selector, | ||
| $block_rules .= WP_Style_Engine_Gutenberg::compile_css( | ||
| array( | ||
| array( | ||
| 'name' => 'display', | ||
| 'value' => $layout_definition['displayMode'], | ||
| ), | ||
| ) | ||
| ), | ||
| $layout_selector | ||
| ); | ||
| } | ||
|
|
||
|
|
@@ -1922,7 +1927,7 @@ | |
| $class_name, | ||
| $base_style_rule['selector'] | ||
| ); | ||
| $block_rules .= static::to_ruleset( $layout_selector, $declarations ); | ||
| $block_rules .= WP_Style_Engine_Gutenberg::compile_css( $declarations, $layout_selector ); | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -2047,7 +2052,7 @@ | |
| } | ||
|
|
||
| foreach ( $vars_by_selector as $rule_selector => $declarations ) { | ||
| $stylesheet .= static::to_ruleset( $rule_selector, $declarations ); | ||
| $stylesheet .= WP_Style_Engine_Gutenberg::compile_css( $declarations, $rule_selector ); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -2090,24 +2095,17 @@ | |
| * creates the corresponding ruleset. | ||
| * | ||
| * @since 5.8.0 | ||
| * @deprecated 7.0.0 Use WP_Style_Engine_Gutenberg::compile_css() instead. | ||
| * | ||
| * @param string $selector CSS selector. | ||
| * @param array $declarations List of declarations. | ||
| * @return string The resulting CSS ruleset. | ||
| */ | ||
| protected static function to_ruleset( $selector, $declarations ) { | ||
| if ( empty( $declarations ) ) { | ||
| return ''; | ||
| } | ||
| _deprecated_function( __METHOD__, 'Gutenberg 7.0.0', 'WP_Style_Engine_Gutenberg::compile_css()' ); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In Core, this will be
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a protected method, does it need a Or is it to protect classes (like this one in Gutenberg) that might be extending the Theme JSON class? If so, then yes, deprecating sounds safer! (Did I just answer my own question? 😆)
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a good question. I'd actually be happy to delete it. But I did it for the reason you mention (in case it's been extended) and this is despite the 4yo notice at the top of the class that says /**
* Class that encapsulates the processing of structures that adhere to the theme.json spec.
*
* This class is for internal core usage and is not supposed to be used by extenders (plugins and/or themes).
* This is a low-level API that may need to do breaking changes. Please,
* use get_global_settings, get_global_styles, and get_global_stylesheet instead.
*
* @access private
*/So do we care about folks who extended anyway? 🤔 |
||
|
|
||
| $declaration_block = array_reduce( | ||
| $declarations, | ||
| static function ( $carry, $element ) { | ||
| return $carry .= $element['name'] . ': ' . $element['value'] . ';'; }, | ||
| '' | ||
| ); | ||
|
|
||
| return $selector . '{' . $declaration_block . '}'; | ||
| // Delegate to Style Engine, which handles format detection and conversion. | ||
| return WP_Style_Engine_Gutenberg::compile_css( $declarations, $selector ); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -2143,14 +2141,14 @@ | |
|
|
||
| // $selector is often empty, so we can save ourselves the `append_to_selector()` call then. | ||
| $new_selector = '' === $selector ? $class_name : static::append_to_selector( $selector, $class_name ); | ||
| $stylesheet .= static::to_ruleset( | ||
| $new_selector, | ||
| $stylesheet .= WP_Style_Engine_Gutenberg::compile_css( | ||
| array( | ||
| array( | ||
| 'name' => $property, | ||
| 'value' => 'var(' . $css_var . ') !important', | ||
| ), | ||
| ) | ||
| ), | ||
| $new_selector | ||
| ); | ||
| } | ||
| } | ||
|
|
@@ -3355,14 +3353,14 @@ | |
| * and matches the behavior of the site editor. | ||
| */ | ||
| if ( $should_set_root_min_height ) { | ||
| $block_rules .= static::to_ruleset( | ||
| 'html', | ||
| $block_rules .= WP_Style_Engine_Gutenberg::compile_css( | ||
| array( | ||
| array( | ||
| 'name' => 'min-height', | ||
| 'value' => 'calc(100% - var(--wp-admin--admin-bar--height, 0px))', | ||
| ), | ||
| ) | ||
| ), | ||
| 'html' | ||
| ); | ||
| } | ||
|
|
||
|
|
@@ -3392,11 +3390,11 @@ | |
|
|
||
| // 2. Generate and append the rules that use the general selector. | ||
| $general_selector = $element_only_selector ? $selector : ":root :where($selector)"; | ||
| $block_rules .= static::to_ruleset( $general_selector, $declarations ); | ||
| $block_rules .= WP_Style_Engine_Gutenberg::compile_css( $declarations, $general_selector ); | ||
|
|
||
| // 3. Generate and append the rules that use the duotone selector. | ||
| if ( isset( $block_metadata['duotone'] ) && ! empty( $declarations_duotone ) ) { | ||
| $block_rules .= static::to_ruleset( $block_metadata['duotone'], $declarations_duotone ); | ||
| $block_rules .= WP_Style_Engine_Gutenberg::compile_css( $declarations_duotone, $block_metadata['duotone'] ); | ||
| } | ||
|
|
||
| // 4. Generate Layout block gap styles. | ||
|
|
@@ -3409,12 +3407,12 @@ | |
|
|
||
| // 5. Generate and append the feature level rulesets. | ||
| foreach ( $feature_declarations as $feature_selector => $individual_feature_declarations ) { | ||
| $block_rules .= static::to_ruleset( ":root :where($feature_selector)", $individual_feature_declarations ); | ||
| $block_rules .= WP_Style_Engine_Gutenberg::compile_css( $individual_feature_declarations, ":root :where($feature_selector)" ); | ||
| } | ||
|
|
||
| // 6. Generate and append the style variation rulesets. | ||
| foreach ( $style_variation_declarations as $style_variation_selector => $individual_style_variation_declarations ) { | ||
| $block_rules .= static::to_ruleset( ":root :where($style_variation_selector)", $individual_style_variation_declarations ); | ||
| $block_rules .= WP_Style_Engine_Gutenberg::compile_css( $individual_style_variation_declarations, ":root :where($style_variation_selector)" ); | ||
| if ( isset( $style_variation_layout_metadata[ $style_variation_selector ] ) ) { | ||
| $variation_data = $style_variation_layout_metadata[ $style_variation_selector ]; | ||
| $block_rules .= $this->get_layout_styles( $variation_data['metadata'], array( 'node' => $variation_data['node'] ) ); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trunk uses
to_ruleset— which builds CSS via string concatenation($dec['name'] . ': ' . $dec['value'])`.When
$dec['value']isbool(true), PHP's implicit coercion converts it to '1' and producesmargin-block-start: 1. That's why trunk tests pass without any fix.