Make WordPress Core


Ignore:
Timestamp:
11/05/2008 07:09:20 AM (15 years ago)
Author:
markjaquith
Message:

Massive get_comment_link() performance improvements for posts with a lot of comments. props Viper007Bond. fixes #7956

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/comment.php

    r9511 r9522  
    553553 */
    554554function get_page_of_comment( $comment_ID, $per_page = null, $threaded = null ) {
     555    global $wpdb;
     556
    555557    if ( !$comment = get_comment( $comment_ID ) )
    556558        return;
     
    569571        return get_page_of_comment( $comment->comment_parent, $per_page, $threaded );
    570572
    571     $comments = get_comments( array( 'post_id' => $comment->comment_post_ID, 'order' => 'ASC' ) );
    572 
    573     // Start going through the comments until we find what page number the above top level comment is on
    574     $page = 1;
    575     $comthispage = 0;
    576     foreach ( $comments as $com ) {
    577         if ( $threaded && 0 != $com->comment_parent )
    578             continue;
    579 
    580         if ( $com->comment_ID == $comment->comment_ID )
    581             return $page;
    582 
    583         $comthispage++;
    584 
    585         if ( $comthispage >= $per_page ) {
    586             $page++;
    587             $comthispage = 0;
    588         }
    589     }
     573    // Count comments older than this one
     574    $oldercoms = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_parent = 0 AND comment_date_gmt < '%s'", $comment->comment_post_ID, $comment->comment_date_gmt ) );
     575
     576    // No older comments? Then it's page #1.
     577    if ( 0 == $oldercoms )
     578        return 1;
     579
     580    // Divide comments older than this one by comments per page to get this comment's page number
     581    return ceil( ( $oldercoms + 1 ) / $per_page );
    590582}
    591583
Note: See TracChangeset for help on using the changeset viewer.