without_css_inline( $post, $template ); $template_html = $content_result['html']; $content_styles = $content_result['styles']; $layout = $this->theme_controller->get_layout_settings(); ob_start(); include self::TEMPLATE_FILE; $rendered_template = (string) ob_get_clean(); $template_styles = WP_Style_Engine::compile_css( array( 'background-color' => $email_styles['color']['background'] ?? 'inherit', 'color' => $email_styles['color']['text'] ?? 'inherit', 'padding-top' => $email_styles['spacing']['padding']['top'] ?? '0px', 'padding-bottom' => $email_styles['spacing']['padding']['bottom'] ?? '0px', 'font-family' => $email_styles['typography']['fontFamily'] ?? 'inherit', 'line-height' => $email_styles['typography']['lineHeight'] ?? '1.5', 'font-size' => $email_styles['typography']['fontSize'] ?? 'inherit', ), 'body, .email_layout_wrapper' ); $template_styles .= '.email_layout_wrapper { box-sizing: border-box;}'; $template_styles .= file_get_contents( __DIR__ . '/' . self::TEMPLATE_STYLES_FILE ); $template_styles = wp_strip_all_tags( (string) apply_filters( 'woocommerce_email_renderer_styles', $template_styles, $post ) ); // Single CSS inlining pass: combine content and template styles, then inline all at once. $all_styles = ''; $rendered_template = $this->inline_css_styles( $all_styles . $rendered_template ); // Postprocess after CSS inlining (border normalization, CSS variable replacement, etc.). $rendered_template = $this->process_manager->postprocess( $rendered_template ); // This is a workaround to support link :hover in some clients. Ideally we would remove the ability to set :hover // however this is not possible using the color panel from Gutenberg. if ( isset( $email_styles['elements']['link'][':hover']['color']['text'] ) ) { $rendered_template = str_replace( '', '', $rendered_template ); } return array( 'html' => $rendered_template, 'text' => $this->render_text_version( $rendered_template ), ); } /** * Inlines CSS styles into the HTML * * @param string $template HTML template. * @return string */ private function inline_css_styles( $template ) { return $this->css_inliner->from_html( $template )->inline_css()->render(); } /** * Renders the text version of the email template. * * @param string $template HTML template. * @return string */ private function render_text_version( $template ) { $template = ( mb_detect_encoding( $template, 'UTF-8', true ) ) ? $template : mb_convert_encoding( $template, 'UTF-8', mb_list_encodings() ); // Ensure template is a string before processing. if ( ! is_string( $template ) ) { return ''; } // Preserve personalization tags by temporarily replacing them with unique placeholders. $template = $this->preserve_personalization_tags( $template ); $result = Html2Text::convert( (string) $template, array( 'ignore_errors' => true ) ); if ( ! $result ) { return ''; } // Restore personalization tags from placeholders. $result = $this->restore_personalization_tags( $result ); return $result; } /** * Preserves personalization tags by replacing them with unique placeholders (not inside comments). * * @param string $template HTML template. * @return string */ private function preserve_personalization_tags( string $template ): string { $all_registered_tags = $this->personalization_tags_registry->get_all(); $this->personalization_tag_placeholders = array(); $counter = 0; $base_tokens = array(); // All the tokens used in the email, e.g. [woocommerce/customer-username]. $token_prefixes = array(); // All the used prefixes, e.g. woocommerce, mailpoet, etc. foreach ( $all_registered_tags as $tag ) { $token = $tag->get_token(); // E.g. [woocommerce/customer-username]. $base_tokens[ $token ] = true; // Remove brackets for regex matching, escape for regex. $token_prefixes[] = preg_quote( substr( $token, 1, -1 ), '/' ); } if ( empty( $token_prefixes ) ) { return $template; } // Match all of the code comments that look like a personalization tags. $pattern = '//'; $template = preg_replace_callback( $pattern, function ( $matches ) use ( &$counter, $base_tokens ) { // $matches[1] is the token without brackets, add brackets for lookup. $base_token = '[' . $matches[1] . ']'; if ( isset( $base_tokens[ $base_token ] ) ) { $placeholder = 'PERSONALIZATION_TAG_PLACEHOLDER_' . $counter; $this->personalization_tag_placeholders[ $placeholder ] = $matches[0]; ++$counter; return $placeholder; } return $matches[0]; }, $template ); return $template ?? ''; } /** * Restores personalization tags from placeholders * * @param string $text Text content. * @return string */ private function restore_personalization_tags( string $text ): string { if ( empty( $this->personalization_tag_placeholders ) ) { return $text; } foreach ( $this->personalization_tag_placeholders as $placeholder => $html_comment ) { $text = str_replace( $placeholder, $html_comment, $text ); } return $text; } }