Logger
1 year ago
Patterns
1 year ago
PersonalizationTags
9 months ago
Renderer
3 weeks ago
Templates
4 months ago
class-assets-manager.php
6 months ago
class-dependency-check.php
1 year ago
class-email-api-controller.php
6 months ago
class-email-editor.php
3 weeks ago
class-email-styles-schema.php
1 year ago
class-personalizer.php
6 months 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
1 year ago
content-editor.css
3 weeks ago
content-shared.css
1 year ago
index.php
1 year ago
theme.json
3 months ago
class-personalizer.php
121 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_Tags_Registry; |
| 7 | class Personalizer { |
| 8 | private const TAG_NAME_PATTERN = '[a-zA-Z0-9\-\/]+'; |
| 9 | private Personalization_Tags_Registry $tags_registry; |
| 10 | private array $context; |
| 11 | public function __construct( Personalization_Tags_Registry $tags_registry ) { |
| 12 | $this->tags_registry = $tags_registry; |
| 13 | $this->context = array(); |
| 14 | } |
| 15 | public function set_context( array $context ) { |
| 16 | $this->context = $context; |
| 17 | } |
| 18 | public function get_context(): array { |
| 19 | return $this->context; |
| 20 | } |
| 21 | public function personalize_content( string $content ): string { |
| 22 | $content_processor = new HTML_Tag_Processor( $content ); |
| 23 | while ( $content_processor->next_token() ) { |
| 24 | if ( $content_processor->get_token_type() === '#comment' ) { |
| 25 | $modifiable_text = $content_processor->get_modifiable_text(); |
| 26 | $token = $this->parse_token( $modifiable_text ); |
| 27 | $tag = $this->tags_registry->get_by_token( $token['token'] ); |
| 28 | if ( ! $tag ) { |
| 29 | continue; |
| 30 | } |
| 31 | $value = $tag->execute_callback( $this->context, $token['arguments'] ); |
| 32 | $content_processor->replace_token( $value ); |
| 33 | } elseif ( $content_processor->get_token_type() === '#tag' && $content_processor->get_tag() === 'TITLE' ) { |
| 34 | // The title tag contains the subject of the email which should be personalized. HTML_Tag_Processor does parse the header tags. |
| 35 | $modifiable_text = $content_processor->get_modifiable_text(); |
| 36 | $title = $this->personalize_content( $modifiable_text ); |
| 37 | $content_processor->set_modifiable_text( $title ); |
| 38 | } elseif ( $content_processor->get_token_type() === '#tag' && $content_processor->get_tag() === 'A' && $content_processor->get_attribute( 'data-link-href' ) ) { |
| 39 | // The anchor tag contains the data-link-href attribute which should be personalized. |
| 40 | $href = (string) $content_processor->get_attribute( 'data-link-href' ); |
| 41 | $token = $this->parse_token( $href ); |
| 42 | $tag = $this->tags_registry->get_by_token( $token['token'] ); |
| 43 | if ( ! $tag ) { |
| 44 | continue; |
| 45 | } |
| 46 | $value = $tag->execute_callback( $this->context, $token['arguments'] ); |
| 47 | $value = $this->replace_link_href( $href, $tag->get_token(), $value ); |
| 48 | if ( $value ) { |
| 49 | $content_processor->set_attribute( 'href', $value ); |
| 50 | $content_processor->remove_attribute( 'data-link-href' ); |
| 51 | $content_processor->remove_attribute( 'contenteditable' ); |
| 52 | } |
| 53 | } elseif ( $content_processor->get_token_type() === '#tag' && $content_processor->get_tag() === 'A' ) { |
| 54 | $href = $content_processor->get_attribute( 'href' ); |
| 55 | if ( ! is_string( $href ) ) { |
| 56 | continue; |
| 57 | } |
| 58 | // Decode both URL encoding (%XX) and HTML entities (') to handle various encoding scenarios. |
| 59 | $decoded_href = html_entity_decode( urldecode( $href ), ENT_QUOTES, 'UTF-8' ); |
| 60 | if ( ! preg_match( '/\[' . self::TAG_NAME_PATTERN . '(?:\s+[^\]]+)?\]/', $decoded_href, $matches ) ) { |
| 61 | continue; |
| 62 | } |
| 63 | $token = $this->parse_token( $matches[0] ); |
| 64 | $tag = $this->tags_registry->get_by_token( $token['token'] ); |
| 65 | if ( ! $tag ) { |
| 66 | continue; |
| 67 | } |
| 68 | $value = $tag->execute_callback( $this->context, $token['arguments'] ); |
| 69 | if ( $value ) { |
| 70 | $content_processor->set_attribute( 'href', $value ); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | $content_processor->flush_updates(); |
| 75 | return $content_processor->get_updated_html(); |
| 76 | } |
| 77 | private function parse_token( string $token ): array { |
| 78 | $result = array( |
| 79 | 'token' => '', |
| 80 | 'arguments' => array(), |
| 81 | ); |
| 82 | // Step 1: Separate the tag and attributes. |
| 83 | if ( preg_match( '/^\[(' . self::TAG_NAME_PATTERN . ')\s*(.*?)\]$/', trim( $token ), $matches ) ) { |
| 84 | $result['token'] = "[{$matches[1]}]"; // The tag part (e.g., "[mailpoet/subscriber-firstname]"). |
| 85 | $attributes_string = $matches[2]; // The attributes part (e.g., 'default="subscriber"'). |
| 86 | // Step 2: Extract attributes from the attribute string. |
| 87 | // Match quoted values (double or single quotes separately to avoid mixing) and unquoted values. |
| 88 | // Unquoted values can occur when esc_url() strips quotes from personalization tags. |
| 89 | // For unquoted values with spaces, capture until the next key= pattern or closing bracket. |
| 90 | // The negative lookahead (?!\w+=) is critical for preventing ReDoS: |
| 91 | // it ensures the inner loop terminates as soon as the next key= pattern appears, |
| 92 | // preventing excessive backtracking despite the nested quantifiers. |
| 93 | if ( preg_match_all( '/(\w+)=(?:"([^"]*)"|\'([^\']*)\'|([^\s\]]+(?:\s+(?!\w+=)[^\s\]]+)*))/', $attributes_string, $attribute_matches, PREG_SET_ORDER ) ) { |
| 94 | foreach ( $attribute_matches as $attribute ) { |
| 95 | // $attribute[2] is double-quoted value, $attribute[3] is single-quoted value, |
| 96 | // $attribute[4] is unquoted value (may contain spaces). |
| 97 | // Use null coalescing as only one of these will be populated depending on which pattern matched. |
| 98 | $double_quoted_value = $attribute[2] ?? ''; |
| 99 | $single_quoted_value = $attribute[3] ?? ''; |
| 100 | $unquoted_value = $attribute[4] ?? ''; |
| 101 | if ( '' !== $double_quoted_value ) { |
| 102 | $result['arguments'][ $attribute[1] ] = $double_quoted_value; |
| 103 | } elseif ( '' !== $single_quoted_value ) { |
| 104 | $result['arguments'][ $attribute[1] ] = $single_quoted_value; |
| 105 | } else { |
| 106 | $result['arguments'][ $attribute[1] ] = $unquoted_value; |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | return $result; |
| 112 | } |
| 113 | private function replace_link_href( string $content, string $token, string $replacement ) { |
| 114 | // Escape the shortcode name for safe regex usage and strip the brackets. |
| 115 | $escaped_shortcode = preg_quote( substr( $token, 1, strlen( $token ) - 2 ), '/' ); |
| 116 | // Create a regex pattern dynamically. |
| 117 | $pattern = '/\[' . $escaped_shortcode . '(?:\s+[^\]]+)?\]/'; |
| 118 | return trim( (string) preg_replace( $pattern, $replacement, $content ) ); |
| 119 | } |
| 120 | } |
| 121 |