* regular expression sutiable for using in preg_replace
*/
private static function tokenRegex($token_type) {
- return '/(?<!\{|\\\\)\{' . $token_type . '\.([\w]+(\-[\w\s]+)?)\}(?!\})/';
+ return '/(?<!\{|\\\\)\{' . $token_type . '\.([\w]+:?\w*(\-[\w\s]+)?)\}(?!\})/';
}
/**
public static function &replaceHookTokens(
$str,
&$contact,
- &$categories,
+ $categories = NULL,
$html = FALSE,
$escapeSmarty = FALSE
) {
+ if (!$categories) {
+ $categories = self::getTokenCategories();
+ }
foreach ($categories as $key) {
$str = preg_replace_callback(
self::tokenRegex($key),
return $str;
}
+ /**
+ * Get the categories required for rendering tokens.
+ *
+ * @return array
+ */
+ public static function getTokenCategories(): array {
+ if (!isset(\Civi::$statics[__CLASS__]['token_categories'])) {
+ $tokens = [];
+ \CRM_Utils_Hook::tokens($tokens);
+ \Civi::$statics[__CLASS__]['token_categories'] = array_keys($tokens);
+ }
+ return \Civi::$statics[__CLASS__]['token_categories'];
+ }
+
/**
* Parse html through Smarty resolving any smarty functions.
* @param string $tokenHtml
public static function getTokens($string) {
$matches = [];
$tokens = [];
- preg_match_all('/(?<!\{|\\\\)\{(\w+\.\w+)\}(?!\})/',
+ preg_match_all('/(?<!\{|\\\\)\{(\w+\.\w+:?\w*)\}(?!\})/',
$string,
$matches,
PREG_PATTERN_ORDER
protected static function _buildContributionTokens() {
$key = 'contribution';
- if (self::$_tokens[$key] == NULL) {
- self::$_tokens[$key] = array_keys(array_merge(CRM_Contribute_BAO_Contribution::exportableFields('All'),
- ['campaign', 'financial_type'],
- self::getCustomFieldTokens('Contribution')
- ));
+
+ if (!isset(Civi::$statics[__CLASS__][__FUNCTION__][$key])) {
+ $processor = new CRM_Contribute_Tokens();
+ $tokens = array_merge(CRM_Contribute_BAO_Contribution::exportableFields('All'),
+ ['campaign' => [], 'financial_type' => [], 'payment_instrument' => []],
+ self::getCustomFieldTokens('Contribution'),
+ $processor->getPseudoTokens()
+ );
+ foreach ($tokens as $token) {
+ if (!empty($token['name'])) {
+ $tokens[$token['name']] = [];
+ }
+ }
+ Civi::$statics[__CLASS__][__FUNCTION__][$key] = array_keys($tokens);
}
+ self::$_tokens[$key] = Civi::$statics[__CLASS__][__FUNCTION__][$key];
}
/**
//early return
return $str;
}
- self::_buildContributionTokens();
// here we intersect with the list of pre-configured valid tokens
// so that we remove anything we do not recognize
return $value;
}
+ /**
+ * Get token deprecation information.
+ *
+ * @return array
+ */
+ public static function getTokenDeprecations(): array {
+ return [
+ 'WorkFlowMessageTemplates' => [
+ 'contribution_invoice_receipt' => [
+ '$display_name' => 'contact.display_name',
+ ],
+ ],
+ ];
+ }
+
}