Automattic\WooCommerce\EmailEditor\Integrations\Utils

Styles_Helper::get_block_stylespublic staticWC 1.0

Get block styles.

Method of the class: Styles_Helper{}

No Hooks.

Returns

Array.

Usage

$result = Styles_Helper::get_block_styles( $block_attributes, $rendering_context, $properties );
$block_attributes(array) (required)
Block attributes.
$rendering_context(Rendering_Context) (required)
Rendering context.
$properties(array) (required)
List of style properties to include. Supported values: 'spacing', 'padding', 'margin', 'border', 'border-width', 'border-style', 'border-radius', 'border-color', 'background', 'background-color', 'color', 'typography', 'font-size', 'font-family', 'font-weight', 'text-align'.

Styles_Helper::get_block_styles() code WC 10.5.0

public static function get_block_styles( array $block_attributes, Rendering_Context $rendering_context, array $properties ) {
	$styles          = self::get_normalized_block_styles( $block_attributes, $rendering_context );
	$filtered_styles = array();
	$style_mappings  = array(
		'spacing'          => array( 'spacing' ),
		'padding'          => array( 'spacing', 'padding' ),
		'margin'           => array( 'spacing', 'margin' ),
		'border'           => array( 'border' ),
		'border-width'     => array( 'border', 'width' ),
		'border-style'     => array( 'border', 'style' ),
		'border-radius'    => array( 'border', 'radius' ),
		'border-color'     => array( 'border', 'color' ),
		'background'       => array( 'background' ),
		'background-color' => array( 'color', 'background' ),
		'color'            => array( 'color', 'text' ),
		'typography'       => array( 'typography' ),
		'font-size'        => array( 'typography', 'fontSize' ),
		'font-family'      => array( 'typography', 'fontFamily' ),
		'font-weight'      => array( 'typography', 'fontWeight' ),
	);

	foreach ( $properties as $property ) {
		if ( ! isset( $style_mappings[ $property ] ) ) {
			continue;
		}

		$style_pointer = $styles;
		foreach ( $style_mappings[ $property ] as $path_segment ) {
			if ( ! isset( $style_pointer[ $path_segment ] ) ) {
				continue 2;
			}

			$style_pointer = $style_pointer[ $path_segment ];
		}

		/**
		 * Pointer to filtered styles.
		 *
		 * @var array<string, mixed> $filtered_styles_pointer
		 */
		$filtered_styles_pointer = & $filtered_styles;

		foreach ( $style_mappings[ $property ] as $path_index => $path_segment ) {
			if ( count( $style_mappings[ $property ] ) - 1 === $path_index ) {
				$filtered_styles_pointer[ $path_segment ] = $style_pointer;
				break;
			}

			if ( ! isset( $filtered_styles_pointer[ $path_segment ] ) || ! is_array( $filtered_styles_pointer[ $path_segment ] ) ) {
				$filtered_styles_pointer[ $path_segment ] = array();
			}

			$filtered_styles_pointer = & $filtered_styles_pointer[ $path_segment ];
		}
	}

	$additional_css_declarations = array_filter(
		array_intersect_key(
			array(
				'text-align' => $block_attributes['textAlign'] ?? null,
			),
			array_flip( $properties )
		)
	);

	$styles = count( $filtered_styles ) > 0 ? self::get_styles_from_block( $filtered_styles ) : self::$empty_block_styles;

	return self::extend_block_styles( $styles, $additional_css_declarations );
}