Skip to:
Content

bbPress.org

Ticket #2299: forum-subscriptions.3.diff

File forum-subscriptions.3.diff, 70.4 KB (added by netweb, 10 years ago)
  • includes/admin/tools.php

     
    158158 */
    159159function bbp_admin_repair_list() {
    160160        $repair_list = array(
    161                 0  => array( 'bbp-sync-topic-meta',        __( 'Recalculate the parent topic for each post',        'bbpress' ), 'bbp_admin_repair_topic_meta'               ),
    162                 5  => array( 'bbp-sync-forum-meta',        __( 'Recalculate the parent forum for each post',        'bbpress' ), 'bbp_admin_repair_forum_meta'               ),
    163                 10 => array( 'bbp-sync-forum-visibility',  __( 'Recalculate private and hidden forums',             'bbpress' ), 'bbp_admin_repair_forum_visibility'         ),
    164                 15 => array( 'bbp-sync-all-topics-forums', __( 'Recalculate last activity in each topic and forum', 'bbpress' ), 'bbp_admin_repair_freshness'                ),
    165                 20 => array( 'bbp-sync-all-topics-sticky', __( 'Recalculate the sticky relationship of each topic', 'bbpress' ), 'bbp_admin_repair_sticky'                   ),
    166                 25 => array( 'bbp-group-forums',           __( 'Repair BuddyPress Group Forum relationships',       'bbpress' ), 'bbp_admin_repair_group_forum_relationship' ),
    167                 30 => array( 'bbp-forum-topics',           __( 'Count topics in each forum',                        'bbpress' ), 'bbp_admin_repair_forum_topic_count'        ),
    168                 35 => array( 'bbp-forum-replies',          __( 'Count replies in each forum',                       'bbpress' ), 'bbp_admin_repair_forum_reply_count'        ),
    169                 40 => array( 'bbp-topic-replies',          __( 'Count replies in each topic',                       'bbpress' ), 'bbp_admin_repair_topic_reply_count'        ),
    170                 45 => array( 'bbp-topic-voices',           __( 'Count voices in each topic',                        'bbpress' ), 'bbp_admin_repair_topic_voice_count'        ),
    171                 50 => array( 'bbp-topic-hidden-replies',   __( 'Count spammed & trashed replies in each topic',     'bbpress' ), 'bbp_admin_repair_topic_hidden_reply_count' ),
    172                 55 => array( 'bbp-user-topics',            __( 'Count topics for each user',                        'bbpress' ), 'bbp_admin_repair_user_topic_count'         ),
    173                 60 => array( 'bbp-user-replies',           __( 'Count replies for each user',                       'bbpress' ), 'bbp_admin_repair_user_reply_count'         ),
    174                 65 => array( 'bbp-user-favorites',         __( 'Remove trashed topics from user favorites',         'bbpress' ), 'bbp_admin_repair_user_favorites'           ),
    175                 70 => array( 'bbp-user-subscriptions',     __( 'Remove trashed topics from user subscriptions',     'bbpress' ), 'bbp_admin_repair_user_subscriptions'       ),
    176                 75 => array( 'bbp-user-role-map',          __( 'Remap existing users to default forum roles',       'bbpress' ), 'bbp_admin_repair_user_roles'               )
     161                0  => array( 'bbp-sync-topic-meta',          __( 'Recalculate the parent topic for each post',        'bbpress' ), 'bbp_admin_repair_topic_meta'               ),
     162                5  => array( 'bbp-sync-forum-meta',          __( 'Recalculate the parent forum for each post',        'bbpress' ), 'bbp_admin_repair_forum_meta'               ),
     163                10 => array( 'bbp-sync-forum-visibility',    __( 'Recalculate private and hidden forums',             'bbpress' ), 'bbp_admin_repair_forum_visibility'         ),
     164                15 => array( 'bbp-sync-all-topics-forums',   __( 'Recalculate last activity in each topic and forum', 'bbpress' ), 'bbp_admin_repair_freshness'                ),
     165                20 => array( 'bbp-sync-all-topics-sticky',   __( 'Recalculate the sticky relationship of each topic', 'bbpress' ), 'bbp_admin_repair_sticky'                   ),
     166                25 => array( 'bbp-group-forums',             __( 'Repair BuddyPress Group Forum relationships',       'bbpress' ), 'bbp_admin_repair_group_forum_relationship' ),
     167                30 => array( 'bbp-forum-topics',             __( 'Count topics in each forum',                        'bbpress' ), 'bbp_admin_repair_forum_topic_count'        ),
     168                35 => array( 'bbp-forum-replies',            __( 'Count replies in each forum',                       'bbpress' ), 'bbp_admin_repair_forum_reply_count'        ),
     169                40 => array( 'bbp-topic-replies',            __( 'Count replies in each topic',                       'bbpress' ), 'bbp_admin_repair_topic_reply_count'        ),
     170                45 => array( 'bbp-topic-voices',             __( 'Count voices in each topic',                        'bbpress' ), 'bbp_admin_repair_topic_voice_count'        ),
     171                50 => array( 'bbp-topic-hidden-replies',     __( 'Count spammed & trashed replies in each topic',     'bbpress' ), 'bbp_admin_repair_topic_hidden_reply_count' ),
     172                55 => array( 'bbp-user-topics',              __( 'Count topics for each user',                        'bbpress' ), 'bbp_admin_repair_user_topic_count'         ),
     173                60 => array( 'bbp-user-replies',             __( 'Count replies for each user',                       'bbpress' ), 'bbp_admin_repair_user_reply_count'         ),
     174                65 => array( 'bbp-user-favorites',           __( 'Remove trashed topics from user favorites',         'bbpress' ), 'bbp_admin_repair_user_favorites'           ),
     175                70 => array( 'bbp-user-subscriptions',       __( 'Remove trashed topics from user subscriptions',     'bbpress' ), 'bbp_admin_repair_user_subscriptions'       ),
     176                75 => array( 'bbp-user-forum-subscriptions', __( 'Remove trashed forums from user subscriptions',     'bbpress' ), 'bbp_admin_repair_user_forum_subscriptions' ),
     177                80 => array( 'bbp-user-role-map',            __( 'Remap existing users to default forum roles',       'bbpress' ), 'bbp_admin_repair_user_roles'               )
    177178        );
    178179        ksort( $repair_list );
    179180
     
    646647}
    647648
    648649/**
    649  * Clean the users' subscriptions
     650 * Clean the users' topic subscriptions
    650651 *
    651652 * @since bbPress (r2668)
    652653 *
     
    707708}
    708709
    709710/**
     711 * Clean the users' forum subscriptions
     712 *
     713 * @since bbPress (r5155)
     714 *
     715 * @uses bbp_get_forum_post_type() To get the topic post type
     716 * @uses wpdb::query() To run our recount sql queries
     717 * @uses is_wp_error() To check if the executed query returned {@link WP_Error}
     718 * @return array An array of the status code and the message
     719 */
     720function bbp_admin_repair_user_forum_subscriptions() {
     721        global $wpdb;
     722
     723        $statement = __( 'Removing trashed forums from user subscriptions… %s', 'bbpress' );
     724        $result    = __( 'Failed!', 'bbpress' );
     725        $key       = $wpdb->prefix . '_bbp_forum_subscriptions';
     726        $users     = $wpdb->get_results( "SELECT `user_id`, `meta_value` AS `subscriptions` FROM `{$wpdb->usermeta}` WHERE `meta_key` = '{$key}';" );
     727
     728        if ( is_wp_error( $users ) )
     729                return array( 1, sprintf( $statement, $result ) );
     730
     731        $forums = $wpdb->get_col( "SELECT `ID` FROM `{$wpdb->posts}` WHERE `post_type` = '" . bbp_get_forum_post_type() . "' AND `post_status` = '" . bbp_get_public_status_id() . "';" );
     732        if ( is_wp_error( $forums ) )
     733                return array( 2, sprintf( $statement, $result ) );
     734
     735        $values = array();
     736        foreach ( $users as $user ) {
     737                if ( empty( $user->subscriptions ) || !is_string( $user->subscriptions ) )
     738                        continue;
     739
     740                $subscriptions = array_intersect( $forums, explode( ',', $user->subscriptions ) );
     741                if ( empty( $subscriptions ) || !is_array( $subscriptions ) )
     742                        continue;
     743
     744                $subscriptions_joined = implode( ',', $subscriptions );
     745                $values[]             = "('{$user->user_id}', '{$key}', '{$subscriptions_joined}')";
     746
     747                // Cleanup
     748                unset( $subscriptions, $subscriptions_joined );
     749        }
     750
     751        if ( !count( $values ) ) {
     752                $result = __( 'Nothing to remove!', 'bbpress' );
     753                return array( 0, sprintf( $statement, $result ) );
     754        }
     755
     756        $sql_delete = "DELETE FROM `{$wpdb->usermeta}` WHERE `meta_key` = '{$key}';";
     757        if ( is_wp_error( $wpdb->query( $sql_delete ) ) )
     758                return array( 4, sprintf( $statement, $result ) );
     759
     760        foreach ( array_chunk( $values, 10000 ) as $chunk ) {
     761                $chunk = "\n" . implode( ",\n", $chunk );
     762                $sql_insert = "INSERT INTO `{$wpdb->usermeta}` (`user_id`, `meta_key`, `meta_value`) VALUES $chunk;";
     763                if ( is_wp_error( $wpdb->query( $sql_insert ) ) ) {
     764                        return array( 5, sprintf( $statement, $result ) );
     765                }
     766        }
     767
     768        return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
     769}
     770
     771/**
    710772 * This repair tool will map each user of the current site to their respective
    711773 * forums role. By default, Admins will be Key Masters, and every other role
    712774 * will be the default role defined in Settings > Forums (Participant).
  • 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

     
    163163 * Return the raw database count of topics by a user
    164164 *
    165165 * @since bbPress (r3633)
     166 *
    166167 * @global WPDB $wpdb
    167168 * @uses bbp_get_user_id()
    168169 * @uses get_posts_by_author_sql()
     
    187188 * Return the raw database count of replies by a user
    188189 *
    189190 * @since bbPress (r3633)
     191 *
    190192 * @global WPDB $wpdb
    191193 * @uses bbp_get_user_id()
    192194 * @uses get_posts_by_author_sql()
     
    313315        $favorites = bbp_get_user_favorites_topic_ids( $user_id );
    314316
    315317        if ( !empty( $favorites ) ) {
    316                
     318
    317319                // Checking a specific topic id
    318320                if ( !empty( $topic_id ) ) {
    319321                        $topic    = bbp_get_topic( $topic_id );
     
    412414/**
    413415 * Handles the front end adding and removing of favorite topics
    414416 *
     417 * @since bbPress (rXXXX)
     418 *
    415419 * @param string $action The requested action to compare this function to
    416420 * @uses bbp_get_user_id() To get the user id
    417421 * @uses bbp_verify_nonce_request() To verify the nonce and check the request
     
    512516/** Subscriptions *************************************************************/
    513517
    514518/**
     519 * Get the users who have subscribed to the forum
     520 *
     521 * @since bbPress (r5155)
     522 *
     523 * @param int $forum_id Optional. forum id
     524 * @global WPDB $wpdb
     525 * @uses bbp_get_forum_id() To get the forum id
     526 * @uses wpdb::get_col() To execute our query and get the column back
     527 * @uses apply_filters() Calls 'bbp_get_forum_subscribers' with the subscribers
     528 * @return array|bool Results if the forum has any subscribers, otherwise false
     529 */
     530function bbp_get_forum_subscribers( $forum_id = 0 ) {
     531        $forum_id = bbp_get_forum_id( $forum_id );
     532        if ( empty( $forum_id ) )
     533                return;
     534
     535        global $wpdb;
     536
     537        $key   = $wpdb->prefix . '_bbp_forum_subscriptions';
     538        $users = wp_cache_get( 'bbp_get_forum_subscribers_' . $forum_id, 'bbpress_users' );
     539        if ( false === $users ) {
     540                $users = $wpdb->get_col( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = '{$key}' and FIND_IN_SET('{$forum_id}', meta_value) > 0" );
     541                wp_cache_set( 'bbp_get_forum_subscribers_' . $forum_id, $users, 'bbpress_users' );
     542        }
     543
     544        return apply_filters( 'bbp_get_forum_subscribers', $users );
     545}
     546
     547/**
    515548 * Get the users who have subscribed to the topic
    516549 *
    517550 * @since bbPress (r2668)
    518551 *
    519552 * @param int $topic_id Optional. Topic id
     553 * @global WPDB $wpdb
    520554 * @uses wpdb::get_col() To execute our query and get the column back
     555 * @uses bbp_get_topic_id() To get the topic id
    521556 * @uses apply_filters() Calls 'bbp_get_topic_subscribers' with the subscribers
    522557 * @return array|bool Results if the topic has any subscribers, otherwise false
    523558 */
     
    547582 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions
    548583 * @uses bbp_has_topics() To get the topics
    549584 * @uses apply_filters() Calls 'bbp_get_user_subscriptions' with the topic query
    550  *                        and user id
     585 *                    and user id
    551586 * @return array|bool Results if user has subscriptions, otherwise false
    552587 */
    553588function bbp_get_user_subscriptions( $user_id = 0 ) {
     
    569604}
    570605
    571606/**
     607 * Get a user's subscribed forum ids
     608 *
     609 * @since bbPress (r5155)
     610 *
     611 * @param int $user_id Optional. User id
     612 * @uses bbp_get_user_id() To get the user id
     613 * @uses get_user_option() To get the user's subscriptions
     614 * @uses apply_filters() Calls 'bbp_get_user_subscribed_forum_ids' with
     615 *                    the subscriptions and user id
     616 * @return array|bool Results if user has subscriptions, otherwise false
     617 */
     618function bbp_get_user_subscribed_forum_ids( $user_id = 0 ) {
     619        $user_id = bbp_get_user_id( $user_id );
     620        if ( empty( $user_id ) )
     621                return false;
     622
     623        $subscriptions = get_user_option( '_bbp_forum_subscriptions', $user_id );
     624        $subscriptions = array_filter( wp_parse_id_list( $subscriptions ) );
     625
     626        return (array) apply_filters( 'bbp_get_user_subscribed_forum_ids', $subscriptions, $user_id );
     627}
     628
     629/**
    572630 * Get a user's subscribed topics' ids
    573631 *
    574632 * @since bbPress (r2668)
     
    577635 * @uses bbp_get_user_id() To get the user id
    578636 * @uses get_user_option() To get the user's subscriptions
    579637 * @uses apply_filters() Calls 'bbp_get_user_subscribed_topic_ids' with
    580  *                        the subscriptions and user id
     638 *                    the subscriptions and user id
    581639 * @return array|bool Results if user has subscriptions, otherwise false
    582640 */
    583641function bbp_get_user_subscribed_topic_ids( $user_id = 0 ) {
     
    592650}
    593651
    594652/**
     653 * Check if in a user's subscription list or not
     654 *
     655 * @since bbPress (r2688)
     656 *
     657 * @param int $user_id Optional. User id
     658 * @param int $object_id Optional. Forum or Topic id
     659 * @uses get_post() To get the post object
     660 * @uses bbp_get_forum_post_type() To get the forum post type
     661 * @uses bbp_get_user_subscribed_forum_ids() To get the user's forum subscriptions
     662 * @uses bbp_get_topic_post_type() To get the topic post type
     663 * @uses bbp_get_user_subscribed_topic_ids() To get the user's topic subscriptions
     664 * @uses apply_filters() Calls 'bbp_is_user_subscribed' with the bool, user id,
     665 *                        forum/topic id and subsriptions
     666 * @return bool True if the forum or topic is in user's subscriptions, otherwise false
     667 */
     668function bbp_is_user_subscribed( $user_id = 0, $object_id = 0 ) {
     669        if ( empty( $user_id ) || empty( $object_id ) )
     670                return false;
     671
     672        $object = get_post( $object_id );
     673        if( ! $object )
     674                return false;
     675
     676        $post_type = $object->post_type;
     677
     678        switch( $post_type ) {
     679
     680                case bbp_get_forum_post_type() :
     681
     682                        $subscriptions = bbp_get_user_subscribed_forum_ids( $user_id );
     683                        $retval        = bbp_is_user_subscribed_to_forum( $user_id, $object_id );
     684                        break;
     685
     686                case bbp_get_topic_post_type() :
     687                default :
     688
     689                        $subscriptions = bbp_get_user_subscribed_topic_ids( $user_id );
     690                        $retval        = bbp_is_user_subscribed_to_topic( $user_id, $object_id );
     691                        break;
     692        }
     693
     694        return (bool) apply_filters( 'bbp_is_user_subscribed', $retval, $user_id, $object_id, $subscriptions );
     695}
     696
     697/**
     698 * Check if a forum is in user's subscription list or not
     699 *
     700 * @since bbPress (r5155)
     701 *
     702 * @param int $user_id Optional. User id
     703 * @param int $forum_id Optional. Forum id
     704 * @uses bbp_get_user_id() To get the user id
     705 * @uses bbp_get_user_subscribed_forum_ids() To get the user's subscriptions
     706 * @uses bbp_get_forum() To get the forum
     707 * @uses bbp_get_forum_id() To get the forum id
     708 * @uses apply_filters() Calls 'bbp_is_user_subscribed_to_forum' with the bool,
     709 *                    user id, forum id and subsriptions
     710 * @return bool True if the forum is in user's subscriptions, otherwise false
     711 */
     712function bbp_is_user_subscribed_to_forum( $user_id = 0, $forum_id = 0 ) {
     713
     714        // Validate user
     715        $user_id = bbp_get_user_id( $user_id, true, true );
     716        if ( empty( $user_id ) )
     717                return false;
     718
     719        $retval        = false;
     720        $subscriptions = bbp_get_user_subscribed_forum_ids( $user_id );
     721
     722        if ( ! empty( $subscriptions ) ) {
     723
     724                // Checking a specific forum id
     725                if ( ! empty( $forum_id ) ) {
     726                        $forum    = bbp_get_forum( $forum_id );
     727                        $forum_id = ! empty( $forum ) ? $forum->ID : 0;
     728
     729                // Using the global forum id
     730                } elseif ( bbp_get_forum_id() ) {
     731                        $forum_id = bbp_get_forum_id();
     732
     733                // Use the current post id
     734                } elseif ( ! bbp_get_forum_id() ) {
     735                        $forum_id = get_the_ID();
     736                }
     737
     738                // Is forum_id in the user's favorites
     739                if ( ! empty( $forum_id ) ) {
     740                        $retval = in_array( $forum_id, $subscriptions );
     741                }
     742        }
     743
     744        return (bool) apply_filters( 'bbp_is_user_subscribed_to_forum', (bool) $retval, $user_id, $forum_id, $subscriptions );
     745}
     746
     747/**
    595748 * Check if a topic is in user's subscription list or not
    596749 *
    597750 * @since bbPress (r2668)
     
    602755 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions
    603756 * @uses bbp_get_topic() To get the topic
    604757 * @uses bbp_get_topic_id() To get the topic id
    605  * @uses apply_filters() Calls 'bbp_is_user_subscribed' with the bool, user id,
    606  *                        topic id and subsriptions
     758 * @uses apply_filters() Calls 'bbp_is_user_subscribed_to_topic' with the bool,
     759 *                    user id, topic id and subsriptions
    607760 * @return bool True if the topic is in user's subscriptions, otherwise false
    608761 */
    609 function bbp_is_user_subscribed( $user_id = 0, $topic_id = 0 ) {
     762function bbp_is_user_subscribed_to_topic( $user_id = 0, $topic_id = 0 ) {
    610763
    611764        // Validate user
    612765        $user_id = bbp_get_user_id( $user_id, true, true );
     
    616769        $retval        = false;
    617770        $subscriptions = bbp_get_user_subscribed_topic_ids( $user_id );
    618771
    619         if ( !empty( $subscriptions ) ) {
     772        if ( ! empty( $subscriptions ) ) {
    620773
    621774                // 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;
     775                if ( ! empty( $topic_id ) ) {
     776                        $topic    = bbp_get_topic( $topic_id );
     777                        $topic_id = ! empty( $topic ) ? $topic->ID : 0;
    625778
    626779                // Using the global topic id
    627780                } elseif ( bbp_get_topic_id() ) {
     
    633786                }
    634787
    635788                // Is topic_id in the user's favorites
    636                 if ( !empty( $topic_id ) ) {
     789                if ( ! empty( $topic_id ) ) {
    637790                        $retval = in_array( $topic_id, $subscriptions );
    638791                }
    639792        }
    640793
    641         return (bool) apply_filters( 'bbp_is_user_subscribed', (bool) $retval, $user_id, $topic_id, $subscriptions );
     794        return (bool) apply_filters( 'bbp_is_user_subscribed_to_topic', (bool) $retval, $user_id, $topic_id, $subscriptions );
    642795}
    643796
    644797/**
     798 * Add to user's subscriptions
     799 *
     800 * @since bbPress (r2668)
     801 *
     802 * @param int $user_id Optional. User id
     803 * @param int $object_id Optional. Forum or Topic id
     804 * @uses get_post() To get the post object
     805 * @uses bbp_get_forum_post_type() To get the forum post type
     806 * @uses bbp_get_user_subscribed_forum_ids() To get the user's forum subscriptions
     807 * @uses bbp_get_topic_post_type() To get the topic post type
     808 * @uses bbp_get_user_subscribed_topic_ids() To get the user's topic subscriptions
     809 * @uses update_user_option() To update the user's subscriptions
     810 * @uses do_action() Calls 'bbp_add_user_subscription' with the user,
     811 *                    Forum/Topic id and post type
     812 * @return bool Always true
     813 */
     814function bbp_add_user_subscription( $user_id = 0, $object_id = 0 ) {
     815        if ( empty( $user_id ) || empty( $object_id ) )
     816                return false;
     817
     818        $object = get_post( $object_id );
     819        if( ! $object )
     820                return false;
     821
     822        $post_type = $object->post_type;
     823
     824        switch( $post_type ) {
     825
     826                case bbp_get_forum_post_type() :
     827
     828                        $subscriptions = bbp_get_user_subscribed_forum_ids( $user_id );
     829
     830                        if ( ! in_array( $object_id, $subscriptions ) ) {
     831                                $subscriptions[] = $object_id;
     832                                $subscriptions   = implode( ',', wp_parse_id_list( array_filter( $subscriptions ) ) );
     833                                update_user_option( $user_id, '_bbp_forum_subscriptions', $subscriptions );
     834
     835                                wp_cache_delete( 'bbp_get_forum_subscribers_' . $object_id, 'bbpress_users' );
     836                        }
     837
     838                        break;
     839
     840                case bbp_get_topic_post_type() :
     841                default :
     842
     843                        $subscriptions = bbp_get_user_subscribed_topic_ids( $user_id );
     844
     845                        if ( ! in_array( $object_id, $subscriptions ) ) {
     846                                $subscriptions[] = $object_id;
     847                                $subscriptions   = implode( ',', wp_parse_id_list( array_filter( $subscriptions ) ) );
     848                                update_user_option( $user_id, '_bbp_subscriptions', $subscriptions );
     849
     850                                wp_cache_delete( 'bbp_get_topic_subscribers_' . $object_id, 'bbpress_users' );
     851                        }
     852
     853                        break;
     854        }
     855
     856        do_action( 'bbp_add_user_subscription', $user_id, $object_id, $post_type );
     857
     858        return true;
     859}
     860
     861/**
     862 * Add a forum to user's subscriptions
     863 *
     864 * @since bbPress (r5155)
     865 *
     866 * @param int $user_id Optional. User id
     867 * @param int $forum_id Optional. forum id
     868 * @uses bbp_get_forum() To get the forum
     869 * @uses bbp_get_user_subscribed_forum_ids() To get the user's subscriptions
     870 * @uses update_user_option() To update the user's subscriptions
     871 * @uses do_action() Calls 'bbp_add_user_forum_subscription' with the user & forum id
     872 * @return bool Always true
     873 */
     874function bbp_add_user_forum_subscription( $user_id = 0, $forum_id = 0 ) {
     875        if ( empty( $user_id ) || empty( $forum_id ) )
     876                return false;
     877
     878        $forum = bbp_get_forum( $forum_id );
     879        if ( empty( $forum ) )
     880                return false;
     881
     882        $subscriptions = (array) bbp_get_user_subscribed_forum_ids( $user_id );
     883        if ( !in_array( $forum_id, $subscriptions ) ) {
     884                $subscriptions[] = $forum_id;
     885                $subscriptions   = implode( ',', wp_parse_id_list( array_filter( $subscriptions ) ) );
     886                update_user_option( $user_id, '_bbp_forum_subscriptions', $subscriptions );
     887
     888                wp_cache_delete( 'bbp_get_forum_subscribers_' . $forum_id, 'bbpress_users' );
     889        }
     890
     891        do_action( 'bbp_add_user_forum_subscription', $user_id, $forum_id );
     892
     893        return true;
     894}
     895
     896/**
    645897 * Add a topic to user's subscriptions
    646898 *
    647899 * @since bbPress (r2668)
     
    648900 *
    649901 * @param int $user_id Optional. User id
    650902 * @param int $topic_id Optional. Topic id
     903 * @uses bbp_get_topic() To get the topic
    651904 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions
    652  * @uses bbp_get_topic() To get the topic
    653905 * @uses update_user_option() To update the user's subscriptions
    654  * @uses do_action() Calls 'bbp_add_user_subscription' with the user & topic id
     906 * @uses do_action() Calls 'bbp_add_user_topic_subscription' with the user & topic id
    655907 * @return bool Always true
    656908 */
    657 function bbp_add_user_subscription( $user_id = 0, $topic_id = 0 ) {
     909function bbp_add_user_topic_subscription( $user_id = 0, $topic_id = 0 ) {
    658910        if ( empty( $user_id ) || empty( $topic_id ) )
    659911                return false;
    660912
     
    671923                wp_cache_delete( 'bbp_get_topic_subscribers_' . $topic_id, 'bbpress_users' );
    672924        }
    673925
    674         do_action( 'bbp_add_user_subscription', $user_id, $topic_id );
     926        do_action( 'bbp_add_user_topic_subscription', $user_id, $topic_id );
    675927
    676928        return true;
    677929}
    678930
    679931/**
     932 * Remove from user's subscriptions
     933 *
     934 * @since bbPress (r2668)
     935 *
     936 * @param int $user_id Optional. User id
     937 * @param int $object_id Optional. Forum or Topic id
     938 * @uses get_post_type() To get the post type
     939 * @uses bbp_get_forum_post_type() To get the forum post type
     940 * @uses bbp_get_topic_post_type() To get the topic post type
     941 * @uses bbp_remove_user_forum_subscription() To remove the user's subscription
     942 * @uses bbp_remove_user_topic_subscription() To remove the user's subscription
     943 * @uses do_action() Calls 'bbp_remove_user_subscription' with the user id and
     944 *                    forum/topic id
     945 * @return bool True if the topic was removed from user's subscriptions,
     946 *               otherwise false
     947 */
     948function bbp_remove_user_subscription( $user_id = 0, $object_id = 0 ) {
     949        if ( empty( $user_id ) || empty( $object_id ) )
     950                return false;
     951
     952        $post_type = get_post_type( $object_id );
     953
     954        switch( $post_type ) {
     955
     956                case bbp_get_forum_post_type() :
     957
     958                        bbp_remove_user_forum_subscription( $user_id, $object_id );
     959
     960                        break;
     961
     962                case bbp_get_topic_post_type() :
     963                default :
     964
     965                        bbp_remove_user_topic_subscription( $user_id, $object_id );
     966
     967                        break;
     968        }
     969
     970        do_action( 'bbp_remove_user_subscription', $user_id, $object_id, $post_type );
     971
     972        return true;
     973}
     974
     975/**
     976 * Remove a forum from user's subscriptions
     977 *
     978 * @since bbPress (r5155)
     979 *
     980 * @param int $user_id Optional. User id
     981 * @param int $forum_id Optional. forum id
     982 * @uses bbp_get_user_subscribed_forum_ids() To get the user's subscriptions
     983 * @uses update_user_option() To update the user's subscriptions
     984 * @uses delete_user_option() To delete the user's subscriptions meta
     985 * @uses do_action() Calls 'bbp_remove_user_forum_subscription' with the user id and
     986 *                    forum id
     987 * @return bool True if the forum was removed from user's subscriptions,
     988 *               otherwise false
     989 */
     990function bbp_remove_user_forum_subscription( $user_id, $forum_id ) {
     991        if ( empty( $user_id ) || empty( $forum_id ) )
     992                return false;
     993
     994        $subscriptions = (array) bbp_get_user_subscribed_forum_ids( $user_id );
     995
     996        if ( empty( $subscriptions ) )
     997                return false;
     998
     999        $pos = array_search( $forum_id, $subscriptions );
     1000        if ( is_numeric( $pos ) ) {
     1001                array_splice( $subscriptions, $pos, 1 );
     1002                $subscriptions = array_filter( $subscriptions );
     1003
     1004                if ( !empty( $subscriptions ) ) {
     1005                        $subscriptions = implode( ',', wp_parse_id_list( $subscriptions ) );
     1006                        update_user_option( $user_id, '_bbp_forum_subscriptions', $subscriptions );
     1007                } else {
     1008                        delete_user_option( $user_id, '_bbp_forum_subscriptions' );
     1009                }
     1010
     1011                wp_cache_delete( 'bbp_get_forum_subscribers_' . $forum_id, 'bbpress_users' );
     1012        }
     1013
     1014        do_action( 'bbp_remove_user_forum_subscription', $user_id, $forum_id );
     1015
     1016        return true;
     1017}
     1018
     1019/**
    6801020 * Remove a topic from user's subscriptions
    6811021 *
    6821022 * @since bbPress (r2668)
     
    6861026 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions
    6871027 * @uses update_user_option() To update the user's subscriptions
    6881028 * @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
     1029 * @uses do_action() Calls 'bbp_remove_user_topic_subscription' with the user id and
    6901030 *                    topic id
    6911031 * @return bool True if the topic was removed from user's subscriptions,
    6921032 *               otherwise false
    6931033 */
    694 function bbp_remove_user_subscription( $user_id, $topic_id ) {
     1034function bbp_remove_user_topic_subscription( $user_id, $topic_id ) {
    6951035        if ( empty( $user_id ) || empty( $topic_id ) )
    6961036                return false;
    6971037
     
    7151055                wp_cache_delete( 'bbp_get_topic_subscribers_' . $topic_id, 'bbpress_users' );
    7161056        }
    7171057
    718         do_action( 'bbp_remove_user_subscription', $user_id, $topic_id );
     1058        do_action( 'bbp_remove_user_topic_subscription', $user_id, $topic_id );
    7191059
    7201060        return true;
    7211061}
    7221062
    7231063/**
     1064 * Handles the front end subscribing and unsubscribing forums
     1065 *
     1066 * @since bbPress (r5155)
     1067 *
     1068 * @param string $action The requested action to compare this function to
     1069 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
     1070 * @uses bbp_get_user_id() To get the user id
     1071 * @uses bbp_verify_nonce_request() To verify the nonce and check the request
     1072 * @uses current_user_can() To check if the current user can edit the user
     1073 * @uses bbPress:errors:add() To log the error messages
     1074 * @uses bbp_is_user_subscribed() To check if the forum is in user's
     1075 *                    subscriptions
     1076 * @uses bbp_remove_user_subscription() To remove the user subscription
     1077 * @uses bbp_add_user_subscription() To add the user subscription
     1078 * @uses do_action() Calls 'bbp_subscriptions_handler' with success, user id,
     1079 *                    forum id and action
     1080 * @uses bbp_is_subscription() To check if it's the subscription page
     1081 * @uses bbp_get_subscription_permalink() To get the subscription page link
     1082 * @uses bbp_get_user_profile_url() To get the user profile link
     1083 * @uses bbp_get_forum_permalink() To get the forum permalink
     1084 * @uses wp_safe_redirect() To redirect to the url
     1085 */
     1086function bbp_forum_subscriptions_handler( $action = '' ) {
     1087
     1088        if ( !bbp_is_subscriptions_active() )
     1089                return false;
     1090
     1091        // Bail if no forum ID is passed
     1092        if ( empty( $_GET['forum_id'] ) )
     1093                return;
     1094
     1095        // Setup possible get actions
     1096        $possible_actions = array(
     1097                'bbp_subscribe',
     1098                'bbp_unsubscribe',
     1099        );
     1100
     1101        // Bail if actions aren't meant for this function
     1102        if ( !in_array( $action, $possible_actions ) )
     1103                return;
     1104
     1105        // Get required data
     1106        $user_id  = bbp_get_user_id( 0, true, true );
     1107        $forum_id = intval( $_GET['forum_id'] );
     1108
     1109        // Check for empty forum
     1110        if ( empty( $forum_id ) ) {
     1111                bbp_add_error( 'bbp_subscription_forum_id', __( '<strong>ERROR</strong>: No forum was found! Which forum are you subscribing/unsubscribing to?', 'bbpress' ) );
     1112
     1113        // Check nonce
     1114        } elseif ( ! bbp_verify_nonce_request( 'toggle-subscription_' . $forum_id ) ) {
     1115                bbp_add_error( 'bbp_subscription_forum_id', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
     1116
     1117        // Check current user's ability to edit the user
     1118        } elseif ( !current_user_can( 'edit_user', $user_id ) ) {
     1119                bbp_add_error( 'bbp_subscription_permissions', __( '<strong>ERROR</strong>: You don\'t have the permission to edit favorites of that user!', 'bbpress' ) );
     1120        }
     1121
     1122        // Bail if we have errors
     1123        if ( bbp_has_errors() )
     1124                return;
     1125
     1126        /** No errors *************************************************************/
     1127
     1128        $is_subscription = bbp_is_user_subscribed( $user_id, $forum_id );
     1129        $success         = false;
     1130
     1131        if ( true === $is_subscription && 'bbp_unsubscribe' === $action )
     1132                $success = bbp_remove_user_subscription( $user_id, $forum_id );
     1133        elseif ( false === $is_subscription && 'bbp_subscribe' === $action )
     1134                $success = bbp_add_user_subscription( $user_id, $forum_id );
     1135
     1136        // Do additional subscriptions actions
     1137        do_action( 'bbp_subscriptions_handler', $success, $user_id, $forum_id, $action );
     1138
     1139        // Success!
     1140        if ( true === $success ) {
     1141
     1142                // Redirect back from whence we came
     1143                if ( bbp_is_subscriptions() ) {
     1144                        $redirect = bbp_get_subscriptions_permalink( $user_id );
     1145                } elseif ( bbp_is_single_user() ) {
     1146                        $redirect = bbp_get_user_profile_url();
     1147                } elseif ( is_singular( bbp_get_forum_post_type() ) ) {
     1148                        $redirect = bbp_get_forum_permalink( $forum_id );
     1149                } elseif ( is_single() || is_page() ) {
     1150                        $redirect = get_permalink();
     1151                } else {
     1152                        $redirect = get_permalink( $forum_id );
     1153                }
     1154
     1155                wp_safe_redirect( $redirect );
     1156
     1157                // For good measure
     1158                exit();
     1159
     1160        // Fail! Handle errors
     1161        } elseif ( true === $is_subscription && 'bbp_unsubscribe' === $action ) {
     1162                bbp_add_error( 'bbp_unsubscribe', __( '<strong>ERROR</strong>: There was a problem unsubscribing from that forum!', 'bbpress' ) );
     1163        } elseif ( false === $is_subscription && 'bbp_subscribe' === $action ) {
     1164                bbp_add_error( 'bbp_subscribe',    __( '<strong>ERROR</strong>: There was a problem subscribing to that forum!', 'bbpress' ) );
     1165        }
     1166}
     1167
     1168/**
    7241169 * Handles the front end subscribing and unsubscribing topics
    7251170 *
     1171 * @since bbPress (rXXXX)
     1172 *
    7261173 * @param string $action The requested action to compare this function to
    7271174 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
    7281175 * @uses bbp_get_user_id() To get the user id
     
    7301177 * @uses current_user_can() To check if the current user can edit the user
    7311178 * @uses bbPress:errors:add() To log the error messages
    7321179 * @uses bbp_is_user_subscribed() To check if the topic is in user's
    733  *                                 subscriptions
     1180 *                    subscriptions
    7341181 * @uses bbp_remove_user_subscription() To remove the user subscription
    7351182 * @uses bbp_add_user_subscription() To add the user subscription
    7361183 * @uses do_action() Calls 'bbp_subscriptions_handler' with success, user id,
    7371184 *                    topic id and action
    7381185 * @uses bbp_is_subscription() To check if it's the subscription page
    739  * @uses bbp_get_subscription_link() To get the subscription page link
     1186 * @uses bbp_get_subscription_permalink() To get the subscription page link
     1187 * @uses bbp_get_user_profile_url() To get the user profile link
    7401188 * @uses bbp_get_topic_permalink() To get the topic permalink
    7411189 * @uses wp_safe_redirect() To redirect to the url
    7421190 */
     
    8271275/**
    8281276 * Handles the front end user editing
    8291277 *
     1278 * @since bbPress (rXXXX)
     1279 *
    8301280 * @param string $action The requested action to compare this function to
    8311281 * @uses is_multisite() To check if it's a multisite
    8321282 * @uses bbp_is_user_home() To check if the user is at home (the display page
    833  *                           is the one of the logged in user)
     1283 *                    is the one of the logged in user)
    8341284 * @uses get_option() To get the displayed user's new email id option
    8351285 * @uses wpdb::prepare() To sanitize our sql query
    8361286 * @uses wpdb::get_var() To execute our query and get back the variable
     
    9681418 * @return array|bool Results if the user has created topics, otherwise false
    9691419 */
    9701420function bbp_get_user_topics_started( $user_id = 0 ) {
    971        
     1421
    9721422        // Validate user
    9731423        $user_id = bbp_get_user_id( $user_id );
    9741424        if ( empty( $user_id ) )
     
    9931443 * @return array|bool Results if the user has created topics, otherwise false
    9941444 */
    9951445function bbp_get_user_replies_created( $user_id = 0 ) {
    996        
     1446
    9971447        // Validate user
    9981448        $user_id = bbp_get_user_id( $user_id );
    9991449        if ( empty( $user_id ) )
     
    10131463 * Get the total number of users on the forums
    10141464 *
    10151465 * @since bbPress (r2769)
     1466 *
    10161467 * @uses count_users() To execute our query and get the var back
    10171468 * @uses apply_filters() Calls 'bbp_get_total_users' with number of users
    10181469 * @return int Total number of users
     
    10311482 * which a user can edit another user (or themselves.) If these conditions are
    10321483 * met. We assume a user cannot perform this task, and look for ways they can
    10331484 * earn the ability to access this template.
    1034  * 
     1485 *
    10351486 * @since bbPress (r3605)
    10361487 *
    10371488 * @uses bbp_is_topic_edit()
     
    10991550 * Convert passwords from previous platfrom encryption to WordPress encryption.
    11001551 *
    11011552 * @since bbPress (r3813)
     1553 *
    11021554 * @global WPDB $wpdb
    11031555 */
    11041556function bbp_user_maybe_convert_pass() {
  • 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-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/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/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}