diff --git a/backport-changelog/6.6/6756.md b/backport-changelog/6.6/6756.md index e60e128bf45cb..688b258c095fa 100644 --- a/backport-changelog/6.6/6756.md +++ b/backport-changelog/6.6/6756.md @@ -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 diff --git a/lib/class-wp-rest-global-styles-controller-gutenberg.php b/lib/class-wp-rest-global-styles-controller-gutenberg.php index 9bbca1ffcab40..aadcb2fd7de1e 100644 --- a/lib/class-wp-rest-global-styles-controller-gutenberg.php +++ b/lib/class-wp-rest-global-styles-controller-gutenberg.php @@ -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'] ) ) { diff --git a/phpunit/class-wp-rest-global-styles-controller-gutenberg-test.php b/phpunit/class-wp-rest-global-styles-controller-gutenberg-test.php index f5b216a084a4c..66a236c1c40a9 100644 --- a/phpunit/class-wp-rest-global-styles-controller-gutenberg-test.php +++ b/phpunit/class-wp-rest-global-styles-controller-gutenberg-test.php @@ -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 */