Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
7 changes: 7 additions & 0 deletions backport-changelog/7.3/14094.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
https://github.com/WordPress/wordpress-develop/pull/14094
Comment thread
aaronrobertshaw marked this conversation as resolved.
Resolved

* 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
105 changes: 70 additions & 35 deletions lib/block-supports/colors.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
27 changes: 11 additions & 16 deletions packages/block-library/src/calendar/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand Down
5 changes: 5 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
176 changes: 175 additions & 1 deletion phpunit/block-supports/colors-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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(),
),
);
}
}
Loading
Loading