wpseek.com
Bazujące na WordPress narzędzie wyszukiwania dla deweloperów i twórców motywów.



wp_get_background_classes_and_styles › WordPress Function

Od7.3.0
Przestarzałyn/a
› wp_get_background_classes_and_styles ( $block_attributes )
Parametry:
  • (array<string,mixed>) $block_attributes Block attributes.
    Wymagane: Tak
Powrót:
  • (array<string,string>) Array with `class` and `style` keys, each present only when non-empty.
Zdefiniowane na:
Codex:

Returns background classes and inline styles for block attributes, like the JS `getBackgroundClassesAndStyles()`. Does not check block support or skipped serialization.



Źródło

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;
}