Skip to content

Commit ee2786a

Browse files
dsastellthemachines
authored andcommitted
Preserve block style variations when securing theme json (#53466)
* Preserve block style variations when securing theme json Valid and safe block style variations were being removed by `WP_Theme_JSON_Gutenberg::remove_insecure_properties` when securing the theme.json. When this was a problem varied depending upon site configuration, but out-of-the-box it was a problem for administrators on multi-site installs. This change adds explicit processing of variations in `remove_insecure_properties` so that they won't get removed. * Add another variation sanitisation test This test checks that when removing insecure properties an unknown/unsupported property is removed from the variation.
1 parent 9c23d2f commit ee2786a

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

lib/class-wp-theme-json-gutenberg.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2906,6 +2906,20 @@ public static function remove_insecure_properties( $theme_json ) {
29062906
if ( ! empty( $output ) ) {
29072907
_wp_array_set( $sanitized, $metadata['path'], $output );
29082908
}
2909+
2910+
if ( isset( $metadata['variations'] ) ) {
2911+
foreach ( $metadata['variations'] as $variation ) {
2912+
$variation_input = _wp_array_get( $theme_json, $variation['path'], array() );
2913+
if ( empty( $variation_input ) ) {
2914+
continue;
2915+
}
2916+
2917+
$variation_output = static::remove_insecure_styles( $variation_input );
2918+
if ( ! empty( $variation_output ) ) {
2919+
_wp_array_set( $sanitized, $variation['path'], $variation_output );
2920+
}
2921+
}
2922+
}
29092923
}
29102924

29112925
$setting_nodes = static::get_setting_nodes( $theme_json );

phpunit/class-wp-theme-json-test.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,6 +1702,85 @@ public function data_get_styles_for_block_with_style_variations() {
17021702
);
17031703
}
17041704

1705+
public function test_block_style_variations() {
1706+
wp_set_current_user( static::$administrator_id );
1707+
1708+
$expected = array(
1709+
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
1710+
'styles' => array(
1711+
'blocks' => array(
1712+
'core/button' => array(
1713+
'color' => array(
1714+
'background' => 'blue',
1715+
),
1716+
'variations' => array(
1717+
'outline' => array(
1718+
'color' => array(
1719+
'background' => 'purple',
1720+
),
1721+
),
1722+
),
1723+
),
1724+
),
1725+
),
1726+
);
1727+
1728+
$actual = WP_Theme_JSON_Gutenberg::remove_insecure_properties( $expected );
1729+
1730+
$this->assertSameSetsWithIndex( $expected, $actual );
1731+
}
1732+
1733+
public function test_block_style_variations_with_invalid_properties() {
1734+
wp_set_current_user( static::$administrator_id );
1735+
1736+
$partially_invalid_variation = array(
1737+
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
1738+
'styles' => array(
1739+
'blocks' => array(
1740+
'core/button' => array(
1741+
'color' => array(
1742+
'background' => 'blue',
1743+
),
1744+
'variations' => array(
1745+
'outline' => array(
1746+
'color' => array(
1747+
'background' => 'purple',
1748+
),
1749+
'invalid' => array(
1750+
'value' => 'should be stripped',
1751+
),
1752+
),
1753+
),
1754+
),
1755+
),
1756+
),
1757+
);
1758+
1759+
$expected = array(
1760+
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
1761+
'styles' => array(
1762+
'blocks' => array(
1763+
'core/button' => array(
1764+
'color' => array(
1765+
'background' => 'blue',
1766+
),
1767+
'variations' => array(
1768+
'outline' => array(
1769+
'color' => array(
1770+
'background' => 'purple',
1771+
),
1772+
),
1773+
),
1774+
),
1775+
),
1776+
),
1777+
);
1778+
1779+
$actual = WP_Theme_JSON_Gutenberg::remove_insecure_properties( $partially_invalid_variation );
1780+
1781+
$this->assertSameSetsWithIndex( $expected, $actual );
1782+
}
1783+
17051784
public function test_update_separator_declarations() {
17061785
// If only background is defined, test that includes border-color to the style so it is applied on the front end.
17071786
$theme_json = new WP_Theme_JSON_Gutenberg(

0 commit comments

Comments
 (0)