bbp_notify_topic_subscribers

Sends notification emails for new replies to subscribed topics.

Description

bbp_notify_topic_subscribers( (int) $reply_id = 0, (int) $topic_id = 0, (int) $forum_id = 0, (constant) $anonymous_data = false, (int) $reply_author = 0 ); 

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.

Parameters (5)

0. $reply_id — Optional. (int)
ID of the newly made reply
1. $topic_id — Optional. (int)
ID of the topic of the reply
2. $forum_id — Optional. (int)
ID of the forum of the reply
3. $anonymous_data — Optional. (constant) => false
Array of anonymous user data
4. $reply_author — Optional. (int)
ID of the topic author ID

Usage

  1. if ( !function_exists( 'bbp_notify_topic_subscribers' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'bbpress/includes/common/functions.php'; 
  3.  
  4. // ID of the newly made reply 
  5. $reply_id = -1; 
  6.  
  7. // ID of the topic of the reply 
  8. $topic_id = -1; 
  9.  
  10. // ID of the forum of the reply 
  11. $forum_id = -1; 
  12.  
  13. // Array of anonymous user data 
  14. $anonymous_data = false; 
  15.  
  16. // ID of the topic author ID 
  17. $reply_author = -1; 
  18.  
  19. // NOTICE! Understand what this does before running. 
  20. $result = bbp_notify_topic_subscribers($reply_id, $topic_id, $forum_id, $anonymous_data, $reply_author); 
  21.  

Defined (1)

The function is defined in the following location(s).

/includes/common/functions.php  
  1. function bbp_notify_topic_subscribers( $reply_id = 0, $topic_id = 0, $forum_id = 0, $anonymous_data = false, $reply_author = 0 ) { 
  2.  
  3. // Bail if subscriptions are turned off 
  4. if ( !bbp_is_subscriptions_active() ) { 
  5. return false; 
  6.  
  7. /** Validation ************************************************************/ 
  8.  
  9. $reply_id = bbp_get_reply_id( $reply_id ); 
  10. $topic_id = bbp_get_topic_id( $topic_id ); 
  11. $forum_id = bbp_get_forum_id( $forum_id ); 
  12.  
  13. /** Topic *****************************************************************/ 
  14.  
  15. // Bail if topic is not published 
  16. if ( !bbp_is_topic_published( $topic_id ) ) { 
  17. return false; 
  18.  
  19. /** Reply *****************************************************************/ 
  20.  
  21. // Bail if reply is not published 
  22. if ( !bbp_is_reply_published( $reply_id ) ) { 
  23. return false; 
  24.  
  25. // Poster name 
  26. $reply_author_name = bbp_get_reply_author_display_name( $reply_id ); 
  27.  
  28. /** Mail ******************************************************************/ 
  29.  
  30. // Remove filters from reply content and topic title to prevent content 
  31. // from being encoded with HTML entities, wrapped in paragraph tags, etc... 
  32. remove_all_filters( 'bbp_get_reply_content' ); 
  33. remove_all_filters( 'bbp_get_topic_title' ); 
  34.  
  35. // Strip tags from text and setup mail data 
  36. $topic_title = strip_tags( bbp_get_topic_title( $topic_id ) ); 
  37. $reply_content = strip_tags( bbp_get_reply_content( $reply_id ) ); 
  38. $reply_url = bbp_get_reply_url( $reply_id ); 
  39. $blog_name = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); 
  40.  
  41. // For plugins to filter messages per reply/topic/user 
  42. $message = sprintf( __( '%1$s wrote: 
  43.  
  44. %2$s 
  45.  
  46. Post Link: %3$s 
  47.  
  48. ----------- 
  49.  
  50. You are receiving this email because you subscribed to a forum topic. 
  51.  
  52. Login and visit the topic to unsubscribe from these emails.', bbpress ),  
  53.  
  54. $reply_author_name,  
  55. $reply_content,  
  56. $reply_url 
  57. ); 
  58.  
  59. $message = apply_filters( 'bbp_subscription_mail_message', $message, $reply_id, $topic_id ); 
  60. if ( empty( $message ) ) { 
  61. return; 
  62.  
  63. // For plugins to filter titles per reply/topic/user 
  64. $subject = apply_filters( 'bbp_subscription_mail_title', '[' . $blog_name . '] ' . $topic_title, $reply_id, $topic_id ); 
  65. if ( empty( $subject ) ) { 
  66. return; 
  67.  
  68. /** Users *****************************************************************/ 
  69.  
  70. // Get the noreply@ address 
  71.  
  72. // Setup "From" email address 
  73. $from_email = apply_filters( 'bbp_subscription_from_email', $no_reply ); 
  74.  
  75. // Setup the From header 
  76. $headers = array( 'From: ' . get_bloginfo( 'name' ) . ' <' . $from_email . '>' ); 
  77.  
  78. // Get topic subscribers and bail if empty 
  79. $user_ids = bbp_get_topic_subscribers( $topic_id, true ); 
  80.  
  81. // Dedicated filter to manipulate user ID's to send emails to 
  82. $user_ids = apply_filters( 'bbp_topic_subscription_user_ids', $user_ids ); 
  83. if ( empty( $user_ids ) ) { 
  84. return false; 
  85.  
  86. // Loop through users 
  87. foreach ( (array) $user_ids as $user_id ) { 
  88.  
  89. // Don't send notifications to the person who made the post 
  90. if ( !empty( $reply_author ) && (int) $user_id === (int) $reply_author ) { 
  91. continue; 
  92.  
  93. // Get email address of subscribed user 
  94. $headers[] = 'Bcc: ' . get_userdata( $user_id )->user_email; 
  95.  
  96. /** Send it ***************************************************************/ 
  97.  
  98. // Custom headers 
  99. $headers = apply_filters( 'bbp_subscription_mail_headers', $headers ); 
  100. $to_email = apply_filters( 'bbp_subscription_to_email', $no_reply ); 
  101.  
  102. do_action( 'bbp_pre_notify_subscribers', $reply_id, $topic_id, $user_ids ); 
  103.  
  104. // Send notification email 
  105. wp_mail( $to_email, $subject, $message, $headers ); 
  106.  
  107. do_action( 'bbp_post_notify_subscribers', $reply_id, $topic_id, $user_ids ); 
  108.  
  109. return true;