/** * Send a notification to subscribers * * @wp-filter bbp_new_reply 1 */ public function notify_on_reply($reply_id = 0, $topic_id = 0, $forum_id = 0, $anonymous_data = false, $reply_author = 0) { if ($this->handler === null) { return false; } global $wpdb; if (!bbp_is_subscriptions_active()) { return false; } $reply_id = bbp_get_reply_id($reply_id); $topic_id = bbp_get_topic_id($topic_id); $forum_id = bbp_get_forum_id($forum_id); if (!bbp_is_reply_published($reply_id)) { return false; } if (!bbp_is_topic_published($topic_id)) { return false; } $user_ids = bbp_get_topic_subscribers($topic_id, true); if (empty($user_ids)) { return false; } // Poster name $reply_author_name = apply_filters('bbsub_reply_author_name', bbp_get_reply_author_display_name($reply_id)); do_action('bbp_pre_notify_subscribers', $reply_id, $topic_id, $user_ids); // Don't send notifications to the person who made the post $send_to_author = Falcon::get_option('bbsub_send_to_author', false); if (!$send_to_author && !empty($reply_author)) { $user_ids = array_filter($user_ids, function ($id) use($reply_author) { return (int) $id !== (int) $reply_author; }); } // Get userdata for all users $user_ids = array_map(function ($id) { return get_userdata($id); }, $user_ids); // Sanitize the HTML into text $content = apply_filters('bbsub_html_to_text', bbp_get_reply_content($reply_id)); // Build email $text = "%1\$s\n\n"; $text .= "---\nReply to this email directly or view it online:\n%2\$s\n\n"; $text .= "You are receiving this email because you subscribed to it. Login and visit the topic to unsubscribe from these emails."; $text = sprintf($text, $content, bbp_get_reply_url($reply_id)); $text = apply_filters('bbsub_email_message', $text, $reply_id, $topic_id, $content); $subject = apply_filters('bbsub_email_subject', 'Re: [' . get_option('blogname') . '] ' . bbp_get_topic_title($topic_id), $reply_id, $topic_id); $options = array('id' => $topic_id, 'author' => $reply_author_name); $this->handler->send_mail($user_ids, $subject, $text, $options); do_action('bbp_post_notify_subscribers', $reply_id, $topic_id, $user_ids); return true; }
/** * @covers ::bbp_get_topic_subscribers */ public function test_bbp_get_topic_subscribers() { $u = $this->factory->user->create_many(3); $t = $this->factory->topic->create_many(2); // Add topic subscriptions. bbp_add_user_topic_subscription($u[0], $t[0]); bbp_add_user_topic_subscription($u[1], $t[0]); bbp_add_user_topic_subscription($u[2], $t[0]); $subscribers = bbp_get_topic_subscribers($t[0]); $this->assertEquals(array($u[0], $u[1], $u[2]), $subscribers); // Add topic subscriptions. bbp_add_user_topic_subscription($u[0], $t[1]); bbp_add_user_topic_subscription($u[2], $t[1]); $subscribers = bbp_get_topic_subscribers($t[1]); $this->assertEquals(array($u[0], $u[2]), $subscribers); }
/** * Remove a deleted topic from all users' subscriptions * * @since bbPress (r2652) * * @param int $topic_id Get the topic id to remove * @uses bbp_is_subscriptions_active() To check if the subscriptions are active * @uses bbp_get_topic_id To get the topic id * @uses bbp_get_topic_subscribers() To get the topic subscribers * @uses bbp_remove_user_subscription() To remove the user subscription */ function bbp_remove_topic_from_all_subscriptions($topic_id = 0) { // Subscriptions are not active if (!bbp_is_subscriptions_active()) { return; } $topic_id = bbp_get_topic_id($topic_id); // Bail if no topic if (empty($topic_id)) { return; } // Get users $users = (array) bbp_get_topic_subscribers($topic_id); // Users exist if (!empty($users)) { // Loop through users foreach ($users as $user) { // Remove each user bbp_remove_user_subscription($user, $topic_id); } } }
/** * Sends notification emails for new replies to subscribed topics * * Gets new post's ID and check if there are subscribed users to that topic, and * if there are, send notifications * * Note: in bbPress 2.6, we've moved away from 1 email per subscriber to 1 email * with everyone BCC'd. This may have negative repercussions for email services * that limit the number of addresses in a BCC field (often to around 500.) In * those cases, we recommend unhooking this function and creating your own * custom emailer script. * * @since 2.6.0 bbPress (r5413) * * @param int $reply_id ID of the newly made reply * @param int $topic_id ID of the topic of the reply * @param int $forum_id ID of the forum of the reply * @param mixed $anonymous_data Array of anonymous user data * @param int $reply_author ID of the topic author ID * * @uses bbp_is_subscriptions_active() To check if the subscriptions are active * @uses bbp_get_reply_id() To validate the reply ID * @uses bbp_get_topic_id() To validate the topic ID * @uses bbp_get_forum_id() To validate the forum ID * @uses bbp_get_reply() To get the reply * @uses bbp_is_reply_published() To make sure the reply is published * @uses bbp_get_topic_id() To validate the topic ID * @uses bbp_get_topic() To get the reply's topic * @uses bbp_is_topic_published() To make sure the topic is published * @uses bbp_get_reply_author_display_name() To get the reply author's display name * @uses do_action() Calls 'bbp_pre_notify_subscribers' with the reply id, * topic id and user id * @uses bbp_get_topic_subscribers() To get the topic subscribers * @uses apply_filters() Calls 'bbp_subscription_mail_message' with the * message, reply id, topic id and user id * @uses apply_filters() Calls 'bbp_subscription_mail_title' with the * topic title, reply id, topic id and user id * @uses apply_filters() Calls 'bbp_subscription_mail_headers' * @uses get_userdata() To get the user data * @uses wp_mail() To send the mail * @uses do_action() Calls 'bbp_post_notify_subscribers' with the reply id, * topic id and user id * @return bool True on success, false on failure */ function bbp_notify_topic_subscribers($reply_id = 0, $topic_id = 0, $forum_id = 0, $anonymous_data = false, $reply_author = 0) { // Bail if subscriptions are turned off if (!bbp_is_subscriptions_active()) { return false; } /** Validation ************************************************************/ $reply_id = bbp_get_reply_id($reply_id); $topic_id = bbp_get_topic_id($topic_id); $forum_id = bbp_get_forum_id($forum_id); /** Topic *****************************************************************/ // Bail if topic is not published if (!bbp_is_topic_published($topic_id)) { return false; } /** Reply *****************************************************************/ // Bail if reply is not published if (!bbp_is_reply_published($reply_id)) { return false; } // Poster name $reply_author_name = bbp_get_reply_author_display_name($reply_id); /** Mail ******************************************************************/ // Remove filters from reply content and topic title to prevent content // from being encoded with HTML entities, wrapped in paragraph tags, etc... remove_all_filters('bbp_get_reply_content'); remove_all_filters('bbp_get_topic_title'); // Strip tags from text and setup mail data $topic_title = strip_tags(bbp_get_topic_title($topic_id)); $reply_content = strip_tags(bbp_get_reply_content($reply_id)); $reply_url = bbp_get_reply_url($reply_id); $blog_name = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES); // For plugins to filter messages per reply/topic/user $message = sprintf(__('%1$s wrote: %2$s Post Link: %3$s ----------- You are receiving this email because you subscribed to a forum topic. Login and visit the topic to unsubscribe from these emails.', 'bbpress'), $reply_author_name, $reply_content, $reply_url); $message = apply_filters('bbp_subscription_mail_message', $message, $reply_id, $topic_id); if (empty($message)) { return; } // For plugins to filter titles per reply/topic/user $subject = apply_filters('bbp_subscription_mail_title', '[' . $blog_name . '] ' . $topic_title, $reply_id, $topic_id); if (empty($subject)) { return; } /** Users *****************************************************************/ // Get the noreply@ address $no_reply = bbp_get_do_not_reply_address(); // Setup "From" email address $from_email = apply_filters('bbp_subscription_from_email', $no_reply); // Setup the From header $headers = array('From: ' . get_bloginfo('name') . ' <' . $from_email . '>'); // Get topic subscribers and bail if empty $user_ids = bbp_get_topic_subscribers($topic_id, true); // Dedicated filter to manipulate user ID's to send emails to $user_ids = apply_filters('bbp_topic_subscription_user_ids', $user_ids); if (empty($user_ids)) { return false; } // Loop through users foreach ((array) $user_ids as $user_id) { // Don't send notifications to the person who made the post if (!empty($reply_author) && (int) $user_id === (int) $reply_author) { continue; } // Get email address of subscribed user $headers[] = 'Bcc: ' . get_userdata($user_id)->user_email; } /** Send it ***************************************************************/ // Custom headers $headers = apply_filters('bbp_subscription_mail_headers', $headers); $to_email = apply_filters('bbp_subscription_to_email', $no_reply); do_action('bbp_pre_notify_subscribers', $reply_id, $topic_id, $user_ids); // Send notification email wp_mail($to_email, $subject, $message, $headers); do_action('bbp_post_notify_subscribers', $reply_id, $topic_id, $user_ids); return true; }
/** * Sends the new reply notification email to moderators on private replies * * @since 1.2 * * @param $message string The email message * @param $reply_id int The ID of the reply * @param $topic_id int The ID of the reply's topic * * @return void */ public function subscription_email($message, $reply_id, $topic_id) { if (!$this->is_private($reply_id)) { return false; // reply isn't private so do nothing } $topic_author = bbp_get_topic_author_id($topic_id); $reply_author = bbp_get_reply_author_id($reply_id); $reply_author_name = bbp_get_reply_author_display_name($reply_id); // Strip tags from text and setup mail data $topic_title = strip_tags(bbp_get_topic_title($topic_id)); $reply_content = strip_tags(bbp_get_reply_content($reply_id)); $reply_url = bbp_get_reply_url($reply_id); $blog_name = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES); $do_not_reply = '<noreply@' . ltrim(get_home_url(), '^(http|https)://') . '>'; $subject = apply_filters('bbp_subscription_mail_title', '[' . $blog_name . '] ' . $topic_title, $reply_id, $topic_id); // Array to hold BCC's $headers = array(); // Setup the From header $headers[] = 'From: ' . get_bloginfo('name') . ' ' . $do_not_reply; // Get topic subscribers and bail if empty $user_ids = bbp_get_topic_subscribers($topic_id, true); if (empty($user_ids)) { return false; } // Loop through users foreach ((array) $user_ids as $user_id) { // Don't send notifications to the person who made the post if (!empty($reply_author) && (int) $user_id === (int) $reply_author) { continue; } if (user_can($user_id, 'moderate') || (int) $topic_author === (int) $user_id) { // Get email address of subscribed user $headers[] = 'Bcc: ' . get_userdata($user_id)->user_email; } } wp_mail($do_not_reply, $subject, $message, $headers); }
/** * Sends notification emails for new posts * * Gets new post's ID and check if there are subscribed users to that topic, and * if there are, send notifications * * @since bbPress (r2668) * * @param int $reply_id ID of the newly made reply * @uses bbp_is_subscriptions_active() To check if the subscriptions are active * @uses bbp_get_reply_id() To validate the reply ID * @uses bbp_get_reply() To get the reply * @uses bbp_get_reply_topic_id() To get the topic ID of the reply * @uses bbp_is_reply_published() To make sure the reply is published * @uses bbp_get_topic_id() To validate the topic ID * @uses bbp_get_topic() To get the reply's topic * @uses bbp_is_topic_published() To make sure the topic is published * @uses get_the_author_meta() To get the author's display name * @uses do_action() Calls 'bbp_pre_notify_subscribers' with the reply id and * topic id * @uses bbp_get_topic_subscribers() To get the topic subscribers * @uses apply_filters() Calls 'bbp_subscription_mail_message' with the * message, reply id, topic id and user id * @uses get_userdata() To get the user data * @uses wp_mail() To send the mail * @uses do_action() Calls 'bbp_post_notify_subscribers' with the reply id * and topic id * @return bool True on success, false on failure */ function bbp_notify_subscribers($reply_id = 0, $topic_id = 0, $forum_id = 0, $anonymous_data = false, $reply_author = 0) { // Bail if subscriptions are turned off if (!bbp_is_subscriptions_active()) { return false; } /** Validation ************************************************************/ $reply_id = bbp_get_reply_id($reply_id); $topic_id = bbp_get_topic_id($topic_id); $forum_id = bbp_get_forum_id($forum_id); /** Reply *****************************************************************/ // Bail if reply is not published if (!bbp_is_reply_published($reply_id)) { return false; } /** Topic *****************************************************************/ // Bail if topic is not published if (!bbp_is_topic_published($topic_id)) { return false; } /** User ******************************************************************/ // Get subscribers and bail if empty $user_ids = bbp_get_topic_subscribers($topic_id, true); if (empty($user_ids)) { return false; } // Poster name $reply_author_name = bbp_get_reply_author_display_name($reply_id); /** Mail ******************************************************************/ do_action('bbp_pre_notify_subscribers', $reply_id, $topic_id, $user_ids); // Remove filters from reply content and topic title to prevent content // from being encoded with HTML entities, wrapped in paragraph tags, etc... remove_all_filters('bbp_get_reply_content'); remove_all_filters('bbp_get_topic_title'); // Strip tags from text $topic_title = strip_tags(bbp_get_topic_title($topic_id)); $reply_content = strip_tags(bbp_get_reply_content($reply_id)); $reply_url = bbp_get_reply_url($reply_id); $blog_name = get_option('blogname'); // Loop through users foreach ((array) $user_ids as $user_id) { // Don't send notifications to the person who made the post if (!empty($reply_author) && (int) $user_id == (int) $reply_author) { continue; } // For plugins to filter messages per reply/topic/user $message = sprintf(__('%1$s wrote: %2$s Post Link: %3$s ----------- You are receiving this email because you subscribed to a forum topic. Login and visit the topic to unsubscribe from these emails.', 'bbpress'), $reply_author_name, $reply_content, $reply_url); $message = apply_filters('bbp_subscription_mail_message', $message, $reply_id, $topic_id, $user_id); if (empty($message)) { continue; } // For plugins to filter titles per reply/topic/user $subject = apply_filters('bbp_subscription_mail_title', '[' . $blog_name . '] ' . $topic_title, $reply_id, $topic_id, $user_id); if (empty($subject)) { continue; } // Custom headers $headers = apply_filters('bbp_subscription_mail_headers', array()); // Get user data of this user $user = get_userdata($user_id); // Send notification email wp_mail($user->user_email, $subject, $message, $headers); } do_action('bbp_post_notify_subscribers', $reply_id, $topic_id, $user_ids); return true; }