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

Section Styles: improve performance and conceptual consistency #62712

Merged
merged 35 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
6b10584
Remove variation style registration from theme json and theme style v…
aaronrobertshaw Jun 21, 2024
fbb6775
Make variation style registration specific to partials
aaronrobertshaw Jun 21, 2024
e00fbfd
Inject partial theme.json variations before filters
aaronrobertshaw Jun 21, 2024
87b5be2
Remove existing filters that resolve shared definitions
aaronrobertshaw Jun 21, 2024
6001757
Unwrap shared variation definitions within theme json constructor
aaronrobertshaw Jun 21, 2024
82efc17
Remove retrieval of user origin data in block style variations test
aaronrobertshaw Jun 21, 2024
18c0636
Remove styles.blocks.variations from internal schema
aaronrobertshaw Jun 21, 2024
b738360
Add unit test for unwrapping shared definitions
aaronrobertshaw Jun 21, 2024
28ce75e
Fix injection of variation partials data
aaronrobertshaw Jun 21, 2024
9b41371
Fix block style registry variations
aaronrobertshaw Jun 21, 2024
ffe3db8
Make registry variations be overridden by partials again
aaronrobertshaw Jun 21, 2024
0242883
Fix global styles API test
aaronrobertshaw Jun 21, 2024
e980752
Fix copy paste docblock
aaronrobertshaw Jun 21, 2024
0d86f4b
Switch to directly registering variations from partials
aaronrobertshaw Jun 21, 2024
77e9f50
Fix typo
aaronrobertshaw Jun 21, 2024
eb78e91
Try removing blockTypes from shared variation definitions
aaronrobertshaw Jun 21, 2024
39920cc
Update testing grounds
oandregal Jun 21, 2024
5f146c8
Merge top & block level data instead of block registry data
oandregal Jun 21, 2024
d8b0335
Make linter happy
oandregal Jun 21, 2024
20f2754
Update PHP docblock
oandregal Jun 21, 2024
016f7ba
Process variations from block styles registry.
oandregal Jun 21, 2024
7b9a2fe
Make linter happy
oandregal Jun 21, 2024
4d56029
Add backport
oandregal Jun 21, 2024
2e04467
Fix minor nits
aaronrobertshaw Jun 22, 2024
681741f
Remove extraneous whitespace
aaronrobertshaw Jun 22, 2024
7626433
Add test for variation styles merge order
aaronrobertshaw Jun 22, 2024
d77a6b0
Prevent partial styles overriding registered variation for different …
aaronrobertshaw Jun 22, 2024
d781aed
Fix test_add_registered_block_styles_to_theme_data
aaronrobertshaw Jun 22, 2024
e33638f
Update unwrapping to include variations for core blocks not in registry
aaronrobertshaw Jun 22, 2024
ba38fa3
Fix typo
aaronrobertshaw Jun 23, 2024
37f6865
Add since comment for change to theme json constructor
aaronrobertshaw Jun 23, 2024
e90b542
Try relocating variations in theme.json schema
aaronrobertshaw Jun 23, 2024
af5b180
Remove title and block types from shared definitions
aaronrobertshaw Jun 24, 2024
b7013f8
Remove title from translatable theme.json schema
aaronrobertshaw Jun 24, 2024
1e0065e
Ensure block style variations are registered when retrieving theme st…
aaronrobertshaw Jun 24, 2024
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
Prev Previous commit
Next Next commit
Make registry variations be overridden by partials again
  • Loading branch information
aaronrobertshaw committed Jun 24, 2024
commit ffe3db82bedfd18bd5e41a6369022f49eebf2926
66 changes: 66 additions & 0 deletions lib/block-supports/block-style-variations.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,69 @@ function gutenberg_register_block_style_variations_from_theme_json_partials( $va
}
}
}

/**
* Merges variations data with existing theme.json data ensuring that the
* current theme.json data values take precedence.
*
* @since 6.6.0
*
* @param array $variations_data Block style variations data keyed by block type.
* @param WP_Theme_JSON_Data_Gutenberg $theme_json Current theme.json data.
* @param string $origin Origin for the theme.json data.
*
* @return WP_Theme_JSON_Gutenberg The merged theme.json data.
*/
function gutenberg_merge_block_style_variations_data( $variations_data, $theme_json, $origin = 'theme' ) {
if ( empty( $variations_data ) ) {
return $theme_json;
}

$variations_theme_json_data = array(
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
'styles' => array( 'blocks' => $variations_data ),
);

$variations_theme_json = new WP_Theme_JSON_Data_Gutenberg( $variations_theme_json_data, $origin );

/*
* Merge the current theme.json data over shared variation data so that
* any explicit per block variation values take precedence.
*/
return $variations_theme_json->update_with( $theme_json->get_data() );
}

/**
* Merges block style variations registered via the block styles registry with a
* style object, under their appropriate block types within theme.json styles.
* Any variation values defined within the theme.json specific to a block type
* will take precedence over these shared definitions.
*
* @since 6.6.0
*
* @param WP_Theme_JSON_Data_Gutenberg $theme_json Current theme.json data.
*
* @return WP_Theme_JSON_Data_Gutenberg
*/
function gutenberg_resolve_block_style_variations_from_styles_registry( $theme_json ) {
$registry = WP_Block_Styles_Registry::get_instance();
$styles = $registry->get_all_registered();
$variations_data = array();

foreach ( $styles as $block_type => $variations ) {
foreach ( $variations as $variation_name => $variation ) {
if ( ! empty( $variation['style_data'] ) ) {
$path = array( $block_type, 'variations', $variation_name );
_wp_array_set( $variations_data, $path, $variation['style_data'] );
}
}
}

return gutenberg_merge_block_style_variations_data( $variations_data, $theme_json );
}

if ( function_exists( 'wp_resolve_block_style_variations_from_styles_registry' ) ) {
remove_filter( 'wp_theme_json_data_theme', 'wp_resolve_block_style_variations_from_styles_registry' );
}

add_filter( 'wp_theme_json_data_theme', 'gutenberg_resolve_block_style_variations_from_styles_registry', 10, 1 );
40 changes: 3 additions & 37 deletions lib/class-wp-theme-json-resolver-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,15 +257,12 @@ public static function get_theme_data( $deprecated = array(), $options = array()
* When the resulting data is passed to `WP_Theme_JSON_Data_Gutenberg`
* it will create a `WP_Theme_JSON_Gutenberg` instance which in turn
* unwraps shared variations into their respective block types.
*
* Note: WP_Block_Styles_Registry defined are still merged via
* wp_theme_json_data_theme filter so partials etc can take precedence.
*/
$theme_json_data = static::inject_shared_variations_from_theme_json_partials( $theme_json_data, $variations );

/*
* Merge any block style variations registered through the WP_Block_Styles_Registry
* with a style object.
*/
$theme_json_data = static::inject_shared_variations_from_style_registry( $theme_json_data );

/**
* Filters the data provided by the theme for global styles and settings.
*
Expand Down Expand Up @@ -907,35 +904,4 @@ private static function inject_shared_variations_from_theme_json_partials( $data

return $data;
}

/**
* Adds shared block style variation definitions sourced from the WP_Block_Styles_Registry.
*
* @since 6.6.0
*
* @param array $data Array following the theme.json specification.
* @return array Theme json data including shared block style variation definitions.
*/
private static function inject_shared_variations_from_style_registry( $data ) {
$registry = WP_Block_Styles_Registry::get_instance();
$styles = $registry->get_all_registered();
$variations_data = array();

/*
* As the block style registry stores the styles per block type we don't have
* a shared variation to inject. It will go directly into the block type's variations.
*/
foreach ( $styles as $block_type => $variations ) {
foreach ( $variations as $variation_name => $variation ) {
if ( ! empty( $variation['style_data'] ) ) {
$current_variation = $theme_json_data['styles']['blocks'][ $block_type ]['variations'][ $variation_name ] ?? array();
$merged_variation = array_replace_recursive( $variation['style_data'], $current_variation );
$path = array( 'styles', 'blocks', $block_type, 'variations', $variation_name );
_wp_array_set( $data, $path, $merged_variation );
}
}
}

return $data;
}
}
5 changes: 1 addition & 4 deletions phpunit/block-supports/block-style-variations-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,6 @@ public function filter_set_theme_root() {
public function test_add_registered_block_styles_to_theme_data() {
switch_theme( 'block-theme' );

// Register theme-defined variations.
WP_Theme_JSON_Resolver_Gutenberg::get_theme_data();

$variation_styles_data = array(
'color' => array(
'background' => 'darkslateblue',
Expand Down Expand Up @@ -186,6 +183,6 @@ public function test_add_registered_block_styles_to_theme_data() {
unregister_block_style( 'core/group', 'my-variation' );
unregister_block_style( 'core/group', 'WithSlug' );

$this->assertSameSetsWithIndex( $group_styles, $expected );
$this->assertSameSetsWithIndex( $expected, $group_styles, 'Variation data does not match' );
}
}