Commit a44f2f10a4 for wordpress.org

commit a44f2f10a4a301eb6f421b1d72e50ec7827553e2
Author: aaronrobertshaw <aaronrobertshaw@git.wordpress.org>
Date:   Thu Nov 26 06:53:02 2026 +0000

    Editor: Add PHP helpers for block support classes and styles.

    Adds `wp_get_border_classes_and_styles()`, `wp_get_shadow_classes_and_styles()`, `wp_get_spacing_classes_and_styles()`, `wp_get_color_classes_and_styles()` and `wp_get_background_classes_and_styles()`. Each takes a block's attributes and returns the classes and styles for that block support, similar to the JavaScript `get*ClassesAndStyles()` functions.

    Blocks that skip serialization of a block support can call these helpers instead of rebuilding the classes and styles by hand. The border, shadow, spacing, color and background block supports now build their output with them.

    The block support output is unchanged with one exception. A null, false, empty or zero preset color now falls back to the custom color, as it does in the editor.

    Developed in: https://github.com/WordPress/wordpress-develop/pull/14094

    Props westonruter, ramonopoly, andrewserong, talldanwp, sakibmd, testerrahul.
    Fixes #66269.

    Built from https://develop.svn.wordpress.org/trunk@65798


    git-svn-id: http://core.svn.wordpress.org/trunk@64827 1a063a9b-81f0-0310-95a4-ce76da25c4cd

diff --git a/wp-includes/block-supports/background.php b/wp-includes/block-supports/background.php
index 8d464cc789..76fa6b193b 100644
--- a/wp-includes/block-supports/background.php
+++ b/wp-includes/block-supports/background.php
@@ -34,6 +34,67 @@ function wp_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<string, mixed> $block_attributes Block attributes.
+ * @return array<string, string> Array with `class` and `style` keys, each present only when non-empty.
+ *
+ * @phpstan-param array{ style?: array<string, mixed>, ...<string, mixed> } $block_attributes
+ * @phpstan-return array{ class?: 'has-background', style?: non-empty-string }
+ */
+function wp_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 = wp_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
@@ -91,36 +152,31 @@ function wp_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'] ??= '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 = wp_style_engine_get_styles( array( 'background' => $background_styles ) );
+	$styles = wp_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 );

@@ -128,24 +184,15 @@ function wp_render_background_support( $block_content, $block ) {
 			$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'] );
 			}
 		}

diff --git a/wp-includes/block-supports/border.php b/wp-includes/block-supports/border.php
index 89a3ef175e..2d31aea1eb 100644
--- a/wp-includes/block-supports/border.php
+++ b/wp-includes/block-supports/border.php
@@ -52,76 +52,114 @@ function wp_apply_border_support( $block_type, $block_attributes ) {
 		return array();
 	}

-	$border_block_styles      = array();
-	$has_border_color_support = wp_has_border_feature_support( $block_type, 'color' );
-	$has_border_width_support = wp_has_border_feature_support( $block_type, 'width' );
-
-	// Border radius.
-	if (
-		wp_has_border_feature_support( $block_type, 'radius' ) &&
-		isset( $block_attributes['style']['border']['radius'] ) &&
-		! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'radius' )
-	) {
-		$border_radius = $block_attributes['style']['border']['radius'];
-
-		if ( is_numeric( $border_radius ) ) {
-			$border_radius .= 'px';
-		}
+	$has_border_color_support  = wp_has_border_feature_support( $block_type, 'color' );
+	$has_border_radius_support = wp_has_border_feature_support( $block_type, 'radius' );
+	$has_border_style_support  = wp_has_border_feature_support( $block_type, 'style' );
+	$has_border_width_support  = wp_has_border_feature_support( $block_type, 'width' );
+	$skip_color                = wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' );
+	$skip_radius               = wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'radius' );
+	$skip_style                = wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' );
+	$skip_width                = wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' );
+
+	// The unset() calls below fail on a non-array style or border.
+	if ( ! isset( $block_attributes['style']['border'] ) || ! is_array( $block_attributes['style']['border'] ) ) {
+		unset( $block_attributes['style'] );
+	}
+
+	if ( ! $has_border_radius_support || $skip_radius ) {
+		unset( $block_attributes['style']['border']['radius'] );
+	}

-		$border_block_styles['radius'] = $border_radius;
+	if ( ! $has_border_style_support || $skip_style ) {
+		unset( $block_attributes['style']['border']['style'] );
 	}

-	// Border style.
-	if (
-		wp_has_border_feature_support( $block_type, 'style' ) &&
-		isset( $block_attributes['style']['border']['style'] ) &&
-		! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' )
-	) {
-		$border_block_styles['style'] = $block_attributes['style']['border']['style'];
+	if ( ! $has_border_width_support || $skip_width ) {
+		unset( $block_attributes['style']['border']['width'] );
 	}

-	// Border width.
-	if (
-		$has_border_width_support &&
-		isset( $block_attributes['style']['border']['width'] ) &&
-		! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' )
-	) {
-		$border_width = $block_attributes['style']['border']['width'];
+	if ( ! $has_border_color_support || $skip_color ) {
+		unset( $block_attributes['style']['border']['color'], $block_attributes['borderColor'] );
+	}
+
+	// Side values follow skipped serialization only, as on trunk.
+	foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) {
+		$has_side = isset( $block_attributes['style']['border'][ $side ] ) && is_array( $block_attributes['style']['border'][ $side ] );
+
+		if ( ! $has_side || ( ! $has_border_color_support && ! $has_border_width_support ) ) {
+			unset( $block_attributes['style']['border'][ $side ] );
+			continue;
+		}
+
+		if ( $skip_width ) {
+			unset( $block_attributes['style']['border'][ $side ]['width'] );
+		}
+
+		if ( $skip_color ) {
+			unset( $block_attributes['style']['border'][ $side ]['color'] );
+		}

-		// This check handles original unitless implementation.
-		if ( is_numeric( $border_width ) ) {
-			$border_width .= 'px';
+		if ( $skip_style ) {
+			unset( $block_attributes['style']['border'][ $side ]['style'] );
 		}
+	}
+
+	return wp_get_border_classes_and_styles( $block_attributes );
+}
+
+/**
+ * Returns border classes and inline styles for block attributes, like the JS
+ * `getBorderClassesAndStyles()`. Does not check block support or skipped
+ * serialization.
+ *
+ * @since 7.3.0
+ *
+ * @param array<string, mixed> $block_attributes Block attributes.
+ * @return array<string, string> Array with `class` and `style` keys, each present only when non-empty.
+ *
+ * @phpstan-param array{ style?: array<string, mixed>, ...<string, mixed> } $block_attributes
+ * @phpstan-return array{ class?: non-empty-string, style?: non-empty-string }
+ */
+function wp_get_border_classes_and_styles( $block_attributes ) {
+	if ( ! is_array( $block_attributes ) ) {
+		return array();
+	}
+
+	$border        = isset( $block_attributes['style']['border'] ) && is_array( $block_attributes['style']['border'] )
+		? $block_attributes['style']['border']
+		: array();
+	$border_styles = array();

-		$border_block_styles['width'] = $border_width;
-	}
-
-	// Border color.
-	if (
-		$has_border_color_support &&
-		! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' )
-	) {
-		$preset_border_color          = array_key_exists( 'borderColor', $block_attributes ) ? "var:preset|color|{$block_attributes['borderColor']}" : null;
-		$custom_border_color          = $block_attributes['style']['border']['color'] ?? null;
-		$border_block_styles['color'] = $preset_border_color ? $preset_border_color : $custom_border_color;
-	}
-
-	// Generates styles for individual border sides.
-	if ( $has_border_color_support || $has_border_width_support ) {
-		foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) {
-			$border                       = $block_attributes['style']['border'][ $side ] ?? null;
-			$border_side_values           = array(
-				'width' => isset( $border['width'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' ) ? $border['width'] : null,
-				'color' => isset( $border['color'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' ) ? $border['color'] : null,
-				'style' => isset( $border['style'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' ) ? $border['style'] : null,
-			);
-			$border_block_styles[ $side ] = $border_side_values;
+	// Unitless radius and width are from the original implementation.
+	if ( isset( $border['radius'] ) ) {
+		$border_styles['radius'] = is_numeric( $border['radius'] ) ? "{$border['radius']}px" : $border['radius'];
+	}
+
+	if ( isset( $border['style'] ) ) {
+		$border_styles['style'] = $border['style'];
+	}
+
+	if ( isset( $border['width'] ) ) {
+		$border_styles['width'] = is_numeric( $border['width'] ) ? "{$border['width']}px" : $border['width'];
+	}
+
+	// Preset slugs are strings in theme.json and the editor.
+	$preset_slug            = $block_attributes['borderColor'] ?? null;
+	$border_styles['color'] = is_string( $preset_slug ) && '' !== $preset_slug ? "var:preset|color|{$preset_slug}" : ( $border['color'] ?? null );
+
+	foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) {
+		if ( ! isset( $border[ $side ] ) || ! is_array( $border[ $side ] ) ) {
+			continue;
 		}
+		$border_styles[ $side ] = array(
+			'width' => $border[ $side ]['width'] ?? null,
+			'color' => $border[ $side ]['color'] ?? null,
+			'style' => $border[ $side ]['style'] ?? null,
+		);
 	}

-	// Collect classes and styles.
 	$attributes = array();
-	$styles     = wp_style_engine_get_styles( array( 'border' => $border_block_styles ) );
+	$styles     = wp_style_engine_get_styles( array( 'border' => $border_styles ) );

 	if ( ! empty( $styles['classnames'] ) ) {
 		$attributes['class'] = $styles['classnames'];
diff --git a/wp-includes/block-supports/colors.php b/wp-includes/block-supports/colors.php
index 3c3396c587..ce002215be 100644
--- a/wp-includes/block-supports/colors.php
+++ b/wp-includes/block-supports/colors.php
@@ -98,54 +98,93 @@ function wp_apply_colors_support( WP_Block_Type $block_type, array $block_attrib
 		( 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.
-	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     = wp_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 = wp_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<string, mixed> $block_attributes Block attributes.
+ * @return array<string, string> Array with `class` and `style` keys, each present only when non-empty.
+ *
+ * @phpstan-param array{ style?: array{ color?: array<string, mixed>, ...<string, mixed> }, ...<string, mixed> } $block_attributes
+ * @phpstan-return array{ class?: non-empty-string, style?: non-empty-string }
+ */
+function wp_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     = wp_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/wp-includes/block-supports/shadow.php b/wp-includes/block-supports/shadow.php
index 272eb87407..cd651b6832 100644
--- a/wp-includes/block-supports/shadow.php
+++ b/wp-includes/block-supports/shadow.php
@@ -60,13 +60,35 @@ function wp_apply_shadow_support( $block_type, $block_attributes ) {
 		return array();
 	}

-	$shadow_block_styles = array();
+	return wp_get_shadow_classes_and_styles( $block_attributes );
+}

-	$custom_shadow                 = $block_attributes['style']['shadow'] ?? null;
-	$shadow_block_styles['shadow'] = $custom_shadow;
+/**
+ * Returns shadow classes and inline styles for block attributes, like the JS
+ * `getShadowClassesAndStyles()`. Does not check block support or skipped
+ * serialization.
+ *
+ * @since 7.3.0
+ *
+ * @param array<string, mixed> $block_attributes Block attributes.
+ * @return array<string, string> Array with `class` and `style` keys, each present only when non-empty.
+ *
+ * @phpstan-param array{ style?: array<string, mixed>, ...<string, mixed> } $block_attributes
+ * @phpstan-return array{ class?: non-empty-string, style?: non-empty-string }
+ */
+function wp_get_shadow_classes_and_styles( $block_attributes ) {
+	if ( ! is_array( $block_attributes ) ) {
+		return array();
+	}
+
+	$shadow = $block_attributes['style']['shadow'] ?? null;

 	$attributes = array();
-	$styles     = wp_style_engine_get_styles( $shadow_block_styles );
+	$styles     = wp_style_engine_get_styles( array( 'shadow' => $shadow ) );
+
+	if ( ! empty( $styles['classnames'] ) ) {
+		$attributes['class'] = $styles['classnames'];
+	}

 	if ( ! empty( $styles['css'] ) ) {
 		$attributes['style'] = $styles['css'];
diff --git a/wp-includes/block-supports/spacing.php b/wp-includes/block-supports/spacing.php
index a778eebeb4..e92fbad6af 100644
--- a/wp-includes/block-supports/spacing.php
+++ b/wp-includes/block-supports/spacing.php
@@ -49,28 +49,62 @@ function wp_apply_spacing_support( $block_type, $block_attributes ) {
 		return array();
 	}

-	$attributes          = array();
+	if ( ! isset( $block_attributes['style']['spacing'] ) || ! is_array( $block_attributes['style']['spacing'] ) ) {
+		return array();
+	}
+
 	$has_padding_support = block_has_support( $block_type, array( 'spacing', 'padding' ), false );
 	$has_margin_support  = block_has_support( $block_type, array( 'spacing', 'margin' ), false );
-	$block_styles        = $block_attributes['style'] ?? null;
+	$skip_padding        = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'padding' );
+	$skip_margin         = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'margin' );

-	if ( ! $block_styles ) {
-		return $attributes;
+	if ( ! $has_padding_support || $skip_padding ) {
+		unset( $block_attributes['style']['spacing']['padding'] );
 	}

-	$skip_padding         = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'padding' );
-	$skip_margin          = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'margin' );
-	$spacing_block_styles = array(
-		'padding' => null,
-		'margin'  => null,
-	);
-	if ( $has_padding_support && ! $skip_padding ) {
-		$spacing_block_styles['padding'] = $block_styles['spacing']['padding'] ?? null;
+	if ( ! $has_margin_support || $skip_margin ) {
+		unset( $block_attributes['style']['spacing']['margin'] );
+	}
+
+	return wp_get_spacing_classes_and_styles( $block_attributes );
+}
+
+/**
+ * Returns spacing classes and inline styles for block attributes, like the JS
+ * `getSpacingClassesAndStyles()`. Does not check block support or skipped
+ * serialization.
+ *
+ * @since 7.3.0
+ *
+ * @param array<string, mixed> $block_attributes Block attributes.
+ * @return array<string, string> Array with `class` and `style` keys, each present only when non-empty.
+ *
+ * @phpstan-param array{ style?: array<string, mixed>, ...<string, mixed> } $block_attributes
+ * @phpstan-return array{ class?: non-empty-string, style?: non-empty-string }
+ */
+function wp_get_spacing_classes_and_styles( $block_attributes ) {
+	if ( ! is_array( $block_attributes ) ) {
+		return array();
+	}
+
+	$spacing = $block_attributes['style']['spacing'] ?? null;
+	if ( ! is_array( $spacing ) ) {
+		return array();
 	}
-	if ( $has_margin_support && ! $skip_margin ) {
-		$spacing_block_styles['margin'] = $block_styles['spacing']['margin'] ?? null;
+
+	$styles     = wp_style_engine_get_styles(
+		array(
+			'spacing' => array(
+				'padding' => $spacing['padding'] ?? null,
+				'margin'  => $spacing['margin'] ?? null,
+			),
+		)
+	);
+	$attributes = array();
+
+	if ( ! empty( $styles['classnames'] ) ) {
+		$attributes['class'] = $styles['classnames'];
 	}
-	$styles = wp_style_engine_get_styles( array( 'spacing' => $spacing_block_styles ) );

 	if ( ! empty( $styles['css'] ) ) {
 		$attributes['style'] = $styles['css'];
diff --git a/wp-includes/version.php b/wp-includes/version.php
index f3cf4571bc..eceb99cf77 100644
--- a/wp-includes/version.php
+++ b/wp-includes/version.php
@@ -16,7 +16,7 @@
  *
  * @global string $wp_version
  */
-$wp_version = '7.3-alpha-65797';
+$wp_version = '7.3-alpha-65798';

 /**
  * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.