Automattic\WooCommerce\EmailEditor\Engine
Personalizer::personalize_content
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, $rendering_context ): string;
- $content(string) (required)
- The content to personalize.
- $rendering_context(string)
- The rendering context of the content — one of the RENDERING_CONTEXT_HTML or RENDERING_CONTEXT_TEXT constants. Unknown values fall back to RENDERING_CONTEXT_HTML.
Default:self::RENDERING_CONTEXT_HTML
Personalizer::personalize_content() Personalizer::personalize content code WC 11.1.2
public function personalize_content( string $content, string $rendering_context = self::RENDERING_CONTEXT_HTML ): string {
if ( ! in_array( $rendering_context, array( self::RENDERING_CONTEXT_HTML, self::RENDERING_CONTEXT_TEXT ), true ) ) {
$rendering_context = self::RENDERING_CONTEXT_HTML;
}
$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->get_callback_context( $rendering_context ), $token['arguments'] );
if ( self::RENDERING_CONTEXT_HTML === $rendering_context && Personalization_Tag::VALUE_TYPE_TEXT === $tag->get_value_type() ) {
$value = esc_html( $value );
}
$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.
// The title content is effectively plain text, so it is personalized in the text rendering context.
$modifiable_text = $content_processor->get_modifiable_text();
$title = $this->personalize_content( $modifiable_text, self::RENDERING_CONTEXT_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->get_callback_context( self::RENDERING_CONTEXT_HREF ), $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;
}
$personalized_href = $this->personalize_href_tokens( $href );
if ( null !== $personalized_href ) {
$content_processor->set_attribute( 'href', $personalized_href );
}
}
}
$content_processor->flush_updates();
return $content_processor->get_updated_html();
}