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:
parent
5c9e75b18e
commit
88bfbbd948
|
@ -435,15 +435,16 @@ function comment_ID() {
|
||||||
* @uses $comment
|
* @uses $comment
|
||||||
*
|
*
|
||||||
* @param object|string|int $comment Comment to retrieve.
|
* @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
|
* @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;
|
global $wp_rewrite;
|
||||||
|
|
||||||
$comment = get_comment($comment);
|
$comment = get_comment($comment);
|
||||||
|
|
||||||
if ( get_option('page_comments') ) {
|
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() )
|
if ( $wp_rewrite->using_permalinks() )
|
||||||
return user_trailingslashit( trailingslashit( get_permalink( $comment->comment_post_ID ) ) . "comment-page-$page", 'comment' ) . '#comment-' . $comment->comment_ID;
|
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 />
|
<br />
|
||||||
<?php endif; ?>
|
<?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',' ','') ?></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',' ','') ?></div>
|
||||||
|
|
||||||
<?php comment_text() ?>
|
<?php comment_text() ?>
|
||||||
|
|
||||||
|
|
|
@ -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.
|
* @return int|null Comment page number or null on error.
|
||||||
*/
|
*/
|
||||||
function get_page_of_comment( $comment_ID, $per_page = null, $threaded = null ) {
|
function get_page_of_comment( $comment_ID, $per_page = null, $threaded = null ) {
|
||||||
|
global $wpdb;
|
||||||
|
|
||||||
if ( !$comment = get_comment( $comment_ID ) )
|
if ( !$comment = get_comment( $comment_ID ) )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -568,25 +570,15 @@ function get_page_of_comment( $comment_ID, $per_page = null, $threaded = null )
|
||||||
if ( $threaded && 0 != $comment->comment_parent )
|
if ( $threaded && 0 != $comment->comment_parent )
|
||||||
return get_page_of_comment( $comment->comment_parent, $per_page, $threaded );
|
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
|
// No older comments? Then it's page #1.
|
||||||
$page = 1;
|
if ( 0 == $oldercoms )
|
||||||
$comthispage = 0;
|
return 1;
|
||||||
foreach ( $comments as $com ) {
|
|
||||||
if ( $threaded && 0 != $com->comment_parent )
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if ( $com->comment_ID == $comment->comment_ID )
|
// Divide comments older than this one by comments per page to get this comment's page number
|
||||||
return $page;
|
return ceil( ( $oldercoms + 1 ) / $per_page );
|
||||||
|
|
||||||
$comthispage++;
|
|
||||||
|
|
||||||
if ( $comthispage >= $per_page ) {
|
|
||||||
$page++;
|
|
||||||
$comthispage = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1387,7 +1387,7 @@ function wp_widget_recent_comments($args) {
|
||||||
$number = 15;
|
$number = 15;
|
||||||
|
|
||||||
if ( !$comments = wp_cache_get( 'recent_comments', 'widget' ) ) {
|
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' );
|
wp_cache_add( 'recent_comments', $comments, 'widget' );
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
@ -1396,7 +1396,7 @@ function wp_widget_recent_comments($args) {
|
||||||
<?php echo $before_title . $title . $after_title; ?>
|
<?php echo $before_title . $title . $after_title; ?>
|
||||||
<ul id="recentcomments"><?php
|
<ul id="recentcomments"><?php
|
||||||
if ( $comments ) : foreach ( (array) $comments as $comment) :
|
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>
|
endforeach; endif;?></ul>
|
||||||
<?php echo $after_widget; ?>
|
<?php echo $after_widget; ?>
|
||||||
<?php
|
<?php
|
||||||
|
|
Loading…
Reference in New Issue