Skip to:
Content

bbPress.org

Ticket #2586: tools-fix.2.patch

File tools-fix.2.patch, 3.0 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        // Delete the meta key _bbp_reply_count for each topic
     335        $sql_delete = "DELETE `postmeta` FROM `{$wpdb->postmeta}` AS `postmeta`
     336                                                LEFT JOIN `{$wpdb->posts}` AS `posts` ON `posts`.`ID` = `postmeta`.`post_id`
     337                                                WHERE `posts`.`post_type` = '{$tpt}'
     338                                                AND `postmeta`.`meta_key` = '_bbp_reply_count'";
     339        if ( is_wp_error( $wpdb->query( $sql_delete ) ) ) {
     340                return array( 1, sprintf( $statement, $result ) );
     341        }
     342
     343        // Recalculate the meta key _bbp_reply_count for each topic
    338344        $sql = "INSERT INTO `{$wpdb->postmeta}` (`post_id`, `meta_key`, `meta_value`) (
    339345                        SELECT `topics`.`ID` AS `post_id`, '_bbp_reply_count' AS `meta_key`, COUNT(`replies`.`ID`) As `meta_value`
    340346                                FROM `{$wpdb->posts}` AS `topics`
     
    346352                                        AND `topics`.`post_status` IN ( '{$pps}', '{$cps}' )
    347353                                GROUP BY `topics`.`ID`);";
    348354
    349         if ( is_wp_error( $wpdb->query( $sql ) ) )
     355        if ( is_wp_error( $wpdb->query( $sql ) ) ) {
    350356                return array( 2, sprintf( $statement, $result ) );
     357        }
    351358
    352359        return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
    353360}
     
    619626        $statement = __( 'Counting the number of replies in each forum… %s', 'bbpress' );
    620627        $result    = __( 'Failed!', 'bbpress' );
    621628
    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 ) ) )
     629        // Post types and status
     630        $fpt = bbp_get_forum_post_type();
     631
     632        // Delete the meta keys _bbp_reply_count and _bbp_total_reply_count for each forum
     633        $sql_delete = "DELETE `postmeta` FROM `{$wpdb->postmeta}` AS `postmeta`
     634                                                LEFT JOIN `{$wpdb->posts}` AS `posts` ON `posts`.`ID` = `postmeta`.`post_id`
     635                                                WHERE `posts`.`post_type` = '{$fpt}'
     636                                                AND `postmeta`.`meta_key` = '_bbp_reply_count'
     637                                                OR `postmeta`.`meta_key` = '_bbp_total_reply_count'";
     638
     639        if ( is_wp_error( $wpdb->query( $sql_delete ) ) ) {
    624640                return array( 1, sprintf( $statement, $result ) );
     641        }
    625642
     643        // Recalculate the metas key _bbp_reply_count and _bbp_total_reply_count for each forum
    626644        $forums = get_posts( array( 'post_type' => bbp_get_forum_post_type(), 'numberposts' => -1 ) );
    627645        if ( !empty( $forums ) ) {
    628646                foreach ( $forums as $forum ) {
     
    14841502                        bbp_admin_tools_feedback( $message );
    14851503                }
    14861504        }
    1487 }
    1488  No newline at end of file
     1505}