Skip to:
Content

bbPress.org


Ignore:
Timestamp:
05/22/2012 08:00:55 AM (13 years ago)
Author:
johnjamesjacoby
Message:

User Options:

  • Introduce bbp-user-options.php to handle all user option related code.
  • Add $user_options array to main bbPress class to allow for easy, static, per-user option overrides.
  • Tweak action order for option and user_option overrides in bbp-core-hooks.php.
  • Replace all appropriate user_meta calls with user_option calls to automatically prefix keys with blog ID.
  • Update favorites, subscriptions, and topic/reply count option keys to be per-blog in multisite configurations.
  • Fixes #1826.
File:
1 edited

Legend:

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

    r3910 r3911  
    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  */
    225 function 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 /**
    243218 * Get the users who have made the topic favorite
    244219 *
     
    258233
    259234    // Get the users who have favorited the topic
    260     $key   = bbp_get_favorites_key();
     235    $key   = $wpdb->prefix . '_bbp_favorites';
    261236    $users = $wpdb->get_col( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = '{$key}' and FIND_IN_SET('{$topic_id}', meta_value) > 0" );
    262237    $users = apply_filters( 'bbp_get_topic_favoriters', $users, $topic_id );
     
    305280 * @param int $user_id Optional. User id
    306281 * @uses bbp_get_user_id() To get the user id
    307  * @uses get_user_meta() To get the user favorites
     282 * @uses get_user_option() To get the user favorites
    308283 * @uses apply_filters() Calls 'bbp_get_user_favorites_topic_ids' with
    309284 *                        the favorites and user id
     
    315290        return false;
    316291
    317     $favorites = (string) get_user_meta( $user_id, bbp_get_favorites_key(), true );
     292    $favorites = (string) get_user_option( '_bbp_favorites', $user_id );
    318293    $favorites = (array) explode( ',', $favorites );
    319294    $favorites = array_filter( $favorites );
     
    379354 * @param int $topic_id Optional. Topic id
    380355 * @uses bbp_get_user_favorites_topic_ids() To get the user favorites
    381  * @uses update_user_meta() To update the user favorites
     356 * @uses update_user_option() To update the user favorites
    382357 * @uses do_action() Calls 'bbp_add_user_favorite' with the user id and topic id
    383358 * @return bool Always true
     
    396371        $favorites   = array_filter( $favorites );
    397372        $favorites   = (string) implode( ',', $favorites );
    398         update_user_meta( $user_id, bbp_get_favorites_key(), $favorites );
     373        update_user_option( $user_id, '_bbp_favorites', $favorites );
    399374    }
    400375
     
    412387 * @param int $topic_id Optional. Topic id
    413388 * @uses bbp_get_user_favorites_topic_ids() To get the user favorites
    414  * @uses update_user_meta() To update the user favorites
    415  * @uses delete_user_meta() To delete the user favorites meta
     389 * @uses update_user_option() To update the user favorites
     390 * @uses delete_user_option() To delete the user favorites meta
    416391 * @uses do_action() Calls 'bbp_remove_user_favorite' with the user & topic id
    417392 * @return bool True if the topic was removed from user's favorites, otherwise
     
    433408        if ( !empty( $favorites ) ) {
    434409            $favorites = implode( ',', $favorites );
    435             update_user_meta( $user_id, bbp_get_favorites_key(), $favorites );
     410            update_user_option( $user_id, '_bbp_favorites', $favorites );
    436411        } else {
    437             delete_user_meta( $user_id, bbp_get_favorites_key() );
     412            delete_user_option( $user_id, '_bbp_favorites' );
    438413        }
    439414    }
     
    546521
    547522/**
    548  * Get the meta key for subscriptions, based on blog prefix
    549  *
    550  * @since bbPress (r3856)
    551  * @global WPDB $wpdb
    552  * @return string
    553  */
    554 function 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 /**
    572523 * Get the users who have subscribed to the topic
    573524 *
     
    584535    global $wpdb;
    585536
    586     $key   = bbp_get_subscriptions_key();
     537    $key   = $wpdb->prefix . '_bbp_subscriptions';
    587538    $users = wp_cache_get( 'bbp_get_topic_subscribers_' . $topic_id, 'bbpress' );
    588539    if ( empty( $users ) ) {
     
    635586 * @param int $user_id Optional. User id
    636587 * @uses bbp_get_user_id() To get the user id
    637  * @uses get_user_meta() To get the user's subscriptions
     588 * @uses get_user_option() To get the user's subscriptions
    638589 * @uses apply_filters() Calls 'bbp_get_user_subscribed_topic_ids' with
    639590 *                        the subscriptions and user id
     
    645596        return false;
    646597
    647     $subscriptions = (string) get_user_meta( $user_id, bbp_get_subscriptions_key(), true );
     598    $subscriptions = (string) get_user_option( '_bbp_subscriptions', $user_id );
    648599    $subscriptions = (array) explode( ',', $subscriptions );
    649600    $subscriptions = array_filter( $subscriptions );
     
    711662 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions
    712663 * @uses bbp_get_topic() To get the topic
    713  * @uses update_user_meta() To update the user's subscriptions
     664 * @uses update_user_option() To update the user's subscriptions
    714665 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & topic id
    715666 * @return bool Always true
     
    729680        $subscriptions   = array_filter( $subscriptions );
    730681        $subscriptions   = (string) implode( ',', $subscriptions );
    731         update_user_meta( $user_id, bbp_get_subscriptions_key(), $subscriptions );
     682        update_user_option( $user_id, '_bbp_subscriptions', $subscriptions );
    732683
    733684        wp_cache_delete( 'bbp_get_topic_subscribers_' . $topic_id, 'bbpress' );
     
    747698 * @param int $topic_id Optional. Topic id
    748699 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions
    749  * @uses update_user_meta() To update the user's subscriptions
    750  * @uses delete_user_meta() To delete the user's subscriptions meta
     700 * @uses update_user_option() To update the user's subscriptions
     701 * @uses delete_user_option() To delete the user's subscriptions meta
    751702 * @uses do_action() Calls 'bbp_remove_user_subscription' with the user id and
    752703 *                    topic id
     
    770721        if ( !empty( $subscriptions ) ) {
    771722            $subscriptions = implode( ',', $subscriptions );
    772             update_user_meta( $user_id, bbp_get_subscriptions_key(), $subscriptions );
     723            update_user_option( $user_id, '_bbp_subscriptions', $subscriptions );
    773724        } else {
    774             delete_user_meta( $user_id, bbp_get_subscriptions_key() );
     725            delete_user_option( $user_id, '_bbp_subscriptions' );
    775726        }
    776727
     
    994945
    995946        // stops users being added to current blog when they are edited
    996         if ( $delete_role ) {
     947        if ( true === $delete_role ) {
    997948            delete_user_meta( $user_id, $blog_prefix . 'capabilities' );
    998949        }
     
    10701021    wp_cache_set( 'bbp_total_users', $bbp_total_users, 'bbpress' );
    10711022
    1072     return (int) apply_filters( 'bbp_get_total_users', (int) $bbp_total_users );
     1023    return apply_filters( 'bbp_get_total_users', (int) $bbp_total_users );
    10731024}
    10741025
Note: See TracChangeset for help on using the changeset viewer.