ContentRenderer
3 days ago
class-html2text-exception.php
11 months ago
class-html2text.php
11 months ago
class-renderer.php
3 days ago
index.php
11 months ago
interface-css-inliner.php
11 months ago
template-canvas.css
3 months ago
template-canvas.php
3 months ago
class-renderer.php
187 lines
| 1 | <?php |
| 2 | declare(strict_types = 1); |
| 3 | namespace Automattic\WooCommerce\EmailEditor\Engine\Renderer; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use Automattic\WooCommerce\EmailEditor\Engine\Renderer\ContentRenderer\Content_Renderer; |
| 6 | use Automattic\WooCommerce\EmailEditor\Engine\Renderer\ContentRenderer\Process_Manager; |
| 7 | use Automattic\WooCommerce\EmailEditor\Engine\Renderer\ContentRenderer\Rendering_Context; |
| 8 | use Automattic\WooCommerce\EmailEditor\Engine\Templates\Templates; |
| 9 | use Automattic\WooCommerce\EmailEditor\Engine\Theme_Controller; |
| 10 | use Automattic\WooCommerce\EmailEditor\Engine\PersonalizationTags\Personalization_Tags_Registry; |
| 11 | use WP_Style_Engine; |
| 12 | class Renderer { |
| 13 | private Theme_Controller $theme_controller; |
| 14 | private Content_Renderer $content_renderer; |
| 15 | private Templates $templates; |
| 16 | private Css_Inliner $css_inliner; |
| 17 | private Process_Manager $process_manager; |
| 18 | private Personalization_Tags_Registry $personalization_tags_registry; |
| 19 | private array $personalization_tag_placeholders = array(); |
| 20 | const TEMPLATE_FILE = 'template-canvas.php'; |
| 21 | const TEMPLATE_STYLES_FILE = 'template-canvas.css'; |
| 22 | public function __construct( |
| 23 | Content_Renderer $content_renderer, |
| 24 | Templates $templates, |
| 25 | Css_Inliner $css_inliner, |
| 26 | Theme_Controller $theme_controller, |
| 27 | Personalization_Tags_Registry $personalization_tags_registry, |
| 28 | Process_Manager $process_manager |
| 29 | ) { |
| 30 | $this->content_renderer = $content_renderer; |
| 31 | $this->templates = $templates; |
| 32 | $this->theme_controller = $theme_controller; |
| 33 | $this->css_inliner = $css_inliner; |
| 34 | $this->personalization_tags_registry = $personalization_tags_registry; |
| 35 | $this->process_manager = $process_manager; |
| 36 | } |
| 37 | public function render( \WP_Post $post, string $subject, string $pre_header, string $language, string $meta_robots = '', string $template_slug = '' ): array { |
| 38 | if ( ! $template_slug ) { |
| 39 | $template_slug = get_page_template_slug( $post ) ? get_page_template_slug( $post ) : 'email-general'; |
| 40 | } |
| 41 | // phpcs:ignore Generic.Commenting.DocComment.MissingShort -- used for phpstan |
| 42 | $template = $this->templates->get_block_template( $template_slug ); |
| 43 | $previous_rendering_context = $this->content_renderer->get_current_rendering_context(); |
| 44 | try { |
| 45 | $rendering_context = $this->content_renderer->create_rendering_context( $language, $post, $template ); |
| 46 | $this->content_renderer->set_rendering_context( $rendering_context ); |
| 47 | $email_styles = $this->theme_controller->get_styles(); |
| 48 | $content_result = $this->content_renderer->render_without_css_inline( $post, $template ); |
| 49 | $template_html = $content_result['html']; |
| 50 | $content_styles = $content_result['styles']; |
| 51 | $layout = $this->theme_controller->get_layout_settings(); |
| 52 | ob_start(); |
| 53 | include self::TEMPLATE_FILE; |
| 54 | $rendered_template = (string) ob_get_clean(); |
| 55 | $template_styles = |
| 56 | WP_Style_Engine::compile_css( |
| 57 | array( |
| 58 | 'background-color' => $email_styles['color']['background'] ?? 'inherit', |
| 59 | 'color' => $email_styles['color']['text'] ?? 'inherit', |
| 60 | 'padding-top' => $email_styles['spacing']['padding']['top'] ?? '0px', |
| 61 | 'padding-bottom' => $email_styles['spacing']['padding']['bottom'] ?? '0px', |
| 62 | 'font-family' => $email_styles['typography']['fontFamily'] ?? 'inherit', |
| 63 | 'font-weight' => $email_styles['typography']['fontWeight'] ?? 'inherit', |
| 64 | 'font-style' => $email_styles['typography']['fontStyle'] ?? 'inherit', |
| 65 | 'letter-spacing' => $email_styles['typography']['letterSpacing'] ?? 'normal', |
| 66 | 'line-height' => $email_styles['typography']['lineHeight'] ?? '1.5', |
| 67 | 'font-size' => $email_styles['typography']['fontSize'] ?? 'inherit', |
| 68 | 'direction' => $rendering_context->get_text_direction(), |
| 69 | 'text-align' => $rendering_context->get_default_text_align(), |
| 70 | ), |
| 71 | 'body, .email_layout_wrapper' |
| 72 | ); |
| 73 | $template_styles .= WP_Style_Engine::compile_css( |
| 74 | array( |
| 75 | 'direction' => $rendering_context->get_text_direction(), |
| 76 | 'text-align' => $rendering_context->get_default_text_align(), |
| 77 | ), |
| 78 | '.email_content_wrapper, .email_preheader' |
| 79 | ); |
| 80 | $template_styles .= '.email_layout_wrapper { box-sizing: border-box;}'; |
| 81 | $template_styles .= file_get_contents( __DIR__ . '/' . self::TEMPLATE_STYLES_FILE ); |
| 82 | $template_styles = wp_strip_all_tags( (string) apply_filters( 'woocommerce_email_renderer_styles', $template_styles, $post ) ); |
| 83 | // Single CSS inlining pass: combine content and template styles, then inline all at once. |
| 84 | $all_styles = '<style>' . $template_styles . $content_styles . '</style>'; |
| 85 | $rendered_template = $this->inline_css_styles( $all_styles . $rendered_template ); |
| 86 | // Postprocess after CSS inlining (border normalization, CSS variable replacement, etc.). |
| 87 | $rendered_template = $this->process_manager->postprocess( $rendered_template ); |
| 88 | $rendered_template = $this->apply_html_attributes( $rendered_template, $rendering_context ); |
| 89 | // This is a workaround to support link :hover in some clients. Ideally we would remove the ability to set :hover |
| 90 | // however this is not possible using the color panel from Gutenberg. |
| 91 | if ( isset( $email_styles['elements']['link'][':hover']['color']['text'] ) ) { |
| 92 | $rendered_template = str_replace( '<!-- Forced Styles -->', '<style>a:hover { color: ' . esc_attr( $email_styles['elements']['link'][':hover']['color']['text'] ) . ' !important; }</style>', $rendered_template ); |
| 93 | } |
| 94 | return array( |
| 95 | 'html' => $rendered_template, |
| 96 | 'text' => $this->render_text_version( $rendered_template ), |
| 97 | ); |
| 98 | } finally { |
| 99 | $this->content_renderer->restore_rendering_context( $previous_rendering_context ); |
| 100 | } |
| 101 | } |
| 102 | public function render_from_content( string $content, string $template_slug, string $subject, string $pre_header, string $language = 'en', string $meta_robots = '' ): array { |
| 103 | $synthetic_post = new \WP_Post( |
| 104 | (object) array( |
| 105 | 'ID' => 0, |
| 106 | 'post_status' => 'publish', |
| 107 | 'post_content' => $content, |
| 108 | ) |
| 109 | ); |
| 110 | return $this->render( $synthetic_post, $subject, $pre_header, $language, $meta_robots, $template_slug ); |
| 111 | } |
| 112 | private function inline_css_styles( $template ) { |
| 113 | return $this->css_inliner->from_html( $template )->inline_css()->render(); |
| 114 | } |
| 115 | private function apply_html_attributes( string $template, Rendering_Context $rendering_context ): string { |
| 116 | $processor = new \WP_HTML_Tag_Processor( $template ); |
| 117 | if ( ! $processor->next_tag( array( 'tag_name' => 'html' ) ) ) { |
| 118 | return $template; |
| 119 | } |
| 120 | $language = $rendering_context->get_language(); |
| 121 | if ( $language ) { |
| 122 | $processor->set_attribute( 'lang', str_replace( '_', '-', $language ) ); |
| 123 | } |
| 124 | $processor->set_attribute( 'dir', $rendering_context->get_text_direction() ); |
| 125 | return $processor->get_updated_html(); |
| 126 | } |
| 127 | private function render_text_version( $template ) { |
| 128 | $template = ( mb_detect_encoding( $template, 'UTF-8', true ) ) ? $template : mb_convert_encoding( $template, 'UTF-8', mb_list_encodings() ); |
| 129 | // Ensure template is a string before processing. |
| 130 | if ( ! is_string( $template ) ) { |
| 131 | return ''; |
| 132 | } |
| 133 | // Preserve personalization tags by temporarily replacing them with unique placeholders. |
| 134 | $template = $this->preserve_personalization_tags( $template ); |
| 135 | $result = Html2Text::convert( (string) $template, array( 'ignore_errors' => true ) ); |
| 136 | if ( ! $result ) { |
| 137 | return ''; |
| 138 | } |
| 139 | // Restore personalization tags from placeholders. |
| 140 | $result = $this->restore_personalization_tags( $result ); |
| 141 | return $result; |
| 142 | } |
| 143 | private function preserve_personalization_tags( string $template ): string { |
| 144 | $all_registered_tags = $this->personalization_tags_registry->get_all(); |
| 145 | $this->personalization_tag_placeholders = array(); |
| 146 | $counter = 0; |
| 147 | $base_tokens = array(); // All the tokens used in the email, e.g. [woocommerce/customer-username]. |
| 148 | $token_prefixes = array(); // All the used prefixes, e.g. woocommerce, mailpoet, etc. |
| 149 | foreach ( $all_registered_tags as $tag ) { |
| 150 | $token = $tag->get_token(); // E.g. [woocommerce/customer-username]. |
| 151 | $base_tokens[ $token ] = true; |
| 152 | // Remove brackets for regex matching, escape for regex. |
| 153 | $token_prefixes[] = preg_quote( substr( $token, 1, -1 ), '/' ); |
| 154 | } |
| 155 | if ( empty( $token_prefixes ) ) { |
| 156 | return $template; |
| 157 | } |
| 158 | // Match all of the code comments that look like a personalization tags. |
| 159 | $pattern = '/<!--\[(' . implode( '|', $token_prefixes ) . ')(?:\s+[^\]]*)?\]-->/'; |
| 160 | $template = preg_replace_callback( |
| 161 | $pattern, |
| 162 | function ( $matches ) use ( &$counter, $base_tokens ) { |
| 163 | // $matches[1] is the token without brackets, add brackets for lookup. |
| 164 | $base_token = '[' . $matches[1] . ']'; |
| 165 | if ( isset( $base_tokens[ $base_token ] ) ) { |
| 166 | $placeholder = 'PERSONALIZATION_TAG_PLACEHOLDER_' . $counter; |
| 167 | $this->personalization_tag_placeholders[ $placeholder ] = $matches[0]; |
| 168 | ++$counter; |
| 169 | return $placeholder; |
| 170 | } |
| 171 | return $matches[0]; |
| 172 | }, |
| 173 | $template |
| 174 | ); |
| 175 | return $template ?? ''; |
| 176 | } |
| 177 | private function restore_personalization_tags( string $text ): string { |
| 178 | if ( empty( $this->personalization_tag_placeholders ) ) { |
| 179 | return $text; |
| 180 | } |
| 181 | foreach ( $this->personalization_tag_placeholders as $placeholder => $html_comment ) { |
| 182 | $text = str_replace( $placeholder, $html_comment, $text ); |
| 183 | } |
| 184 | return $text; |
| 185 | } |
| 186 | } |
| 187 |