From 89e4eb65862878afb9c15d3a55b66deb8d2d71d9 Mon Sep 17 00:00:00 2001 From: Ramon Date: Sun, 11 Oct 2026 21:15:28 +1100 Subject: [PATCH 1/4] Global Styles: Ignore theme.json values of unexpected types Custom CSS, refs, presets, spacing scales, shared style variations and blockTypes items of the wrong type caused fatal TypeErrors when generating the global stylesheet. Skip or remove them instead. --- lib/class-wp-theme-json-gutenberg.php | 40 +++++++- phpunit/class-wp-theme-json-test.php | 126 ++++++++++++++++++++++++++ 2 files changed, 161 insertions(+), 5 deletions(-) diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index cfd3ee3455dc71..db9910fe5b8162 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -1110,7 +1110,7 @@ public function __construct( $theme_json = array( 'version' => WP_Theme_JSON_Gut // In addition to presets, spacingScale (which generates presets) is also keyed by origin. $scale_path = array( 'settings', 'spacing', 'spacingScale' ); $spacing_scale = _wp_array_get( $this->theme_json, $scale_path, null ); - if ( null !== $spacing_scale ) { + if ( is_array( $spacing_scale ) ) { // If the spacingScale is not already keyed by origin. if ( empty( array_intersect( array_keys( $spacing_scale ), static::VALID_ORIGINS ) ) ) { _wp_array_set( $this->theme_json, $scale_path, array( $origin => $spacing_scale ) ); @@ -1185,7 +1185,10 @@ private static function unwrap_shared_block_style_variations( $theme_json, $vali foreach ( $registered_variations as $variation_name ) { $block_level_data = $new_theme_json['styles']['blocks'][ $block_type ]['variations'][ $variation_name ] ?? array(); $top_level_data = $variations[ $variation_name ] ?? array(); - $merged_data = array_replace_recursive( $top_level_data, $block_level_data ); + if ( ! is_array( $block_level_data ) || ! is_array( $top_level_data ) ) { + continue; + } + $merged_data = array_replace_recursive( $top_level_data, $block_level_data ); if ( ! empty( $merged_data ) ) { _wp_array_set( $new_theme_json, array( 'styles', 'blocks', $block_type, 'variations', $variation_name ), $merged_data ); } @@ -1460,7 +1463,7 @@ protected static function sanitize( $input, $valid_block_names, $valid_element_n if ( ! empty( $input['blockTypes'] ) && is_array( $input['blockTypes'] ) ) { $variation_pseudo_selectors = array(); foreach ( $input['blockTypes'] as $variation_block_type ) { - if ( isset( static::VALID_BLOCK_PSEUDO_SELECTORS[ $variation_block_type ] ) ) { + if ( is_string( $variation_block_type ) && isset( static::VALID_BLOCK_PSEUDO_SELECTORS[ $variation_block_type ] ) ) { $variation_pseudo_selectors = array_merge( $variation_pseudo_selectors, static::VALID_BLOCK_PSEUDO_SELECTORS[ $variation_block_type ] @@ -1500,6 +1503,32 @@ protected static function sanitize( $input, $valid_block_names, $valid_element_n $result['viewport'] = static::sanitize_viewport_settings( $input[ $subtree ]['viewport'] ); } + if ( 'settings' === $subtree ) { + // Preset leaves are untyped in VALID_SETTINGS, but consumers read `$preset['slug']` as a string. + $is_valid_preset = static function ( $preset ) { + return is_array( $preset ) && ( ! isset( $preset['slug'] ) || is_scalar( $preset['slug'] ) ); + }; + foreach ( static::get_setting_nodes( array( 'settings' => $result ) ) as $node ) { + foreach ( static::PRESETS_METADATA as $preset_metadata ) { + $path = array_merge( array_slice( $node['path'], 1 ), $preset_metadata['path'] ); + $presets = _wp_array_get( $result, $path, null ); + if ( ! is_array( $presets ) ) { + continue; + } + if ( wp_is_numeric_array( $presets ) ) { + $presets = array_values( array_filter( $presets, $is_valid_preset ) ); + } else { + foreach ( static::VALID_ORIGINS as $origin ) { + if ( isset( $presets[ $origin ] ) && is_array( $presets[ $origin ] ) ) { + $presets[ $origin ] = array_values( array_filter( $presets[ $origin ], $is_valid_preset ) ); + } + } + } + _wp_array_set( $result, $path, $presets ); + } + } + } + if ( empty( $result ) ) { unset( $output[ $subtree ] ); } else { @@ -2080,7 +2109,7 @@ public function get_stylesheet( $types = array( 'variables', 'styles', 'presets' public static function process_blocks_custom_css( $css, $selector ) { $processed_css = ''; - if ( empty( $css ) ) { + if ( empty( $css ) || ! is_string( $css ) ) { return $processed_css; } @@ -3260,7 +3289,7 @@ protected static function get_property_value( $styles, $path, $theme_json = null * to a path, this converts that path into the value at that path. * For example: { "ref": "style.color.background" } => "#fff". */ - if ( is_array( $value ) && isset( $value['ref'] ) ) { + if ( is_array( $value ) && isset( $value['ref'] ) && is_string( $value['ref'] ) ) { $value_path = explode( '.', $value['ref'] ); $ref_value = _wp_array_get( $theme_json, $value_path, null ); // Background Image refs can refer to a string or an array containing a URL string. @@ -5571,6 +5600,7 @@ private static function compute_spacing_sizes( $spacing_scale ) { ! isset( $spacing_scale['mediumStep'] ) || ! is_numeric( $spacing_scale['mediumStep'] ) || ! isset( $spacing_scale['unit'] ) || + ! is_string( $spacing_scale['unit'] ) || ! isset( $spacing_scale['operator'] ) || ( '+' !== $spacing_scale['operator'] && '*' !== $spacing_scale['operator'] ) || ! isset( $spacing_scale['increment'] ) || diff --git a/phpunit/class-wp-theme-json-test.php b/phpunit/class-wp-theme-json-test.php index 712f3b623cb82e..e4e5337f5340a3 100644 --- a/phpunit/class-wp-theme-json-test.php +++ b/phpunit/class-wp-theme-json-test.php @@ -5493,6 +5493,118 @@ public function data_sanitize_with_invalid_style_variation() { ); } + /** + * @dataProvider data_get_stylesheet_ignores_values_of_unexpected_types + * + * @param array $theme_json_data The theme.json data, without the version. + */ + public function test_get_stylesheet_ignores_values_of_unexpected_types( $theme_json_data ) { + $theme_json = new WP_Theme_JSON_Gutenberg( + array_merge( array( 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA ), $theme_json_data ) + ); + + $this->assertIsString( $theme_json->get_stylesheet() ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_get_stylesheet_ignores_values_of_unexpected_types() { + return array( + 'element custom css as an object' => array( + array( 'styles' => array( 'elements' => array( 'link' => array( 'css' => array( 'color' => 'red' ) ) ) ) ), + ), + 'block custom css as a list' => array( + array( 'styles' => array( 'blocks' => array( 'core/paragraph' => array( 'css' => array( 'color: red;' ) ) ) ) ), + ), + 'ref as an array' => array( + array( 'styles' => array( 'color' => array( 'text' => array( 'ref' => array( 'styles', 'color' ) ) ) ) ), + ), + 'palette as a list of strings' => array( + array( 'settings' => array( 'color' => array( 'palette' => array( '#000', '#fff' ) ) ) ), + ), + 'palette keyed by origin with a string' => array( + array( 'settings' => array( 'color' => array( 'palette' => array( 'custom' => array( '#000' ) ) ) ) ), + ), + 'preset slug as an array' => array( + array( + 'settings' => array( + 'typography' => array( + 'fontSizes' => array( + array( + 'slug' => array( 'small' ), + 'size' => '1rem', + ), + ), + ), + ), + ), + ), + 'font families as a list of strings' => array( + array( 'settings' => array( 'typography' => array( 'fontFamilies' => array( 'Inter' ) ) ) ), + ), + 'spacing scale as a string' => array( + array( 'settings' => array( 'spacing' => array( 'spacingScale' => '1.5' ) ) ), + ), + 'spacing scale unit as an array' => array( + array( + 'settings' => array( + 'spacing' => array( + 'spacingScale' => array( + 'operator' => '*', + 'increment' => 1.5, + 'steps' => 7, + 'mediumStep' => 1.5, + 'unit' => array( 'rem' ), + ), + ), + ), + ), + ), + 'shared style variation as a string' => array( + array( 'styles' => array( 'variations' => array( 'plain' => 'red' ) ) ), + ), + 'block types item as an array' => array( + array( 'blockTypes' => array( array( 'core/button' ) ) ), + ), + ); + } + + public function test_sanitize_removes_preset_items_of_unexpected_types() { + $valid_color = array( + 'slug' => 'black', + 'color' => '#000', + 'name' => 'Black', + ); + $invalid = array( + '#fff', + array( + 'slug' => array( 'white' ), + 'color' => '#fff', + ), + ); + + $theme_json = new WP_Theme_JSON_Gutenberg( + array( + 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, + 'settings' => array( + 'color' => array( 'palette' => array_merge( $invalid, array( $valid_color ) ) ), + 'blocks' => array( + 'core/paragraph' => array( + 'color' => array( 'palette' => array( 'custom' => array_merge( $invalid, array( $valid_color ) ) ) ), + ), + ), + ), + ) + ); + $settings = $theme_json->get_settings(); + + $this->assertSame( array( $valid_color ), $settings['color']['palette']['theme'], 'Top-level preset list' ); + $this->assertSame( array( $valid_color ), $settings['blocks']['core/paragraph']['color']['palette']['custom'], 'Block-level presets keyed by origin' ); + } + /** * @dataProvider data_get_styles_for_block_with_style_variations * @@ -7276,6 +7388,20 @@ public function data_process_blocks_custom_css() { ), 'expected' => '', ), + 'css as an object' => array( + 'input' => array( + 'selector' => '.foo', + 'css' => array( 'color' => 'red' ), + ), + 'expected' => '', + ), + 'css as a list of strings' => array( + 'input' => array( + 'selector' => '.foo', + 'css' => array( 'color: red;' ), + ), + 'expected' => '', + ), 'no nested selectors' => array( 'input' => array( 'selector' => '.foo', From 021d0771d7fe2c55a80c1456e96ad4fd4b026b81 Mon Sep 17 00:00:00 2001 From: Ramon Date: Sun, 11 Oct 2026 21:18:59 +1100 Subject: [PATCH 2/4] Remove preset item filtering from sanitize() Out of scope for this change; to be handled separately. --- lib/class-wp-theme-json-gutenberg.php | 26 ---------- phpunit/class-wp-theme-json-test.php | 70 +++------------------------ 2 files changed, 7 insertions(+), 89 deletions(-) diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index db9910fe5b8162..2f47ed6de4c0eb 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -1503,32 +1503,6 @@ protected static function sanitize( $input, $valid_block_names, $valid_element_n $result['viewport'] = static::sanitize_viewport_settings( $input[ $subtree ]['viewport'] ); } - if ( 'settings' === $subtree ) { - // Preset leaves are untyped in VALID_SETTINGS, but consumers read `$preset['slug']` as a string. - $is_valid_preset = static function ( $preset ) { - return is_array( $preset ) && ( ! isset( $preset['slug'] ) || is_scalar( $preset['slug'] ) ); - }; - foreach ( static::get_setting_nodes( array( 'settings' => $result ) ) as $node ) { - foreach ( static::PRESETS_METADATA as $preset_metadata ) { - $path = array_merge( array_slice( $node['path'], 1 ), $preset_metadata['path'] ); - $presets = _wp_array_get( $result, $path, null ); - if ( ! is_array( $presets ) ) { - continue; - } - if ( wp_is_numeric_array( $presets ) ) { - $presets = array_values( array_filter( $presets, $is_valid_preset ) ); - } else { - foreach ( static::VALID_ORIGINS as $origin ) { - if ( isset( $presets[ $origin ] ) && is_array( $presets[ $origin ] ) ) { - $presets[ $origin ] = array_values( array_filter( $presets[ $origin ], $is_valid_preset ) ); - } - } - } - _wp_array_set( $result, $path, $presets ); - } - } - } - if ( empty( $result ) ) { unset( $output[ $subtree ] ); } else { diff --git a/phpunit/class-wp-theme-json-test.php b/phpunit/class-wp-theme-json-test.php index e4e5337f5340a3..c20e7a7389a3a6 100644 --- a/phpunit/class-wp-theme-json-test.php +++ b/phpunit/class-wp-theme-json-test.php @@ -5513,42 +5513,19 @@ public function test_get_stylesheet_ignores_values_of_unexpected_types( $theme_j */ public function data_get_stylesheet_ignores_values_of_unexpected_types() { return array( - 'element custom css as an object' => array( + 'element custom css as an object' => array( array( 'styles' => array( 'elements' => array( 'link' => array( 'css' => array( 'color' => 'red' ) ) ) ) ), ), - 'block custom css as a list' => array( + 'block custom css as a list' => array( array( 'styles' => array( 'blocks' => array( 'core/paragraph' => array( 'css' => array( 'color: red;' ) ) ) ) ), ), - 'ref as an array' => array( + 'ref as an array' => array( array( 'styles' => array( 'color' => array( 'text' => array( 'ref' => array( 'styles', 'color' ) ) ) ) ), ), - 'palette as a list of strings' => array( - array( 'settings' => array( 'color' => array( 'palette' => array( '#000', '#fff' ) ) ) ), - ), - 'palette keyed by origin with a string' => array( - array( 'settings' => array( 'color' => array( 'palette' => array( 'custom' => array( '#000' ) ) ) ) ), - ), - 'preset slug as an array' => array( - array( - 'settings' => array( - 'typography' => array( - 'fontSizes' => array( - array( - 'slug' => array( 'small' ), - 'size' => '1rem', - ), - ), - ), - ), - ), - ), - 'font families as a list of strings' => array( - array( 'settings' => array( 'typography' => array( 'fontFamilies' => array( 'Inter' ) ) ) ), - ), - 'spacing scale as a string' => array( + 'spacing scale as a string' => array( array( 'settings' => array( 'spacing' => array( 'spacingScale' => '1.5' ) ) ), ), - 'spacing scale unit as an array' => array( + 'spacing scale unit as an array' => array( array( 'settings' => array( 'spacing' => array( @@ -5563,48 +5540,15 @@ public function data_get_stylesheet_ignores_values_of_unexpected_types() { ), ), ), - 'shared style variation as a string' => array( + 'shared style variation as a string' => array( array( 'styles' => array( 'variations' => array( 'plain' => 'red' ) ) ), ), - 'block types item as an array' => array( + 'block types item as an array' => array( array( 'blockTypes' => array( array( 'core/button' ) ) ), ), ); } - public function test_sanitize_removes_preset_items_of_unexpected_types() { - $valid_color = array( - 'slug' => 'black', - 'color' => '#000', - 'name' => 'Black', - ); - $invalid = array( - '#fff', - array( - 'slug' => array( 'white' ), - 'color' => '#fff', - ), - ); - - $theme_json = new WP_Theme_JSON_Gutenberg( - array( - 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, - 'settings' => array( - 'color' => array( 'palette' => array_merge( $invalid, array( $valid_color ) ) ), - 'blocks' => array( - 'core/paragraph' => array( - 'color' => array( 'palette' => array( 'custom' => array_merge( $invalid, array( $valid_color ) ) ) ), - ), - ), - ), - ) - ); - $settings = $theme_json->get_settings(); - - $this->assertSame( array( $valid_color ), $settings['color']['palette']['theme'], 'Top-level preset list' ); - $this->assertSame( array( $valid_color ), $settings['blocks']['core/paragraph']['color']['palette']['custom'], 'Block-level presets keyed by origin' ); - } - /** * @dataProvider data_get_styles_for_block_with_style_variations * From 0c228381ed791b473906e860874481adbc67ac7c Mon Sep 17 00:00:00 2001 From: Ramon Date: Sun, 11 Oct 2026 21:38:04 +1100 Subject: [PATCH 3/4] Add backport changelog entry for core PR 14190 --- backport-changelog/7.2/14190.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 backport-changelog/7.2/14190.md diff --git a/backport-changelog/7.2/14190.md b/backport-changelog/7.2/14190.md new file mode 100644 index 00000000000000..596d5a726d9e57 --- /dev/null +++ b/backport-changelog/7.2/14190.md @@ -0,0 +1,3 @@ +https://github.com/WordPress/wordpress-develop/pull/14190 + +* https://github.com/WordPress/gutenberg/pull/84523 From ba8f807c5fabaf29cbb4e85ff524293d92df789b Mon Sep 17 00:00:00 2001 From: Ramon Date: Mon, 12 Oct 2026 13:26:40 +1100 Subject: [PATCH 4/4] Only output root custom CSS when it is a string --- lib/class-wp-theme-json-gutenberg.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index 2f47ed6de4c0eb..4921b3acedff46 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -2065,7 +2065,10 @@ public function get_stylesheet( $types = array( 'variables', 'styles', 'presets' // 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' ) ); + $root_css = $this->theme_json['styles']['css'] ?? ''; + if ( is_string( $root_css ) ) { + $stylesheet .= $root_css; + } } return $stylesheet;