You are here

function simplenews_mail_tokens in Simplenews 6

Build array of mail tokens.

Depending on the context in which this function is called, some tokens may return an empty value. e.g. !newsletter_url is empty when called while building a confirmation email.

Parameters

$subscription: Subscription or user object

$context: [newsletter] newsletter term object [node] node object

$language: Language object

Return value

array of tokens and token values.

2 calls to simplenews_mail_tokens()
simplenews_mail in ./simplenews.module
Implementation of hook_mail().
simplenews_nodeapi in ./simplenews.module
Implementation of hook_nodeapi().

File

./simplenews.module, line 2207
Simplnews node handling, sent email, newsletter block and general hooks

Code

function simplenews_mail_tokens($subscription, $context, $language) {

  // Build hash for (un)subscription confirmation link for subscribed user.
  $hash = '';
  if (isset($subscription->snid) && isset($context['newsletter']->tid)) {
    $hash = _simplenews_generate_hash($subscription->mail, $subscription->snid, $context['newsletter']->tid);
  }

  // Get newsletter name if not in newsletter object.
  $name = isset($context['newsletter']->name) ? $context['newsletter']->name : '';
  if (!$name) {
    if (isset($context['node']->simplenews['tid'])) {
      if ($term = taxonomy_get_term($context['node']->simplenews['tid'])) {

        // Translate newsletter name if required.
        $name = $term->name;
        if (module_exists('i18ntaxonomy') && i18ntaxonomy_vocabulary(variable_get('simplenews_vid', '')) == I18N_TAXONOMY_LOCALIZE) {
          $name = tt('taxonomy:term:' . $context['node']->simplenews['tid'] . ':name', $name, $language->language);
        }
      }
    }
  }

  // Get tokens from user_mail_tokens() and add simplenews variables.
  if ($subscription->uid) {
    $vars = user_mail_tokens($subscription, $language);
  }
  else {
    $vars = array();
    $vars['!site'] = variable_get('site_name', 'Drupal');
    $vars['!mailto'] = isset($subscription->mail) ? $subscription->mail : '';
    $vars['!date'] = format_date(time(), 'medium', '', NULL, $language->language);
    $vars['!login_uri'] = url('user', array(
      'absolute' => TRUE,
      'language' => $language,
    ));
  }

  // Replace existing uri and uri_brief to cope with multilingual sites.
  $vars['!uri'] = url('', array(
    'absolute' => TRUE,
    'language' => $language,
  ));
  $vars['!uri_brief'] = preg_replace('!^https?://!', '', $vars['!uri']);
  $vars['!confirm_subscribe_url'] = url('newsletter/confirm/add/' . $hash, array(
    'absolute' => TRUE,
    'language' => $language,
  ));
  $vars['!confirm_unsubscribe_url'] = url('newsletter/confirm/remove/' . $hash, array(
    'absolute' => TRUE,
    'language' => $language,
  ));
  $vars['!newsletter_url'] = isset($context['node']->nid) ? url('node/' . $context['node']->nid, array(
    'absolute' => TRUE,
    'language' => $language,
  )) : '';
  $vars['!newsletter_name'] = $name;
  return $vars;
}