mailpoet
/
vendor
/
woocommerce
/
email-editor
/
src
/
Integrations
/
Utils
/
class-styles-helper.php
class-dom-document-helper.php
2 weeks ago
class-html-processing-helper.php
2 weeks ago
class-social-links-helper.php
11 months ago
class-styles-helper.php
6 months ago
class-table-wrapper-helper.php
11 months ago
index.php
11 months ago
class-styles-helper.php
219 lines
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | namespace Automattic\WooCommerce\EmailEditor\Integrations\Utils; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use Automattic\WooCommerce\EmailEditor\Engine\Renderer\ContentRenderer\Rendering_Context; |
| 6 | use WP_Style_Engine; |
| 7 | class Styles_Helper { |
| 8 | public static $empty_block_styles = array( |
| 9 | 'css' => '', |
| 10 | 'declarations' => array(), |
| 11 | 'classnames' => '', |
| 12 | ); |
| 13 | public static function parse_value( $value ): float { |
| 14 | // Handle numeric values. |
| 15 | if ( is_numeric( $value ) ) { |
| 16 | return (float) $value; |
| 17 | } |
| 18 | // Handle string values. |
| 19 | if ( is_string( $value ) ) { |
| 20 | if ( preg_match( '/^\s*(-?\d+(?:\.\d+)?)/', $value, $m ) ) { |
| 21 | return (float) $m[1]; |
| 22 | } |
| 23 | } |
| 24 | return 0.0; |
| 25 | } |
| 26 | public static function parse_styles_to_array( string $styles ): array { |
| 27 | $styles = explode( ';', $styles ); |
| 28 | $parsed_styles = array(); |
| 29 | foreach ( $styles as $style ) { |
| 30 | $style = explode( ':', $style, 2 ); |
| 31 | if ( count( $style ) === 2 ) { |
| 32 | $parsed_styles[ trim( $style[0] ) ] = trim( $style[1] ); |
| 33 | } |
| 34 | } |
| 35 | return $parsed_styles; |
| 36 | } |
| 37 | public static function get_normalized_block_styles( array $block_attributes, Rendering_Context $rendering_context ): array { |
| 38 | $normalized_colors = array_filter( |
| 39 | array( |
| 40 | 'color' => array_filter( |
| 41 | array( |
| 42 | 'background' => isset( $block_attributes['backgroundColor'] ) && $block_attributes['backgroundColor'] |
| 43 | ? $rendering_context->translate_slug_to_color( $block_attributes['backgroundColor'] ) |
| 44 | : null, |
| 45 | 'text' => isset( $block_attributes['textColor'] ) && $block_attributes['textColor'] |
| 46 | ? $rendering_context->translate_slug_to_color( $block_attributes['textColor'] ) |
| 47 | : null, |
| 48 | ) |
| 49 | ), |
| 50 | 'border' => array_filter( |
| 51 | array( |
| 52 | 'color' => isset( $block_attributes['borderColor'] ) && $block_attributes['borderColor'] |
| 53 | ? $rendering_context->translate_slug_to_color( $block_attributes['borderColor'] ) |
| 54 | : null, |
| 55 | ) |
| 56 | ), |
| 57 | ) |
| 58 | ); |
| 59 | return array_replace_recursive( |
| 60 | $normalized_colors, |
| 61 | $block_attributes['style'] ?? array() |
| 62 | ); |
| 63 | } |
| 64 | public static function get_styles_from_block( array $block_styles, $skip_convert_vars = false ) { |
| 65 | $unsupported_props = array( |
| 66 | 'margin' => array( 'spacing', 'margin' ), |
| 67 | ); |
| 68 | $unsupported_props = apply_filters( 'woocommerce_email_editor_styles_unsupported_props', $unsupported_props ); |
| 69 | foreach ( $unsupported_props as $path ) { |
| 70 | if ( ! is_array( $path ) || count( $path ) === 0 ) { |
| 71 | continue; |
| 72 | } |
| 73 | $pointer = & $block_styles; |
| 74 | $last_key = array_pop( $path ); |
| 75 | foreach ( $path as $segment ) { |
| 76 | if ( ! is_string( $segment ) && ! is_int( $segment ) ) { |
| 77 | continue 2; |
| 78 | } |
| 79 | if ( ! array_key_exists( $segment, $pointer ) || ! is_array( $pointer[ $segment ] ) ) { |
| 80 | continue 2; |
| 81 | } |
| 82 | $pointer = & $pointer[ $segment ]; |
| 83 | } |
| 84 | if ( ( is_string( $last_key ) || is_int( $last_key ) ) && array_key_exists( $last_key, $pointer ) ) { |
| 85 | unset( $pointer[ $last_key ] ); |
| 86 | } |
| 87 | } |
| 88 | return wp_parse_args( |
| 89 | wp_style_engine_get_styles( $block_styles, array( 'convert_vars_to_classnames' => $skip_convert_vars ) ), |
| 90 | self::$empty_block_styles |
| 91 | ); |
| 92 | } |
| 93 | public static function extend_block_styles( array $block_styles, array $css_declarations ) { |
| 94 | // Ensure block_styles has the required WP_Style_Engine structure. |
| 95 | if ( ! isset( $block_styles['declarations'] ) || ! is_array( $block_styles['declarations'] ) ) { |
| 96 | $block_styles = self::$empty_block_styles; |
| 97 | } |
| 98 | $block_styles['declarations'] = array_merge( $block_styles['declarations'], $css_declarations ); |
| 99 | $block_styles['css'] = WP_Style_Engine::compile_css( $block_styles['declarations'], '' ); |
| 100 | return $block_styles; |
| 101 | } |
| 102 | public static function get_block_styles( array $block_attributes, Rendering_Context $rendering_context, array $properties ) { |
| 103 | $styles = self::get_normalized_block_styles( $block_attributes, $rendering_context ); |
| 104 | $filtered_styles = array(); |
| 105 | $style_mappings = array( |
| 106 | 'spacing' => array( 'spacing' ), |
| 107 | 'padding' => array( 'spacing', 'padding' ), |
| 108 | 'margin' => array( 'spacing', 'margin' ), |
| 109 | 'border' => array( 'border' ), |
| 110 | 'border-width' => array( 'border', 'width' ), |
| 111 | 'border-style' => array( 'border', 'style' ), |
| 112 | 'border-radius' => array( 'border', 'radius' ), |
| 113 | 'border-color' => array( 'border', 'color' ), |
| 114 | 'background' => array( 'background' ), |
| 115 | 'background-color' => array( 'color', 'background' ), |
| 116 | 'color' => array( 'color', 'text' ), |
| 117 | 'typography' => array( 'typography' ), |
| 118 | 'font-size' => array( 'typography', 'fontSize' ), |
| 119 | 'font-family' => array( 'typography', 'fontFamily' ), |
| 120 | 'font-weight' => array( 'typography', 'fontWeight' ), |
| 121 | ); |
| 122 | foreach ( $properties as $property ) { |
| 123 | if ( ! isset( $style_mappings[ $property ] ) ) { |
| 124 | continue; |
| 125 | } |
| 126 | $style_pointer = $styles; |
| 127 | foreach ( $style_mappings[ $property ] as $path_segment ) { |
| 128 | if ( ! isset( $style_pointer[ $path_segment ] ) ) { |
| 129 | continue 2; |
| 130 | } |
| 131 | $style_pointer = $style_pointer[ $path_segment ]; |
| 132 | } |
| 133 | $filtered_styles_pointer = & $filtered_styles; |
| 134 | foreach ( $style_mappings[ $property ] as $path_index => $path_segment ) { |
| 135 | if ( count( $style_mappings[ $property ] ) - 1 === $path_index ) { |
| 136 | $filtered_styles_pointer[ $path_segment ] = $style_pointer; |
| 137 | break; |
| 138 | } |
| 139 | if ( ! isset( $filtered_styles_pointer[ $path_segment ] ) || ! is_array( $filtered_styles_pointer[ $path_segment ] ) ) { |
| 140 | $filtered_styles_pointer[ $path_segment ] = array(); |
| 141 | } |
| 142 | $filtered_styles_pointer = & $filtered_styles_pointer[ $path_segment ]; |
| 143 | } |
| 144 | } |
| 145 | $additional_css_declarations = array_filter( |
| 146 | array_intersect_key( |
| 147 | array( |
| 148 | 'text-align' => $block_attributes['textAlign'] ?? null, |
| 149 | ), |
| 150 | array_flip( $properties ) |
| 151 | ) |
| 152 | ); |
| 153 | $styles = count( $filtered_styles ) > 0 ? self::get_styles_from_block( $filtered_styles ) : self::$empty_block_styles; |
| 154 | return self::extend_block_styles( $styles, $additional_css_declarations ); |
| 155 | } |
| 156 | public static function convert_to_px( string $input, bool $use_fallback = true, ?int $base_font_size = 16 ): ?string { |
| 157 | $fallback = $use_fallback ? $base_font_size . 'px' : null; |
| 158 | if ( ! $input ) { |
| 159 | return $fallback; |
| 160 | } |
| 161 | $input = trim( $input ); |
| 162 | // Validate input against potentially malicious values. |
| 163 | if ( preg_match( '/[<>"\']/', $input ) ) { |
| 164 | return $fallback; |
| 165 | } |
| 166 | if ( str_ends_with( $input, 'px' ) ) { |
| 167 | // If already in px, return as is. |
| 168 | return $input; |
| 169 | } |
| 170 | if ( str_ends_with( $input, 'rem' ) || str_ends_with( $input, 'em' ) ) { |
| 171 | // Convert rem/em to px (assuming 16px base). |
| 172 | $value = (float) str_replace( array( 'rem', 'em' ), '', $input ); |
| 173 | return round( $value * $base_font_size ) . 'px'; |
| 174 | } |
| 175 | if ( str_ends_with( $input, '%' ) ) { |
| 176 | // Convert percentage to px (assuming 16px base). |
| 177 | $value = (float) str_replace( '%', '', $input ); |
| 178 | return round( ( $value / 100 ) * $base_font_size ) . 'px'; |
| 179 | } |
| 180 | if ( is_numeric( $input ) ) { |
| 181 | // If it's just a number, assume px. |
| 182 | return $input . 'px'; |
| 183 | } |
| 184 | return $fallback; |
| 185 | } |
| 186 | public static function remove_css_unit( string $input ): string { |
| 187 | $units = array( 'px', 'pt', 'pc', 'rem', 'em', 'vmin', 'vmax', '%', 'vh', 'vw', 'ex', 'ch', 'fr' ); |
| 188 | return str_ireplace( $units, '', $input ); |
| 189 | } |
| 190 | public static function clamp_to_static_px( $clamp_str, $strategy = 'min' ): ?string { |
| 191 | if ( stripos( $clamp_str, 'clamp(' ) === false ) { |
| 192 | return $clamp_str; |
| 193 | } |
| 194 | $value_array = explode( ',', $clamp_str ); |
| 195 | if ( count( $value_array ) < 2 ) { |
| 196 | return $clamp_str; // Invalid clamp format. |
| 197 | } |
| 198 | $first_element = $value_array[0]; |
| 199 | $min = trim( str_ireplace( array( 'clamp(', 'min(', 'max(' ), '', $first_element ) ); |
| 200 | $last_element = $value_array[ count( $value_array ) - 1 ]; |
| 201 | $max = trim( rtrim( $last_element, ')' ) ); |
| 202 | $min_px = self::convert_to_px( $min, false ); |
| 203 | $max_px = self::convert_to_px( $max, false ); |
| 204 | // Determine which value to use. |
| 205 | if ( 'min' === $strategy && ! is_null( $min_px ) ) { |
| 206 | return $min_px; |
| 207 | } |
| 208 | if ( 'max' === $strategy && ! is_null( $max_px ) ) { |
| 209 | return $max_px; |
| 210 | } |
| 211 | if ( 'avg' === $strategy && ! is_null( $min_px ) && ! is_null( $max_px ) ) { |
| 212 | $avg = ( self::parse_value( $min_px ) + self::parse_value( $max_px ) ) / 2; |
| 213 | return $avg . 'px'; |
| 214 | } |
| 215 | // Default. |
| 216 | return $min_px ?? $max_px ?? $clamp_str; |
| 217 | } |
| 218 | } |
| 219 |