You are here

function _comment_notify_mailalert in Comment Notify 7

Same name and namespace in other branches
  1. 8 comment_notify.module \_comment_notify_mailalert()
  2. 5.2 comment_notify.module \_comment_notify_mailalert()
  3. 5 comment_notify.module \_comment_notify_mailalert()
  4. 6 comment_notify.module \_comment_notify_mailalert()

Private function to send the notifications.

Parameters

array $comment: The comment array as found in hook_comment $op = publish.

3 calls to _comment_notify_mailalert()
comment_notify_comment_insert in ./comment_notify.module
Implements hook_comment_insert().
comment_notify_comment_publish in ./comment_notify.module
comment_notify_comment_update in ./comment_notify.module
Implements hook_comment_update().

File

./comment_notify.module, line 434
This module provides comment follow-up e-mail notification for anonymous and registered users.

Code

function _comment_notify_mailalert($comment) {
  module_load_include('inc', 'comment_notify', 'comment_notify');
  $comment = (object) $comment;
  global $language;
  global $user;
  $initial_language = $language;
  $nid = $comment->nid;

  // Check to see if a notification has already been sent for this
  // comment so that edits to a comment don't trigger an additional
  // notification.
  if (!empty($comment->notified)) {
    return;
  }
  $node = node_load($nid);

  // No mails if this is not an enabled content type.
  $enabled_types = variable_get('comment_notify_node_types', array(
    $node->type => TRUE,
  ));
  if (empty($enabled_types[$node->type])) {
    return;
  }
  if (empty($comment->mail)) {
    $comment_account = user_load_by_name($comment->name);
    $comment_mail = isset($comment_account->mail) ? $comment_account->mail : '';
  }
  else {
    $comment_mail = $comment->mail;
  }
  $sent_to = array();

  // Send to a subscribed author if they are not the current commenter.
  $author = user_load($node->uid);

  // Do they explicitly want this? Or is it default to send to users?
  // Is the comment author not the node author? Do they have access? Do they
  // have an email (e.g. anonymous)?
  if ((!empty($author->comment_notify_settings->node_notify) && $author->comment_notify_settings->node_notify == 1 || comment_notify_variable_registry_get('node_notify_default_mailalert') == 1 && !isset($author->comment_notify_settings->node_notify)) && $user->uid != $author->uid && node_access('view', $node, $author) && !empty($author->mail)) {

    // Get the author's language.
    $language = user_preferred_language($author);
    $raw_values = array(
      'subject' => comment_notify_variable_registry_get('author_subject'),
      // JS @todo:change this.
      'body' => comment_notify_variable_registry_get('node_notify_default_mailtext'),
    );
    foreach ($raw_values as $k => $v) {
      $message[$k] = token_replace(t($v), array(
        'comment' => $comment,
      ), array(
        'sanitize' => FALSE,
      ));
    }
    drupal_mail('comment_notify', 'comment_notify_mail', $author->mail, $language, $message);
    $sent_to[] = strtolower($author->mail);
  }

  // For "reply to my comments" notifications, figure out what thread this is.
  $thread = isset($comment->thread) ? $comment->thread : '';

  // Get the list of commenters to notify.
  $watchers = comment_notify_get_watchers($nid);
  foreach ($watchers as $alert) {

    // If the user is not anonymous, always load the current e-mail address
    // from his or her user account instead of trusting $comment->mail.
    $recipient_user = !empty($alert->uid) ? user_load($alert->uid) : drupal_anonymous_user();
    $mail = !empty($recipient_user->mail) ? $recipient_user->mail : $alert->cmail;
    $relevant_thread = drupal_substr($thread, 0, drupal_strlen($alert->thread) - 1);
    if ($alert->notify == COMMENT_NOTIFY_COMMENT && strcmp($relevant_thread . '/', $alert->thread) != 0) {
      continue;
    }
    if ($mail != $comment_mail && !in_array(strtolower($mail), $sent_to) && ($alert->uid != $comment->uid || $alert->uid == 0)) {
      $message = array();
      $language = !empty($alert->uid) ? user_preferred_language($recipient_user) : language_default();

      // Make sure they have access to this node before showing a bunch of node
      // information.
      if (!node_access('view', $node, $recipient_user)) {
        continue;
      }
      $raw_values = array(
        'subject' => comment_notify_variable_registry_get('watcher_subject'),
        // JS @todo:change this var name.
        'body' => comment_notify_variable_registry_get('comment_notify_default_mailtext'),
      );
      foreach ($raw_values as $k => $v) {
        $message[$k] = token_replace(t($v), array(
          'comment' => $comment,
          'comment-subscribed' => $alert,
        ), array(
          'sanitize' => FALSE,
        ));
      }
      drupal_mail('comment_notify', 'comment_notify_mail', $mail, $language, $message);
      $sent_to[] = strtolower($mail);

      // Make the mail link to user's /edit, unless it's an anonymous user.
      if ($alert->uid != 0) {
        $user_mail = l($mail, 'user/' . $alert->uid . '/edit');
      }
      else {
        $user_mail = check_plain($mail);
      }

      // Add an entry to the watchdog log.
      watchdog('comment_notify', 'Notified: @user_mail', array(
        '@user_mail' => $user_mail,
      ), WATCHDOG_NOTICE, l(t('source comment'), 'node/' . $nid, array(
        'fragment' => 'comment-' . $alert->cid,
      )));

      // Revert to previous (site default) locale.
      $language = $initial_language;
    }
  }

  // Record that a notification was sent for this comment so that
  // notifications aren't sent again if the comment is later edited.
  comment_notify_mark_comment_as_notified($comment);
}