Skip to:
Content

bbPress.org

Ticket #2299: forum-subscriptions.2.diff

File forum-subscriptions.2.diff, 57.3 KB (added by mordauk, 10 years ago)
  • includes/common/functions.php

     
    982982/** Subscriptions *************************************************************/
    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
    988988 * if there are, send notifications
     
    992992 * @param int $reply_id ID of the newly made reply
    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 */
    10131017function bbp_notify_subscribers( $reply_id = 0, $topic_id = 0, $forum_id = 0, $anonymous_data = false, $reply_author = 0 ) {
     
    10361040
    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 ) )
    10421046                return false;
     
    11081112        return true;
    11091113}
    11101114
     1115/**
     1116 * Sends notification emails for new topics to subscribed forums
     1117 *
     1118 * @since bbPress (r5155)
     1119 *
     1120 * Gets new post's ID and check if there are subscribed users to that topic, and
     1121 * if there are, send notifications
     1122 *
     1123 * @since bbPress (r5155)
     1124 *
     1125 * @param int $topic_id ID of the newly made reply
     1126 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
     1127 * @uses bbp_get_topic_id() To validate the topic ID
     1128 * @uses bbp_get_forum_id() To validate the forum ID
     1129 * @uses bbp_is_topic_published() To make sure the topic is published
     1130 * @uses bbp_get_forum_subscribers() To get the forum subscribers
     1131 * @uses bbp_get_topic_author_display_name() To get the topic author's display name
     1132 * @uses do_action() Calls 'bbp_pre_notify_forum_subscribers' with the topic id,
     1133 *                    forum id and user id
     1134 * @uses apply_filters() Calls 'bbp_forum_subscription_mail_message' with the
     1135 *                    message, topic id, forum id and user id
     1136 * @uses apply_filters() Calls 'bbp_forum_subscription_mail_title' with the
     1137 *                    topic title, topic id, forum id and user id
     1138 * @uses apply_filters() Calls 'bbp_forum_subscription_mail_headers'
     1139 * @uses get_userdata() To get the user data
     1140 * @uses wp_mail() To send the mail
     1141 * @uses do_action() Calls 'bbp_post_notify_forum_subscribers' with the topic,
     1142 *                    id, forum id and user id
     1143 * @return bool True on success, false on failure
     1144 */
     1145function bbp_notify_forum_subscribers( $topic_id = 0, $forum_id = 0, $anonymous_data = false, $topic_author = 0 ) {
     1146
     1147        // Bail if subscriptions are turned off
     1148        if ( !bbp_is_subscriptions_active() )
     1149                return false;
     1150
     1151        /** Validation ************************************************************/
     1152
     1153        $topic_id = bbp_get_topic_id( $topic_id );
     1154        $forum_id = bbp_get_forum_id( $forum_id );
     1155
     1156        /** Topic *****************************************************************/
     1157
     1158        // Bail if topic is not published
     1159        if ( ! bbp_is_topic_published( $topic_id ) )
     1160                return false;
     1161
     1162        /** User ******************************************************************/
     1163
     1164        // Get forum subscribers and bail if empty
     1165        $user_ids = bbp_get_forum_subscribers( $forum_id, true );
     1166        if ( empty( $user_ids ) )
     1167                return false;
     1168
     1169        // Poster name
     1170        $topic_author_name = bbp_get_topic_author_display_name( $topic_id );
     1171
     1172        /** Mail ******************************************************************/
     1173
     1174        do_action( 'bbp_pre_notify_forum_subscribers', $topic_id, $forum_id, $user_ids );
     1175
     1176        // Remove filters from reply content and topic title to prevent content
     1177        // from being encoded with HTML entities, wrapped in paragraph tags, etc...
     1178        remove_all_filters( 'bbp_get_topic_content' );
     1179        remove_all_filters( 'bbp_get_topic_title'   );
     1180
     1181        // Strip tags from text
     1182        $topic_title   = strip_tags( bbp_get_topic_title( $topic_id ) );
     1183        $topic_content = strip_tags( bbp_get_topic_content( $topic_id ) );
     1184        $topic_url     = get_permalink( $topic_id );
     1185        $blog_name     = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
     1186
     1187        // Loop through users
     1188        foreach ( (array) $user_ids as $user_id ) {
     1189
     1190                // Don't send notifications to the person who made the post
     1191                if ( !empty( $topic_author ) && (int) $user_id === (int) $topic_author )
     1192                        continue;
     1193
     1194                // For plugins to filter messages per reply/topic/user
     1195                $message = sprintf( __( '%1$s wrote:
     1196
     1197%2$s
     1198
     1199Topic Link: %3$s
     1200
     1201-----------
     1202
     1203You are receiving this email because you subscribed to a forum.
     1204
     1205Login and visit the topic to unsubscribe from these emails.', 'bbpress' ),
     1206
     1207                        $topic_author_name,
     1208                        $topic_content,
     1209                        $topic_url
     1210                );
     1211                $message = apply_filters( 'bbp_forum_subscription_mail_message', $message, $topic_id, $forum_id, $user_id );
     1212                if ( empty( $message ) )
     1213                        continue;
     1214
     1215                // For plugins to filter titles per reply/topic/user
     1216                $subject = apply_filters( 'bbp_forum_subscription_mail_title', '[' . $blog_name . '] ' . $topic_title, $topic_id, $forum_id, $user_id );
     1217                if ( empty( $subject ) )
     1218                        continue;
     1219
     1220                // Custom headers
     1221                $headers = apply_filters( 'bbp_forum_subscription_mail_headers', array() );
     1222
     1223                // Get user data of this user
     1224                $user = get_userdata( $user_id );
     1225
     1226                // Send notification email
     1227                wp_mail( $user->user_email, $subject, $message, $headers );
     1228        }
     1229
     1230        do_action( 'bbp_post_notify_forum_subscribers', $topic_id, $forum_id, $user_ids );
     1231
     1232        return true;
     1233}
     1234
    11111235/** Login *********************************************************************/
    11121236
    11131237/**
  • includes/common/template.php

     
    23442344                return apply_filters( 'bbp_get_breadcrumb', $trail, $crumbs, $r );
    23452345        }
    23462346
     2347/** Forum Subscriptions ******************************************************/
     2348
     2349/**
     2350 * Output the forum subscription link
     2351 *
     2352 * @since bbPress (r5155)
     2353 *
     2354 * @uses bbp_get_forum_subscription_link()
     2355 */
     2356function bbp_forum_subscription_link( $args = array() ) {
     2357        echo bbp_get_forum_subscription_link( $args );
     2358}
     2359
     2360        /**
     2361         * Get the forum subscription link
     2362         *
     2363         * @since bbPress (r5155)
     2364         *
     2365         * @uses bbp_parse_args()
     2366         * @uses bbp_get_user_subscribe_link()
     2367         * @uses apply_filters() Calls 'bbp_get_forum_subscribe_link'
     2368         */
     2369        function bbp_get_forum_subscription_link( $args = array() ) {
     2370
     2371                // Parse args
     2372                $r = bbp_parse_args( $args, array(
     2373
     2374                        // Forum
     2375                        'forum_id'    => 0,
     2376
     2377                        // User
     2378                        'user_id'     => 0,
     2379
     2380                        // HTML
     2381                        'before'      => '<div class="bbp-forum-subscription"><p>',
     2382                        'after'       => '</p></div>',
     2383
     2384                        // Text
     2385                        'subscribe'   => __( 'Subscribe to Forum', 'bbpress' ),
     2386                        'unsubscribe' => __( 'Unsubscribe from Forum', 'bbpress' ),
     2387
     2388                ), 'get_forum_subscribe_link' );
     2389
     2390                $link = $r['before'];
     2391
     2392                $link .= bbp_get_user_subscribe_link( array(
     2393                        'before'      => '',
     2394                        'subscribe'   => $r['subscribe'],
     2395                        'unsubscribe' => $r['unsubscribe']
     2396                ) );
     2397
     2398                $link .= $r['after'];
     2399
     2400                return apply_filters( 'bbp_get_forum_subscribe_link', $link, $r );
     2401        }
     2402
    23472403/** Topic Tags ***************************************************************/
    23482404
    23492405/**
  • includes/core/actions.php

     
    3737 *
    3838 *           v--WordPress Actions        v--bbPress Sub-actions
    3939 */
    40 add_action( 'plugins_loaded',           'bbp_loaded',                   10    );
    41 add_action( 'init',                     'bbp_init',                     0     ); // Early for bbp_register
    42 add_action( 'parse_query',              'bbp_parse_query',              2     ); // Early for overrides
    43 add_action( 'widgets_init',             'bbp_widgets_init',             10    );
    44 add_action( 'generate_rewrite_rules',   'bbp_generate_rewrite_rules',   10    );
    45 add_action( 'wp_enqueue_scripts',       'bbp_enqueue_scripts',          10    );
    46 add_action( 'wp_head',                  'bbp_head',                     10    );
    47 add_action( 'wp_footer',                'bbp_footer',                   10    );
    48 add_action( 'set_current_user',         'bbp_setup_current_user',       10    );
    49 add_action( 'setup_theme',              'bbp_setup_theme',              10    );
    50 add_action( 'after_setup_theme',        'bbp_after_setup_theme',        10    );
    51 add_action( 'template_redirect',        'bbp_template_redirect',        8     ); // Before BuddyPress's 10 [BB2225]
    52 add_action( 'login_form_login',         'bbp_login_form_login',         10    );
    53 add_action( 'profile_update',           'bbp_profile_update',           10, 2 ); // user_id and old_user_data
    54 add_action( 'user_register',            'bbp_user_register',            10    );
     40add_action( 'plugins_loaded',           'bbp_loaded',                 10    );
     41add_action( 'init',                     'bbp_init',                   0     ); // Early for bbp_register
     42add_action( 'parse_query',              'bbp_parse_query',            2     ); // Early for overrides
     43add_action( 'widgets_init',             'bbp_widgets_init',           10    );
     44add_action( 'generate_rewrite_rules',   'bbp_generate_rewrite_rules', 10    );
     45add_action( 'wp_enqueue_scripts',       'bbp_enqueue_scripts',        10    );
     46add_action( 'wp_head',                  'bbp_head',                   10    );
     47add_action( 'wp_footer',                'bbp_footer',                 10    );
     48add_action( 'set_current_user',         'bbp_setup_current_user',     10    );
     49add_action( 'setup_theme',              'bbp_setup_theme',            10    );
     50add_action( 'after_setup_theme',        'bbp_after_setup_theme',      10    );
     51add_action( 'template_redirect',        'bbp_template_redirect',      8     ); // Before BuddyPress's 10 [BB2225]
     52add_action( 'login_form_login',         'bbp_login_form_login',       10    );
     53add_action( 'profile_update',           'bbp_profile_update',         10, 2 ); // user_id and old_user_data
     54add_action( 'user_register',            'bbp_user_register',          10    );
    5555
    5656/**
    5757 * bbp_loaded - Attached to 'plugins_loaded' above
     
    118118add_action( 'bbp_register', 'bbp_register_shortcodes',     10 );
    119119
    120120// Autoembeds
    121 add_action( 'bbp_init', 'bbp_reply_content_autoembed', 8   );
    122 add_action( 'bbp_init', 'bbp_topic_content_autoembed', 8   );
     121add_action( 'bbp_init', 'bbp_reply_content_autoembed', 8 );
     122add_action( 'bbp_init', 'bbp_topic_content_autoembed', 8 );
    123123
    124124/**
    125125 * bbp_ready - attached to end 'bbp_init' above
     
    224224// Subscriptions
    225225add_action( 'bbp_trash_topic',  'bbp_remove_topic_from_all_subscriptions'       );
    226226add_action( 'bbp_delete_topic', 'bbp_remove_topic_from_all_subscriptions'       );
     227add_action( 'bbp_trash_forum',  'bbp_remove_forum_from_all_subscriptions'       );
     228add_action( 'bbp_delete_forum', 'bbp_remove_forum_from_all_subscriptions'       );
    227229add_action( 'bbp_new_reply',    'bbp_notify_subscribers',                 11, 5 );
     230add_action( 'bbp_new_topic',    'bbp_notify_forum_subscribers',           11, 4 );
    228231
    229232// Sticky
    230233add_action( 'bbp_trash_topic',  'bbp_unstick_topic' );
     
    297300add_action( 'bbp_post_request', 'bbp_new_topic_handler',      10 );
    298301
    299302// Theme-side GET requests
    300 add_action( 'bbp_get_request', 'bbp_toggle_topic_handler',    1  );
    301 add_action( 'bbp_get_request', 'bbp_toggle_reply_handler',    1  );
    302 add_action( 'bbp_get_request', 'bbp_favorites_handler',       1  );
    303 add_action( 'bbp_get_request', 'bbp_subscriptions_handler',   1  );
    304 add_action( 'bbp_get_request', 'bbp_search_results_redirect', 10 );
     303add_action( 'bbp_get_request', 'bbp_toggle_topic_handler',        1  );
     304add_action( 'bbp_get_request', 'bbp_toggle_reply_handler',        1  );
     305add_action( 'bbp_get_request', 'bbp_favorites_handler',           1  );
     306add_action( 'bbp_get_request', 'bbp_subscriptions_handler',       1  );
     307add_action( 'bbp_get_request', 'bbp_forum_subscriptions_handler', 1  );
     308add_action( 'bbp_get_request', 'bbp_search_results_redirect',     10 );
    305309
    306310// Maybe convert the users password
    307311add_action( 'bbp_login_form_login', 'bbp_user_maybe_convert_pass' );
  • includes/forums/functions.php

     
    961961        return true;
    962962}
    963963
     964/** Subscriptions *************************************************/
     965
     966/**
     967 * Remove a deleted forum from all users' subscriptions
     968 *
     969 * @since bbPress (r5155)
     970 *
     971 * @param int $forum_id Get the forum ID to remove
     972 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
     973 * @uses bbp_get_forum_id To get the forum id
     974 * @uses bbp_get_forum_subscribers() To get the forum subscribers
     975 * @uses bbp_remove_user_subscription() To remove the user subscription
     976 */
     977function bbp_remove_forum_from_all_subscriptions( $forum_id = 0 ) {
     978
     979        // Subscriptions are not active
     980        if ( ! bbp_is_subscriptions_active() )
     981                return;
     982
     983        $forum_id = bbp_get_forum_id( $forum_id );
     984
     985        // Bail if no forum
     986        if ( empty( $forum_id ) )
     987                return;
     988
     989        // Get users
     990        $users = (array) bbp_get_forum_subscribers( $forum_id );
     991
     992        // Users exist
     993        if ( !empty( $users ) ) {
     994
     995                // Loop through users
     996                foreach ( $users as $user ) {
     997
     998                        // Remove each user
     999                        bbp_remove_user_subscription( $user, $forum_id );
     1000                }
     1001        }
     1002}
     1003
    9641004/** Count Bumpers *************************************************************/
    9651005
    9661006/**
  • includes/forums/template.php

     
    442442}
    443443
    444444/**
     445 * Output checked value of forum subscription
     446 *
     447 * @since bbPress (r5155)
     448 *
     449 * @uses bbp_get_form_forum_subscribed() To get the subscribed checkbox value
     450 */
     451function bbp_form_forum_subscribed() {
     452        echo bbp_get_form_forum_subscribed();
     453}
     454        /**
     455         * Return checked value of forum subscription
     456         *
     457         * @since bbPress (r5155)
     458         *
     459         * @uses bbp_is_forum_edit() To check if it's the forum edit page
     460         * @uses bbp_get_global_post_field() To get current post author
     461         * @uses bbp_get_current_user_id() To get the current user id
     462         * @uses bbp_is_user_subscribed() To check if the user is subscribed to
     463         *                the forum
     464         * @uses apply_filters() Calls 'bbp_get_form_forum_subscribed' with the
     465         *                option
     466         * @return string Checked value of forum subscription
     467         */
     468        function bbp_get_form_forum_subscribed() {
     469
     470                // Get _POST data
     471                if ( bbp_is_post_request() && isset( $_POST['bbp_forum_subscription'] ) ) {
     472                        $forum_subscribed = (bool) $_POST['bbp_forum_subscription'];
     473
     474                // Get edit data
     475                } elseif ( bbp_is_forum_edit() || bbp_is_reply_edit() ) {
     476
     477                        // Get current posts author
     478                        $post_author = bbp_get_global_post_field( 'post_author', 'raw' );
     479
     480                        // Post author is not the current user
     481                        if ( bbp_get_current_user_id() !== $post_author ) {
     482                                $forum_subscribed = bbp_is_user_subscribed( $post_author );
     483
     484                        // Post author is the current user
     485                        } else {
     486                                $forum_subscribed = bbp_is_user_subscribed( bbp_get_current_user_id() );
     487                        }
     488
     489                // Get current status
     490                } elseif ( bbp_is_single_forum() ) {
     491                        $forum_subscribed = bbp_is_user_subscribed( bbp_get_current_user_id() );
     492
     493                // No data
     494                } else {
     495                        $forum_subscribed = false;
     496                }
     497
     498                // Get checked output
     499                $checked = checked( $forum_subscribed, true, false );
     500
     501                return apply_filters( 'bbp_get_form_forum_subscribed', $checked, $forum_subscribed );
     502        }
     503
     504/**
    445505 * Output the forums last active ID
    446506 *
    447507 * @since bbPress (r2860)
  • includes/topics/functions.php

     
    22002200 *
    22012201 * @since bbPress (r2652)
    22022202 *
    2203  * @param int $topic_id Topic ID to remove
     2203 * @param int $topic_id Get the topic id to remove
     2204 * @uses bbp_get_topic_id To get the topic id
    22042205 * @uses bbp_get_topic_favoriters() To get the topic's favoriters
    22052206 * @uses bbp_remove_user_favorite() To remove the topic from user's favorites
    22062207 */
     
    22312232 *
    22322233 * @since bbPress (r2652)
    22332234 *
    2234  * @param int $topic_id Topic ID to remove
     2235 * @param int $topic_id Get the topic id to remove
    22352236 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
     2237 * @uses bbp_get_topic_id To get the topic id
    22362238 * @uses bbp_get_topic_subscribers() To get the topic subscribers
    22372239 * @uses bbp_remove_user_subscription() To remove the user subscription
    22382240 */
  • includes/users/functions.php

     
    313313        $favorites = bbp_get_user_favorites_topic_ids( $user_id );
    314314
    315315        if ( !empty( $favorites ) ) {
    316                
     316
    317317                // Checking a specific topic id
    318318                if ( !empty( $topic_id ) ) {
    319319                        $topic    = bbp_get_topic( $topic_id );
     
    512512/** Subscriptions *************************************************************/
    513513
    514514/**
     515 * Get the users who have subscribed to the forum
     516 *
     517 * @since bbPress (r2668)
     518 *
     519 * @param int $forum_id Optional. forum id
     520 * @uses wpdb::get_col() To execute our query and get the column back
     521 * @uses apply_filters() Calls 'bbp_get_forum_subscribers' with the subscribers
     522 * @return array|bool Results if the forum has any subscribers, otherwise false
     523 */
     524function bbp_get_forum_subscribers( $forum_id = 0 ) {
     525        $forum_id = bbp_get_forum_id( $forum_id );
     526        if ( empty( $forum_id ) )
     527                return;
     528
     529        global $wpdb;
     530
     531        $key   = $wpdb->prefix . '_bbp_forum_subscriptions';
     532        $users = wp_cache_get( 'bbp_get_forum_subscribers_' . $forum_id, 'bbpress_users' );
     533        if ( false === $users ) {
     534                $users = $wpdb->get_col( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = '{$key}' and FIND_IN_SET('{$forum_id}', meta_value) > 0" );
     535                wp_cache_set( 'bbp_get_forum_subscribers_' . $forum_id, $users, 'bbpress_users' );
     536        }
     537
     538        return apply_filters( 'bbp_get_forum_subscribers', $users );
     539}
     540
     541/**
    515542 * Get the users who have subscribed to the topic
    516543 *
    517544 * @since bbPress (r2668)
     
    569596}
    570597
    571598/**
     599 * Get a user's subscribed forum ids
     600 *
     601 * @since bbPress (r2668)
     602 *
     603 * @param int $user_id Optional. User id
     604 * @uses bbp_get_user_id() To get the user id
     605 * @uses get_user_option() To get the user's subscriptions
     606 * @uses apply_filters() Calls 'bbp_get_user_subscribed_forum_ids' with
     607 *                        the subscriptions and user id
     608 * @return array|bool Results if user has subscriptions, otherwise false
     609 */
     610function bbp_get_user_subscribed_forum_ids( $user_id = 0 ) {
     611        $user_id = bbp_get_user_id( $user_id );
     612        if ( empty( $user_id ) )
     613                return false;
     614
     615        $subscriptions = get_user_option( '_bbp_forum_subscriptions', $user_id );
     616        $subscriptions = array_filter( wp_parse_id_list( $subscriptions ) );
     617
     618        return (array) apply_filters( 'bbp_get_user_subscribed_forum_ids', $subscriptions, $user_id );
     619}
     620
     621/**
    572622 * Get a user's subscribed topics' ids
    573623 *
    574624 * @since bbPress (r2668)
     
    592642}
    593643
    594644/**
    595  * Check if a topic is in user's subscription list or not
     645 * Check if a user's subscription list or not
    596646 *
     647 * @param int $user_id Optional. User id
     648 * @param int $forum_id Optional. Topic id
     649 * @uses get_post() To get the post object
     650 * @uses bbp_get_user_subscribed_forum_ids() To get the user's forum subscriptions
     651 * @uses bbp_get_user_subscribed_topic_ids() To get the user's topic subscriptions
     652 * @uses bbp_get_forum_post_type() To get the forum post type
     653 * @uses bbp_get_topic_post_type() To get the topic post type
     654 * @uses apply_filters() Calls 'bbp_is_user_subscribed' with the bool, user id,
     655 *                        forum/topic id and subsriptions
     656 * @return bool True if the forum or topic is in user's subscriptions, otherwise false
     657 */
     658function bbp_is_user_subscribed( $user_id = 0, $object_id = 0 ) {
     659        if ( empty( $user_id ) || empty( $object_id ) )
     660                return false;
     661
     662        $object = get_post( $object_id );
     663        if( ! $object )
     664                return false;
     665
     666        $post_type = $object->post_type;
     667
     668        switch( $post_type ) {
     669
     670                case bbp_get_forum_post_type() :
     671
     672                        $subscriptions = bbp_get_user_subscribed_forum_ids( $user_id );
     673                        $retval        = bbp_is_user_subscribed_to_forum( $user_id, $object_id );
     674                        break;
     675
     676                case bbp_get_topic_post_type() :
     677                default :
     678
     679                        $subscriptions = bbp_get_user_subscribed_topic_ids( $user_id );
     680                        $retval        = bbp_is_user_subscribed_to_topic( $user_id, $object_id );
     681                        break;
     682        }
     683
     684        return (bool) apply_filters( 'bbp_is_user_subscribed', $retval, $user_id, $object_id, $subscriptions );
     685}
     686
     687/**
     688 * Check if a forum is in user's subscription list or not
     689 *
    597690 * @since bbPress (r2668)
    598691 *
    599692 * @param int $user_id Optional. User id
     693 * @param int $forum_id Optional. Topic id
     694 * @uses bbp_get_user_id() To get the user id
     695 * @uses bbp_get_user_subscribed_forum_ids() To get the user's subscriptions
     696 * @uses bbp_get_forum() To get the forum
     697 * @uses bbp_get_forum_id() To get the forum id
     698 * @uses apply_filters() Calls 'bbp_is_user_subscribed' with the bool, user id,
     699 *                        forum id and subsriptions
     700 * @return bool True if the forum is in user's subscriptions, otherwise false
     701 */
     702function bbp_is_user_subscribed_to_forum( $user_id = 0, $forum_id = 0 ) {
     703
     704        // Validate user
     705        $user_id = bbp_get_user_id( $user_id, true, true );
     706        if ( empty( $user_id ) )
     707                return false;
     708
     709        $retval        = false;
     710        $subscriptions = bbp_get_user_subscribed_forum_ids( $user_id );
     711
     712        if ( ! empty( $subscriptions ) ) {
     713
     714                // Checking a specific forum id
     715                if ( ! empty( $forum_id ) ) {
     716                        $forum    = bbp_get_forum( $forum_id );
     717                        $forum_id = ! empty( $forum ) ? $forum->ID : 0;
     718
     719                // Using the global forum id
     720                } elseif ( bbp_get_forum_id() ) {
     721                        $forum_id = bbp_get_forum_id();
     722
     723                // Use the current post id
     724                } elseif ( ! bbp_get_forum_id() ) {
     725                        $forum_id = get_the_ID();
     726                }
     727
     728                // Is forum_id in the user's favorites
     729                if ( ! empty( $forum_id ) ) {
     730                        $retval = in_array( $forum_id, $subscriptions );
     731                }
     732        }
     733
     734        return (bool) apply_filters( 'bbp_is_user_subscribed_to_forum', (bool) $retval, $user_id, $forum_id, $subscriptions );
     735}
     736
     737/**
     738 * Check if a topic is in user's subscription list or not
     739 *
     740 * @param int $user_id Optional. User id
    600741 * @param int $topic_id Optional. Topic id
    601742 * @uses bbp_get_user_id() To get the user id
    602743 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions
     
    606747 *                        topic id and subsriptions
    607748 * @return bool True if the topic is in user's subscriptions, otherwise false
    608749 */
    609 function bbp_is_user_subscribed( $user_id = 0, $topic_id = 0 ) {
     750function bbp_is_user_subscribed_to_topic( $user_id = 0, $topic_id = 0 ) {
    610751
    611752        // Validate user
    612753        $user_id = bbp_get_user_id( $user_id, true, true );
     
    616757        $retval        = false;
    617758        $subscriptions = bbp_get_user_subscribed_topic_ids( $user_id );
    618759
    619         if ( !empty( $subscriptions ) ) {
     760        if ( ! empty( $subscriptions ) ) {
    620761
    621762                // Checking a specific topic id
    622                 if ( !empty( $topic_id ) ) {
    623                         $topic     = bbp_get_topic( $topic_id );
    624                         $topic_id = !empty( $topic ) ? $topic->ID : 0;
     763                if ( ! empty( $topic_id ) ) {
     764                        $topic    = bbp_get_topic( $topic_id );
     765                        $topic_id = ! empty( $topic ) ? $topic->ID : 0;
    625766
    626767                // Using the global topic id
    627768                } elseif ( bbp_get_topic_id() ) {
     
    633774                }
    634775
    635776                // Is topic_id in the user's favorites
    636                 if ( !empty( $topic_id ) ) {
     777                if ( ! empty( $topic_id ) ) {
    637778                        $retval = in_array( $topic_id, $subscriptions );
    638779                }
    639780        }
    640781
    641         return (bool) apply_filters( 'bbp_is_user_subscribed', (bool) $retval, $user_id, $topic_id, $subscriptions );
     782        return (bool) apply_filters( 'bbp_is_user_subscribed_to_topic', (bool) $retval, $user_id, $topic_id, $subscriptions );
    642783}
    643784
    644785/**
     
    648789 *
    649790 * @param int $user_id Optional. User id
    650791 * @param int $topic_id Optional. Topic id
     792 * @uses get_post() To get the post object
     793 * @uses bbp_get_user_subscribed_forum_ids() To get the user's forum subscriptions
     794 * @uses bbp_get_user_subscribed_topic_ids() To get the user's topic subscriptions
     795 * @uses bbp_get_forum_post_type() To get the forum post type
     796 * @uses bbp_get_topic_post_type() To get the topic post type
     797 * @uses update_user_option() To update the user's subscriptions
     798 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & topic id
     799 * @return bool Always true
     800 */
     801function bbp_add_user_subscription( $user_id = 0, $object_id = 0 ) {
     802        if ( empty( $user_id ) || empty( $object_id ) )
     803                return false;
     804
     805        $object = get_post( $object_id );
     806        if( ! $object )
     807                return false;
     808
     809        $post_type = $object->post_type;
     810
     811        switch( $post_type ) {
     812
     813                case bbp_get_forum_post_type() :
     814
     815                        $subscriptions = bbp_get_user_subscribed_forum_ids( $user_id );
     816
     817                        if ( ! in_array( $object_id, $subscriptions ) ) {
     818                                $subscriptions[] = $object_id;
     819                                $subscriptions   = implode( ',', wp_parse_id_list( array_filter( $subscriptions ) ) );
     820                                update_user_option( $user_id, '_bbp_forum_subscriptions', $subscriptions );
     821
     822                                wp_cache_delete( 'bbp_get_forum_subscribers_' . $object_id, 'bbpress_users' );
     823                        }
     824
     825                        break;
     826
     827                case bbp_get_topic_post_type() :
     828                default :
     829
     830                        $subscriptions = bbp_get_user_subscribed_topic_ids( $user_id );
     831
     832                        if ( ! in_array( $object_id, $subscriptions ) ) {
     833                                $subscriptions[] = $object_id;
     834                                $subscriptions   = implode( ',', wp_parse_id_list( array_filter( $subscriptions ) ) );
     835                                update_user_option( $user_id, '_bbp_subscriptions', $subscriptions );
     836
     837                                wp_cache_delete( 'bbp_get_topic_subscribers_' . $object_id, 'bbpress_users' );
     838                        }
     839
     840                        break;
     841        }
     842
     843        do_action( 'bbp_add_user_subscription', $user_id, $object_id, $post_type );
     844
     845        return true;
     846}
     847
     848/**
     849 * Add a forum to user's subscriptions
     850 *
     851 * @param int $user_id Optional. User id
     852 * @param int $forum_id Optional. forum id
     853 * @uses bbp_get_user_subscribed_forum_ids() To get the user's subscriptions
     854 * @uses bbp_get_forum() To get the forum
     855 * @uses update_user_option() To update the user's subscriptions
     856 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & forum id
     857 * @return bool Always true
     858 */
     859function bbp_add_user_forum_subscription( $user_id = 0, $forum_id = 0 ) {
     860        if ( empty( $user_id ) || empty( $forum_id ) )
     861                return false;
     862
     863        $forum = bbp_get_forum( $forum_id );
     864        if ( empty( $forum ) )
     865                return false;
     866
     867        $subscriptions = (array) bbp_get_user_subscribed_forum_ids( $user_id );
     868        if ( !in_array( $forum_id, $subscriptions ) ) {
     869                $subscriptions[] = $forum_id;
     870                $subscriptions   = implode( ',', wp_parse_id_list( array_filter( $subscriptions ) ) );
     871                update_user_option( $user_id, '_bbp_forum_subscriptions', $subscriptions );
     872
     873                wp_cache_delete( 'bbp_get_forum_subscribers_' . $forum_id, 'bbpress_users' );
     874        }
     875
     876        do_action( 'bbp_add_user_forum_subscription', $user_id, $forum_id );
     877
     878        return true;
     879}
     880
     881/**
     882 * Add a topic to user's subscriptions
     883 *
     884 * @since bbPress (r2668)
     885 *
     886 * @param int $user_id Optional. User id
     887 * @param int $topic_id Optional. Topic id
    651888 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions
    652889 * @uses bbp_get_topic() To get the topic
    653890 * @uses update_user_option() To update the user's subscriptions
    654891 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & topic id
    655892 * @return bool Always true
    656893 */
    657 function bbp_add_user_subscription( $user_id = 0, $topic_id = 0 ) {
     894function bbp_add_user_topic_subscription( $user_id = 0, $topic_id = 0 ) {
    658895        if ( empty( $user_id ) || empty( $topic_id ) )
    659896                return false;
    660897
     
    671908                wp_cache_delete( 'bbp_get_topic_subscribers_' . $topic_id, 'bbpress_users' );
    672909        }
    673910
    674         do_action( 'bbp_add_user_subscription', $user_id, $topic_id );
     911        do_action( 'bbp_add_user_topic_subscription', $user_id, $topic_id );
    675912
    676913        return true;
    677914}
     
    683920 *
    684921 * @param int $user_id Optional. User id
    685922 * @param int $topic_id Optional. Topic id
     923 * @uses get_post() To get the post object
     924 * @uses bbp_get_forum_post_type() To get the forum post type
     925 * @uses bbp_get_topic_post_type() To get the topic post type
     926 * @uses bbp_remove_user_forum_subscription() To remove the user's subscription
     927 * @uses bbp_remove_user_topic_subscription() To remove the user's subscription
     928 * @uses do_action() Calls 'bbp_remove_user_subscription' with the user id and
     929 *                    topic id
     930 * @return bool True if the topic was removed from user's subscriptions,
     931 *               otherwise false
     932 */
     933function bbp_remove_user_subscription( $user_id = 0, $object_id = 0 ) {
     934        if ( empty( $user_id ) || empty( $object_id ) )
     935                return false;
     936
     937        $post_type = get_post_type( $object_id );
     938
     939        switch( $post_type ) {
     940
     941                case bbp_get_forum_post_type() :
     942
     943                        bbp_remove_user_forum_subscription( $user_id, $object_id );
     944
     945                        break;
     946
     947                case bbp_get_topic_post_type() :
     948                default :
     949
     950                        bbp_remove_user_topic_subscription( $user_id, $object_id );
     951
     952                        break;
     953        }
     954
     955        do_action( 'bbp_remove_user_subscription', $user_id, $object_id, $post_type );
     956
     957        return true;
     958}
     959
     960/**
     961 * Remove a forum from user's subscriptions
     962 *
     963 * @param int $user_id Optional. User id
     964 * @param int $forum_id Optional. forum id
     965 * @uses bbp_get_user_subscribed_forum_ids() To get the user's subscriptions
     966 * @uses update_user_option() To update the user's subscriptions
     967 * @uses delete_user_option() To delete the user's subscriptions meta
     968 * @uses do_action() Calls 'bbp_remove_user_subscription' with the user id and
     969 *                    forum id
     970 * @return bool True if the forum was removed from user's subscriptions,
     971 *               otherwise false
     972 */
     973function bbp_remove_user_forum_subscription( $user_id, $forum_id ) {
     974        if ( empty( $user_id ) || empty( $forum_id ) )
     975                return false;
     976
     977        $subscriptions = (array) bbp_get_user_subscribed_forum_ids( $user_id );
     978
     979        if ( empty( $subscriptions ) )
     980                return false;
     981
     982        $pos = array_search( $forum_id, $subscriptions );
     983        if ( is_numeric( $pos ) ) {
     984                array_splice( $subscriptions, $pos, 1 );
     985                $subscriptions = array_filter( $subscriptions );
     986
     987                if ( !empty( $subscriptions ) ) {
     988                        $subscriptions = implode( ',', wp_parse_id_list( $subscriptions ) );
     989                        update_user_option( $user_id, '_bbp_forum_subscriptions', $subscriptions );
     990                } else {
     991                        delete_user_option( $user_id, '_bbp_forum_subscriptions' );
     992                }
     993
     994                wp_cache_delete( 'bbp_get_forum_subscribers_' . $forum_id, 'bbpress_users' );
     995        }
     996
     997        do_action( 'bbp_remove_user_forum_subscription', $user_id, $forum_id );
     998
     999        return true;
     1000}
     1001
     1002/**
     1003 * Remove a topic from user's subscriptions
     1004 *
     1005 * @param int $user_id Optional. User id
     1006 * @param int $topic_id Optional. Topic id
    6861007 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions
    6871008 * @uses update_user_option() To update the user's subscriptions
    6881009 * @uses delete_user_option() To delete the user's subscriptions meta
    689  * @uses do_action() Calls 'bbp_remove_user_subscription' with the user id and
     1010 * @uses do_action() Calls 'bbp_remove_user_topic_subscription' with the user id and
    6901011 *                    topic id
    6911012 * @return bool True if the topic was removed from user's subscriptions,
    6921013 *               otherwise false
    6931014 */
    694 function bbp_remove_user_subscription( $user_id, $topic_id ) {
     1015function bbp_remove_user_topic_subscription( $user_id, $topic_id ) {
    6951016        if ( empty( $user_id ) || empty( $topic_id ) )
    6961017                return false;
    6971018
     
    7151036                wp_cache_delete( 'bbp_get_topic_subscribers_' . $topic_id, 'bbpress_users' );
    7161037        }
    7171038
    718         do_action( 'bbp_remove_user_subscription', $user_id, $topic_id );
     1039        do_action( 'bbp_remove_user_topic_subscription', $user_id, $topic_id );
    7191040
    7201041        return true;
    7211042}
    7221043
    7231044/**
     1045 * Handles the front end subscribing and unsubscribing forums
     1046 *
     1047 * @param string $action The requested action to compare this function to
     1048 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
     1049 * @uses bbp_get_user_id() To get the user id
     1050 * @uses bbp_verify_nonce_request() To verify the nonce and check the request
     1051 * @uses current_user_can() To check if the current user can edit the user
     1052 * @uses bbPress:errors:add() To log the error messages
     1053 * @uses bbp_is_user_subscribed() To check if the forum is in user's
     1054 *                                 subscriptions
     1055 * @uses bbp_remove_user_subscription() To remove the user subscription
     1056 * @uses bbp_add_user_subscription() To add the user subscription
     1057 * @uses do_action() Calls 'bbp_subscriptions_handler' with success, user id,
     1058 *                    forum id and action
     1059 * @uses bbp_is_subscription() To check if it's the subscription page
     1060 * @uses bbp_get_subscription_link() To get the subscription page link
     1061 * @uses bbp_get_forum_permalink() To get the forum permalink
     1062 * @uses wp_safe_redirect() To redirect to the url
     1063 */
     1064function bbp_forum_subscriptions_handler( $action = '' ) {
     1065
     1066        if ( !bbp_is_subscriptions_active() )
     1067                return false;
     1068
     1069        // Bail if no forum ID is passed
     1070        if ( empty( $_GET['forum_id'] ) )
     1071                return;
     1072
     1073        // Setup possible get actions
     1074        $possible_actions = array(
     1075                'bbp_subscribe',
     1076                'bbp_unsubscribe',
     1077        );
     1078
     1079        // Bail if actions aren't meant for this function
     1080        if ( !in_array( $action, $possible_actions ) )
     1081                return;
     1082
     1083        // Get required data
     1084        $user_id  = bbp_get_user_id( 0, true, true );
     1085        $forum_id = intval( $_GET['forum_id'] );
     1086
     1087        // Check for empty forum
     1088        if ( empty( $forum_id ) ) {
     1089                bbp_add_error( 'bbp_subscription_forum_id', __( '<strong>ERROR</strong>: No forum was found! Which forum are you subscribing/unsubscribing to?', 'bbpress' ) );
     1090
     1091        // Check nonce
     1092        } elseif ( ! bbp_verify_nonce_request( 'toggle-subscription_' . $forum_id ) ) {
     1093                bbp_add_error( 'bbp_subscription_forum_id', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
     1094
     1095        // Check current user's ability to edit the user
     1096        } elseif ( !current_user_can( 'edit_user', $user_id ) ) {
     1097                bbp_add_error( 'bbp_subscription_permissions', __( '<strong>ERROR</strong>: You don\'t have the permission to edit favorites of that user!', 'bbpress' ) );
     1098        }
     1099
     1100        // Bail if we have errors
     1101        if ( bbp_has_errors() )
     1102                return;
     1103
     1104        /** No errors *************************************************************/
     1105
     1106        $is_subscription = bbp_is_user_subscribed( $user_id, $forum_id );
     1107        $success         = false;
     1108
     1109        if ( true === $is_subscription && 'bbp_unsubscribe' === $action )
     1110                $success = bbp_remove_user_subscription( $user_id, $forum_id );
     1111        elseif ( false === $is_subscription && 'bbp_subscribe' === $action )
     1112                $success = bbp_add_user_subscription( $user_id, $forum_id );
     1113
     1114        // Do additional subscriptions actions
     1115        do_action( 'bbp_subscriptions_handler', $success, $user_id, $forum_id, $action );
     1116
     1117        // Success!
     1118        if ( true === $success ) {
     1119
     1120                // Redirect back from whence we came
     1121                if ( bbp_is_subscriptions() ) {
     1122                        $redirect = bbp_get_subscriptions_permalink( $user_id );
     1123                } elseif ( bbp_is_single_user() ) {
     1124                        $redirect = bbp_get_user_profile_url();
     1125                } elseif ( is_singular( bbp_get_forum_post_type() ) ) {
     1126                        $redirect = bbp_get_forum_permalink( $forum_id );
     1127                } elseif ( is_single() || is_page() ) {
     1128                        $redirect = get_permalink();
     1129                } else {
     1130                        $redirect = get_permalink( $forum_id );
     1131                }
     1132
     1133                wp_safe_redirect( $redirect );
     1134
     1135                // For good measure
     1136                exit();
     1137
     1138        // Fail! Handle errors
     1139        } elseif ( true === $is_subscription && 'bbp_unsubscribe' === $action ) {
     1140                bbp_add_error( 'bbp_unsubscribe', __( '<strong>ERROR</strong>: There was a problem unsubscribing from that forum!', 'bbpress' ) );
     1141        } elseif ( false === $is_subscription && 'bbp_subscribe' === $action ) {
     1142                bbp_add_error( 'bbp_subscribe',    __( '<strong>ERROR</strong>: There was a problem subscribing to that forum!', 'bbpress' ) );
     1143        }
     1144}
     1145
     1146/**
    7241147 * Handles the front end subscribing and unsubscribing topics
    7251148 *
    7261149 * @param string $action The requested action to compare this function to
     
    8221245        }
    8231246}
    8241247
     1248
    8251249/** Edit **********************************************************************/
    8261250
    8271251/**
     
    9681392 * @return array|bool Results if the user has created topics, otherwise false
    9691393 */
    9701394function bbp_get_user_topics_started( $user_id = 0 ) {
    971        
     1395
    9721396        // Validate user
    9731397        $user_id = bbp_get_user_id( $user_id );
    9741398        if ( empty( $user_id ) )
     
    9931417 * @return array|bool Results if the user has created topics, otherwise false
    9941418 */
    9951419function bbp_get_user_replies_created( $user_id = 0 ) {
    996        
     1420
    9971421        // Validate user
    9981422        $user_id = bbp_get_user_id( $user_id );
    9991423        if ( empty( $user_id ) )
     
    10311455 * which a user can edit another user (or themselves.) If these conditions are
    10321456 * met. We assume a user cannot perform this task, and look for ways they can
    10331457 * earn the ability to access this template.
    1034  * 
     1458 *
    10351459 * @since bbPress (r3605)
    10361460 *
    10371461 * @uses bbp_is_topic_edit()
  • includes/users/template.php

     
    612612        }
    613613
    614614/** Anonymous Fields **********************************************************/
    615        
     615
    616616/**
    617617 * Output the author disylay-name of a topic or reply.
    618618 *
     
    10271027         *  - unsubscribe: Unsubscribe text
    10281028         *  - user_id: User id
    10291029         *  - topic_id: Topic id
     1030         *  - forum_id: Forum id
    10301031         *  - before: Before the link
    10311032         *  - after: After the link
    10321033         * @param int $user_id Optional. User id
    10331034         * @param bool $wrap Optional. If you want to wrap the link in <span id="subscription-toggle">.
     1035         * @uses bbp_is_subscriptions_active() to check if subscriptions are active
    10341036         * @uses bbp_get_user_id() To get the user id
     1037         * @uses bbp_get_user_id() To get the user id
     1038         * @uses bbp_get_topic_id() To get the topic id
     1039         * @uses bbp_get_forum_id() To get the forum id
    10351040         * @uses current_user_can() To check if the current user can edit user
    1036          * @uses bbp_get_topic_id() To get the topic id
    1037          * @uses bbp_is_user_subscribed() To check if the user is subscribed
     1041         * @uses bbp_is_user_subscribed_to_forum() To check if the user is subscribed to the forum
     1042         * @uses bbp_is_user_subscribed_to_topic() To check if the user is subscribed to the topic
    10381043         * @uses bbp_is_subscriptions() To check if it's the subscriptions page
    10391044         * @uses bbp_get_subscriptions_permalink() To get subscriptions link
    10401045         * @uses bbp_get_topic_permalink() To get topic link
     
    10431048         * @return string Permanent link to topic
    10441049         */
    10451050        function bbp_get_user_subscribe_link( $args = '', $user_id = 0, $wrap = true ) {
    1046                 if ( !bbp_is_subscriptions_active() )
     1051                if ( ! bbp_is_subscriptions_active() )
    10471052                        return;
    10481053
    10491054                // Parse arguments against default values
     
    10521057                        'unsubscribe' => __( 'Unsubscribe', 'bbpress' ),
    10531058                        'user_id'     => 0,
    10541059                        'topic_id'    => 0,
     1060                        'forum_id'    => 0,
    10551061                        'before'      => '&nbsp;|&nbsp;',
    10561062                        'after'       => ''
    10571063                ), 'get_user_subscribe_link' );
    10581064
    1059                 // Validate user and topic ID's
    1060                 $user_id  = bbp_get_user_id( $r['user_id'], true, true );
    1061                 $topic_id = bbp_get_topic_id( $r['topic_id'] );
    1062                 if ( empty( $user_id ) || empty( $topic_id ) ) {
     1065                // Validate user and object ID's
     1066                $user_id   = bbp_get_user_id( $r['user_id'], true, true );
     1067                $topic_id  = bbp_get_topic_id( $r['topic_id'] );
     1068                $forum_id  = bbp_get_forum_id( $r['forum_id'] );
     1069                if ( empty( $user_id ) || ( empty( $topic_id ) && empty( $forum_id ) ) ) {
    10631070                        return false;
    10641071                }
    10651072
    10661073                // No link if you can't edit yourself
    1067                 if ( !current_user_can( 'edit_user', (int) $user_id ) ) {
     1074                if ( ! current_user_can( 'edit_user', (int) $user_id ) ) {
    10681075                        return false;
    10691076                }
    10701077
    1071                 // Decide which link to show
    1072                 $is_subscribed = bbp_is_user_subscribed( $user_id, $topic_id );
    1073                 if ( !empty( $is_subscribed ) ) {
    1074                         $text       = $r['unsubscribe'];
    1075                         $query_args = array( 'action' => 'bbp_unsubscribe', 'topic_id' => $topic_id );
    1076                 } else {
    1077                         $text       = $r['subscribe'];
    1078                         $query_args = array( 'action' => 'bbp_subscribe', 'topic_id' => $topic_id );
    1079                 }
     1078                // Check if viewing a single forum
     1079                if( empty( $topic_id ) && ! empty( $forum_id ) ) {
    10801080
    1081                 // Create the link based where the user is and if the user is
    1082                 // subscribed already
    1083                 if ( bbp_is_subscriptions() ) {
    1084                         $permalink = bbp_get_subscriptions_permalink( $user_id );
    1085                 } elseif ( bbp_is_single_topic() || bbp_is_single_reply() ) {
    1086                         $permalink = bbp_get_topic_permalink( $topic_id );
     1081                        // Decide which link to show
     1082                        $is_subscribed = bbp_is_user_subscribed_to_forum( $user_id, $forum_id );
     1083                        if ( ! empty( $is_subscribed ) ) {
     1084                                $text       = $r['unsubscribe'];
     1085                                $query_args = array( 'action' => 'bbp_unsubscribe', 'forum_id' => $forum_id );
     1086                        } else {
     1087                                $text       = $r['subscribe'];
     1088                                $query_args = array( 'action' => 'bbp_subscribe', 'forum_id' => $forum_id );
     1089                        }
     1090
     1091                        // Create the link based where the user is and if the user is
     1092                        // subscribed already
     1093                        if ( bbp_is_subscriptions() ) {
     1094                                $permalink = bbp_get_subscriptions_permalink( $user_id );
     1095                        } elseif ( bbp_is_single_forum() || bbp_is_single_reply() ) {
     1096                                $permalink = bbp_get_forum_permalink( $forum_id );
     1097                        } else {
     1098                                $permalink = get_permalink();
     1099                        }
     1100
     1101                        $url  = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-subscription_' . $forum_id ) );
     1102                        $sub  = $is_subscribed ? ' class="is-subscribed"' : '';
     1103                        $html = sprintf( '%s<span id="subscribe-%d"  %s><a href="%s" class="subscription-toggle" data-forum="%d">%s</a></span>%s', $r['before'], $forum_id, $sub, $url, $forum_id, $text, $r['after'] );
     1104
     1105                        // Initial output is wrapped in a span, ajax output is hooked to this
     1106                        if ( !empty( $wrap ) ) {
     1107                                $html = '<span id="subscription-toggle">' . $html . '</span>';
     1108                        }
     1109
    10871110                } else {
    1088                         $permalink = get_permalink();
    1089                 }
    10901111
    1091                 $url  = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-subscription_' . $topic_id ) );
    1092                 $sub  = $is_subscribed ? ' class="is-subscribed"' : '';
    1093                 $html = sprintf( '%s<span id="subscribe-%d"  %s><a href="%s" class="subscription-toggle" data-topic="%d">%s</a></span>%s', $r['before'], $topic_id, $sub, $url, $topic_id, $text, $r['after'] );
     1112                        // Decide which link to show
     1113                        $is_subscribed = bbp_is_user_subscribed_to_topic( $user_id, $topic_id );
     1114                        if ( ! empty( $is_subscribed ) ) {
     1115                                $text       = $r['unsubscribe'];
     1116                                $query_args = array( 'action' => 'bbp_unsubscribe', 'topic_id' => $topic_id );
     1117                        } else {
     1118                                $text       = $r['subscribe'];
     1119                                $query_args = array( 'action' => 'bbp_subscribe', 'topic_id' => $topic_id );
     1120                        }
    10941121
    1095                 // Initial output is wrapped in a span, ajax output is hooked to this
    1096                 if ( !empty( $wrap ) ) {
    1097                         $html = '<span id="subscription-toggle">' . $html . '</span>';
     1122                        // Create the link based where the user is and if the user is
     1123                        // subscribed already
     1124                        if ( bbp_is_subscriptions() ) {
     1125                                $permalink = bbp_get_subscriptions_permalink( $user_id );
     1126                        } elseif ( bbp_is_single_topic() || bbp_is_single_reply() ) {
     1127                                $permalink = bbp_get_topic_permalink( $topic_id );
     1128                        } else {
     1129                                $permalink = get_permalink();
     1130                        }
     1131
     1132                        $url  = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-subscription_' . $topic_id ) );
     1133                        $sub  = $is_subscribed ? ' class="is-subscribed"' : '';
     1134                        $html = sprintf( '%s<span id="subscribe-%d"  %s><a href="%s" class="subscription-toggle" data-topic="%d">%s</a></span>%s', $r['before'], $topic_id, $sub, $url, $topic_id, $text, $r['after'] );
     1135
     1136                        // Initial output is wrapped in a span, ajax output is hooked to this
     1137                        if ( !empty( $wrap ) ) {
     1138                                $html = '<span id="subscription-toggle">' . $html . '</span>';
     1139                        }
     1140
    10981141                }
    10991142
    11001143                // Return the link
  • templates/default/bbpress/content-archive-forum.php

     
    2323
    2424        <?php bbp_breadcrumb(); ?>
    2525
     26        <?php bbp_forum_subscription_link(); ?>
     27
    2628        <?php do_action( 'bbp_template_before_forums_index' ); ?>
    2729
    2830        <?php if ( bbp_has_forums() ) : ?>
  • templates/default/bbpress/content-single-forum.php

     
    1313
    1414        <?php bbp_breadcrumb(); ?>
    1515
     16        <?php bbp_forum_subscription_link(); ?>
     17
    1618        <?php do_action( 'bbp_template_before_single_forum' ); ?>
    1719
    1820        <?php if ( post_password_required() ) : ?>
  • templates/default/bbpress-functions.php

     
    8484
    8585                /** Scripts ***********************************************************/
    8686
    87                 add_action( 'bbp_enqueue_scripts',   array( $this, 'enqueue_styles'        ) ); // Enqueue theme CSS
    88                 add_action( 'bbp_enqueue_scripts',   array( $this, 'enqueue_scripts'       ) ); // Enqueue theme JS
    89                 add_filter( 'bbp_enqueue_scripts',   array( $this, 'localize_topic_script' ) ); // Enqueue theme script localization
    90                 add_action( 'bbp_head',              array( $this, 'head_scripts'          ) ); // Output some extra JS in the <head>
    91                 add_action( 'bbp_ajax_favorite',     array( $this, 'ajax_favorite'         ) ); // Handles the ajax favorite/unfavorite
    92                 add_action( 'bbp_ajax_subscription', array( $this, 'ajax_subscription'     ) ); // Handles the ajax subscribe/unsubscribe
     87                add_action( 'bbp_enqueue_scripts',         array( $this, 'enqueue_styles'          ) ); // Enqueue theme CSS
     88                add_action( 'bbp_enqueue_scripts',         array( $this, 'enqueue_scripts'         ) ); // Enqueue theme JS
     89                add_filter( 'bbp_enqueue_scripts',         array( $this, 'localize_topic_script'   ) ); // Enqueue theme script localization
     90                add_action( 'bbp_head',                    array( $this, 'head_scripts'            ) ); // Output some extra JS in the <head>
     91                add_action( 'bbp_ajax_favorite',           array( $this, 'ajax_favorite'           ) ); // Handles the topic ajax favorite/unfavorite
     92                add_action( 'bbp_ajax_subscription',       array( $this, 'ajax_subscription'       ) ); // Handles the topic ajax subscribe/unsubscribe
     93                add_action( 'bbp_ajax_forum_subscription', array( $this, 'ajax_forum_subscription' ) ); // Handles the forum ajax subscribe/unsubscribe
    9394
    9495                /** Template Wrappers *************************************************/
    9596
     
    168169         *
    169170         * @since bbPress (r3732)
    170171         *
     172         * @uses bbp_is_single_forum() To check if it's the forum page
    171173         * @uses bbp_is_single_topic() To check if it's the topic page
     174         * @uses bbp_thread_replies() To check if threaded replies are enabled
    172175         * @uses bbp_is_single_user_edit() To check if it's the profile edit page
    173176         * @uses wp_enqueue_script() To enqueue the scripts
    174177         */
     
    179182                        wp_enqueue_script( 'jquery' );
    180183                }
    181184
     185                // Forum-specific scripts
     186                if ( bbp_is_single_forum() ) {
     187
     188                        // Forum subscribe/unsubscribe
     189                        wp_enqueue_script( 'bbpress-forum', $this->url . 'js/forum.js', array( 'jquery' ), $this->version );
     190                }
     191
    182192                // Topic-specific scripts
    183193                if ( bbp_is_single_topic() ) {
    184194
     
    285295         *
    286296         * @since bbPress (r3732)
    287297         *
     298         * @uses bbp_is_single_forum() To check if it's the forum page
    288299         * @uses bbp_is_single_topic() To check if it's the topic page
    289300         * @uses is_user_logged_in() To check if user is logged in
    290301         * @uses bbp_get_current_user_id() To get the current user id
     302         * @uses bbp_get_forum_id() To get the forum id
    291303         * @uses bbp_get_topic_id() To get the topic id
    292304         * @uses bbp_get_favorites_permalink() To get the favorites permalink
    293305         * @uses bbp_is_user_favorite() To check if the topic is in user's favorites
     
    298310         */
    299311        public function localize_topic_script() {
    300312
    301                 // Bail if not viewing a single topic
    302                 if ( !bbp_is_single_topic() )
    303                         return;
     313                if( bbp_is_single_forum() ) {
    304314
    305                 wp_localize_script( 'bbpress-topic', 'bbpTopicJS', array(
    306                         'bbp_ajaxurl'        => bbp_get_ajax_url(),
    307                         'generic_ajax_error' => __( 'Something went wrong. Refresh your browser and try again.', 'bbpress' ),
    308                         'is_user_logged_in'  => is_user_logged_in(),
    309                         'fav_nonce'          => wp_create_nonce( 'toggle-favorite_' .     get_the_ID() ),
    310                         'subs_nonce'         => wp_create_nonce( 'toggle-subscription_' . get_the_ID() )
    311                 ) );
     315                        wp_localize_script( 'bbpress-forum', 'bbpForumJS', array(
     316                                'bbp_ajaxurl'        => bbp_get_ajax_url(),
     317                                'generic_ajax_error' => __( 'Something went wrong. Refresh your browser and try again.', 'bbpress' ),
     318                                'is_user_logged_in'  => is_user_logged_in(),
     319                                'subs_nonce'         => wp_create_nonce( 'toggle-subscription_' . get_the_ID() )
     320                        ) );
     321                }
     322
     323                if ( bbp_is_single_topic() ) {
     324
     325                        wp_localize_script( 'bbpress-topic', 'bbpTopicJS', array(
     326                                'bbp_ajaxurl'        => bbp_get_ajax_url(),
     327                                'generic_ajax_error' => __( 'Something went wrong. Refresh your browser and try again.', 'bbpress' ),
     328                                'is_user_logged_in'  => is_user_logged_in(),
     329                                'fav_nonce'          => wp_create_nonce( 'toggle-favorite_' .     get_the_ID() ),
     330                                'subs_nonce'         => wp_create_nonce( 'toggle-subscription_' . get_the_ID() )
     331                        ) );
     332                }
    312333        }
    313334
    314335        /**
     336         * AJAX handler to Subscribe/Unsubscribe a user from a forum
     337         *
     338         * @since bbPress (r5155)
     339         *
     340         * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
     341         * @uses bbp_is_user_logged_in() To check if user is logged in
     342         * @uses bbp_get_current_user_id() To get the current user id
     343         * @uses current_user_can() To check if the current user can edit the user
     344         * @uses bbp_get_forum() To get the forum
     345         * @uses wp_verify_nonce() To verify the nonce
     346         * @uses bbp_is_user_subscribed() To check if the forum is in user's subscriptions
     347         * @uses bbp_remove_user_subscriptions() To remove the forum from user's subscriptions
     348         * @uses bbp_add_user_subscriptions() To add the forum from user's subscriptions
     349         * @uses bbp_ajax_response() To return JSON
     350         */
     351        public function ajax_forum_subscription() {
     352
     353                // Bail if subscriptions are not active
     354                if ( !bbp_is_subscriptions_active() ) {
     355                        bbp_ajax_response( false, __( 'Subscriptions are no longer active.', 'bbpress' ), 300 );
     356                }
     357
     358                // Bail if user is not logged in
     359                if ( !is_user_logged_in() ) {
     360                        bbp_ajax_response( false, __( 'Please login to subscribe to this forum.', 'bbpress' ), 301 );
     361                }
     362
     363                // Get user and forum data
     364                $user_id = bbp_get_current_user_id();
     365                $id      = intval( $_POST['id'] );
     366
     367                // Bail if user cannot add favorites for this user
     368                if ( !current_user_can( 'edit_user', $user_id ) ) {
     369                        bbp_ajax_response( false, __( 'You do not have permission to do this.', 'bbpress' ), 302 );
     370                }
     371
     372                // Get the forum
     373                $forum = bbp_get_forum( $id );
     374
     375                // Bail if forum cannot be found
     376                if ( empty( $forum ) ) {
     377                        bbp_ajax_response( false, __( 'The forum could not be found.', 'bbpress' ), 303 );
     378                }
     379
     380                // Bail if user did not take this action
     381                if ( !isset( $_POST['nonce'] ) || !wp_verify_nonce( $_POST['nonce'], 'toggle-subscription_' . $forum->ID ) ) {
     382                        bbp_ajax_response( false, __( 'Are you sure you meant to do that?', 'bbpress' ), 304 );
     383                }
     384
     385                // Take action
     386                $status = bbp_is_user_subscribed( $user_id, $forum->ID ) ? bbp_remove_user_subscription( $user_id, $forum->ID ) : bbp_add_user_subscription( $user_id, $forum->ID );
     387
     388                // Bail if action failed
     389                if ( empty( $status ) ) {
     390                        bbp_ajax_response( false, __( 'The request was unsuccessful. Please try again.', 'bbpress' ), 305 );
     391                }
     392
     393                // Put subscription attributes in convenient array
     394                $attrs = array(
     395                        'forum_id' => $forum->ID,
     396                        'user_id'  => $user_id
     397                );
     398
     399                // Action succeeded
     400                bbp_ajax_response( true, bbp_get_forum_subscription_link( $attrs, $user_id, false ), 200 );
     401        }
     402
     403        /**
    315404         * AJAX handler to add or remove a topic from a user's favorites
    316405         *
    317406         * @since bbPress (r3732)
    318407         *
     408         * @uses bbp_is_favorites_active() To check if favorites are active
     409         * @uses bbp_is_user_logged_in() To check if user is logged in
    319410         * @uses bbp_get_current_user_id() To get the current user id
    320411         * @uses current_user_can() To check if the current user can edit the user
    321412         * @uses bbp_get_topic() To get the topic
     
    383474         * @since bbPress (r3732)
    384475         *
    385476         * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
     477         * @uses bbp_is_user_logged_in() To check if user is logged in
    386478         * @uses bbp_get_current_user_id() To get the current user id
    387479         * @uses current_user_can() To check if the current user can edit the user
    388480         * @uses bbp_get_topic() To get the topic
  • templates/default/css/bbpress-rtl.css

     
    173173        margin: 8px 0;
    174174        padding: 0;
    175175        text-transform: none;
    176 } 
     176}
    177177
    178 #bbpress-forums div.bbp-forum-author, 
     178#bbpress-forums div.bbp-forum-author,
    179179#bbpress-forums div.bbp-topic-author,
    180180#bbpress-forums div.bbp-reply-author {
    181181        float: right;
     
    183183        width: 115px;
    184184}
    185185
    186 #bbpress-forums div.bbp-forum-author img.avatar, 
     186#bbpress-forums div.bbp-forum-author img.avatar,
    187187#bbpress-forums div.bbp-topic-author img.avatar,
    188188#bbpress-forums div.bbp-reply-author img.avatar {
    189189        border: none;
     
    250250        padding: 8px;
    251251}
    252252
    253 div.bbp-forum-header, 
     253div.bbp-forum-header,
    254254div.bbp-topic-header,
    255255div.bbp-reply-header {
    256256        border-top: 1px solid #ddd;
     
    398398        text-align: left;
    399399}
    400400
    401 /* =Breadcrumb and Tags
     401/* =Breadcrumb, Forum Subscription Link and Tags
    402402-------------------------------------------------------------- */
    403403
    404404div.bbp-breadcrumb {
     
    415415        margin-bottom: 10px
    416416}
    417417
     418#bbpress-forums div.bbp-forum-subscription {
     419        float: left;
     420}
     421
    418422#bbpress-forums div.bbp-topic-tags {
    419423        float: left;
    420424}
     
    473477        width: 50%;
    474478}
    475479
    476 .bbp-forum-header a.bbp-forum-permalink, 
     480.bbp-forum-header a.bbp-forum-permalink,
    477481.bbp-topic-header a.bbp-topic-permalink,
    478482.bbp-reply-header a.bbp-reply-permalink {
    479483        float: left;
     
    11811185                display: inline-block;
    11821186                margin-right: 0;
    11831187                word-wrap: break-word;
    1184         }       
     1188        }
    11851189        #bbpress-forums div.bbp-reply-author img.avatar {
    11861190                position: absolute;
    11871191                top: 15px;
     
    12041208        div.bbp-submit-wrapper {
    12051209                float: right;
    12061210        }
    1207         #bbpress-forums fieldset.bbp-form { 
     1211        #bbpress-forums fieldset.bbp-form {
    12081212                padding: 0 10px 10px;
    12091213        }
    12101214        #bbpress-forums #bbp-user-body {
     
    12991303        #bbpress-forums li.bbp-header li.bbp-topic-reply-count,
    13001304        #bbpress-forums li.bbp-body li.bbp-topic-voice-count,
    13011305        #bbpress-forums li.bbp-body li.bbp-topic-reply-count,
    1302         #bbpress-forums li.bbp-footer div.bbp-reply-author, 
     1306        #bbpress-forums li.bbp-footer div.bbp-reply-author,
    13031307        #bbpress-forums li.bbp-footer div.bbp-reply-content {
    13041308                width: 45%;
    13051309        }
     
    13131317                width: 100%;
    13141318        }
    13151319        #bbpress-forums li.bbp-header li.bbp-forum-freshness,
    1316         #bbpress-forums li.bbp-header li.bbp-topic-freshness { 
     1320        #bbpress-forums li.bbp-header li.bbp-topic-freshness {
    13171321                text-align: center;
    13181322        }
    13191323        #bbpress-forums li.bbp-body li.bbp-topic-freshness,
     
    13381342        #bbpress-forums div.bbp-reply-author img.avatar {
    13391343                width: 40px;
    13401344                height: auto;
    1341         }       
     1345        }
    13421346}
     1347 No newline at end of file
  • templates/default/css/bbpress.css

     
    398398        text-align: right;
    399399}
    400400
    401 /* =Breadcrumb and Tags
     401/* =Breadcrumb, Forum Subscription Link and Tags
    402402-------------------------------------------------------------- */
    403403
    404404div.bbp-breadcrumb {
     
    415415        margin-bottom: 10px
    416416}
    417417
     418#bbpress-forums div.bbp-forum-subscription {
     419        float: right;
     420}
     421
    418422#bbpress-forums div.bbp-topic-tags {
    419423        float: right;
    420424}