Skip to:
Content

bbPress.org


Ignore:
Timestamp:
04/28/2012 10:55:53 PM (9 years ago)
Author:
johnjamesjacoby
Message:

Allow user favorites and subscriptions to work per site on multisite installations. Props isaacchapman. Fixes #1818.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/plugin/bbp-includes/bbp-user-functions.php

    r3809 r3856  
    216216
    217217/**
     218 * Get the meta key for favorites, based on blog prefix
     219 *
     220 * @since bbPress (r3856)
     221 * @param int $blog_id Optional blog id to switch to
     222 * @global WPDB $wpdb
     223 * @return string
     224 */
     225function bbp_get_favorites_key( $blog_id = 0 ) {
     226    global $wpdb;
     227
     228    // If blog ID is passed, switch to that blog
     229    if ( !empty( $blog_id ) ) {
     230        switch_to_blog( $blog_id );
     231        $prefix = $wpdb->prefix;
     232        restore_current_blog();
     233
     234    // Use current blog
     235    } else {
     236        $prefix = $wpdb->prefix;
     237    }
     238
     239    return apply_filters( 'bbp_get_favorites_key', '_bbp_' . $prefix . 'favorites', $blog_id, $prefix );
     240}
     241
     242/**
    218243 * Get the users who have made the topic favorite
    219244 *
     
    233258
    234259    // Get the users who have favorited the topic
    235     $users = $wpdb->get_col( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = '_bbp_favorites' and FIND_IN_SET('{$topic_id}', meta_value) > 0" );
     260    $key   = bbp_get_favorites_key();
     261    $users = $wpdb->get_col( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = '{$key}' and FIND_IN_SET('{$topic_id}', meta_value) > 0" );
    236262    $users = apply_filters( 'bbp_get_topic_favoriters', $users, $topic_id );
    237263
     
    289315            return false;
    290316
    291         $favorites = (string) get_user_meta( $user_id, '_bbp_favorites', true );
     317        $favorites = (string) get_user_meta( $user_id, bbp_get_favorites_key(), true );
    292318        $favorites = (array) explode( ',', $favorites );
    293319        $favorites = array_filter( $favorites );
     
    370396        $favorites   = array_filter( $favorites );
    371397        $favorites   = (string) implode( ',', $favorites );
    372         update_user_meta( $user_id, '_bbp_favorites', $favorites );
     398        update_user_meta( $user_id, bbp_get_favorites_key(), $favorites );
    373399    }
    374400
     
    407433        if ( !empty( $favorites ) ) {
    408434            $favorites = implode( ',', $favorites );
    409             update_user_meta( $user_id, '_bbp_favorites', $favorites );
     435            update_user_meta( $user_id, bbp_get_favorites_key(), $favorites );
    410436        } else {
    411             delete_user_meta( $user_id, '_bbp_favorites' );
     437            delete_user_meta( $user_id, bbp_get_favorites_key() );
    412438        }
    413439    }
     
    520546
    521547/**
     548 * Get the meta key for subscriptions, based on blog prefix
     549 *
     550 * @since bbPress (r3856)
     551 * @global WPDB $wpdb
     552 * @return string
     553 */
     554function bbp_get_subscriptions_key( $blog_id = 0 ) {
     555    global $wpdb;
     556
     557    // If blog ID is passed, switch to that blog
     558    if ( !empty( $blog_id ) ) {
     559        switch_to_blog( $blog_id );
     560        $prefix = $wpdb->prefix;
     561        restore_current_blog();
     562
     563    // Use current blog
     564    } else {
     565        $prefix = $wpdb->prefix;
     566    }
     567
     568    return apply_filters( 'bbp_get_subscriptions_key', '_bbp_' . $prefix . 'subscriptions', $blog_id, $prefix );
     569}
     570
     571/**
    522572 * Get the users who have subscribed to the topic
    523573 *
     
    534584    global $wpdb;
    535585
     586    $key   = bbp_get_subscriptions_key();
    536587    $users = wp_cache_get( 'bbp_get_topic_subscribers_' . $topic_id, 'bbpress' );
    537588    if ( empty( $users ) ) {
    538         $users = $wpdb->get_col( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = '_bbp_subscriptions' and FIND_IN_SET('{$topic_id}', meta_value) > 0" );
     589        $users = $wpdb->get_col( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = '{$key}' and FIND_IN_SET('{$topic_id}', meta_value) > 0" );
    539590        wp_cache_set( 'bbp_get_topic_subscribers_' . $topic_id, $users, 'bbpress' );
    540591    }
     
    594645        return false;
    595646
    596     $subscriptions = (string) get_user_meta( $user_id, '_bbp_subscriptions', true );
     647    $subscriptions = (string) get_user_meta( $user_id, bbp_get_subscriptions_key(), true );
    597648    $subscriptions = (array) explode( ',', $subscriptions );
    598649    $subscriptions = array_filter( $subscriptions );
     
    678729        $subscriptions   = array_filter( $subscriptions );
    679730        $subscriptions   = (string) implode( ',', $subscriptions );
    680         update_user_meta( $user_id, '_bbp_subscriptions', $subscriptions );
     731        update_user_meta( $user_id, bbp_get_subscriptions_key(), $subscriptions );
    681732
    682733        wp_cache_delete( 'bbp_get_topic_subscribers_' . $topic_id, 'bbpress' );
     
    719770        if ( !empty( $subscriptions ) ) {
    720771            $subscriptions = implode( ',', $subscriptions );
    721             update_user_meta( $user_id, '_bbp_subscriptions', $subscriptions );
     772            update_user_meta( $user_id, bbp_get_subscriptions_key(), $subscriptions );
    722773        } else {
    723             delete_user_meta( $user_id, '_bbp_subscriptions' );
     774            delete_user_meta( $user_id, bbp_get_subscriptions_key() );
    724775        }
    725776
Note: See TracChangeset for help on using the changeset viewer.