MailPoet\EmailEditor\Engine

Personalizer::parse_token()privateWC 1.0

Parse a personalization tag to the token and attributes.

Method of the class: Personalizer{}

No Hooks.

Return

Array{token:. string, arguments: array} The parsed token.

Usage

// private - for code of main (parent) class only
$result = $this->parse_token( $token ): array;
$token(string) (required)
The token to parse.

Personalizer::parse_token() code WC 9.8.1

private function parse_token( string $token ): array {
	$result = array(
		'token'     => '',
		'arguments' => array(),
	);

	// Step 1: Separate the tag and attributes.
	if ( preg_match( '/^\[([a-zA-Z0-9\-\/]+)\s*(.*?)\]$/', trim( $token ), $matches ) ) {
		$result['token']   = "[{$matches[1]}]"; // The tag part (e.g., "[mailpoet/subscriber-firstname]").
		$attributes_string = $matches[2]; // The attributes part (e.g., 'default="subscriber"').

		// Step 2: Extract attributes from the attribute string.
		if ( preg_match_all( '/(\w+)=["\']([^"\']+)["\']/', $attributes_string, $attribute_matches, PREG_SET_ORDER ) ) {
			foreach ( $attribute_matches as $attribute ) {
				$result['arguments'][ $attribute[1] ] = $attribute[2];
			}
		}
	}

	return $result;
}