Skip to:
Content

bbPress.org

Changeset 3856


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.

Location:
branches/plugin
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/plugin/bbp-admin/bbp-tools.php

    r3811 r3856  
    723723    $statement = __( 'Removing trashed topics from user favorites… %s', 'bbpress' );
    724724    $result    = __( 'Failed!', 'bbpress' );
    725 
    726     $users = $wpdb->get_results( "SELECT `user_id`, `meta_value` AS `favorites` FROM `$wpdb->usermeta` WHERE `meta_key` = '_bbp_favorites';" );
     725    $key       = bbp_get_favorites_key();
     726    $users     = $wpdb->get_results( "SELECT `user_id`, `meta_value` AS `favorites` FROM `{$wpdb->usermeta}` WHERE `meta_key` = '{$key}';" );
     727
    727728    if ( is_wp_error( $users ) )
    728729        return array( 1, sprintf( $statement, $result ) );
    729730
    730     $topics = $wpdb->get_col( "SELECT `ID` FROM `$wpdb->posts` WHERE `post_type` = '" . bbp_get_topic_post_type() . "' AND `post_status` = '" . bbp_get_public_status_id() . "';" );
     731    $topics = $wpdb->get_col( "SELECT `ID` FROM `{$wpdb->posts}` WHERE `post_type` = '" . bbp_get_topic_post_type() . "' AND `post_status` = '" . bbp_get_public_status_id() . "';" );
    731732
    732733    if ( is_wp_error( $topics ) )
     
    743744
    744745        $favorites = join( ',', $favorites );
    745         $values[] = "('$user->user_id', '_bbp_favorites', '$favorites')";
     746        $values[] = "('{$user->user_id}', '{$key}, '{$favorites}')";
    746747    }
    747748
     
    751752    }
    752753
    753     $sql_delete = "DELETE FROM `$wpdb->usermeta` WHERE `meta_key` = '_bbp_favorites';";
     754    $sql_delete = "DELETE FROM `{$wpdb->usermeta}` WHERE `meta_key` = '{$key}';";
    754755    if ( is_wp_error( $wpdb->query( $sql_delete ) ) )
    755756        return array( 4, sprintf( $statement, $result ) );
     
    782783    $statement = __( 'Removing trashed topics from user subscriptions… %s', 'bbpress' );
    783784    $result    = __( 'Failed!', 'bbpress' );
    784 
    785     $users = $wpdb->get_results( "SELECT `user_id`, `meta_value` AS `subscriptions` FROM `$wpdb->usermeta` WHERE `meta_key` = '_bbp_subscriptions';" );
     785    $key       = bbp_get_subscriptions_key();
     786    $users     = $wpdb->get_results( "SELECT `user_id`, `meta_value` AS `subscriptions` FROM `{$wpdb->usermeta}` WHERE `meta_key` = '{$key}';" );
     787
    786788    if ( is_wp_error( $users ) )
    787789        return array( 1, sprintf( $statement, $result ) );
    788790
    789     $topics = $wpdb->get_col( "SELECT `ID` FROM `$wpdb->posts` WHERE `post_type` = '" . bbp_get_topic_post_type() . "' AND `post_status` = '" . bbp_get_public_status_id() . "';" );
     791    $topics = $wpdb->get_col( "SELECT `ID` FROM `{$wpdb->posts}` WHERE `post_type` = '" . bbp_get_topic_post_type() . "' AND `post_status` = '" . bbp_get_public_status_id() . "';" );
    790792    if ( is_wp_error( $topics ) )
    791793        return array( 2, sprintf( $statement, $result ) );
     
    801803
    802804        $subscriptions = join( ',', $subscriptions );
    803         $values[] = "('$user->user_id', '_bbp_subscriptions', '$subscriptions')";
     805        $values[] = "('{$user->user_id}', '{$key}', '{$subscriptions}')";
    804806    }
    805807
     
    809811    }
    810812
    811     $sql_delete = "DELETE FROM `$wpdb->usermeta` WHERE `meta_key` = '_bbp_subscriptions';";
     813    $sql_delete = "DELETE FROM `{$wpdb->usermeta}` WHERE `meta_key` = '{$key}';";
    812814    if ( is_wp_error( $wpdb->query( $sql_delete ) ) )
    813815        return array( 4, sprintf( $statement, $result ) );
     
    816818    foreach ( $values as $chunk ) {
    817819        $chunk = "\n" . join( ",\n", $chunk );
    818         $sql_insert = "INSERT INTO `$wpdb->usermeta` (`user_id`, `meta_key`, `meta_value`) VALUES $chunk;";
     820        $sql_insert = "INSERT INTO `{$wpdb->usermeta}` (`user_id`, `meta_key`, `meta_value`) VALUES $chunk;";
    819821        if ( is_wp_error( $wpdb->query( $sql_insert ) ) )
    820822            return array( 5, sprintf( $statement, $result ) );
  • 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.