Automattic\WooCommerce\EmailEditor\Engine\PersonalizationTags

Personalization_Tag::__constructpublicWC 1.0

Personalization_Tag constructor.

Example usage: $tag = new Personalization_Tag(

'First Name',
'user:first_name',
'User',
 function( $context, $args ) {
   return $context['user_firstname'] ?? 'user';
 },
 array( default => 'user' ),
 'user:first default="user"'

);

Method of the class: Personalization_Tag{}

No Hooks.

Returns

null. Nothing (null).

Usage

$Personalization_Tag = new Personalization_Tag();
$Personalization_Tag->__construct( $name, $token, $category, $callback, $attributes, ?string $value_to_insert, $post_types );
$name(string) (required)
The name of the tag displayed in the UI.
$token(string) (required)
The token used in HTML_Tag_Processor to replace the tag with its value.
$category(string) (required)
The category of the personalization tag for categorization on the UI.
$callback(callable) (required)
The callback function which returns the value of the personalization tag.
$attributes(array)
The attributes which are used in the Personalization Tag UI.
Default: array()
?string $value_to_insert
.
Default: null
$post_types(string[])
The list of supported post types.
Default: array()

Personalization_Tag::__construct() code WC 10.6.2

public function __construct(
	string $name,
	string $token,
	string $category,
	callable $callback,
	array $attributes = array(),
	?string $value_to_insert = null,
	array $post_types = array()
) {
	$this->name = $name;
	// Because Gutenberg does not wrap the token with square brackets, we need to add them here.
	$this->token      = strpos( $token, '[' ) === 0 ? $token : "[$token]";
	$this->category   = $category;
	$this->callback   = $callback;
	$this->attributes = $attributes;
	// Composing token to insert based on the token and attributes if it is not set.
	if ( ! $value_to_insert ) {
		if ( $this->attributes ) {
			$value_to_insert = substr( $this->token, 0, -1 ) . ' ' .
				implode(
					' ',
					array_map(
						function ( $key ) {
							return $key . '="' . esc_attr( $this->attributes[ $key ] ) . '"';
						},
						array_keys( $this->attributes )
					)
				) . ']';
		} else {
			$value_to_insert = $this->token;
		}
	}
	$this->value_to_insert = $value_to_insert;
	$this->post_types      = $post_types;
}