Automattic\WooCommerce\EmailEditor\Engine

Personalizer::personalize_contentpublicWC 1.0

Personalize the content by replacing the personalization tags with their values.

Method of the class: Personalizer{}

No Hooks.

Returns

String. The personalized content.

Usage

$Personalizer = new Personalizer();
$Personalizer->personalize_content( $content ): string;
$content(string) (required)
The content to personalize.

Personalizer::personalize_content() code WC 10.5.0

public function personalize_content( string $content ): string {
	$content_processor = new HTML_Tag_Processor( $content );
	while ( $content_processor->next_token() ) {
		if ( $content_processor->get_token_type() === '#comment' ) {
			$modifiable_text = $content_processor->get_modifiable_text();
			$token           = $this->parse_token( $modifiable_text );
			$tag             = $this->tags_registry->get_by_token( $token['token'] );
			if ( ! $tag ) {
				continue;
			}

			$value = $tag->execute_callback( $this->context, $token['arguments'] );
			$content_processor->replace_token( $value );

		} elseif ( $content_processor->get_token_type() === '#tag' && $content_processor->get_tag() === 'TITLE' ) {
			// The title tag contains the subject of the email which should be personalized. HTML_Tag_Processor does parse the header tags.
			$modifiable_text = $content_processor->get_modifiable_text();
			$title           = $this->personalize_content( $modifiable_text );
			$content_processor->set_modifiable_text( $title );

		} elseif ( $content_processor->get_token_type() === '#tag' && $content_processor->get_tag() === 'A' && $content_processor->get_attribute( 'data-link-href' ) ) {
			// The anchor tag contains the data-link-href attribute which should be personalized.
			$href  = (string) $content_processor->get_attribute( 'data-link-href' );
			$token = $this->parse_token( $href );
			$tag   = $this->tags_registry->get_by_token( $token['token'] );
			if ( ! $tag ) {
				continue;
			}

			$value = $tag->execute_callback( $this->context, $token['arguments'] );
			$value = $this->replace_link_href( $href, $tag->get_token(), $value );
			if ( $value ) {
				$content_processor->set_attribute( 'href', $value );
				$content_processor->remove_attribute( 'data-link-href' );
				$content_processor->remove_attribute( 'contenteditable' );
			}
		} elseif ( $content_processor->get_token_type() === '#tag' && $content_processor->get_tag() === 'A' ) {
			$href = $content_processor->get_attribute( 'href' );
			if ( ! is_string( $href ) ) {
				continue;
			}

			if ( ! $href || ! preg_match( '/\[[a-z-\/]+\]/', urldecode( $href ), $matches ) ) {
				continue;
			}

			$token = $this->parse_token( $matches[0] );
			$tag   = $this->tags_registry->get_by_token( $token['token'] );

			if ( ! $tag ) {
				continue;
			}

			$value = $tag->execute_callback( $this->context, $token['arguments'] );

			if ( $value ) {
				$content_processor->set_attribute( 'href', $value );
			}
		}
	}

	$content_processor->flush_updates();
	return $content_processor->get_updated_html();
}