Skip to:
Content

bbPress.org

Changeset 5827


Ignore:
Timestamp:
07/14/2015 12:31:42 AM (9 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.

Location:
trunk/src/includes
Files:
17 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/admin/admin.php

    r5809 r5827  
    693693     */
    694694    public function suggest_topic() {
    695         global $wpdb;
    696695
    697696        // Bail early if no request
     
    710709        // Try to get some topics
    711710        $topics = get_posts( array(
    712             's'         => $wpdb->esc_like( $_REQUEST['q'] ),
     711            's'         => bbp_db()->esc_like( $_REQUEST['q'] ),
    713712            'post_type' => bbp_get_topic_post_type()
    714713        ) );
     
    729728     */
    730729    public function suggest_user() {
    731         global $wpdb;
    732730
    733731        // Bail early if no request
     
    746744        // Try to get some users
    747745        $users_query = new WP_User_Query( array(
    748             'search'         => '*' . $wpdb->esc_like( $_REQUEST['q'] ) . '*',
     746            'search'         => '*' . bbp_db()->esc_like( $_REQUEST['q'] ) . '*',
    749747            'fields'         => array( 'ID', 'user_nicename' ),
    750748            'search_columns' => array( 'ID', 'user_nicename', 'user_email' ),
     
    948946     * @since bbPress (r3689)
    949947     *
    950      * @global WPDB $wpdb
    951948     * @uses get_blog_option()
    952949     * @uses wp_remote_get()
     
    995992     * @since bbPress (r3689)
    996993     *
    997      * @global WPDB $wpdb
    998994     * @uses get_blog_option()
    999995     * @uses wp_remote_get()
    1000996     */
    1001997    public static function network_update_screen() {
    1002         global $wpdb;
     998        $bbp_db = bbp_db();
    1003999
    10041000        // Get action
     
    10191015
    10201016                // Get blogs 5 at a time
    1021                 $blogs = $wpdb->get_results( "SELECT * FROM {$wpdb->blogs} WHERE site_id = '{$wpdb->siteid}' AND spam = '0' AND deleted = '0' AND archived = '0' ORDER BY registered DESC LIMIT {$n}, 5", ARRAY_A );
     1017                $blogs = $bbp_db->get_results( "SELECT * FROM {$bbp_db->blogs} WHERE site_id = '{$bbp_db->siteid}' AND spam = '0' AND deleted = '0' AND archived = '0' ORDER BY registered DESC LIMIT {$n}, 5", ARRAY_A );
    10221018
    10231019                // No blogs so all done!
  • trunk/src/includes/admin/converter.php

    r5770 r5827  
    630630     */
    631631    public function sync_table( $drop = false ) {
    632         global $wpdb;
    633 
    634         $table_name = $wpdb->prefix . 'bbp_converter_translator';
    635         if ( ! empty( $drop ) && $wpdb->get_var( "SHOW TABLES LIKE '{$table_name}'" ) == $table_name ) {
    636             $wpdb->query( "DROP TABLE {$table_name}" );
     632
     633        $bbp_db     = bbp_db();
     634        $table_name = $bbp_db->prefix . 'bbp_converter_translator';
     635        if ( ! empty( $drop ) && $bbp_db->get_var( "SHOW TABLES LIKE '{$table_name}'" ) == $table_name ) {
     636            $bbp_db->query( "DROP TABLE {$table_name}" );
    637637        }
    638638
    639639        require_once( ABSPATH . '/wp-admin/includes/upgrade.php' );
    640640
    641         if ( !empty( $wpdb->charset ) ) {
    642             $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
    643         }
    644 
    645         if ( !empty( $wpdb->collate ) ) {
    646             $charset_collate .= " COLLATE $wpdb->collate";
     641        if ( !empty( $bbp_db->charset ) ) {
     642            $charset_collate = "DEFAULT CHARACTER SET $bbp_db->charset";
     643        }
     644
     645        if ( !empty( $bbp_db->collate ) ) {
     646            $charset_collate .= " COLLATE $bbp_db->collate";
    647647        }
    648648
     
    676676
    677677    /**
    678      * @var object This is the connection to the WordPress datbase.
     678     * @var object This is the connection to the WordPress database.
    679679     */
    680680    protected $wpdb;
     
    740740
    741741    private function setup_globals() {
    742         global $wpdb;
    743742
    744743        /** Get database connections ******************************************/
    745744
    746         $this->wpdb         = $wpdb;
     745        $this->wpdb         = bbp_db();
    747746        $this->max_rows     = (int) $_POST['_bbp_converter_rows'];
    748747        $this->opdb         = new wpdb( $_POST['_bbp_converter_db_user'], $_POST['_bbp_converter_db_pass'], $_POST['_bbp_converter_db_name'], $_POST['_bbp_converter_db_server'] );
  • trunk/src/includes/admin/tools.php

    r5783 r5827  
    241241 */
    242242function bbp_admin_tools_feedback( $message, $class = false ) {
     243
     244    // Dismiss button
     245    $dismiss = '<button type="button" class="notice-dismiss"><span class="screen-reader-text">' . __( 'Dismiss this notice.', 'bbpress' ) . '</span></button>';
     246
     247    // One message as string
    243248    if ( is_string( $message ) ) {
    244249        $message = '<p>' . $message . '</p>';
    245         $class = $class ? $class : 'updated';
     250        $class   = $class ? $class : 'updated';
     251
     252    // Messages as objects
    246253    } elseif ( is_wp_error( $message ) ) {
    247         $errors = $message->get_error_messages();
     254        $errors  = $message->get_error_messages();
    248255
    249256        switch ( count( $errors ) ) {
     
    260267        }
    261268
    262         $class = $class ? $class : 'error';
     269        $class = $class ? $class : 'is-error';
    263270    } else {
    264271        return false;
    265272    }
    266273
    267     $message = '<div id="message" class="' . esc_attr( $class ) . '">' . $message . '</div>';
     274    // Assemble the message
     275    $message = '<div id="message" class="is-dismissible notice ' . esc_attr( $class ) . '">' . $message . $dismiss . '</div>';
    268276    $message = str_replace( "'", "\'", $message );
     277
     278    // Ugh
    269279    $lambda  = create_function( '', "echo '$message';" );
    270 
    271280    add_action( 'admin_notices', $lambda );
    272281
     
    323332 */
    324333function bbp_admin_repair_topic_reply_count() {
    325     global $wpdb;
    326 
     334
     335    // Define variables
     336    $bbp_db    = bbp_db();
    327337    $statement = __( 'Counting the number of replies in each topic&hellip; %s', 'bbpress' );
    328338    $result    = __( 'Failed!', 'bbpress' );
     
    335345
    336346    // Delete the meta key _bbp_reply_count for each topic
    337     $sql_delete = "DELETE `postmeta` FROM `{$wpdb->postmeta}` AS `postmeta`
    338                         LEFT JOIN `{$wpdb->posts}` AS `posts` ON `posts`.`ID` = `postmeta`.`post_id`
     347    $sql_delete = "DELETE `postmeta` FROM `{$bbp_db->postmeta}` AS `postmeta`
     348                        LEFT JOIN `{$bbp_db->posts}` AS `posts` ON `posts`.`ID` = `postmeta`.`post_id`
    339349                        WHERE `posts`.`post_type` = '{$tpt}'
    340350                        AND `postmeta`.`meta_key` = '_bbp_reply_count'";
    341351
    342     if ( is_wp_error( $wpdb->query( $sql_delete ) ) ) {
     352    if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
    343353        return array( 1, sprintf( $statement, $result ) );
    344354    }
    345355
    346356    // Recalculate the meta key _bbp_reply_count for each topic
    347     $sql = "INSERT INTO `{$wpdb->postmeta}` (`post_id`, `meta_key`, `meta_value`) (
     357    $sql = "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`) (
    348358            SELECT `topics`.`ID` AS `post_id`, '_bbp_reply_count' AS `meta_key`, COUNT(`replies`.`ID`) As `meta_value`
    349                 FROM `{$wpdb->posts}` AS `topics`
    350                     LEFT JOIN `{$wpdb->posts}` as `replies`
     359                FROM `{$bbp_db->posts}` AS `topics`
     360                    LEFT JOIN `{$bbp_db->posts}` as `replies`
    351361                        ON  `replies`.`post_parent` = `topics`.`ID`
    352362                        AND `replies`.`post_status` = '{$pps}'
     
    356366                GROUP BY `topics`.`ID`);";
    357367
    358     if ( is_wp_error( $wpdb->query( $sql ) ) ) {
     368    if ( is_wp_error( $bbp_db->query( $sql ) ) ) {
    359369        return array( 2, sprintf( $statement, $result ) );
    360370    }
     
    377387 */
    378388function bbp_admin_repair_topic_voice_count() {
    379     global $wpdb;
    380 
     389
     390    // Define variables
     391    $bbp_db    = bbp_db();
    381392    $statement = __( 'Counting the number of voices in each topic&hellip; %s', 'bbpress' );
    382393    $result    = __( 'Failed!', 'bbpress' );
    383394
    384     $sql_delete = "DELETE FROM `{$wpdb->postmeta}` WHERE `meta_key` = '_bbp_voice_count';";
    385     if ( is_wp_error( $wpdb->query( $sql_delete ) ) ) {
     395    $sql_delete = "DELETE FROM `{$bbp_db->postmeta}` WHERE `meta_key` = '_bbp_voice_count';";
     396    if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
    386397        return array( 1, sprintf( $statement, $result ) );
    387398    }
     
    393404    $cps = bbp_get_closed_status_id();
    394405
    395     $sql = "INSERT INTO `{$wpdb->postmeta}` (`post_id`, `meta_key`, `meta_value`) (
     406    $sql = "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`) (
    396407            SELECT `postmeta`.`meta_value`, '_bbp_voice_count', COUNT(DISTINCT `post_author`) as `meta_value`
    397                 FROM `{$wpdb->posts}` AS `posts`
    398                 LEFT JOIN `{$wpdb->postmeta}` AS `postmeta`
     408                FROM `{$bbp_db->posts}` AS `posts`
     409                LEFT JOIN `{$bbp_db->postmeta}` AS `postmeta`
    399410                    ON `posts`.`ID` = `postmeta`.`post_id`
    400411                    AND `postmeta`.`meta_key` = '_bbp_topic_id'
     
    404415                GROUP BY `postmeta`.`meta_value`);";
    405416
    406     if ( is_wp_error( $wpdb->query( $sql ) ) ) {
     417    if ( is_wp_error( $bbp_db->query( $sql ) ) ) {
    407418        return array( 2, sprintf( $statement, $result ) );
    408419    }
     
    424435 */
    425436function bbp_admin_repair_topic_hidden_reply_count() {
    426     global $wpdb;
    427 
     437
     438    // Define variables
     439    $bbp_db    = bbp_db();
    428440    $statement = __( 'Counting the number of spammed and trashed replies in each topic&hellip; %s', 'bbpress' );
    429441    $result    = __( 'Failed!', 'bbpress' );
    430442
    431     $sql_delete = "DELETE FROM `{$wpdb->postmeta}` WHERE `meta_key` = '_bbp_reply_count_hidden';";
    432     if ( is_wp_error( $wpdb->query( $sql_delete ) ) ) {
     443    $sql_delete = "DELETE FROM `{$bbp_db->postmeta}` WHERE `meta_key` = '_bbp_reply_count_hidden';";
     444    if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
    433445        return array( 1, sprintf( $statement, $result ) );
    434446    }
     
    439451    $sps = bbp_get_spam_status_id();
    440452
    441     $sql = "INSERT INTO `{$wpdb->postmeta}` (`post_id`, `meta_key`, `meta_value`) (SELECT `post_parent`, '_bbp_reply_count_hidden', COUNT(`post_status`) as `meta_value` FROM `{$wpdb->posts}` WHERE `post_type` = '{$rpt}' AND `post_status` IN ( '{$tps}', '{$sps}' ) GROUP BY `post_parent`);";
    442     if ( is_wp_error( $wpdb->query( $sql ) ) ) {
     453    $sql = "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`) (SELECT `post_parent`, '_bbp_reply_count_hidden', COUNT(`post_status`) as `meta_value` FROM `{$bbp_db->posts}` WHERE `post_type` = '{$rpt}' AND `post_status` IN ( '{$tps}', '{$sps}' ) GROUP BY `post_parent`);";
     454    if ( is_wp_error( $bbp_db->query( $sql ) ) ) {
    443455        return array( 2, sprintf( $statement, $result ) );
    444456    }
     
    452464 * @since bbPress (r4395)
    453465 *
    454  * @global WPDB $wpdb
    455466 * @uses bbp_get_forum_post_type() To get the forum post type
    456467 * @return If a wp_error() occurs and no converted forums are found
    457468 */
    458469function bbp_admin_repair_group_forum_relationship() {
    459     global $wpdb;
    460 
     470
     471    // Define variables
     472    $bbp_db    = bbp_db();
    461473    $statement = __( 'Repairing BuddyPress group-forum relationships&hellip; %s', 'bbpress' );
    462     $g_count     = 0;
    463     $f_count     = 0;
    464     $s_count     = 0;
     474    $g_count   = 0;
     475    $f_count   = 0;
     476    $s_count   = 0;
    465477
    466478    // Copy the BuddyPress filter here, incase BuddyPress is not active
    467     $prefix            = apply_filters( 'bp_core_get_table_prefix', $wpdb->base_prefix );
     479    $prefix            = apply_filters( 'bp_core_get_table_prefix', $bbp_db->base_prefix );
    468480    $groups_table      = $prefix . 'bp_groups';
    469481    $groups_meta_table = $prefix . 'bp_groups_groupmeta';
    470482
    471483    // Get the converted forum IDs
    472     $forum_ids = $wpdb->query( "SELECT `forum`.`ID`, `forummeta`.`meta_value`
    473                                 FROM `{$wpdb->posts}` AS `forum`
    474                                     LEFT JOIN `{$wpdb->postmeta}` AS `forummeta`
     484    $forum_ids = $bbp_db->query( "SELECT `forum`.`ID`, `forummeta`.`meta_value`
     485                                FROM `{$bbp_db->posts}` AS `forum`
     486                                    LEFT JOIN `{$bbp_db->postmeta}` AS `forummeta`
    475487                                        ON `forum`.`ID` = `forummeta`.`post_id`
    476488                                        AND `forummeta`.`meta_key` = '_bbp_old_forum_id'
     
    479491
    480492    // Bail if forum IDs returned an error
    481     if ( is_wp_error( $forum_ids ) || empty( $wpdb->last_result ) ) {
     493    if ( is_wp_error( $forum_ids ) || empty( $bbp_db->last_result ) ) {
    482494        return array( 2, sprintf( $statement, __( 'Failed!', 'bbpress' ) ) );
    483495    }
    484496
    485497    // Stash the last results
    486     $results = $wpdb->last_result;
     498    $results = $bbp_db->last_result;
    487499
    488500    // Update each group forum
     
    495507
    496508        // Attempt to update group meta
    497         $updated = $wpdb->query( "UPDATE `{$groups_meta_table}` SET `meta_value` = '{$group_forums->ID}' WHERE `meta_key` = 'forum_id' AND `meta_value` = '{$group_forums->meta_value}';" );
     509        $updated = $bbp_db->query( "UPDATE `{$groups_meta_table}` SET `meta_value` = '{$group_forums->ID}' WHERE `meta_key` = 'forum_id' AND `meta_value` = '{$group_forums->meta_value}';" );
    498510
    499511        // Bump the count
     
    503515
    504516        // Update group to forum relationship data
    505         $group_id = (int) $wpdb->get_var( "SELECT `group_id` FROM `{$groups_meta_table}` WHERE `meta_key` = 'forum_id' AND `meta_value` = '{$group_forums->ID}';" );
     517        $group_id = (int) $bbp_db->get_var( "SELECT `group_id` FROM `{$groups_meta_table}` WHERE `meta_key` = 'forum_id' AND `meta_value` = '{$group_forums->ID}';" );
    506518        if ( !empty( $group_id ) ) {
    507519
     
    510522
    511523            // Get the group status
    512             $group_status = $wpdb->get_var( "SELECT `status` FROM `{$groups_table}` WHERE `id` = '{$group_id}';" );
     524            $group_status = $bbp_db->get_var( "SELECT `status` FROM `{$groups_table}` WHERE `id` = '{$group_id}';" );
    513525
    514526            // Sync up forum visibility based on group status
     
    597609 */
    598610function bbp_admin_repair_forum_topic_count() {
    599     global $wpdb;
    600 
     611
     612    // Define variables
     613    $bbp_db    = bbp_db();
    601614    $statement = __( 'Counting the number of topics in each forum&hellip; %s', 'bbpress' );
    602615    $result    = __( 'Failed!', 'bbpress' );
    603616
    604     $sql_delete = "DELETE FROM {$wpdb->postmeta} WHERE meta_key IN ( '_bbp_topic_count', '_bbp_total_topic_count', '_bbp_topic_count_hidden' );";
    605     if ( is_wp_error( $wpdb->query( $sql_delete ) ) ) {
     617    $sql_delete = "DELETE FROM {$bbp_db->postmeta} WHERE meta_key IN ( '_bbp_topic_count', '_bbp_total_topic_count', '_bbp_topic_count_hidden' );";
     618    if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
    606619        return array( 1, sprintf( $statement, $result ) );
    607620    }
     
    633646 */
    634647function bbp_admin_repair_forum_reply_count() {
    635     global $wpdb;
    636 
     648
     649    // Define variables
     650    $bbp_db    = bbp_db();
    637651    $statement = __( 'Counting the number of replies in each forum&hellip; %s', 'bbpress' );
    638652    $result    = __( 'Failed!', 'bbpress' );
     
    642656
    643657    // Delete the meta keys _bbp_reply_count and _bbp_total_reply_count for each forum
    644     $sql_delete = "DELETE `postmeta` FROM `{$wpdb->postmeta}` AS `postmeta`
    645                         LEFT JOIN `{$wpdb->posts}` AS `posts` ON `posts`.`ID` = `postmeta`.`post_id`
     658    $sql_delete = "DELETE `postmeta` FROM `{$bbp_db->postmeta}` AS `postmeta`
     659                        LEFT JOIN `{$bbp_db->posts}` AS `posts` ON `posts`.`ID` = `postmeta`.`post_id`
    646660                        WHERE `posts`.`post_type` = '{$fpt}'
    647661                        AND `postmeta`.`meta_key` = '_bbp_reply_count'
    648662                        OR `postmeta`.`meta_key` = '_bbp_total_reply_count'";
    649663
    650     if ( is_wp_error( $wpdb->query( $sql_delete ) ) ) {
     664    if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
    651665        return array( 1, sprintf( $statement, $result ) );
    652666    }
     
    677691 */
    678692function bbp_admin_repair_user_topic_count() {
    679     global $wpdb;
    680 
     693
     694    // Define variables
     695    $bbp_db      = bbp_db();
    681696    $statement   = __( 'Counting the number of topics each user has created&hellip; %s', 'bbpress' );
    682697    $result      = __( 'Failed!', 'bbpress' );
    683     $sql_select  = "SELECT `post_author`, COUNT(DISTINCT `ID`) as `_count` FROM `{$wpdb->posts}` WHERE `post_type` = '" . bbp_get_topic_post_type() . "' AND `post_status` = '" . bbp_get_public_status_id() . "' GROUP BY `post_author`;";
    684     $insert_rows = $wpdb->get_results( $sql_select );
     698
     699    $sql_select  = "SELECT `post_author`, COUNT(DISTINCT `ID`) as `_count` FROM `{$bbp_db->posts}` WHERE `post_type` = '" . bbp_get_topic_post_type() . "' AND `post_status` = '" . bbp_get_public_status_id() . "' GROUP BY `post_author`;";
     700    $insert_rows = $bbp_db->get_results( $sql_select );
    685701
    686702    if ( is_wp_error( $insert_rows ) ) {
     
    688704    }
    689705
    690     $key           = $wpdb->prefix . '_bbp_topic_count';
     706    $key           = $bbp_db->prefix . '_bbp_topic_count';
    691707    $insert_values = array();
    692708    foreach ( $insert_rows as $insert_row ) {
     
    698714    }
    699715
    700     $sql_delete = "DELETE FROM `{$wpdb->usermeta}` WHERE `meta_key` = '{$key}';";
    701     if ( is_wp_error( $wpdb->query( $sql_delete ) ) ) {
     716    $sql_delete = "DELETE FROM `{$bbp_db->usermeta}` WHERE `meta_key` = '{$key}';";
     717    if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
    702718        return array( 3, sprintf( $statement, $result ) );
    703719    }
     
    705721    foreach ( array_chunk( $insert_values, 10000 ) as $chunk ) {
    706722        $chunk = "\n" . implode( ",\n", $chunk );
    707         $sql_insert = "INSERT INTO `{$wpdb->usermeta}` (`user_id`, `meta_key`, `meta_value`) VALUES $chunk;";
    708 
    709         if ( is_wp_error( $wpdb->query( $sql_insert ) ) ) {
     723        $sql_insert = "INSERT INTO `{$bbp_db->usermeta}` (`user_id`, `meta_key`, `meta_value`) VALUES {$chunk};";
     724
     725        if ( is_wp_error( $bbp_db->query( $sql_insert ) ) ) {
    710726            return array( 4, sprintf( $statement, $result ) );
    711727        }
     
    727743 */
    728744function bbp_admin_repair_user_reply_count() {
    729     global $wpdb;
    730 
     745
     746    // Define variables
     747    $bbp_db    = bbp_db();
    731748    $statement   = __( 'Counting the number of topics to which each user has replied&hellip; %s', 'bbpress' );
    732749    $result      = __( 'Failed!', 'bbpress' );
    733     $sql_select  = "SELECT `post_author`, COUNT(DISTINCT `ID`) as `_count` FROM `{$wpdb->posts}` WHERE `post_type` = '" . bbp_get_reply_post_type() . "' AND `post_status` = '" . bbp_get_public_status_id() . "' GROUP BY `post_author`;";
    734     $insert_rows = $wpdb->get_results( $sql_select );
     750
     751    $sql_select  = "SELECT `post_author`, COUNT(DISTINCT `ID`) as `_count` FROM `{$bbp_db->posts}` WHERE `post_type` = '" . bbp_get_reply_post_type() . "' AND `post_status` = '" . bbp_get_public_status_id() . "' GROUP BY `post_author`;";
     752    $insert_rows = $bbp_db->get_results( $sql_select );
    735753
    736754    if ( is_wp_error( $insert_rows ) ) {
     
    738756    }
    739757
    740     $key           = $wpdb->prefix . '_bbp_reply_count';
     758    $key           = $bbp_db->prefix . '_bbp_reply_count';
    741759    $insert_values = array();
    742760    foreach ( $insert_rows as $insert_row ) {
     
    748766    }
    749767
    750     $sql_delete = "DELETE FROM `{$wpdb->usermeta}` WHERE `meta_key` = '{$key}';";
    751     if ( is_wp_error( $wpdb->query( $sql_delete ) ) ) {
     768    $sql_delete = "DELETE FROM `{$bbp_db->usermeta}` WHERE `meta_key` = '{$key}';";
     769    if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
    752770        return array( 3, sprintf( $statement, $result ) );
    753771    }
     
    755773    foreach ( array_chunk( $insert_values, 10000 ) as $chunk ) {
    756774        $chunk = "\n" . implode( ",\n", $chunk );
    757         $sql_insert = "INSERT INTO `{$wpdb->usermeta}` (`user_id`, `meta_key`, `meta_value`) VALUES $chunk;";
    758 
    759         if ( is_wp_error( $wpdb->query( $sql_insert ) ) ) {
     775        $sql_insert = "INSERT INTO `{$bbp_db->usermeta}` (`user_id`, `meta_key`, `meta_value`) VALUES {$chunk};";
     776
     777        if ( is_wp_error( $bbp_db->query( $sql_insert ) ) ) {
    760778            return array( 4, sprintf( $statement, $result ) );
    761779        }
     
    777795 */
    778796function bbp_admin_repair_user_favorites() {
    779     global $wpdb;
    780 
     797
     798    // Define variables
     799    $bbp_db    = bbp_db();
    781800    $statement = __( 'Removing trashed topics from user favorites&hellip; %s', 'bbpress' );
    782801    $result    = __( 'Failed!', 'bbpress' );
    783     $key       = $wpdb->prefix . '_bbp_favorites';
    784     $users     = $wpdb->get_results( "SELECT `user_id`, `meta_value` AS `favorites` FROM `{$wpdb->usermeta}` WHERE `meta_key` = '{$key}';" );
     802
     803    $key       = $bbp_db->prefix . '_bbp_favorites';
     804    $users     = $bbp_db->get_results( "SELECT `user_id`, `meta_value` AS `favorites` FROM `{$bbp_db->usermeta}` WHERE `meta_key` = '{$key}';" );
    785805
    786806    if ( is_wp_error( $users ) ) {
     
    788808    }
    789809
    790     $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() . "';" );
     810    $topics = $bbp_db->get_col( "SELECT `ID` FROM `{$bbp_db->posts}` WHERE `post_type` = '" . bbp_get_topic_post_type() . "' AND `post_status` = '" . bbp_get_public_status_id() . "';" );
    791811
    792812    if ( is_wp_error( $topics ) ) {
     
    817837    }
    818838
    819     $sql_delete = "DELETE FROM `{$wpdb->usermeta}` WHERE `meta_key` = '{$key}';";
    820     if ( is_wp_error( $wpdb->query( $sql_delete ) ) ) {
     839    $sql_delete = "DELETE FROM `{$bbp_db->usermeta}` WHERE `meta_key` = '{$key}';";
     840    if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
    821841        return array( 4, sprintf( $statement, $result ) );
    822842    }
     
    824844    foreach ( array_chunk( $values, 10000 ) as $chunk ) {
    825845        $chunk = "\n" . implode( ",\n", $chunk );
    826         $sql_insert = "INSERT INTO `$wpdb->usermeta` (`user_id`, `meta_key`, `meta_value`) VALUES $chunk;";
    827         if ( is_wp_error( $wpdb->query( $sql_insert ) ) ) {
     846        $sql_insert = "INSERT INTO `{$bbp_db->usermeta}` (`user_id`, `meta_key`, `meta_value`) VALUES {$chunk};";
     847        if ( is_wp_error( $bbp_db->query( $sql_insert ) ) ) {
    828848            return array( 5, sprintf( $statement, $result ) );
    829849        }
     
    845865 */
    846866function bbp_admin_repair_user_topic_subscriptions() {
    847     global $wpdb;
    848 
     867
     868    // Define variables
     869    $bbp_db    = bbp_db();
    849870    $statement = __( 'Removing trashed topics from user subscriptions&hellip; %s', 'bbpress' );
    850871    $result    = __( 'Failed!', 'bbpress' );
    851     $key       = $wpdb->prefix . '_bbp_subscriptions';
    852     $users     = $wpdb->get_results( "SELECT `user_id`, `meta_value` AS `subscriptions` FROM `{$wpdb->usermeta}` WHERE `meta_key` = '{$key}';" );
     872
     873    $key       = $bbp_db->prefix . '_bbp_subscriptions';
     874    $users     = $bbp_db->get_results( "SELECT `user_id`, `meta_value` AS `subscriptions` FROM `{$bbp_db->usermeta}` WHERE `meta_key` = '{$key}';" );
    853875
    854876    if ( is_wp_error( $users ) ) {
     
    856878    }
    857879
    858     $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() . "';" );
     880    $topics = $bbp_db->get_col( "SELECT `ID` FROM `{$bbp_db->posts}` WHERE `post_type` = '" . bbp_get_topic_post_type() . "' AND `post_status` = '" . bbp_get_public_status_id() . "';" );
    859881    if ( is_wp_error( $topics ) ) {
    860882        return array( 2, sprintf( $statement, $result ) );
     
    884906    }
    885907
    886     $sql_delete = "DELETE FROM `{$wpdb->usermeta}` WHERE `meta_key` = '{$key}';";
    887     if ( is_wp_error( $wpdb->query( $sql_delete ) ) ) {
     908    $sql_delete = "DELETE FROM `{$bbp_db->usermeta}` WHERE `meta_key` = '{$key}';";
     909    if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
    888910        return array( 4, sprintf( $statement, $result ) );
    889911    }
     
    891913    foreach ( array_chunk( $values, 10000 ) as $chunk ) {
    892914        $chunk = "\n" . implode( ",\n", $chunk );
    893         $sql_insert = "INSERT INTO `{$wpdb->usermeta}` (`user_id`, `meta_key`, `meta_value`) VALUES $chunk;";
    894         if ( is_wp_error( $wpdb->query( $sql_insert ) ) ) {
     915        $sql_insert = "INSERT INTO `{$bbp_db->usermeta}` (`user_id`, `meta_key`, `meta_value`) VALUES {$chunk};";
     916        if ( is_wp_error( $bbp_db->query( $sql_insert ) ) ) {
    895917            return array( 5, sprintf( $statement, $result ) );
    896918        }
     
    912934 */
    913935function bbp_admin_repair_user_forum_subscriptions() {
    914     global $wpdb;
    915 
     936
     937    // Define variables
     938    $bbp_db    = bbp_db();
    916939    $statement = __( 'Removing trashed forums from user subscriptions&hellip; %s', 'bbpress' );
    917940    $result    = __( 'Failed!', 'bbpress' );
    918     $key       = $wpdb->prefix . '_bbp_forum_subscriptions';
    919     $users     = $wpdb->get_results( "SELECT `user_id`, `meta_value` AS `subscriptions` FROM `{$wpdb->usermeta}` WHERE `meta_key` = '{$key}';" );
     941
     942    $key       = $bbp_db->prefix . '_bbp_forum_subscriptions';
     943    $users     = $bbp_db->get_results( "SELECT `user_id`, `meta_value` AS `subscriptions` FROM `{$bbp_db->usermeta}` WHERE `meta_key` = '{$key}';" );
    920944
    921945    if ( is_wp_error( $users ) ) {
     
    923947    }
    924948
    925     $forums = $wpdb->get_col( "SELECT `ID` FROM `{$wpdb->posts}` WHERE `post_type` = '" . bbp_get_forum_post_type() . "' AND `post_status` = '" . bbp_get_public_status_id() . "';" );
     949    $forums = $bbp_db->get_col( "SELECT `ID` FROM `{$bbp_db->posts}` WHERE `post_type` = '" . bbp_get_forum_post_type() . "' AND `post_status` = '" . bbp_get_public_status_id() . "';" );
    926950    if ( is_wp_error( $forums ) ) {
    927951        return array( 2, sprintf( $statement, $result ) );
     
    951975    }
    952976
    953     $sql_delete = "DELETE FROM `{$wpdb->usermeta}` WHERE `meta_key` = '{$key}';";
    954     if ( is_wp_error( $wpdb->query( $sql_delete ) ) ) {
     977    $sql_delete = "DELETE FROM `{$bbp_db->usermeta}` WHERE `meta_key` = '{$key}';";
     978    if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
    955979        return array( 4, sprintf( $statement, $result ) );
    956980    }
     
    958982    foreach ( array_chunk( $values, 10000 ) as $chunk ) {
    959983        $chunk = "\n" . implode( ",\n", $chunk );
    960         $sql_insert = "INSERT INTO `{$wpdb->usermeta}` (`user_id`, `meta_key`, `meta_value`) VALUES $chunk;";
    961         if ( is_wp_error( $wpdb->query( $sql_insert ) ) ) {
     984        $sql_insert = "INSERT INTO `{$bbp_db->usermeta}` (`user_id`, `meta_key`, `meta_value`) VALUES {$chunk};";
     985        if ( is_wp_error( $bbp_db->query( $sql_insert ) ) ) {
    962986            return array( 5, sprintf( $statement, $result ) );
    963987        }
     
    9991023
    10001024        // If no role map exists, give the default forum role (bbp-participant)
    1001         $new_role = isset( $role_map[$role] ) ? $role_map[$role] : $default_role;
     1025        $new_role = isset( $role_map[ $role ] ) ? $role_map[ $role ] : $default_role;
    10021026
    10031027        // Get users of this site, limited to 1000
     
    10221046
    10231047    $result = sprintf( __( 'Complete! %s users updated.', 'bbpress' ), bbp_number_format( $changed ) );
     1048
    10241049    return array( 0, sprintf( $statement, $result ) );
    10251050}
     
    10411066 */
    10421067function bbp_admin_repair_freshness() {
    1043     global $wpdb;
    1044 
     1068
     1069    // Define variables
     1070    $bbp_db    = bbp_db();
    10451071    $statement = __( 'Recomputing latest post in every topic and forum&hellip; %s', 'bbpress' );
    10461072    $result    = __( 'Failed!', 'bbpress' );
    10471073
    10481074    // First, delete everything.
    1049     if ( is_wp_error( $wpdb->query( "DELETE FROM `$wpdb->postmeta` WHERE `meta_key` IN ( '_bbp_last_reply_id', '_bbp_last_topic_id', '_bbp_last_active_id', '_bbp_last_active_time' );" ) ) ) {
     1075    if ( is_wp_error( $bbp_db->query( "DELETE FROM `{$bbp_db->postmeta}` WHERE `meta_key` IN ( '_bbp_last_reply_id', '_bbp_last_topic_id', '_bbp_last_active_id', '_bbp_last_active_time' );" ) ) ) {
    10501076        return array( 1, sprintf( $statement, $result ) );
    10511077    }
     
    10581084
    10591085    // Next, give all the topics with replies the ID their last reply.
    1060     if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
     1086    if ( is_wp_error( $bbp_db->query( "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`)
    10611087            ( SELECT `topic`.`ID`, '_bbp_last_reply_id', MAX( `reply`.`ID` )
    1062             FROM `$wpdb->posts` AS `topic` INNER JOIN `$wpdb->posts` AS `reply` ON `topic`.`ID` = `reply`.`post_parent`
     1088            FROM `{$bbp_db->posts}` AS `topic` INNER JOIN `{$bbp_db->posts}` AS `reply` ON `topic`.`ID` = `reply`.`post_parent`
    10631089            WHERE `reply`.`post_status` = '{$pps}' AND `topic`.`post_type` = '{$tpt}' AND `reply`.`post_type` = '{$rpt}'
    10641090            GROUP BY `topic`.`ID` );" ) ) ) {
     
    10671093
    10681094    // For any remaining topics, give a reply ID of 0.
    1069     if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
     1095    if ( is_wp_error( $bbp_db->query( "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`)
    10701096            ( SELECT `ID`, '_bbp_last_reply_id', 0
    1071             FROM `$wpdb->posts` AS `topic` LEFT JOIN `$wpdb->postmeta` AS `reply`
     1097            FROM `{$bbp_db->posts}` AS `topic` LEFT JOIN `{$bbp_db->postmeta}` AS `reply`
    10721098            ON `topic`.`ID` = `reply`.`post_id` AND `reply`.`meta_key` = '_bbp_last_reply_id'
    10731099            WHERE `reply`.`meta_id` IS NULL AND `topic`.`post_type` = '{$tpt}' );" ) ) ) {
     
    10761102
    10771103    // Now we give all the forums with topics the ID their last topic.
    1078     if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
     1104    if ( is_wp_error( $bbp_db->query( "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`)
    10791105            ( SELECT `forum`.`ID`, '_bbp_last_topic_id', `topic`.`ID`
    1080             FROM `$wpdb->posts` AS `forum` INNER JOIN `$wpdb->posts` AS `topic` ON `forum`.`ID` = `topic`.`post_parent`
     1106            FROM `{$bbp_db->posts}` AS `forum` INNER JOIN `{$bbp_db->posts}` AS `topic` ON `forum`.`ID` = `topic`.`post_parent`
    10811107            WHERE `topic`.`post_status` = '{$pps}' AND `forum`.`post_type` = '{$fpt}' AND `topic`.`post_type` = '{$tpt}'
    10821108            GROUP BY `forum`.`ID` );" ) ) ) {
     
    10851111
    10861112    // For any remaining forums, give a topic ID of 0.
    1087     if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
     1113    if ( is_wp_error( $bbp_db->query( "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`)
    10881114            ( SELECT `ID`, '_bbp_last_topic_id', 0
    1089             FROM `$wpdb->posts` AS `forum` LEFT JOIN `$wpdb->postmeta` AS `topic`
     1115            FROM `{$bbp_db->posts}` AS `forum` LEFT JOIN `{$bbp_db->postmeta}` AS `topic`
    10901116            ON `forum`.`ID` = `topic`.`post_id` AND `topic`.`meta_key` = '_bbp_last_topic_id'
    10911117            WHERE `topic`.`meta_id` IS NULL AND `forum`.`post_type` = '{$fpt}' );" ) ) ) {
     
    10941120
    10951121    // After that, we give all the topics with replies the ID their last reply (again, this time for a different reason).
    1096     if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
     1122    if ( is_wp_error( $bbp_db->query( "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`)
    10971123            ( SELECT `topic`.`ID`, '_bbp_last_active_id', MAX( `reply`.`ID` )
    1098             FROM `$wpdb->posts` AS `topic` INNER JOIN `$wpdb->posts` AS `reply` ON `topic`.`ID` = `reply`.`post_parent`
     1124            FROM `{$bbp_db->posts}` AS `topic` INNER JOIN `{$bbp_db->posts}` AS `reply` ON `topic`.`ID` = `reply`.`post_parent`
    10991125            WHERE `reply`.`post_status` = '{$pps}' AND `topic`.`post_type` = '{$tpt}' AND `reply`.`post_type` = '{$rpt}'
    11001126            GROUP BY `topic`.`ID` );" ) ) ) {
     
    11031129
    11041130    // For any remaining topics, give a reply ID of themself.
    1105     if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
     1131    if ( is_wp_error( $bbp_db->query( "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`)
    11061132            ( SELECT `ID`, '_bbp_last_active_id', `ID`
    1107             FROM `$wpdb->posts` AS `topic` LEFT JOIN `$wpdb->postmeta` AS `reply`
     1133            FROM `{$bbp_db->posts}` AS `topic` LEFT JOIN `{$bbp_db->postmeta}` AS `reply`
    11081134            ON `topic`.`ID` = `reply`.`post_id` AND `reply`.`meta_key` = '_bbp_last_active_id'
    11091135            WHERE `reply`.`meta_id` IS NULL AND `topic`.`post_type` = '{$tpt}' );" ) ) ) {
     
    11121138
    11131139    // Give topics with replies their last update time.
    1114     if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
     1140    if ( is_wp_error( $bbp_db->query( "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`)
    11151141            ( SELECT `topic`.`ID`, '_bbp_last_active_time', MAX( `reply`.`post_date` )
    1116             FROM `$wpdb->posts` AS `topic` INNER JOIN `$wpdb->posts` AS `reply` ON `topic`.`ID` = `reply`.`post_parent`
     1142            FROM `{$bbp_db->posts}` AS `topic` INNER JOIN `{$bbp_db->posts}` AS `reply` ON `topic`.`ID` = `reply`.`post_parent`
    11171143            WHERE `reply`.`post_status` = '{$pps}' AND `topic`.`post_type` = '{$tpt}' AND `reply`.`post_type` = '{$rpt}'
    11181144            GROUP BY `topic`.`ID` );" ) ) ) {
     
    11211147
    11221148    // Give topics without replies their last update time.
    1123     if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
     1149    if ( is_wp_error( $bbp_db->query( "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`)
    11241150            ( SELECT `ID`, '_bbp_last_active_time', `post_date`
    1125             FROM `$wpdb->posts` AS `topic` LEFT JOIN `$wpdb->postmeta` AS `reply`
     1151            FROM `{$bbp_db->posts}` AS `topic` LEFT JOIN `{$bbp_db->postmeta}` AS `reply`
    11261152            ON `topic`.`ID` = `reply`.`post_id` AND `reply`.`meta_key` = '_bbp_last_active_time'
    11271153            WHERE `reply`.`meta_id` IS NULL AND `topic`.`post_type` = '{$tpt}' );" ) ) ) {
     
    11301156
    11311157    // Forums need to know what their last active item is as well. Now it gets a bit more complex to do in the database.
    1132     $forums = $wpdb->get_col( "SELECT `ID` FROM `$wpdb->posts` WHERE `post_type` = '{$fpt}' and `post_status` != 'auto-draft';" );
     1158    $forums = $bbp_db->get_col( "SELECT `ID` FROM `{$bbp_db->posts}` WHERE `post_type` = '{$fpt}' and `post_status` != 'auto-draft';" );
    11331159    if ( is_wp_error( $forums ) ) {
    11341160        return array( 10, sprintf( $statement, $result ) );
     
    11681194 */
    11691195function bbp_admin_repair_sticky() {
    1170     global $wpdb;
    1171 
     1196
     1197    // Define variables
     1198    $bbp_db    = bbp_db();
    11721199    $statement = __( 'Repairing the sticky topic to the parent forum relationships&hellip; %s', 'bbpress' );
    11731200    $result    = __( 'Failed!', 'bbpress' );
    1174     $forums    = $wpdb->get_col( "SELECT ID FROM `{$wpdb->posts}` WHERE `post_type` = '" . bbp_get_forum_post_type() . "';" );
     1201
     1202    $forums    = $bbp_db->get_col( "SELECT ID FROM `{$bbp_db->posts}` WHERE `post_type` = '" . bbp_get_forum_post_type() . "';" );
    11751203
    11761204    // Bail if no forums found
     
    11811209    // Loop through forums and get their sticky topics
    11821210    foreach ( $forums as $forum ) {
    1183         $forum_stickies[$forum] = get_post_meta( $forum, '_bbp_sticky_topics', true );
     1211        $forum_stickies[ $forum ] = get_post_meta( $forum, '_bbp_sticky_topics', true );
    11841212    }
    11851213
     
    12011229            // match the topic's forum ID, unset the forum's sticky meta.
    12021230            if ( ! bbp_is_topic_super_sticky( $topic_id ) && $forum_id !== bbp_get_topic_forum_id( $topic_id ) ) {
    1203                 unset( $forum_stickies[$forum_id][$id] );
     1231                unset( $forum_stickies[ $forum_id ][ $id ] );
    12041232            }
    12051233        }
    12061234
    12071235        // Get sticky topic ID's, or use empty string
    1208         $stickers = empty( $forum_stickies[$forum_id] ) ? '' : array_values( $forum_stickies[$forum_id] );
     1236        $stickers = empty( $forum_stickies[$forum_id] ) ? '' : array_values( $forum_stickies[ $forum_id ] );
    12091237
    12101238        // Update the forum's sticky topics meta
     
    12341262 */
    12351263function bbp_admin_repair_closed_topics() {
    1236     global $wpdb;
    1237 
     1264
     1265    // Define variables
     1266    $bbp_db        = bbp_db();
    12381267    $statement     = __( 'Repairing closed topics&hellip; %s', 'bbpress' );
     1268    $result        = __( 'No closed topics to repair.', 'bbpress' );
    12391269    $changed       = 0;
    1240     $result        = __( 'No closed topics to repair.', 'bbpress' );
    1241     $closed_topics = $wpdb->get_col( "SELECT ID FROM `{$wpdb->posts}` WHERE `post_type` = '" . bbp_get_topic_post_type() . "' AND `post_status` = 'closed';" );
     1270
     1271    $closed_topics = $bbp_db->get_col( "SELECT ID FROM `{$bbp_db->posts}` WHERE `post_type` = '" . bbp_get_topic_post_type() . "' AND `post_status` = 'closed';" );
    12421272
    12431273    // Bail if no closed topics found
    1244     if ( empty( $closed_topics ) || is_wp_error( $closed_topics ) )
     1274    if ( empty( $closed_topics ) || is_wp_error( $closed_topics ) ) {
    12451275        return array( 1, sprintf( $statement, $result ) );
     1276    }
    12461277
    12471278    // Loop through each closed topic
     
    12621293
    12631294    // Complete results
    1264     $result = sprintf( _n( 'Complete! %d closed topic repaired.', 'Complete! %d closed topics repaired.', $changed, 'bbpress' ),      $changed );
     1295    $result = sprintf( _n( 'Complete! %d closed topic repaired.', 'Complete! %d closed topics repaired.', $changed, 'bbpress' ), $changed );
     1296
    12651297    return array( 0, sprintf( $statement, $result ) );
    12661298}
     
    12991331 */
    13001332function bbp_admin_repair_forum_meta() {
    1301     global $wpdb;
    1302 
     1333
     1334    // Define variables
     1335    $bbp_db    = bbp_db();
    13031336    $statement = __( 'Recalculating the forum for each post &hellip; %s', 'bbpress' );
    13041337    $result    = __( 'Failed!', 'bbpress' );
    13051338
    13061339    // First, delete everything.
    1307     if ( is_wp_error( $wpdb->query( "DELETE FROM `$wpdb->postmeta` WHERE `meta_key` = '_bbp_forum_id';" ) ) ) {
     1340    if ( is_wp_error( $bbp_db->query( "DELETE FROM `{$bbp_db->postmeta}` WHERE `meta_key` = '_bbp_forum_id';" ) ) ) {
    13081341        return array( 1, sprintf( $statement, $result ) );
    13091342    }
     
    13141347
    13151348    // Next, give all the topics their parent forum id.
    1316     if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
     1349    if ( is_wp_error( $bbp_db->query( "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`)
    13171350            ( SELECT `topic`.`ID`, '_bbp_forum_id', `topic`.`post_parent`
    1318             FROM `$wpdb->posts`
     1351            FROM `$bbp_db->posts`
    13191352                AS `topic`
    13201353            WHERE `topic`.`post_type` = '{$tpt}'
     
    13241357
    13251358    // Next, give all the replies their parent forum id.
    1326     if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
     1359    if ( is_wp_error( $bbp_db->query( "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`)
    13271360            ( SELECT `reply`.`ID`, '_bbp_forum_id', `topic`.`post_parent`
    1328             FROM `$wpdb->posts`
     1361            FROM `$bbp_db->posts`
    13291362                AS `reply`
    1330             INNER JOIN `$wpdb->posts`
     1363            INNER JOIN `$bbp_db->posts`
    13311364                AS `topic`
    13321365                ON `reply`.`post_parent` = `topic`.`ID`
     
    13531386 */
    13541387function bbp_admin_repair_topic_meta() {
    1355     global $wpdb;
    1356 
     1388
     1389    // Define variables
     1390    $bbp_db    = bbp_db();
    13571391    $statement = __( 'Recalculating the topic for each post &hellip; %s', 'bbpress' );
    13581392    $result    = __( 'Failed!', 'bbpress' );
    13591393
    13601394    // First, delete everything.
    1361     if ( is_wp_error( $wpdb->query( "DELETE FROM `$wpdb->postmeta` WHERE `meta_key` = '_bbp_topic_id';" ) ) ) {
     1395    if ( is_wp_error( $bbp_db->query( "DELETE FROM `{$bbp_db->postmeta}` WHERE `meta_key` = '_bbp_topic_id';" ) ) ) {
    13621396        return array( 1, sprintf( $statement, $result ) );
    13631397    }
     
    13681402
    13691403    // Next, give all the topics with replies the ID their last reply.
    1370     if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
     1404    if ( is_wp_error( $bbp_db->query( "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`)
    13711405            ( SELECT `topic`.`ID`, '_bbp_topic_id', `topic`.`ID`
    1372             FROM `$wpdb->posts`
     1406            FROM `$bbp_db->posts`
    13731407                AS `topic`
    13741408            WHERE `topic`.`post_type` = '{$tpt}'
     
    13781412
    13791413    // Next, give all the topics with replies the ID their last reply.
    1380     if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
     1414    if ( is_wp_error( $bbp_db->query( "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`)
    13811415            ( SELECT `reply`.`ID`, '_bbp_topic_id', `topic`.`ID`
    1382             FROM `$wpdb->posts`
     1416            FROM `$bbp_db->posts`
    13831417                AS `reply`
    1384             INNER JOIN `$wpdb->posts`
     1418            INNER JOIN `$bbp_db->posts`
    13851419                AS `topic`
    13861420                ON `reply`.`post_parent` = `topic`.`ID`
     
    14071441 */
    14081442function bbp_admin_repair_reply_menu_order() {
    1409     global $wpdb;
    1410 
     1443
     1444    // Define variables
     1445    $bbp_db    = bbp_db();
    14111446    $statement = __( 'Recalculating reply menu order &hellip; %s', 'bbpress' );
    14121447    $result    = __( 'No reply positions to recalculate!',         'bbpress' );
    14131448
    14141449    // Delete cases where `_bbp_reply_to` was accidentally set to itself
    1415     if ( is_wp_error( $wpdb->query( "DELETE FROM `{$wpdb->postmeta}` WHERE `meta_key` = '_bbp_reply_to' AND `post_id` = `meta_value`;" ) ) ) {
     1450    if ( is_wp_error( $bbp_db->query( "DELETE FROM `{$bbp_db->postmeta}` WHERE `meta_key` = '_bbp_reply_to' AND `post_id` = `meta_value`;" ) ) ) {
    14161451        return array( 1, sprintf( $statement, $result ) );
    14171452    }
     
    14211456
    14221457    // Get an array of reply id's to update the menu oder for each reply
    1423     $replies = $wpdb->get_results( "SELECT `a`.`ID` FROM `{$wpdb->posts}` AS `a`
     1458    $replies = $bbp_db->get_results( "SELECT `a`.`ID` FROM `{$bbp_db->posts}` AS `a`
    14241459                                        INNER JOIN (
    14251460                                            SELECT `menu_order`, `post_parent`
    1426                                             FROM `{$wpdb->posts}`
     1461                                            FROM `{$bbp_db->posts}`
    14271462                                            GROUP BY `menu_order`, `post_parent`
    14281463                                            HAVING COUNT( * ) >1
     
    15461581    check_admin_referer( 'bbpress-reset' );
    15471582
    1548     global $wpdb;
    1549 
    15501583    // Stores messages
    15511584    $messages = array();
     
    15631596    $rpt = bbp_get_reply_post_type();
    15641597
     1598    // Define variables
     1599    $bbp_db    = bbp_db();
    15651600    $statement  = __( 'Deleting Posts&hellip; %s', 'bbpress' );
    1566     $sql_posts  = $wpdb->get_results( "SELECT `ID` FROM `{$wpdb->posts}` WHERE `post_type` IN ('{$fpt}', '{$tpt}', '{$rpt}')", OBJECT_K );
    1567     $sql_delete = "DELETE FROM `{$wpdb->posts}` WHERE `post_type` IN ('{$fpt}', '{$tpt}', '{$rpt}')";
    1568     $result     = is_wp_error( $wpdb->query( $sql_delete ) ) ? $failed : $success;
     1601
     1602    $sql_posts  = $bbp_db->get_results( "SELECT `ID` FROM `{$bbp_db->posts}` WHERE `post_type` IN ('{$fpt}', '{$tpt}', '{$rpt}')", OBJECT_K );
     1603    $sql_delete = "DELETE FROM `{$bbp_db->posts}` WHERE `post_type` IN ('{$fpt}', '{$tpt}', '{$rpt}')";
     1604    $result     = is_wp_error( $bbp_db->query( $sql_delete ) ) ? $failed : $success;
    15691605    $messages[] = sprintf( $statement, $result );
    15701606
     
    15781614        $statement  = __( 'Deleting Post Meta&hellip; %s', 'bbpress' );
    15791615        $sql_meta   = implode( "', '", $sql_meta );
    1580         $sql_delete = "DELETE FROM `{$wpdb->postmeta}` WHERE `post_id` IN ('{$sql_meta}');";
    1581         $result     = is_wp_error( $wpdb->query( $sql_delete ) ) ? $failed : $success;
     1616        $sql_delete = "DELETE FROM `{$bbp_db->postmeta}` WHERE `post_id` IN ('{$sql_meta}');";
     1617        $result     = is_wp_error( $bbp_db->query( $sql_delete ) ) ? $failed : $success;
    15821618        $messages[] = sprintf( $statement, $result );
    15831619    }
     
    15861622
    15871623    $statement  = __( 'Deleting Topic Tags&hellip; %s', 'bbpress' );
    1588     $sql_delete = "DELETE a,b,c FROM `{$wpdb->terms}` AS a LEFT JOIN `{$wpdb->term_taxonomy}` AS c ON a.term_id = c.term_id LEFT JOIN `{$wpdb->term_relationships}` AS b ON b.term_taxonomy_id = c.term_taxonomy_id WHERE c.taxonomy = 'topic-tag';";
    1589     $result     = is_wp_error( $wpdb->query( $sql_delete ) ) ? $failed : $success;
     1624    $sql_delete = "DELETE a,b,c FROM `{$bbp_db->terms}` AS a LEFT JOIN `{$bbp_db->term_taxonomy}` AS c ON a.term_id = c.term_id LEFT JOIN `{$bbp_db->term_relationships}` AS b ON b.term_taxonomy_id = c.term_taxonomy_id WHERE c.taxonomy = 'topic-tag';";
     1625    $result     = is_wp_error( $bbp_db->query( $sql_delete ) ) ? $failed : $success;
    15901626    $messages[] = sprintf( $statement, $result );
    15911627
     
    15941630    // First, if we're deleting previously imported users, delete them now
    15951631    if ( !empty( $_POST['bbpress-delete-imported-users'] ) ) {
    1596         $sql_users  = $wpdb->get_results( "SELECT `user_id` FROM `{$wpdb->usermeta}` WHERE `meta_key` = '_bbp_user_id'", OBJECT_K );
     1632        $sql_users  = $bbp_db->get_results( "SELECT `user_id` FROM `{$bbp_db->usermeta}` WHERE `meta_key` = '_bbp_user_id'", OBJECT_K );
    15971633        if ( !empty( $sql_users ) ) {
    15981634            $sql_meta = array();
     
    16021638            $statement  = __( 'Deleting User&hellip; %s', 'bbpress' );
    16031639            $sql_meta   = implode( "', '", $sql_meta );
    1604             $sql_delete = "DELETE FROM `{$wpdb->users}` WHERE `ID` IN ('{$sql_meta}');";
    1605             $result     = is_wp_error( $wpdb->query( $sql_delete ) ) ? $failed : $success;
     1640            $sql_delete = "DELETE FROM `{$bbp_db->users}` WHERE `ID` IN ('{$sql_meta}');";
     1641            $result     = is_wp_error( $bbp_db->query( $sql_delete ) ) ? $failed : $success;
    16061642            $messages[] = sprintf( $statement, $result );
    16071643            $statement  = __( 'Deleting User Meta&hellip; %s', 'bbpress' );
    1608             $sql_delete = "DELETE FROM `{$wpdb->usermeta}` WHERE `user_id` IN ('{$sql_meta}');";
    1609             $result     = is_wp_error( $wpdb->query( $sql_delete ) ) ? $failed : $success;
     1644            $sql_delete = "DELETE FROM `{$bbp_db->usermeta}` WHERE `user_id` IN ('{$sql_meta}');";
     1645            $result     = is_wp_error( $bbp_db->query( $sql_delete ) ) ? $failed : $success;
    16101646            $messages[] = sprintf( $statement, $result );
    16111647        }
     
    16141650    // Next, if we still have users that were not imported delete that meta data
    16151651    $statement  = __( 'Deleting User Meta&hellip; %s', 'bbpress' );
    1616     $sql_delete = "DELETE FROM `{$wpdb->usermeta}` WHERE `meta_key` LIKE '%%_bbp_%%';";
    1617     $result     = is_wp_error( $wpdb->query( $sql_delete ) ) ? $failed : $success;
     1652    $sql_delete = "DELETE FROM `{$bbp_db->usermeta}` WHERE `meta_key` LIKE '%%_bbp_%%';";
     1653    $result     = is_wp_error( $bbp_db->query( $sql_delete ) ) ? $failed : $success;
    16181654    $messages[] = sprintf( $statement, $result );
    16191655
     
    16211657
    16221658    $statement  = __( 'Deleting Conversion Table&hellip; %s', 'bbpress' );
    1623     $table_name = $wpdb->prefix . 'bbp_converter_translator';
    1624     if ( $wpdb->get_var( "SHOW TABLES LIKE '{$table_name}'" ) === $table_name ) {
    1625         $wpdb->query( "DROP TABLE {$table_name}" );
     1659    $table_name = $bbp_db->prefix . 'bbp_converter_translator';
     1660    if ( $bbp_db->get_var( "SHOW TABLES LIKE '{$table_name}'" ) === $table_name ) {
     1661        $bbp_db->query( "DROP TABLE {$table_name}" );
    16261662        $result = $success;
    16271663    } else {
  • trunk/src/includes/common/functions.php

    r5790 r5827  
    672672    }
    673673
    674     // Define global to use get_meta_sql() and get_var() methods
    675     global $wpdb;
    676 
    677674    // Parse arguments against default values
    678675    $r = bbp_parse_args( $post_data, array(
     
    685682    ), 'check_for_duplicate' );
    686683
     684    // Get the DB
     685    $bbp_db = bbp_db();
     686
    687687    // Check for anonymous post
    688688    if ( empty( $r['post_author'] ) && ( !empty( $r['anonymous_data'] ) && !empty( $r['anonymous_data']['bbp_anonymous_email'] ) ) ) {
     
    690690            'key'   => '_bbp_anonymous_email',
    691691            'value' => $r['anonymous_data']['bbp_anonymous_email']
    692         ) ), 'post', $wpdb->posts, 'ID' );
     692        ) ), 'post', $bbp_db->posts, 'ID' );
    693693
    694694        $join    = $clauses['join'];
     
    698698    }
    699699
    700     // Unslash $r to pass through $wpdb->prepare()
     700    // Unslash $r to pass through DB->prepare()
    701701    //
    702702    // @see: https://bbpress.trac.wordpress.org/ticket/2185/
     
    705705
    706706    // Prepare duplicate check query
    707     $query  = $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} {$join} WHERE post_type = %s AND post_status != %s AND post_author = %d AND post_content = %s {$where}", $r['post_type'], $r['post_status'], $r['post_author'], $r['post_content'] );
    708     $query .= !empty( $r['post_parent'] ) ? $wpdb->prepare( " AND post_parent = %d", $r['post_parent'] ) : '';
     707    $query  = $bbp_db->prepare( "SELECT ID FROM {$bbp_db->posts} {$join} WHERE post_type = %s AND post_status != %s AND post_author = %d AND post_content = %s {$where}", $r['post_type'], $r['post_status'], $r['post_author'], $r['post_content'] );
     708    $query .= !empty( $r['post_parent'] ) ? $bbp_db->prepare( " AND post_parent = %d", $r['post_parent'] ) : '';
    709709    $query .= " LIMIT 1";
    710710    $dupe   = apply_filters( 'bbp_check_for_duplicate_query', $query, $r );
    711711
    712     if ( $wpdb->get_var( $dupe ) ) {
     712    if ( $bbp_db->get_var( $dupe ) ) {
    713713        do_action( 'bbp_check_for_duplicate_trigger', $post_data );
    714714        return false;
     
    14511451 *
    14521452 * @since bbPress (r2996)
    1453  *
    1454  * @global DB $wpdb
     1453 * @deprecated bbPress (r5814)
     1454 *
    14551455 * @global WP $wp
    14561456 * @param string $where
     
    14591459 */
    14601460function bbp_query_post_parent__in( $where, $object = '' ) {
    1461     global $wpdb, $wp;
     1461    global $wp;
    14621462
    14631463    // Noop if WP core supports this already
     
    14761476    }
    14771477
     1478    // Get the DB
     1479    $bbp_db = bbp_db();
     1480
    14781481    // Including specific post_parent's
    14791482    if ( ! empty( $object->query_vars['post_parent__in'] ) ) {
    14801483        $ids    = implode( ',', wp_parse_id_list( $object->query_vars['post_parent__in'] ) );
    1481         $where .= " AND {$wpdb->posts}.post_parent IN ($ids)";
     1484        $where .= " AND {$bbp_db->posts}.post_parent IN ($ids)";
    14821485
    14831486    // Excluding specific post_parent's
    14841487    } elseif ( ! empty( $object->query_vars['post_parent__not_in'] ) ) {
    14851488        $ids    = implode( ',', wp_parse_id_list( $object->query_vars['post_parent__not_in'] ) );
    1486         $where .= " AND {$wpdb->posts}.post_parent NOT IN ($ids)";
     1489        $where .= " AND {$bbp_db->posts}.post_parent NOT IN ($ids)";
    14871490    }
    14881491
     
    15061509 */
    15071510function bbp_get_public_child_last_id( $parent_id = 0, $post_type = 'post' ) {
    1508     global $wpdb;
    15091511
    15101512    // Bail if nothing passed
     
    15281530        // Join post statuses together
    15291531        $post_status = "'" . implode( "', '", $post_status ) . "'";
    1530 
    1531         $child_id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s' ORDER BY ID DESC LIMIT 1;", $parent_id, $post_type ) );
     1532        $bbp_db      = bbp_db();
     1533        $query       = $bbp_db->prepare( "SELECT ID FROM {$bbp_db->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s' ORDER BY ID DESC LIMIT 1;", $parent_id, $post_type );
     1534        $child_id    = (int) $bbp_db->get_var( $query );
    15321535
    15331536        wp_cache_set( $cache_id, $child_id, 'bbpress_posts' );
     
    15571560 */
    15581561function bbp_get_public_child_count( $parent_id = 0, $post_type = 'post' ) {
    1559     global $wpdb;
    15601562
    15611563    // Bail if nothing passed
     
    15791581        // Join post statuses together
    15801582        $post_status = "'" . implode( "', '", $post_status ) . "'";
    1581 
    1582         $child_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s';", $parent_id, $post_type ) );
     1583        $bbp_db      = bbp_db();
     1584        $query       = $bbp_db->prepare( "SELECT COUNT(ID) FROM {$bbp_db->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s';", $parent_id, $post_type );
     1585        $child_count = (int) $bbp_db->get_var( $query );
    15831586
    15841587        wp_cache_set( $cache_id, $child_count, 'bbpress_posts' );
     
    16101613 */
    16111614function bbp_get_public_child_ids( $parent_id = 0, $post_type = 'post' ) {
    1612     global $wpdb;
    16131615
    16141616    // Bail if nothing passed
     
    16321634        // Join post statuses together
    16331635        $post_status = "'" . implode( "', '", $post_status ) . "'";
    1634 
    1635         $child_ids = (array) $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s' ORDER BY ID DESC;", $parent_id, $post_type ) );
     1636        $bbp_db      = bbp_db();
     1637        $query       = $bbp_db->prepare( "SELECT ID FROM {$bbp_db->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s' ORDER BY ID DESC;", $parent_id, $post_type );
     1638        $child_ids   = (array) $bbp_db->get_col( $query );
    16361639
    16371640        wp_cache_set( $cache_id, $child_ids, 'bbpress_posts' );
     
    16701673 */
    16711674function bbp_get_all_child_ids( $parent_id = 0, $post_type = 'post' ) {
    1672     global $wpdb;
    16731675
    16741676    // Bail if nothing passed
     
    17121714        // Join post statuses together
    17131715        $post_status = "'" . implode( "', '", $post_status ) . "'";
    1714 
    1715         $child_ids = (array) $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s' ORDER BY ID DESC;", $parent_id, $post_type ) );
     1716        $bbp_db      = bbp_db();
     1717        $query       = $bbp_db->prepare( "SELECT ID FROM {$bbp_db->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s' ORDER BY ID DESC;", $parent_id, $post_type );
     1718        $child_ids   = (array) $bbp_db->get_col( $query );
    17161719
    17171720        wp_cache_set( $cache_id, $child_ids, 'bbpress_posts' );
  • trunk/src/includes/common/template.php

    r5770 r5827  
    20582058     */
    20592059    function bbp_get_view_url( $view = false ) {
    2060         global $wp_rewrite;
    20612060
    20622061        $view = bbp_get_view_id( $view );
     
    20662065
    20672066        // Pretty permalinks
    2068         if ( $wp_rewrite->using_permalinks() ) {
    2069             $url = $wp_rewrite->root . bbp_get_view_slug() . '/' . $view;
    2070             $url = home_url( user_trailingslashit( $url ) );
     2067        if ( bbp_use_pretty_urls() ) {
     2068            $url = trailingslashit( bbp_get_root_url() . bbp_get_view_slug() ) . $view;
     2069            $url = user_trailingslashit( $url );
     2070            $url = home_url( $url );
    20712071
    20722072        // Unpretty permalinks
    20732073        } else {
    2074             $url = add_query_arg( array( bbp_get_view_rewrite_id() => $view ), home_url( '/' ) );
     2074            $url = add_query_arg( array(
     2075                bbp_get_view_rewrite_id() => $view
     2076            ), home_url( '/' ) );
    20752077        }
    20762078
  • trunk/src/includes/core/capabilities.php

    r5770 r5827  
    244244
    245245/**
    246  * Get the $wp_roles global without needing to declare it everywhere
     246 * Get the `$wp_roles` global without needing to declare it everywhere
    247247 *
    248248 * @since bbPress (r4293)
    249249 *
    250  * @global WP_Roles $wp_roles
    251250 * @return WP_Roles
    252251 */
    253252function bbp_get_wp_roles() {
    254     global $wp_roles;
    255 
    256     // Load roles if not set
    257     if ( ! isset( $wp_roles ) ) {
    258         $wp_roles = new WP_Roles();
    259     }
    260 
    261     return $wp_roles;
     253
     254    // Try to get `$wp_roles`
     255    $retval = bbp_get_global_object( 'wp_roles', 'WP_Roles' );
     256
     257    // Set roles if not loaded
     258    if ( is_null( $retval ) ) {
     259        $retval = $GLOBALS['wp_roles'] = new WP_Roles();
     260    }
     261
     262    return $retval;
    262263}
    263264
     
    273274
    274275    // Get WordPress's roles (returns $wp_roles global)
    275     $wp_roles  = bbp_get_wp_roles();
     276    $wp_roles = bbp_get_wp_roles();
    276277
    277278    // Apply the WordPress 'editable_roles' filter to let plugins ride along.
     
    302303
    303304    foreach ( bbp_get_dynamic_roles() as $role_id => $details ) {
    304         $wp_roles->roles[$role_id]        = $details;
    305         $wp_roles->role_objects[$role_id] = new WP_Role( $role_id, $details['capabilities'] );
    306         $wp_roles->role_names[$role_id]   = $details['name'];
     305        $wp_roles->roles[ $role_id ]        = $details;
     306        $wp_roles->role_objects[ $role_id ] = new WP_Role( $role_id, $details['capabilities'] );
     307        $wp_roles->role_names[ $role_id ]   = $details['name'];
    307308    }
    308309
     
    316317 *
    317318 * @see _bbp_reinit_dynamic_roles()
    318  *
    319  * @global WPDB $wpdb Used to get the database prefix
    320319 */
    321320function bbp_filter_user_roles_option() {
    322     global $wpdb;
    323 
    324     $role_key = $wpdb->prefix . 'user_roles';
     321    $role_key = bbp_db()->prefix . 'user_roles';
    325322
    326323    add_filter( 'option_' . $role_key, '_bbp_reinit_dynamic_roles' );
     
    419416function bbp_get_dynamic_role_name( $role_id = '' ) {
    420417    $roles = bbp_get_dynamic_roles();
    421     $role  = isset( $roles[$role_id] ) ? $roles[$role_id]['name'] : '';
     418    $role  = isset( $roles[ $role_id ] ) ? $roles[ $role_id ]['name'] : '';
    422419
    423420    return apply_filters( 'bbp_get_dynamic_role_name', $role, $role_id, $roles );
     
    445442            // If keys match, unset
    446443            if ( $wp_role === $bbp_role ) {
    447                 unset( $all_roles[$wp_role] );
     444                unset( $all_roles[ $wp_role ] );
    448445            }
    449446        }
  • trunk/src/includes/core/theme-compat.php

    r5770 r5827  
    804804 *
    805805 * @since bbPress (r2628)
     806 *
    806807 * @param string $redirect_url Redirect url
    807  * @uses WP_Rewrite::using_permalinks() To check if the blog is using permalinks
     808 * @uses bbp_use_pretty_urls() To check if the blog is using permalinks
    808809 * @uses bbp_get_paged() To get the current page number
    809810 * @uses bbp_is_single_topic() To check if it's a topic page
    810811 * @uses bbp_is_single_forum() To check if it's a forum page
     812 *
    811813 * @return bool|string False if it's a topic/forum and their first page,
    812814 *                      otherwise the redirect url
    813815 */
    814816function bbp_redirect_canonical( $redirect_url ) {
    815     global $wp_rewrite;
    816817
    817818    // Canonical is for the beautiful
    818     if ( $wp_rewrite->using_permalinks() ) {
     819    if ( bbp_use_pretty_urls() ) {
    819820
    820821        // If viewing beyond page 1 of several
  • trunk/src/includes/forums/functions.php

    r5775 r5827  
    822822    // Only run queries if visibility is changing
    823823    if ( bbp_get_public_status_id() !== $current_visibility ) {
    824 
    825         // Update forums visibility setting
    826         global $wpdb;
    827         $wpdb->update( $wpdb->posts, array( 'post_status' => bbp_get_public_status_id() ), array( 'ID' => $forum_id ) );
     824        $bbp_db = bbp_db();
     825        $bbp_db->update( $bbp_db->posts, array( 'post_status' => bbp_get_public_status_id() ), array( 'ID' => $forum_id ) );
    828826        wp_transition_post_status( bbp_get_public_status_id(), $current_visibility, get_post( $forum_id ) );
    829827        bbp_clean_post_cache( $forum_id );
     
    874872
    875873        // Update forums visibility setting
    876         global $wpdb;
    877         $wpdb->update( $wpdb->posts, array( 'post_status' => bbp_get_private_status_id() ), array( 'ID' => $forum_id ) );
     874        $bbp_db = bbp_db();
     875        $bbp_db->update( $bbp_db->posts, array( 'post_status' => bbp_get_private_status_id() ), array( 'ID' => $forum_id ) );
    878876        wp_transition_post_status( bbp_get_private_status_id(), $current_visibility, get_post( $forum_id ) );
    879877        bbp_clean_post_cache( $forum_id );
     
    924922
    925923        // Update forums visibility setting
    926         global $wpdb;
    927         $wpdb->update( $wpdb->posts, array( 'post_status' => bbp_get_hidden_status_id() ), array( 'ID' => $forum_id ) );
     924        $bbp_db = bbp_db();
     925        $bbp_db->update( $bbp_db->posts, array( 'post_status' => bbp_get_hidden_status_id() ), array( 'ID' => $forum_id ) );
    928926        wp_transition_post_status( bbp_get_hidden_status_id(), $current_visibility, get_post( $forum_id ) );
    929927        bbp_clean_post_cache( $forum_id );
     
    15111509 */
    15121510function bbp_update_forum_topic_count_hidden( $forum_id = 0, $topic_count = 0 ) {
    1513     global $wpdb;
    15141511
    15151512    // If topic_id was passed as $forum_id, then get its forum
     
    15281525        // Get topics of forum
    15291526        if ( empty( $topic_count ) ) {
     1527            $bbp_db      = bbp_db();
    15301528            $post_status = "'" . implode( "','", array( bbp_get_trash_status_id(), bbp_get_spam_status_id(), bbp_get_pending_status_id() ) ) . "'";
    1531             $topic_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s';", $forum_id, bbp_get_topic_post_type() ) );
     1529            $topic_count = $bbp_db->get_var( $bbp_db->prepare( "SELECT COUNT(ID) FROM {$bbp_db->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s';", $forum_id, bbp_get_topic_post_type() ) );
    15321530        }
    15331531
     
    15631561 */
    15641562function bbp_update_forum_reply_count( $forum_id = 0 ) {
    1565     global $wpdb;
    15661563
    15671564    $forum_id = bbp_get_forum_id( $forum_id );
     
    15801577    $topic_ids   = bbp_forum_query_topic_ids( $forum_id );
    15811578    if ( ! empty( $topic_ids ) ) {
     1579        $bbp_db      = bbp_db();
    15821580        $topic_ids   = implode( ',', wp_parse_id_list( $topic_ids ) );
    1583         $reply_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_parent IN ( {$topic_ids} ) AND post_status = '%s' AND post_type = '%s';", bbp_get_public_status_id(), bbp_get_reply_post_type() ) );
     1581        $reply_count = (int) $bbp_db->get_var( $bbp_db->prepare( "SELECT COUNT(ID) FROM {$bbp_db->posts} WHERE post_parent IN ( {$topic_ids} ) AND post_status = '%s' AND post_type = '%s';", bbp_get_public_status_id(), bbp_get_reply_post_type() ) );
    15841582    }
    15851583
     
    20042002 */
    20052003function bbp_forum_query_last_reply_id( $forum_id, $topic_ids = 0 ) {
    2006     global $wpdb;
    20072004
    20082005    $cache_id = 'bbp_get_forum_' . $forum_id . '_reply_id';
     
    20162013
    20172014        if ( !empty( $topic_ids ) ) {
     2015            $bbp_db    = bbp_db();
    20182016            $topic_ids = implode( ',', wp_parse_id_list( $topic_ids ) );
    2019             $reply_id  = (int) $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent IN ( {$topic_ids} ) AND post_status = '%s' AND post_type = '%s' ORDER BY ID DESC LIMIT 1;", bbp_get_public_status_id(), bbp_get_reply_post_type() ) );
     2017            $reply_id  = (int) $bbp_db->get_var( $bbp_db->prepare( "SELECT ID FROM {$bbp_db->posts} WHERE post_parent IN ( {$topic_ids} ) AND post_status = '%s' AND post_type = '%s' ORDER BY ID DESC LIMIT 1;", bbp_get_public_status_id(), bbp_get_reply_post_type() ) );
    20202018        } else {
    20212019            $reply_id = 0;
  • trunk/src/includes/replies/functions.php

    r5825 r5827  
    12491249 *                    and destination topic ids
    12501250 * @uses bbp_get_reply_post_type() To get the reply post type
    1251  * @uses wpdb::prepare() To prepare our sql query
    1252  * @uses wpdb::get_results() To execute the sql query and get results
    12531251 * @uses wp_update_post() To update the replies
    12541252 * @uses bbp_update_reply_topic_id() To update the reply topic id
     
    21122110    /** Proceed ***************************************************************/
    21132111
    2114     global $wpdb;
    2115 
    21162112    // Table name for posts
    2117     $table_name = $wpdb->prefix . 'posts';
     2113    $table_name = bbp_db()->prefix . 'posts';
    21182114
    21192115    // Get the topic ID from the post_parent, set in bbp_has_replies()
  • trunk/src/includes/replies/template.php

    r5770 r5827  
    109109 *                           others' replies
    110110 * @uses WP_Query To make query and get the replies
    111  * @uses WP_Rewrite::using_permalinks() To check if the blog is using permalinks
     111 * @uses bbp_use_pretty_urls() To check if the site is using pretty URLs
    112112 * @uses get_permalink() To get the permalink
    113113 * @uses add_query_arg() To add custom args to the url
     
    120120 */
    121121function bbp_has_replies( $args = array() ) {
    122     global $wp_rewrite;
    123122
    124123    /** Defaults **************************************************************/
     
    226225
    227226        // If pretty permalinks are enabled, make our pagination pretty
    228         if ( $wp_rewrite->using_permalinks() ) {
     227        if ( bbp_use_pretty_urls() ) {
    229228
    230229            // User's replies
     
    245244            }
    246245
    247             $base = trailingslashit( $base ) . user_trailingslashit( $wp_rewrite->pagination_base . '/%#%/' );
     246            $base = trailingslashit( $base ) . user_trailingslashit( bbp_get_paged_slug() . '/%#%/' );
    248247
    249248        // Unpretty permalinks
     
    272271
    273272            // Remove first page from pagination
    274             if ( $wp_rewrite->using_permalinks() ) {
    275                 $bbp->reply_query->pagination_links = str_replace( $wp_rewrite->pagination_base . '/1/', '', $bbp->reply_query->pagination_links );
     273            if ( bbp_use_pretty_urls() ) {
     274                $bbp->reply_query->pagination_links = str_replace( bbp_get_paged_slug() . '/1/', '', $bbp->reply_query->pagination_links );
    276275            } else {
    277276                $bbp->reply_query->pagination_links = str_replace( '&#038;paged=1', '', $bbp->reply_query->pagination_links );
     
    474473     * @uses bbp_get_reply_position() To get the reply position
    475474     * @uses get_option() To get the replies per page option
    476      * @uses WP_Rewrite::using_permalinks() To check if the blog uses
    477      *                                       permalinks
     475     * @uses bbp_use_pretty_urls() To check if the site uses pretty URLs
    478476     * @uses add_query_arg() To add custom args to the url
    479477     * @uses apply_filters() Calls 'bbp_get_reply_url' with the reply url,
     
    506504        // Include pagination
    507505        } else {
    508             global $wp_rewrite;
    509506
    510507            // Pretty permalinks
    511             if ( $wp_rewrite->using_permalinks() ) {
    512                 $url = trailingslashit( $topic_url ) . trailingslashit( $wp_rewrite->pagination_base ) . trailingslashit( $reply_page ) . $reply_hash;
     508            if ( bbp_use_pretty_urls() ) {
     509                $url = trailingslashit( $topic_url ) . trailingslashit( bbp_get_paged_slug() ) . trailingslashit( $reply_page ) . $reply_hash;
    513510
    514511            // Yucky links
     
    20222019     */
    20232020    function bbp_get_reply_edit_url( $reply_id = 0 ) {
    2024         global $wp_rewrite;
    20252021
    20262022        $reply = bbp_get_reply( $reply_id );
     
    20322028
    20332029        // Pretty permalinks
    2034         if ( $wp_rewrite->using_permalinks() ) {
     2030        if ( bbp_use_pretty_urls() ) {
    20352031            $url = trailingslashit( $reply_link ) . bbp_get_edit_rewrite_id();
    2036             $url = trailingslashit( $url );
     2032            $url = user_trailingslashit( $url );
    20372033
    20382034        // Unpretty permalinks
    20392035        } else {
    2040             $url = add_query_arg( array( bbp_get_reply_post_type() => $reply->post_name, bbp_get_edit_rewrite_id() => '1' ), $reply_link );
     2036            $url = add_query_arg( array(
     2037                bbp_get_reply_post_type() => $reply->post_name,
     2038                bbp_get_edit_rewrite_id() => '1'
     2039            ), $reply_link );
    20412040        }
    20422041
  • trunk/src/includes/search/functions.php

    r5770 r5827  
    1616 * Run the search query
    1717 *
    18  * @since bbPress (r4579) 
     18 * @since bbPress (r4579)
    1919 *
    2020 * @param mixed $new_args New arguments
     
    2626function bbp_search_query( $new_args = array() ) {
    2727
    28     // Existing arguments 
     28    // Existing arguments
    2929    $query_args = bbp_get_search_query_args();
    3030
     
    5959 *
    6060 * @since bbPress (r4928)
     61 *
    6162 * @return If a redirect is not needed
    6263 */
    6364function bbp_search_results_redirect() {
    64     global $wp_rewrite;
    65    
     65
    6666    // Bail if not a search request action
    6767    if ( empty( $_GET['action'] ) || ( 'bbp-search-request' !== $_GET['action'] ) ) {
     
    7070
    7171    // Bail if not using pretty permalinks
    72     if ( ! $wp_rewrite->using_permalinks() ) {
     72    if ( ! bbp_use_pretty_urls() ) {
    7373        return;
    7474    }
  • trunk/src/includes/search/template.php

    r5770 r5827  
    3131 * @uses bbp_get_search_terms() To get the search terms
    3232 * @uses WP_Query To make query and get the search results
    33  * @uses WP_Rewrite::using_permalinks() To check if the blog is using permalinks
     33 * @uses bbp_use_pretty_urls() To check if the site is using pretty URLs
    3434 * @uses bbp_get_search_url() To get the forum search url
    3535 * @uses paginate_links() To paginate search results
     
    4040 */
    4141function bbp_has_search_results( $args = array() ) {
    42     global $wp_rewrite;
    4342
    4443    /** Defaults **************************************************************/
     
    115114
    116115        // If pretty permalinks are enabled, make our pagination pretty
    117         if ( $wp_rewrite->using_permalinks() ) {
     116        if ( bbp_use_pretty_urls() ) {
    118117
    119118            // Shortcode territory
     
    127126
    128127            // Add pagination base
    129             $base = $base . user_trailingslashit( $wp_rewrite->pagination_base . '/%#%/' );
     128            $base = $base . user_trailingslashit( bbp_get_paged_slug() . '/%#%/' );
    130129
    131130        // Unpretty permalinks
     
    154153
    155154        // Remove first page from pagination
    156         if ( $wp_rewrite->using_permalinks() ) {
    157             $bbp->search_query->pagination_links = str_replace( $wp_rewrite->pagination_base . '/1/', '', $bbp->search_query->pagination_links );
     155        if ( bbp_use_pretty_urls() ) {
     156            $bbp->search_query->pagination_links = str_replace( bbp_get_paged_slug() . '/1/', '', $bbp->search_query->pagination_links );
    158157        } else {
    159158            $bbp->search_query->pagination_links = str_replace( '&#038;paged=1', '', $bbp->search_query->pagination_links );
     
    264263     */
    265264    function bbp_get_search_url() {
    266         global $wp_rewrite;
    267265
    268266        // Pretty permalinks
    269         if ( $wp_rewrite->using_permalinks() ) {
    270             $url = $wp_rewrite->root . bbp_get_search_slug();
    271             $url = home_url( user_trailingslashit( $url ) );
     267        if ( bbp_use_pretty_urls() ) {
     268            $url = bbp_get_root_url() . bbp_get_search_slug();
     269            $url = user_trailingslashit( $url );
     270            $url = home_url( $url );
    272271
    273272        // Unpretty permalinks
    274273        } else {
    275             $url = add_query_arg( array( bbp_get_search_rewrite_id() => '' ), home_url( '/' ) );
     274            $url = add_query_arg( array(
     275                bbp_get_search_rewrite_id() => ''
     276            ), home_url( '/' ) );
    276277        }
    277278
     
    302303     */
    303304    function bbp_get_search_results_url() {
    304         global $wp_rewrite;
    305305
    306306        // Get the search terms
     
    308308
    309309        // Pretty permalinks
    310         if ( $wp_rewrite->using_permalinks() ) {
     310        if ( bbp_use_pretty_urls() ) {
    311311
    312312            // Root search URL
    313             $url = $wp_rewrite->root . bbp_get_search_slug();
     313            $url = bbp_get_root_url() . bbp_get_search_slug();
    314314
    315315            // Append search terms
    316316            if ( ! empty( $search_terms ) ) {
    317                 $url = trailingslashit( $url ) . user_trailingslashit( urlencode( $search_terms ) );
     317                $url = trailingslashit( $url ) . urlencode( $search_terms );
    318318            }
    319319
    320320            // Run through home_url()
    321             $url = home_url( user_trailingslashit( $url ) );
     321            $url = user_trailingslashit( $url );
     322            $url = home_url( $url );
    322323
    323324        // Unpretty permalinks
    324325        } else {
    325             $url = add_query_arg( array( bbp_get_search_rewrite_id() => urlencode( $search_terms ) ), home_url( '/' ) );
     326            $url = add_query_arg( array(
     327                bbp_get_search_rewrite_id() => urlencode( $search_terms )
     328            ), home_url( '/' ) );
    326329        }
    327330
  • trunk/src/includes/topics/functions.php

    r5778 r5827  
    14721472    }
    14731473
    1474     global $wpdb;
    1475 
    14761474    // Prevent debug notices
    14771475    $from_reply_id = $destination_topic_id = 0;
     
    16781676    // get_posts() is not used because it doesn't allow us to use '>='
    16791677    // comparision without a filter.
    1680     $replies = (array) $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->posts} WHERE {$wpdb->posts}.post_date >= %s AND {$wpdb->posts}.post_parent = %d AND {$wpdb->posts}.post_type = %s ORDER BY {$wpdb->posts}.post_date ASC", $from_reply->post_date, $source_topic->ID, bbp_get_reply_post_type() ) );
     1678    $bbp_db  = bbp_db();
     1679    $query   = $bbp_db->prepare( "SELECT * FROM {$bbp_db->posts} WHERE {$bbp_db->posts}.post_date >= %s AND {$bbp_db->posts}.post_parent = %d AND {$bbp_db->posts}.post_type = %s ORDER BY {$bbp_db->posts}.post_date ASC", $from_reply->post_date, $source_topic->ID, bbp_get_reply_post_type() );
     1680    $replies = (array) $bbp_db->get_results( $query );
    16811681
    16821682    // Make sure there are replies to loop through
     
    25092509 */
    25102510function bbp_update_topic_reply_count_hidden( $topic_id = 0, $reply_count = 0 ) {
    2511     global $wpdb;
    25122511
    25132512    // If it's a reply, then get the parent (topic id)
     
    25202519    // Get replies of topic
    25212520    if ( empty( $reply_count ) ) {
     2521        $bbp_db      = bbp_db();
    25222522        $post_status = "'" . implode( "','", array( bbp_get_trash_status_id(), bbp_get_spam_status_id(), bbp_get_pending_status_id() ) ) . "'";
    2523         $reply_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s';", $topic_id, bbp_get_reply_post_type() ) );
     2523        $query       = $bbp_db->prepare( "SELECT COUNT(ID) FROM {$bbp_db->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s';", $topic_id, bbp_get_reply_post_type() );
     2524        $reply_count = $bbp_db->get_var( $query );
    25242525    }
    25252526
     
    26732674 * @uses bbp_get_topic_post_type() To get the topic post type
    26742675 * @uses wpdb::prepare() To prepare our sql query
    2675  * @uses wpdb::get_col() To execute our query and get the column back
     2676 * @uses wpdb::get_var() To execute our query and get the column back
    26762677 * @uses update_post_meta() To update the topic voice count meta
    26772678 * @uses apply_filters() Calls 'bbp_update_topic_voice_count' with the voice
     
    26802681 */
    26812682function bbp_update_topic_voice_count( $topic_id = 0 ) {
    2682     global $wpdb;
    26832683
    26842684    // If it's a reply, then get the parent (topic id)
     
    26922692
    26932693    // Query the DB to get voices in this topic
    2694     $voices = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( DISTINCT post_author ) FROM {$wpdb->posts} WHERE ( post_parent = %d AND post_status = '%s' AND post_type = '%s' ) OR ( ID = %d AND post_type = '%s' );", $topic_id, bbp_get_public_status_id(), bbp_get_reply_post_type(), $topic_id, bbp_get_topic_post_type() ) );
     2694    $bbp_db = bbp_db();
     2695    $query  = $bbp_db->prepare( "SELECT COUNT( DISTINCT post_author ) FROM {$bbp_db->posts} WHERE ( post_parent = %d AND post_status = '%s' AND post_type = '%s' ) OR ( ID = %d AND post_type = '%s' );", $topic_id, bbp_get_public_status_id(), bbp_get_reply_post_type(), $topic_id, bbp_get_topic_post_type() );
     2696    $voices = (int) $bbp_db->get_var( $query );
    26952697
    26962698    // Update the voice count for this topic id
     
    27132715 * @uses bbp_get_topic_post_type() To get the topic post type
    27142716 * @uses wpdb::prepare() To prepare our sql query
    2715  * @uses wpdb::get_col() To execute our query and get the column back
     2717 * @uses wpdb::get_var() To execute our query and get the column back
    27162718 * @uses update_post_meta() To update the topic anonymous reply count meta
    27172719 * @uses apply_filters() Calls 'bbp_update_topic_anonymous_reply_count' with the
     
    27202722 */
    27212723function bbp_update_topic_anonymous_reply_count( $topic_id = 0 ) {
    2722     global $wpdb;
    27232724
    27242725    // If it's a reply, then get the parent (topic id)
     
    27312732    }
    27322733
    2733     $anonymous_replies = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( ID ) FROM {$wpdb->posts} WHERE ( post_parent = %d AND post_status = '%s' AND post_type = '%s' AND post_author = 0 ) OR ( ID = %d AND post_type = '%s' AND post_author = 0 );", $topic_id, bbp_get_public_status_id(), bbp_get_reply_post_type(), $topic_id, bbp_get_topic_post_type() ) );
    2734 
    2735     update_post_meta( $topic_id, '_bbp_anonymous_reply_count', $anonymous_replies );
    2736 
    2737     return (int) apply_filters( 'bbp_update_topic_anonymous_reply_count', $anonymous_replies, $topic_id );
     2734    // Query the DB to get anonymous replies in this topic
     2735    $bbp_db  = bbp_db();
     2736    $query   = $bbp_db->prepare( "SELECT COUNT( ID ) FROM {$bbp_db->posts} WHERE ( post_parent = %d AND post_status = '%s' AND post_type = '%s' AND post_author = 0 ) OR ( ID = %d AND post_type = '%s' AND post_author = 0 );", $topic_id, bbp_get_public_status_id(), bbp_get_reply_post_type(), $topic_id, bbp_get_topic_post_type() );
     2737    $replies = (int) $bbp_db->get_var( $query );
     2738
     2739    update_post_meta( $topic_id, '_bbp_anonymous_reply_count', $replies );
     2740
     2741    return (int) apply_filters( 'bbp_update_topic_anonymous_reply_count', $replies, $topic_id );
    27382742}
    27392743
  • trunk/src/includes/topics/template.php

    r5811 r5827  
    128128 * @uses bbp_get_super_stickies() To get the super stickies
    129129 * @uses bbp_get_stickies() To get the forum stickies
    130  * @uses wpdb::get_results() To execute our query and get the results
    131  * @uses WP_Rewrite::using_permalinks() To check if the blog is using permalinks
     130 * @uses bbp_use_pretty_urls() To check if the site is using pretty URLs
    132131 * @uses get_permalink() To get the permalink
    133132 * @uses add_query_arg() To add custom args to the url
     
    140139 */
    141140function bbp_has_topics( $args = array() ) {
    142     global $wp_rewrite;
    143141
    144142    /** Defaults **************************************************************/
     
    354352
    355353        // If pretty permalinks are enabled, make our pagination pretty
    356         if ( $wp_rewrite->using_permalinks() ) {
     354        if ( bbp_use_pretty_urls() ) {
    357355
    358356            // User's topics
     
    398396
    399397            // Use pagination base
    400             $base = trailingslashit( $base ) . user_trailingslashit( $wp_rewrite->pagination_base . '/%#%/' );
     398            $base = trailingslashit( $base ) . user_trailingslashit( bbp_get_paged_slug() . '/%#%/' );
    401399
    402400        // Unpretty pagination
     
    420418
    421419        // Remove first page from pagination
    422         $bbp->topic_query->pagination_links = str_replace( $wp_rewrite->pagination_base . "/1/'", "'", $bbp->topic_query->pagination_links );
     420        $bbp->topic_query->pagination_links = str_replace( bbp_get_paged_slug() . "/1/'", "'", $bbp->topic_query->pagination_links );
    423421    }
    424422
     
    851849     *  - after: After the links
    852850     * @uses bbp_get_topic_id() To get the topic id
    853      * @uses WP_Rewrite::using_permalinks() To check if the blog is using
    854      *                                       permalinks
     851     * @uses bbp_use_pretty_urls() To check if the site is using pretty URLs
    855852     * @uses user_trailingslashit() To add a trailing slash
    856853     * @uses trailingslashit() To add a trailing slash
     
    866863     */
    867864    function bbp_get_topic_pagination( $args = array() ) {
    868         global $wp_rewrite;
    869865
    870866        // Bail if threading replies
     
    881877
    882878        // If pretty permalinks are enabled, make our pagination pretty
    883         if ( $wp_rewrite->using_permalinks() ) {
    884             $base = trailingslashit( get_permalink( $r['topic_id'] ) ) . user_trailingslashit( $wp_rewrite->pagination_base . '/%#%/' );
     879        if ( bbp_use_pretty_urls() ) {
     880            $base = trailingslashit( get_permalink( $r['topic_id'] ) ) . user_trailingslashit( bbp_get_paged_slug() . '/%#%/' );
    885881        } else {
    886882            $base = add_query_arg( 'paged', '%#%', get_permalink( $r['topic_id'] ) );
     
    910906
    911907            // Remove first page from pagination
    912             if ( $wp_rewrite->using_permalinks() ) {
    913                 $pagination_links = str_replace( $wp_rewrite->pagination_base . '/1/', '', $pagination_links );
     908            if ( bbp_use_pretty_urls() ) {
     909                $pagination_links = str_replace( bbp_get_paged_slug() . '/1/', '', $pagination_links );
    914910            } else {
    915911                $pagination_links = str_replace( '&#038;paged=1', '', $pagination_links );
     
    26402636     */
    26412637    function bbp_get_topic_edit_url( $topic_id = 0 ) {
    2642         global $wp_rewrite;
    26432638
    26442639        $topic = bbp_get_topic( $topic_id );
     
    26512646
    26522647        // Pretty permalinks
    2653         if ( $wp_rewrite->using_permalinks() ) {
     2648        if ( bbp_use_pretty_urls() ) {
    26542649            $url = trailingslashit( $topic_link ) . bbp_get_edit_rewrite_id();
    2655             $url = trailingslashit( $url );
     2650            $url = user_trailingslashit( $url );
    26562651
    26572652        // Unpretty permalinks
    26582653        } else {
    2659             $url = add_query_arg( array( bbp_get_topic_post_type() => $topic->post_name, bbp_get_edit_rewrite_id() => '1' ), $topic_link );
     2654            $url = add_query_arg( array(
     2655                bbp_get_topic_post_type() => $topic->post_name,
     2656                bbp_get_edit_rewrite_id() => '1'
     2657            ), $topic_link );
    26602658        }
    26612659
     
    37673765     */
    37683766    function bbp_get_topic_tag_edit_link( $tag = '' ) {
    3769         global $wp_rewrite;
    37703767
    37713768        // Get the term
     
    37813778
    37823779            // Pretty
    3783             if ( $wp_rewrite->using_permalinks() ) {
     3780            if ( bbp_use_pretty_urls() ) {
    37843781                $retval = user_trailingslashit( trailingslashit( bbp_get_topic_tag_link() ) . bbp_get_edit_rewrite_id() );
    37853782
  • trunk/src/includes/users/capabilities.php

    r5770 r5827  
    383383 * @since bbPress (r3405)
    384384 *
    385  * @global WPDB $wpdb
    386385 * @param int $user_id Optional. User ID to spam. Defaults to displayed user.
    387386
     
    420419
    421420    // Arm the torpedos
    422     global $wpdb;
     421    $bbp_db = bbp_db();
    423422
    424423    // Get the blog IDs of the user to mark as spam
     
    427426    // If user has no blogs, they are a guest on this site
    428427    if ( empty( $blogs ) ) {
    429         $blogs[ $wpdb->blogid ] = array();
     428        $blogs[ $bbp_db->blogid ] = array();
    430429    }
    431430
     
    441440
    442441        // Get topics and replies
    443         $posts = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_author = %d AND post_status = '%s' AND post_type IN ( {$post_types} )", $user_id, bbp_get_public_status_id() ) );
     442        $query = $bbp_db->prepare( "SELECT ID FROM {$bbp_db->posts} WHERE post_author = %d AND post_status = '%s' AND post_type IN ( {$post_types} )", $user_id, bbp_get_public_status_id() );
     443        $posts = $bbp_db->get_col( $query );
    444444
    445445        // Loop through posts and spam them
     
    475475 * @since bbPress (r3405)
    476476 *
    477  * @global WPDB $wpdb
    478477 * @param int $user_id Optional. User ID to unspam. Defaults to displayed user.
    479478 *
     
    511510
    512511    // Arm the torpedos
    513     global $wpdb;
     512    $bbp_db = bbp_db();
    514513
    515514    // Get the blog IDs of the user to mark as spam
     
    518517    // If user has no blogs, they are a guest on this site
    519518    if ( empty( $blogs ) ) {
    520         $blogs[ $wpdb->blogid ] = array();
     519        $blogs[ $bbp_db->blogid ] = array();
    521520    }
    522521
     
    532531
    533532        // Get topics and replies
    534         $posts = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_author = %d AND post_status = '%s' AND post_type IN ( {$post_types} )", $user_id, bbp_get_spam_status_id() ) );
     533        $query = $bbp_db->prepare( "SELECT ID FROM {$bbp_db->posts} WHERE post_author = %d AND post_status = '%s' AND post_type IN ( {$post_types} )", $user_id, bbp_get_spam_status_id() );
     534        $posts = $bbp_db->get_col( $query );
    535535
    536536        // Loop through posts and spam them
  • 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;
  • trunk/src/includes/users/template.php

    r5815 r5827  
    336336     * @param string $user_nicename Optional. User nicename
    337337     * @uses bbp_get_user_id() To get user id
    338      * @uses WP_Rewrite::using_permalinks() To check if the blog is using
    339      *                                       permalinks
     338     * @uses bbp_use_pretty_urls() To check if the site is using pretty URLs
    340339     * @uses add_query_arg() To add custom args to the url
    341340     * @uses home_url() To get blog home url
     
    345344     */
    346345    function bbp_get_user_profile_url( $user_id = 0, $user_nicename = '' ) {
    347         global $wp_rewrite;
    348346
    349347        // Use displayed user ID if there is one, and one isn't requested
     
    360358
    361359        // Pretty permalinks
    362         if ( $wp_rewrite->using_permalinks() ) {
     360        if ( bbp_use_pretty_urls() ) {
    363361
    364362            // Get username if not passed
     
    367365            }
    368366
    369             $url = trailingslashit( $wp_rewrite->root . bbp_get_user_slug() ) . $user_nicename;
     367            $url = trailingslashit( bbp_get_root_url() . bbp_get_user_slug() ) . $user_nicename;
    370368            $url = user_trailingslashit( $url );
    371369            $url = home_url( $url );
     
    439437     * @uses bbp_get_user_id() To get user id
    440438     * @uses bbp_get_user_profile_url() To get the user profile url
    441      * @uses WP_Rewrite::using_permalinks() To check if the blog is using
    442      *                                       permalinks
     439     * @uses bbp_use_pretty_urls() To check if the site is using pretty URLs
    443440     * @uses add_query_arg() To add custom args to the url
    444441     * @uses home_url() To get blog home url
     
    448445     */
    449446    function bbp_get_user_profile_edit_url( $user_id = 0, $user_nicename = '' ) {
    450         global $wp_rewrite;
    451447
    452448        $user_id = bbp_get_user_id( $user_id );
     
    465461
    466462        // Pretty permalinks
    467         if ( $wp_rewrite->using_permalinks() ) {
     463        if ( bbp_use_pretty_urls() ) {
    468464            $url = trailingslashit( $profile_url ) . 'edit';
    469465            $url = user_trailingslashit( $url );
     
    820816     */
    821817    function bbp_get_favorites_permalink( $user_id = 0 ) {
    822         global $wp_rewrite;
    823818
    824819        // Use displayed user ID if there is one, and one isn't requested
     
    838833
    839834        // Pretty permalinks
    840         if ( $wp_rewrite->using_permalinks() ) {
     835        if ( bbp_use_pretty_urls() ) {
    841836            $url = trailingslashit( $profile_url ) . bbp_get_user_favorites_rewrite_id();
    842837            $url = user_trailingslashit( $url );
     
    980975     */
    981976    function bbp_get_subscriptions_permalink( $user_id = 0 ) {
    982         global $wp_rewrite;
    983977
    984978        // Use displayed user ID if there is one, and one isn't requested
     
    998992
    999993        // Pretty permalinks
    1000         if ( $wp_rewrite->using_permalinks() ) {
     994        if ( bbp_use_pretty_urls() ) {
    1001995            $url = trailingslashit( $profile_url ) . bbp_get_user_subscriptions_slug();
    1002996            $url = user_trailingslashit( $url );
     
    14211415     */
    14221416    function bbp_get_user_topics_created_url( $user_id = 0 ) {
    1423         global $wp_rewrite;
    14241417
    14251418        // Use displayed user ID if there is one, and one isn't requested
     
    14391432
    14401433        // Pretty permalinks
    1441         if ( $wp_rewrite->using_permalinks() ) {
     1434        if ( bbp_use_pretty_urls() ) {
    14421435            $url = trailingslashit( $profile_url ) . bbp_get_topic_archive_slug();
    14431436            $url = user_trailingslashit( $url );
     
    14791472     */
    14801473    function bbp_get_user_replies_created_url( $user_id = 0 ) {
    1481         global $wp_rewrite;
    14821474
    14831475        // Use displayed user ID if there is one, and one isn't requested
     
    14971489
    14981490        // Pretty permalinks
    1499         if ( $wp_rewrite->using_permalinks() ) {
     1491        if ( bbp_use_pretty_urls() ) {
    15001492            $url = trailingslashit( $profile_url ) . bbp_get_reply_archive_slug();
    15011493            $url = user_trailingslashit( $url );
Note: See TracChangeset for help on using the changeset viewer.