WordPress.org

Make WordPress Core

Changeset 9522


Ignore:
Timestamp:
11/05/08 07:09:20 (5 years ago)
Author:
markjaquith
Message:

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

Location:
trunk/wp-includes
Files:
3 edited

Legend:

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

    r9436 r9522  
    436436 * 
    437437 * @param object|string|int $comment Comment to retrieve. 
     438 * @param string|int $page The comment's page if known. Optional. Avoids extra database query. 
    438439 * @return string The permalink to the current comment 
    439440 */ 
    440 function get_comment_link($comment = null) { 
     441function get_comment_link( $comment = null, $page = null ) { 
    441442    global $wp_rewrite; 
    442443 
     
    444445 
    445446    if ( get_option('page_comments') ) { 
    446         $page = get_page_of_comment( $comment->comment_ID ); 
     447        $page = ( null !== $page ) ? (int) $page : get_page_of_comment( $comment->comment_ID ); 
    447448 
    448449        if ( $wp_rewrite->using_permalinks() ) 
     
    11511152<?php endif; ?> 
    11521153 
    1153         <div class="comment-meta commentmetadata"><a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ) ?>"><?php printf(__('%1$s at %2$s'), get_comment_date('F jS, Y'),  get_comment_time()) ?></a><?php edit_comment_link('edit','&nbsp;&nbsp;','') ?></div> 
     1154        <div class="comment-meta commentmetadata"><a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID, $page ) ) ?>"><?php printf(__('%1$s at %2$s'), get_comment_date('F jS, Y'),  get_comment_time()) ?></a><?php edit_comment_link('edit','&nbsp;&nbsp;','') ?></div> 
    11541155 
    11551156        <?php comment_text() ?> 
  • 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 
  • trunk/wp-includes/widgets.php

    r9506 r9522  
    13881388 
    13891389    if ( !$comments = wp_cache_get( 'recent_comments', 'widget' ) ) { 
    1390         $comments = $wpdb->get_results("SELECT comment_author, comment_author_url, comment_ID, comment_post_ID FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT $number"); 
     1390        $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT $number"); 
    13911391        wp_cache_add( 'recent_comments', $comments, 'widget' ); 
    13921392    } 
     
    13971397            <ul id="recentcomments"><?php 
    13981398            if ( $comments ) : foreach ( (array) $comments as $comment) : 
    1399             echo  '<li class="recentcomments">' . sprintf(__('%1$s on %2$s'), get_comment_author_link(), '<a href="'. get_permalink($comment->comment_post_ID) . '#comment-' . $comment->comment_ID . '">' . get_the_title($comment->comment_post_ID) . '</a>') . '</li>'; 
     1399            echo  '<li class="recentcomments">' . sprintf(__('%1$s on %2$s'), get_comment_author_link(), '<a href="'. get_comment_link($comment->comment_ID) . '">' . get_the_title($comment->comment_post_ID) . '</a>') . '</li>'; 
    14001400            endforeach; endif;?></ul> 
    14011401        <?php echo $after_widget; ?> 
Note: See TracChangeset for help on using the changeset viewer.