Skip to:
Content

bbPress.org


Ignore:
Timestamp:
07/14/2015 12:31:42 AM (10 years ago)
Author:
johnjamesjacoby
Message:

Abstraction: Use bbp_db(), bbp_rewrite() & friends, introduced in r5823 & r5826.

This commit improves the stability of bbPress in the WordPress environment by reducing global variable exposure. It also comes with minimal opcode improvements in some circumstances where $GLOBALS is preferred over defining via global statements.

Some additional surrounding cleanup directly related to functions & methods being altered is also being performed here.

Fixes #2786.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/users/functions.php

    r5805 r5827  
    188188    }
    189189
    190     global $wpdb;
    191 
    192     $key   = $wpdb->prefix . '_bbp_favorites';
    193     $users = wp_cache_get( 'bbp_get_topic_favoriters_' . $topic_id, 'bbpress_users' );
     190    $bbp_db = bbp_db();
     191    $key    = $bbp_db->prefix . '_bbp_favorites';
     192    $users  = wp_cache_get( 'bbp_get_topic_favoriters_' . $topic_id, 'bbpress_users' );
    194193    if ( false === $users ) {
    195         $users = $wpdb->get_col( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = '{$key}' and FIND_IN_SET('{$topic_id}', meta_value) > 0" );
     194        $users = $bbp_db->get_col( "SELECT user_id FROM {$bbp_db->usermeta} WHERE meta_key = '{$key}' and FIND_IN_SET('{$topic_id}', meta_value) > 0" );
    196195        wp_cache_set( 'bbp_get_topic_favoriters_' . $topic_id, $users, 'bbpress_users' );
    197196    }
     
    499498    }
    500499
    501     global $wpdb;
    502 
    503     $key   = $wpdb->prefix . '_bbp_forum_subscriptions';
    504     $users = wp_cache_get( 'bbp_get_forum_subscribers_' . $forum_id, 'bbpress_users' );
     500    $bbp_db = bbp_db();
     501    $key    = $bbp_db->prefix . '_bbp_forum_subscriptions';
     502    $users  = wp_cache_get( 'bbp_get_forum_subscribers_' . $forum_id, 'bbpress_users' );
    505503    if ( false === $users ) {
    506         $users = $wpdb->get_col( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = '{$key}' and FIND_IN_SET('{$forum_id}', meta_value) > 0" );
     504        $users = $bbp_db->get_col( "SELECT user_id FROM {$bbp_db->usermeta} WHERE meta_key = '{$key}' and FIND_IN_SET('{$forum_id}', meta_value) > 0" );
    507505        wp_cache_set( 'bbp_get_forum_subscribers_' . $forum_id, $users, 'bbpress_users' );
    508506    }
     
    527525    }
    528526
    529     global $wpdb;
    530 
    531     $key   = $wpdb->prefix . '_bbp_subscriptions';
    532     $users = wp_cache_get( 'bbp_get_topic_subscribers_' . $topic_id, 'bbpress_users' );
     527    $bbp_db = bbp_db();
     528    $key    = $bbp_db->prefix . '_bbp_subscriptions';
     529    $users  = wp_cache_get( 'bbp_get_topic_subscribers_' . $topic_id, 'bbpress_users' );
    533530    if ( false === $users ) {
    534         $users = $wpdb->get_col( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = '{$key}' and FIND_IN_SET('{$topic_id}', meta_value) > 0" );
     531        $users = $bbp_db->get_col( "SELECT user_id FROM {$bbp_db->usermeta} WHERE meta_key = '{$key}' and FIND_IN_SET('{$topic_id}', meta_value) > 0" );
    535532        wp_cache_set( 'bbp_get_topic_subscribers_' . $topic_id, $users, 'bbpress_users' );
    536533    }
     
    13161313 *                           is the one of the logged in user)
    13171314 * @uses get_option() To get the displayed user's new email id option
    1318  * @uses wpdb::prepare() To sanitize our sql query
    1319  * @uses wpdb::get_var() To execute our query and get back the variable
    1320  * @uses wpdb::query() To execute our query
    13211315 * @uses wp_update_user() To update the user
    13221316 * @uses delete_option() To delete the displayed user's email id option
     
    13311325 * @uses get_userdata() To get the user data
    13321326 * @uses is_email() To check if the string is an email id or not
    1333  * @uses wpdb::get_blog_prefix() To get the blog prefix
    13341327 * @uses is_network_admin() To check if the user is the network admin
    13351328 * @uses revoke_super_admin() To revoke super admin priviledges
     
    14491442 * @since bbPress (r5660)
    14501443 *
    1451  * @global object $wpdb
    1452  * @param  string $action
     1444 * @param string $action
    14531445 *
    14541446 * @uses bbp_is_user_home_edit()         To check if on the current users profile edit page
     
    15311523                    // Update signups table, if signups table & entry exists
    15321524                    // For Multisite & BuddyPress compatibility
    1533                     global $wpdb;
    1534                     if ( ! empty( $wpdb->signups ) && $wpdb->get_var( $wpdb->prepare( "SELECT user_login FROM {$wpdb->signups} WHERE user_login = %s", bbp_get_displayed_user_field( 'user_login', 'raw' ) ) ) ) {
    1535                         $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->signups} SET user_email = %s WHERE user_login = %s", $user->user_email, bbp_get_displayed_user_field( 'user_login', 'raw' ) ) );
     1525                    $bbp_db = bbp_db();
     1526                    if ( ! empty( $bbp_db->signups ) && $bbp_db->get_var( $bbp_db->prepare( "SELECT user_login FROM {$bbp_db->signups} WHERE user_login = %s", bbp_get_displayed_user_field( 'user_login', 'raw' ) ) ) ) {
     1527                        $bbp_db->query( $bbp_db->prepare( "UPDATE {$bbp_db->signups} SET user_email = %s WHERE user_login = %s", $user->user_email, bbp_get_displayed_user_field( 'user_login', 'raw' ) ) );
    15361528                    }
    15371529
     
    17271719 *
    17281720 * @since bbPress (r3633)
     1721 *
    17291722 * @param int $user_id User ID to get count for
    1730  * @global WPDB $wpdb
     1723 *
    17311724 * @uses bbp_get_user_id()
    17321725 * @uses get_posts_by_author_sql()
    17331726 * @uses bbp_get_topic_post_type()
    17341727 * @uses apply_filters()
     1728 *
    17351729 * @return int Raw DB count of topics
    17361730 */
     
    17411735    }
    17421736
    1743     global $wpdb;
    1744 
    1745     $where = get_posts_by_author_sql( bbp_get_topic_post_type(), true, $user_id );
    1746     $count = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->posts} {$where}" );
     1737    $bbp_db = bbp_db();
     1738    $where  = get_posts_by_author_sql( bbp_get_topic_post_type(), true, $user_id );
     1739    $count  = (int) $bbp_db->get_var( "SELECT COUNT(*) FROM {$bbp_db->posts} {$where}" );
    17471740
    17481741    return (int) apply_filters( 'bbp_get_user_topic_count_raw', $count, $user_id );
     
    17531746 *
    17541747 * @since bbPress (r3633)
     1748 *
    17551749 * @param int $user_id User ID to get count for
    1756  * @global WPDB $wpdb
     1750 *
    17571751 * @uses bbp_get_user_id()
    17581752 * @uses get_posts_by_author_sql()
    17591753 * @uses bbp_get_reply_post_type()
    17601754 * @uses apply_filters()
     1755 *
    17611756 * @return int Raw DB count of replies
    17621757 */
     
    17671762    }
    17681763
    1769     global $wpdb;
    1770 
    1771     $where = get_posts_by_author_sql( bbp_get_reply_post_type(), true, $user_id );
    1772     $count = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->posts} {$where}" );
     1764    $bbp_db = bbp_db();
     1765    $where  = get_posts_by_author_sql( bbp_get_reply_post_type(), true, $user_id );
     1766    $count  = (int) $bbp_db->get_var( "SELECT COUNT(*) FROM {$bbp_db->posts} {$where}" );
    17731767
    17741768    return (int) apply_filters( 'bbp_get_user_reply_count_raw', $count, $user_id );
     
    18461840
    18471841    // Add them up and filter them
    1848     $new_count = apply_filters( 'bbp_bump_user_reply_count', ( (int) $count + (int) $difference ), $user_id, $difference, $count );
     1842    $new_count = apply_filters( 'bbp_bump_user_reply_count', $user_reply_count, $user_id, $difference, $count );
    18491843
    18501844    return bbp_update_user_reply_count( $user_id, $new_count );
     
    20692063 *
    20702064 * @since bbPress (r3813)
    2071  * @global WPDB $wpdb
    20722065 */
    20732066function bbp_user_maybe_convert_pass() {
     
    20832076    }
    20842077
    2085     global $wpdb;
    2086 
    20872078    // Bail if no user password to convert
    2088     $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->users} INNER JOIN {$wpdb->usermeta} ON user_id = ID WHERE meta_key = '_bbp_class' AND user_login = '%s' LIMIT 1", $username ) );
     2079    $bbp_db = bbp_db();
     2080    $query  = $bbp_db->prepare( "SELECT * FROM {$bbp_db->users} INNER JOIN {$bbp_db->usermeta} ON user_id = ID WHERE meta_key = '_bbp_class' AND user_login = '%s' LIMIT 1", $username );
     2081    $row    = $bbp_db->get_row( $query );
    20892082    if ( empty( $row ) || is_wp_error( $row ) ) {
    20902083        return;
Note: See TracChangeset for help on using the changeset viewer.