For some reason the uncheck didn't register in the database when I used a checkbox. I've made a version that uses radio-buttons instead.
(I can't edit my old post, so sorry for the inconvenience with the extra code...)
<?php
/**
* Plugin Name: Post Notification
* Plugin Description: Sends a Notification email if there's a new post to a favorite topic.
(Modified Version 1.4 with Post Content included in E-Mail)
(And modified to send mail in utf8 format so danish, swedish, russian etc. characters display correctly).
Also modified so user can select whether to get a mail on favorite topics or all topics.
Changed subject of email to show which topic is updated - and from: is now set to the username of the poster instead of the forum name.
* Author: Thomas Klaiber
* Author URI: http://thomasklaiber.com/
* Plugin URI: http://thomasklaiber.com/bbpress/post-notification/
* Version: 1.4.x
*/
function notification_new_post($post_id=0) {
global $bbdb, $bb_table_prefix, $topic_id, $bb_current_user;
$all_users = notification_select_all_users();
foreach ($all_users as $userdata) :
if ( notification_is_activated( $userdata->ID ) && $userdata->ID != $bb_current_user->ID) :
if (notification_all_is_activated( $userdata->ID ) || is_user_favorite( $userdata->ID, $topic_id ) ) :
$message = __("There is a new post on:\n %1\$s \n\nReply by:\n %2\$s \n\nText:\n %3\$s \n\n%4\$s ");
mail_utf8( $userdata->user_email, bb_get_option('name') . ': ' . get_topic_title($topic_id), sprintf( $message, get_topic_title($topic_id), get_user_name($bb_current_user->ID), strip_tags(get_post_text($post_id)), get_topic_link($topic_id) ), 'From: '.get_user_name($bb_current_user->ID).' <'.bb_get_option('from_email').'>'
);
endif;
endif;
endforeach;
}
add_action('bb_new_post', 'notification_new_post');
function mail_utf8($to, $subject = '(No subject)', $message = '', $header = '') {
$header_ = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/plain; charset=UTF-8' . "\r\n";
mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=', $message, $header_ . $header);
}
function notification_select_all_users() {
global $bbdb;
$all_users = $bbdb->get_results("SELECT ID, user_email FROM $bbdb->users WHERE user_status=0");
return $all_users;
}
function notification_profile() {
global $user_id, $bb_current_user;
if ( bb_is_user_logged_in() ) :
$checked_on = "checked='checked'";
$checked_off = "";
if (!notification_is_activated($user_id)) :
$checked_on = "";
$checked_off = "checked='checked'";
endif;
$checked_all_on = "";
$checked_all_off = "checked='checked'";
if (notification_all_is_activated($user_id)) :
$checked_all_on = "checked='checked'";
$checked_all_off = "";
endif;
echo "<fieldset>
<legend>Favorite Notification</legend>
<p> " . __('Email me when there'."'".'s a new post in a topic.') . "</p>
<table width=\"100%\">
<tr>
<th width=\"21%\" scope=\"row\">" . __("Activate") . ":</th>
<td width=\"79%\" ><input name=\"favorite_notification\" id=\"favorite_notification\" type=\"radio\" value=\"1\"" . $checked_on . " /></td>
</tr>
<tr>
<th width=\"21%\" scope=\"row\">" . __("Deactivate") . ":</th>
<td width=\"79%\" ><input name=\"favorite_notification\" id=\"favorite_notification\" type=\"radio\" value=\"0\"" . $checked_off . " /></td>
</tr>
</table>
<p> " . __('When should the system send an email?') . "</p>
<table width=\"100%\">
<tr>
<th width=\"21%\" scope=\"row\">" . __("Only favorites") . ":</th>
<td width=\"79%\" >
<input name=\"favorite_notification_all\" id=\"favorite_notification_all\" type=\"radio\" value=\"0\"" . $checked_all_off . " /></td>
</tr>
<tr>
<th width=\"21%\" scope=\"row\">" . __("All topics") . ":</th>
<td width=\"79%\" >
<input name=\"favorite_notification_all\" id=\"favorite_notification_all\" type=\"radio\" value=\"1\"" . $checked_all_on . " /></td>
</tr>
</table>
</fieldset>\n\n";
endif;
}
add_action('extra_profile_info', 'notification_profile');
function notification_profile_edit() {
global $user_id;
bb_update_usermeta($user_id, "favorite_notification", $_POST['favorite_notification']);
bb_update_usermeta($user_id, "favorite_notification_all", $_POST['favorite_notification_all']);
}
add_action('profile_edited', 'notification_profile_edit');
function notification_is_activated($user_id) {
$user = bb_get_user( $user_id );
if ($user->favorite_notification) : return true; else : return false; endif;
}
function notification_all_is_activated($user_id) {
$user = bb_get_user( $user_id );
if ($user->favorite_notification_all) : return true; else : return false; endif;
}
?>