Skip to:
Content

bbPress.org

Changeset 5260


Ignore:
Timestamp:
01/16/2014 08:39:12 PM (13 years ago)
Author:
johnjamesjacoby
Message:

Refactor subscription notifications functions to send 1 email with all subscribers BCC'd rather than 1 email per subscriber. This speeds up posting significantly in forums or topics where there are any number of subscribers, particularly on configurations with slow email servers or when using external email services.

(Subscription notifications could be improved further in a future iteration by moving the actual sending into a wp_cron process, but this improvement is easy and significant enough where it will be sufficient for the widest audience.)

Hat tip netweb, rossagrant. Fixes #2162. (trunk)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/includes/common/functions.php

    r5239 r5260  
    988988 * if there are, send notifications
    989989 *
     990 * Note: in bbPress 2.6, we've moved away from 1 email per subscriber to 1 email
     991 * with everyone BCC'd. This may have negative repercussions for email services
     992 * that limit the number of addresses in a BCC field (often to around 500.) In
     993 * those cases, we recommend unhooking this function and creating your own
     994 * custom emailer script.
     995 *
    990996 * @since bbPress (r2668)
    991997 *
     
    10181024
    10191025        // Bail if subscriptions are turned off
    1020         if ( !bbp_is_subscriptions_active() )
     1026        if ( !bbp_is_subscriptions_active() ) {
    10211027                return false;
     1028        }
    10221029
    10231030        /** Validation ************************************************************/
     
    10271034        $forum_id = bbp_get_forum_id( $forum_id );
    10281035
     1036        /** Topic *****************************************************************/
     1037
     1038        // Bail if topic is not published
     1039        if ( !bbp_is_topic_published( $topic_id ) ) {
     1040                return false;
     1041        }
     1042
    10291043        /** Reply *****************************************************************/
    10301044
    10311045        // Bail if reply is not published
    1032         if ( !bbp_is_reply_published( $reply_id ) )
     1046        if ( !bbp_is_reply_published( $reply_id ) ) {
    10331047                return false;
    1034 
    1035         /** Topic *****************************************************************/
    1036 
    1037         // Bail if topic is not published
    1038         if ( !bbp_is_topic_published( $topic_id ) )
    1039                 return false;
    1040 
    1041         /** User ******************************************************************/
    1042 
    1043         // Get topic subscribers and bail if empty
    1044         $user_ids = bbp_get_topic_subscribers( $topic_id );
    1045         if ( empty( $user_ids ) )
    1046                 return false;
     1048        }
    10471049
    10481050        // Poster name
     
    10501052
    10511053        /** Mail ******************************************************************/
    1052 
    1053         do_action( 'bbp_pre_notify_subscribers', $reply_id, $topic_id, $user_ids );
    10541054
    10551055        // Remove filters from reply content and topic title to prevent content
     
    10581058        remove_all_filters( 'bbp_get_topic_title'   );
    10591059
    1060         // Strip tags from text
     1060        // Strip tags from text and setup mail data
    10611061        $topic_title   = strip_tags( bbp_get_topic_title( $topic_id ) );
    10621062        $reply_content = strip_tags( bbp_get_reply_content( $reply_id ) );
    10631063        $reply_url     = bbp_get_reply_url( $reply_id );
    10641064        $blog_name     = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
     1065        $do_not_reply  = '<noreply@' . ltrim( get_home_url(), '^(http|https)://' ) . '>';
     1066
     1067        // For plugins to filter messages per reply/topic/user
     1068        $message = sprintf( __( '%1$s wrote:
     1069
     1070%2$s
     1071
     1072Post Link: %3$s
     1073
     1074-----------
     1075
     1076You are receiving this email because you subscribed to a forum topic.
     1077
     1078Login and visit the topic to unsubscribe from these emails.', 'bbpress' ),
     1079
     1080                $reply_author_name,
     1081                $reply_content,
     1082                $reply_url
     1083        );
     1084
     1085        $message = apply_filters( 'bbp_subscription_mail_message', $message, $reply_id, $topic_id );
     1086        if ( empty( $message ) ) {
     1087                return;
     1088        }
     1089
     1090        // For plugins to filter titles per reply/topic/user
     1091        $subject = apply_filters( 'bbp_subscription_mail_title', '[' . $blog_name . '] ' . $topic_title, $reply_id, $topic_id );
     1092        if ( empty( $subject ) ) {
     1093                return;
     1094        }
     1095
     1096        /** Users *****************************************************************/
     1097
     1098        // Array to hold BCC's
     1099        $headers = array();
     1100
     1101        // Setup the From header
     1102        $headers[] = 'From: ' . get_bloginfo( 'name' ) . ' ' . $do_not_reply;
     1103
     1104        // Get topic subscribers and bail if empty
     1105        $user_ids = bbp_get_topic_subscribers( $topic_id, true );
     1106        if ( empty( $user_ids ) ) {
     1107                return false;
     1108        }
    10651109
    10661110        // Loop through users
     
    10681112
    10691113                // Don't send notifications to the person who made the post
    1070                 if ( !empty( $reply_author ) && (int) $user_id === (int) $reply_author )
     1114                if ( !empty( $reply_author ) && (int) $user_id === (int) $reply_author ) {
    10711115                        continue;
    1072 
    1073                 // For plugins to filter messages per reply/topic/user
    1074                 $message = sprintf( __( '%1$s wrote:
    1075 
    1076 %2$s
    1077 
    1078 Post Link: %3$s
    1079 
    1080 -----------
    1081 
    1082 You are receiving this email because you subscribed to a forum topic.
    1083 
    1084 Login and visit the topic to unsubscribe from these emails.', 'bbpress' ),
    1085 
    1086                         $reply_author_name,
    1087                         $reply_content,
    1088                         $reply_url
    1089                 );
    1090 
    1091                 $message = apply_filters( 'bbp_subscription_mail_message', $message, $reply_id, $topic_id, $user_id );
    1092                 if ( empty( $message ) )
    1093                         continue;
    1094 
    1095                 // For plugins to filter titles per reply/topic/user
    1096                 $subject = apply_filters( 'bbp_subscription_mail_title', '[' . $blog_name . '] ' . $topic_title, $reply_id, $topic_id, $user_id );
    1097                 if ( empty( $subject ) )
    1098                         continue;
    1099 
    1100                 // Custom headers
    1101                 $headers = apply_filters( 'bbp_subscription_mail_headers', array() );
    1102 
    1103                 // Get user data of this user
    1104                 $user = get_userdata( $user_id );
    1105 
    1106                 // Send notification email
    1107                 wp_mail( $user->user_email, $subject, $message, $headers );
    1108         }
     1116                }
     1117
     1118                // Get email address of subscribed user
     1119                $headers[] = 'Bcc: ' . get_userdata( $user_id )->user_email;
     1120        }
     1121
     1122        /** Send it ***************************************************************/
     1123
     1124        // Custom headers
     1125        $headers = apply_filters( 'bbp_subscription_mail_headers', $headers );
     1126
     1127        do_action( 'bbp_pre_notify_subscribers', $reply_id, $topic_id, $user_ids );
     1128
     1129        // Send notification email
     1130        wp_mail( $do_not_reply, $subject, $message, $headers );
    11091131
    11101132        do_action( 'bbp_post_notify_subscribers', $reply_id, $topic_id, $user_ids );
     
    11161138 * Sends notification emails for new topics to subscribed forums
    11171139 *
    1118  * Gets new post's ID and check if there are subscribed users to that topic, and
     1140 * Gets new post's ID and check if there are subscribed users to that forum, and
    11191141 * if there are, send notifications
     1142 *
     1143 * Note: in bbPress 2.6, we've moved away from 1 email per subscriber to 1 email
     1144 * with everyone BCC'd. This may have negative repercussions for email services
     1145 * that limit the number of addresses in a BCC field (often to around 500.) In
     1146 * those cases, we recommend unhooking this function and creating your own
     1147 * custom emailer script.
    11201148 *
    11211149 * @since bbPress (r5156)
     
    11441172
    11451173        // Bail if subscriptions are turned off
    1146         if ( !bbp_is_subscriptions_active() )
     1174        if ( !bbp_is_subscriptions_active() ) {
    11471175                return false;
     1176        }
    11481177
    11491178        /** Validation ************************************************************/
     
    11551184
    11561185        // Bail if topic is not published
    1157         if ( ! bbp_is_topic_published( $topic_id ) )
     1186        if ( ! bbp_is_topic_published( $topic_id ) ) {
    11581187                return false;
    1159 
    1160         /** User ******************************************************************/
    1161 
    1162         // Get forum subscribers and bail if empty
    1163         $user_ids = bbp_get_forum_subscribers( $forum_id );
    1164         if ( empty( $user_ids ) )
    1165                 return false;
     1188        }
    11661189
    11671190        // Poster name
     
    11691192
    11701193        /** Mail ******************************************************************/
    1171 
    1172         do_action( 'bbp_pre_notify_forum_subscribers', $topic_id, $forum_id, $user_ids );
    11731194
    11741195        // Remove filters from reply content and topic title to prevent content
     
    11771198        remove_all_filters( 'bbp_get_topic_title'   );
    11781199
    1179         // Strip tags from text
     1200        // Strip tags from text and setup mail data
    11801201        $topic_title   = strip_tags( bbp_get_topic_title( $topic_id ) );
    11811202        $topic_content = strip_tags( bbp_get_topic_content( $topic_id ) );
    11821203        $topic_url     = get_permalink( $topic_id );
    11831204        $blog_name     = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
     1205        $do_not_reply  = '<noreply@' . ltrim( get_home_url(), '^(http|https)://' ) . '>';
     1206
     1207        // For plugins to filter messages per reply/topic/user
     1208        $message = sprintf( __( '%1$s wrote:
     1209
     1210%2$s
     1211
     1212Topic Link: %3$s
     1213
     1214-----------
     1215
     1216You are receiving this email because you subscribed to a forum.
     1217
     1218Login and visit the topic to unsubscribe from these emails.', 'bbpress' ),
     1219
     1220                $topic_author_name,
     1221                $topic_content,
     1222                $topic_url
     1223        );
     1224
     1225        $message = apply_filters( 'bbp_forum_subscription_mail_message', $message, $topic_id, $forum_id, $user_id );
     1226        if ( empty( $message ) ) {
     1227                return;
     1228        }
     1229
     1230        // For plugins to filter titles per reply/topic/user
     1231        $subject = apply_filters( 'bbp_forum_subscription_mail_title', '[' . $blog_name . '] ' . $topic_title, $topic_id, $forum_id, $user_id );
     1232        if ( empty( $subject ) ) {
     1233                return;
     1234        }
     1235
     1236        /** User ******************************************************************/
     1237
     1238        // Array to hold BCC's
     1239        $headers = array();
     1240
     1241        // Setup the From header
     1242        $headers[] = 'From: ' . get_bloginfo( 'name' ) . ' ' . $do_not_reply;
     1243
     1244        // Get topic subscribers and bail if empty
     1245        $user_ids = bbp_get_forum_subscribers( $forum_id, true );
     1246        if ( empty( $user_ids ) ) {
     1247                return false;
     1248        }
    11841249
    11851250        // Loop through users
     
    11871252
    11881253                // Don't send notifications to the person who made the post
    1189                 if ( !empty( $topic_author ) && (int) $user_id === (int) $topic_author )
     1254                if ( !empty( $topic_author ) && (int) $user_id === (int) $topic_author ) {
    11901255                        continue;
    1191 
    1192                 // For plugins to filter messages per reply/topic/user
    1193                 $message = sprintf( __( '%1$s wrote:
    1194 
    1195 %2$s
    1196 
    1197 Topic Link: %3$s
    1198 
    1199 -----------
    1200 
    1201 You are receiving this email because you subscribed to a forum.
    1202 
    1203 Login and visit the topic to unsubscribe from these emails.', 'bbpress' ),
    1204 
    1205                         $topic_author_name,
    1206                         $topic_content,
    1207                         $topic_url
    1208                 );
    1209                 $message = apply_filters( 'bbp_forum_subscription_mail_message', $message, $topic_id, $forum_id, $user_id );
    1210                 if ( empty( $message ) )
    1211                         continue;
    1212 
    1213                 // For plugins to filter titles per reply/topic/user
    1214                 $subject = apply_filters( 'bbp_forum_subscription_mail_title', '[' . $blog_name . '] ' . $topic_title, $topic_id, $forum_id, $user_id );
    1215                 if ( empty( $subject ) )
    1216                         continue;
    1217 
    1218                 // Custom headers
    1219                 $headers = apply_filters( 'bbp_forum_subscription_mail_headers', array() );
    1220 
    1221                 // Get user data of this user
    1222                 $user = get_userdata( $user_id );
    1223 
    1224                 // Send notification email
    1225                 wp_mail( $user->user_email, $subject, $message, $headers );
    1226         }
     1256                }
     1257
     1258                // Get email address of subscribed user
     1259                $headers[] = 'Bcc: ' . get_userdata( $user_id )->user_email;
     1260        }
     1261
     1262        /** Send it ***************************************************************/
     1263
     1264        // Custom headers
     1265        $headers = apply_filters( 'bbp_subscription_mail_headers', $headers );
     1266
     1267        do_action( 'bbp_pre_notify_forum_subscribers', $topic_id, $forum_id, $user_ids );
     1268
     1269        // Send notification email
     1270        wp_mail( $do_not_reply, $subject, $message, $headers );
    12271271
    12281272        do_action( 'bbp_post_notify_forum_subscribers', $topic_id, $forum_id, $user_ids );
Note: See TracChangeset for help on using the changeset viewer.