/** * @covers ::bbp_get_forum_subscribers */ public function test_bbp_get_forum_subscribers() { $u = $this->factory->user->create_many(3); $f = $this->factory->forum->create_many(2); // Add forum subscriptions. bbp_add_user_forum_subscription($u[0], $f[0]); bbp_add_user_forum_subscription($u[1], $f[0]); bbp_add_user_forum_subscription($u[2], $f[0]); $subscribers = bbp_get_forum_subscribers($f[0]); $this->assertEquals(array($u[0], $u[1], $u[2]), $subscribers); // Add forum subscriptions. bbp_add_user_forum_subscription($u[0], $f[1]); bbp_add_user_forum_subscription($u[2], $f[1]); $subscribers = bbp_get_forum_subscribers($f[1]); $this->assertEquals(array($u[0], $u[2]), $subscribers); }
/** * Remove a deleted forum from all users' subscriptions * * @since bbPress (r5156) * * @param int $forum_id Get the forum ID to remove * @uses bbp_is_subscriptions_active() To check if the subscriptions are active * @uses bbp_get_forum_id To get the forum id * @uses bbp_get_forum_subscribers() To get the forum subscribers * @uses bbp_remove_user_subscription() To remove the user subscription */ function bbp_remove_forum_from_all_subscriptions($forum_id = 0) { // Subscriptions are not active if (!bbp_is_subscriptions_active()) { return; } $forum_id = bbp_get_forum_id($forum_id); // Bail if no forum if (empty($forum_id)) { return; } // Get users $users = (array) bbp_get_forum_subscribers($forum_id); // Users exist if (!empty($users)) { // Loop through users foreach ($users as $user) { // Remove each user bbp_remove_user_subscription($user, $forum_id); } } }
/** * Sends notification emails for new topics to subscribed forums * * Gets new post's ID and check if there are subscribed users to that forum, 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.5.0 bbPress (r5156) * * @param int $topic_id ID of the newly made reply * @param int $forum_id ID of the forum for the topic * @param mixed $anonymous_data Array of anonymous user data * @param int $topic_author ID of the topic author ID * * @uses bbp_is_subscriptions_active() To check if the subscriptions are active * @uses bbp_get_topic_id() To validate the topic ID * @uses bbp_get_forum_id() To validate the forum ID * @uses bbp_is_topic_published() To make sure the topic is published * @uses bbp_get_forum_subscribers() To get the forum subscribers * @uses bbp_get_topic_author_display_name() To get the topic author's display name * @uses do_action() Calls 'bbp_pre_notify_forum_subscribers' with the topic id, * forum id and user id * @uses apply_filters() Calls 'bbp_forum_subscription_mail_message' with the * message, topic id, forum id and user id * @uses apply_filters() Calls 'bbp_forum_subscription_mail_title' with the * topic title, topic id, forum id and user id * @uses apply_filters() Calls 'bbp_forum_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_forum_subscribers' with the topic, * id, forum id and user id * @return bool True on success, false on failure */ function bbp_notify_forum_subscribers($topic_id = 0, $forum_id = 0, $anonymous_data = false, $topic_author = 0) { // Bail if subscriptions are turned off if (!bbp_is_subscriptions_active()) { return false; } /** Validation ************************************************************/ $topic_id = bbp_get_topic_id($topic_id); $forum_id = bbp_get_forum_id($forum_id); /** * Necessary for backwards compatibility * * @see https://bbpress.trac.wordpress.org/ticket/2620 */ $user_id = 0; /** Topic *****************************************************************/ // Bail if topic is not published if (!bbp_is_topic_published($topic_id)) { return false; } // Poster name $topic_author_name = bbp_get_topic_author_display_name($topic_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_topic_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)); $topic_content = strip_tags(bbp_get_topic_content($topic_id)); $topic_url = get_permalink($topic_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 Topic Link: %3$s ----------- You are receiving this email because you subscribed to a forum. Login and visit the topic to unsubscribe from these emails.', 'bbpress'), $topic_author_name, $topic_content, $topic_url); $message = apply_filters('bbp_forum_subscription_mail_message', $message, $topic_id, $forum_id, $user_id); if (empty($message)) { return; } // For plugins to filter titles per reply/topic/user $subject = apply_filters('bbp_forum_subscription_mail_title', '[' . $blog_name . '] ' . $topic_title, $topic_id, $forum_id, $user_id); if (empty($subject)) { return; } /** User ******************************************************************/ // 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_forum_subscribers($forum_id, true); // Dedicated filter to manipulate user ID's to send emails to $user_ids = apply_filters('bbp_forum_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($topic_author) && (int) $user_id === (int) $topic_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_forum_subscribers', $topic_id, $forum_id, $user_ids); // Send notification email wp_mail($to_email, $subject, $message, $headers); do_action('bbp_post_notify_forum_subscribers', $topic_id, $forum_id, $user_ids); return true; }
/** * Sends notification emails for new topics to subscribed forums * * Gets new post's ID and check if there are subscribed users to that topic, and * if there are, send notifications * * @since bbPress (r5156) * * @param int $topic_id ID of the newly made reply * @uses bbp_is_subscriptions_active() To check if the subscriptions are active * @uses bbp_get_topic_id() To validate the topic ID * @uses bbp_get_forum_id() To validate the forum ID * @uses bbp_is_topic_published() To make sure the topic is published * @uses bbp_get_forum_subscribers() To get the forum subscribers * @uses bbp_get_topic_author_display_name() To get the topic author's display name * @uses do_action() Calls 'bbp_pre_notify_forum_subscribers' with the topic id, * forum id and user id * @uses apply_filters() Calls 'bbp_forum_subscription_mail_message' with the * message, topic id, forum id and user id * @uses apply_filters() Calls 'bbp_forum_subscription_mail_title' with the * topic title, topic id, forum id and user id * @uses apply_filters() Calls 'bbp_forum_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_forum_subscribers' with the topic, * id, forum id and user id * @return bool True on success, false on failure */ function bbp_notify_forum_subscribers($topic_id = 0, $forum_id = 0, $anonymous_data = false, $topic_author = 0) { // Bail if subscriptions are turned off if (!bbp_is_subscriptions_active()) { return false; } /** Validation ************************************************************/ $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; } /** User ******************************************************************/ // Get forum subscribers and bail if empty $user_ids = bbp_get_forum_subscribers($forum_id, true); if (empty($user_ids)) { return false; } // Poster name $topic_author_name = bbp_get_topic_author_display_name($topic_id); /** Mail ******************************************************************/ do_action('bbp_pre_notify_forum_subscribers', $topic_id, $forum_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_topic_content'); remove_all_filters('bbp_get_topic_title'); // Strip tags from text $topic_title = strip_tags(bbp_get_topic_title($topic_id)); $topic_content = strip_tags(bbp_get_topic_content($topic_id)); $topic_url = get_permalink($topic_id); $blog_name = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES); // Loop through users foreach ((array) $user_ids as $user_id) { // Don't send notifications to the person who made the post if (!empty($topic_author) && (int) $user_id === (int) $topic_author) { continue; } // For plugins to filter messages per reply/topic/user $message = sprintf(__('%1$s wrote: %2$s Topic Link: %3$s ----------- You are receiving this email because you subscribed to a forum. Login and visit the topic to unsubscribe from these emails.', 'bbpress'), $topic_author_name, $topic_content, $topic_url); $message = apply_filters('bbp_forum_subscription_mail_message', $message, $topic_id, $forum_id, $user_id); if (empty($message)) { continue; } // For plugins to filter titles per reply/topic/user $subject = apply_filters('bbp_forum_subscription_mail_title', '[' . $blog_name . '] ' . $topic_title, $topic_id, $forum_id, $user_id); if (empty($subject)) { continue; } // Custom headers $headers = apply_filters('bbp_forum_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_forum_subscribers', $topic_id, $forum_id, $user_ids); return true; }