ContentRenderer
1 month ago
class-html2text-exception.php
10 months ago
class-html2text.php
10 months ago
class-renderer.php
1 month ago
interface-css-inliner.php
1 year ago
template-canvas.css
1 month ago
template-canvas.php
1 month ago
class-renderer.php
305 lines
| 1 | <?php |
| 2 | /** |
| 3 | * This file is part of the WooCommerce Email Editor package. |
| 4 | * |
| 5 | * @package Automattic\WooCommerce\EmailEditor |
| 6 | */ |
| 7 | |
| 8 | declare(strict_types = 1); |
| 9 | namespace Automattic\WooCommerce\EmailEditor\Engine\Renderer; |
| 10 | |
| 11 | use Automattic\WooCommerce\EmailEditor\Engine\Renderer\ContentRenderer\Content_Renderer; |
| 12 | use Automattic\WooCommerce\EmailEditor\Engine\Renderer\ContentRenderer\Process_Manager; |
| 13 | use Automattic\WooCommerce\EmailEditor\Engine\Renderer\ContentRenderer\Rendering_Context; |
| 14 | use Automattic\WooCommerce\EmailEditor\Engine\Templates\Templates; |
| 15 | use Automattic\WooCommerce\EmailEditor\Engine\Theme_Controller; |
| 16 | use Automattic\WooCommerce\EmailEditor\Engine\PersonalizationTags\Personalization_Tags_Registry; |
| 17 | use WP_Style_Engine; |
| 18 | |
| 19 | /** |
| 20 | * Class Renderer |
| 21 | */ |
| 22 | class Renderer { |
| 23 | /** |
| 24 | * Theme controller |
| 25 | * |
| 26 | * @var Theme_Controller |
| 27 | */ |
| 28 | private Theme_Controller $theme_controller; |
| 29 | |
| 30 | /** |
| 31 | * Content renderer |
| 32 | * |
| 33 | * @var Content_Renderer |
| 34 | */ |
| 35 | private Content_Renderer $content_renderer; |
| 36 | |
| 37 | /** |
| 38 | * Templates |
| 39 | * |
| 40 | * @var Templates |
| 41 | */ |
| 42 | private Templates $templates; |
| 43 | |
| 44 | /** |
| 45 | * Css inliner |
| 46 | * |
| 47 | * @var Css_Inliner |
| 48 | */ |
| 49 | private Css_Inliner $css_inliner; |
| 50 | |
| 51 | /** |
| 52 | * Process manager |
| 53 | * |
| 54 | * @var Process_Manager |
| 55 | */ |
| 56 | private Process_Manager $process_manager; |
| 57 | |
| 58 | /** |
| 59 | * Personalization tags registry |
| 60 | * |
| 61 | * @var Personalization_Tags_Registry |
| 62 | */ |
| 63 | private Personalization_Tags_Registry $personalization_tags_registry; |
| 64 | |
| 65 | /** |
| 66 | * Map of placeholders to full HTML comment tags for restoration. |
| 67 | * |
| 68 | * @var array |
| 69 | */ |
| 70 | private array $personalization_tag_placeholders = array(); |
| 71 | |
| 72 | const TEMPLATE_FILE = 'template-canvas.php'; |
| 73 | const TEMPLATE_STYLES_FILE = 'template-canvas.css'; |
| 74 | |
| 75 | |
| 76 | /** |
| 77 | * Renderer constructor. |
| 78 | * |
| 79 | * @param Content_Renderer $content_renderer Content renderer. |
| 80 | * @param Templates $templates Templates. |
| 81 | * @param Css_Inliner $css_inliner CSS Inliner. |
| 82 | * @param Theme_Controller $theme_controller Theme controller. |
| 83 | * @param Personalization_Tags_Registry $personalization_tags_registry Personalization tags registry. |
| 84 | * @param Process_Manager $process_manager Process manager. |
| 85 | */ |
| 86 | public function __construct( |
| 87 | Content_Renderer $content_renderer, |
| 88 | Templates $templates, |
| 89 | Css_Inliner $css_inliner, |
| 90 | Theme_Controller $theme_controller, |
| 91 | Personalization_Tags_Registry $personalization_tags_registry, |
| 92 | Process_Manager $process_manager |
| 93 | ) { |
| 94 | $this->content_renderer = $content_renderer; |
| 95 | $this->templates = $templates; |
| 96 | $this->theme_controller = $theme_controller; |
| 97 | $this->css_inliner = $css_inliner; |
| 98 | $this->personalization_tags_registry = $personalization_tags_registry; |
| 99 | $this->process_manager = $process_manager; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Renders the email template |
| 104 | * |
| 105 | * @param \WP_Post $post Post object. |
| 106 | * @param string $subject Email subject. |
| 107 | * @param string $pre_header An email preheader or preview text is the short snippet of text that follows the subject line in an inbox. See https://kb.mailpoet.com/article/418-preview-text. |
| 108 | * @param string $language Email language. |
| 109 | * @param string $meta_robots Optional string. Can be left empty for sending, but you can provide a value (e.g. noindex, nofollow) when you want to display email html in a browser. |
| 110 | * @param string $template_slug Optional block template slug used for cases when email doesn't have associated template. |
| 111 | * @return array |
| 112 | */ |
| 113 | public function render( \WP_Post $post, string $subject, string $pre_header, string $language, string $meta_robots = '', string $template_slug = '' ): array { |
| 114 | if ( ! $template_slug ) { |
| 115 | $template_slug = get_page_template_slug( $post ) ? get_page_template_slug( $post ) : 'email-general'; |
| 116 | } |
| 117 | /** @var \WP_Block_Template $template */ // phpcs:ignore Generic.Commenting.DocComment.MissingShort -- used for phpstan |
| 118 | $template = $this->templates->get_block_template( $template_slug ); |
| 119 | |
| 120 | $previous_rendering_context = $this->content_renderer->get_current_rendering_context(); |
| 121 | try { |
| 122 | $rendering_context = $this->content_renderer->create_rendering_context( $language, $post, $template ); |
| 123 | $this->content_renderer->set_rendering_context( $rendering_context ); |
| 124 | $email_styles = $this->theme_controller->get_styles(); |
| 125 | $content_result = $this->content_renderer->render_without_css_inline( $post, $template ); |
| 126 | $template_html = $content_result['html']; |
| 127 | $content_styles = $content_result['styles']; |
| 128 | $layout = $this->theme_controller->get_layout_settings(); |
| 129 | |
| 130 | ob_start(); |
| 131 | include self::TEMPLATE_FILE; |
| 132 | $rendered_template = (string) ob_get_clean(); |
| 133 | |
| 134 | $template_styles = |
| 135 | WP_Style_Engine::compile_css( |
| 136 | array( |
| 137 | 'background-color' => $email_styles['color']['background'] ?? 'inherit', |
| 138 | 'color' => $email_styles['color']['text'] ?? 'inherit', |
| 139 | 'padding-top' => $email_styles['spacing']['padding']['top'] ?? '0px', |
| 140 | 'padding-bottom' => $email_styles['spacing']['padding']['bottom'] ?? '0px', |
| 141 | 'font-family' => $email_styles['typography']['fontFamily'] ?? 'inherit', |
| 142 | 'line-height' => $email_styles['typography']['lineHeight'] ?? '1.5', |
| 143 | 'font-size' => $email_styles['typography']['fontSize'] ?? 'inherit', |
| 144 | 'direction' => $rendering_context->get_text_direction(), |
| 145 | 'text-align' => $rendering_context->get_default_text_align(), |
| 146 | ), |
| 147 | 'body, .email_layout_wrapper' |
| 148 | ); |
| 149 | $template_styles .= WP_Style_Engine::compile_css( |
| 150 | array( |
| 151 | 'direction' => $rendering_context->get_text_direction(), |
| 152 | 'text-align' => $rendering_context->get_default_text_align(), |
| 153 | ), |
| 154 | '.email_content_wrapper, .email_preheader' |
| 155 | ); |
| 156 | $template_styles .= '.email_layout_wrapper { box-sizing: border-box;}'; |
| 157 | $template_styles .= file_get_contents( __DIR__ . '/' . self::TEMPLATE_STYLES_FILE ); |
| 158 | $template_styles = wp_strip_all_tags( (string) apply_filters( 'woocommerce_email_renderer_styles', $template_styles, $post ) ); |
| 159 | |
| 160 | // Single CSS inlining pass: combine content and template styles, then inline all at once. |
| 161 | $all_styles = '<style>' . $template_styles . $content_styles . '</style>'; |
| 162 | $rendered_template = $this->inline_css_styles( $all_styles . $rendered_template ); |
| 163 | |
| 164 | // Postprocess after CSS inlining (border normalization, CSS variable replacement, etc.). |
| 165 | $rendered_template = $this->process_manager->postprocess( $rendered_template ); |
| 166 | $rendered_template = $this->apply_html_attributes( $rendered_template, $rendering_context ); |
| 167 | |
| 168 | // This is a workaround to support link :hover in some clients. Ideally we would remove the ability to set :hover |
| 169 | // however this is not possible using the color panel from Gutenberg. |
| 170 | if ( isset( $email_styles['elements']['link'][':hover']['color']['text'] ) ) { |
| 171 | $rendered_template = str_replace( '<!-- Forced Styles -->', '<style>a:hover { color: ' . esc_attr( $email_styles['elements']['link'][':hover']['color']['text'] ) . ' !important; }</style>', $rendered_template ); |
| 172 | } |
| 173 | |
| 174 | return array( |
| 175 | 'html' => $rendered_template, |
| 176 | 'text' => $this->render_text_version( $rendered_template ), |
| 177 | ); |
| 178 | } finally { |
| 179 | $this->content_renderer->restore_rendering_context( $previous_rendering_context ); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Inlines CSS styles into the HTML |
| 185 | * |
| 186 | * @param string $template HTML template. |
| 187 | * @return string |
| 188 | */ |
| 189 | private function inline_css_styles( $template ) { |
| 190 | return $this->css_inliner->from_html( $template )->inline_css()->render(); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Apply document-level language and direction attributes after CSS inlining. |
| 195 | * |
| 196 | * @param string $template HTML template. |
| 197 | * @param Rendering_Context $rendering_context Rendering context. |
| 198 | * @return string |
| 199 | */ |
| 200 | private function apply_html_attributes( string $template, Rendering_Context $rendering_context ): string { |
| 201 | $processor = new \WP_HTML_Tag_Processor( $template ); |
| 202 | if ( ! $processor->next_tag( array( 'tag_name' => 'html' ) ) ) { |
| 203 | return $template; |
| 204 | } |
| 205 | |
| 206 | $language = $rendering_context->get_language(); |
| 207 | if ( $language ) { |
| 208 | $processor->set_attribute( 'lang', str_replace( '_', '-', $language ) ); |
| 209 | } |
| 210 | $processor->set_attribute( 'dir', $rendering_context->get_text_direction() ); |
| 211 | |
| 212 | return $processor->get_updated_html(); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Renders the text version of the email template. |
| 217 | * |
| 218 | * @param string $template HTML template. |
| 219 | * @return string |
| 220 | */ |
| 221 | private function render_text_version( $template ) { |
| 222 | $template = ( mb_detect_encoding( $template, 'UTF-8', true ) ) ? $template : mb_convert_encoding( $template, 'UTF-8', mb_list_encodings() ); |
| 223 | |
| 224 | // Ensure template is a string before processing. |
| 225 | if ( ! is_string( $template ) ) { |
| 226 | return ''; |
| 227 | } |
| 228 | |
| 229 | // Preserve personalization tags by temporarily replacing them with unique placeholders. |
| 230 | $template = $this->preserve_personalization_tags( $template ); |
| 231 | |
| 232 | $result = Html2Text::convert( (string) $template, array( 'ignore_errors' => true ) ); |
| 233 | if ( ! $result ) { |
| 234 | return ''; |
| 235 | } |
| 236 | |
| 237 | // Restore personalization tags from placeholders. |
| 238 | $result = $this->restore_personalization_tags( $result ); |
| 239 | |
| 240 | return $result; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Preserves personalization tags by replacing them with unique placeholders (not inside comments). |
| 245 | * |
| 246 | * @param string $template HTML template. |
| 247 | * @return string |
| 248 | */ |
| 249 | private function preserve_personalization_tags( string $template ): string { |
| 250 | $all_registered_tags = $this->personalization_tags_registry->get_all(); |
| 251 | $this->personalization_tag_placeholders = array(); |
| 252 | $counter = 0; |
| 253 | |
| 254 | $base_tokens = array(); // All the tokens used in the email, e.g. [woocommerce/customer-username]. |
| 255 | $token_prefixes = array(); // All the used prefixes, e.g. woocommerce, mailpoet, etc. |
| 256 | foreach ( $all_registered_tags as $tag ) { |
| 257 | $token = $tag->get_token(); // E.g. [woocommerce/customer-username]. |
| 258 | $base_tokens[ $token ] = true; |
| 259 | // Remove brackets for regex matching, escape for regex. |
| 260 | $token_prefixes[] = preg_quote( substr( $token, 1, -1 ), '/' ); |
| 261 | } |
| 262 | |
| 263 | if ( empty( $token_prefixes ) ) { |
| 264 | return $template; |
| 265 | } |
| 266 | |
| 267 | // Match all of the code comments that look like a personalization tags. |
| 268 | $pattern = '/<!--\[(' . implode( '|', $token_prefixes ) . ')(?:\s+[^\]]*)?\]-->/'; |
| 269 | |
| 270 | $template = preg_replace_callback( |
| 271 | $pattern, |
| 272 | function ( $matches ) use ( &$counter, $base_tokens ) { |
| 273 | // $matches[1] is the token without brackets, add brackets for lookup. |
| 274 | $base_token = '[' . $matches[1] . ']'; |
| 275 | if ( isset( $base_tokens[ $base_token ] ) ) { |
| 276 | $placeholder = 'PERSONALIZATION_TAG_PLACEHOLDER_' . $counter; |
| 277 | $this->personalization_tag_placeholders[ $placeholder ] = $matches[0]; |
| 278 | ++$counter; |
| 279 | return $placeholder; |
| 280 | } |
| 281 | return $matches[0]; |
| 282 | }, |
| 283 | $template |
| 284 | ); |
| 285 | |
| 286 | return $template ?? ''; |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Restores personalization tags from placeholders |
| 291 | * |
| 292 | * @param string $text Text content. |
| 293 | * @return string |
| 294 | */ |
| 295 | private function restore_personalization_tags( string $text ): string { |
| 296 | if ( empty( $this->personalization_tag_placeholders ) ) { |
| 297 | return $text; |
| 298 | } |
| 299 | foreach ( $this->personalization_tag_placeholders as $placeholder => $html_comment ) { |
| 300 | $text = str_replace( $placeholder, $html_comment, $text ); |
| 301 | } |
| 302 | return $text; |
| 303 | } |
| 304 | } |
| 305 |