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

Global Styles: Do not sanitize block style variations based on registration status #62405

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
49 changes: 14 additions & 35 deletions lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -745,8 +745,7 @@ public function __construct( $theme_json = array( 'version' => WP_Theme_JSON_Gut
$registry = WP_Block_Type_Registry::get_instance();
$valid_block_names = array_keys( $registry->get_all_registered() );
$valid_element_names = array_keys( static::ELEMENTS );
$valid_variations = static::get_valid_block_style_variations();
$this->theme_json = static::sanitize( $this->theme_json, $valid_block_names, $valid_element_names, $valid_variations );
$this->theme_json = static::sanitize( $this->theme_json, $valid_block_names, $valid_element_names );
$this->theme_json = static::maybe_opt_in_into_settings( $this->theme_json );

// Internally, presets are keyed by origin.
Expand Down Expand Up @@ -847,10 +846,9 @@ protected static function do_opt_in_into_settings( &$context ) {
* @param array $input Structure to sanitize.
* @param array $valid_block_names List of valid block names.
* @param array $valid_element_names List of valid element names.
* @param array $valid_variations List of valid variations per block.
* @return array The sanitized output.
*/
protected static function sanitize( $input, $valid_block_names, $valid_element_names, $valid_variations ) {
protected static function sanitize( $input, $valid_block_names, $valid_element_names ) {

$output = array();

Expand Down Expand Up @@ -927,13 +925,9 @@ protected static function sanitize( $input, $valid_block_names, $valid_element_n
$style_variation_names = array();
if (
! empty( $input['styles']['blocks'][ $block ]['variations'] ) &&
is_array( $input['styles']['blocks'][ $block ]['variations'] ) &&
isset( $valid_variations[ $block ] )
is_array( $input['styles']['blocks'][ $block ]['variations'] )
) {
$style_variation_names = array_intersect(
array_keys( $input['styles']['blocks'][ $block ]['variations'] ),
$valid_variations[ $block ]
);
$style_variation_names = array_keys( $input['styles']['blocks'][ $block ]['variations'] );
}

$schema_styles_variations = array();
Expand Down Expand Up @@ -2632,10 +2626,14 @@ private static function get_block_nodes( $theme_json, $selectors = array() ) {
$variation_selectors = array();
if ( isset( $node['variations'] ) ) {
foreach ( $node['variations'] as $variation => $node ) {
$variation_selectors[] = array(
'path' => array( 'styles', 'blocks', $name, 'variations', $variation ),
'selector' => $selectors[ $name ]['styleVariations'][ $variation ],
);
// Only add registered variations to this node's variations to
// avoid CSS generated without a selector.
if ( isset( $selectors[ $name ]['styleVariations'][ $variation ] ) ) {
Copy link
Member

Choose a reason for hiding this comment

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

So, what this PR does is:

  • do not remove the unregistered variations when we build the theme.json in memory
  • but make sure we only process the registered ones when we use them

This is the kind of thing that I mentioned: we are not removing sanitization but moving it everywhere. Each time we have to work with them, we need to make sure to use the ones that are registered.

$variation_selectors[] = array(
'path' => array( 'styles', 'blocks', $name, 'variations', $variation ),
'selector' => $selectors[ $name ]['styleVariations'][ $variation ],
);
}
}
}

Expand Down Expand Up @@ -3280,7 +3278,7 @@ protected static function filter_slugs( $node, $slugs ) {
* @since 5.9.0
* @since 6.6.0 Added support for block style variation element styles and $origin parameter.
*
* @param array $theme_json Structure to sanitize.
* @param array $theme_json Structure to sanitize.
* @param string $origin Optional. What source of data this object represents.
* One of 'blocks', 'default', 'theme', or 'custom'. Default 'theme'.
* @return array Sanitized structure.
Expand All @@ -3296,9 +3294,7 @@ public static function remove_insecure_properties( $theme_json, $origin = 'theme

$valid_block_names = array_keys( static::get_blocks_metadata() );
$valid_element_names = array_keys( static::ELEMENTS );
$valid_variations = static::get_valid_block_style_variations();

$theme_json = static::sanitize( $theme_json, $valid_block_names, $valid_element_names, $valid_variations );
$theme_json = static::sanitize( $theme_json, $valid_block_names, $valid_element_names );

$blocks_metadata = static::get_blocks_metadata();
$style_nodes = static::get_style_nodes( $theme_json, $blocks_metadata );
Expand Down Expand Up @@ -4294,21 +4290,4 @@ function ( $matches ) use ( $variation_class ) {

return implode( ',', $result );
}

/**
* Collects valid block style variations keyed by block type.
*
* @return array Valid block style variations by block type.
*/
protected static function get_valid_block_style_variations() {
$valid_variations = array();
foreach ( self::get_blocks_metadata() as $block_name => $block_meta ) {
if ( ! isset( $block_meta['styleVariations'] ) ) {
continue;
}
$valid_variations[ $block_name ] = array_keys( $block_meta['styleVariations'] );
}

return $valid_variations;
}
}
56 changes: 26 additions & 30 deletions phpunit/class-wp-theme-json-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1463,12 +1463,14 @@ public function test_get_stylesheet_generates_fluid_typography_values() {
'default'
);

$stylesheet = $theme_json->get_stylesheet();

unregister_block_type( 'test/clamp-me' );

// Results also include root site blocks styles.
$this->assertSame(
':root{--wp--preset--font-size--pickles: clamp(14px, 0.875rem + ((1vw - 3.2px) * 0.156), 16px);--wp--preset--font-size--toast: clamp(14.642px, 0.915rem + ((1vw - 3.2px) * 0.575), 22px);}:where(body) { margin: 0; }.wp-site-blocks > .alignleft { float: left; margin-right: 2em; }.wp-site-blocks > .alignright { float: right; margin-left: 2em; }.wp-site-blocks > .aligncenter { justify-content: center; margin-left: auto; margin-right: auto; }:where(.is-layout-flex){gap: 0.5em;}:where(.is-layout-grid){gap: 0.5em;}.is-layout-flow > .alignleft{float: left;margin-inline-start: 0;margin-inline-end: 2em;}.is-layout-flow > .alignright{float: right;margin-inline-start: 2em;margin-inline-end: 0;}.is-layout-flow > .aligncenter{margin-left: auto !important;margin-right: auto !important;}.is-layout-constrained > .alignleft{float: left;margin-inline-start: 0;margin-inline-end: 2em;}.is-layout-constrained > .alignright{float: right;margin-inline-start: 2em;margin-inline-end: 0;}.is-layout-constrained > .aligncenter{margin-left: auto !important;margin-right: auto !important;}.is-layout-constrained > :where(:not(.alignleft):not(.alignright):not(.alignfull)){margin-left: auto !important;margin-right: auto !important;}body .is-layout-flex{display: flex;}.is-layout-flex{flex-wrap: wrap;align-items: center;}.is-layout-flex > :is(*, div){margin: 0;}body .is-layout-grid{display: grid;}.is-layout-grid > :is(*, div){margin: 0;}:root :where(body){font-size: clamp(0.875em, 0.875rem + ((1vw - 0.2em) * 0.156), 1em);}:root :where(h1){font-size: clamp(50.171px, 3.136rem + ((1vw - 3.2px) * 3.893), 100px);}:root :where(.wp-block-test-clamp-me){font-size: clamp(27.894px, 1.743rem + ((1vw - 3.2px) * 1.571), 48px);}.has-pickles-font-size{font-size: var(--wp--preset--font-size--pickles) !important;}.has-toast-font-size{font-size: var(--wp--preset--font-size--toast) !important;}',
$theme_json->get_stylesheet()
$stylesheet
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It turned out the deregistration of the test block before generating the stylesheet was causing this test to fail on this branch. Not 100% sure why yet but I'll look into that on Monday.

);
}

Expand Down Expand Up @@ -3807,39 +3809,19 @@ public function test_sanitization() {
$this->assertEqualSetsWithIndex( $expected, $actual );
}

public function test_sanitize_for_unregistered_style_variations() {
$theme_json = new WP_Theme_JSON_Gutenberg(
array(
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
'styles' => array(
'blocks' => array(
'core/quote' => array(
'variations' => array(
'unregisteredVariation' => array(
'color' => array(
'background' => 'hotpink',
),
),
'plain' => array(
'color' => array(
'background' => 'hotpink',
),
),
),
),
),
),
)
);

$sanitized_theme_json = $theme_json->get_raw_data();
$expected = array(
public function test_unregistered_style_variations_are_omitted_from_block_style_nodes() {
$theme_json_data = array(
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
'styles' => array(
'blocks' => array(
'core/quote' => array(
'variations' => array(
'plain' => array(
'unregistered-variation' => array(
'color' => array(
'background' => 'hotpink',
),
),
'plain' => array(
'color' => array(
'background' => 'hotpink',
),
Expand All @@ -3849,7 +3831,21 @@ public function test_sanitize_for_unregistered_style_variations() {
),
),
);
$this->assertSameSetsWithIndex( $expected, $sanitized_theme_json, 'Sanitized theme.json styles does not match' );

$theme_json = new ReflectionClass( 'WP_Theme_JSON_Gutenberg' );

$func = $theme_json->getMethod( 'get_block_nodes' );
$func->setAccessible( true );

$nodes = $func->invoke( null, $theme_json_data );
$actual = $nodes[0]['variations'];
$expected = array(
array(
'path' => array( 'styles', 'blocks', 'core/quote', 'variations', 'plain' ),
'selector' => '.wp-block-quote.is-style-plain',
),
);
$this->assertSameSetsWithIndex( $expected, $actual, 'Unregistered block style variations should not be included in block node data' );
}

/**
Expand Down
Loading