Automattic\WooCommerce\Blocks\Utils

StyleAttributesUtils::get_classes_and_styles_by_attributes()public staticWC 1.0

Get classes and styles from attributes.

Excludes link_color and link_hover_color since those should not apply to the container.

Method of the class: StyleAttributesUtils{}

No Hooks.

Return

Array.

Usage

$result = StyleAttributesUtils::get_classes_and_styles_by_attributes( $attributes, $properties, $exclude );
$attributes(array) (required)
Block attributes.
$properties(array)
Properties to get classes/styles from.
Default: array()
$exclude(array)
Properties to exclude.
Default: array()

StyleAttributesUtils::get_classes_and_styles_by_attributes() code WC 9.4.2

public static function get_classes_and_styles_by_attributes( $attributes, $properties = array(), $exclude = array() ) {
	$classes_and_styles = array(
		'align'            => self::get_align_class_and_style( $attributes ),
		'background_color' => self::get_background_color_class_and_style( $attributes ),
		'border_color'     => self::get_border_color_class_and_style( $attributes ),
		'border_radius'    => self::get_border_radius_class_and_style( $attributes ),
		'border_width'     => self::get_border_width_class_and_style( $attributes ),
		'border_style'     => self::get_border_style_class_and_style( $attributes ),
		'font_family'      => self::get_font_family_class_and_style( $attributes ),
		'font_size'        => self::get_font_size_class_and_style( $attributes ),
		'font_style'       => self::get_font_style_class_and_style( $attributes ),
		'font_weight'      => self::get_font_weight_class_and_style( $attributes ),
		'letter_spacing'   => self::get_letter_spacing_class_and_style( $attributes ),
		'line_height'      => self::get_line_height_class_and_style( $attributes ),
		'margin'           => self::get_margin_class_and_style( $attributes ),
		'padding'          => self::get_padding_class_and_style( $attributes ),
		'text_align'       => self::get_text_align_class_and_style( $attributes ),
		'text_color'       => self::get_text_color_class_and_style( $attributes ),
		'text_decoration'  => self::get_text_decoration_class_and_style( $attributes ),
		'text_transform'   => self::get_text_transform_class_and_style( $attributes ),
	);

	if ( ! empty( $properties ) ) {
		foreach ( $classes_and_styles as $key => $value ) {
			if ( ! in_array( $key, $properties, true ) ) {
				unset( $classes_and_styles[ $key ] );
			}
		}
	}

	if ( ! empty( $exclude ) ) {
		foreach ( $classes_and_styles as $key => $value ) {
			if ( in_array( $key, $exclude, true ) ) {
				unset( $classes_and_styles[ $key ] );
			}
		}
	}

	$classes_and_styles = array_filter( $classes_and_styles );

	$classes = array_map(
		function( $item ) {
			return $item['class'];
		},
		$classes_and_styles
	);

	$styles = array_map(
		function( $item ) {
			return $item['style'];
		},
		// Exclude link color styles from parent to avoid conflict with text color.
		array_diff_key(
			$classes_and_styles,
			array_flip( array( 'link_color' ) )
		)
	);

	$classes = array_filter( $classes );
	$styles  = array_filter( $styles );

	return array(
		'classes' => implode( ' ', $classes ),
		'styles'  => implode( ' ', $styles ),
	);
}