You are here

function uc_addresses_update_token_text in Ubercart Addresses 7

Update a string containing Ubercart Addresses 6.x-2.x tokens to Ubercart Addresses 7.x-1.x tokens.

Parameters

$text: A string containing tokens.

$updates: An optional array of Drupal 7 tokens keyed by their Drupal 6 token name. The default tokens will be merged into this array. Note neither the old or new token names should include the surrounding bracket ([ and ]) characters.

Return value

A string with the tokens upgraded.

See also

_token_upgrade_token_list()

_token_upgrade_token_date_list()

token_update_token_text()

1 call to uc_addresses_update_token_text()
uc_addresses_update_7100 in ./uc_addresses.install
Upgrade from Ubercart Addresses 6.x-2.x.

File

./uc_addresses.install, line 201
Install file for Ubercart Addresses.

Code

function uc_addresses_update_token_text($text, $updates = array(), $leading = '[', $trailing = ']') {

  // Get a list of old tokens.
  module_load_include('install', 'token');
  $updates += _token_upgrade_token_date_list('uc_addresses_created-', 'uc_addresses:created');
  $updates += _token_upgrade_token_date_list('uc_addresses_modified-', 'uc_addresses:modified');
  $updates += _token_upgrade_token_list();

  // Because of a bug in token_update_token_text() (v7.x-1.0-rc1) a copy
  // of the code there is directly executed here.
  // The bug is that the regex contains one closing bracket too much.
  // The following updates Ubercart Addresses tokens for 'created' and 'modified'
  // + it updates other tokens not defined by Ubercart Addresses.
  $regex = '/' . preg_quote($leading, '/') . '([^\\s]*)' . preg_quote($trailing, '/') . '/';
  preg_match_all($regex, $text, $matches);
  foreach ($matches[1] as $index => $old_token) {
    if (isset($updates[$old_token])) {
      $new_token = $updates[$old_token];
      $text = str_replace("{$leading}{$old_token}{$trailing}", "[{$new_token}]", $text);

      // Also replace any tokens that have a -raw suffix.
      $text = str_replace("{$leading}{$old_token}-raw{$trailing}", "[{$new_token}]", $text);
    }
  }

  // Convert other Ubercart Addresses tokens.
  $match = array(
    '[uc_addresses_country_',
    '[uc_addresses_zone_',
    'created-',
    'modified-',
    '[uc_addresses_',
  );
  $replace = array(
    '[uc_addresses:country:country_',
    '[uc_addresses:zone:zone_',
    'created:',
    'modified:',
    '[uc_addresses:',
  );
  $replaced = str_replace($match, $replace, $text);

  // Remove "-raw" parts.
  $match = array(
    '/\\[uc\\_addresses:([^\\s]*)\\-raw\\]/',
  );
  $replace = array(
    '[uc_addresses:${1}]',
  );
  $replaced = preg_replace($match, $replace, $replaced);
  return $replaced;
}