bbp_notify_forum_subscribers( int $topic_id, int $forum_id, array $anonymous_data = array(), int $topic_author )

Sends notification emails for new topics to subscribed forums


Description Description

Gets new post 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 email script.


Parameters Parameters

$topic_id

(Required) ID of the newly made reply

$forum_id

(Required) ID of the forum for the topic

$anonymous_data

(Optional) - if it's an anonymous post. Do not supply if supplying $author_id. Should be sanitized (see bbp_filter_anonymous_post_data()

Default value: array()

$topic_author

(Optional) ID of the topic author ID


Top ↑

Return Return

(bool) True on success, false on failure


Top ↑

Source Source

File: includes/common/functions.php

function bbp_notify_forum_subscribers( $topic_id = 0, $forum_id = 0, $anonymous_data = array(), $topic_author = 0 ) {

	// Bail if subscriptions are turned off
	if ( ! bbp_is_subscriptions_active() ) {
		return false;
	}

	// Bail if importing
	if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
		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 public (includes closed)
	if ( ! bbp_is_topic_public( $topic_id ) ) {
		return false;
	}

	// Poster name
	$topic_author_name = bbp_get_topic_author_display_name( $topic_id );

	/** Users *****************************************************************/

	// Get topic subscribers and bail if empty
	$user_ids = bbp_get_subscribers( $forum_id );

	// Remove the topic author from the list.
	$topic_author_key = array_search( (int) $topic_author, $user_ids, true );
	if ( false !== $topic_author_key ) {
		unset( $user_ids[ $topic_author_key ] );
	}

	// Dedicated filter to manipulate user ID's to send emails to
	$user_ids = (array) apply_filters( 'bbp_forum_subscription_user_ids', $user_ids, $topic_id, $forum_id );

	// Bail of the reply author was the only one subscribed.
	if ( empty( $user_ids ) ) {
		return false;
	}

	// Get email addresses, bail if empty
	$email_addresses = bbp_get_email_addresses_from_user_ids( $user_ids );
	if ( empty( $email_addresses ) ) {
		return false;
	}

	/** Mail ******************************************************************/

	// Remove filters from reply content and topic title to prevent content
	// from being encoded with HTML entities, wrapped in paragraph tags, etc...
	bbp_remove_all_filters( 'bbp_get_topic_content' );
	bbp_remove_all_filters( 'bbp_get_topic_title'   );
	bbp_remove_all_filters( 'the_title'             );

	// Strip tags from text and setup mail data
	$blog_name         = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
	$topic_title       = wp_specialchars_decode( strip_tags( bbp_get_topic_title( $topic_id ) ), ENT_QUOTES );
	$topic_author_name = wp_specialchars_decode( strip_tags( $topic_author_name ), ENT_QUOTES );
	$topic_content     = wp_specialchars_decode( strip_tags( bbp_get_topic_content( $topic_id ) ), ENT_QUOTES );
	$topic_url         = get_permalink( $topic_id );

	// For plugins to filter messages per reply/topic/user
	$message = sprintf( esc_html__( '%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;
	}

	/** Headers ***************************************************************/

	// Default bbPress X-header
	$headers    = array( bbp_get_email_header() );

	// 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[] = 'From: ' . get_bloginfo( 'name' ) . ' <' . $from_email . '>';

	// Loop through addresses
	foreach ( (array) $email_addresses as $address ) {
		$headers[] = 'Bcc: ' . $address;
	}

	/** Send it ***************************************************************/

	// Custom headers
	$headers  = apply_filters( 'bbp_subscription_mail_headers', $headers  );
	$to_email = apply_filters( 'bbp_subscription_to_email',     $no_reply );

	// Before
	do_action( 'bbp_pre_notify_forum_subscribers', $topic_id, $forum_id, $user_ids );

	// Send notification email
	wp_mail( $to_email, $subject, $message, $headers );

	// After
	do_action( 'bbp_post_notify_forum_subscribers', $topic_id, $forum_id, $user_ids );

	// Restore previously removed filters
	bbp_restore_all_filters( 'bbp_get_topic_content' );
	bbp_restore_all_filters( 'bbp_get_topic_title'   );
	bbp_restore_all_filters( 'the_title'             );

	return true;
}

Top ↑

Changelog Changelog

Changelog
Version Description
2.5.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.