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

git-svn-id: http://svn.automattic.com/wordpress/trunk@9522 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
markjaquith 2008-11-05 07:09:20 +00:00
parent 5c9e75b18e
commit 88bfbbd948
3 changed files with 15 additions and 22 deletions

View File

@ -435,15 +435,16 @@ function comment_ID() {
* @uses $comment
*
* @param object|string|int $comment Comment to retrieve.
* @param string|int $page The comment's page if known. Optional. Avoids extra database query.
* @return string The permalink to the current comment
*/
function get_comment_link($comment = null) {
function get_comment_link( $comment = null, $page = null ) {
global $wp_rewrite;
$comment = get_comment($comment);
if ( get_option('page_comments') ) {
$page = get_page_of_comment( $comment->comment_ID );
$page = ( null !== $page ) ? (int) $page : get_page_of_comment( $comment->comment_ID );
if ( $wp_rewrite->using_permalinks() )
return user_trailingslashit( trailingslashit( get_permalink( $comment->comment_post_ID ) ) . "comment-page-$page", 'comment' ) . '#comment-' . $comment->comment_ID;
@ -1150,7 +1151,7 @@ class Walker_Comment extends Walker {
<br />
<?php endif; ?>
<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>
<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>
<?php comment_text() ?>

View File

@ -552,6 +552,8 @@ function get_comment_pages_count( $comments = null, $per_page = null, $threaded
* @return int|null Comment page number or null on error.
*/
function get_page_of_comment( $comment_ID, $per_page = null, $threaded = null ) {
global $wpdb;
if ( !$comment = get_comment( $comment_ID ) )
return;
@ -568,25 +570,15 @@ function get_page_of_comment( $comment_ID, $per_page = null, $threaded = null )
if ( $threaded && 0 != $comment->comment_parent )
return get_page_of_comment( $comment->comment_parent, $per_page, $threaded );
$comments = get_comments( array( 'post_id' => $comment->comment_post_ID, 'order' => 'ASC' ) );
// Count comments older than this one
$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 ) );
// Start going through the comments until we find what page number the above top level comment is on
$page = 1;
$comthispage = 0;
foreach ( $comments as $com ) {
if ( $threaded && 0 != $com->comment_parent )
continue;
// No older comments? Then it's page #1.
if ( 0 == $oldercoms )
return 1;
if ( $com->comment_ID == $comment->comment_ID )
return $page;
$comthispage++;
if ( $comthispage >= $per_page ) {
$page++;
$comthispage = 0;
}
}
// Divide comments older than this one by comments per page to get this comment's page number
return ceil( ( $oldercoms + 1 ) / $per_page );
}
/**

View File

@ -1387,7 +1387,7 @@ function wp_widget_recent_comments($args) {
$number = 15;
if ( !$comments = wp_cache_get( 'recent_comments', 'widget' ) ) {
$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");
$comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT $number");
wp_cache_add( 'recent_comments', $comments, 'widget' );
}
?>
@ -1396,7 +1396,7 @@ function wp_widget_recent_comments($args) {
<?php echo $before_title . $title . $after_title; ?>
<ul id="recentcomments"><?php
if ( $comments ) : foreach ( (array) $comments as $comment) :
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>';
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>';
endforeach; endif;?></ul>
<?php echo $after_widget; ?>
<?php