Logger
10 months ago
Patterns
10 months ago
PersonalizationTags
7 months ago
Renderer
4 weeks ago
Templates
2 months ago
class-assets-manager.php
4 months ago
class-dependency-check.php
1 year ago
class-email-api-controller.php
4 months ago
class-email-editor.php
4 months ago
class-email-styles-schema.php
1 year ago
class-personalizer.php
4 months ago
class-send-preview-email.php
3 months ago
class-settings-controller.php
2 months ago
class-site-style-sync-controller.php
2 months ago
class-theme-controller.php
2 months ago
class-user-theme.php
1 year ago
content-editor.css
9 months ago
content-shared.css
10 months ago
theme.json
2 months ago
class-personalizer.php
230 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 | |
| 10 | namespace Automattic\WooCommerce\EmailEditor\Engine; |
| 11 | |
| 12 | use Automattic\WooCommerce\EmailEditor\Engine\PersonalizationTags\HTML_Tag_Processor; |
| 13 | use Automattic\WooCommerce\EmailEditor\Engine\PersonalizationTags\Personalization_Tags_Registry; |
| 14 | |
| 15 | /** |
| 16 | * Class for replacing personalization tags with their values in the email content. |
| 17 | */ |
| 18 | class Personalizer { |
| 19 | |
| 20 | /** |
| 21 | * Regex pattern for matching personalization tag names (e.g., "woocommerce/store-url", "user-firstname"). |
| 22 | * Used in both tag detection and parsing. |
| 23 | */ |
| 24 | private const TAG_NAME_PATTERN = '[a-zA-Z0-9\-\/]+'; |
| 25 | |
| 26 | /** |
| 27 | * Personalization tags registry. |
| 28 | * |
| 29 | * @var Personalization_Tags_Registry |
| 30 | */ |
| 31 | private Personalization_Tags_Registry $tags_registry; |
| 32 | |
| 33 | /** |
| 34 | * Context for personalization tags. |
| 35 | * |
| 36 | * The `context` is an associative array containing recipient-specific or |
| 37 | * campaign-specific data. This data is used to resolve personalization tags |
| 38 | * and provide input for tag callbacks during email content processing. |
| 39 | * |
| 40 | * Example context: |
| 41 | * array( |
| 42 | * 'recipient_email' => 'john@example.com', // Recipient's email |
| 43 | * 'custom_field' => 'Special Value', // Custom campaign-specific data |
| 44 | * ) |
| 45 | * |
| 46 | * @var array<string, mixed> |
| 47 | */ |
| 48 | private array $context; |
| 49 | |
| 50 | /** |
| 51 | * Class constructor with required dependencies. |
| 52 | * |
| 53 | * @param Personalization_Tags_Registry $tags_registry Personalization tags registry. |
| 54 | */ |
| 55 | public function __construct( Personalization_Tags_Registry $tags_registry ) { |
| 56 | $this->tags_registry = $tags_registry; |
| 57 | $this->context = array(); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Set the context for personalization. |
| 62 | * |
| 63 | * The `context` provides data required for resolving personalization tags |
| 64 | * during content processing. This method allows the context to be set or updated. |
| 65 | * |
| 66 | * Example usage: |
| 67 | * $personalizer->set_context(array( |
| 68 | * 'recipient_email' => 'john@example.com', |
| 69 | * )); |
| 70 | * |
| 71 | * @param array<string, mixed> $context Associative array containing personalization data. |
| 72 | * @return void |
| 73 | */ |
| 74 | public function set_context( array $context ) { |
| 75 | $this->context = $context; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Get the current context. |
| 80 | * |
| 81 | * The `context` is an associative array containing recipient-specific or |
| 82 | * campaign-specific data. This data is used to resolve personalization tags |
| 83 | * and provide input for tag callbacks during email content processing. |
| 84 | * |
| 85 | * @return array<string, mixed> The current context. |
| 86 | */ |
| 87 | public function get_context(): array { |
| 88 | return $this->context; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Personalize the content by replacing the personalization tags with their values. |
| 93 | * |
| 94 | * @param string $content The content to personalize. |
| 95 | * @return string The personalized content. |
| 96 | */ |
| 97 | public function personalize_content( string $content ): string { |
| 98 | $content_processor = new HTML_Tag_Processor( $content ); |
| 99 | while ( $content_processor->next_token() ) { |
| 100 | if ( $content_processor->get_token_type() === '#comment' ) { |
| 101 | $modifiable_text = $content_processor->get_modifiable_text(); |
| 102 | $token = $this->parse_token( $modifiable_text ); |
| 103 | $tag = $this->tags_registry->get_by_token( $token['token'] ); |
| 104 | if ( ! $tag ) { |
| 105 | continue; |
| 106 | } |
| 107 | |
| 108 | $value = $tag->execute_callback( $this->context, $token['arguments'] ); |
| 109 | $content_processor->replace_token( $value ); |
| 110 | |
| 111 | } elseif ( $content_processor->get_token_type() === '#tag' && $content_processor->get_tag() === 'TITLE' ) { |
| 112 | // The title tag contains the subject of the email which should be personalized. HTML_Tag_Processor does parse the header tags. |
| 113 | $modifiable_text = $content_processor->get_modifiable_text(); |
| 114 | $title = $this->personalize_content( $modifiable_text ); |
| 115 | $content_processor->set_modifiable_text( $title ); |
| 116 | |
| 117 | } elseif ( $content_processor->get_token_type() === '#tag' && $content_processor->get_tag() === 'A' && $content_processor->get_attribute( 'data-link-href' ) ) { |
| 118 | // The anchor tag contains the data-link-href attribute which should be personalized. |
| 119 | $href = (string) $content_processor->get_attribute( 'data-link-href' ); |
| 120 | $token = $this->parse_token( $href ); |
| 121 | $tag = $this->tags_registry->get_by_token( $token['token'] ); |
| 122 | if ( ! $tag ) { |
| 123 | continue; |
| 124 | } |
| 125 | |
| 126 | $value = $tag->execute_callback( $this->context, $token['arguments'] ); |
| 127 | $value = $this->replace_link_href( $href, $tag->get_token(), $value ); |
| 128 | if ( $value ) { |
| 129 | $content_processor->set_attribute( 'href', $value ); |
| 130 | $content_processor->remove_attribute( 'data-link-href' ); |
| 131 | $content_processor->remove_attribute( 'contenteditable' ); |
| 132 | } |
| 133 | } elseif ( $content_processor->get_token_type() === '#tag' && $content_processor->get_tag() === 'A' ) { |
| 134 | $href = $content_processor->get_attribute( 'href' ); |
| 135 | if ( ! is_string( $href ) ) { |
| 136 | continue; |
| 137 | } |
| 138 | |
| 139 | // Decode both URL encoding (%XX) and HTML entities (') to handle various encoding scenarios. |
| 140 | $decoded_href = html_entity_decode( urldecode( $href ), ENT_QUOTES, 'UTF-8' ); |
| 141 | if ( ! preg_match( '/\[' . self::TAG_NAME_PATTERN . '(?:\s+[^\]]+)?\]/', $decoded_href, $matches ) ) { |
| 142 | continue; |
| 143 | } |
| 144 | |
| 145 | $token = $this->parse_token( $matches[0] ); |
| 146 | $tag = $this->tags_registry->get_by_token( $token['token'] ); |
| 147 | |
| 148 | if ( ! $tag ) { |
| 149 | continue; |
| 150 | } |
| 151 | |
| 152 | $value = $tag->execute_callback( $this->context, $token['arguments'] ); |
| 153 | |
| 154 | if ( $value ) { |
| 155 | $content_processor->set_attribute( 'href', $value ); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | $content_processor->flush_updates(); |
| 161 | return $content_processor->get_updated_html(); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Parse a personalization tag to the token and attributes. |
| 166 | * |
| 167 | * @param string $token The token to parse. |
| 168 | * @return array{token: string, arguments: array<string, string>} The parsed token. |
| 169 | */ |
| 170 | private function parse_token( string $token ): array { |
| 171 | $result = array( |
| 172 | 'token' => '', |
| 173 | 'arguments' => array(), |
| 174 | ); |
| 175 | |
| 176 | // Step 1: Separate the tag and attributes. |
| 177 | if ( preg_match( '/^\[(' . self::TAG_NAME_PATTERN . ')\s*(.*?)\]$/', trim( $token ), $matches ) ) { |
| 178 | $result['token'] = "[{$matches[1]}]"; // The tag part (e.g., "[mailpoet/subscriber-firstname]"). |
| 179 | $attributes_string = $matches[2]; // The attributes part (e.g., 'default="subscriber"'). |
| 180 | |
| 181 | // Step 2: Extract attributes from the attribute string. |
| 182 | // Match quoted values (double or single quotes separately to avoid mixing) and unquoted values. |
| 183 | // Unquoted values can occur when esc_url() strips quotes from personalization tags. |
| 184 | // For unquoted values with spaces, capture until the next key= pattern or closing bracket. |
| 185 | // The negative lookahead (?!\w+=) is critical for preventing ReDoS: |
| 186 | // it ensures the inner loop terminates as soon as the next key= pattern appears, |
| 187 | // preventing excessive backtracking despite the nested quantifiers. |
| 188 | if ( preg_match_all( '/(\w+)=(?:"([^"]*)"|\'([^\']*)\'|([^\s\]]+(?:\s+(?!\w+=)[^\s\]]+)*))/', $attributes_string, $attribute_matches, PREG_SET_ORDER ) ) { |
| 189 | foreach ( $attribute_matches as $attribute ) { |
| 190 | // $attribute[2] is double-quoted value, $attribute[3] is single-quoted value, |
| 191 | // $attribute[4] is unquoted value (may contain spaces). |
| 192 | // Use null coalescing as only one of these will be populated depending on which pattern matched. |
| 193 | $double_quoted_value = $attribute[2] ?? ''; |
| 194 | $single_quoted_value = $attribute[3] ?? ''; |
| 195 | $unquoted_value = $attribute[4] ?? ''; |
| 196 | |
| 197 | if ( '' !== $double_quoted_value ) { |
| 198 | $result['arguments'][ $attribute[1] ] = $double_quoted_value; |
| 199 | } elseif ( '' !== $single_quoted_value ) { |
| 200 | $result['arguments'][ $attribute[1] ] = $single_quoted_value; |
| 201 | } else { |
| 202 | $result['arguments'][ $attribute[1] ] = $unquoted_value; |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | return $result; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Replace the href attribute of the anchor tag with the personalized value. |
| 213 | * The replacement uses regular expression to match the shortcode and its attributes. |
| 214 | * |
| 215 | * @param string $content The content to replace the link href. |
| 216 | * @param string $token Personalization tag token. |
| 217 | * @param string $replacement The callback output to replace the link href. |
| 218 | * @return string |
| 219 | */ |
| 220 | private function replace_link_href( string $content, string $token, string $replacement ) { |
| 221 | // Escape the shortcode name for safe regex usage and strip the brackets. |
| 222 | $escaped_shortcode = preg_quote( substr( $token, 1, strlen( $token ) - 2 ), '/' ); |
| 223 | |
| 224 | // Create a regex pattern dynamically. |
| 225 | $pattern = '/\[' . $escaped_shortcode . '(?:\s+[^\]]+)?\]/'; |
| 226 | |
| 227 | return trim( (string) preg_replace( $pattern, $replacement, $content ) ); |
| 228 | } |
| 229 | } |
| 230 |