Skip to:
Content

bbPress.org


Ignore:
Timestamp:
05/07/2012 12:09:05 AM (15 years ago)
Author:
johnjamesjacoby
Message:

Converter:

  • Add user_topic_count repair function
  • Fix incorrect meta key in user_reply_count repair function
  • See #1820
File:
1 edited

Legend:

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

    r3887 r3890  
    365365
    366366/**
    367  * Recount topic replied by the users
    368  *
    369  * @since bbPress (r2613)
     367 * Recount topics by the users
     368 *
     369 * @since bbPress (r3889)
    370370 *
    371371 * @uses bbp_get_reply_post_type() To get the reply post type
    … …  
    374374 * @return array An array of the status code and the message
    375375 */
    376 function bbp_admin_repair_user_reply_count() {
    377         global $wpdb;
    378 
    379         $statement = __( 'Counting the number of topics to which each user has replied… %s', 'bbpress' );
    380         $result    = __( 'Failed!', 'bbpress' );
    381 
    382         $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`;";
     376function bbp_admin_repair_user_topic_count() {
     377        global $wpdb;
     378
     379        $statement   = __( 'Counting the number of topics each user has created… %s', 'bbpress' );
     380        $result      = __( 'Failed!', 'bbpress' );
     381        $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`;";
    383382        $insert_rows = $wpdb->get_results( $sql_select );
    384383
    … …  
    388387        $insert_values = array();
    389388        foreach ( $insert_rows as $insert_row )
    390                 $insert_values[] = "('{$insert_row->post_author}', '_bbp_topics_replied', '{$insert_row->_count}')";
     389                $insert_values[] = "('{$insert_row->post_author}', '_bbp_topic_count', '{$insert_row->_count}')";
    391390
    392391        if ( !count( $insert_values ) )
    393392                return array( 2, sprintf( $statement, $result ) );
    394393
    395         $sql_delete = "DELETE FROM `{$wpdb->usermeta}` WHERE `meta_key` = '_bbp_topics_replied';";
     394        $sql_delete = "DELETE FROM `{$wpdb->usermeta}` WHERE `meta_key` = '_bbp_topic_count';";
    396395        if ( is_wp_error( $wpdb->query( $sql_delete ) ) )
    397396                return array( 3, sprintf( $statement, $result ) );
    … …  
    402401                $sql_insert = "INSERT INTO `{$wpdb->usermeta}` (`user_id`, `meta_key`, `meta_value`) VALUES $chunk;";
    403402
    404                 if ( is_wp_error( $wpdb->query( $sql_insert ) ) )
     403                if ( is_wp_error( $wpdb->query( $sql_insert ) ) ) {
    405404                        return array( 4, sprintf( $statement, $result ) );
     405                }
     406        }
     407
     408        $result = __( 'Complete!', 'bbpress' );
     409        return array( 0, sprintf( $statement, $result ) );
     410}
     411
     412/**
     413 * Recount topic replied by the users
     414 *
     415 * @since bbPress (r2613)
     416 *
     417 * @uses bbp_get_reply_post_type() To get the reply post type
     418 * @uses wpdb::query() To run our recount sql queries
     419 * @uses is_wp_error() To check if the executed query returned {@link WP_Error}
     420 * @return array An array of the status code and the message
     421 */
     422function bbp_admin_repair_user_reply_count() {
     423        global $wpdb;
     424
     425        $statement   = __( 'Counting the number of topics to which each user has replied… %s', 'bbpress' );
     426        $result      = __( 'Failed!', 'bbpress' );
     427        $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`;";
     428        $insert_rows = $wpdb->get_results( $sql_select );
     429
     430        if ( is_wp_error( $insert_rows ) )
     431                return array( 1, sprintf( $statement, $result ) );
     432
     433        $insert_values = array();
     434        foreach ( $insert_rows as $insert_row )
     435                $insert_values[] = "('{$insert_row->post_author}', '_bbp_reply_count', '{$insert_row->_count}')";
     436
     437        if ( !count( $insert_values ) )
     438                return array( 2, sprintf( $statement, $result ) );
     439
     440        $sql_delete = "DELETE FROM `{$wpdb->usermeta}` WHERE `meta_key` = '_bbp_reply_count';";
     441        if ( is_wp_error( $wpdb->query( $sql_delete ) ) )
     442                return array( 3, sprintf( $statement, $result ) );
     443
     444        $insert_values = array_chunk( $insert_values, 10000 );
     445        foreach ( $insert_values as $chunk ) {
     446                $chunk = "\n" . join( ",\n", $chunk );
     447                $sql_insert = "INSERT INTO `{$wpdb->usermeta}` (`user_id`, `meta_key`, `meta_value`) VALUES $chunk;";
     448
     449                if ( is_wp_error( $wpdb->query( $sql_insert ) ) ) {
     450                        return array( 4, sprintf( $statement, $result ) );
     451                }
    406452        }
    407453
Note: See TracChangeset for help on using the changeset viewer.