Add new Personalization Tag property defining value to insert
[MAILPOET-6376]
This commit is contained in:
committed by
Rostislav Wolný
parent
6fbaebcc09
commit
e4c80a09b6
@ -44,6 +44,12 @@ class Personalization_Tag {
|
|||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
private array $attributes;
|
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.
|
* Personalization_Tag constructor.
|
||||||
@ -56,21 +62,24 @@ class Personalization_Tag {
|
|||||||
* function( $context, $args ) {
|
* function( $context, $args ) {
|
||||||
* return $context['user_firstname'] ?? 'user';
|
* 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 $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 $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 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 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 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(
|
public function __construct(
|
||||||
string $name,
|
string $name,
|
||||||
string $token,
|
string $token,
|
||||||
string $category,
|
string $category,
|
||||||
callable $callback,
|
callable $callback,
|
||||||
array $attributes = array()
|
array $attributes = array(),
|
||||||
|
?string $value_to_insert = null
|
||||||
) {
|
) {
|
||||||
$this->name = $name;
|
$this->name = $name;
|
||||||
// Because Gutenberg does not wrap the token with square brackets, we need to add them here.
|
// 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->category = $category;
|
||||||
$this->callback = $callback;
|
$this->callback = $callback;
|
||||||
$this->attributes = $attributes;
|
$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;
|
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.
|
* Executes the callback function for the personalization tag.
|
||||||
*
|
*
|
||||||
|
@ -99,10 +99,11 @@ class Email_Api_Controller {
|
|||||||
array_map(
|
array_map(
|
||||||
function ( Personalization_Tag $tag ) {
|
function ( Personalization_Tag $tag ) {
|
||||||
return array(
|
return array(
|
||||||
'name' => $tag->get_name(),
|
'name' => $tag->get_name(),
|
||||||
'token' => $tag->get_token(),
|
'token' => $tag->get_token(),
|
||||||
'category' => $tag->get_category(),
|
'category' => $tag->get_category(),
|
||||||
'attributes' => $tag->get_attributes(),
|
'attributes' => $tag->get_attributes(),
|
||||||
|
'valueToInsert' => $tag->get_value_to_insert(),
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
$tags
|
$tags
|
||||||
|
@ -58,6 +58,7 @@ class PersonalizationTagsRegistryTest extends TestCase {
|
|||||||
$this->assertSame( 'Subscriber Info', $tag->get_category() );
|
$this->assertSame( 'Subscriber Info', $tag->get_category() );
|
||||||
$this->assertSame( 'Personalized Value', $tag->execute_callback( array(), array() ) );
|
$this->assertSame( 'Personalized Value', $tag->execute_callback( array(), array() ) );
|
||||||
$this->assertSame( array( 'description' => 'First name of the subscriber' ), $tag->get_attributes() );
|
$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',
|
||||||
'[last_name]',
|
'[last_name]',
|
||||||
'Subscriber Info',
|
'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( '[last_name]', $tag->get_token() );
|
||||||
$this->assertSame( 'Subscriber Info', $tag->get_category() );
|
$this->assertSame( 'Subscriber Info', $tag->get_category() );
|
||||||
$this->assertSame( 'Personalized Value', $tag->execute_callback( array(), array() ) );
|
$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() );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user