Automattic\WooCommerce\EmailEditor\Engine

Personalizer::parse_tokenprivateWC 1.0

Parse a personalization tag to the token and attributes.

Метод класса: Personalizer{}

Хуков нет.

Возвращает

Массив{token:. string, arguments: array<string, string>} The parsed token.

Использование

// private - только в коде основоного (родительского) класса
$result = $this->parse_token( $token ): array;
$token(строка) (обязательный)
The token to parse.

Код Personalizer::parse_token() WC 10.0.2

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;
}