Skip to content

Commit

Permalink
Fix block style variation schemas and sanitization
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronrobertshaw committed Jan 4, 2024
1 parent 683bbec commit bfa61f1
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 9 deletions.
27 changes: 18 additions & 9 deletions lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -841,8 +841,25 @@ protected static function sanitize( $input, $valid_block_names, $valid_element_n

$schema_styles_blocks = array();
$schema_settings_blocks = array();

// Generate blocks schema without variations. They will be added later
// as each variation can support inner block styles which will need a
// schema for variation block styles that don't contain variations
// i.e. the variable block styles schema will be the same as the
// `$schema_styles_blocks` generated here.
foreach ( $valid_block_names as $block ) {
$schema_settings_blocks[ $block ] = static::VALID_SETTINGS;
$schema_styles_blocks[ $block ] = $styles_non_top_level;
$schema_styles_blocks[ $block ]['elements'] = $schema_styles_elements;
}

// Generate block style variations schema including nested block styles
// schema as generated above.
$block_style_variation_styles = static::VALID_STYLES;
$block_style_variation_styles['blocks'] = $schema_styles_blocks;
$block_style_variation_styles['elements'] = $schema_styles_elements;

foreach ( $valid_block_names as $block ) {
// Build the schema for each block style variation.
$style_variation_names = array();

if (
Expand All @@ -856,19 +873,11 @@ protected static function sanitize( $input, $valid_block_names, $valid_element_n
);
}

$schema_styles_variations = array();
$block_style_variation_styles = static::VALID_STYLES;
$block_style_variation_styles['blocks'] = null;
$block_style_variation_styles['elements'] = null;

$schema_styles_variations = array();
if ( ! empty( $style_variation_names ) ) {
$schema_styles_variations = array_fill_keys( $style_variation_names, $block_style_variation_styles );
}

$schema_settings_blocks[ $block ] = static::VALID_SETTINGS;
$schema_styles_blocks[ $block ] = $styles_non_top_level;
$schema_styles_blocks[ $block ]['elements'] = $schema_styles_elements;
$schema_styles_blocks[ $block ]['variations'] = $schema_styles_variations;
}

Expand Down
129 changes: 129 additions & 0 deletions phpunit/class-wp-theme-json-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2137,6 +2137,135 @@ public function test_block_style_variations_with_invalid_properties() {
$this->assertSameSetsWithIndex( $expected, $actual );
}

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

$partially_invalid_elements = array(
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
'styles' => array(
'blocks' => array(
'core/button' => array(
'elements' => array(
'link' => array(
'color' => array(
'text' => 'deepskyblue',
),
'invalid' => array(
'value' => 'should be stripped',
),
':hover' => array(
'color' => array(
'text' => 'cyan',
),
'invalid' => array(
'value' => 'should be stripped',
),
),
),
'invalid' => array(
'value' => 'should be stripped',
),
),
),
),
),
);

$expected = array(
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
'styles' => array(
'blocks' => array(
'core/button' => array(
'elements' => array(
'link' => array(
'color' => array(
'text' => 'deepskyblue',
),
':hover' => array(
'color' => array(
'text' => 'cyan',
),
),
),
),
),
),
),
);

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

$this->assertSameSetsWithIndex( $expected, $actual );
}
public function test_block_style_variations_with_elements_and_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/quote' => array(
'variations' => array(
'plain' => array(
'elements' => array(
'link' => array(
'color' => array(
'text' => 'deepskyblue',
),
'invalid' => array(
'value' => 'should be stripped',
),
':hover' => array(
'color' => array(
'text' => 'cyan',
),
'invalid' => array(
'value' => 'should be stripped',
),
),
),
'invalid' => array(
'value' => 'should be stripped',
),
),
),
),
),
),
),
);

$expected = array(
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
'styles' => array(
'blocks' => array(
'core/quote' => array(
'variations' => array(
'plain' => array(
'elements' => array(
'link' => array(
'color' => array(
'text' => 'deepskyblue',
),
':hover' => array(
'color' => array(
'text' => 'cyan',
),
),
),
),
),
),
),
),
),
);

$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

0 comments on commit bfa61f1

Please sign in to comment.