Skip to content
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
60 changes: 29 additions & 31 deletions lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Check failure on line 1815 in lib/class-wp-theme-json-gutenberg.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Line indented incorrectly; expected at least 8 tabs, found 7

Copy link
Copy Markdown
Member Author

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'] is bool(true), PHP's implicit coercion converts it to '1' and produces margin-block-start: 1. That's why trunk tests pass without any fix.

foreach ( $spacing_rule['rules'] as $css_property => $css_value ) {

Check failure on line 1816 in lib/class-wp-theme-json-gutenberg.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Line indented incorrectly; expected 8 tabs, found 7
$current_css_value = is_string( $css_value ) ? $css_value : $block_gap_value;

Check failure on line 1817 in lib/class-wp-theme-json-gutenberg.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Line indented incorrectly; expected at least 9 tabs, found 8
// Boolean blockGap (a feature flag, not a CSS value) must be cast to string

Check failure on line 1818 in lib/class-wp-theme-json-gutenberg.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Line indented incorrectly; expected at least 9 tabs, found 8
// to match PHP's implicit coercion: true => '1', false => ''.

Check failure on line 1819 in lib/class-wp-theme-json-gutenberg.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Line indented incorrectly; expected at least 9 tabs, found 8
if ( is_bool( $current_css_value ) ) {

Check failure on line 1820 in lib/class-wp-theme-json-gutenberg.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Line indented incorrectly; expected 9 tabs, found 8
$current_css_value = $current_css_value ? '1' : '';

Check failure on line 1821 in lib/class-wp-theme-json-gutenberg.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Line indented incorrectly; expected at least 10 tabs, found 9
}

Check failure on line 1822 in lib/class-wp-theme-json-gutenberg.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Line indented incorrectly; expected 9 tabs, found 8
if ( static::is_safe_css_declaration( $css_property, $current_css_value ) ) {

Check failure on line 1823 in lib/class-wp-theme-json-gutenberg.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Line indented incorrectly; expected 9 tabs, found 8
$declarations[] = array(
'name' => $css_property,
'value' => $current_css_value,
Expand All @@ -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 );
}
}
}
Expand Down Expand Up @@ -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
);
}

Expand Down Expand Up @@ -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 );
}
}
}
Expand Down Expand Up @@ -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 );
}
}

Expand Down Expand Up @@ -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()' );

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Core, this will be WP_Style_Engine::compile_css()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a protected method, does it need a _deprecated_function call, or can it just be removed? I don't see any calls to it in wordpress-develop other than internal calls within the Theme JSON class.

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? 😆)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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 );
}

/**
Expand Down Expand Up @@ -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
);
}
}
Expand Down Expand Up @@ -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'
);
}

Expand Down Expand Up @@ -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.
Expand All @@ -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'] ) );
Expand Down
18 changes: 18 additions & 0 deletions lib/compat/wordpress-7.0/kses.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,21 @@ function gutenberg_add_display_to_safe_style_css( $attr ) {
return $attr;
}
add_filter( 'safe_style_css', 'gutenberg_add_display_to_safe_style_css' );

/**
* Add box-shadow and outline sub-properties to the list of safe CSS properties.
* These are needed for outline styles support in theme.json.
*
* @param array $attr List of allowed CSS attributes.
* @return array Modified list of allowed CSS attributes.
*/
function gutenberg_add_outline_to_safe_style_css( $attr ) {
$outline_props = array( 'box-shadow', 'outline', 'outline-color', 'outline-offset', 'outline-style', 'outline-width' );
foreach ( $outline_props as $prop ) {
if ( ! in_array( $prop, $attr, true ) ) {
$attr[] = $prop;
}
}
return $attr;
}
add_filter( 'safe_style_css', 'gutenberg_add_outline_to_safe_style_css' );
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,12 @@ public function add_declaration( $property, $value ) {
return $this;
}

// Bail early if value is not a string. Prevents fatal errors from malformed block markup.
if ( ! is_string( $value ) ) {
// Bail early if value is not a string or number. Prevents fatal errors from malformed
// block markup passing arrays or objects (e.g. a nested spacing object).
// Numbers are cast to string so that valid CSS values like integer 0 are not dropped.
if ( is_numeric( $value ) ) {
$value = (string) $value;
} elseif ( ! is_string( $value ) ) {
return $this;
}
$value = trim( $value );
Expand Down
68 changes: 66 additions & 2 deletions packages/style-engine/src/class-wp-style-engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -660,8 +660,13 @@ protected static function get_url_or_value_css_declaration( $style_value, $style
/**
* Returns compiled CSS from css_declarations.
*
* @param string[] $css_declarations An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ).
* @param string $css_selector When a selector is passed, the function will return a full CSS rule `$selector { ...rules }`, otherwise a concatenated string of properties and values.
* For backwards compatibility, accepts declarations in two formats:
* - Associative array: array( 'property' => 'value', ... )
* - Theme.json format (deprecated): array( array( 'name' => 'property', 'value' => 'value' ), ... )
*
* @param string[]|array[] $css_declarations An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ),
* or theme.json format array( array( 'name' => 'property', 'value' => 'value' ), ... ).
* @param string $css_selector When a selector is passed, the function will return a full CSS rule `$selector { ...rules }`, otherwise a concatenated string of properties and values.
*
* @return string A compiled CSS string.
*/
Expand All @@ -670,6 +675,9 @@ public static function compile_css( $css_declarations, $css_selector ) {
return '';
}

// Auto-detect and convert theme.json format if needed.
$css_declarations = static::normalize_declarations( $css_declarations );

// Return an entire rule if there is a selector.
if ( $css_selector ) {
$css_rule = new WP_Style_Engine_CSS_Rule( $css_selector, $css_declarations );
Expand Down Expand Up @@ -698,5 +706,61 @@ public static function compile_stylesheet_from_css_rules( $css_rules, $options =
$processor->add_rules( $css_rules );
return $processor->get_css( $options );
}

/**
* Normalizes CSS declarations to associative array format.
*
* Detects and converts theme.json format (array of arrays with 'name' and 'value' keys)
* to associative array format. If already in associative format, returns as-is.
* Marked as private because it is only used internally, and will be removed in the future.
*
* @since 7.0.0
*
* @param array $declarations CSS declarations in either format.
* @return array Associative array of property => value pairs.
*/
private static function normalize_declarations( $declarations ) {
if ( empty( $declarations ) || ! is_array( $declarations ) ) {
return array();
}

// Check if this is theme.json format (indexed array with 'name' and 'value' keys).
$first_key = array_key_first( $declarations );
if ( is_numeric( $first_key ) && isset( $declarations[ $first_key ]['name'], $declarations[ $first_key ]['value'] ) ) {
// Convert from theme.json format to associative array.
return static::convert_theme_json_declarations( $declarations );
}

// Already in associative array format.
return $declarations;
}

/**
* Converts declarations from theme.json format to style engine format.
* Marked as private because it is only used internally, and will be removed in the future.
*
* Converts from theme.json's array-of-arrays format:
* array(array('name' => 'color', 'value' => 'red'), ...)
* To style engine's associative array format:
* array('color' => 'red', ...)
*
* @since 7.0.0
*
* @param array $declarations Array of declarations with 'name' and 'value' keys.
* @return array Associative array of property => value pairs.
*/
private static function convert_theme_json_declarations( $declarations ) {
if ( empty( $declarations ) || ! is_array( $declarations ) ) {
return array();
}

$associative = array();
foreach ( $declarations as $declaration ) {
if ( isset( $declaration['name'], $declaration['value'] ) ) {
$associative[ $declaration['name'] ] = $declaration['value'];
}
}
return $associative;
}
}
}
Loading
Loading