You are here

function dfp_token_replace in Doubleclick for Publishers (DFP) 7

Same name and namespace in other branches
  1. 7.2 dfp.module \dfp_token_replace()

Replaces all tokens in a given string with appropriate values, with caching.

This function is a memoizing wrapper for token_replace(), which is quite slow and inefficient. It takes advantage of specific knowledge about how DFP works to cache the result of token replacement. It is not a fully general solution but works for this module.

Parameters

string $text: A string potentially containing replaceable tokens.

stdClass $tag: The tag for which we are encoding a string. This value will be provided as additional context to token_replace().

array $options: An array of options to pass to token_replace(). See that function for further documentation.

Return value

string Text with tokens replaced.

See also

token_replace()

3 calls to dfp_token_replace()
dfp_format_targeting in ./dfp.module
Format the given array of values to be displayed as part of a javascript.
template_preprocess_dfp_short_tag in ./dfp.module
Preprocess function for DFP tags.
template_preprocess_dfp_tag in ./dfp.module
Preprocess function for DFP tags.

File

./dfp.module, line 871

Code

function dfp_token_replace($text, $tag = NULL, array $options = array()) {

  // Short-circuit the degenerate case, just like token_replace() does.
  $text_tokens = token_scan($text);
  if (empty($text_tokens)) {
    return $text;
  }

  // Get the possible replacement sources.
  // @todo This doesn't vary, so we could probably refactor it to be cached,
  // too.
  $data = _dfp_prepare_tokens($tag);

  // Tokens cache.
  $dfp_token_cache_enabled = variable_get('dfp_token_cache_enabled', TRUE);
  if ($dfp_token_cache_enabled && function_exists('entity_modified_last')) {
    $replacement = _dfp_token_replace_cache($text, $data, $options);
  }
  else {
    $replacement = token_replace($text, $data, $options);
  }
  return $replacement;
}