Make WordPress Core

Changeset 20609


Ignore:
Timestamp:
04/26/2012 09:09:51 PM (13 years ago)
Author:
nacin
Message:

Make the Recent Comments dashboard widget more performant on sites with large amounts of comments, in particular with a heavy spam ratio.

Simplifies the query by avoiding a join, and leverages the API now rather than a direct query.

fixes #14222.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/dashboard.php

    r20579 r20609  
    596596    global $wpdb;
    597597
    598     if ( current_user_can('edit_posts') )
    599         $allowed_states = array('0', '1');
    600     else
    601         $allowed_states = array('1');
    602 
    603598    // Select all comment types and filter out spam later for better query performance.
    604599    $comments = array();
     
    609604        ? absint( $widgets['dashboard_recent_comments']['items'] ) : 5;
    610605
    611     while ( count( $comments ) < $total_items && $possible = $wpdb->get_results( "SELECT * FROM $wpdb->comments c LEFT JOIN $wpdb->posts p ON c.comment_post_ID = p.ID WHERE p.post_status != 'trash' ORDER BY c.comment_date_gmt DESC LIMIT $start, 50" ) ) {
    612 
     606    $comments_query = array( 'number' => $total_items * 5, 'offset' => 0 );
     607    if ( ! current_user_can( 'edit_posts' ) )
     608        $comments_query['status'] = 'approve';
     609
     610    while ( count( $comments ) < $total_items && $possible = get_comments( $comments_query ) ) {
    613611        foreach ( $possible as $comment ) {
    614             if ( count( $comments ) >= $total_items )
    615                 break;
    616             if ( in_array( $comment->comment_approved, $allowed_states ) && current_user_can( 'read_post', $comment->comment_post_ID ) )
    617                 $comments[] = $comment;
    618         }
    619 
    620         $start = $start + 50;
    621     }
    622 
    623     if ( $comments ) :
    624 ?>
    625 
    626         <div id="the-comment-list" class="list:comment">
    627 <?php
     612            if ( ! current_user_can( 'read_post', $comment->comment_post_ID ) )
     613                continue;
     614            $comments[] = $comment;
     615            if ( count( $comments ) == $total_items )
     616                break 2;
     617        }
     618        $comments_query['offset'] += $comments_query['number'];
     619        $comments_query['number'] = $total_items * 10;
     620    }
     621
     622    if ( $comments ) {
     623        echo '<div id="the-comment-list" class="list:comment">';
    628624        foreach ( $comments as $comment )
    629625            _wp_dashboard_recent_comments_row( $comment );
    630 ?>
    631 
    632         </div>
    633 
    634 <?php
    635         if ( current_user_can('edit_posts') ) { ?>
    636             <?php _get_list_table('WP_Comments_List_Table')->views(); ?>
    637 <?php   }
     626        echo '</div>';
     627
     628        if ( current_user_can('edit_posts') )
     629            _get_list_table('WP_Comments_List_Table')->views();
    638630
    639631        wp_comment_reply( -1, false, 'dashboard', false );
    640632        wp_comment_trashnotice();
    641 
    642     else :
    643 ?>
    644 
    645     <p><?php _e( 'No comments yet.' ); ?></p>
    646 
    647 <?php
    648     endif; // $comments;
     633    } else {
     634        echo '<p>' . __( 'No comments yet.' ) . '</p>';
     635    }
    649636}
    650637
Note: See TracChangeset for help on using the changeset viewer.