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

Preserve block style variations when securing theme json #53466

Merged
merged 2 commits into from
Aug 11, 2023
Merged
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
14 changes: 14 additions & 0 deletions lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -2900,6 +2900,20 @@ public static function remove_insecure_properties( $theme_json ) {
if ( ! empty( $output ) ) {
_wp_array_set( $sanitized, $metadata['path'], $output );
}

if ( isset( $metadata['variations'] ) ) {
foreach ( $metadata['variations'] as $variation ) {
$variation_input = _wp_array_get( $theme_json, $variation['path'], array() );
if ( empty( $variation_input ) ) {
continue;
}

$variation_output = static::remove_insecure_styles( $variation_input );
if ( ! empty( $variation_output ) ) {
_wp_array_set( $sanitized, $variation['path'], $variation_output );
}
}
}
}

$setting_nodes = static::get_setting_nodes( $theme_json );
Expand Down
79 changes: 79 additions & 0 deletions phpunit/class-wp-theme-json-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1702,6 +1702,85 @@ public function data_get_styles_for_block_with_style_variations() {
);
}

public function test_block_style_variations() {
wp_set_current_user( static::$administrator_id );

$expected = array(
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
'styles' => array(
'blocks' => array(
'core/button' => array(
'color' => array(
'background' => 'blue',
),
'variations' => array(
'outline' => array(
'color' => array(
'background' => 'purple',
Copy link
Contributor

Choose a reason for hiding this comment

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

Not a blocker, but to be extra thorough about the test, would it be worth adding an invalid property to the outline array, to test that it correctly gets removed by the call to remove_insecure_styles?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cheers @andrewserong I've added another test for that

),
),
),
),
),
),
);

$actual = WP_Theme_JSON_Gutenberg::remove_insecure_properties( $expected );

$this->assertSameSetsWithIndex( $expected, $actual );
}

public function test_block_style_variations_with_invalid_properties() {
wp_set_current_user( static::$administrator_id );

$partially_invalid_variation = array(
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
'styles' => array(
'blocks' => array(
'core/button' => array(
'color' => array(
'background' => 'blue',
),
'variations' => array(
'outline' => array(
'color' => array(
'background' => 'purple',
),
'invalid' => array(
'value' => 'should be stripped',
),
),
),
),
),
),
);

$expected = array(
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
'styles' => array(
'blocks' => array(
'core/button' => array(
'color' => array(
'background' => 'blue',
),
'variations' => array(
'outline' => array(
'color' => array(
'background' => 'purple',
),
),
),
),
),
),
);

$actual = WP_Theme_JSON_Gutenberg::remove_insecure_properties( $partially_invalid_variation );

$this->assertSameSetsWithIndex( $expected, $actual );
}

public function test_update_separator_declarations() {
// If only background is defined, test that includes border-color to the style so it is applied on the front end.
$theme_json = new WP_Theme_JSON_Gutenberg(
Expand Down
Loading