Automattic\WooCommerce\EmailEditor\Integrations\Utils

Styles_Helper::clamp_to_static_pxpublic staticWC 1.0

Convert a CSS clamp() value to a static px value for email clients.

Method of the class: Styles_Helper{}

No Hooks.

Returns

?String. The static pixel value (e.g., 30px).

Usage

$result = Styles_Helper::clamp_to_static_px( $clamp_str, $strategy ): ?string;
$clamp_str(string) (required)
The clamp() CSS string (e.g., "clamp(30px, 5vw, 50px)").
$strategy(string)
"min"|"max"|"avg" — which strategy to use.
Default: 'min'

Styles_Helper::clamp_to_static_px() code WC 10.7.0

public static function clamp_to_static_px( $clamp_str, $strategy = 'min' ): ?string {
	if ( stripos( $clamp_str, 'clamp(' ) === false ) {
		return $clamp_str;
	}

	$value_array = explode( ',', $clamp_str );

	if ( count( $value_array ) < 2 ) {
		return $clamp_str; // Invalid clamp format.
	}

	$first_element = $value_array[0];
	$min           = trim( str_ireplace( array( 'clamp(', 'min(', 'max(' ), '', $first_element ) );

	$last_element = $value_array[ count( $value_array ) - 1 ];
	$max          = trim( rtrim( $last_element, ')' ) );

	$min_px = self::convert_to_px( $min, false );
	$max_px = self::convert_to_px( $max, false );

	// Determine which value to use.
	if ( 'min' === $strategy && ! is_null( $min_px ) ) {
		return $min_px;
	}
	if ( 'max' === $strategy && ! is_null( $max_px ) ) {
		return $max_px;
	}
	if ( 'avg' === $strategy && ! is_null( $min_px ) && ! is_null( $max_px ) ) {
		$avg = ( self::parse_value( $min_px ) + self::parse_value( $max_px ) ) / 2;
		return $avg . 'px';
	}
	// Default.
	return $min_px ?? $max_px ?? $clamp_str;
}