You are here

function privatemsg_token_replace in Privatemsg 7

Same name and namespace in other branches
  1. 7.2 privatemsg.module \privatemsg_token_replace()

Wrapper function for token_replace() that does not replace the tokens if the user viewing the message is not a recipient.

5 calls to privatemsg_token_replace()
privatemsg_new in ./privatemsg.pages.inc
privatemsg_thread_load in ./privatemsg.module
Load a thread with all the messages and participants.
privatemsg_tokens in ./privatemsg.module
Implements hook_tokens().
template_preprocess_privatemsg_view in ./privatemsg.module
theme_privatemsg_list_field__subject in ./privatemsg.theme.inc
Theme the subject of the thread.

File

./privatemsg.module, line 2972
Allows users to send private messages to other users.

Code

function privatemsg_token_replace($text, $data, array $options = array()) {
  global $user;
  if (empty($data['privatemsg_recipient'])) {
    $recipient = $user;
  }
  else {
    $recipient = $data['privatemsg_recipient'];
  }
  if (isset($options['language'])) {
    $url_options['language'] = $options['language'];
    $language_code = $options['language']->language;
  }
  else {
    $language_code = NULL;
  }
  $message = $data['privatemsg_message'];
  $show_span = !isset($options['privatemsg-show-span']) || $options['privatemsg-show-span'];

  // We do not replace tokens if the user viewing the message is the author or
  // not a real recipient to avoid confusion.
  $sql = "SELECT 1 FROM {pm_index} WHERE recipient = :uid AND type IN ('hidden', 'user') AND mid = :mid";
  $args = array(
    ':uid' => $recipient->uid,
    ':mid' => $message->mid,
  );
  if ($message->author->uid == $recipient->uid || !db_query($sql, $args)
    ->fetchField()) {

    // Get all tokens of the message.
    $tokens = token_scan($text);
    $invalid_tokens = array();
    if (function_exists('token_get_invalid_tokens_by_context')) {
      $invalid_tokens = token_get_invalid_tokens_by_context($text, array(
        'privatemsg_message',
      ));
    }
    if (!empty($tokens)) {
      $replacements = array();

      // Loop over the found tokens.
      foreach ($tokens as $tokens_type) {

        // token_replace() returns tokens separated by type.
        foreach ($tokens_type as $original) {

          // Displaying invalid tokens only works with token.module.
          if (in_array($original, $invalid_tokens)) {
            $token = t('INVALID TOKEN @token', array(
              '@token' => $original,
            ), array(
              'langcode' => $language_code,
            ));
            if (!$show_span) {
              $replacements[$original] = '< ' . $token . ' >';
            }
            else {
              $replacements[$original] = '<span class="privatemsg-token-invalid">&lt; ' . $token . ' &gt;</span>';
            }
          }
          else {
            $token = t('Token @token', array(
              '@token' => $original,
            ), array(
              'langcode' => $language_code,
            ));
            if (!$show_span) {
              $replacements[$original] = '< ' . $token . ' >';
            }
            else {
              $replacements[$original] = '<span class="privatemsg-token-valid">&lt; ' . $token . ' &gt;</span>';
            }
          }
        }
      }
      $text = str_replace(array_keys($replacements), $replacements, $text);

      // If there are any tokens, add a notice that the tokens will be replaced
      // for the recipient.
      if (!empty($options['privatemsg-token-notice'])) {
        $text .= '<p class="privatemsg-token-notice">' . t('Note: Valid tokens will be replaced when a recipient is reading this message.') . '</p>';
      }
    }
    return $text;
  }

  // If the user is a recipient, use default token_replace() function.
  return token_replace($text, $data, $options);
}