Skip to:
Content

bbPress.org


Ignore:
Timestamp:
11/20/2013 07:50:55 PM (8 years ago)
Author:
johnjamesjacoby
Message:

Forum Subscriptions - Allow users to subscribe to new topics in specific forums.

  • Code largely lifted from existing Topics Subscriptions, and is based largely on forum-subscriptions.2.diff from mordauk, with edits for code consistency across bbPress components.
  • Refactor existing ambiguous function names into base functions for both forum and topic subscriptions.
  • Include new functions for getting and outputting subscriptions.
  • Modify user-subscriptions.php to show subscribed forums. This includes a modification to content-single-forum.php to include the "Unsubscribe" link if looking at a user profile page.
  • Modify templates/default/bbpress-functions.php to enqueue new JS file to handle forum subscription ajax.
  • Rename HTML element classes from bbp-topic-action to bbp-row-actions to better accommodate forum subscriptions (and any future actions.)
  • BuddyPress tested, JJJ approved.
  • See #2299. Props mordauk, netweb for the considerable effort.
  • More to do here, largely from forum-subscriptions.3.diff
File:
1 edited

Legend:

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

    r5134 r5156  
    983983
    984984/**
    985  * Sends notification emails for new posts
     985 * Sends notification emails for new replies to subscribed topics
    986986 *
    987987 * Gets new post's ID and check if there are subscribed users to that topic, and
     
    993993 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
    994994 * @uses bbp_get_reply_id() To validate the reply ID
     995 * @uses bbp_get_topic_id() To validate the topic ID
     996 * @uses bbp_get_forum_id() To validate the forum ID
    995997 * @uses bbp_get_reply() To get the reply
    996  * @uses bbp_get_reply_topic_id() To get the topic ID of the reply
    997998 * @uses bbp_is_reply_published() To make sure the reply is published
    998999 * @uses bbp_get_topic_id() To validate the topic ID
    9991000 * @uses bbp_get_topic() To get the reply's topic
    10001001 * @uses bbp_is_topic_published() To make sure the topic is published
    1001  * @uses get_the_author_meta() To get the author's display name
    1002  * @uses do_action() Calls 'bbp_pre_notify_subscribers' with the reply id and
    1003  *                    topic id
     1002 * @uses bbp_get_reply_author_display_name() To get the reply author's display name
     1003 * @uses do_action() Calls 'bbp_pre_notify_subscribers' with the reply id,
     1004 *                    topic id and user id
    10041005 * @uses bbp_get_topic_subscribers() To get the topic subscribers
    10051006 * @uses apply_filters() Calls 'bbp_subscription_mail_message' with the
    1006  *                        message, reply id, topic id and user id
     1007 *                    message, reply id, topic id and user id
     1008 * @uses apply_filters() Calls 'bbp_subscription_mail_title' with the
     1009 *                    topic title, reply id, topic id and user id
     1010 * @uses apply_filters() Calls 'bbp_subscription_mail_headers'
    10071011 * @uses get_userdata() To get the user data
    10081012 * @uses wp_mail() To send the mail
    1009  * @uses do_action() Calls 'bbp_post_notify_subscribers' with the reply id
    1010  *                    and topic id
     1013 * @uses do_action() Calls 'bbp_post_notify_subscribers' with the reply id,
     1014 *                    topic id and user id
    10111015 * @return bool True on success, false on failure
    10121016 */
     
    10371041    /** User ******************************************************************/
    10381042
    1039     // Get subscribers and bail if empty
     1043    // Get topic subscribers and bail if empty
    10401044    $user_ids = bbp_get_topic_subscribers( $topic_id, true );
    10411045    if ( empty( $user_ids ) )
     
    11051109
    11061110    do_action( 'bbp_post_notify_subscribers', $reply_id, $topic_id, $user_ids );
     1111
     1112    return true;
     1113}
     1114
     1115/**
     1116 * Sends notification emails for new topics to subscribed forums
     1117 *
     1118 * Gets new post's ID and check if there are subscribed users to that topic, and
     1119 * if there are, send notifications
     1120 *
     1121 * @since bbPress (rxxxx)
     1122 *
     1123 * @param int $topic_id ID of the newly made reply
     1124 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
     1125 * @uses bbp_get_topic_id() To validate the topic ID
     1126 * @uses bbp_get_forum_id() To validate the forum ID
     1127 * @uses bbp_is_topic_published() To make sure the topic is published
     1128 * @uses bbp_get_forum_subscribers() To get the forum subscribers
     1129 * @uses bbp_get_topic_author_display_name() To get the topic author's display name
     1130 * @uses do_action() Calls 'bbp_pre_notify_forum_subscribers' with the topic id,
     1131 *                    forum id and user id
     1132 * @uses apply_filters() Calls 'bbp_forum_subscription_mail_message' with the
     1133 *                    message, topic id, forum id and user id
     1134 * @uses apply_filters() Calls 'bbp_forum_subscription_mail_title' with the
     1135 *                    topic title, topic id, forum id and user id
     1136 * @uses apply_filters() Calls 'bbp_forum_subscription_mail_headers'
     1137 * @uses get_userdata() To get the user data
     1138 * @uses wp_mail() To send the mail
     1139 * @uses do_action() Calls 'bbp_post_notify_forum_subscribers' with the topic,
     1140 *                    id, forum id and user id
     1141 * @return bool True on success, false on failure
     1142 */
     1143function bbp_notify_forum_subscribers( $topic_id = 0, $forum_id = 0, $anonymous_data = false, $topic_author = 0 ) {
     1144
     1145    // Bail if subscriptions are turned off
     1146    if ( !bbp_is_subscriptions_active() )
     1147        return false;
     1148
     1149    /** Validation ************************************************************/
     1150
     1151    $topic_id = bbp_get_topic_id( $topic_id );
     1152    $forum_id = bbp_get_forum_id( $forum_id );
     1153
     1154    /** Topic *****************************************************************/
     1155
     1156    // Bail if topic is not published
     1157    if ( ! bbp_is_topic_published( $topic_id ) )
     1158        return false;
     1159
     1160    /** User ******************************************************************/
     1161
     1162    // Get forum subscribers and bail if empty
     1163    $user_ids = bbp_get_forum_subscribers( $forum_id, true );
     1164    if ( empty( $user_ids ) )
     1165        return false;
     1166
     1167    // Poster name
     1168    $topic_author_name = bbp_get_topic_author_display_name( $topic_id );
     1169
     1170    /** Mail ******************************************************************/
     1171
     1172    do_action( 'bbp_pre_notify_forum_subscribers', $topic_id, $forum_id, $user_ids );
     1173
     1174    // Remove filters from reply content and topic title to prevent content
     1175    // from being encoded with HTML entities, wrapped in paragraph tags, etc...
     1176    remove_all_filters( 'bbp_get_topic_content' );
     1177    remove_all_filters( 'bbp_get_topic_title'   );
     1178
     1179    // Strip tags from text
     1180    $topic_title   = strip_tags( bbp_get_topic_title( $topic_id ) );
     1181    $topic_content = strip_tags( bbp_get_topic_content( $topic_id ) );
     1182    $topic_url     = get_permalink( $topic_id );
     1183    $blog_name     = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
     1184
     1185    // Loop through users
     1186    foreach ( (array) $user_ids as $user_id ) {
     1187
     1188        // Don't send notifications to the person who made the post
     1189        if ( !empty( $topic_author ) && (int) $user_id === (int) $topic_author )
     1190            continue;
     1191
     1192        // For plugins to filter messages per reply/topic/user
     1193        $message = sprintf( __( '%1$s wrote:
     1194
     1195%2$s
     1196
     1197Topic Link: %3$s
     1198
     1199-----------
     1200
     1201You are receiving this email because you subscribed to a forum.
     1202
     1203Login 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    }
     1227
     1228    do_action( 'bbp_post_notify_forum_subscribers', $topic_id, $forum_id, $user_ids );
    11071229
    11081230    return true;
Note: See TracChangeset for help on using the changeset viewer.