Commit f7b5a4de authored by greggles's avatar greggles

Issue #952072 by Dave Reid: [comment:unsubscribe-url] is not working (and a few other token issues)

parent 3dd19db3
......@@ -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;
}
......@@ -340,7 +340,7 @@ function comment_notify_variable_registry_get($name) {
$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['watcher_subject'] = '[site:name] :: new comment on [comment:node:title]';
$variables['comment_notify_default_mailtext'] = DEFAULT_MAILTEXT;
$variables['node_types'] = array('article' => 'article');
......@@ -359,4 +359,3 @@ function comment_notify_variable_registry_get($name) {
function comment_notify_variable_registry_set($name, $value) {
return variable_set("comment_notify_" . $name, $value);
}
......@@ -120,4 +120,38 @@ 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.';
}
......@@ -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]
......@@ -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.
......@@ -438,7 +437,7 @@ function _comment_notify_mailalert($comment) {
'body' => comment_notify_variable_registry_get('node_notify_default_mailtext'), //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 +452,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,14 +465,8 @@ 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;
......@@ -483,7 +478,7 @@ function _comment_notify_mailalert($comment) {
);
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);
......@@ -639,7 +634,9 @@ function comment_notify_settings() {
'#default_value' => comment_notify_variable_registry_get('comment_notify_default_mailtext'),
'#return_value' => 1,
'#cols' => 80,
'#rows' => 15
'#rows' => 15,
'#token_types' => array('comment'),
'#element_validate' => array('token_element_validate'),
);
$form['comment_notify_settings']['comment_notify_node_notify_default_mailtext'] = array(
......@@ -648,7 +645,9 @@ function comment_notify_settings() {
'#default_value' => comment_notify_variable_registry_get('node_notify_default_mailtext'),
'#return_value' => 1,
'#cols' => 80,
'#rows' => 15
'#rows' => 15,
'#token_types' => array('comment'),
'#element_validate' => array('token_element_validate'),
);
$form['comment_notify_settings']['token_help'] = array(
......@@ -670,3 +669,22 @@ function comment_notify_settings_validate($form, &$form_state) {
form_set_error('comment_notify_available_alerts', 'You must enable at least one subscription mode.');
}
}
/**
* 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_get_unsubscribe_url($comment) {
module_load_include('inc', 'comment_notify', 'comment_notify');
if ($hash = comment_notify_get_notify_hash($comment->cid)) {
return 'comment_notify/disable/' . $hash;
}
}
......@@ -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;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment