Add new Personalization Tag property defining value to insert

[MAILPOET-6376]
This commit is contained in:
Jan Lysý
2024-12-17 17:25:39 +01:00
committed by Rostislav Wolný
parent 6fbaebcc09
commit e4c80a09b6
3 changed files with 54 additions and 12 deletions

View File

@ -44,6 +44,12 @@ class Personalization_Tag {
* @var array
*/
private array $attributes;
/**
* The value that is inserted via the UI. When the value is null the token is generated based on $token attribute and $attributes.
*
* @var string
*/
private string $value_to_insert;
/**
* Personalization_Tag constructor.
@ -56,21 +62,24 @@ class Personalization_Tag {
* function( $context, $args ) {
* return $context['user_firstname'] ?? 'user';
* },
* array( default => 'user' )
* array( default => 'user' ),
* 'user:first default="user"'
* );
*
* @param string $name The name of the tag displayed in the UI.
* @param string $token The token used in HTML_Tag_Processor to replace the tag with its value.
* @param string $category The category of the personalization tag for categorization on the UI.
* @param callable $callback The callback function which returns the value of the personalization tag.
* @param array $attributes The attributes which are used in the Personalization Tag UI.
* @param string $name The name of the tag displayed in the UI.
* @param string $token The token used in HTML_Tag_Processor to replace the tag with its value.
* @param string $category The category of the personalization tag for categorization on the UI.
* @param callable $callback The callback function which returns the value of the personalization tag.
* @param array $attributes The attributes which are used in the Personalization Tag UI.
* @param string|null $value_to_insert The value that is inserted via the UI. When the value is null the token is generated based on $token attribute and $attributes.
*/
public function __construct(
string $name,
string $token,
string $category,
callable $callback,
array $attributes = array()
array $attributes = array(),
?string $value_to_insert = null
) {
$this->name = $name;
// Because Gutenberg does not wrap the token with square brackets, we need to add them here.
@ -78,6 +87,24 @@ class Personalization_Tag {
$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;
}
/**
@ -116,6 +143,15 @@ class Personalization_Tag {
return $this->attributes;
}
/**
* Returns the token to insert via UI in the editor.
*
* @return string
*/
public function get_value_to_insert(): string {
return $this->value_to_insert;
}
/**
* Executes the callback function for the personalization tag.
*

View File

@ -99,10 +99,11 @@ class Email_Api_Controller {
array_map(
function ( Personalization_Tag $tag ) {
return array(
'name' => $tag->get_name(),
'token' => $tag->get_token(),
'category' => $tag->get_category(),
'attributes' => $tag->get_attributes(),
'name' => $tag->get_name(),
'token' => $tag->get_token(),
'category' => $tag->get_category(),
'attributes' => $tag->get_attributes(),
'valueToInsert' => $tag->get_value_to_insert(),
);
},
$tags

View File

@ -58,6 +58,7 @@ class PersonalizationTagsRegistryTest extends TestCase {
$this->assertSame( 'Subscriber Info', $tag->get_category() );
$this->assertSame( 'Personalized Value', $tag->execute_callback( array(), array() ) );
$this->assertSame( array( 'description' => 'First name of the subscriber' ), $tag->get_attributes() );
$this->assertSame( '[first_name description="First name of the subscriber"]', $tag->get_value_to_insert() );
}
/**
@ -74,7 +75,9 @@ class PersonalizationTagsRegistryTest extends TestCase {
'Last Name',
'[last_name]',
'Subscriber Info',
$callback
$callback,
array( 'default' => 'subscriber' ),
'[last_name default="user"]'
)
);
@ -87,6 +90,8 @@ class PersonalizationTagsRegistryTest extends TestCase {
$this->assertSame( '[last_name]', $tag->get_token() );
$this->assertSame( 'Subscriber Info', $tag->get_category() );
$this->assertSame( 'Personalized Value', $tag->execute_callback( array(), array() ) );
$this->assertSame( array( 'default' => 'subscriber' ), $tag->get_attributes() );
$this->assertSame( '[last_name default="user"]', $tag->get_value_to_insert() );
}
/**