From 53220e2a0ef8fa8dec270101f05f3165a293d61d Mon Sep 17 00:00:00 2001 From: ryan Date: Mon, 27 Oct 2008 16:31:26 +0000 Subject: [PATCH] Make get_comment_link() paging aware. Props Viper007Bond. see #7956 git-svn-id: http://svn.automattic.com/wordpress/trunk@9367 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/canonical.php | 6 ++-- wp-includes/comment-template.php | 18 ++++++++++-- wp-includes/comment.php | 49 ++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 6 deletions(-) diff --git a/wp-includes/canonical.php b/wp-includes/canonical.php index 16bda4a63f..11fc07d9d5 100644 --- a/wp-includes/canonical.php +++ b/wp-includes/canonical.php @@ -249,20 +249,20 @@ function redirect_canonical($requested_url=null, $do_redirect=true) { if ( $compare_original !== $compare_redirect ) { $redirect_url = $redirect['scheme'] . '://' . $redirect['host']; if ( !empty($redirect['port']) ) - $redirect_url .= ':' . $redirect['port']; + $redirect_url .= ':' . $redirect['port']; $redirect_url .= $redirect['path']; if ( !empty($redirect['query']) ) $redirect_url .= '?' . $redirect['query']; } if ( !$redirect_url || $redirect_url == $requested_url ) - return false; + return false; // Note that you can use the "redirect_canonical" filter to cancel a canonical redirect for whatever reason by returning FALSE $redirect_url = apply_filters('redirect_canonical', $redirect_url, $requested_url); if ( !$redirect_url || $redirect_url == $requested_url ) // yes, again -- in case the filter aborted the request - return false; + return false; if ( $do_redirect ) { // protect against chained redirects diff --git a/wp-includes/comment-template.php b/wp-includes/comment-template.php index b9c85dc757..246ee54f47 100644 --- a/wp-includes/comment-template.php +++ b/wp-includes/comment-template.php @@ -402,7 +402,7 @@ function comment_ID() { } /** - * Retrieve the link to the current comment. + * Retrieve the link to a given comment. * * @since 1.5.0 * @uses $comment @@ -411,8 +411,20 @@ function comment_ID() { * @return string The permalink to the current comment */ function get_comment_link($comment = null) { + global $wp_rewrite; + $comment = get_comment($comment); - return get_permalink( $comment->comment_post_ID ) . '#comment-' . $comment->comment_ID; + + if ( get_option('page_comments') ) { + $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; + else + return add_query_arg( 'cpage', $page, get_permalink( $comment->comment_post_ID ) ) . '#comment-' . $comment->comment_ID; + } else { + return get_permalink( $comment->comment_post_ID ) . '#comment-' . $comment->comment_ID; + } } /** @@ -1085,7 +1097,7 @@ class Walker_Comment extends Walker {
-
+
diff --git a/wp-includes/comment.php b/wp-includes/comment.php index b33b817368..538429a51f 100644 --- a/wp-includes/comment.php +++ b/wp-includes/comment.php @@ -519,6 +519,55 @@ function get_comment_pages_count( $comments = null, $per_page = null, $threaded return $count; } +/** + * Calculate what page number a comment will appear on for comment paging. + * + * @since 2.7.0 + * + * @param int $comment_ID Comment ID. + * @param int $per_page Optional comments per page. + * @return int|null Comment page number or null on error. + */ +function get_page_of_comment( $comment_ID, $per_page = null, $threaded = null ) { + if ( !$comment = get_comment( $comment_ID ) ) + return; + + if ( !get_option('page_comments') ) + return 1; + + $comments = array_reverse( get_comments( $comment->comment_post_ID ) ); + + if ( null === $per_page ) + $per_page = get_option('comments_per_page'); + + if ( null === $threaded ) + $threaded = get_option('thread_comments'); + + // Find this comment's top level parent + if ( $threaded ) { + while ( 0 != $comment->comment_parent ) + $comment = get_comment( $comment->comment_parent ); + } + + // 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; + + if ( $com->comment_ID == $comment->comment_ID ) + return $page; + + $comthispage++; + + if ( $comthispage >= $per_page ) { + $page++; + $comthispage = 0; + } + } +} + /** * Does comment contain blacklisted characters or words. *