Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 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
116 changes: 81 additions & 35 deletions lib/block-supports/background.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,64 @@ function gutenberg_register_background_support( $block_type ) {
}
}

/**
* Returns background classes and inline styles for block attributes, like the JS
* `getBackgroundClassesAndStyles()`. 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_background_classes_and_styles( $block_attributes ) {
if ( ! is_array( $block_attributes ) ) {
return array();
}

$background = $block_attributes['style']['background'] ?? null;

if ( ! is_array( $background ) ) {
return array();
}

$background_styles = array(
'backgroundImage' => $background['backgroundImage'] ?? null,
'backgroundSize' => $background['backgroundSize'] ?? null,
'backgroundPosition' => $background['backgroundPosition'] ?? null,
'backgroundRepeat' => $background['backgroundRepeat'] ?? null,
'backgroundAttachment' => $background['backgroundAttachment'] ?? null,
'gradient' => $background['gradient'] ?? null,
'backgroundClip' => $background['backgroundClip'] ?? null,
);

if ( ! empty( $background_styles['backgroundImage'] ) ) {
$background_styles['backgroundSize'] ??= 'cover';
if ( 'contain' === $background_styles['backgroundSize'] && ! $background_styles['backgroundPosition'] ) {
$background_styles['backgroundPosition'] = '50% 50%';
}
}

$styles = gutenberg_style_engine_get_styles( array( 'background' => $background_styles ) );

if ( empty( $styles['css'] ) ) {
return array();
}

$attributes = array();

$has_background = ! empty( $background_styles['backgroundImage'] ) || ! empty( $background_styles['gradient'] );

// A text clip paints the glyphs, not the box, so there is no block background.
if ( $has_background && 'text' !== $background_styles['backgroundClip'] ) {
$attributes['class'] = 'has-background';
}

$attributes['style'] = $styles['css'];

return $attributes;
}

/**
* Renders the background styles to the block wrapper.
* This block support uses the `render_block` hook to ensure that
Expand Down Expand Up @@ -62,59 +120,47 @@ function gutenberg_render_background_support( $block_content, $block ) {
return $block_content;
}

$background_styles = array();

if ( ! $skip_background_image ) {
$background_styles['backgroundImage'] = $block_attributes['style']['background']['backgroundImage'] ?? null;
$background_styles['backgroundSize'] = $block_attributes['style']['background']['backgroundSize'] ?? null;
$background_styles['backgroundPosition'] = $block_attributes['style']['background']['backgroundPosition'] ?? null;
$background_styles['backgroundRepeat'] = $block_attributes['style']['background']['backgroundRepeat'] ?? null;
$background_styles['backgroundAttachment'] = $block_attributes['style']['background']['backgroundAttachment'] ?? null;
if ( ! empty( $background_styles['backgroundImage'] ) ) {
$background_styles['backgroundSize'] = $background_styles['backgroundSize'] ?? 'cover';
// If the background size is set to `contain` and no position is set, set the position to `center`.
if ( 'contain' === $background_styles['backgroundSize'] && ! $background_styles['backgroundPosition'] ) {
$background_styles['backgroundPosition'] = '50% 50%';
}
// unset() fatals on a string, and the helper ignores one anyway.
if ( is_array( $block_attributes['style']['background'] ) ) {
if ( $skip_background_image ) {
unset(
$block_attributes['style']['background']['backgroundImage'],
$block_attributes['style']['background']['backgroundSize'],
$block_attributes['style']['background']['backgroundPosition'],
$block_attributes['style']['background']['backgroundRepeat'],
$block_attributes['style']['background']['backgroundAttachment']
);
}
}

if ( ! $skip_background_gradient ) {
$background_styles['gradient'] = $block_attributes['style']['background']['gradient'] ?? null;
}
if ( $skip_background_gradient ) {
unset( $block_attributes['style']['background']['gradient'] );
}

if ( ! $skip_background_clip ) {
$background_styles['backgroundClip'] = $block_attributes['style']['background']['backgroundClip'] ?? null;
// Serialized values only, so a skipped clip cannot drop `has-background`.
if ( $skip_background_clip ) {
unset( $block_attributes['style']['background']['backgroundClip'] );
}
}

$styles = gutenberg_style_engine_get_styles( array( 'background' => $background_styles ) );
$styles = gutenberg_get_background_classes_and_styles( $block_attributes );

if ( ! empty( $styles['css'] ) ) {
if ( ! empty( $styles['style'] ) ) {
// Inject background styles to the first element, presuming it's the wrapper, if it exists.
$tags = new WP_HTML_Tag_Processor( $block_content );

if ( $tags->next_tag() ) {
$existing_style = $tags->get_attribute( 'style' );
if ( is_string( $existing_style ) && '' !== $existing_style ) {
$separator = str_ends_with( $existing_style, ';' ) ? '' : ';';
$updated_style = "{$existing_style}{$separator}{$styles['css']}";
$updated_style = "{$existing_style}{$separator}{$styles['style']}";
} else {
$updated_style = $styles['css'];
$updated_style = $styles['style'];
}

$tags->set_attribute( 'style', $updated_style );

/*
* A background clipped to the text paints the glyphs rather than the
* block's box, so there is no block background to announce. Read the
* clip from the serialized styles so an unsupported or skipped value
* left in the block's attributes cannot suppress the class.
*/
$background_clip = $background_styles['backgroundClip'] ?? null;
$has_background = ! empty( $background_styles['backgroundImage'] ) || ! empty( $background_styles['gradient'] );

if ( $has_background && 'text' !== $background_clip ) {
$tags->add_class( 'has-background' );
if ( ! empty( $styles['class'] ) ) {
$tags->add_class( $styles['class'] );
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/src/hooks/background.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export function getBackgroundImageClasses( style: PanelStyle | undefined ) {
hasBackgroundImageValue( style ) || hasBackgroundGradientValue( style );

// A text clip paints the glyphs, not the block. Mirror of
// `gutenberg_render_background_support()`.
// `gutenberg_get_background_classes_and_styles()`.
if ( ! hasBackground || 'text' === style?.background?.backgroundClip ) {
return '';
}
Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/src/hooks/use-background-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function getBackgroundClassesAndStyles( attributes: StyleAttributes ) {
const backgroundStyles: NonNullable< PanelStyle[ 'background' ] > =
style?.background || {};

// Mirrors gutenberg_render_background_support() in lib/block-supports/background.php.
// Mirrors gutenberg_get_background_classes_and_styles() in lib/block-supports/background.php.
const className =
'text' !== backgroundStyles.backgroundClip
? getBackgroundImageClasses( style )
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 @@ -76,6 +76,7 @@
"wp_style_engine_get_styles",
"wp_get_spacing_classes_and_styles",
"wp_get_global_styles",
"wp_get_background_classes_and_styles",
"wp_get_global_settings"
],
"suffixClasses": [
Expand Down
31 changes: 8 additions & 23 deletions packages/block-library/src/button/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,26 +64,11 @@ function render_block_core_button( $attributes, $content ) {
$background = $attributes['style']['background'] ?? null;

if ( is_array( $background ) ) {
$background_styles = array(
'backgroundImage' => $background['backgroundImage'] ?? null,
'backgroundSize' => $background['backgroundSize'] ?? null,
'backgroundPosition' => $background['backgroundPosition'] ?? null,
'backgroundRepeat' => $background['backgroundRepeat'] ?? null,
'backgroundAttachment' => $background['backgroundAttachment'] ?? null,
'gradient' => $background['gradient'] ?? null,
);

// Mirror the background block support defaults.
if ( ! empty( $background_styles['backgroundImage'] ) ) {
$background_styles['backgroundSize'] ??= 'cover';
if ( 'contain' === $background_styles['backgroundSize'] && ! $background_styles['backgroundPosition'] ) {
$background_styles['backgroundPosition'] = '50% 50%';
}
}

$styles = wp_style_engine_get_styles( array( 'background' => $background_styles ) );
// Button has no clip support, so ignore a stale value.
unset( $background['backgroundClip'] );
$styles = wp_get_background_classes_and_styles( array( 'style' => array( 'background' => $background ) ) );

if ( ! empty( $styles['css'] ) ) {
if ( ! empty( $styles['style'] ) ) {
$processor = new WP_HTML_Tag_Processor( $content );
while ( $processor->next_tag() ) {
if ( $tag !== $processor->get_tag() ) {
Expand All @@ -93,13 +78,13 @@ function render_block_core_button( $attributes, $content ) {
$existing_style = $processor->get_attribute( 'style' );
if ( is_string( $existing_style ) && '' !== $existing_style ) {
$separator = str_ends_with( $existing_style, ';' ) ? '' : ';';
$processor->set_attribute( 'style', $existing_style . $separator . $styles['css'] );
$processor->set_attribute( 'style', $existing_style . $separator . $styles['style'] );
} else {
$processor->set_attribute( 'style', $styles['css'] );
$processor->set_attribute( 'style', $styles['style'] );
}

if ( ! empty( $background_styles['backgroundImage'] ) || ! empty( $background_styles['gradient'] ) ) {
$processor->add_class( 'has-background' );
if ( ! empty( $styles['class'] ) ) {
$processor->add_class( $styles['class'] );
}
break;
}
Expand Down
35 changes: 13 additions & 22 deletions packages/block-library/src/icon/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,12 @@ function render_block_core_icon( $attributes ) {
$custom_background_color = $attributes['style']['color']['background'] ?? null;
$color_styles['background'] = $preset_background_color ? $preset_background_color : $custom_background_color;

// Background image, size and gradient.
$background_styles = array();
if ( isset( $attributes['style']['background'] ) && is_array( $attributes['style']['background'] ) ) {
$background_styles = array_intersect_key(
$attributes['style']['background'],
array_flip( array( 'backgroundImage', 'backgroundSize', 'backgroundPosition', 'backgroundRepeat', 'backgroundAttachment', 'gradient' ) )
);

// Mirror of the defaults in the background block support.
if ( ! empty( $background_styles['backgroundImage'] ) ) {
$background_styles['backgroundSize'] ??= 'cover';
if ( 'contain' === $background_styles['backgroundSize'] && empty( $background_styles['backgroundPosition'] ) ) {
$background_styles['backgroundPosition'] = '50% 50%';
}
}
// Background image, size and gradient. Icon has no clip support, so ignore a stale value.
$background = $attributes['style']['background'] ?? null;
if ( is_array( $background ) ) {
unset( $background['backgroundClip'] );
}
$background_styles = wp_get_background_classes_and_styles( array( 'style' => array( 'background' => $background ) ) );

// Border.
$border_styles = array();
Expand Down Expand Up @@ -98,7 +88,6 @@ function render_block_core_icon( $attributes ) {
$styles = wp_style_engine_get_styles(
array(
'color' => $color_styles,
'background' => $background_styles,
'border' => $border_styles,
'shadow' => $shadow_styles,
'dimensions' => $dimensions_styles,
Expand All @@ -115,6 +104,9 @@ function render_block_core_icon( $attributes ) {
);
$styles['css'] = ( $styles['css'] ?? '' ) . ( $spacing_styles['style'] ?? '' );

// The style engine outputs background before the other groups, so it leads here too.
$css = ( $background_styles['style'] ?? '' ) . ( $styles['css'] ?? '' );

$svg = wp_get_icon(
$attributes['icon'],
array(
Expand All @@ -131,7 +123,7 @@ function render_block_core_icon( $attributes ) {

$processor = new WP_HTML_Tag_Processor( $svg );
if ( $processor->next_tag( 'svg' ) ) {
if ( ! empty( $styles['css'] ) ) {
if ( '' !== $css ) {
// Merge with the SVG's intrinsic style (e.g. `fill: none` on
// stroke-based icons) so it is preserved. The block styles come last
// so they win on any conflicting property.
Expand All @@ -140,14 +132,13 @@ function render_block_core_icon( $attributes ) {
? rtrim( trim( $existing_style ), ';' )
: '';
$merged_style = '' !== $trimmed_style
? $trimmed_style . '; ' . $styles['css']
: $styles['css'];
? $trimmed_style . '; ' . $css
: $css;
$processor->set_attribute( 'style', $merged_style );
}

// The style engine only adds `has-background` for a gradient.
if ( ! empty( $background_styles['backgroundImage'] ) ) {
$processor->add_class( 'has-background' );
if ( ! empty( $background_styles['class'] ) ) {
$processor->add_class( $background_styles['class'] );
}

// Apply flip classes to the SVG.
Expand Down
34 changes: 9 additions & 25 deletions packages/block-library/src/post-navigation-link/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,27 +197,17 @@ function block_core_post_navigation_link_get_support_styles( $attributes ) {
);
}

$background = $block_styles['background'] ?? array();
$background_styles = array(
'backgroundImage' => $background['backgroundImage'] ?? null,
'backgroundSize' => $background['backgroundSize'] ?? null,
'backgroundPosition' => $background['backgroundPosition'] ?? null,
'backgroundRepeat' => $background['backgroundRepeat'] ?? null,
'backgroundAttachment' => $background['backgroundAttachment'] ?? null,
'gradient' => $background['gradient'] ?? null,
);
$background = $block_styles['background'] ?? null;

// Mirrors the background image defaults of the background block support.
if ( ! empty( $background_styles['backgroundImage'] ) ) {
$background_styles['backgroundSize'] ??= 'cover';
if ( 'contain' === $background_styles['backgroundSize'] && ! $background_styles['backgroundPosition'] ) {
$background_styles['backgroundPosition'] = '50% 50%';
}
// Post Navigation Link has no clip support, so ignore a stale value.
if ( is_array( $background ) ) {
unset( $background['backgroundClip'] );
}

$background_styles = wp_get_background_classes_and_styles( array( 'style' => array( 'background' => $background ) ) );

$styles = wp_style_engine_get_styles(
array(
'background' => $background_styles,
'border' => $border_styles,
'dimensions' => array(
'minHeight' => $block_styles['dimensions']['minHeight'] ?? null,
Expand All @@ -228,16 +218,10 @@ function block_core_post_navigation_link_get_support_styles( $attributes ) {
);
$spacing_styles = wp_get_spacing_classes_and_styles( $attributes );

$class_names = $styles['classnames'] ?? '';

// The style engine only adds `has-background` for a gradient, the background block support adds it for an image too.
if ( ! empty( $background_styles['backgroundImage'] ) && ! in_array( 'has-background', explode( ' ', $class_names ), true ) ) {
$class_names = trim( "{$class_names} has-background" );
}

// The style engine outputs background before the other groups, so it leads here too.
return array(
'class' => $class_names,
'style' => ( $styles['css'] ?? '' ) . ( $spacing_styles['style'] ?? '' ),
'class' => trim( ( $background_styles['class'] ?? '' ) . ' ' . ( $styles['classnames'] ?? '' ) ),
'style' => ( $background_styles['style'] ?? '' ) . ( $styles['css'] ?? '' ) . ( $spacing_styles['style'] ?? '' ),
);
}

Expand Down
26 changes: 13 additions & 13 deletions packages/block-library/src/search/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -515,21 +515,21 @@ function styles_for_block_core_search( $attributes ) {
}
}

// Background skips serialization, so it follows color onto the same element.
if ( ! empty( $attributes['style']['background']['gradient'] ) ) {
$background_styles = wp_style_engine_get_styles(
array(
// Background skips serialization, so it follows color onto the same element. Search only supports a gradient.
$background_styles = wp_get_background_classes_and_styles(
array(
'style' => array(
'background' => array(
'gradient' => $attributes['style']['background']['gradient'],
'gradient' => $attributes['style']['background']['gradient'] ?? null,
),
)
);
if ( ! empty( $background_styles['css'] ) ) {
if ( $use_input_for_colors ) {
$input_styles[] = $background_styles['css'];
} else {
$button_styles[] = $background_styles['css'];
}
),
)
);
if ( ! empty( $background_styles['style'] ) ) {
if ( $use_input_for_colors ) {
$input_styles[] = $background_styles['style'];
} else {
$button_styles[] = $background_styles['style'];
}
}

Expand Down
6 changes: 6 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ parameters:
message: '#^Function wp_get_spacing_classes_and_styles not found\.$#'
identifier: function.notFound

# wp_get_background_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_background_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
# template (`@template TOffset of key-of<T>`, `@param T[TOffset] $value`). PHPStan does not resolve that
Expand Down
Loading
Loading