), $email_theme_settings['color']['palette']['default'] ?? array() ); foreach ( $color_definitions as $color ) { $css_presets .= ".has-{$color['slug']}-color { color: {$color['color']}; } \n"; $css_presets .= ".has-{$color['slug']}-background-color { background-color: {$color['color']}; } \n"; $css_presets .= ".has-{$color['slug']}-border-color { border-color: {$color['color']}; } \n"; } // Block specific styles. $css_blocks = ''; $blocks = $this->get_theme()->get_styles_block_nodes(); foreach ( $blocks as $block_metadata ) { $css_blocks .= $this->get_theme()->get_styles_for_block( $block_metadata ); } // Remove `:root :where(...)` selectors since they are not supported in the CSS inliner. $css_blocks = preg_replace( '/:root\s:where\((.*?)\)/', '$1', $css_blocks ); // Element specific styles. $elements_styles = $this->get_theme()->get_raw_data()['styles']['elements'] ?? array(); // Because the section styles is not a part of the output the `get_styles_block_nodes` method, we need to get it separately. if ( $template && $template->wp_id ) { $template_theme = (array) get_post_meta( $template->wp_id, Email_Editor::WOOCOMMERCE_EMAIL_META_THEME_TYPE, true ); $template_styles = (array) ( $template_theme['styles'] ?? array() ); $template_elements = $template_styles['elements'] ?? array(); $elements_styles = array_replace_recursive( (array) $elements_styles, (array) $template_elements ); } if ( $post ) { $post_theme = (array) get_post_meta( $post->ID, 'woocommerce_email_theme', true ); $post_styles = (array) ( $post_theme['styles'] ?? array() ); $post_elements = $post_styles['elements'] ?? array(); $elements_styles = array_replace_recursive( (array) $elements_styles, (array) $post_elements ); } $css_elements = ''; foreach ( $elements_styles as $key => $elements_style ) { $selector = $key; if ( 'button' === $key ) { $selector = '.wp-block-button'; $css_elements .= wp_style_engine_get_styles( $elements_style, array( 'selector' => '.wp-block-button' ) )['css'] ?? ''; // Add color to link element. $css_elements .= wp_style_engine_get_styles( array( 'color' => array( 'text' => $elements_style['color']['text'] ?? '' ) ), array( 'selector' => '.wp-block-button a' ) )['css'] ?? ''; continue; } switch ( $key ) { case 'heading': $selector = 'h1, h2, h3, h4, h5, h6'; break; case 'link': $selector = 'a:not(.button-link)'; break; } $css_elements .= wp_style_engine_get_styles( $elements_style, array( 'selector' => $selector ) )['css'] ?? ''; } $result = $css_presets . $css_blocks . $css_elements; // Because font-size can by defined by the clamp() function that is not supported in the e-mail clients, we need to replace it to the value. // Regular expression to match clamp() function and capture its max value. $pattern = '/clamp\([^,]+,\s*[^,]+,\s*([^)]+)\)/'; // Replace clamp() with its maximum value. $result = (string) preg_replace( $pattern, '$1', $result ); return $result; } public function translate_slug_to_font_size( string $font_size ): string { $settings = $this->get_settings(); foreach ( $settings['typography']['fontSizes']['default'] as $font_size_definition ) { if ( $font_size_definition['slug'] === $font_size ) { return $font_size_definition['size']; } } return $font_size; } public function translate_slug_to_color( string $color_slug ): string { $settings = $this->get_settings(); $color_definitions = array_merge( $settings['color']['palette']['theme'] ?? array(), $settings['color']['palette']['default'] ?? array() ); foreach ( $color_definitions as $color_definition ) { if ( $color_definition['slug'] === $color_slug ) { return strtolower( $color_definition['color'] ); } } return $color_slug; } public function get_variables_values_map(): array { $variables_css = $this->get_theme()->get_stylesheet( array( 'variables' ) ); $map = array(); // Regular expression to match CSS variable definitions. $pattern = '/--(.*?):\s*(.*?);/'; if ( preg_match_all( $pattern, $variables_css, $matches, PREG_SET_ORDER ) ) { foreach ( $matches as $match ) { // '--' . $match[1] is the variable name, $match[2] is the variable value. $map[ '--' . $match[1] ] = $match[2]; } } return $map; } }