Skip to:
Content

bbPress.org

Ticket #2299: forum-subscriptions2.diff

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

    diff --git includes/common/functions.php includes/common/functions.php
    index 67f9dd7..d12586f 100644
    Login and visit the topic to unsubscribe from these emails.', 'bbpress' ), 
    11081108        return true;
    11091109}
    11101110
     1111/**
     1112 * Sends notification emails for new posts
     1113 *
     1114 * Gets new post's ID and check if there are subscribed users to that topic, and
     1115 * if there are, send notifications
     1116 *
     1117 * @since bbPress (r2668)
     1118 *
     1119 * @param int $reply_id ID of the newly made reply
     1120 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
     1121 * @uses bbp_get_reply_id() To validate the reply ID
     1122 * @uses bbp_get_reply() To get the reply
     1123 * @uses bbp_get_reply_topic_id() To get the topic ID of the reply
     1124 * @uses bbp_is_reply_published() To make sure the reply is published
     1125 * @uses bbp_get_topic_id() To validate the topic ID
     1126 * @uses bbp_get_topic() To get the reply's topic
     1127 * @uses bbp_is_topic_published() To make sure the topic is published
     1128 * @uses get_the_author_meta() To get the author's display name
     1129 * @uses do_action() Calls 'bbp_pre_notify_subscribers' with the reply id and
     1130 *                    topic id
     1131 * @uses bbp_get_topic_subscribers() To get the topic subscribers
     1132 * @uses apply_filters() Calls 'bbp_subscription_mail_message' with the
     1133 *                        message, reply id, topic id and user id
     1134 * @uses get_userdata() To get the user data
     1135 * @uses wp_mail() To send the mail
     1136 * @uses do_action() Calls 'bbp_post_notify_subscribers' with the reply id
     1137 *                    and topic id
     1138 * @return bool True on success, false on failure
     1139 */
     1140function bbp_notify_forum_subscribers( $topic_id = 0, $forum_id = 0, $anonymous_data = false, $topic_author = 0 ) {
     1141
     1142        // Bail if subscriptions are turned off
     1143        if ( !bbp_is_subscriptions_active() )
     1144                return false;
     1145
     1146        /** Validation ************************************************************/
     1147
     1148        $topic_id = bbp_get_topic_id( $topic_id );
     1149        $forum_id = bbp_get_forum_id( $forum_id );
     1150
     1151        /** Topic *****************************************************************/
     1152
     1153        // Bail if topic is not published
     1154        if ( ! bbp_is_topic_published( $topic_id ) )
     1155                return false;
     1156
     1157        /** User ******************************************************************/
     1158
     1159        // Get subscribers and bail if empty
     1160        $user_ids = bbp_get_forum_subscribers( $forum_id, true );
     1161        if ( empty( $user_ids ) )
     1162                return false;
     1163
     1164        // Poster name
     1165        $topic_author_name = bbp_get_topic_author_display_name( $topic_id );
     1166
     1167        /** Mail ******************************************************************/
     1168
     1169        do_action( 'bbp_pre_notify_forum_subscribers', $topic_id, $forum_id, $user_ids );
     1170
     1171        // Remove filters from reply content and topic title to prevent content
     1172        // from being encoded with HTML entities, wrapped in paragraph tags, etc...
     1173        remove_all_filters( 'bbp_get_topic_content' );
     1174        remove_all_filters( 'bbp_get_topic_title'   );
     1175
     1176        // Strip tags from text
     1177        $topic_title   = strip_tags( bbp_get_topic_title( $topic_id ) );
     1178        $topic_content = strip_tags( bbp_get_topic_content( $topic_id ) );
     1179        $topic_url     = get_permalink( $topic_id );
     1180        $blog_name     = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
     1181
     1182        // Loop through users
     1183        foreach ( (array) $user_ids as $user_id ) {
     1184
     1185                // Don't send notifications to the person who made the post
     1186                if ( !empty( $topic_author ) && (int) $user_id === (int) $topic_author )
     1187                        continue;
     1188
     1189                // For plugins to filter messages per reply/topic/user
     1190                $message = sprintf( __( '%1$s wrote:
     1191
     1192%2$s
     1193
     1194Topic Link: %3$s
     1195
     1196-----------
     1197
     1198You are receiving this email because you subscribed to a forum.
     1199
     1200Login and visit the topic to unsubscribe from these emails.', 'bbpress' ),
     1201
     1202                        $topic_author_name,
     1203                        $topic_content,
     1204                        $topic_url
     1205                );
     1206                $message = apply_filters( 'bbp_forum_subscription_mail_message', $message, $topic_id, $forum_id, $user_id );
     1207                if ( empty( $message ) )
     1208                        continue;
     1209
     1210                // For plugins to filter titles per reply/topic/user
     1211                $subject = apply_filters( 'bbp_forum_subscription_mail_title', '[' . $blog_name . '] ' . $topic_title, $topic_id, $forum_id, $user_id );
     1212                if ( empty( $subject ) )
     1213                        continue;
     1214
     1215                // Custom headers
     1216                $headers = apply_filters( 'bbp_forum_subscription_mail_headers', array() );
     1217
     1218                // Get user data of this user
     1219                $user = get_userdata( $user_id );
     1220
     1221                // Send notification email
     1222                wp_mail( $user->user_email, $subject, $message, $headers );
     1223        }
     1224
     1225        do_action( 'bbp_post_notify_forum_subscribers', $topic_id, $forum_id, $user_ids );
     1226
     1227        return true;
     1228}
     1229
    11111230/** Login *********************************************************************/
    11121231
    11131232/**
    function bbp_request_feed_trap( $query_vars = array() ) { 
    15311650
    15321651                // Forum/Topic/Reply Feed
    15331652                if ( isset( $query_vars['post_type'] ) ) {
    1534                    
     1653
    15351654                        // Supported select query vars
    15361655                        $select_query_vars = array(
    15371656                                'p'                      => false,
  • includes/common/template.php

    diff --git includes/common/template.php includes/common/template.php
    index c943158..65ae111 100644
    function bbp_breadcrumb( $args = array() ) { 
    23322332                return apply_filters( 'bbp_get_breadcrumb', $trail, $crumbs, $r );
    23332333        }
    23342334
     2335
     2336/** Forum Subscriptions ******************************************************/
     2337
     2338/**
     2339 * Output the forum subscription link
     2340 *
     2341 * @uses bbp_get_forum_subscription_link()
     2342 */
     2343function bbp_forum_subscription_link( $args = array() ) {
     2344        echo bbp_get_forum_subscription_link( $args );
     2345}
     2346
     2347        /**
     2348         * Get the forum subscription link
     2349         *
     2350         * @uses bbp_parse_args()
     2351         * @uses bbp_get_user_subscribe_link()
     2352         */
     2353        function bbp_get_forum_subscription_link( $args = array() ) {
     2354
     2355                // Parse args
     2356                $r = bbp_parse_args( $args, array(
     2357
     2358                        // Forum
     2359                        'forum_id'    => 0,
     2360
     2361                        // User
     2362                        'user_id'     => 0,
     2363
     2364                        // HTML
     2365                        'before'      => '<div class="bbp-forum-subscription"><p>',
     2366                        'after'       => '</p></div>',
     2367
     2368                        // Text
     2369                        'subscribe'   => __( 'Subscribe to Forum', 'bbpress' ),
     2370                        'unsubscribe' => __( 'Unsubscribe from Forum', 'bbpress' ),
     2371
     2372                ), 'get_forum_subscribe_link' );
     2373
     2374                $link = $r['before'];
     2375
     2376                $link .= bbp_get_user_subscribe_link( array(
     2377                        'before'      => '',
     2378                        'subscribe'   => $r['subscribe'],
     2379                        'unsubscribe' => $r['unsubscribe']
     2380                ) );
     2381
     2382                $link .= $r['after'];
     2383
     2384                return apply_filters( 'bbp_get_forum_subscribe_link', $link, $r );
     2385        }
     2386
    23352387/** Topic Tags ***************************************************************/
    23362388
    23372389/**
  • includes/core/actions.php

    diff --git includes/core/actions.php includes/core/actions.php
    index a698269..b870ae3 100644
    add_action( 'bbp_delete_topic', 'bbp_remove_topic_from_all_favorites' ); 
    224224// Subscriptions
    225225add_action( 'bbp_trash_topic',  'bbp_remove_topic_from_all_subscriptions'       );
    226226add_action( 'bbp_delete_topic', 'bbp_remove_topic_from_all_subscriptions'       );
    227 add_action( 'bbp_new_reply',    'bbp_notify_subscribers',                 11, 5 );
     227
     228// TODO add remove forum subscriptions for forums
     229
     230add_action( 'bbp_new_reply',    'bbp_notify_subscribers',       11, 5 );
     231add_action( 'bbp_new_topic',    'bbp_notify_forum_subscribers', 11, 4 );
    228232
    229233// Sticky
    230234add_action( 'bbp_trash_topic',  'bbp_unstick_topic' );
    add_action( 'bbp_get_request', 'bbp_toggle_topic_handler', 1 ); 
    301305add_action( 'bbp_get_request', 'bbp_toggle_reply_handler',    1  );
    302306add_action( 'bbp_get_request', 'bbp_favorites_handler',       1  );
    303307add_action( 'bbp_get_request', 'bbp_subscriptions_handler',   1  );
     308add_action( 'bbp_get_request', 'bbp_forum_subscriptions_handler',   1  );
    304309add_action( 'bbp_get_request', 'bbp_search_results_redirect', 10 );
    305310
    306311// Maybe convert the users password
  • includes/forums/functions.php

    diff --git includes/forums/functions.php includes/forums/functions.php
    index 5952ad5..137a3eb 100644
    function bbp_repair_forum_visibility() { 
    957957        return true;
    958958}
    959959
     960/** Subscriptions *************************************************/
     961
     962/**
     963 * Remove a deleted forum from all users' subscriptions
     964 *
     965 * @since bbPress (r2652)
     966 *
     967 * @param int $forum_id forum ID to remove
     968 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
     969 * @uses bbp_get_forum_subscribers() To get the forum subscribers
     970 * @uses bbp_remove_user_subscription() To remove the user subscription
     971 */
     972function bbp_remove_forum_from_all_subscriptions( $forum_id = 0 ) {
     973
     974        // Subscriptions are not active
     975        if ( ! bbp_is_subscriptions_active() )
     976                return;
     977
     978        $forum_id = bbp_get_forum_id( $forum_id );
     979
     980        // Bail if no forum
     981        if ( empty( $forum_id ) )
     982                return;
     983
     984        // Get users
     985        $users = (array) bbp_get_forum_subscribers( $forum_id );
     986
     987        // Users exist
     988        if ( !empty( $users ) ) {
     989
     990                // Loop through users
     991                foreach ( $users as $user ) {
     992
     993                        // Remove each user
     994                        bbp_remove_user_subscription( $user, $forum_id );
     995                }
     996        }
     997}
     998
    960999/** Count Bumpers *************************************************************/
    9611000
    9621001/**
  • includes/forums/template.php

    diff --git includes/forums/template.php includes/forums/template.php
    index 6f5a3aa..7d3524e 100644
    function bbp_forum_row_actions() { 
    385385}
    386386
    387387/**
     388 * Output checked value of topic subscription
     389 *
     390 * @since bbPress (r2976)
     391 *
     392 * @uses bbp_get_form_topic_subscribed() To get the subscribed checkbox value
     393 */
     394function bbp_form_forum_subscribed() {
     395        echo bbp_get_form_forum_subscribed();
     396}
     397        /**
     398         * Return checked value of forum subscription
     399         *
     400         * @since bbPress (r2976)
     401         *
     402         * @uses bbp_is_forum_edit() To check if it's the forum edit page
     403         * @uses bbp_is_user_subscribed() To check if the user is subscribed to
     404         *                                 the forum
     405         * @uses apply_filters() Calls 'bbp_get_form_forum_subscribed' with the
     406         *                        option
     407         * @return string Checked value of forum subscription
     408         */
     409        function bbp_get_form_forum_subscribed() {
     410
     411                // Get _POST data
     412                if ( bbp_is_post_request() && isset( $_POST['bbp_forum_subscription'] ) ) {
     413                        $forum_subscribed = (bool) $_POST['bbp_forum_subscription'];
     414
     415                // Get edit data
     416                } elseif ( bbp_is_forum_edit() || bbp_is_reply_edit() ) {
     417
     418                        // Get current posts author
     419                        $post_author = bbp_get_global_post_field( 'post_author', 'raw' );
     420
     421                        // Post author is not the current user
     422                        if ( bbp_get_current_user_id() !== $post_author ) {
     423                                $forum_subscribed = bbp_is_user_subscribed( $post_author );
     424
     425                        // Post author is the current user
     426                        } else {
     427                                $forum_subscribed = bbp_is_user_subscribed( bbp_get_current_user_id() );
     428                        }
     429
     430                // Get current status
     431                } elseif ( bbp_is_single_forum() ) {
     432                        $forum_subscribed = bbp_is_user_subscribed( bbp_get_current_user_id() );
     433
     434                // No data
     435                } else {
     436                        $forum_subscribed = false;
     437                }
     438
     439                // Get checked output
     440                $checked = checked( $forum_subscribed, true, false );
     441
     442                return apply_filters( 'bbp_get_form_forum_subscribed', $checked, $forum_subscribed );
     443        }
     444
     445/**
    388446 * Output the forums last active ID
    389447 *
    390448 * @since bbPress (r2860)
  • includes/users/functions.php

    diff --git includes/users/functions.php includes/users/functions.php
    index caa11de..7d1eb47 100644
    function bbp_is_user_favorite( $user_id = 0, $topic_id = 0 ) { 
    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 );
    function bbp_get_topic_subscribers( $topic_id = 0 ) { 
    539539}
    540540
    541541/**
     542 * Get the users who have subscribed to the forum
     543 *
     544 * @since bbPress (r2668)
     545 *
     546 * @param int $forum_id Optional. forum id
     547 * @uses wpdb::get_col() To execute our query and get the column back
     548 * @uses apply_filters() Calls 'bbp_get_forum_subscribers' with the subscribers
     549 * @return array|bool Results if the forum has any subscribers, otherwise false
     550 */
     551function bbp_get_forum_subscribers( $forum_id = 0 ) {
     552        $forum_id = bbp_get_forum_id( $forum_id );
     553        if ( empty( $forum_id ) )
     554                return;
     555
     556        global $wpdb;
     557
     558        $key   = $wpdb->prefix . '_bbp_forum_subscriptions';
     559        $users = wp_cache_get( 'bbp_get_forum_subscribers_' . $forum_id, 'bbpress_users' );
     560        if ( false === $users ) {
     561                $users = $wpdb->get_col( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = '{$key}' and FIND_IN_SET('{$forum_id}', meta_value) > 0" );
     562                wp_cache_set( 'bbp_get_forum_subscribers_' . $forum_id, $users, 'bbpress_users' );
     563        }
     564
     565        return apply_filters( 'bbp_get_forum_subscribers', $users );
     566}
     567
     568/**
    542569 * Get a user's subscribed topics
    543570 *
    544571 * @since bbPress (r2668)
    function bbp_get_user_subscribed_topic_ids( $user_id = 0 ) { 
    592619}
    593620
    594621/**
    595  * Check if a topic is in user's subscription list or not
     622 * Get a user's subscribed forum ids
    596623 *
    597624 * @since bbPress (r2668)
    598625 *
    599626 * @param int $user_id Optional. User id
     627 * @uses bbp_get_user_id() To get the user id
     628 * @uses get_user_option() To get the user's subscriptions
     629 * @uses apply_filters() Calls 'bbp_get_user_subscribed_forum_ids' with
     630 *                        the subscriptions and user id
     631 * @return array|bool Results if user has subscriptions, otherwise false
     632 */
     633function bbp_get_user_subscribed_forum_ids( $user_id = 0 ) {
     634        $user_id = bbp_get_user_id( $user_id );
     635        if ( empty( $user_id ) )
     636                return false;
     637
     638        $subscriptions = get_user_option( '_bbp_forum_subscriptions', $user_id );
     639        $subscriptions = array_filter( wp_parse_id_list( $subscriptions ) );
     640
     641        return (array) apply_filters( 'bbp_get_user_subscribed_forum_ids', $subscriptions, $user_id );
     642}
     643
     644/**
     645 * Check if a topic is in user's subscription list or not
     646 *
     647 * @param int $user_id Optional. User id
    600648 * @param int $topic_id Optional. Topic id
    601649 * @uses bbp_get_user_id() To get the user id
    602650 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions
    function bbp_get_user_subscribed_topic_ids( $user_id = 0 ) { 
    606654 *                        topic id and subsriptions
    607655 * @return bool True if the topic is in user's subscriptions, otherwise false
    608656 */
    609 function bbp_is_user_subscribed( $user_id = 0, $topic_id = 0 ) {
     657function bbp_is_user_subscribed_to_topic( $user_id = 0, $topic_id = 0 ) {
    610658
    611659        // Validate user
    612660        $user_id = bbp_get_user_id( $user_id, true, true );
    function bbp_is_user_subscribed( $user_id = 0, $topic_id = 0 ) { 
    616664        $retval        = false;
    617665        $subscriptions = bbp_get_user_subscribed_topic_ids( $user_id );
    618666
    619         if ( !empty( $subscriptions ) ) {
     667        if ( ! empty( $subscriptions ) ) {
    620668
    621669                // 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;
     670                if ( ! empty( $topic_id ) ) {
     671                        $topic    = bbp_get_topic( $topic_id );
     672                        $topic_id = ! empty( $topic ) ? $topic->ID : 0;
    625673
    626674                // Using the global topic id
    627675                } elseif ( bbp_get_topic_id() ) {
    function bbp_is_user_subscribed( $user_id = 0, $topic_id = 0 ) { 
    633681                }
    634682
    635683                // Is topic_id in the user's favorites
    636                 if ( !empty( $topic_id ) ) {
     684                if ( ! empty( $topic_id ) ) {
    637685                        $retval = in_array( $topic_id, $subscriptions );
    638686                }
    639687        }
    640688
    641         return (bool) apply_filters( 'bbp_is_user_subscribed', (bool) $retval, $user_id, $topic_id, $subscriptions );
     689        return (bool) apply_filters( 'bbp_is_user_subscribed_to_topic', (bool) $retval, $user_id, $topic_id, $subscriptions );
     690}
     691
     692/**
     693 * Check if a forum is in user's subscription list or not
     694 *
     695 * @since bbPress (r2668)
     696 *
     697 * @param int $user_id Optional. User id
     698 * @param int $forum_id Optional. Topic id
     699 * @uses bbp_get_user_id() To get the user id
     700 * @uses bbp_get_user_subscribed_forum_ids() To get the user's subscriptions
     701 * @uses bbp_get_forum() To get the forum
     702 * @uses bbp_get_forum_id() To get the forum id
     703 * @uses apply_filters() Calls 'bbp_is_user_subscribed' with the bool, user id,
     704 *                        forum id and subsriptions
     705 * @return bool True if the forum is in user's subscriptions, otherwise false
     706 */
     707function bbp_is_user_subscribed_to_forum( $user_id = 0, $forum_id = 0 ) {
     708
     709        // Validate user
     710        $user_id = bbp_get_user_id( $user_id, true, true );
     711        if ( empty( $user_id ) )
     712                return false;
     713
     714        $retval        = false;
     715        $subscriptions = bbp_get_user_subscribed_forum_ids( $user_id );
     716
     717        if ( ! empty( $subscriptions ) ) {
     718
     719                // Checking a specific forum id
     720                if ( ! empty( $forum_id ) ) {
     721                        $forum    = bbp_get_forum( $forum_id );
     722                        $forum_id = ! empty( $forum ) ? $forum->ID : 0;
     723
     724                // Using the global forum id
     725                } elseif ( bbp_get_forum_id() ) {
     726                        $forum_id = bbp_get_forum_id();
     727
     728                // Use the current post id
     729                } elseif ( ! bbp_get_forum_id() ) {
     730                        $forum_id = get_the_ID();
     731                }
     732
     733                // Is forum_id in the user's favorites
     734                if ( ! empty( $forum_id ) ) {
     735                        $retval = in_array( $forum_id, $subscriptions );
     736                }
     737        }
     738
     739        return (bool) apply_filters( 'bbp_is_user_subscribed_to_forum', (bool) $retval, $user_id, $forum_id, $subscriptions );
     740}
     741
     742
     743/**
     744 * Check if a user's subscription list or not
     745 *
     746 * @param int $user_id Optional. User id
     747 * @param int $forum_id Optional. Topic id
     748 * @uses get_post() To get the post object
     749 * @uses bbp_get_user_subscribed_forum_ids() To get the user's forum subscriptions
     750 * @uses bbp_get_user_subscribed_topic_ids() To get the user's topic subscriptions
     751 * @uses bbp_get_forum_post_type() To get the forum post type
     752 * @uses bbp_get_topic_post_type() To get the topic post type
     753 * @uses apply_filters() Calls 'bbp_is_user_subscribed' with the bool, user id,
     754 *                        forum/topic id and subsriptions
     755 * @return bool True if the forum or topic is in user's subscriptions, otherwise false
     756 */
     757function bbp_is_user_subscribed( $user_id = 0, $object_id = 0 ) {
     758        if ( empty( $user_id ) || empty( $object_id ) )
     759                return false;
     760
     761        $object = get_post( $object_id );
     762        if( ! $object )
     763                return false;
     764
     765        $post_type = $object->post_type;
     766
     767        switch( $post_type ) {
     768
     769                case bbp_get_forum_post_type() :
     770
     771                        $subscriptions = bbp_get_user_subscribed_forum_ids( $user_id );
     772                        $retval        = bbp_is_user_subscribed_to_forum( $user_id, $object_id );
     773                        break;
     774
     775                case bbp_get_topic_post_type() :
     776                default :
     777
     778                        $subscriptions = bbp_get_user_subscribed_topic_ids( $user_id );
     779                        $retval        = bbp_is_user_subscribed_to_topic( $user_id, $object_id );
     780                        break;
     781        }
     782
     783        return (bool) apply_filters( 'bbp_is_user_subscribed', $retval, $user_id, $object_id, $subscriptions );
    642784}
    643785
    644786/**
    function bbp_is_user_subscribed( $user_id = 0, $topic_id = 0 ) { 
    654796 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & topic id
    655797 * @return bool Always true
    656798 */
    657 function bbp_add_user_subscription( $user_id = 0, $topic_id = 0 ) {
     799function bbp_add_user_topic_subscription( $user_id = 0, $topic_id = 0 ) {
    658800        if ( empty( $user_id ) || empty( $topic_id ) )
    659801                return false;
    660802
    function bbp_add_user_subscription( $user_id = 0, $topic_id = 0 ) { 
    671813                wp_cache_delete( 'bbp_get_topic_subscribers_' . $topic_id, 'bbpress_users' );
    672814        }
    673815
    674         do_action( 'bbp_add_user_subscription', $user_id, $topic_id );
     816        do_action( 'bbp_add_user_topic_subscription', $user_id, $topic_id );
    675817
    676818        return true;
    677819}
    678820
    679821/**
    680  * Remove a topic from user's subscriptions
     822 * Add a forum to user's subscriptions
     823 *
     824 * @param int $user_id Optional. User id
     825 * @param int $forum_id Optional. forum id
     826 * @uses bbp_get_user_subscribed_forum_ids() To get the user's subscriptions
     827 * @uses bbp_get_forum() To get the forum
     828 * @uses update_user_option() To update the user's subscriptions
     829 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & forum id
     830 * @return bool Always true
     831 */
     832function bbp_add_user_forum_subscription( $user_id = 0, $forum_id = 0 ) {
     833        if ( empty( $user_id ) || empty( $forum_id ) )
     834                return false;
     835
     836        $forum = bbp_get_forum( $forum_id );
     837        if ( empty( $forum ) )
     838                return false;
     839
     840        $subscriptions = (array) bbp_get_user_subscribed_forum_ids( $user_id );
     841        if ( !in_array( $forum_id, $subscriptions ) ) {
     842                $subscriptions[] = $forum_id;
     843                $subscriptions   = implode( ',', wp_parse_id_list( array_filter( $subscriptions ) ) );
     844                update_user_option( $user_id, '_bbp_forum_subscriptions', $subscriptions );
     845
     846                wp_cache_delete( 'bbp_get_forum_subscribers_' . $forum_id, 'bbpress_users' );
     847        }
     848
     849        do_action( 'bbp_add_user_forum_subscription', $user_id, $forum_id );
     850
     851        return true;
     852}
     853
     854/**
     855 * Add a topic to user's subscriptions
    681856 *
    682857 * @since bbPress (r2668)
    683858 *
    684859 * @param int $user_id Optional. User id
    685860 * @param int $topic_id Optional. Topic id
     861 * @uses get_post() To get the post object
     862 * @uses bbp_get_user_subscribed_forum_ids() To get the user's forum subscriptions
     863 * @uses bbp_get_user_subscribed_topic_ids() To get the user's topic subscriptions
     864 * @uses bbp_get_forum_post_type() To get the forum post type
     865 * @uses bbp_get_topic_post_type() To get the topic post type
     866 * @uses update_user_option() To update the user's subscriptions
     867 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & topic id
     868 * @return bool Always true
     869 */
     870function bbp_add_user_subscription( $user_id = 0, $object_id = 0 ) {
     871        if ( empty( $user_id ) || empty( $object_id ) )
     872                return false;
     873
     874        $object = get_post( $object_id );
     875        if( ! $object )
     876                return false;
     877
     878        $post_type = $object->post_type;
     879
     880        switch( $post_type ) {
     881
     882                case bbp_get_forum_post_type() :
     883
     884                        $subscriptions = bbp_get_user_subscribed_forum_ids( $user_id );
     885
     886                        if ( ! in_array( $object_id, $subscriptions ) ) {
     887                                $subscriptions[] = $object_id;
     888                                $subscriptions   = implode( ',', wp_parse_id_list( array_filter( $subscriptions ) ) );
     889                                update_user_option( $user_id, '_bbp_forum_subscriptions', $subscriptions );
     890
     891                                wp_cache_delete( 'bbp_get_forum_subscribers_' . $object_id, 'bbpress_users' );
     892                        }
     893
     894                        break;
     895
     896                case bbp_get_topic_post_type() :
     897                default :
     898
     899                        $subscriptions = bbp_get_user_subscribed_topic_ids( $user_id );
     900
     901                        if ( ! in_array( $object_id, $subscriptions ) ) {
     902                                $subscriptions[] = $object_id;
     903                                $subscriptions   = implode( ',', wp_parse_id_list( array_filter( $subscriptions ) ) );
     904                                update_user_option( $user_id, '_bbp_subscriptions', $subscriptions );
     905
     906                                wp_cache_delete( 'bbp_get_topic_subscribers_' . $object_id, 'bbpress_users' );
     907                        }
     908
     909                        break;
     910        }
     911
     912        do_action( 'bbp_add_user_subscription', $user_id, $object_id, $post_type );
     913
     914        return true;
     915}
     916
     917/**
     918 * Remove a topic from user's subscriptions
     919 *
     920 * @param int $user_id Optional. User id
     921 * @param int $topic_id Optional. Topic id
    686922 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions
    687923 * @uses update_user_option() To update the user's subscriptions
    688924 * @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
     925 * @uses do_action() Calls 'bbp_remove_user_topic_subscription' with the user id and
    690926 *                    topic id
    691927 * @return bool True if the topic was removed from user's subscriptions,
    692928 *               otherwise false
    693929 */
    694 function bbp_remove_user_subscription( $user_id, $topic_id ) {
     930function bbp_remove_user_topic_subscription( $user_id, $topic_id ) {
    695931        if ( empty( $user_id ) || empty( $topic_id ) )
    696932                return false;
    697933
    function bbp_remove_user_subscription( $user_id, $topic_id ) { 
    715951                wp_cache_delete( 'bbp_get_topic_subscribers_' . $topic_id, 'bbpress_users' );
    716952        }
    717953
    718         do_action( 'bbp_remove_user_subscription', $user_id, $topic_id );
     954        do_action( 'bbp_remove_user_topic_subscription', $user_id, $topic_id );
     955
     956        return true;
     957}
     958
     959/**
     960 * Remove a forum from user's subscriptions
     961 *
     962 * @param int $user_id Optional. User id
     963 * @param int $forum_id Optional. forum id
     964 * @uses bbp_get_user_subscribed_forum_ids() To get the user's subscriptions
     965 * @uses update_user_option() To update the user's subscriptions
     966 * @uses delete_user_option() To delete the user's subscriptions meta
     967 * @uses do_action() Calls 'bbp_remove_user_subscription' with the user id and
     968 *                    forum id
     969 * @return bool True if the forum was removed from user's subscriptions,
     970 *               otherwise false
     971 */
     972function bbp_remove_user_forum_subscription( $user_id, $forum_id ) {
     973        if ( empty( $user_id ) || empty( $forum_id ) )
     974                return false;
     975
     976        $subscriptions = (array) bbp_get_user_subscribed_forum_ids( $user_id );
     977
     978        if ( empty( $subscriptions ) )
     979                return false;
     980
     981        $pos = array_search( $forum_id, $subscriptions );
     982        if ( is_numeric( $pos ) ) {
     983                array_splice( $subscriptions, $pos, 1 );
     984                $subscriptions = array_filter( $subscriptions );
     985
     986                if ( !empty( $subscriptions ) ) {
     987                        $subscriptions = implode( ',', wp_parse_id_list( $subscriptions ) );
     988                        update_user_option( $user_id, '_bbp_forum_subscriptions', $subscriptions );
     989                } else {
     990                        delete_user_option( $user_id, '_bbp_forum_subscriptions' );
     991                }
     992
     993                wp_cache_delete( 'bbp_get_forum_subscribers_' . $forum_id, 'bbpress_users' );
     994        }
     995
     996        do_action( 'bbp_remove_user_forum_subscription', $user_id, $forum_id );
     997
     998        return true;
     999}
     1000
     1001/**
     1002 * Remove a topic from user's subscriptions
     1003 *
     1004 * @since bbPress (r2668)
     1005 *
     1006 * @param int $user_id Optional. User id
     1007 * @param int $topic_id Optional. Topic id
     1008 * @uses get_post() To get the post object
     1009 * @uses bbp_get_forum_post_type() To get the forum post type
     1010 * @uses bbp_get_topic_post_type() To get the topic post type
     1011 * @uses bbp_remove_user_forum_subscription() To remove the user's subscription
     1012 * @uses bbp_remove_user_topic_subscription() To remove the user's subscription
     1013 * @uses do_action() Calls 'bbp_remove_user_subscription' with the user id and
     1014 *                    topic id
     1015 * @return bool True if the topic was removed from user's subscriptions,
     1016 *               otherwise false
     1017 */
     1018function bbp_remove_user_subscription( $user_id = 0, $object_id = 0 ) {
     1019        if ( empty( $user_id ) || empty( $object_id ) )
     1020                return false;
     1021
     1022        $post_type = get_post_type( $object_id );
     1023
     1024        switch( $post_type ) {
     1025
     1026                case bbp_get_forum_post_type() :
     1027
     1028                        bbp_remove_user_forum_subscription( $user_id, $object_id );
     1029
     1030                        break;
     1031
     1032                case bbp_get_topic_post_type() :
     1033                default :
     1034
     1035                        bbp_remove_user_topic_subscription( $user_id, $object_id );
     1036
     1037                        break;
     1038        }
     1039
     1040        do_action( 'bbp_remove_user_subscription', $user_id, $object_id, $post_type );
    7191041
    7201042        return true;
    7211043}
    function bbp_subscriptions_handler( $action = '' ) { 
    8221144        }
    8231145}
    8241146
     1147/**
     1148 * Handles the front end subscribing and unsubscribing forums
     1149 *
     1150 * @param string $action The requested action to compare this function to
     1151 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
     1152 * @uses bbp_get_user_id() To get the user id
     1153 * @uses bbp_verify_nonce_request() To verify the nonce and check the request
     1154 * @uses current_user_can() To check if the current user can edit the user
     1155 * @uses bbPress:errors:add() To log the error messages
     1156 * @uses bbp_is_user_subscribed() To check if the forum is in user's
     1157 *                                 subscriptions
     1158 * @uses bbp_remove_user_subscription() To remove the user subscription
     1159 * @uses bbp_add_user_subscription() To add the user subscription
     1160 * @uses do_action() Calls 'bbp_subscriptions_handler' with success, user id,
     1161 *                    forum id and action
     1162 * @uses bbp_is_subscription() To check if it's the subscription page
     1163 * @uses bbp_get_subscription_link() To get the subscription page link
     1164 * @uses bbp_get_forum_permalink() To get the forum permalink
     1165 * @uses wp_safe_redirect() To redirect to the url
     1166 */
     1167function bbp_forum_subscriptions_handler( $action = '' ) {
     1168
     1169        if ( !bbp_is_subscriptions_active() )
     1170                return false;
     1171
     1172        // Bail if no forum ID is passed
     1173        if ( empty( $_GET['forum_id'] ) )
     1174                return;
     1175
     1176        // Setup possible get actions
     1177        $possible_actions = array(
     1178                'bbp_subscribe',
     1179                'bbp_unsubscribe',
     1180        );
     1181
     1182        // Bail if actions aren't meant for this function
     1183        if ( !in_array( $action, $possible_actions ) )
     1184                return;
     1185
     1186        // Get required data
     1187        $user_id  = bbp_get_user_id( 0, true, true );
     1188        $forum_id = intval( $_GET['forum_id'] );
     1189
     1190        // Check for empty forum
     1191        if ( empty( $forum_id ) ) {
     1192                bbp_add_error( 'bbp_subscription_forum_id', __( '<strong>ERROR</strong>: No forum was found! Which forum are you subscribing/unsubscribing to?', 'bbpress' ) );
     1193
     1194        // Check nonce
     1195        } elseif ( ! bbp_verify_nonce_request( 'toggle-subscription_' . $forum_id ) ) {
     1196                bbp_add_error( 'bbp_subscription_forum_id', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
     1197
     1198        // Check current user's ability to edit the user
     1199        } elseif ( !current_user_can( 'edit_user', $user_id ) ) {
     1200                bbp_add_error( 'bbp_subscription_permissions', __( '<strong>ERROR</strong>: You don\'t have the permission to edit favorites of that user!', 'bbpress' ) );
     1201        }
     1202
     1203        // Bail if we have errors
     1204        if ( bbp_has_errors() )
     1205                return;
     1206
     1207        /** No errors *************************************************************/
     1208
     1209        $is_subscription = bbp_is_user_subscribed( $user_id, $forum_id );
     1210        $success         = false;
     1211
     1212        if ( true === $is_subscription && 'bbp_unsubscribe' === $action )
     1213                $success = bbp_remove_user_subscription( $user_id, $forum_id );
     1214        elseif ( false === $is_subscription && 'bbp_subscribe' === $action )
     1215                $success = bbp_add_user_subscription( $user_id, $forum_id );
     1216
     1217        // Do additional subscriptions actions
     1218        do_action( 'bbp_subscriptions_handler', $success, $user_id, $forum_id, $action );
     1219
     1220        // Success!
     1221        if ( true === $success ) {
     1222
     1223                // Redirect back from whence we came
     1224                if ( bbp_is_subscriptions() ) {
     1225                        $redirect = bbp_get_subscriptions_permalink( $user_id );
     1226                } elseif ( bbp_is_single_user() ) {
     1227                        $redirect = bbp_get_user_profile_url();
     1228                } elseif ( is_singular( bbp_get_forum_post_type() ) ) {
     1229                        $redirect = bbp_get_forum_permalink( $forum_id );
     1230                } elseif ( is_single() || is_page() ) {
     1231                        $redirect = get_permalink();
     1232                } else {
     1233                        $redirect = get_permalink( $forum_id );
     1234                }
     1235
     1236                wp_safe_redirect( $redirect );
     1237
     1238                // For good measure
     1239                exit();
     1240
     1241        // Fail! Handle errors
     1242        } elseif ( true === $is_subscription && 'bbp_unsubscribe' === $action ) {
     1243                bbp_add_error( 'bbp_unsubscribe', __( '<strong>ERROR</strong>: There was a problem unsubscribing from that forum!', 'bbpress' ) );
     1244        } elseif ( false === $is_subscription && 'bbp_subscribe' === $action ) {
     1245                bbp_add_error( 'bbp_subscribe',    __( '<strong>ERROR</strong>: There was a problem subscribing to that forum!', 'bbpress' ) );
     1246        }
     1247}
     1248
    8251249/** Edit **********************************************************************/
    8261250
    8271251/**
    function bbp_user_edit_after() { 
    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 ) )
    function bbp_get_user_topics_started( $user_id = 0 ) { 
    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 ) )
    function bbp_get_total_users() { 
    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

    diff --git includes/users/template.php includes/users/template.php
    index 6ccf1ef..41ea205 100644
    function bbp_user_subscribe_link( $args = '', $user_id = 0, $wrap = true ) { 
    874874         * @return string Permanent link to topic
    875875         */
    876876        function bbp_get_user_subscribe_link( $args = '', $user_id = 0, $wrap = true ) {
    877                 if ( !bbp_is_subscriptions_active() )
     877                if ( ! bbp_is_subscriptions_active() )
    878878                        return;
    879879
    880880                // Parse arguments against default values
    function bbp_user_subscribe_link( $args = '', $user_id = 0, $wrap = true ) { 
    883883                        'unsubscribe' => __( 'Unsubscribe', 'bbpress' ),
    884884                        'user_id'     => 0,
    885885                        'topic_id'    => 0,
     886                        'forum_id'    => 0,
    886887                        'before'      => '&nbsp;|&nbsp;',
    887888                        'after'       => ''
    888889                ), 'get_user_subscribe_link' );
    889890
    890                 // Validate user and topic ID's
    891                 $user_id  = bbp_get_user_id( $r['user_id'], true, true );
    892                 $topic_id = bbp_get_topic_id( $r['topic_id'] );
    893                 if ( empty( $user_id ) || empty( $topic_id ) ) {
     891                // Validate user and object ID's
     892                $user_id   = bbp_get_user_id( $r['user_id'], true, true );
     893                $topic_id  = bbp_get_topic_id( $r['topic_id'] );
     894                $forum_id  = bbp_get_forum_id( $r['forum_id'] );
     895                if ( empty( $user_id ) || ( empty( $topic_id ) && empty( $forum_id ) ) ) {
    894896                        return false;
    895897                }
    896898
    897899                // No link if you can't edit yourself
    898                 if ( !current_user_can( 'edit_user', (int) $user_id ) ) {
     900                if ( ! current_user_can( 'edit_user', (int) $user_id ) ) {
    899901                        return false;
    900902                }
    901903
    902                 // Decide which link to show
    903                 $is_subscribed = bbp_is_user_subscribed( $user_id, $topic_id );
    904                 if ( !empty( $is_subscribed ) ) {
    905                         $text       = $r['unsubscribe'];
    906                         $query_args = array( 'action' => 'bbp_unsubscribe', 'topic_id' => $topic_id );
    907                 } else {
    908                         $text       = $r['subscribe'];
    909                         $query_args = array( 'action' => 'bbp_subscribe', 'topic_id' => $topic_id );
    910                 }
     904                // Check if viewing a single forum
     905                if( empty( $topic_id ) && ! empty( $forum_id ) ) {
     906
     907                        // Decide which link to show
     908                        $is_subscribed = bbp_is_user_subscribed_to_forum( $user_id, $forum_id );
     909                        if ( ! empty( $is_subscribed ) ) {
     910                                $text       = $r['unsubscribe'];
     911                                $query_args = array( 'action' => 'bbp_unsubscribe', 'forum_id' => $forum_id );
     912                        } else {
     913                                $text       = $r['subscribe'];
     914                                $query_args = array( 'action' => 'bbp_subscribe', 'forum_id' => $forum_id );
     915                        }
     916
     917                        // Create the link based where the user is and if the user is
     918                        // subscribed already
     919                        if ( bbp_is_subscriptions() ) {
     920                                $permalink = bbp_get_subscriptions_permalink( $user_id );
     921                        } elseif ( bbp_is_single_forum() || bbp_is_single_reply() ) {
     922                                $permalink = bbp_get_forum_permalink( $forum_id );
     923                        } else {
     924                                $permalink = get_permalink();
     925                        }
     926
     927                        $url  = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-subscription_' . $forum_id ) );
     928                        $sub  = $is_subscribed ? ' class="is-subscribed"' : '';
     929                        $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'] );
     930
     931                        // Initial output is wrapped in a span, ajax output is hooked to this
     932                        if ( !empty( $wrap ) ) {
     933                                $html = '<span id="subscription-toggle">' . $html . '</span>';
     934                        }
     935
    911936
    912                 // Create the link based where the user is and if the user is
    913                 // subscribed already
    914                 if ( bbp_is_subscriptions() ) {
    915                         $permalink = bbp_get_subscriptions_permalink( $user_id );
    916                 } elseif ( bbp_is_single_topic() || bbp_is_single_reply() ) {
    917                         $permalink = bbp_get_topic_permalink( $topic_id );
    918937                } else {
    919                         $permalink = get_permalink();
    920                 }
    921938
    922                 $url  = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-subscription_' . $topic_id ) );
    923                 $sub  = $is_subscribed ? ' class="is-subscribed"' : '';
    924                 $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'] );
     939                        // Decide which link to show
     940                        $is_subscribed = bbp_is_user_subscribed_to_topic( $user_id, $topic_id );
     941                        if ( ! empty( $is_subscribed ) ) {
     942                                $text       = $r['unsubscribe'];
     943                                $query_args = array( 'action' => 'bbp_unsubscribe', 'topic_id' => $topic_id );
     944                        } else {
     945                                $text       = $r['subscribe'];
     946                                $query_args = array( 'action' => 'bbp_subscribe', 'topic_id' => $topic_id );
     947                        }
     948
     949                        // Create the link based where the user is and if the user is
     950                        // subscribed already
     951                        if ( bbp_is_subscriptions() ) {
     952                                $permalink = bbp_get_subscriptions_permalink( $user_id );
     953                        } elseif ( bbp_is_single_topic() || bbp_is_single_reply() ) {
     954                                $permalink = bbp_get_topic_permalink( $topic_id );
     955                        } else {
     956                                $permalink = get_permalink();
     957                        }
     958
     959                        $url  = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-subscription_' . $topic_id ) );
     960                        $sub  = $is_subscribed ? ' class="is-subscribed"' : '';
     961                        $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'] );
     962
     963                        // Initial output is wrapped in a span, ajax output is hooked to this
     964                        if ( !empty( $wrap ) ) {
     965                                $html = '<span id="subscription-toggle">' . $html . '</span>';
     966                        }
    925967
    926                 // Initial output is wrapped in a span, ajax output is hooked to this
    927                 if ( !empty( $wrap ) ) {
    928                         $html = '<span id="subscription-toggle">' . $html . '</span>';
    929968                }
    930969
    931970                // Return the link
  • templates/default/bbpress-functions.php

    diff --git templates/default/bbpress-functions.php templates/default/bbpress-functions.php
    index d216947..a31e7c5 100644
    class BBP_Default extends BBP_Theme_Compat { 
    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 ajax favorite/unfavorite
     92                add_action( 'bbp_ajax_subscription',       array( $this, 'ajax_subscription'       ) ); // Handles the ajax subscribe/unsubscribe
     93                add_action( 'bbp_ajax_forum_subscription', array( $this, 'ajax_forum_subscription' ) ); // Handles the ajax subscribe/unsubscribe
    9394
    9495                /** Template Wrappers *************************************************/
    9596
    class BBP_Default extends BBP_Theme_Compat { 
    191192                        }
    192193                }
    193194
     195                // Forum-specific scripts
     196                if ( bbp_is_single_forum() ) {
     197
     198                        // Forum subscribe/unsubscribe
     199                        wp_enqueue_script( 'bbpress-forum', $this->url . 'js/forum.js', array( 'jquery' ), $this->version );
     200
     201                }
     202
    194203                // User Profile edit
    195204                if ( bbp_is_single_user_edit() ) {
    196205                        wp_enqueue_script( 'user-profile' );
    class BBP_Default extends BBP_Theme_Compat { 
    299308        public function localize_topic_script() {
    300309
    301310                // Bail if not viewing a single topic
    302                 if ( !bbp_is_single_topic() )
    303                         return;
    304 
    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                 ) );
     311                if ( bbp_is_single_topic() ) {
     312
     313                        wp_localize_script( 'bbpress-topic', 'bbpTopicJS', array(
     314                                'bbp_ajaxurl'        => bbp_get_ajax_url(),
     315                                'generic_ajax_error' => __( 'Something went wrong. Refresh your browser and try again.', 'bbpress' ),
     316                                'is_user_logged_in'  => is_user_logged_in(),
     317                                'fav_nonce'          => wp_create_nonce( 'toggle-favorite_' .     get_the_ID() ),
     318                                'subs_nonce'         => wp_create_nonce( 'toggle-subscription_' . get_the_ID() )
     319                        ) );
     320
     321                }
     322
     323                if( bbp_is_single_forum() ) {
     324
     325                        wp_localize_script( 'bbpress-forum', 'bbpForumJS', 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                                'subs_nonce'         => wp_create_nonce( 'toggle-subscription_' . get_the_ID() )
     330                        ) );
     331
     332                }
    312333        }
    313334
    314335        /**
    class BBP_Default extends BBP_Theme_Compat { 
    443464                // Action succeeded
    444465                bbp_ajax_response( true, bbp_get_user_subscribe_link( $attrs, $user_id, false ), 200 );
    445466        }
     467
     468        /**
     469         * AJAX handler to Subscribe/Unsubscribe a user from a forum
     470         *
     471         * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
     472         * @uses bbp_get_current_user_id() To get the current user id
     473         * @uses current_user_can() To check if the current user can edit the user
     474         * @uses bbp_get_forum() To get the forum
     475         * @uses wp_verify_nonce() To verify the nonce
     476         * @uses bbp_is_user_subscribed() To check if the forum is in user's subscriptions
     477         * @uses bbp_remove_user_subscriptions() To remove the forum from user's subscriptions
     478         * @uses bbp_add_user_subscriptions() To add the forum from user's subscriptions
     479         * @uses bbp_ajax_response() To return JSON
     480         */
     481        public function ajax_forum_subscription() {
     482
     483                // Bail if subscriptions are not active
     484                if ( !bbp_is_subscriptions_active() ) {
     485                        bbp_ajax_response( false, __( 'Subscriptions are no longer active.', 'bbpress' ), 300 );
     486                }
     487
     488                // Bail if user is not logged in
     489                if ( !is_user_logged_in() ) {
     490                        bbp_ajax_response( false, __( 'Please login to subscribe to this forum.', 'bbpress' ), 301 );
     491                }
     492
     493                // Get user and forum data
     494                $user_id = bbp_get_current_user_id();
     495                $id      = intval( $_POST['id'] );
     496
     497                // Bail if user cannot add favorites for this user
     498                if ( !current_user_can( 'edit_user', $user_id ) ) {
     499                        bbp_ajax_response( false, __( 'You do not have permission to do this.', 'bbpress' ), 302 );
     500                }
     501
     502                // Get the forum
     503                $forum = bbp_get_forum( $id );
     504
     505                // Bail if forum cannot be found
     506                if ( empty( $forum ) ) {
     507                        bbp_ajax_response( false, __( 'The forum could not be found.', 'bbpress' ), 303 );
     508                }
     509
     510                // Bail if user did not take this action
     511                if ( !isset( $_POST['nonce'] ) || !wp_verify_nonce( $_POST['nonce'], 'toggle-subscription_' . $forum->ID ) ) {
     512                        bbp_ajax_response( false, __( 'Are you sure you meant to do that?', 'bbpress' ), 304 );
     513                }
     514
     515                // Take action
     516                $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 );
     517
     518                // Bail if action failed
     519                if ( empty( $status ) ) {
     520                        bbp_ajax_response( false, __( 'The request was unsuccessful. Please try again.', 'bbpress' ), 305 );
     521                }
     522
     523                // Put subscription attributes in convenient array
     524                $attrs = array(
     525                        'forum_id' => $forum->ID,
     526                        'user_id'  => $user_id
     527                );
     528
     529                // Action succeeded
     530                bbp_ajax_response( true, bbp_get_forum_subscription_link( $attrs, $user_id, false ), 200 );
     531        }
    446532}
    447533new BBP_Default();
    448534endif;
  • templates/default/bbpress/content-archive-forum.php

    diff --git templates/default/bbpress/content-archive-forum.php templates/default/bbpress/content-archive-forum.php
    index bb17ac8..e9f628c 100644
     
    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

    diff --git templates/default/bbpress/content-single-forum.php templates/default/bbpress/content-single-forum.php
    index c2ecbb0..3f7057d 100644
     
    1212<div id="bbpress-forums">
    1313
    1414        <?php bbp_breadcrumb(); ?>
     15        <?php bbp_forum_subscription_link(); ?>
    1516
    1617        <?php do_action( 'bbp_template_before_single_forum' ); ?>
    1718
  • templates/default/css/bbpress.css

    diff --git templates/default/css/bbpress.css templates/default/css/bbpress.css
    index 34caeb8..ad82380 100644
    span.bbp-author-ip { 
    397397        text-align: right;
    398398}
    399399
    400 /* =Breadcrumb and Tags
     400/* =Breadcrumb, Forum Subscription Link and Tags
    401401-------------------------------------------------------------- */
    402402
    403403div.bbp-breadcrumb {
    div.bbp-topic-tags { 
    414414        margin-bottom: 10px
    415415}
    416416
     417#bbpress-forums div.bbp-forum-subscription {
     418        float: right;
     419}
     420
    417421#bbpress-forums div.bbp-topic-tags {
    418422        float: right;
    419423}
  • templates/default/js/topic.js

    diff --git templates/default/js/topic.js templates/default/js/topic.js
    index 725e87c..a3f08c4 100644
    jQuery( document ).ready( function ( $ ) { 
    2828                e.preventDefault();
    2929                bbp_ajax_call( 'subscription', $( this ).attr( 'data-topic' ), bbpTopicJS.subs_nonce, '#subscription-toggle' );
    3030        } );
     31
    3132} );