wpseek.com
Outil de recherche WordPress pour les développeurs et auteurs de thèmes



wp_get_border_classes_and_styles › WordPress Function

Depuis7.3.0
Dépréciéen/a
› wp_get_border_classes_and_styles ( $block_attributes )
Paramètres:
  • (array<string,mixed>) $block_attributes Block attributes.
    Requis: Oui
Retourne:
  • (array<string,string>) Array with `class` and `style` keys, each present only when non-empty.
Défini(e) dans:
Codex:

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



Source

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();

	// 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,
		);
	}

	$attributes = array();
	$styles     = wp_style_engine_get_styles( array( 'border' => $border_styles ) );

	if ( ! empty( $styles['classnames'] ) ) {
		$attributes['class'] = $styles['classnames'];
	}

	if ( ! empty( $styles['css'] ) ) {
		$attributes['style'] = $styles['css'];
	}

	return $attributes;
}