Automattic\WooCommerce\Blocks\Utils

StyleAttributesUtils::get_classes_and_styles_by_attributes │ public static │ WC 1.0

Get classes and styles from attributes.

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

Метод класса: StyleAttributesUtils{}

Хуков нет.

Возвращает

array.

Использование

$result = StyleAttributesUtils::get_classes_and_styles_by_attributes( $attributes, $properties, $exclude );
$attributes(массив) (обязательный)
Block attributes.
$properties(массив)
Properties to get classes/styles from.
По умолчанию: array()
$exclude(массив)
Properties to exclude.
По умолчанию: array()

Код StyleAttributesUtils::get_classes_and_styles_by_attributes() WC 11.3.0

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

	if ( ! empty( $properties ) ) {
		$class_style_map = array_intersect_key( $class_style_map, array_flip( $properties ) );
	}

	if ( ! empty( $exclude ) ) {
		$class_style_map = array_diff_key( $class_style_map, array_flip( $exclude ) );
	}

	$classes_and_styles = array();
	foreach ( $class_style_map as $key => $method ) {
		$classes_and_styles[ $key ] = call_user_func( array( self::class, $method ), $attributes );
	}

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