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



wp_get_color_classes_and_styles › WordPress Function

Od7.3.0
Przestarzałyn/a
› wp_get_color_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 color classes and inline styles for block attributes, like the JS `getColorClassesAndStyles()`. Does not check block support or skipped serialization.



Źródło

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

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

	return $attributes;
}