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: Fix registration of theme style variation defined block styles #62495

Merged
Merged
Changes from 1 commit
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
Next Next commit
Global Styles: Fix registration of theme style variation defined bloc…
…k styles
  • Loading branch information
aaronrobertshaw committed Jun 12, 2024
commit 1afbf7e2d01d665d04933e6123f810d4e1511488
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
@@ -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'] ) ) {
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
@@ -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
*/