Skip to:
Content

bbPress.org


Ignore:
Timestamp:
06/10/2017 11:19:08 PM (9 years ago)
Author:
johnjamesjacoby
Message:

Tools: Audit repair & upgrade tools.

  • Use escaped gettext variant functions where applicable
  • Use Query/Prepared/Results paradigm for direct database queries for easier step-through debugging
  • Fix a few small bugs in the engagements upgrade functions (none that affect 2.5 or beta/RC users)
  • General code formatting improvements and typo fixes
File:
1 edited

Legend:

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

    r6500 r6523  
    2929        <div class="wrap">
    3030                <h1><?php esc_html_e( 'Forum Tools', 'bbpress' ); ?></h1>
    31                 <h2 class="nav-tab-wrapper"><?php bbp_tools_admin_tabs( __( 'Upgrade Forums', 'bbpress' ) ); ?></h2>
     31                <h2 class="nav-tab-wrapper"><?php bbp_tools_admin_tabs( esc_html__( 'Upgrade Forums', 'bbpress' ) ); ?></h2>
    3232
    3333                <p><?php esc_html_e( 'As bbPress improves, occasionally database upgrades are required but some forums are too large to upgrade automatically. Use the tools below to manually run upgrade routines.', 'bbpress' ); ?></p>
     
    166166        // Define variables
    167167        $bbp_db    = bbp_db();
    168         $statement = __( 'Upgrading user engagements &hellip; %s', 'bbpress' );
    169         $result    = __( 'No engagements to upgrade.',             'bbpress' );
     168        $statement = esc_html__( 'Upgrading user engagements&hellip; %s', 'bbpress' );
     169        $result    = esc_html__( 'No engagements to upgrade.',            'bbpress' );
    170170
    171171        // Delete previous engagements
     
    175175        }
    176176
    177         // Post types and status
     177        // Post types and statuses
    178178        $tpt = bbp_get_topic_post_type();
    179179        $rpt = bbp_get_reply_post_type();
     
    190190                                GROUP BY postmeta.meta_value, posts.post_author)", $tpt, $rpt, $pps, $cps );
    191191
    192         $engagements = $bbp_db->query( $sql );
     192        // Run the big query
     193        $prepare     = $bbp_db->prepare( $sql, $tpt, $rpt, $pps, $cps );
     194        $engagements = $bbp_db->query( $prepare );
    193195
    194196        // Bail if no closed topics found
     
    217219        // Define variables
    218220        $bbp_db    = bbp_db();
    219         $statement = __( 'Upgrading BuddyPress group-forum relationships&hellip; %s', 'bbpress' );
     221        $statement = esc_html__( 'Upgrading BuddyPress group-forum relationships&hellip; %s', 'bbpress' );
    220222        $g_count   = 0;
    221223        $f_count   = 0;
     
    238240        // Bail if forum IDs returned an error
    239241        if ( is_wp_error( $forum_ids ) || empty( $bbp_db->last_result ) ) {
    240                 return array( 2, sprintf( $statement, __( 'Failed!', 'bbpress' ) ) );
     242                return array( 2, sprintf( $statement, esc_html__( 'Failed!', 'bbpress' ) ) );
    241243        }
    242244
     
    321323                        wp_update_post( array(
    322324                                'ID'         => $posts[0]->ID,
    323                                 'post_title' => __( 'Group Forums', 'bbpress' ),
    324                                 'post_name'  => __( 'group-forums', 'bbpress' ),
     325                                'post_title' => esc_html__( 'Group Forums', 'bbpress' ),
     326                                'post_name'  => esc_html__( 'group-forums', 'bbpress' ),
    325327                        ) );
    326328                }
     
    338340
    339341        // Complete results
    340         $result = sprintf( __( 'Complete! %s groups updated; %s forums updated; %s forum statuses synced.', 'bbpress' ), bbp_number_format( $g_count ), bbp_number_format( $f_count ), bbp_number_format( $s_count ) );
     342        $result = sprintf( esc_html__( 'Complete! %s groups updated; %s forums updated; %s forum statuses synced.', 'bbpress' ), bbp_number_format( $g_count ), bbp_number_format( $f_count ), bbp_number_format( $s_count ) );
    341343        return array( 0, sprintf( $statement, $result ) );
    342344}
     
    353355        // Define variables
    354356        $bbp_db    = bbp_db();
    355         $statement = __( 'Upgrading user favorites &hellip; %s', 'bbpress' );
    356         $result    = __( 'No favorites to upgrade.',             'bbpress' );
     357        $statement = esc_html__( 'Upgrading user favorites&hellip; %s', 'bbpress' );
     358        $result    = esc_html__( 'No favorites to upgrade.',            'bbpress' );
    357359        $total     = 0;
    358         $key       = $bbp_db->prefix . '_bbp_favorites';
    359         $favs      = $bbp_db->get_results( $bbp_db->prepare( "SELECT * FROM {$bbp_db->usermeta} WHERE meta_key = %s", $key ) );
     360        $old_key   = $bbp_db->prefix . '_bbp_favorites';
     361        $new_key   = '_bbp_favorite';
     362
     363        // Results
     364        $query     = "SELECT * FROM {$bbp_db->usermeta} WHERE meta_key = %s";
     365        $prepare   = $bbp_db->prepare( $query, $old_key );
     366        $favs      = $bbp_db->get_results( $prepare );
    360367
    361368        // Bail if no closed topics found
     
    374381
    375382                        // Skip if already exists
    376                         if ( $bbp_db->get_var( $bbp_db->prepare( "SELECT COUNT(*) FROM {$bbp_db->postmeta} WHERE post_id = %d AND meta_key = %s AND meta_value = %d", $post_id, '_bbp_favorite', $meta->user_id ) ) ) {
     383                        if ( $bbp_db->get_var( $bbp_db->prepare( "SELECT COUNT(*) FROM {$bbp_db->postmeta} WHERE post_id = %d AND meta_key = %s AND meta_value = %d", $post_id, $new_key, $meta->user_id ) ) ) {
    377384                                continue;
    378385                        }
    379386
    380387                        // Add the post meta
    381                         $added = add_post_meta( $post_id, '_bbp_favorite', $meta->user_id, false );
     388                        $added = add_post_meta( $post_id, $new_key, $meta->user_id, false );
    382389
    383390                        // Bump counts if successfully added
     
    408415        // Define variables
    409416        $bbp_db    = bbp_db();
    410         $statement = __( 'Upgrading user topic subscriptions &hellip; %s', 'bbpress' );
    411         $result    = __( 'No topic subscriptions to upgrade.',             'bbpress' );
     417        $statement = esc_html__( 'Upgrading user topic subscriptions&hellip; %s', 'bbpress' );
     418        $result    = esc_html__( 'No topic subscriptions to upgrade.',            'bbpress' );
    412419        $total     = 0;
    413         $key       = $bbp_db->prefix . '_bbp_subscriptions';
    414         $subs      = $bbp_db->get_results( $bbp_db->prepare( "SELECT * FROM {$bbp_db->usermeta} WHERE meta_key = %s ORDER BY user_id", $key ) );
     420        $old_key   = $bbp_db->prefix . '_bbp_subscriptions';
     421        $new_key   = '_bbp_subscription';
     422
     423        // Results
     424        $query     = "SELECT * FROM {$bbp_db->usermeta} WHERE meta_key = %s ORDER BY user_id";
     425        $prepare   = $bbp_db->prepare( $query, $old_key );
     426        $subs      = $bbp_db->get_results( $prepare );
    415427
    416428        // Bail if no topic subscriptions found
     
    429441
    430442                        // Skip if already exists
    431                         if ( $bbp_db->get_var( $bbp_db->prepare( "SELECT COUNT(*) FROM {$bbp_db->postmeta} WHERE post_id = %d AND meta_key = %s AND meta_value = %d", $post_id, '_bbp_subscription', $meta->user_id ) ) ) {
     443                        if ( $bbp_db->get_var( $bbp_db->prepare( "SELECT COUNT(*) FROM {$bbp_db->postmeta} WHERE post_id = %d AND meta_key = %s AND meta_value = %d", $post_id, $new_key, $meta->user_id ) ) ) {
    432444                                continue;
    433445                        }
    434446
    435447                        // Add the post meta
    436                         $added = add_post_meta( $post_id, '_bbp_subscription', $meta->user_id, false );
     448                        $added = add_post_meta( $post_id, $new_key, $meta->user_id, false );
    437449
    438450                        // Bump counts if successfully added
     
    463475        // Define variables
    464476        $bbp_db    = bbp_db();
    465         $statement = __( 'Upgrading user forum subscriptions &hellip; %s', 'bbpress' );
    466         $result    = __( 'No forum subscriptions to upgrade.',             'bbpress' );
     477        $statement = esc_html__( 'Upgrading user forum subscriptions&hellip; %s', 'bbpress' );
     478        $result    = esc_html__( 'No forum subscriptions to upgrade.',            'bbpress' );
    467479        $total     = 0;
    468         $key       = $bbp_db->prefix . '_bbp_forum_subscriptions';
    469         $subs      = $bbp_db->get_results( $bbp_db->prepare( "SELECT * FROM {$bbp_db->usermeta} WHERE meta_key = %s ORDER BY user_id", $key ) );
     480        $old_key   = $bbp_db->prefix . '_bbp_forum_subscriptions';
     481        $new_key   = '_bbp_subscription';
     482
     483        // Results
     484        $query     = "SELECT * FROM {$bbp_db->usermeta} WHERE meta_key = %s ORDER BY user_id";
     485        $prepare   = $bbp_db->prepare( $query, $old_key );
     486        $subs      = $bbp_db->get_results( $prepare );
    470487
    471488        // Bail if no forum subscriptions found
     
    484501
    485502                        // Skip if already exists
    486                         if ( $bbp_db->get_var( $bbp_db->prepare( "SELECT COUNT(*) FROM {$bbp_db->postmeta} WHERE post_id = %d AND meta_key = %s AND meta_value = %d", $post_id, '_bbp_forum_subscription', $meta->user_id ) ) ) {
     503                        if ( $bbp_db->get_var( $bbp_db->prepare( "SELECT COUNT(*) FROM {$bbp_db->postmeta} WHERE post_id = %d AND meta_key = %s AND meta_value = %d", $post_id, $new_key, $meta->user_id ) ) ) {
    487504                                continue;
    488505                        }
    489506
    490507                        // Add the post meta
    491                         $added = add_post_meta( $post_id, '_bbp_subscription', $meta->user_id, false );
     508                        $added = add_post_meta( $post_id, $new_key, $meta->user_id, false );
    492509
    493510                        // Bump counts if successfully added
     
    508525
    509526/**
    510  * Remove favorites data from wp_usermeta for bbPress 2.6 and higher
     527 * Remove favorites data from user meta for bbPress 2.6 and higher
    511528 *
    512529 * @since 2.6.0 bbPress (r6281)
     
    518535        // Define variables
    519536        $bbp_db    = bbp_db();
    520         $statement = __( 'Remove favorites from usermeta &hellip; %s', 'bbpress' );
    521         $result    = __( 'No favorites to remove.',                    'bbpress' );
     537        $statement = esc_html__( 'Remove favorites from usermeta&hellip; %s', 'bbpress' );
     538        $result    = esc_html__( 'No favorites to remove.',                   'bbpress' );
    522539        $total     = 0;
    523540        $key       = $bbp_db->prefix . '_bbp_favorites';
    524         $favs      = $bbp_db->get_results( $bbp_db->prepare( "SELECT * FROM {$bbp_db->usermeta} WHERE meta_key = %s ORDER BY user_id", $key ) );
     541
     542        // Results
     543        $query     = "SELECT * FROM {$bbp_db->usermeta} WHERE meta_key = %s ORDER BY user_id";
     544        $prepare   = $bbp_db->prepare( $query, $key );
     545        $favs      = $bbp_db->get_results( $prepare );
    525546
    526547        // Bail if no favorites found
     
    528549                return array( 1, sprintf( $statement, $result ) );
    529550        }
     551
     552        delete_metadata( 'user', false, $meta_key, false, true );
    530553
    531554        // Loop through each user's favorites
     
    549572
    550573/**
    551  * Remove topic subscriptions data from wp_usermeta for bbPress 2.6 and higher
     574 * Remove topic subscriptions data from user meta for bbPress 2.6 and higher
    552575 *
    553576 * @since 2.6.0 bbPress (r6281)
     
    559582        // Define variables
    560583        $bbp_db    = bbp_db();
    561         $statement = __( 'Remove topic subscriptions from usermeta &hellip; %s', 'bbpress' );
    562         $result    = __( 'No topic subscriptions to remove.',                    'bbpress' );
     584        $statement = esc_html__( 'Remove topic subscriptions from usermeta&hellip; %s', 'bbpress' );
     585        $result    = esc_html__( 'No topic subscriptions to remove.',                   'bbpress' );
    563586        $total     = 0;
    564587        $key       = $bbp_db->prefix . '_bbp_subscriptions';
    565         $subs      = $bbp_db->get_results( $bbp_db->prepare( "SELECT * FROM {$bbp_db->usermeta} WHERE meta_key = %s ORDER BY user_id", $key ) );
     588
     589        // Results
     590        $query     = "SELECT * FROM {$bbp_db->usermeta} WHERE meta_key = %s ORDER BY user_id";
     591        $prepare   = $bbp_db->prepare( $query, $key );
     592        $subs      = $bbp_db->get_results( $prepare );
    566593
    567594        // Bail if no forum favorites found
     
    590617
    591618/**
    592  * Remove topic subscriptions data from wp_usermeta for bbPress 2.6 and higher
     619 * Remove topic subscriptions data from user meta for bbPress 2.6 and higher
    593620 *
    594621 * @since 2.6.0 bbPress (r6281)
     
    600627        // Define variables
    601628        $bbp_db    = bbp_db();
    602         $statement = __( 'Remove forum subscriptions from usermeta &hellip; %s', 'bbpress' );
    603         $result    = __( 'No forum subscriptions to remove.',                    'bbpress' );
     629        $statement = esc_html__( 'Remove forum subscriptions from usermeta&hellip; %s', 'bbpress' );
     630        $result    = esc_html__( 'No forum subscriptions to remove.',                   'bbpress' );
    604631        $total     = 0;
    605632        $key       = $bbp_db->prefix . '_bbp_forum_subscriptions';
    606         $subs      = $bbp_db->get_results( $bbp_db->prepare( "SELECT * FROM {$bbp_db->usermeta} WHERE meta_key = %s ORDER BY user_id", $key ) );
     633
     634        // Query
     635        $query     = "SELECT * FROM {$bbp_db->usermeta} WHERE meta_key = %s ORDER BY user_id";
     636        $prepare   = $bbp_db->prepare( $query, $key );
     637        $subs      = $bbp_db->get_results( $prepare );
    607638
    608639        // Bail if no forum favorites found
Note: See TracChangeset for help on using the changeset viewer.