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

[Proof of Concept] Optimize how shared block style variations are processed #6868

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
20 changes: 0 additions & 20 deletions src/wp-includes/block-supports/block-style-variations.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,25 +333,6 @@ function wp_resolve_block_style_variations_from_theme_json_partials( $theme_json
return wp_merge_block_style_variations_data( $variations_data, $theme_json );
}

/**
* Merges shared block style variations registered within the
* `styles.blocks.variations` property of the primary theme.json file.
*
* @since 6.6.0
* @access private
*
* @param WP_Theme_JSON_Data $theme_json Current theme.json data.
*
* @return WP_Theme_JSON_Data
*/
function wp_resolve_block_style_variations_from_primary_theme_json( $theme_json ) {
$theme_json_data = $theme_json->get_data();
$block_style_variations = $theme_json_data['styles']['blocks']['variations'] ?? array();
$variations_data = wp_resolve_block_style_variations( $block_style_variations );

return wp_merge_block_style_variations_data( $variations_data, $theme_json );
}

/**
* Merges block style variations registered via the block styles registry with a
* style object, under their appropriate block types within theme.json styles.
Expand Down Expand Up @@ -400,7 +381,6 @@ function wp_enqueue_block_style_variation_styles() {
add_action( 'wp_enqueue_scripts', 'wp_enqueue_block_style_variation_styles', 1 );

// Resolve block style variations from all their potential sources. The order here is deliberate.
add_filter( 'wp_theme_json_data_theme', 'wp_resolve_block_style_variations_from_primary_theme_json', 10, 1 );
add_filter( 'wp_theme_json_data_theme', 'wp_resolve_block_style_variations_from_theme_json_partials', 10, 1 );
add_filter( 'wp_theme_json_data_theme', 'wp_resolve_block_style_variations_from_styles_registry', 10, 1 );

Expand Down
58 changes: 58 additions & 0 deletions src/wp-includes/class-wp-theme-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,7 @@ public function __construct( $theme_json = array( 'version' => self::LATEST_SCHE
}

$this->theme_json = WP_Theme_JSON_Schema::migrate( $theme_json, $origin );
$this->theme_json = static::unwrap_shared_block_style_variations( $this->theme_json );
$valid_block_names = array_keys( static::get_blocks_metadata() );
$valid_element_names = array_keys( static::ELEMENTS );
$valid_variations = static::get_valid_block_style_variations();
Expand Down Expand Up @@ -802,6 +803,63 @@ public function __construct( $theme_json = array( 'version' => self::LATEST_SCHE
}
}

/**
* Unwraps shared block style variations.
*
* Given the following input:
*
* {
* "styles": {
* "blocks": {
* "variations": {
* "blockTypes": [ "core/paragraph", "core/group" ],
* "color": { "background": "backgroundColor" }
* }
* }
* }
* }
*
* It returns the following output:
*
* {
* "styles": {
* "blocks": {
* "variations": {
* "core/paragraph": { "color": { "background": "backgroundColor" } },
* "core/group": { "color": { "background": "backgroundColor" } }
* }
* }
* }
* }
*/
private static function unwrap_shared_block_style_variations( $theme_json ) {
$new_theme_json = $theme_json;

if ( ! isset( $new_theme_json['styles']['blocks']['variations'] ) ) {
return $new_theme_json;
}

$variations = $new_theme_json['styles']['blocks']['variations'];
foreach ( $variations as $variation_name => $data ) {
if ( ! isset( $data['blockTypes'] ) ) {
// Skip shared variations that do not declare blockTypes.
continue;
}

$block_names = $data['blockTypes'];
unset( $data['blockTypes'] );
unset( $data['title'] );
foreach ( $block_names as $block_name ) {
// TODO: what if there was existing data for the block?
$new_theme_json['styles']['blocks'][ $block_name ]['variations'][ $variation_name ] = $data;
}
}

unset( $new_theme_json['styles']['blocks']['variations'] );

return $new_theme_json;
}

/**
* Enables some opt-in settings if theme declared support.
*
Expand Down
62 changes: 62 additions & 0 deletions tests/phpunit/tests/theme/wpThemeJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
class Tests_Theme_wpThemeJson extends WP_UnitTestCase {


/**
* Administrator ID.
*
Expand Down Expand Up @@ -3930,6 +3931,67 @@ public function test_sanitize_for_unregistered_style_variations() {
$this->assertSameSetsWithIndex( $expected, $sanitized_theme_json, 'Sanitized theme.json styles does not match' );
}

/**
* @ticket 61451
* @ticket 61312
*
*/
public function test_unwraps_block_style_variations() {
register_block_style(
'core/paragraph',
array(
'name' => 'myVariation',
'label' => 'My variation',
)
);
register_block_style(
'core/group',
array(
'name' => 'myVariation',
'label' => 'My variation',
)
);

$input = new WP_Theme_JSON(
array(
'version' => 3,
'styles' => array(
'blocks' => array(
'variations' => array(
'myVariation' => array(
'title' => 'My variation',
'blockTypes' => array( 'core/paragraph', 'core/group' ),
'color' => array( 'background' => 'backgroundColor' ),
),
),
),
),
)
);
$expected = array(
'version' => WP_Theme_JSON::LATEST_SCHEMA,
'styles' => array(
'blocks' => array(
'core/paragraph' => array(
'variations' => array(
'myVariation' => array(
'color' => array( 'background' => 'backgroundColor' ),
),
),
),
'core/group' => array(
'variations' => array(
'myVariation' => array(
'color' => array( 'background' => 'backgroundColor' ),
),
),
),
),
),
);
$this->assertSameSetsWithIndex( $expected, $input->get_raw_data(), 'Unwrapped block style variations do not match' );
}

/**
* @ticket 57583
*
Expand Down
Loading