Skip to content

Commit

Permalink
Global Styles: Fix registration of theme style variation defined bloc…
Browse files Browse the repository at this point in the history
…k styles (#62495)

Co-authored-by: aaronrobertshaw <aaronrobertshaw@git.wordpress.org>
Co-authored-by: oandregal <oandregal@git.wordpress.org>
  • Loading branch information
3 people authored Jun 12, 2024
1 parent 907a70e commit bda08ff
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions backport-changelog/6.6/6756.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
https://github.com/WordPress/wordpress-develop/pull/6756

* https://github.com/WordPress/gutenberg/pull/62461
* https://github.com/WordPress/gutenberg/pull/62495
19 changes: 19 additions & 0 deletions lib/class-wp-rest-global-styles-controller-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,25 @@ protected function prepare_item_for_database( $request ) {
} elseif ( isset( $existing_config['styles'] ) ) {
$config['styles'] = $existing_config['styles'];
}

/*
* If the incoming request is going to create a new variation
* that is not yet registered, we register it here.
* This is because the variations are registered on init,
* but we want this endpoint to return the new variation immediately:
* if we don't register it, it'll be stripped out of the response
* just in this request (subsequent ones will be ok).
* Take the variations defined in styles.blocks.variations from the incoming request
* that are not part of the $existing_config.
*/
if ( isset( $request['styles']['blocks']['variations'] ) ) {
$existing_variations = isset( $existing_config['styles']['blocks']['variations'] ) ? $existing_config['styles']['blocks']['variations'] : array();
$new_variations = array_diff_key( $request['styles']['blocks']['variations'], $existing_variations );
if ( ! empty( $new_variations ) ) {
gutenberg_register_block_style_variations_from_theme_json_data( $new_variations );
}
}

if ( isset( $request['settings'] ) ) {
$config['settings'] = $request['settings'];
} elseif ( isset( $existing_config['settings'] ) ) {
Expand Down
50 changes: 50 additions & 0 deletions phpunit/class-wp-rest-global-styles-controller-gutenberg-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,56 @@ public function test_update_item_invalid_styles_css() {
$this->assertErrorResponse( 'rest_custom_css_illegal_markup', $response, 400 );
}

/**
* Tests the submission of a custom block style variation that was defined
* within a theme style variation and wouldn't be registered at the time
* of saving via the API.
*
* @since 6.6.0
*
* @covers WP_REST_Global_Styles_Controller_Gutenberg::update_item
*/
public function test_update_item_with_custom_block_style_variations() {
wp_set_current_user( self::$admin_id );
if ( is_multisite() ) {
grant_super_admin( self::$admin_id );
}

$group_variations = array(
'fromThemeStyleVariation' => array(
'color' => array(
'background' => '#ffffff',
'text' => '#000000',
),
),
);

$request = new WP_REST_Request( 'PUT', '/wp/v2/global-styles/' . self::$global_styles_id );
$request->set_body_params(
array(
'styles' => array(
'blocks' => array(
'variations' => array(
'fromThemeStyleVariation' => array(
'blockTypes' => array( 'core/group', 'core/columns' ),
'color' => array(
'background' => '#000000',
'text' => '#ffffff',
),
),
),
'core/group' => array(
'variations' => $group_variations,
),
),
),
)
);
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertSame( $group_variations, $data['styles']['blocks']['core/group']['variations'] );
}

/**
* @doesNotPerformAssertions
*/
Expand Down

1 comment on commit bda08ff

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in bda08ff.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/9477722749
📝 Reported issues:

Please sign in to comment.