Skip to:
Content

bbPress.org

Ticket #2586: tools-fix.patch

File tools-fix.patch, 3.5 KB (added by netweb, 12 years ago)
  • src/includes/admin/tools.php

     
    325325        $statement = __( 'Counting the number of replies in each topic… %s', 'bbpress' );
    326326        $result    = __( 'Failed!', 'bbpress' );
    327327
    328         $sql_delete = "DELETE FROM `{$wpdb->postmeta}` WHERE `meta_key` = '_bbp_reply_count';";
    329         if ( is_wp_error( $wpdb->query( $sql_delete ) ) )
    330                 return array( 1, sprintf( $statement, $result ) );
    331 
    332328        // Post types and status
    333329        $tpt = bbp_get_topic_post_type();
    334330        $rpt = bbp_get_reply_post_type();
     
    335331        $pps = bbp_get_public_status_id();
    336332        $cps = bbp_get_closed_status_id();
    337333
     334        // Get an array of topic id's and delete the meta key _bbp_reply_count for each
     335        $topics  = $wpdb->get_results( "SELECT `ID` FROM `{$wpdb->posts}` WHERE `post_type` = '{$tpt}';", OBJECT_K );
     336
     337        if ( !empty( $topics ) ) {
     338                $topic_ids = array();
     339                foreach ( $topics as $key => $value ) {
     340                        $topic_ids[] = $key;
     341                }
     342
     343                $topic_ids   = implode( ", ", $topic_ids );
     344                $sql_delete = "DELETE FROM `{$wpdb->postmeta}` WHERE `post_id` IN ({$topic_ids}) AND `meta_key` = '_bbp_reply_count';";
     345                if ( is_wp_error( $wpdb->query( $sql_delete ) ) ) {
     346                        return array( 1, sprintf( $statement, $result ) );
     347                }
     348        }
     349
     350        // Cleanup
     351        unset( $topics, $topic_ids, $sql_delete );
     352
     353        // Recalculate the meta key _bbp_reply_count for each topic
    338354        $sql = "INSERT INTO `{$wpdb->postmeta}` (`post_id`, `meta_key`, `meta_value`) (
    339355                        SELECT `topics`.`ID` AS `post_id`, '_bbp_reply_count' AS `meta_key`, COUNT(`replies`.`ID`) As `meta_value`
    340356                                FROM `{$wpdb->posts}` AS `topics`
     
    346362                                        AND `topics`.`post_status` IN ( '{$pps}', '{$cps}' )
    347363                                GROUP BY `topics`.`ID`);";
    348364
    349         if ( is_wp_error( $wpdb->query( $sql ) ) )
     365        if ( is_wp_error( $wpdb->query( $sql ) ) ) {
    350366                return array( 2, sprintf( $statement, $result ) );
     367        }
    351368
    352369        return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
    353370}
     
    619636        $statement = __( 'Counting the number of replies in each forum… %s', 'bbpress' );
    620637        $result    = __( 'Failed!', 'bbpress' );
    621638
    622         $sql_delete = "DELETE FROM `{$wpdb->postmeta}` WHERE `meta_key` IN ( '_bbp_reply_count', '_bbp_total_reply_count' );";
    623         if ( is_wp_error( $wpdb->query( $sql_delete ) ) )
    624                 return array( 1, sprintf( $statement, $result ) );
     639        // Post types and status
     640        $fpt = bbp_get_forum_post_type();
    625641
     642        // Get an array of forum id's and delete the meta key _bbp_reply_count for each
     643        $forums  = $wpdb->get_results( "SELECT `ID` FROM `{$wpdb->posts}` WHERE `post_type` = '{$fpt}';", OBJECT_K );
     644
     645        if ( !empty( $forums ) ) {
     646                $forums_ids = array();
     647                foreach ( $forums as $key => $value ) {
     648                        $forums_ids[] = $key;
     649                }
     650
     651                $forums_ids   = implode( ", ", $forums_ids );
     652                $sql_delete = "DELETE FROM `{$wpdb->postmeta}` WHERE `post_id` IN ({$forums_ids}) AND `meta_key` = '_bbp_reply_count';";
     653                if ( is_wp_error( $wpdb->query( $sql_delete ) ) ) {
     654                        return array( 1, sprintf( $statement, $result ) );
     655                }
     656        }
     657
     658        // Cleanup
     659        unset( $forums, $forums_ids, $sql_delete );
     660
     661        // Recalculate the meta key _bbp_reply_count for each forum
    626662        $forums = get_posts( array( 'post_type' => bbp_get_forum_post_type(), 'numberposts' => -1 ) );
    627663        if ( !empty( $forums ) ) {
    628664                foreach ( $forums as $forum ) {
     
    14841520                        bbp_admin_tools_feedback( $message );
    14851521                }
    14861522        }
    1487 }
    1488  No newline at end of file
     1523}