Automattic\WooCommerce\EmailEditor\Engine\Renderer\ContentRenderer\Postprocessors

Border_Style_Postprocessor::process_styleprivateWC 1.0

Processes a style string to ensure border-style is set for borders with width > 0 and removes extra border-style properties.

Method of the class: Border_Style_Postprocessor{}

No Hooks.

Returns

String.

Usage

// private - for code of main (parent) class only
$result = $this->process_style( $style ): string;
$style(string) (required)
The style attribute value.

Border_Style_Postprocessor::process_style() code WC 9.9.5

private function process_style( string $style ): string {
	// Parse style into associative array.
	$styles = array();
	foreach ( explode( ';', $style ) as $declaration ) {
		if ( strpos( $declaration, ':' ) !== false ) {
			list( $prop, $value )          = array_map( 'trim', explode( ':', $declaration, 2 ) );
			$styles[ strtolower( $prop ) ] = $value;
		}
	}

	$should_update_style = false;

	// Collect border-widths and styles.
	$border_widths = array();
	$border_styles = array();

	foreach ( $styles as $prop => $value ) {
		if ( 'border' === $prop ) {
			$border_width = $this->extract_width_from_shorthand_value( $value );

			if ( $border_width ) {
				$border_widths['top']    = $border_width;
				$border_widths['right']  = $border_width;
				$border_widths['bottom'] = $border_width;
				$border_widths['left']   = $border_width;
			}

			$border_style = $this->extract_style_from_shorthand_value( $value );
			if ( $border_style ) {
				$border_styles['top']    = $border_style;
				$border_styles['right']  = $border_style;
				$border_styles['bottom'] = $border_style;
				$border_styles['left']   = $border_style;
			}
		}

		if ( preg_match( '/^border-(top|right|bottom|left)$/', $prop, $matches ) ) {
			$border_width = $this->extract_width_from_shorthand_value( $value );
			if ( $border_width ) {
				$border_widths[ $matches[1] ] = $border_width;
			}

			$border_style = $this->extract_style_from_shorthand_value( $value );
			if ( $border_style ) {
				$border_styles[ $matches[1] ] = $border_style;
			}
		}

		if ( 'border-width' === $prop ) {
			$border_widths = array_merge( $border_widths, $this->expand_shorthand_value( $value ) );
		}

		if ( preg_match( '/^border-(top|right|bottom|left)-width$/', $prop, $matches ) ) {
			$border_widths[ $matches[1] ] = $value;
		}

		if ( 'border-style' === $prop ) {
			$border_styles = array_merge( $border_styles, $this->expand_shorthand_value( $value ) );

			// Remove the original border style declaration, as it will be added later.
			unset( $styles[ $prop ] );
			$should_update_style = true;
		}

		if ( preg_match( '/^border-(top|right|bottom|left)-style$/', $prop, $matches ) ) {
			$border_styles[ $matches[1] ] = $value;

			// Remove the original border style declaration, as it will be added later.
			unset( $styles[ $prop ] );
			$should_update_style = true;
		}
	}

	if ( array_diff( array_keys( $border_widths ), array_keys( $border_styles ) ) ) {
		$should_update_style = true;
	}

	if ( ! $should_update_style ) {
		return $style;
	}

	$border_styles_declarations = array();

	foreach ( $border_widths as $side => $value ) {
		if ( $this->is_nonzero_width( $value ) ) {
			$border_styles_declarations[ "border-$side-style" ] = isset( $border_styles[ $side ] ) ? $border_styles[ $side ] : 'solid';
		}
	}

	// If border style declarations for all sides are present and have the same value, use shorthand syntax.
	if ( 4 === count( $border_styles_declarations ) && 1 === count( array_unique( $border_styles_declarations ) ) ) {
		$border_styles_declarations = array( 'border-style' => array_values( $border_styles_declarations )[0] );
	}

	$merged_styles = array_merge( $styles, $border_styles_declarations );
	$updated_style = '';

	foreach ( $merged_styles as $prop => $value ) {
		if ( '' !== $value ) {
			$updated_style .= $prop . ': ' . $value . '; ';
		}
	}

	return trim( $updated_style );
}