Skip to:
Content

bbPress.org

Changeset 5156


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

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

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

Legend:

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

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

    r4999 r5156  
    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/**
     
    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/**
     
    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
     
    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
  • trunk/includes/forums/functions.php

    r5114 r5156  
    962962}
    963963
     964/** Subscriptions *************************************************************/
     965
     966/**
     967 * Remove a deleted forum from all users' subscriptions
     968 *
     969 * @since bbPress (rxxxx)
     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
     984    $forum_id = bbp_get_forum_id( $forum_id );
     985
     986    // Bail if no forum
     987    if ( empty( $forum_id ) ) {
     988        return;
     989    }
     990
     991    // Get users
     992    $users = (array) bbp_get_forum_subscribers( $forum_id );
     993
     994    // Users exist
     995    if ( !empty( $users ) ) {
     996
     997        // Loop through users
     998        foreach ( $users as $user ) {
     999
     1000            // Remove each user
     1001            bbp_remove_user_subscription( $user, $forum_id );
     1002        }
     1003    }
     1004}
     1005
    9641006/** Count Bumpers *************************************************************/
    9651007
  • trunk/includes/forums/template.php

    r5130 r5156  
    790790}
    791791
     792/** Forum Subscriptions *******************************************************/
     793
     794/**
     795 * Output the forum subscription link
     796 *
     797 * @since bbPress (rxxxx)
     798 *
     799 * @uses bbp_get_forum_subscription_link()
     800 */
     801function bbp_forum_subscription_link( $args = array() ) {
     802    echo bbp_get_forum_subscription_link( $args );
     803}
     804
     805    /**
     806     * Get the forum subscription link
     807     *
     808     * A custom wrapper for bbp_get_user_subscribe_link()
     809     *
     810     * @since bbPress (rxxxx)
     811     *
     812     * @uses bbp_parse_args()
     813     * @uses bbp_get_user_subscribe_link()
     814     * @uses apply_filters() Calls 'bbp_get_forum_subscribe_link'
     815     */
     816    function bbp_get_forum_subscription_link( $args = array() ) {
     817
     818        // No link
     819        $retval = false;
     820
     821        // Parse the arguments
     822        $r = bbp_parse_args( $args, array(
     823            'forum_id'    => 0,
     824            'user_id'     => 0,
     825            'before'      => '',
     826            'after'       => '',
     827            'subscribe'   => __( 'Subscribe',   'bbpress' ),
     828            'unsubscribe' => __( 'Unsubscribe', 'bbpress' )
     829        ), 'get_forum_subscribe_link' );
     830
     831        // Get the link
     832        $retval = bbp_get_user_subscribe_link( $r );
     833
     834        return apply_filters( 'bbp_get_forum_subscribe_link', $retval, $r );
     835    }
     836
    792837/** Forum Last Topic **********************************************************/
    793838
     
    21662211        return apply_filters( 'bbp_get_form_forum_visibility', esc_attr( $forum_visibility ) );
    21672212    }
     2213   
     2214/**
     2215 * Output checked value of forum subscription
     2216 *
     2217 * @since bbPress (rxxxx)
     2218 *
     2219 * @uses bbp_get_form_forum_subscribed() To get the subscribed checkbox value
     2220 */
     2221function bbp_form_forum_subscribed() {
     2222    echo bbp_get_form_forum_subscribed();
     2223}
     2224    /**
     2225     * Return checked value of forum subscription
     2226     *
     2227     * @since bbPress (rxxxx)
     2228     *
     2229     * @uses bbp_is_forum_edit() To check if it's the forum edit page
     2230     * @uses bbp_get_global_post_field() To get current post author
     2231     * @uses bbp_get_current_user_id() To get the current user id
     2232     * @uses bbp_is_user_subscribed() To check if the user is subscribed to
     2233     *                the forum
     2234     * @uses apply_filters() Calls 'bbp_get_form_forum_subscribed' with the
     2235     *                option
     2236     * @return string Checked value of forum subscription
     2237     */
     2238    function bbp_get_form_forum_subscribed() {
     2239
     2240        // Get _POST data
     2241        if ( bbp_is_post_request() && isset( $_POST['bbp_forum_subscription'] ) ) {
     2242            $forum_subscribed = (bool) $_POST['bbp_forum_subscription'];
     2243
     2244        // Get edit data
     2245        } elseif ( bbp_is_forum_edit() || bbp_is_reply_edit() ) {
     2246
     2247            // Get current posts author
     2248            $post_author = bbp_get_global_post_field( 'post_author', 'raw' );
     2249
     2250            // Post author is not the current user
     2251            if ( bbp_get_current_user_id() !== $post_author ) {
     2252                $forum_subscribed = bbp_is_user_subscribed( $post_author );
     2253
     2254            // Post author is the current user
     2255            } else {
     2256                $forum_subscribed = bbp_is_user_subscribed( bbp_get_current_user_id() );
     2257            }
     2258
     2259        // Get current status
     2260        } elseif ( bbp_is_single_forum() ) {
     2261            $forum_subscribed = bbp_is_user_subscribed( bbp_get_current_user_id() );
     2262
     2263        // No data
     2264        } else {
     2265            $forum_subscribed = false;
     2266        }
     2267
     2268        // Get checked output
     2269        $checked = checked( $forum_subscribed, true, false );
     2270
     2271        return apply_filters( 'bbp_get_form_forum_subscribed', $checked, $forum_subscribed );
     2272    }
    21682273
    21692274/** Form Dropdowns ************************************************************/
  • trunk/includes/topics/functions.php

    r5078 r5156  
    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
     
    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
  • trunk/includes/topics/template.php

    r5130 r5156  
    18081808    }
    18091809
     1810/** Topic Subscriptions *******************************************************/
     1811
     1812/**
     1813 * Output the topic subscription link
     1814 *
     1815 * @since bbPress (rxxxx)
     1816 *
     1817 * @uses bbp_get_topic_subscription_link()
     1818 */
     1819function bbp_topic_subscription_link( $args = array() ) {
     1820    echo bbp_get_topic_subscription_link( $args );
     1821}
     1822
     1823    /**
     1824     * Get the forum subscription link
     1825     *
     1826     * A custom wrapper for bbp_get_user_subscribe_link()
     1827     *
     1828     * @since bbPress (rxxxx)
     1829     *
     1830     * @uses bbp_parse_args()
     1831     * @uses bbp_get_user_subscribe_link()
     1832     * @uses apply_filters() Calls 'bbp_get_topic_subscribe_link'
     1833     */
     1834    function bbp_get_topic_subscription_link( $args = array() ) {
     1835
     1836        // No link
     1837        $retval = false;
     1838
     1839        // Parse the arguments
     1840        $r = bbp_parse_args( $args, array(
     1841            'user_id'     => 0,
     1842            'topic_id'    => 0,
     1843            'before'      => ' | ',
     1844            'after'       => '',
     1845            'subscribe'   => __( 'Subscribe',   'bbpress' ),
     1846            'unsubscribe' => __( 'Unsubscribe', 'bbpress' )
     1847        ), 'get_forum_subscribe_link' );
     1848
     1849        // Get the link
     1850        $retval = bbp_get_user_subscribe_link( $r );
     1851
     1852        return apply_filters( 'bbp_get_topic_subscribe_link', $retval, $r );
     1853    }
     1854
     1855/** Topic Favorites ***********************************************************/
     1856
     1857/**
     1858 * Output the topic favorite link
     1859 *
     1860 * @since bbPress (rxxxx)
     1861 *
     1862 * @uses bbp_get_topic_favorite_link()
     1863 */
     1864function bbp_topic_favorite_link( $args = array() ) {
     1865    echo bbp_get_topic_favorite_link( $args );
     1866}
     1867
     1868    /**
     1869     * Get the forum favorite link
     1870     *
     1871     * A custom wrapper for bbp_get_user_favorite_link()
     1872     *
     1873     * @since bbPress (rxxxx)
     1874     *
     1875     * @uses bbp_parse_args()
     1876     * @uses bbp_get_user_favorites_link()
     1877     * @uses apply_filters() Calls 'bbp_get_topic_favorite_link'
     1878     */
     1879    function bbp_get_topic_favorite_link( $args = array() ) {
     1880
     1881        // No link
     1882        $retval = false;
     1883
     1884        // Parse the arguments
     1885        $r = bbp_parse_args( $args, array(
     1886            'user_id'   => 0,
     1887            'topic_id'  => 0,
     1888            'before'    => '',
     1889            'after'     => '',
     1890            'favorite'  => __( 'Favorite',   'bbpress' ),
     1891            'favorited' => __( 'Unfavorite', 'bbpress' )
     1892        ), 'get_forum_favorite_link' );
     1893
     1894        // Get the link
     1895        $retval = bbp_get_user_favorites_link( $r );
     1896
     1897        return apply_filters( 'bbp_get_topic_favorite_link', $retval, $r );
     1898    }
     1899
    18101900/** Topic Last Reply **********************************************************/
    18111901
  • trunk/includes/users/functions.php

    r5121 r5156  
    314314
    315315    if ( !empty( $favorites ) ) {
    316        
     316
    317317        // Checking a specific topic id
    318318        if ( !empty( $topic_id ) ) {
     
    513513
    514514/**
     515 * Get the users who have subscribed to the forum
     516 *
     517 * @since bbPress (rxxxx)
     518 *
     519 * @param int $forum_id Optional. forum id
     520 * @uses wpdb::get_col() To execute our query and get the column back
     521 * @uses apply_filters() Calls 'bbp_get_forum_subscribers' with the subscribers
     522 * @return array|bool Results if the forum has any subscribers, otherwise false
     523 */
     524function bbp_get_forum_subscribers( $forum_id = 0 ) {
     525    $forum_id = bbp_get_forum_id( $forum_id );
     526    if ( empty( $forum_id ) )
     527        return;
     528
     529    global $wpdb;
     530
     531    $key   = $wpdb->prefix . '_bbp_forum_subscriptions';
     532    $users = wp_cache_get( 'bbp_get_forum_subscribers_' . $forum_id, 'bbpress_users' );
     533    if ( false === $users ) {
     534        $users = $wpdb->get_col( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = '{$key}' and FIND_IN_SET('{$forum_id}', meta_value) > 0" );
     535        wp_cache_set( 'bbp_get_forum_subscribers_' . $forum_id, $users, 'bbpress_users' );
     536    }
     537
     538    return apply_filters( 'bbp_get_forum_subscribers', $users );
     539}
     540
     541/**
    515542 * Get the users who have subscribed to the topic
    516543 *
     
    544571 * @since bbPress (r2668)
    545572 *
     573 * @deprecated since bbPress (rxxxx)
     574 *
     575 * @param int $user_id Optional. User id
     576 * @uses bbp_get_user_topic_subscriptions() To get the user's subscriptions
     577 * @return array|bool Results if user has subscriptions, otherwise false
     578 */
     579function bbp_get_user_subscriptions( $user_id = 0 ) {
     580    _deprecated_function( __FUNCTION__, 2.5, 'bbp_get_user_topic_subscriptions()' );
     581    $query = bbp_get_user_topic_subscriptions( $user_id );
     582    return apply_filters( 'bbp_get_user_subscriptions', $query, $user_id );
     583}
     584
     585/**
     586 * Get a user's subscribed topics
     587 *
     588 * @since bbPress (r2668)
     589 *
    546590 * @param int $user_id Optional. User id
    547591 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions
     
    551595 * @return array|bool Results if user has subscriptions, otherwise false
    552596 */
    553 function bbp_get_user_subscriptions( $user_id = 0 ) {
     597function bbp_get_user_topic_subscriptions( $user_id = 0 ) {
    554598
    555599    // Default to the displayed user
    556600    $user_id = bbp_get_user_id( $user_id );
    557     if ( empty( $user_id ) )
    558         return false;
     601    if ( empty( $user_id ) ) {
     602        return false;
     603    }
    559604
    560605    // If user has subscriptions, load them
     
    566611    }
    567612
    568     return apply_filters( 'bbp_get_user_subscriptions', $query, $user_id );
     613    return apply_filters( 'bbp_get_user_topic_subscriptions', $query, $user_id );
     614}
     615
     616/**
     617 * Get a user's subscribed forums
     618 *
     619 * @since bbPress (rxxxx)
     620 *
     621 * @param int $user_id Optional. User id
     622 * @uses bbp_get_user_subscribed_forum_ids() To get the user's subscriptions
     623 * @uses bbp_has_forums() To get the forums
     624 * @uses apply_filters() Calls 'bbp_get_user_forum_subscriptions' with the forum
     625 *                        query and user id
     626 * @return array|bool Results if user has subscriptions, otherwise false
     627 */
     628function bbp_get_user_forum_subscriptions( $user_id = 0 ) {
     629
     630    // Default to the displayed user
     631    $user_id = bbp_get_user_id( $user_id );
     632    if ( empty( $user_id ) ) {
     633        return false;
     634    }
     635
     636    // If user has subscriptions, load them
     637    $subscriptions = bbp_get_user_subscribed_forum_ids( $user_id );
     638    if ( !empty( $subscriptions ) ) {
     639        $query = bbp_has_forums( array( 'post__in' => $subscriptions ) );
     640    } else {
     641        $query = false;
     642    }
     643
     644    return apply_filters( 'bbp_get_user_forum_subscriptions', $query, $user_id );
     645}
     646
     647/**
     648 * Get a user's subscribed forum ids
     649 *
     650 * @since bbPress (rxxxx)
     651 *
     652 * @param int $user_id Optional. User id
     653 * @uses bbp_get_user_id() To get the user id
     654 * @uses get_user_option() To get the user's subscriptions
     655 * @uses apply_filters() Calls 'bbp_get_user_subscribed_forum_ids' with
     656 *                        the subscriptions and user id
     657 * @return array|bool Results if user has subscriptions, otherwise false
     658 */
     659function bbp_get_user_subscribed_forum_ids( $user_id = 0 ) {
     660    $user_id = bbp_get_user_id( $user_id );
     661    if ( empty( $user_id ) )
     662        return false;
     663
     664    $subscriptions = get_user_option( '_bbp_forum_subscriptions', $user_id );
     665    $subscriptions = array_filter( wp_parse_id_list( $subscriptions ) );
     666
     667    return (array) apply_filters( 'bbp_get_user_subscribed_forum_ids', $subscriptions, $user_id );
    569668}
    570669
     
    593692
    594693/**
     694 * Check if a topic or forum is in user's subscription list or not
     695 *
     696 * @since bbPress (rxxxx)
     697 *
     698 * @param int $user_id Optional. User id
     699 * @param int $forum_id Optional. Topic id
     700 * @uses get_post() To get the post object
     701 * @uses bbp_get_user_subscribed_forum_ids() To get the user's forum subscriptions
     702 * @uses bbp_get_user_subscribed_topic_ids() To get the user's topic subscriptions
     703 * @uses bbp_get_forum_post_type() To get the forum post type
     704 * @uses bbp_get_topic_post_type() To get the topic post type
     705 * @uses apply_filters() Calls 'bbp_is_user_subscribed' with the bool, user id,
     706 *                        forum/topic id and subsriptions
     707 * @return bool True if the forum or topic is in user's subscriptions, otherwise false
     708 */
     709function bbp_is_user_subscribed( $user_id = 0, $object_id = 0 ) {
     710
     711    // Assume user is not subscribed
     712    $retval = false;
     713
     714    // Setup ID's array
     715    $subscribed_ids = array();
     716
     717    // User and object ID's are passed
     718    if ( ! empty( $user_id ) && ! empty( $object_id ) ) {
     719
     720        // Get the post type
     721        $post_type = get_post_type( $object_id );
     722
     723        // Post exists, so check the types
     724        if ( ! empty( $post_type ) ) {
     725
     726            switch( $post_type ) {
     727
     728                // Forum
     729                case bbp_get_forum_post_type() :
     730                    $subscribed_ids = bbp_get_user_subscribed_forum_ids( $user_id );
     731                    $retval         = bbp_is_user_subscribed_to_forum( $user_id, $object_id, $subscribed_ids );
     732                    break;
     733
     734                // Topic (default)
     735                case bbp_get_topic_post_type() :
     736                default :
     737                    $subscribed_ids = bbp_get_user_subscribed_topic_ids( $user_id );
     738                    $retval         = bbp_is_user_subscribed_to_topic( $user_id, $object_id, $subscribed_ids );
     739                    break;
     740            }
     741        }
     742    }
     743
     744    return (bool) apply_filters( 'bbp_is_user_subscribed', $retval, $user_id, $object_id, $subscribed_ids );
     745}
     746
     747/**
     748 * Check if a forum is in user's subscription list or not
     749 *
     750 * @since bbPress (rxxxx)
     751 *
     752 * @param int $user_id Optional. User id
     753 * @param int $forum_id Optional. Topic id
     754 * @param array $subscribed_ids Optional. Array of forum ID's to check
     755 * @uses bbp_get_user_id() To get the user id
     756 * @uses bbp_get_user_subscribed_forum_ids() To get the user's subscriptions
     757 * @uses bbp_get_forum() To get the forum
     758 * @uses bbp_get_forum_id() To get the forum id
     759 * @uses apply_filters() Calls 'bbp_is_user_subscribed' with the bool, user id,
     760 *                        forum id and subsriptions
     761 * @return bool True if the forum is in user's subscriptions, otherwise false
     762 */
     763function bbp_is_user_subscribed_to_forum( $user_id = 0, $forum_id = 0, $subscribed_ids = array() ) {
     764
     765    // Assume user is not subscribed
     766    $retval = false;
     767
     768    // Validate user
     769    $user_id = bbp_get_user_id( $user_id, true, true );
     770    if ( ! empty( $user_id ) ) {
     771
     772        // Get subscription ID's if none passed
     773        if ( empty( $subscribed_ids ) ) {
     774            $subscribed_ids = bbp_get_user_subscribed_forum_ids( $user_id );
     775        }
     776
     777        // User has forum subscriptions
     778        if ( ! empty( $subscribed_ids ) ) {
     779
     780            // Checking a specific forum id
     781            if ( ! empty( $forum_id ) ) {
     782                $forum    = bbp_get_forum( $forum_id );
     783                $forum_id = ! empty( $forum ) ? $forum->ID : 0;
     784
     785            // Using the global forum id
     786            } elseif ( bbp_get_forum_id() ) {
     787                $forum_id = bbp_get_forum_id();
     788
     789            // Use the current post id
     790            } elseif ( ! bbp_get_forum_id() ) {
     791                $forum_id = get_the_ID();
     792            }
     793
     794            // Is forum_id in the user's favorites
     795            if ( ! empty( $forum_id ) ) {
     796                $retval = in_array( $forum_id, $subscribed_ids );
     797            }
     798        }
     799    }
     800
     801    return (bool) apply_filters( 'bbp_is_user_subscribed_to_forum', (bool) $retval, $user_id, $forum_id, $subscribed_ids );
     802}
     803
     804/**
    595805 * Check if a topic is in user's subscription list or not
    596806 *
    597  * @since bbPress (r2668)
     807 * @since bbPress (rxxxx)
    598808 *
    599809 * @param int $user_id Optional. User id
    600810 * @param int $topic_id Optional. Topic id
     811 * @param array $subscribed_ids Optional. Array of topic ID's to check
    601812 * @uses bbp_get_user_id() To get the user id
    602813 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions
     
    607818 * @return bool True if the topic is in user's subscriptions, otherwise false
    608819 */
    609 function bbp_is_user_subscribed( $user_id = 0, $topic_id = 0 ) {
     820function bbp_is_user_subscribed_to_topic( $user_id = 0, $topic_id = 0, $subscribed_ids = array() ) {
     821
     822    // Assume user is not subscribed
     823    $retval = false;
    610824
    611825    // Validate user
    612826    $user_id = bbp_get_user_id( $user_id, true, true );
    613     if ( empty( $user_id ) )
    614         return false;
    615 
    616     $retval        = false;
    617     $subscriptions = bbp_get_user_subscribed_topic_ids( $user_id );
    618 
    619     if ( !empty( $subscriptions ) ) {
    620 
    621         // 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;
    625 
    626         // Using the global topic id
    627         } elseif ( bbp_get_topic_id() ) {
    628             $topic_id = bbp_get_topic_id();
    629 
    630         // Use the current post id
    631         } elseif ( !bbp_get_topic_id() ) {
    632             $topic_id = get_the_ID();
     827    if ( !empty( $user_id ) ) {
     828
     829        // Get subscription ID's if none passed
     830        if ( empty( $subscribed_ids ) ) {
     831            $subscribed_ids = bbp_get_user_subscribed_topic_ids( $user_id );
    633832        }
    634833
    635         // Is topic_id in the user's favorites
    636         if ( !empty( $topic_id ) ) {
    637             $retval = in_array( $topic_id, $subscriptions );
     834        // User has topic subscriptions
     835        if ( ! empty( $subscribed_ids ) ) {
     836
     837            // Checking a specific topic id
     838            if ( ! empty( $topic_id ) ) {
     839                $topic    = bbp_get_topic( $topic_id );
     840                $topic_id = ! empty( $topic ) ? $topic->ID : 0;
     841
     842            // Using the global topic id
     843            } elseif ( bbp_get_topic_id() ) {
     844                $topic_id = bbp_get_topic_id();
     845
     846            // Use the current post id
     847            } elseif ( !bbp_get_topic_id() ) {
     848                $topic_id = get_the_ID();
     849            }
     850
     851            // Is topic_id in the user's favorites
     852            if ( ! empty( $topic_id ) ) {
     853                $retval = in_array( $topic_id, $subscribed_ids );
     854            }
    638855        }
    639856    }
    640857
    641     return (bool) apply_filters( 'bbp_is_user_subscribed', (bool) $retval, $user_id, $topic_id, $subscriptions );
     858    return (bool) apply_filters( 'bbp_is_user_subscribed_to_topic', (bool) $retval, $user_id, $topic_id, $subscribed_ids );
     859}
     860
     861/**
     862 * Add a topic to user's subscriptions
     863 *
     864 * @since bbPress (rxxxx)
     865 *
     866 * @param int $user_id Optional. User id
     867 * @param int $topic_id Optional. Topic id
     868 * @uses get_post() To get the post object
     869 * @uses bbp_get_user_subscribed_forum_ids() To get the user's forum subscriptions
     870 * @uses bbp_get_user_subscribed_topic_ids() To get the user's topic subscriptions
     871 * @uses bbp_get_forum_post_type() To get the forum post type
     872 * @uses bbp_get_topic_post_type() To get the topic post type
     873 * @uses update_user_option() To update the user's subscriptions
     874 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & topic id
     875 * @return bool Always true
     876 */
     877function bbp_add_user_subscription( $user_id = 0, $object_id = 0 ) {
     878    if ( empty( $user_id ) || empty( $object_id ) ) {
     879        return false;
     880    }
     881
     882    // Get the post type
     883    $post_type = get_post_type( $object_id );
     884    if ( empty( $post_type ) ) {
     885        return false;
     886    }
     887
     888    switch( $post_type ) {
     889
     890        // Forum
     891        case bbp_get_forum_post_type() :
     892            bbp_add_user_forum_subscription( $user_id, $object_id );
     893            break;
     894
     895        // Topic
     896        case bbp_get_topic_post_type() :
     897        default :
     898            bbp_add_user_topic_subscription( $user_id, $object_id );
     899            break;
     900    }
     901
     902    do_action( 'bbp_add_user_subscription', $user_id, $object_id, $post_type );
     903
     904    return true;
     905}
     906
     907/**
     908 * Add a forum to user's subscriptions
     909 *
     910 * @since bbPress (rxxxx)
     911 *
     912 * @param int $user_id Optional. User id
     913 * @param int $forum_id Optional. forum id
     914 * @uses bbp_get_user_subscribed_forum_ids() To get the user's subscriptions
     915 * @uses bbp_get_forum() To get the forum
     916 * @uses update_user_option() To update the user's subscriptions
     917 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & forum id
     918 * @return bool Always true
     919 */
     920function bbp_add_user_forum_subscription( $user_id = 0, $forum_id = 0 ) {
     921    if ( empty( $user_id ) || empty( $forum_id ) ) {
     922        return false;
     923    }
     924
     925    $forum = bbp_get_forum( $forum_id );
     926    if ( empty( $forum ) ) {
     927        return false;
     928    }
     929
     930    $subscriptions = (array) bbp_get_user_subscribed_forum_ids( $user_id );
     931    if ( !in_array( $forum_id, $subscriptions ) ) {
     932        $subscriptions[] = $forum_id;
     933        $subscriptions   = implode( ',', wp_parse_id_list( array_filter( $subscriptions ) ) );
     934        update_user_option( $user_id, '_bbp_forum_subscriptions', $subscriptions );
     935
     936        wp_cache_delete( 'bbp_get_forum_subscribers_' . $forum_id, 'bbpress_users' );
     937    }
     938
     939    do_action( 'bbp_add_user_forum_subscription', $user_id, $forum_id );
     940
     941    return true;
    642942}
    643943
     
    655955 * @return bool Always true
    656956 */
    657 function bbp_add_user_subscription( $user_id = 0, $topic_id = 0 ) {
    658     if ( empty( $user_id ) || empty( $topic_id ) )
    659         return false;
     957function bbp_add_user_topic_subscription( $user_id = 0, $topic_id = 0 ) {
     958    if ( empty( $user_id ) || empty( $topic_id ) ) {
     959        return false;
     960    }
    660961
    661962    $topic = bbp_get_topic( $topic_id );
    662     if ( empty( $topic ) )
    663         return false;
     963    if ( empty( $topic ) ) {
     964        return false;
     965    }
    664966
    665967    $subscriptions = (array) bbp_get_user_subscribed_topic_ids( $user_id );
     
    672974    }
    673975
    674     do_action( 'bbp_add_user_subscription', $user_id, $topic_id );
     976    do_action( 'bbp_add_user_topic_subscription', $user_id, $topic_id );
    675977
    676978    return true;
     
    681983 *
    682984 * @since bbPress (r2668)
     985 *
     986 * @param int $user_id Optional. User id
     987 * @param int $topic_id Optional. Topic id
     988 * @uses get_post() To get the post object
     989 * @uses bbp_get_forum_post_type() To get the forum post type
     990 * @uses bbp_get_topic_post_type() To get the topic post type
     991 * @uses bbp_remove_user_forum_subscription() To remove the user's subscription
     992 * @uses bbp_remove_user_topic_subscription() To remove the user's subscription
     993 * @uses do_action() Calls 'bbp_remove_user_subscription' with the user id and
     994 *                    topic id
     995 * @return bool True if the topic was removed from user's subscriptions,
     996 *               otherwise false
     997 */
     998function bbp_remove_user_subscription( $user_id = 0, $object_id = 0 ) {
     999    if ( empty( $user_id ) || empty( $object_id ) ) {
     1000        return false;
     1001    }
     1002
     1003    $post_type = get_post_type( $object_id );
     1004    if ( empty( $post_type ) ) {
     1005        return false;
     1006    }
     1007
     1008    switch( $post_type ) {
     1009
     1010        // Forum
     1011        case bbp_get_forum_post_type() :
     1012            bbp_remove_user_forum_subscription( $user_id, $object_id );
     1013            break;
     1014
     1015        // Topic
     1016        case bbp_get_topic_post_type() :
     1017        default :
     1018            bbp_remove_user_topic_subscription( $user_id, $object_id );
     1019            break;
     1020    }
     1021
     1022    do_action( 'bbp_remove_user_subscription', $user_id, $object_id, $post_type );
     1023
     1024    return true;
     1025}
     1026
     1027/**
     1028 * Remove a forum from user's subscriptions
     1029 *
     1030 * @since bbPress (rxxxx)
     1031 *
     1032 * @param int $user_id Optional. User id
     1033 * @param int $forum_id Optional. forum id
     1034 * @uses bbp_get_user_subscribed_forum_ids() To get the user's subscriptions
     1035 * @uses update_user_option() To update the user's subscriptions
     1036 * @uses delete_user_option() To delete the user's subscriptions meta
     1037 * @uses do_action() Calls 'bbp_remove_user_subscription' with the user id and
     1038 *                    forum id
     1039 * @return bool True if the forum was removed from user's subscriptions,
     1040 *               otherwise false
     1041 */
     1042function bbp_remove_user_forum_subscription( $user_id, $forum_id ) {
     1043    if ( empty( $user_id ) || empty( $forum_id ) ) {
     1044        return false;
     1045    }
     1046
     1047    $subscriptions = (array) bbp_get_user_subscribed_forum_ids( $user_id );
     1048    if ( empty( $subscriptions ) ) {
     1049        return false;
     1050    }
     1051
     1052    $pos = array_search( $forum_id, $subscriptions );
     1053    if ( false === $pos ) {
     1054        return false;
     1055    }
     1056
     1057    array_splice( $subscriptions, $pos, 1 );
     1058    $subscriptions = array_filter( $subscriptions );
     1059
     1060    if ( !empty( $subscriptions ) ) {
     1061        $subscriptions = implode( ',', wp_parse_id_list( $subscriptions ) );
     1062        update_user_option( $user_id, '_bbp_forum_subscriptions', $subscriptions );
     1063    } else {
     1064        delete_user_option( $user_id, '_bbp_forum_subscriptions' );
     1065    }
     1066
     1067    wp_cache_delete( 'bbp_get_forum_subscribers_' . $forum_id, 'bbpress_users' );
     1068
     1069    do_action( 'bbp_remove_user_forum_subscription', $user_id, $forum_id );
     1070
     1071    return true;
     1072}
     1073
     1074/**
     1075 * Remove a topic from user's subscriptions
     1076 *
     1077 * @since bbPress (rxxxx)
    6831078 *
    6841079 * @param int $user_id Optional. User id
     
    6871082 * @uses update_user_option() To update the user's subscriptions
    6881083 * @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
     1084 * @uses do_action() Calls 'bbp_remove_user_topic_subscription' with the user id and
    6901085 *                    topic id
    6911086 * @return bool True if the topic was removed from user's subscriptions,
    6921087 *               otherwise false
    6931088 */
    694 function bbp_remove_user_subscription( $user_id, $topic_id ) {
    695     if ( empty( $user_id ) || empty( $topic_id ) )
    696         return false;
     1089function bbp_remove_user_topic_subscription( $user_id, $topic_id ) {
     1090    if ( empty( $user_id ) || empty( $topic_id ) ) {
     1091        return false;
     1092    }
    6971093
    6981094    $subscriptions = (array) bbp_get_user_subscribed_topic_ids( $user_id );
    699 
    700     if ( empty( $subscriptions ) )
    701         return false;
     1095    if ( empty( $subscriptions ) ) {
     1096        return false;
     1097    }
    7021098
    7031099    $pos = array_search( $topic_id, $subscriptions );
    704     if ( is_numeric( $pos ) ) {
    705         array_splice( $subscriptions, $pos, 1 );
    706         $subscriptions = array_filter( $subscriptions );
    707 
    708         if ( !empty( $subscriptions ) ) {
    709             $subscriptions = implode( ',', wp_parse_id_list( $subscriptions ) );
    710             update_user_option( $user_id, '_bbp_subscriptions', $subscriptions );
     1100    if ( false === $pos ) {
     1101        return false;
     1102    }
     1103
     1104    array_splice( $subscriptions, $pos, 1 );
     1105    $subscriptions = array_filter( $subscriptions );
     1106
     1107    if ( !empty( $subscriptions ) ) {
     1108        $subscriptions = implode( ',', wp_parse_id_list( $subscriptions ) );
     1109        update_user_option( $user_id, '_bbp_subscriptions', $subscriptions );
     1110    } else {
     1111        delete_user_option( $user_id, '_bbp_subscriptions' );
     1112    }
     1113
     1114    wp_cache_delete( 'bbp_get_topic_subscribers_' . $topic_id, 'bbpress_users' );
     1115
     1116    do_action( 'bbp_remove_user_topic_subscription', $user_id, $topic_id );
     1117
     1118    return true;
     1119}
     1120
     1121/**
     1122 * Handles the front end subscribing and unsubscribing forums
     1123 *
     1124 * @since bbPress (rxxxx)
     1125 *
     1126 * @param string $action The requested action to compare this function to
     1127 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
     1128 * @uses bbp_get_user_id() To get the user id
     1129 * @uses bbp_verify_nonce_request() To verify the nonce and check the request
     1130 * @uses current_user_can() To check if the current user can edit the user
     1131 * @uses bbPress:errors:add() To log the error messages
     1132 * @uses bbp_is_user_subscribed() To check if the forum is in user's
     1133 *                                 subscriptions
     1134 * @uses bbp_remove_user_subscription() To remove the user subscription
     1135 * @uses bbp_add_user_subscription() To add the user subscription
     1136 * @uses do_action() Calls 'bbp_subscriptions_handler' with success, user id,
     1137 *                    forum id and action
     1138 * @uses bbp_is_subscription() To check if it's the subscription page
     1139 * @uses bbp_get_forum_permalink() To get the forum permalink
     1140 * @uses wp_safe_redirect() To redirect to the url
     1141 */
     1142function bbp_forum_subscriptions_handler( $action = '' ) {
     1143
     1144    if ( ! bbp_is_subscriptions_active() ) {
     1145        return false;
     1146    }
     1147
     1148    // Bail if no forum ID is passed
     1149    if ( empty( $_GET['forum_id'] ) ) {
     1150        return;
     1151    }
     1152
     1153    // Setup possible get actions
     1154    $possible_actions = array(
     1155        'bbp_subscribe',
     1156        'bbp_unsubscribe',
     1157    );
     1158
     1159    // Bail if actions aren't meant for this function
     1160    if ( ! in_array( $action, $possible_actions ) ) {
     1161        return;
     1162    }
     1163
     1164    // Get required data
     1165    $user_id  = bbp_get_user_id( 0, true, true );
     1166    $forum_id = intval( $_GET['forum_id'] );
     1167
     1168    // Check for empty forum
     1169    if ( empty( $forum_id ) ) {
     1170        bbp_add_error( 'bbp_subscription_forum_id', __( '<strong>ERROR</strong>: No forum was found! Which forum are you subscribing/unsubscribing to?', 'bbpress' ) );
     1171
     1172    // Check nonce
     1173    } elseif ( ! bbp_verify_nonce_request( 'toggle-subscription_' . $forum_id ) ) {
     1174        bbp_add_error( 'bbp_subscription_forum_id', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
     1175
     1176    // Check current user's ability to edit the user
     1177    } elseif ( !current_user_can( 'edit_user', $user_id ) ) {
     1178        bbp_add_error( 'bbp_subscription_permissions', __( '<strong>ERROR</strong>: You don\'t have the permission to edit favorites of that user!', 'bbpress' ) );
     1179    }
     1180
     1181    // Bail if we have errors
     1182    if ( bbp_has_errors() ) {
     1183        return;
     1184    }
     1185
     1186    /** No errors *************************************************************/
     1187
     1188    $is_subscription = bbp_is_user_subscribed( $user_id, $forum_id );
     1189    $success         = false;
     1190
     1191    if ( true === $is_subscription && 'bbp_unsubscribe' === $action ) {
     1192        $success = bbp_remove_user_subscription( $user_id, $forum_id );
     1193    } elseif ( false === $is_subscription && 'bbp_subscribe' === $action ) {
     1194        $success = bbp_add_user_subscription( $user_id, $forum_id );
     1195    }
     1196
     1197    // Do additional subscriptions actions
     1198    do_action( 'bbp_subscriptions_handler', $success, $user_id, $forum_id, $action );
     1199
     1200    // Success!
     1201    if ( true === $success ) {
     1202
     1203        // Redirect back from whence we came
     1204        if ( bbp_is_subscriptions() ) {
     1205            $redirect = bbp_get_subscriptions_permalink( $user_id );
     1206        } elseif ( bbp_is_single_user() ) {
     1207            $redirect = bbp_get_user_profile_url();
     1208        } elseif ( is_singular( bbp_get_forum_post_type() ) ) {
     1209            $redirect = bbp_get_forum_permalink( $forum_id );
     1210        } elseif ( is_single() || is_page() ) {
     1211            $redirect = get_permalink();
    7111212        } else {
    712             delete_user_option( $user_id, '_bbp_subscriptions' );
     1213            $redirect = get_permalink( $forum_id );
    7131214        }
    7141215
    715         wp_cache_delete( 'bbp_get_topic_subscribers_' . $topic_id, 'bbpress_users' );
    716     }
    717 
    718     do_action( 'bbp_remove_user_subscription', $user_id, $topic_id );
    719 
    720     return true;
     1216        wp_safe_redirect( $redirect );
     1217
     1218        // For good measure
     1219        exit();
     1220
     1221    // Fail! Handle errors
     1222    } elseif ( true === $is_subscription && 'bbp_unsubscribe' === $action ) {
     1223        bbp_add_error( 'bbp_unsubscribe', __( '<strong>ERROR</strong>: There was a problem unsubscribing from that forum!', 'bbpress' ) );
     1224    } elseif ( false === $is_subscription && 'bbp_subscribe' === $action ) {
     1225        bbp_add_error( 'bbp_subscribe',    __( '<strong>ERROR</strong>: There was a problem subscribing to that forum!', 'bbpress' ) );
     1226    }
    7211227}
    7221228
     
    7371243 *                    topic id and action
    7381244 * @uses bbp_is_subscription() To check if it's the subscription page
    739  * @uses bbp_get_subscription_link() To get the subscription page link
    7401245 * @uses bbp_get_topic_permalink() To get the topic permalink
    7411246 * @uses wp_safe_redirect() To redirect to the url
     
    7431248function bbp_subscriptions_handler( $action = '' ) {
    7441249
    745     if ( !bbp_is_subscriptions_active() )
    746         return false;
     1250    if ( !bbp_is_subscriptions_active() ) {
     1251        return false;
     1252    }
    7471253
    7481254    // Bail if no topic ID is passed
    749     if ( empty( $_GET['topic_id'] ) )
    750         return;
     1255    if ( empty( $_GET['topic_id'] ) ) {
     1256        return;
     1257    }
    7511258
    7521259    // Setup possible get actions
     
    7571264
    7581265    // Bail if actions aren't meant for this function
    759     if ( !in_array( $action, $possible_actions ) )
    760         return;
     1266    if ( !in_array( $action, $possible_actions ) ) {
     1267        return;
     1268    }
    7611269
    7621270    // Get required data
     
    7781286
    7791287    // Bail if we have errors
    780     if ( bbp_has_errors() )
    781         return;
     1288    if ( bbp_has_errors() ) {
     1289        return;
     1290    }
    7821291
    7831292    /** No errors *************************************************************/
     
    7861295    $success         = false;
    7871296
    788     if ( true === $is_subscription && 'bbp_unsubscribe' === $action )
     1297    if ( true === $is_subscription && 'bbp_unsubscribe' === $action ) {
    7891298        $success = bbp_remove_user_subscription( $user_id, $topic_id );
    790     elseif ( false === $is_subscription && 'bbp_subscribe' === $action )
     1299    } elseif ( false === $is_subscription && 'bbp_subscribe' === $action ) {
    7911300        $success = bbp_add_user_subscription( $user_id, $topic_id );
     1301    }
    7921302
    7931303    // Do additional subscriptions actions
     
    9691479 */
    9701480function bbp_get_user_topics_started( $user_id = 0 ) {
    971    
     1481
    9721482    // Validate user
    9731483    $user_id = bbp_get_user_id( $user_id );
     
    9941504 */
    9951505function bbp_get_user_replies_created( $user_id = 0 ) {
    996    
     1506
    9971507    // Validate user
    9981508    $user_id = bbp_get_user_id( $user_id );
     
    10321542 * met. We assume a user cannot perform this task, and look for ways they can
    10331543 * earn the ability to access this template.
    1034  * 
     1544 *
    10351545 * @since bbPress (r3605)
    10361546 *
  • trunk/includes/users/template.php

    r5126 r5156  
    613613
    614614/** Anonymous Fields **********************************************************/
    615    
     615
    616616/**
    617617 * Output the author disylay-name of a topic or reply.
     
    886886     */
    887887    function bbp_get_user_favorites_link( $args = '', $user_id = 0, $wrap = true ) {
    888         if ( !bbp_is_favorites_active() )
     888        if ( ! bbp_is_favorites_active() ) {
    889889            return false;
     890        }
    890891
    891892        // Parse arguments against default values
     
    907908
    908909        // No link if you can't edit yourself
    909         if ( !current_user_can( 'edit_user', (int) $user_id ) ) {
     910        if ( ! current_user_can( 'edit_user', (int) $user_id ) ) {
    910911            return false;
    911912        }
     
    913914        // Decide which link to show
    914915        $is_fav = bbp_is_user_favorite( $user_id, $topic_id );
    915         if ( !empty( $is_fav ) ) {
     916        if ( ! empty( $is_fav ) ) {
    916917            $text       = $r['favorited'];
    917918            $query_args = array( 'action' => 'bbp_favorite_remove', 'topic_id' => $topic_id );
     
    936937
    937938        // Initial output is wrapped in a span, ajax output is hooked to this
    938         if ( !empty( $wrap ) ) {
     939        if ( ! empty( $wrap ) ) {
    939940            $html = '<span id="favorite-toggle">' . $html . '</span>';
    940941        }
     
    10191020}
    10201021    /**
    1021      * Return the link to subscribe/unsubscribe from a topic
     1022     * Return the link to subscribe/unsubscribe from a forum or topic
    10221023     *
    10231024     * @since bbPress (r2668)
     
    10281029     *  - user_id: User id
    10291030     *  - topic_id: Topic id
     1031     *  - forum_id: Forum id
    10301032     *  - before: Before the link
    10311033     *  - after: After the link
    10321034     * @param int $user_id Optional. User id
    10331035     * @param bool $wrap Optional. If you want to wrap the link in <span id="subscription-toggle">.
     1036     * @uses bbp_is_subscriptions_active() to check if subscriptions are active
    10341037     * @uses bbp_get_user_id() To get the user id
     1038     * @uses bbp_get_user_id() To get the user id
     1039     * @uses bbp_get_topic_id() To get the topic id
     1040     * @uses bbp_get_forum_id() To get the forum id
    10351041     * @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
     1042     * @uses bbp_is_user_subscribed_to_forum() To check if the user is subscribed to the forum
     1043     * @uses bbp_is_user_subscribed_to_topic() To check if the user is subscribed to the topic
    10381044     * @uses bbp_is_subscriptions() To check if it's the subscriptions page
    10391045     * @uses bbp_get_subscriptions_permalink() To get subscriptions link
     
    10441050     */
    10451051    function bbp_get_user_subscribe_link( $args = '', $user_id = 0, $wrap = true ) {
    1046         if ( !bbp_is_subscriptions_active() )
     1052        if ( ! bbp_is_subscriptions_active() ) {
    10471053            return;
     1054        }
    10481055
    10491056        // Parse arguments against default values
     
    10531060            'user_id'     => 0,
    10541061            'topic_id'    => 0,
     1062            'forum_id'    => 0,
    10551063            'before'      => '&nbsp;|&nbsp;',
    10561064            'after'       => ''
    10571065        ), 'get_user_subscribe_link' );
    10581066
    1059         // Validate user and topic ID's
     1067        // Validate user and object ID's
    10601068        $user_id  = bbp_get_user_id( $r['user_id'], true, true );
    10611069        $topic_id = bbp_get_topic_id( $r['topic_id'] );
    1062         if ( empty( $user_id ) || empty( $topic_id ) ) {
     1070        $forum_id = bbp_get_forum_id( $r['forum_id'] );
     1071        if ( empty( $user_id ) || ( empty( $topic_id ) && empty( $forum_id ) ) ) {
    10631072            return false;
    10641073        }
    10651074
    10661075        // No link if you can't edit yourself
    1067         if ( !current_user_can( 'edit_user', (int) $user_id ) ) {
     1076        if ( ! current_user_can( 'edit_user', (int) $user_id ) ) {
    10681077            return false;
    10691078        }
    10701079
    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 );
     1080        // Check if viewing a single forum
     1081        if ( empty( $topic_id ) && ! empty( $forum_id ) ) {
     1082
     1083            // Decide which link to show
     1084            $is_subscribed = bbp_is_user_subscribed_to_forum( $user_id, $forum_id );
     1085            if ( ! empty( $is_subscribed ) ) {
     1086                $text       = $r['unsubscribe'];
     1087                $query_args = array( 'action' => 'bbp_unsubscribe', 'forum_id' => $forum_id );
     1088            } else {
     1089                $text       = $r['subscribe'];
     1090                $query_args = array( 'action' => 'bbp_subscribe',   'forum_id' => $forum_id );
     1091            }
     1092
     1093            // Create the link based where the user is and if the user is
     1094            // subscribed already
     1095            if ( bbp_is_subscriptions() ) {
     1096                $permalink = bbp_get_subscriptions_permalink( $user_id );
     1097            } elseif ( bbp_is_single_forum() || bbp_is_single_reply() ) {
     1098                $permalink = bbp_get_forum_permalink( $forum_id );
     1099            } else {
     1100                $permalink = get_permalink();
     1101            }
     1102
     1103            $url  = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-subscription_' . $forum_id ) );
     1104            $sub  = $is_subscribed ? ' class="is-subscribed"' : '';
     1105            $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'] );
     1106
     1107            // Initial output is wrapped in a span, ajax output is hooked to this
     1108            if ( !empty( $wrap ) ) {
     1109                $html = '<span id="subscription-toggle">' . $html . '</span>';
     1110            }
     1111
    10761112        } else {
    1077             $text       = $r['subscribe'];
    1078             $query_args = array( 'action' => 'bbp_subscribe', 'topic_id' => $topic_id );
    1079         }
    1080 
    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 );
    1087         } else {
    1088             $permalink = get_permalink();
    1089         }
    1090 
    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'] );
    1094 
    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>';
     1113
     1114            // Decide which link to show
     1115            $is_subscribed = bbp_is_user_subscribed_to_topic( $user_id, $topic_id );
     1116            if ( ! empty( $is_subscribed ) ) {
     1117                $text       = $r['unsubscribe'];
     1118                $query_args = array( 'action' => 'bbp_unsubscribe', 'topic_id' => $topic_id );
     1119            } else {
     1120                $text       = $r['subscribe'];
     1121                $query_args = array( 'action' => 'bbp_subscribe',   'topic_id' => $topic_id );
     1122            }
     1123
     1124            // Create the link based where the user is and if the user is
     1125            // subscribed already
     1126            if ( bbp_is_subscriptions() ) {
     1127                $permalink = bbp_get_subscriptions_permalink( $user_id );
     1128            } elseif ( bbp_is_single_topic() || bbp_is_single_reply() ) {
     1129                $permalink = bbp_get_topic_permalink( $topic_id );
     1130            } else {
     1131                $permalink = get_permalink();
     1132            }
     1133
     1134            $url  = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-subscription_' . $topic_id ) );
     1135            $sub  = $is_subscribed ? ' class="is-subscribed"' : '';
     1136            $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'] );
     1137
     1138            // Initial output is wrapped in a span, ajax output is hooked to this
     1139            if ( !empty( $wrap ) ) {
     1140                $html = '<span id="subscription-toggle">' . $html . '</span>';
     1141            }
    10981142        }
    10991143
  • trunk/templates/default/bbpress-functions.php

    r4944 r5156  
    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 *************************************************/
     
    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
     
    178181        if ( bbp_use_wp_editor() ) {
    179182            wp_enqueue_script( 'jquery' );
     183        }
     184
     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 );
    180190        }
    181191
     
    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
     
    299311    public function localize_topic_script() {
    300312
    301         // Bail if not viewing a single topic
    302         if ( !bbp_is_single_topic() )
    303             return;
    304 
    305         wp_localize_script( 'bbpress-topic', 'bbpTopicJS', array(
    306             'bbp_ajaxurl'        => bbp_get_ajax_url(),
    307             'generic_ajax_error' => __( 'Something went wrong. Refresh your browser and try again.', 'bbpress' ),
    308             'is_user_logged_in'  => is_user_logged_in(),
    309             'fav_nonce'          => wp_create_nonce( 'toggle-favorite_' .     get_the_ID() ),
    310             'subs_nonce'         => wp_create_nonce( 'toggle-subscription_' . get_the_ID() )
    311         ) );
     313        // Single forum
     314        if ( bbp_is_single_forum() ) {
     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        // Single topic
     323        } elseif ( bbp_is_single_topic() ) {
     324            wp_localize_script( 'bbpress-topic', 'bbpTopicJS', array(
     325                'bbp_ajaxurl'        => bbp_get_ajax_url(),
     326                'generic_ajax_error' => __( 'Something went wrong. Refresh your browser and try again.', 'bbpress' ),
     327                'is_user_logged_in'  => is_user_logged_in(),
     328                'fav_nonce'          => wp_create_nonce( 'toggle-favorite_' .     get_the_ID() ),
     329                'subs_nonce'         => wp_create_nonce( 'toggle-subscription_' . get_the_ID() )
     330            ) );
     331        }
     332    }
     333
     334    /**
     335     * AJAX handler to Subscribe/Unsubscribe a user from a forum
     336     *
     337     * @since bbPress (r5155)
     338     *
     339     * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
     340     * @uses bbp_is_user_logged_in() To check if user is logged in
     341     * @uses bbp_get_current_user_id() To get the current user id
     342     * @uses current_user_can() To check if the current user can edit the user
     343     * @uses bbp_get_forum() To get the forum
     344     * @uses wp_verify_nonce() To verify the nonce
     345     * @uses bbp_is_user_subscribed() To check if the forum is in user's subscriptions
     346     * @uses bbp_remove_user_subscriptions() To remove the forum from user's subscriptions
     347     * @uses bbp_add_user_subscriptions() To add the forum from user's subscriptions
     348     * @uses bbp_ajax_response() To return JSON
     349     */
     350    public function ajax_forum_subscription() {
     351
     352        // Bail if subscriptions are not active
     353        if ( ! bbp_is_subscriptions_active() ) {
     354            bbp_ajax_response( false, __( 'Subscriptions are no longer active.', 'bbpress' ), 300 );
     355        }
     356
     357        // Bail if user is not logged in
     358        if ( ! is_user_logged_in() ) {
     359            bbp_ajax_response( false, __( 'Please login to subscribe to this forum.', 'bbpress' ), 301 );
     360        }
     361
     362        // Get user and forum data
     363        $user_id = bbp_get_current_user_id();
     364        $id      = intval( $_POST['id'] );
     365
     366        // Bail if user cannot add favorites for this user
     367        if ( ! current_user_can( 'edit_user', $user_id ) ) {
     368            bbp_ajax_response( false, __( 'You do not have permission to do this.', 'bbpress' ), 302 );
     369        }
     370
     371        // Get the forum
     372        $forum = bbp_get_forum( $id );
     373
     374        // Bail if forum cannot be found
     375        if ( empty( $forum ) ) {
     376            bbp_ajax_response( false, __( 'The forum could not be found.', 'bbpress' ), 303 );
     377        }
     378
     379        // Bail if user did not take this action
     380        if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'toggle-subscription_' . $forum->ID ) ) {
     381            bbp_ajax_response( false, __( 'Are you sure you meant to do that?', 'bbpress' ), 304 );
     382        }
     383
     384        // Take action
     385        $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 );
     386
     387        // Bail if action failed
     388        if ( empty( $status ) ) {
     389            bbp_ajax_response( false, __( 'The request was unsuccessful. Please try again.', 'bbpress' ), 305 );
     390        }
     391
     392        // Put subscription attributes in convenient array
     393        $attrs = array(
     394            'forum_id' => $forum->ID,
     395            'user_id'  => $user_id
     396        );
     397
     398        // Action succeeded
     399        bbp_ajax_response( true, bbp_get_forum_subscription_link( $attrs, $user_id, false ), 200 );
    312400    }
    313401
     
    317405     * @since bbPress (r3732)
    318406     *
     407     * @uses bbp_is_favorites_active() To check if favorites are active
     408     * @uses bbp_is_user_logged_in() To check if user is logged in
    319409     * @uses bbp_get_current_user_id() To get the current user id
    320410     * @uses current_user_can() To check if the current user can edit the user
     
    334424
    335425        // Bail if user is not logged in
    336         if ( !is_user_logged_in() ) {
     426        if ( ! is_user_logged_in() ) {
    337427            bbp_ajax_response( false, __( 'Please login to make this topic a favorite.', 'bbpress' ), 301 );
    338428        }
     
    343433
    344434        // Bail if user cannot add favorites for this user
    345         if ( !current_user_can( 'edit_user', $user_id ) ) {
     435        if ( ! current_user_can( 'edit_user', $user_id ) ) {
    346436            bbp_ajax_response( false, __( 'You do not have permission to do this.', 'bbpress' ), 302 );
    347437        }
     
    356446
    357447        // Bail if user did not take this action
    358         if ( !isset( $_POST['nonce'] ) || !wp_verify_nonce( $_POST['nonce'], 'toggle-favorite_' . $topic->ID ) ) {
     448        if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'toggle-favorite_' . $topic->ID ) ) {
    359449            bbp_ajax_response( false, __( 'Are you sure you meant to do that?', 'bbpress' ), 304 );
    360450        }
     
    384474     *
    385475     * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
     476     * @uses bbp_is_user_logged_in() To check if user is logged in
    386477     * @uses bbp_get_current_user_id() To get the current user id
    387478     * @uses current_user_can() To check if the current user can edit the user
     
    396487
    397488        // Bail if subscriptions are not active
    398         if ( !bbp_is_subscriptions_active() ) {
     489        if ( ! bbp_is_subscriptions_active() ) {
    399490            bbp_ajax_response( false, __( 'Subscriptions are no longer active.', 'bbpress' ), 300 );
    400491        }
    401492
    402493        // Bail if user is not logged in
    403         if ( !is_user_logged_in() ) {
     494        if ( ! is_user_logged_in() ) {
    404495            bbp_ajax_response( false, __( 'Please login to subscribe to this topic.', 'bbpress' ), 301 );
    405496        }
     
    410501
    411502        // Bail if user cannot add favorites for this user
    412         if ( !current_user_can( 'edit_user', $user_id ) ) {
     503        if ( ! current_user_can( 'edit_user', $user_id ) ) {
    413504            bbp_ajax_response( false, __( 'You do not have permission to do this.', 'bbpress' ), 302 );
    414505        }
     
    423514
    424515        // Bail if user did not take this action
    425         if ( !isset( $_POST['nonce'] ) || !wp_verify_nonce( $_POST['nonce'], 'toggle-subscription_' . $topic->ID ) ) {
     516        if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'toggle-subscription_' . $topic->ID ) ) {
    426517            bbp_ajax_response( false, __( 'Are you sure you meant to do that?', 'bbpress' ), 304 );
    427518        }
  • trunk/templates/default/bbpress/content-archive-forum.php

    r4971 r5156  
    2424    <?php bbp_breadcrumb(); ?>
    2525
     26    <?php bbp_forum_subscription_link(); ?>
     27
    2628    <?php do_action( 'bbp_template_before_forums_index' ); ?>
    2729
  • trunk/templates/default/bbpress/content-single-forum.php

    r4836 r5156  
    1313
    1414    <?php bbp_breadcrumb(); ?>
     15
     16    <?php bbp_forum_subscription_link(); ?>
    1517
    1618    <?php do_action( 'bbp_template_before_single_forum' ); ?>
  • trunk/templates/default/bbpress/content-single-topic-lead.php

    r5031 r5156  
    2222            <?php _e( 'Topic', 'bbpress' ); ?>
    2323
    24             <?php bbp_user_subscribe_link(); ?>
     24            <?php bbp_topic_subscription_link(); ?>
    2525
    26             <?php bbp_user_favorites_link(); ?>
     26            <?php bbp_topic_favorite_link(); ?>
    2727
    2828        </div><!-- .bbp-topic-content -->
  • trunk/templates/default/bbpress/loop-replies.php

    r4944 r5156  
    2424                <?php _e( 'Posts', 'bbpress' ); ?>
    2525
    26                 <?php bbp_user_subscribe_link(); ?>
     26                <?php bbp_topic_subscription_link(); ?>
    2727
    2828                <?php bbp_user_favorites_link(); ?>
  • trunk/templates/default/bbpress/loop-single-forum.php

    r4974 r5156  
    1313
    1414    <li class="bbp-forum-info">
     15
     16        <?php if ( bbp_is_user_home() && bbp_is_subscriptions() ) : ?>
     17
     18            <span class="bbp-row-actions">
     19
     20                <?php do_action( 'bbp_theme_before_forum_subscription_action' ); ?>
     21
     22                <?php bbp_forum_subscription_link( array( 'before' => '', 'subscribe' => '+', 'unsubscribe' => '&times;' ) ); ?>
     23
     24                <?php do_action( 'bbp_theme_after_forum_subscription_action' ); ?>
     25
     26            </span>
     27
     28        <?php endif; ?>
    1529
    1630        <?php do_action( 'bbp_theme_before_forum_title' ); ?>
  • trunk/templates/default/bbpress/loop-single-topic.php

    r4995 r5156  
    1818            <?php if ( bbp_is_favorites() ) : ?>
    1919
    20                 <span class="bbp-topic-action">
     20                <span class="bbp-row-actions">
    2121
    2222                    <?php do_action( 'bbp_theme_before_topic_favorites_action' ); ?>
    2323
    24                     <?php bbp_user_favorites_link( array( 'before' => '', 'favorite' => '+', 'favorited' => '&times;' ) ); ?>
     24                    <?php bbp_topic_favorite_link( array( 'before' => '', 'favorite' => '+', 'favorited' => '&times;' ) ); ?>
    2525
    2626                    <?php do_action( 'bbp_theme_after_topic_favorites_action' ); ?>
     
    3030            <?php elseif ( bbp_is_subscriptions() ) : ?>
    3131
    32                 <span class="bbp-topic-action">
     32                <span class="bbp-row-actions">
    3333
    3434                    <?php do_action( 'bbp_theme_before_topic_subscription_action' ); ?>
    3535
    36                     <?php bbp_user_subscribe_link( array( 'before' => '', 'subscribe' => '+', 'unsubscribe' => '&times;' ) ); ?>
     36                    <?php bbp_topic_subscription_link( array( 'before' => '', 'subscribe' => '+', 'unsubscribe' => '&times;' ) ); ?>
    3737
    3838                    <?php do_action( 'bbp_theme_after_topic_subscription_action' ); ?>
  • trunk/templates/default/bbpress/user-subscriptions.php

    r4733 r5156  
    1717
    1818            <div id="bbp-user-subscriptions" class="bbp-user-subscriptions">
    19                 <h2 class="entry-title"><?php _e( 'Subscribed Forum Topics', 'bbpress' ); ?></h2>
     19                <h2 class="entry-title"><?php _e( 'Subscribed Forums', 'bbpress' ); ?></h2>
    2020                <div class="bbp-user-section">
    2121
    22                     <?php if ( bbp_get_user_subscriptions() ) : ?>
     22                    <?php if ( bbp_get_user_forum_subscriptions() ) : ?>
     23
     24                        <?php bbp_get_template_part( 'loop', 'forums' ); ?>
     25
     26                    <?php else : ?>
     27
     28                        <p><?php bbp_is_user_home() ? _e( 'You are not currently subscribed to any forums.', 'bbpress' ) : _e( 'This user is not currently subscribed to any topics.', 'bbpress' ); ?></p>
     29
     30                    <?php endif; ?>
     31
     32                </div>
     33
     34                <h2 class="entry-title"><?php _e( 'Subscribed Topics', 'bbpress' ); ?></h2>
     35                <div class="bbp-user-section">
     36
     37                    <?php if ( bbp_get_user_topic_subscriptions() ) : ?>
    2338
    2439                        <?php bbp_get_template_part( 'pagination', 'topics' ); ?>
  • trunk/templates/default/css/bbpress-rtl.css

    r5085 r5156  
    174174    padding: 0;
    175175    text-transform: none;
    176 } 
    177 
    178 #bbpress-forums div.bbp-forum-author, 
     176}
     177
     178#bbpress-forums div.bbp-forum-author,
    179179#bbpress-forums div.bbp-topic-author,
    180180#bbpress-forums div.bbp-reply-author {
     
    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 {
     
    251251}
    252252
    253 div.bbp-forum-header, 
     253div.bbp-forum-header,
    254254div.bbp-topic-header,
    255255div.bbp-reply-header {
     
    474474}
    475475
    476 .bbp-forum-header a.bbp-forum-permalink, 
     476.bbp-forum-header a.bbp-forum-permalink,
    477477.bbp-topic-header a.bbp-topic-permalink,
    478478.bbp-reply-header a.bbp-reply-permalink {
     
    485485-------------------------------------------------------------- */
    486486
    487 .bbp-topic-action #favorite-toggle a {
     487.bbp-row-actions #favorite-toggle a {
    488488    text-decoration: none;
    489489    padding: 0 3px 1px;
     
    494494    font-size: 13px;
    495495    }
    496     .bbp-topic-action #favorite-toggle a:hover {
     496    .bbp-row-actions #favorite-toggle a:hover {
    497497        color: #5a5;
    498498        border-color: #7c7;
    499499        background-color: #beb;
    500500    }
    501     .bbp-topic-action #favorite-toggle span.is-favorite a {
     501    .bbp-row-actions #favorite-toggle span.is-favorite a {
    502502        color: #faa;
    503503        border: 1px solid #faa;
    504504        background-color: #fee;
    505505        }
    506         .bbp-topic-action #favorite-toggle span.is-favorite a:hover {
     506        .bbp-row-actions #favorite-toggle span.is-favorite a:hover {
    507507            color: #c88;
    508508            border-color: #c88;
     
    510510        }
    511511
    512 .bbp-topic-action #subscription-toggle a {
     512.bbp-row-actions #subscription-toggle a {
    513513    text-decoration: none;
    514514    padding: 0 3px 1px;
     
    519519    font-size: 13px;
    520520    }
    521     .bbp-topic-action #subscription-toggle a:hover {
     521    .bbp-row-actions #subscription-toggle a:hover {
    522522        color: #5a5;
    523523        border-color: #7c7;
    524524        background-color: #beb;
    525525    }
    526     .bbp-topic-action #subscription-toggle span.is-subscribed a {
     526    .bbp-row-actions #subscription-toggle span.is-subscribed a {
    527527        color: #faa;
    528528        border: 1px solid #faa;
    529529        background-color: #fee;
    530530        }
    531         .bbp-topic-action #subscription-toggle span.is-subscribed a:hover {
     531        .bbp-row-actions #subscription-toggle span.is-subscribed a:hover {
    532532            color: #c88;
    533533            border-color: #c88;
     
    11821182        margin-right: 0;
    11831183        word-wrap: break-word;
    1184     }   
     1184    }
    11851185    #bbpress-forums div.bbp-reply-author img.avatar {
    11861186        position: absolute;
     
    12051205        float: right;
    12061206    }
    1207     #bbpress-forums fieldset.bbp-form { 
     1207    #bbpress-forums fieldset.bbp-form {
    12081208        padding: 0 10px 10px;
    12091209    }
     
    13001300    #bbpress-forums li.bbp-body li.bbp-topic-voice-count,
    13011301    #bbpress-forums li.bbp-body li.bbp-topic-reply-count,
    1302     #bbpress-forums li.bbp-footer div.bbp-reply-author, 
     1302    #bbpress-forums li.bbp-footer div.bbp-reply-author,
    13031303    #bbpress-forums li.bbp-footer div.bbp-reply-content {
    13041304        width: 45%;
     
    13141314    }
    13151315    #bbpress-forums li.bbp-header li.bbp-forum-freshness,
    1316     #bbpress-forums li.bbp-header li.bbp-topic-freshness { 
     1316    #bbpress-forums li.bbp-header li.bbp-topic-freshness {
    13171317        text-align: center;
    13181318    }
     
    13391339        width: 40px;
    13401340        height: auto;
    1341     }   
    1342 }
     1341    }
     1342}
  • trunk/templates/default/css/bbpress.css

    r5085 r5156  
    485485-------------------------------------------------------------- */
    486486
    487 .bbp-topic-action #favorite-toggle a {
     487.bbp-row-actions #favorite-toggle a {
    488488    text-decoration: none;
    489489    padding: 0 3px 1px;
     
    494494    font-size: 13px;
    495495    }
    496     .bbp-topic-action #favorite-toggle a:hover {
     496    .bbp-row-actions #favorite-toggle a:hover {
    497497        color: #5a5;
    498498        border-color: #7c7;
    499499        background-color: #beb;
    500500    }
    501     .bbp-topic-action #favorite-toggle span.is-favorite a {
     501    .bbp-row-actions #favorite-toggle span.is-favorite a {
    502502        color: #faa;
    503503        border: 1px solid #faa;
    504504        background-color: #fee;
    505505        }
    506         .bbp-topic-action #favorite-toggle span.is-favorite a:hover {
     506        .bbp-row-actions #favorite-toggle span.is-favorite a:hover {
    507507            color: #c88;
    508508            border-color: #c88;
     
    510510        }
    511511
    512 .bbp-topic-action #subscription-toggle a {
     512.bbp-row-actions #subscription-toggle a {
    513513    text-decoration: none;
    514514    padding: 0 3px 1px;
     
    519519    font-size: 13px;
    520520    }
    521     .bbp-topic-action #subscription-toggle a:hover {
     521    .bbp-row-actions #subscription-toggle a:hover {
    522522        color: #5a5;
    523523        border-color: #7c7;
    524524        background-color: #beb;
    525525    }
    526     .bbp-topic-action #subscription-toggle span.is-subscribed a {
     526    .bbp-row-actions #subscription-toggle span.is-subscribed a {
    527527        color: #faa;
    528528        border: 1px solid #faa;
    529529        background-color: #fee;
    530530        }
    531         .bbp-topic-action #subscription-toggle span.is-subscribed a:hover {
     531        .bbp-row-actions #subscription-toggle span.is-subscribed a:hover {
    532532            color: #c88;
    533533            border-color: #c88;
Note: See TracChangeset for help on using the changeset viewer.