Automattic\WooCommerce\EmailEditor\Integrations\Utils
Html_Processing_Helper::sanitize_image_styles
Sanitize inline styles for image elements - only allow safe properties for email rendering.
Method of the class: Html_Processing_Helper{}
No Hooks.
Returns
String. Sanitized style value.
Usage
$result = Html_Processing_Helper::sanitize_image_styles( $style_value ): string;
- $style_value(string) (required)
- Raw style value.
Html_Processing_Helper::sanitize_image_styles() Html Processing Helper::sanitize image styles code WC 10.8.1
private static function sanitize_image_styles( string $style_value ): string {
$sanitized_styles = array();
$style_parts = explode( ';', $style_value );
foreach ( $style_parts as $style_part ) {
$style_part = trim( $style_part );
if ( empty( $style_part ) ) {
continue;
}
$property_parts = explode( ':', $style_part, 2 );
if ( count( $property_parts ) !== 2 ) {
continue;
}
$property = trim( strtolower( $property_parts[0] ) );
$value = trim( $property_parts[1] );
// Allow safe CSS properties for images in email rendering.
$safe_properties = array( 'width', 'height', 'max-width', 'max-height', 'display', 'margin', 'padding', 'border', 'border-radius' );
if ( in_array( $property, $safe_properties, true ) ) {
$sanitized_value = self::sanitize_css_value( $value );
if ( ! empty( $sanitized_value ) ) {
$sanitized_styles[] = $property . ': ' . $sanitized_value;
}
}
}
return implode( '; ', $sanitized_styles );
}