Logger
11 months ago
Patterns
11 months ago
PersonalizationTags
3 days ago
Renderer
3 days ago
Templates
4 months ago
class-assets-manager.php
5 months ago
class-dependency-check.php
11 months ago
class-email-api-controller.php
6 months ago
class-email-editor.php
3 days ago
class-email-styles-schema.php
11 months ago
class-personalizer.php
3 days ago
class-send-preview-email.php
5 months ago
class-settings-controller.php
3 months ago
class-site-style-sync-controller.php
4 months ago
class-theme-controller.php
3 months ago
class-user-theme.php
11 months ago
content-editor.css
2 weeks ago
content-shared.css
11 months ago
index.php
11 months ago
theme.json
3 months ago
class-personalizer.php
175 lines
| 1 | <?php |
| 2 | declare(strict_types = 1); |
| 3 | namespace Automattic\WooCommerce\EmailEditor\Engine; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use Automattic\WooCommerce\EmailEditor\Engine\PersonalizationTags\HTML_Tag_Processor; |
| 6 | use Automattic\WooCommerce\EmailEditor\Engine\PersonalizationTags\Personalization_Tag; |
| 7 | use Automattic\WooCommerce\EmailEditor\Engine\PersonalizationTags\Personalization_Tags_Registry; |
| 8 | class Personalizer { |
| 9 | private const TAG_NAME_PATTERN = '[a-zA-Z0-9\-\/]+'; |
| 10 | public const RENDERING_CONTEXT_HTML = 'html'; |
| 11 | public const RENDERING_CONTEXT_TEXT = 'text'; |
| 12 | public const RENDERING_CONTEXT_HREF = 'href'; |
| 13 | public const RENDERING_CONTEXT_KEY = 'rendering_context'; |
| 14 | private Personalization_Tags_Registry $tags_registry; |
| 15 | private array $context; |
| 16 | public function __construct( Personalization_Tags_Registry $tags_registry ) { |
| 17 | $this->tags_registry = $tags_registry; |
| 18 | $this->context = array(); |
| 19 | } |
| 20 | public function set_context( array $context ) { |
| 21 | $this->context = $context; |
| 22 | } |
| 23 | public function get_context(): array { |
| 24 | return $this->context; |
| 25 | } |
| 26 | public function personalize_content( string $content, string $rendering_context = self::RENDERING_CONTEXT_HTML ): string { |
| 27 | if ( ! in_array( $rendering_context, array( self::RENDERING_CONTEXT_HTML, self::RENDERING_CONTEXT_TEXT ), true ) ) { |
| 28 | $rendering_context = self::RENDERING_CONTEXT_HTML; |
| 29 | } |
| 30 | $content_processor = new HTML_Tag_Processor( $content ); |
| 31 | while ( $content_processor->next_token() ) { |
| 32 | if ( $content_processor->get_token_type() === '#comment' ) { |
| 33 | $modifiable_text = $content_processor->get_modifiable_text(); |
| 34 | $token = $this->parse_token( $modifiable_text ); |
| 35 | $tag = $this->tags_registry->get_by_token( $token['token'] ); |
| 36 | if ( ! $tag ) { |
| 37 | continue; |
| 38 | } |
| 39 | $value = $tag->execute_callback( $this->get_callback_context( $rendering_context ), $token['arguments'] ); |
| 40 | if ( self::RENDERING_CONTEXT_HTML === $rendering_context && Personalization_Tag::VALUE_TYPE_TEXT === $tag->get_value_type() ) { |
| 41 | $value = esc_html( $value ); |
| 42 | } |
| 43 | $content_processor->replace_token( $value ); |
| 44 | } elseif ( $content_processor->get_token_type() === '#tag' && $content_processor->get_tag() === 'TITLE' ) { |
| 45 | // The title tag contains the subject of the email which should be personalized. HTML_Tag_Processor does parse the header tags. |
| 46 | // The title content is effectively plain text, so it is personalized in the text rendering context. |
| 47 | $modifiable_text = $content_processor->get_modifiable_text(); |
| 48 | $title = $this->personalize_content( $modifiable_text, self::RENDERING_CONTEXT_TEXT ); |
| 49 | $content_processor->set_modifiable_text( $title ); |
| 50 | } elseif ( $content_processor->get_token_type() === '#tag' && $content_processor->get_tag() === 'A' && $content_processor->get_attribute( 'data-link-href' ) ) { |
| 51 | // The anchor tag contains the data-link-href attribute which should be personalized. |
| 52 | $href = (string) $content_processor->get_attribute( 'data-link-href' ); |
| 53 | $token = $this->parse_token( $href ); |
| 54 | $tag = $this->tags_registry->get_by_token( $token['token'] ); |
| 55 | if ( ! $tag ) { |
| 56 | continue; |
| 57 | } |
| 58 | $value = $tag->execute_callback( $this->get_callback_context( self::RENDERING_CONTEXT_HREF ), $token['arguments'] ); |
| 59 | $value = $this->replace_link_href( $href, $tag->get_token(), $value ); |
| 60 | if ( '' !== $value ) { |
| 61 | $content_processor->set_attribute( 'href', $value ); |
| 62 | $content_processor->remove_attribute( 'data-link-href' ); |
| 63 | $content_processor->remove_attribute( 'contenteditable' ); |
| 64 | } |
| 65 | } elseif ( $content_processor->get_token_type() === '#tag' && $content_processor->get_tag() === 'A' ) { |
| 66 | $href = $content_processor->get_attribute( 'href' ); |
| 67 | if ( ! is_string( $href ) ) { |
| 68 | continue; |
| 69 | } |
| 70 | $personalized_href = $this->personalize_href_tokens( $href ); |
| 71 | if ( null !== $personalized_href ) { |
| 72 | $content_processor->set_attribute( 'href', $personalized_href ); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | $content_processor->flush_updates(); |
| 77 | return $content_processor->get_updated_html(); |
| 78 | } |
| 79 | private function personalize_href_tokens( string $href ): ?string { |
| 80 | // Decode both URL encoding (%XX) and HTML entities (') to handle various encoding scenarios. |
| 81 | $decoded_href = html_entity_decode( urldecode( $href ), ENT_QUOTES, 'UTF-8' ); |
| 82 | if ( ! preg_match_all( '/\[' . self::TAG_NAME_PATTERN . '(?:\s+[^\]]+)?\]/', $decoded_href, $matches ) ) { |
| 83 | return null; |
| 84 | } |
| 85 | // Resolve every replaceable token first. |
| 86 | $replacements = array(); |
| 87 | foreach ( array_unique( $matches[0] ) as $token_string ) { |
| 88 | $token = $this->parse_token( $token_string ); |
| 89 | $tag = $this->tags_registry->get_by_token( $token['token'] ); |
| 90 | if ( ! $tag ) { |
| 91 | continue; |
| 92 | } |
| 93 | $value = $tag->execute_callback( $this->get_callback_context( self::RENDERING_CONTEXT_HREF ), $token['arguments'] ); |
| 94 | if ( '' !== $value ) { |
| 95 | $replacements[ $token_string ] = $value; |
| 96 | } |
| 97 | } |
| 98 | if ( ! $replacements ) { |
| 99 | return null; |
| 100 | } |
| 101 | // Prefer the original attribute value as the replacement base so legitimate |
| 102 | // percent-encoding in the surrounding URL is preserved; fall back to the decoded |
| 103 | // form when a replaced token occurrence exists only there (e.g. URL-encoded tokens). |
| 104 | // Only tokens that are actually replaced matter here — an unregistered bracket |
| 105 | // sequence that exists purely in the decoded form must not force the decoded base. |
| 106 | // Known tradeoff: the base is chosen for the whole href, so when the decoded form |
| 107 | // is used, unrelated percent-encoding elsewhere in the URL is decoded too. |
| 108 | $base = $href; |
| 109 | foreach ( array_keys( $replacements ) as $token_string ) { |
| 110 | if ( substr_count( $href, $token_string ) !== substr_count( $decoded_href, $token_string ) ) { |
| 111 | $base = $decoded_href; |
| 112 | break; |
| 113 | } |
| 114 | } |
| 115 | // The editor forces a protocol prefix when a tag is used as the whole URL |
| 116 | // ("http://[tag]"). Strip it only when the token directly after it is being |
| 117 | // replaced, so the tag value is used as-is while any suffix (e.g. appended |
| 118 | // query parameters) is kept. |
| 119 | if ( preg_match( '#^https?://(\[' . self::TAG_NAME_PATTERN . '(?:\s+[^\]]+)?\])#i', $base, $prefix_match ) && isset( $replacements[ $prefix_match[1] ] ) ) { |
| 120 | $base = (string) preg_replace( '#^https?://#i', '', $base ); |
| 121 | } |
| 122 | // Single-pass replacement — tag values are never re-scanned for other tokens, |
| 123 | // and a regex replacement would interpret `$` and `\` in them. |
| 124 | return strtr( $base, $replacements ); |
| 125 | } |
| 126 | private function get_callback_context( string $rendering_context ): array { |
| 127 | // array_replace() (unlike array_merge()) preserves integer keys in the consumer's context. |
| 128 | return array_replace( $this->context, array( self::RENDERING_CONTEXT_KEY => $rendering_context ) ); |
| 129 | } |
| 130 | private function parse_token( string $token ): array { |
| 131 | $result = array( |
| 132 | 'token' => '', |
| 133 | 'arguments' => array(), |
| 134 | ); |
| 135 | // Step 1: Separate the tag and attributes. |
| 136 | if ( preg_match( '/^\[(' . self::TAG_NAME_PATTERN . ')\s*(.*?)\]$/', trim( $token ), $matches ) ) { |
| 137 | $result['token'] = "[{$matches[1]}]"; // The tag part (e.g., "[mailpoet/subscriber-firstname]"). |
| 138 | $attributes_string = $matches[2]; // The attributes part (e.g., 'default="subscriber"'). |
| 139 | // Step 2: Extract attributes from the attribute string. |
| 140 | // Match quoted values (double or single quotes separately to avoid mixing) and unquoted values. |
| 141 | // Unquoted values can occur when esc_url() strips quotes from personalization tags. |
| 142 | // For unquoted values with spaces, capture until the next key= pattern or closing bracket. |
| 143 | // The negative lookahead (?!\w+=) is critical for preventing ReDoS: |
| 144 | // it ensures the inner loop terminates as soon as the next key= pattern appears, |
| 145 | // preventing excessive backtracking despite the nested quantifiers. |
| 146 | if ( preg_match_all( '/(\w+)=(?:"([^"]*)"|\'([^\']*)\'|([^\s\]]+(?:\s+(?!\w+=)[^\s\]]+)*))/', $attributes_string, $attribute_matches, PREG_SET_ORDER ) ) { |
| 147 | foreach ( $attribute_matches as $attribute ) { |
| 148 | // $attribute[2] is double-quoted value, $attribute[3] is single-quoted value, |
| 149 | // $attribute[4] is unquoted value (may contain spaces). |
| 150 | // Use null coalescing as only one of these will be populated depending on which pattern matched. |
| 151 | $double_quoted_value = $attribute[2] ?? ''; |
| 152 | $single_quoted_value = $attribute[3] ?? ''; |
| 153 | $unquoted_value = $attribute[4] ?? ''; |
| 154 | if ( '' !== $double_quoted_value ) { |
| 155 | $result['arguments'][ $attribute[1] ] = $double_quoted_value; |
| 156 | } elseif ( '' !== $single_quoted_value ) { |
| 157 | $result['arguments'][ $attribute[1] ] = $single_quoted_value; |
| 158 | } else { |
| 159 | $result['arguments'][ $attribute[1] ] = $unquoted_value; |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | return $result; |
| 165 | } |
| 166 | private function replace_link_href( string $content, string $token, string $replacement ) { |
| 167 | // Escape the shortcode name for safe regex usage and strip the brackets. |
| 168 | $escaped_shortcode = preg_quote( substr( $token, 1, strlen( $token ) - 2 ), '/' ); |
| 169 | // Create a regex pattern dynamically. |
| 170 | $pattern = '/\[' . $escaped_shortcode . '(?:\s+[^\]]+)?\]/'; |
| 171 | // Escape `$` and `\` so they are inserted literally instead of being interpreted as backreferences. |
| 172 | return trim( (string) preg_replace( $pattern, addcslashes( $replacement, '\\$' ), $content ) ); |
| 173 | } |
| 174 | } |
| 175 |