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rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / packages / email-editor / src / Engine / class-personalizer.php
woocommerce / packages / email-editor / src / Engine Last commit date
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 (&#039;) 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