diff --git a/wp-includes/comment-template.php b/wp-includes/comment-template.php index f95d6fdc80..e4840eab0f 100644 --- a/wp-includes/comment-template.php +++ b/wp-includes/comment-template.php @@ -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 {
-
+
diff --git a/wp-includes/comment.php b/wp-includes/comment.php index d2f0ee4c94..3f0f064dbc 100644 --- a/wp-includes/comment.php +++ b/wp-includes/comment.php @@ -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 ); } /** diff --git a/wp-includes/widgets.php b/wp-includes/widgets.php index 191057c6a1..9e8c366b52 100644 --- a/wp-includes/widgets.php +++ b/wp-includes/widgets.php @@ -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) {