diff --git a/comment_notify.inc b/comment_notify.inc index 8797a5c..b8475fc 100644 --- a/comment_notify.inc +++ b/comment_notify.inc @@ -22,8 +22,8 @@ function comment_notify_get_user_notification_setting($uid) { // Handle anonymous users with defaults. if ($uid == 0) { $users[0] = new stdClass(); - $users[0]->comment_notify = comment_notify_variable_registry_get('default_registered_mailalert'); - $users[0]->node_notify = comment_notify_variable_registry_get('node_notify_default_mailalert'); + $users[0]->comment_notify = variable_get_value('comment_notify_default_registered_mailalert'); + $users[0]->node_notify = variable_get_value('comment_notify_node_notify_default_mailalert'); } else { $setting = db_select('comment_notify_user_settings', 'cnus') @@ -45,8 +45,8 @@ function comment_notify_get_user_notification_setting($uid) { function comment_notify_get_default_notification_setting() { return (object) array( - 'comment_notify' => comment_notify_variable_registry_get('default_registered_mailalert'), - 'node_notify' => comment_notify_variable_registry_get('node_notify_default_mailalert') + 'comment_notify' => variable_get_value('comment_notify_default_registered_mailalert'), + 'node_notify' => variable_get_value('comment_notify_node_notify_default_mailalert') ); } @@ -105,7 +105,7 @@ function comment_notify_set_user_notification_setting($uid, $node_notification = throw new Exception('Cannot set user preference, uid missing'); } $fields = array('uid' => $uid); - + if (!is_null($node_notification)) { $fields['node_notify'] = $node_notification; } @@ -320,43 +320,3 @@ function comment_notify_unsubscribe_by_hash($hash) { ->execute(); } -/** - * Helper function to centralize variable management and defaults. - * - * All variables fall under the "comment_notify" psuedo namespace. This ensures - * consistancy, and eliminates some verbosity in the calling code. In addition - * by storing all of the variables in one place, we avoid repeating duplicate - * defaults which are harder to maintain. - * - * @param string $name - * @return mixed - */ -function comment_notify_variable_registry_get($name) { - $variables = array(); - $variables['author_subject'] = '[site:name] :: new comment for your post.'; - $variables['available_alerts'] = array(COMMENT_NOTIFY_NODE, COMMENT_NOTIFY_COMMENT); - $variables['default_anon_mailalert'] = COMMENT_NOTIFY_NODE; - $variables['node_notify_default_mailtext'] = AUTHOR_MAILTEXT; - $variables['default_registered_mailalert'] = COMMENT_NOTIFY_DISABLED; - $variables['node_notify_default_mailalert'] = 0; - $variables['comment_notify_default_registered_mailalert'] = COMMENT_NOTIFY_DISABLED; - $variables['watcher_subject'] = '[site:name] :: new comment on [node:title]'; - $variables['comment_notify_default_mailtext'] = DEFAULT_MAILTEXT; - $variables['node_types'] = array('article' => 'article'); - - // Errors - $variables['error_anonymous_email_missing'] = 'If you want to subscribe to comments you must supply a valid e-mail address.'; - return variable_get("comment_notify_" . $name, $variables[$name]); -} - -/** - * Helper function to centralize setting variables. - * - * @param string $name - * @param mixed $value - * @return boolean - */ -function comment_notify_variable_registry_set($name, $value) { - return variable_set("comment_notify_" . $name, $value); -} - diff --git a/comment_notify.info b/comment_notify.info index 955d1ee..ccfd2e1 100644 --- a/comment_notify.info +++ b/comment_notify.info @@ -2,6 +2,7 @@ name = Comment Notify description = "Comment follow-up e-mail notification for anonymous as well as registered users." dependencies[] = comment dependencies[] = token +dependencies[] = variable core = 7.x files[] = comment_notify.install files[] = comment_notify.module diff --git a/comment_notify.install b/comment_notify.install index fa342c1..f0c75e1 100644 --- a/comment_notify.install +++ b/comment_notify.install @@ -31,14 +31,6 @@ function comment_notify_install() { ->condition('name', 'comment_notify'); } -/** - * Implements hook_uninstall(). - */ -function comment_notify_uninstall() { - variable_del('node_notify_default_mailtext'); - db_delete('variable') - ->where('name', "comment_notify_%", 'LIKE'); -} /** * Implements hook_schema(). @@ -120,4 +112,56 @@ function comment_notify_update_7002() { ); db_drop_index('comment_notify', 'notify_hash'); db_change_field('comment_notify', 'notify_hash', 'notify_hash', $new_spec, $keys); -} \ No newline at end of file +} + +/** + * Fix tokens in comment subscriber e-mail template. + */ +function comment_notify_update_7003() { + $variables = array( + 'comment_notify_author_subject' => 'author', + 'comment_notify_node_notify_default_mailtext' => 'author', + 'comment_notify_watcher_subject' => 'commenter', + 'comment_notify_comment_notify_default_mailtext' => 'commenter', + ); + foreach ($variables as $variable => $context) { + // If the variable is using the default value, this will return NULL and we + // can skip it. + if ($text = variable_get($variable)) { + $replacements = array( + '[comment:unsubscribe-url]' => '[comment-subscribed:unsubscribe-url]', + '[comment:name]' => '[comment:author]', + '[node:' => '[comment:node:', // Replace all node tokens with comment:node. + ); + if ($context == 'author') { + $replacements['[user:name]'] = '[comment:node:author]'; + $replacements['[user:'] = '[comment:node:author:'; + } + elseif ($context == 'commenter') { + $replacements['[user:name]'] = '[comment-subscribed:author]'; + $replacements['[user:'] = '[comment-subscribed:author:'; + } + $text = strtr($text, $replacements); + variable_set($variable, $text); + } + } + return 'Comment notify token strings updated.'; +} + +/** + * Update comment_notify to use variables + **/ +function comment_notify_update_7004() { + $variables = array( + 'comment_notify_watcher_subject' => 'comment_notify_comment_notify_subject', + 'comment_notify_comment_notify_default_mailtext' => 'comment_notify_comment_notify_body', + 'comment_notify_author_subject' => 'comment_notify_node_notify_subject', + 'comment_notify_node_notify_default_mailtext' => 'comment_notify_node_notify_body' + ); + foreach ($variables as $old => $new) { + $old_val = variable_get($old); + variable_set($new, $old_val); + } + return 'Comment notify variables updated'; +} + diff --git a/comment_notify.module b/comment_notify.module index 87f2cb7..596a9e6 100644 --- a/comment_notify.module +++ b/comment_notify.module @@ -14,7 +14,7 @@ define('COMMENT_NOTIFY_COMMENT', 2); define('AUTHOR_MAILTEXT', 'Hi [comment:node:author], -You have received a comment on: "[node:title]" +You have received a comment on: "[comment:node:title]" ---- [comment:title] @@ -32,9 +32,9 @@ disable this by logging in and changing the settings on your user account at [site:url]'); define('DEFAULT_MAILTEXT', -'Hi [user:name], +'Hi [comment-subscribed:author], -[comment:name] has commented on: "[node:title]" +[comment:author] has commented on: "[comment:node:title]" ---- [comment:title] @@ -45,7 +45,7 @@ You can view the comment at the following url [comment:url] You can stop receiving emails when someone replies to this post, -by going to [comment:unsubscribe-url] +by going to [comment-subscribed:unsubscribe-url] You can set up auto-following feature for all future posts by creating your own user with a few clicks here [site:login-url] @@ -173,7 +173,7 @@ function comment_notify_menu() { 'title' => 'Comment notify', 'description' => 'Configure settings for e-mails about new comments.', 'page callback' => 'drupal_get_form', - 'page arguments' => array('comment_notify_settings'), + 'page arguments' => array('variable_module_form', 'comment_notify'), 'access arguments' => array('administer comment notify'), 'type' => MENU_NORMAL_ITEM, ); @@ -181,7 +181,7 @@ function comment_notify_menu() { 'title' => 'Settings', 'description' => 'Configure settings for e-mails about new comments.', 'page callback' => 'drupal_get_form', - 'page arguments' => array('comment_notify_settings'), + 'page arguments' => array('variable_module_form', 'comment_notify'), 'access arguments' => array('administer comment notify'), 'type' => MENU_DEFAULT_LOCAL_TASK, ); @@ -220,7 +220,6 @@ function comment_notify_disable_page($hash) { return ' '; } - function comment_notify_comment_validate($comment) { global $user; // We assume that if they are non-anonymous then they have a valid mail. @@ -328,7 +327,7 @@ function comment_notify_form_alter(&$form, &$form_state, $form_id) { $form['comment_notify_settings']['node_notify'] = array( '#type' => 'checkbox', '#title' => t('Receive node follow-up notification e-mails'), - '#default_value' => isset($node_notify) ? $node_notify : comment_notify_variable_registry_get('node_notify_default_mailalert'), + '#default_value' => isset($node_notify) ? $node_notify : variable_get_value('comment_notify_node_notify_default_mailalert'), '#description' => t('Check this box to receive an e-mail notification for follow-ups on your nodes (pages, forum topics, etc). You can not disable notifications for individual threads.') ); } @@ -344,7 +343,7 @@ function comment_notify_form_alter(&$form, &$form_state, $form_id) { $form['comment_notify_settings']['comment_notify'] = array( '#type' => 'select', '#title' => t('Receive comment follow-up notification e-mails'), - '#default_value' => isset($comment_notify) ? array($comment_notify) : array(comment_notify_variable_registry_get('comment_notify_default_registered_mailalert')), + '#default_value' => isset($comment_notify) ? array($comment_notify) : array(variable_get_value('comment_notify_default_registered_mailalert')), '#options' => $available_options, '#description' => t("Check this box to receive e-mail notification for follow-up comments to comments you posted. You can later disable this on a post-by-post basis... so if you leave this to YES, you can still disable follow-up notifications for comments you don't want follow-up mails anymore - i.e. for very popular posts.") ); @@ -433,12 +432,20 @@ function _comment_notify_mailalert($comment) { if (!empty($author->comment_notify_settings->node_notify) && $author->node_notify_mailalert == 1 && $user->uid != $author->uid && node_access('view', $node, $author)) { // Get the author's language. $language = user_preferred_language($author); - $raw_values = array( - 'subject' => comment_notify_variable_registry_get('author_subject'), - 'body' => comment_notify_variable_registry_get('node_notify_default_mailtext'), //JS @todo:change this. - ); + if (function_exists('i18n_variable_get')) { + $raw_values = array( + 'subject' => i18n_variable_get('comment_notify_node_notify_subject', $language->language), + 'body' => i18n_variable_get('comment_notify_node_notify_body', $language->language), //JS @todo:change this. + ); + } + else { + $raw_values = array( + 'subject' => variable_get_value('comment_notify_node_notify_subject'), + 'body' => variable_get_value('comment_notify_node_notify_body'), //JS @todo:change this. + ); + } foreach ($raw_values as $k => $v) { - $message[$k] = token_replace(t($v), array('comment' => $comment, 'node' => $node, 'user' => $user)); + $message[$k] = token_replace(t($v), array('comment' => $comment)); } drupal_mail('comment_notify', 'comment_notify_mail', $author->mail, $language, $message); @@ -453,8 +460,10 @@ function _comment_notify_mailalert($comment) { $watchers = comment_notify_get_watchers($nid); foreach ($watchers as $alert) { - $umail = empty($alert->umail) ? $alert->uinit : $alert->umail; - $mail = empty($alert->cmail) ? $umail : $alert->cmail; + // 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) { @@ -464,26 +473,28 @@ function _comment_notify_mailalert($comment) { if ($mail != $comment_mail && !in_array($mail, $sent_to) && ($alert->uid != $comment->uid || $alert->uid == 0)) { $message = array(); - if (!empty($alert->uid)) { - $recipient_user = user_load($alert->uid); - $language = user_preferred_language($recipient_user); - } - else { - $language = language_default(); - $recipient_user = drupal_anonymous_user(); - } + $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'), - 'body' => comment_notify_variable_registry_get('comment_notify_default_mailtext'), //JS @todo:change this var name. - ); + if (function_exists('i18n_variable_get')) { + $raw_values = array( + 'subject' => i18n_variable_get('comment_notify_comment_notify_subject', $language->language), + 'body' => i18n_variable_get('comment_notify_comment_notify_body', $language->language), //JS @todo:change this. + ); + } + else { + $raw_values = array( + 'subject' => variable_get_value('comment_notify_node_notify_subject'), + 'body' => variable_get_value('comment_notify_node_notify_body'), //JS @todo:change this. + ); + } foreach ($raw_values as $k => $v) { - $message[$k] = token_replace(t($v), array('comment' => $comment, 'node' => $node, 'user' => $user)); + $message[$k] = token_replace(t($v), array('comment' => $comment, 'comment-subscribed' => $alert)); } drupal_mail('comment_notify', 'comment_notify_mail', $mail, $language, $message); @@ -559,114 +570,21 @@ function comment_notify_unsubscribe_submit($form, &$form_state) { } } -/* - * Page callback for administrative settings form. +/** + * Get the unsubscribe link for a comment subscriber. + * + * @param $comment + * The subscribed comment object. + * + * @return + * A string with the internal path to the unsubscribe link, ready to be + * passed to the url() function. + * + * @see comment_notify_get_notify_hash(). */ -function comment_notify_settings() { +function comment_notify_get_unsubscribe_url($comment) { module_load_include('inc', 'comment_notify', 'comment_notify'); - - $form['comment_notify_settings'] = array(); - - // Only perform comment_notify for certain node types. - $enabled_types = comment_notify_variable_registry_get('node_types'); - $anonymous_problems = ''; - foreach (node_type_get_names() as $type => $name) { - $checkboxes[$type] = check_plain($name); - $default[] = $type; - - // If they don't have the ability to leave contact info, then we make a report - if (isset($enabled_types[$type]) && $enabled_types[$type] && variable_get('comment_anonymous_' . $type, COMMENT_ANONYMOUS_MAYNOT_CONTACT) == COMMENT_ANONYMOUS_MAYNOT_CONTACT) { - $account = drupal_anonymous_user(); - if (user_access('subscribe to comments', $account)) { - $anonymous_problems[] = l(t('@content-type', array('@content-type' => $name)), 'admin/structure/types/manage/'. $type); - } - } - } - - if (!empty($anonymous_problems)) { - drupal_set_message(t('Anonymous commenters have the permission to subscribe to comments but cannot leave their contact information on the following content types: !types. You should either disable subscriptions on those types here, revoke the permission for anonymous users, or enable anonymous users to leave their contact information in the comment settings.', array('!types' => implode(', ', $anonymous_problems))), 'status', FALSE); - } - - $form['comment_notify_settings']['comment_notify_node_types'] = array( - '#type' => 'checkboxes', - '#title' => t('Content types to enable for comment notification'), - '#default_value' => $enabled_types, - '#options' => $checkboxes, - '#description' => t('Comments on content types enabled here will have the option of comment notification.'), - ); - - $form['comment_notify_settings']['comment_notify_available_alerts'] = array( - '#type' => 'checkboxes', - '#title' => t('Available subscription modes'), - '#return_value' => 1, - '#default_value' => comment_notify_variable_registry_get('available_alerts'), - '#description' => t('Choose which notification subscription styles are available for users'), - '#options' => array( - COMMENT_NOTIFY_NODE => t('All comments'), - COMMENT_NOTIFY_COMMENT => t('Replies to my comment') - ) - ); - - $available_options[COMMENT_NOTIFY_DISABLED] = t('No notifications'); - $available_options += _comment_notify_options(); - $form['comment_notify_settings']['comment_notify_default_anon_mailalert'] = array( - '#type' => 'select', - '#title' => t('Default state for the notification selection box for anonymous users'), - '#return_value' => 1, - '#default_value' => comment_notify_variable_registry_get('default_anon_mailalert'), - '#options' => $available_options, - ); - - $form['comment_notify_settings']['comment_notify_default_registered_mailalert'] = array( - '#type' => 'select', - '#title' => t('Default state for the notification selection box for registered users'), - '#return_value' => 1, - '#default_value' => comment_notify_variable_registry_get('default_registered_mailalert'), - '#description' => t('This flag presets the flag for the follow-up notification on the form that anon users will see when posting a comment'), - '#options' => $available_options, - ); - - $form['comment_notify_settings']['comment_notify_node_notify_default_mailalert'] = array( - '#type' => 'checkbox', - '#title' => t('Subscribe users to their node follow-up notification emails by default'), - '#default_value' => comment_notify_variable_registry_get('node_notify_default_mailalert'), - '#description' => t('If this is checked, new users will receive e-mail notifications for follow-ups on their nodes by default until they individually disable the feature.'), - ); - - $form['comment_notify_settings']['comment_notify_comment_notify_default_mailtext'] = array( - '#type' => 'textarea', - '#title' => t('Default mail text for sending out notifications to commenters'), - '#default_value' => comment_notify_variable_registry_get('comment_notify_default_mailtext'), - '#return_value' => 1, - '#cols' => 80, - '#rows' => 15 - ); - - $form['comment_notify_settings']['comment_notify_node_notify_default_mailtext'] = array( - '#type' => 'textarea', - '#title' => t('Default mail text for sending out the notifications to node authors'), - '#default_value' => comment_notify_variable_registry_get('node_notify_default_mailtext'), - '#return_value' => 1, - '#cols' => 80, - '#rows' => 15 - ); - - $form['comment_notify_settings']['token_help'] = array( - '#theme' => 'token_tree', - '#token_types' => array('comment'), - ); - - $form['#validate'] = array('comment_notify_settings_validate'); - - return system_settings_form($form); -} - -function comment_notify_settings_validate($form, &$form_state) { - $sum_enabled = 0; - foreach ($form_state['values']['comment_notify_available_alerts'] as $enabled) { - $sum_enabled += $enabled; - } - if (!$sum_enabled) { - form_set_error('comment_notify_available_alerts', 'You must enable at least one subscription mode.'); + if ($hash = comment_notify_get_notify_hash($comment->cid)) { + return 'comment_notify/disable/' . $hash; } } diff --git a/comment_notify.tokens.inc b/comment_notify.tokens.inc index 2f1dab7..7c89702 100644 --- a/comment_notify.tokens.inc +++ b/comment_notify.tokens.inc @@ -9,10 +9,20 @@ * Implements hook_token_info(). */ function comment_notify_token_info() { + // Comment tokens. $info['tokens']['comment']['unsubscribe-url'] = array( 'name' => t('Unsubscribe URL'), 'description' => t('The URL to disable notifications for the comment.'), + 'type' => 'url', ); + + // Comment subscriber token type (extends the comment token type). + $info['types']['comment-subscribed'] = array( + 'name' => t('Subscribed comment'), + 'description' => t('Tokens related to a comment that is subscribed to new comments.'), + 'type' => 'comment', + ); + return $info; } @@ -38,12 +48,23 @@ function comment_notify_tokens($type, $tokens, array $data = array(), array $opt foreach ($tokens as $name => $original) { switch ($name) { case 'unsubscribe-url': - module_load_include('inc', 'comment_notify', 'comment_notify'); - if ($hash = comment_notify_get_notify_hash($comment->cid)) { - $replacements[$original] = url('comment_notify/disable/' . $hash, $url_options); + if ($unsubscribe_url = comment_notify_get_unsubscribe_url($comment)) { + $replacements[$original] = url($unsubscribe_url, $url_options); } break; } } + + // [comment:unsubscribe-url:*] chained token replacements. + if (($unsubscribe_url_tokens = token_find_with_prefix($tokens, 'unsubscribe-url')) && $unsubscribe_url = comment_notify_get_unsubscribe_url($comment)) { + $replacements += token_generate('url', $unsubscribe_url_tokens, array('path' => $unsubscribe_url), $options); + } + } + + // Comment subscriber tokens (pass through to comment token replacement). + if ($type == 'comment-subscribed' && !empty($data['comment-subscribed'])) { + $replacements += token_generate('comment', $tokens, array('comment' => $data['comment-subscribed']), $options); } + + return $replacements; } diff --git a/comment_notify.variable.inc b/comment_notify.variable.inc new file mode 100644 index 0000000..6c05e31 --- /dev/null +++ b/comment_notify.variable.inc @@ -0,0 +1,121 @@ + 'options', + 'title' => t('Content types to enable for comment notification'), + 'description' => t('Comments on content types enabled here will have the option of comment notification.'), + 'options' => _comment_notify_node_types_options(), + 'group' => 'comment_notify_settings' + ); + + $variables['comment_notify_available_alerts'] = array( + 'type' => 'options', + 'title' => t('Available subscription modes'), + 'description' => t('Choose which notification subscription styles are available for users'), + 'options' => array( + COMMENT_NOTIFY_NODE => t('All comments'), + COMMENT_NOTIFY_COMMENT => t('Replies to my comment') + ), + 'group' => 'comment_notify_settings' + ); + $variables['comment_notify_default_anon_mailalert'] = array( + 'type' => 'select', + 'title' => t('Default state for the notification selection box for anonymous users'), + 'options' => array( + COMMENT_NOTIFY_DISABLED => t('No notifications'), + COMMENT_NOTIFY_NODE => t('All comments'), + COMMENT_NOTIFY_COMMENT => t('Replies to my comment') + ), + 'group' => 'comment_notify_settings' + ); + + $variables['comment_notify_comment_notify_[mail_part]'] = array( + 'type' => 'mail_text', + 'title' => t('Mail for sending out notifications to commenters'), + 'description' => t('Notify commenters that a new comment has been posted'), + 'children' => array( + 'comment_notify_comment_notify_subject' => array( + 'default' => '[site:name] :: new comment on [node:title]', + ), + 'comment_notify_comment_notify_body' => array( + 'default' => DEFAULT_MAILTEXT, + ), + ), + 'group' => 'comment_notify_mails' + ); + + $variables['comment_notify_default_registered_mailalert'] = array( + 'type' => 'select', + 'title' => t('Default state for the notification selection box for registered users'), + 'options' => array( + COMMENT_NOTIFY_DISABLED => t('No notifications'), + COMMENT_NOTIFY_NODE => t('All comments'), + COMMENT_NOTIFY_COMMENT => t('Replies to my comment') + ), + 'description' => t('This flag presets the flag for the follow-up notification on the form that anon users will see when posting a comment'), + 'group' => 'comment_notify_settings' + ); + + $variables['comment_notify_node_notify_default_mailalert'] = array( + 'type' => 'boolean', + 'title' => t('Subscribe users to their node follow-up notification emails by default'), + 'default' => FALSE, + 'description' => t('If this is checked, new users will receive e-mail notifications for follow-ups on their nodes by default until they individually disable the feature.'), + 'group' => 'comment_notify_settings' + ); + + $variables['comment_notify_node_notify_[mail_part]'] = array( + 'type' => 'mail_text', + 'title' => t('Mail for sending out notifications to node authors'), + 'description' => t('Notify node authors when a new comment has been posted'), + 'group' => 'comment_notify_mails', + 'children' => array( + 'comment_notify_node_notify_subject' => array( + 'default' => '[site:name] :: new comment for your post.', + ), + 'comment_notify_node_notify_body' => array( + 'default' => AUTHOR_MAILTEXT, + ), + ), + ); + + return $variables; +} + +/** + * Helper function to get options for comment_notify_node_types + */ +function _comment_notify_node_types_options() { + foreach (node_type_get_names() as $type => $name) { + $checkboxes[$type] = check_plain($name); + } + return $checkboxes; +} + +/** + * Implements hook_variable_group_info() + */ +function comment_notify_variable_group_info() { + $groups['comment_notify_settings'] = array( + 'title' => t('Comment notify settings'), + 'description' => t('Comment notify settings'), + 'access' => 'administer comment notify', + 'path' => 'admin/config/people/comment_notify/settings', + ); + $groups['comment_notify_mails'] = array( + 'title' => t('Comment notify emails'), + 'description' => t('Comment notify emails'), + 'access' => 'administer comment notify', + 'path' => 'admin/config/people/comment_notify/settings', + ); + return $groups; +} +