diff --git a/backport-changelog/7.3/14094.md b/backport-changelog/7.3/14094.md new file mode 100644 index 00000000000000..af2f1e32bd6b82 --- /dev/null +++ b/backport-changelog/7.3/14094.md @@ -0,0 +1,7 @@ +https://github.com/WordPress/wordpress-develop/pull/14094 + +* https://github.com/WordPress/gutenberg/pull/84205 +* https://github.com/WordPress/gutenberg/pull/84206 +* https://github.com/WordPress/gutenberg/pull/84207 +* https://github.com/WordPress/gutenberg/pull/84208 +* https://github.com/WordPress/gutenberg/pull/84209 diff --git a/lib/block-supports/colors.php b/lib/block-supports/colors.php index 7972383ed65cb5..7008495b192c8a 100644 --- a/lib/block-supports/colors.php +++ b/lib/block-supports/colors.php @@ -88,55 +88,90 @@ function gutenberg_apply_colors_support( $block_type, $block_attributes ) { ( isset( $color_support['background'] ) && $color_support['background'] ) || ( is_array( $color_support ) && ! isset( $color_support['background'] ) ); $has_gradients_support = $color_support['gradients'] ?? false; - $color_block_styles = array(); + $skip_text = wp_should_skip_block_supports_serialization( $block_type, 'color', 'text' ); + $skip_background = wp_should_skip_block_supports_serialization( $block_type, 'color', 'background' ); + $skip_gradients = wp_should_skip_block_supports_serialization( $block_type, 'color', 'gradients' ); - // Text colors. - // Check support for text colors. - if ( $has_text_colors_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'text' ) ) { - $preset_text_color = array_key_exists( 'textColor', $block_attributes ) ? "var:preset|color|{$block_attributes['textColor']}" : null; - $custom_text_color = $block_attributes['style']['color']['text'] ?? null; - $color_block_styles['text'] = $preset_text_color ? $preset_text_color : $custom_text_color; - } - - // Background colors. - if ( $has_background_colors_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'background' ) ) { - $preset_background_color = array_key_exists( 'backgroundColor', $block_attributes ) ? "var:preset|color|{$block_attributes['backgroundColor']}" : null; - $custom_background_color = $block_attributes['style']['color']['background'] ?? null; - $color_block_styles['background'] = $preset_background_color ? $preset_background_color : $custom_background_color; - } - - // Gradients. - // Suppress color.gradient CSS when background.gradient is supported and - // explicitly set. background.php owns CSS generation in that case, and - // emitting the background shorthand here would conflict with it. + // background.php owns the CSS when a background gradient is set, so skip color.gradient. $has_background_gradient_support = block_has_support( $block_type, array( 'background', 'gradient' ), false ); $has_background_gradient_value = ! empty( $block_attributes['style']['background']['gradient'] ); - - if ( - $has_gradients_support && - ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'gradients' ) && - ! ( $has_background_gradient_support && $has_background_gradient_value ) - ) { - $preset_gradient_color = array_key_exists( 'gradient', $block_attributes ) ? "var:preset|gradient|{$block_attributes['gradient']}" : null; - $custom_gradient_color = $block_attributes['style']['color']['gradient'] ?? null; - $color_block_styles['gradient'] = $preset_gradient_color ? $preset_gradient_color : $custom_gradient_color; - } - - $attributes = array(); - $styles = gutenberg_style_engine_get_styles( array( 'color' => $color_block_styles ), array( 'convert_vars_to_classnames' => true ) ); + $has_background_gradient = $has_background_gradient_support && $has_background_gradient_value; /* * Mirror of the background support: a color clipped to the text paints the * glyphs, not the box, so only a gradient still adds `has-background`. + * Read before the attributes are filtered for the helper. */ $clips_to_text = 'text' === ( $block_attributes['style']['background']['backgroundClip'] ?? null ) && block_has_support( $block_type, array( 'background', 'backgroundClip' ), false ) && ! wp_should_skip_block_supports_serialization( $block_type, 'background', 'backgroundClip' ); - if ( $clips_to_text && ! empty( $styles['classnames'] ) && empty( $color_block_styles['gradient'] ) ) { - $styles['classnames'] = implode( ' ', array_diff( explode( ' ', $styles['classnames'] ), array( 'has-background' ) ) ); + // The helper reads only `style.color`, so a non-array value counts as no color styles. + if ( ! isset( $block_attributes['style']['color'] ) || ! is_array( $block_attributes['style']['color'] ) ) { + unset( $block_attributes['style'] ); + } + + if ( ! $has_text_colors_support || $skip_text ) { + unset( $block_attributes['textColor'], $block_attributes['style']['color']['text'] ); + } + + if ( ! $has_background_colors_support || $skip_background ) { + unset( $block_attributes['backgroundColor'], $block_attributes['style']['color']['background'] ); + } + + if ( ! $has_gradients_support || $skip_gradients || $has_background_gradient ) { + unset( $block_attributes['gradient'], $block_attributes['style']['color']['gradient'] ); + } + + $classes_and_styles = gutenberg_get_color_classes_and_styles( $block_attributes ); + $has_gradient = ! empty( $block_attributes['gradient'] ) || ! empty( $block_attributes['style']['color']['gradient'] ); + + if ( $clips_to_text && ! $has_gradient && ! empty( $classes_and_styles['class'] ) ) { + $classes = array_diff( explode( ' ', $classes_and_styles['class'] ), array( 'has-background' ) ); + if ( empty( $classes ) ) { + unset( $classes_and_styles['class'] ); + } else { + $classes_and_styles['class'] = implode( ' ', $classes ); + } + } + + return $classes_and_styles; +} + +/** + * Returns color classes and inline styles for block attributes, like the JS + * `getColorClassesAndStyles()`. Does not check block support or skipped + * serialization. + * + * @since 7.3.0 + * + * @param array $block_attributes Block attributes. + * @return array Array with `class` and `style` keys, each present only when non-empty. + */ +function gutenberg_get_color_classes_and_styles( $block_attributes ) { + if ( ! is_array( $block_attributes ) ) { + return array(); + } + + $color_block_styles = array(); + $preset_attributes = array( + 'text' => array( 'textColor', 'color' ), + 'background' => array( 'backgroundColor', 'color' ), + 'gradient' => array( 'gradient', 'gradient' ), + ); + + foreach ( $preset_attributes as $feature => list( $preset_attribute, $preset_type ) ) { + $preset_slug = $block_attributes[ $preset_attribute ] ?? null; + + // Preset slugs are strings in theme.json and the editor. + $preset_value = is_string( $preset_slug ) && '' !== $preset_slug ? "var:preset|{$preset_type}|{$preset_slug}" : null; + $custom_value = $block_attributes['style']['color'][ $feature ] ?? null; + $color_block_styles[ $feature ] = $preset_value ?? $custom_value; } + $attributes = array(); + $styles = gutenberg_style_engine_get_styles( array( 'color' => $color_block_styles ), array( 'convert_vars_to_classnames' => true ) ); + if ( ! empty( $styles['classnames'] ) ) { $attributes['class'] = $styles['classnames']; } diff --git a/packages/block-library/package.json b/packages/block-library/package.json index e6f461b3dcbcf0..3437f7ca704d21 100644 --- a/packages/block-library/package.json +++ b/packages/block-library/package.json @@ -69,6 +69,7 @@ "classSuffix": "_Gutenberg", "prefixFunctions": [ "wp_apply_colors_support", + "wp_get_color_classes_and_styles", "wp_enqueue_block_support_styles", "wp_get_typography_font_size_value", "wp_style_engine_get_styles", diff --git a/packages/block-library/src/calendar/index.php b/packages/block-library/src/calendar/index.php index e3870bb46adb5f..2ca0b013529853 100644 --- a/packages/block-library/src/calendar/index.php +++ b/packages/block-library/src/calendar/index.php @@ -43,22 +43,17 @@ function render_block_core_calendar( $attributes ) { } } - $color_block_styles = array(); - - // Text color. - $preset_text_color = array_key_exists( 'textColor', $attributes ) ? "var:preset|color|{$attributes['textColor']}" : null; - $custom_text_color = $attributes['style']['color']['text'] ?? null; - $color_block_styles['text'] = $preset_text_color ? $preset_text_color : $custom_text_color; - - // Background Color. - $preset_background_color = array_key_exists( 'backgroundColor', $attributes ) ? "var:preset|color|{$attributes['backgroundColor']}" : null; - $custom_background_color = $attributes['style']['color']['background'] ?? null; - $color_block_styles['background'] = $preset_background_color ? $preset_background_color : $custom_background_color; - - // Generate color styles and classes. - $styles = wp_style_engine_get_styles( array( 'color' => $color_block_styles ), array( 'convert_vars_to_classnames' => true ) ); - $inline_styles = empty( $styles['css'] ) ? '' : sprintf( ' style="%s"', esc_attr( $styles['css'] ) ); - $classnames = empty( $styles['classnames'] ) ? '' : ' ' . esc_attr( $styles['classnames'] ); + // Calendar applies only text and background colors, not gradients. + $color_attributes = array_intersect_key( $attributes, array_flip( array( 'textColor', 'backgroundColor' ) ) ); + foreach ( array( 'text', 'background' ) as $feature ) { + if ( isset( $attributes['style']['color'][ $feature ] ) ) { + $color_attributes['style']['color'][ $feature ] = $attributes['style']['color'][ $feature ]; + } + } + + $styles = wp_get_color_classes_and_styles( $color_attributes ); + $inline_styles = empty( $styles['style'] ) ? '' : sprintf( ' style="%s"', esc_attr( $styles['style'] ) ); + $classnames = empty( $styles['class'] ) ? '' : ' ' . esc_attr( $styles['class'] ); if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) { $classnames .= ' has-link-color'; } diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 9ca9711515a713..fd161800a4f20f 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -26,6 +26,11 @@ parameters: - message: '#^Action callback returns .+ but should not return anything\.$#' identifier: return.void + # wp_get_color_classes_and_styles() is new in WordPress Core: https://github.com/WordPress/wordpress-develop/pull/14094 + # Remove this entry once the WordPress stubs are generated from a Core revision that includes it. + - + message: '#^Function wp_get_color_classes_and_styles not found\.$#' + identifier: function.notFound # Level 3: # WP_REST_Request implements ArrayAccess, and the WordPress stubs declare its offsetSet() with a method-level diff --git a/phpunit/block-supports/colors-test.php b/phpunit/block-supports/colors-test.php index 8a6e9e2748ae04..0cfdb7fe52eb8d 100644 --- a/phpunit/block-supports/colors-test.php +++ b/phpunit/block-supports/colors-test.php @@ -18,7 +18,9 @@ public function set_up() { } public function tear_down() { - unregister_block_type( $this->test_block_name ); + if ( $this->test_block_name ) { + unregister_block_type( $this->test_block_name ); + } $this->test_block_name = null; parent::tear_down(); } @@ -269,6 +271,74 @@ public function test_color_gradient_emitted_when_background_gradient_is_not_supp $this->assertSame( $expected, $actual ); } + public function test_color_support_true_applies_text_and_background_colors() { + $this->test_block_name = 'test/color-support-true'; + register_block_type( + $this->test_block_name, + array( + 'api_version' => 3, + 'attributes' => array( + 'textColor' => array( + 'type' => 'string', + ), + 'backgroundColor' => array( + 'type' => 'string', + ), + ), + 'supports' => array( + 'color' => true, + ), + ) + ); + + $registry = WP_Block_Type_Registry::get_instance(); + $block_type = $registry->get_registered( $this->test_block_name ); + + $block_atts = array( + 'textColor' => 'contrast', + 'backgroundColor' => 'base', + ); + + $actual = gutenberg_apply_colors_support( $block_type, $block_atts ); + $expected = array( + 'class' => 'has-text-color has-contrast-color has-background has-base-background-color', + ); + + $this->assertSame( $expected, $actual ); + } + + public function test_color_text_support_disabled_skips_text_color() { + $this->test_block_name = 'test/color-text-support-disabled'; + register_block_type( + $this->test_block_name, + array( + 'api_version' => 3, + 'attributes' => array( + 'textColor' => array( + 'type' => 'string', + ), + ), + 'supports' => array( + 'color' => array( + 'text' => false, + ), + ), + ) + ); + + $registry = WP_Block_Type_Registry::get_instance(); + $block_type = $registry->get_registered( $this->test_block_name ); + + $block_atts = array( + 'textColor' => 'contrast', + ); + + $actual = gutenberg_apply_colors_support( $block_type, $block_atts ); + $expected = array(); + + $this->assertSame( $expected, $actual ); + } + /** * Registers a block with color and background clip support. * @@ -374,4 +444,108 @@ public function data_background_color_has_background_with_a_clip() { ), ); } + + /** + * @covers ::gutenberg_get_color_classes_and_styles + * + * @dataProvider data_get_color_classes_and_styles + * + * @param mixed $block_attributes Block attributes. + * @param array $expected Expected classes and styles. + */ + public function test_get_color_classes_and_styles( $block_attributes, $expected ) { + $this->assertSame( $expected, gutenberg_get_color_classes_and_styles( $block_attributes ) ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_get_color_classes_and_styles() { + return array( + 'custom' => array( + 'block_attributes' => array( + 'style' => array( + 'color' => array( + 'text' => '#d92828', + 'background' => '#ffffff', + 'gradient' => 'linear-gradient(135deg,rgb(6,147,227) 0%,rgb(155,81,224) 100%)', + ), + ), + ), + 'expected' => array( + 'class' => 'has-text-color has-background', + 'style' => 'color:#d92828;background-color:#ffffff;background:linear-gradient(135deg,rgb(6,147,227) 0%,rgb(155,81,224) 100%);', + ), + ), + 'preset' => array( + 'block_attributes' => array( + 'textColor' => 'contrast', + 'backgroundColor' => 'base', + 'gradient' => 'vivid-cyan-blue-to-vivid-purple', + ), + 'expected' => array( + 'class' => 'has-text-color has-contrast-color has-background has-base-background-color has-vivid-cyan-blue-to-vivid-purple-gradient-background', + ), + ), + 'preset wins over custom' => array( + 'block_attributes' => array( + 'textColor' => 'contrast', + 'style' => array( + 'color' => array( + 'text' => '#d92828', + ), + ), + ), + 'expected' => array( + 'class' => 'has-text-color has-contrast-color', + ), + ), + 'null preset uses custom text' => array( + 'block_attributes' => array( + 'textColor' => null, + 'style' => array( + 'color' => array( + 'text' => '#d92828', + ), + ), + ), + 'expected' => array( + 'class' => 'has-text-color', + 'style' => 'color:#d92828;', + ), + ), + 'non-string preset uses custom' => array( + 'block_attributes' => array( + 'textColor' => 1, + 'style' => array( + 'color' => array( + 'text' => '#d92828', + ), + ), + ), + 'expected' => array( + 'class' => 'has-text-color', + 'style' => 'color:#d92828;', + ), + ), + 'empty' => array( + 'block_attributes' => array(), + 'expected' => array(), + ), + 'malformed color' => array( + 'block_attributes' => array( + 'style' => array( + 'color' => '#d92828', + ), + ), + 'expected' => array(), + ), + 'malformed attributes' => array( + 'block_attributes' => 'color', + 'expected' => array(), + ), + ); + } } diff --git a/phpunit/blocks/render-block-calendar-test.php b/phpunit/blocks/render-block-calendar-test.php new file mode 100644 index 00000000000000..045790a91e5fad --- /dev/null +++ b/phpunit/blocks/render-block-calendar-test.php @@ -0,0 +1,101 @@ +post->create( array( 'post_status' => 'publish' ) ); + } + + /** + * @covers ::gutenberg_render_block_core_calendar + * + * @dataProvider data_color_classes_and_styles + * + * @param array $attributes Block attributes. + * @param string $expected_class Expected table class attribute. + * @param string|null $expected_style Expected table style attribute. + */ + public function test_applies_color_classes_and_styles_to_table( $attributes, $expected_class, $expected_style ) { + $block = new WP_Block( + array( + 'blockName' => 'core/calendar', + 'attrs' => $attributes, + ) + ); + + $processor = new WP_HTML_Tag_Processor( $block->render() ); + + $this->assertTrue( $processor->next_tag( 'table' ) ); + $this->assertSame( $expected_class, $processor->get_attribute( 'class' ) ); + $this->assertSame( $expected_style, $processor->get_attribute( 'style' ) ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_color_classes_and_styles() { + return array( + 'preset colors' => array( + 'attributes' => array( + 'textColor' => 'contrast', + 'backgroundColor' => 'base', + ), + 'expected_class' => 'wp-calendar-table has-text-color has-contrast-color has-background has-base-background-color', + 'expected_style' => null, + ), + 'custom colors' => array( + 'attributes' => array( + 'style' => array( + 'color' => array( + 'text' => '#123456', + 'background' => '#abcdef', + ), + ), + ), + 'expected_class' => 'wp-calendar-table has-text-color has-background', + 'expected_style' => 'color:#123456;background-color:#abcdef;', + ), + 'link color' => array( + 'attributes' => array( + 'textColor' => 'contrast', + 'style' => array( + 'elements' => array( + 'link' => array( + 'color' => array( + 'text' => '#ff0000', + ), + ), + ), + ), + ), + 'expected_class' => 'wp-calendar-table has-text-color has-contrast-color has-link-color', + 'expected_style' => null, + ), + 'gradient is not applied' => array( + 'attributes' => array( + 'gradient' => 'vivid-cyan-blue-to-vivid-purple', + 'style' => array( + 'color' => array( + 'gradient' => 'linear-gradient(135deg,rgb(6,147,227) 0%,rgb(155,81,224) 100%)', + ), + ), + ), + 'expected_class' => 'wp-calendar-table', + 'expected_style' => null, + ), + ); + } +}