Improvements for get_comment_link() from Viper007Bond. fixes #8287

git-svn-id: http://svn.automattic.com/wordpress/trunk@9808 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
markjaquith 2008-11-20 06:41:55 +00:00
parent 821218975c
commit 30c3602b92
2 changed files with 63 additions and 22 deletions

View File

@ -441,21 +441,40 @@ 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.
* @param array $args Optional args.
* @return string The permalink to the current comment
*/
function get_comment_link( $comment = null, $page = null ) {
global $wp_rewrite;
function get_comment_link( $comment = null, $args = array() ) {
global $wp_rewrite, $in_comment_loop;
$comment = get_comment($comment);
if ( get_option('page_comments') ) {
$page = ( null !== $page ) ? (int) $page : get_page_of_comment( $comment->comment_ID );
// Backwards compat
if ( !is_array($args) ) {
$page = $args;
$args = array();
$args['page'] = $page;
}
$defaults = array( 'type' => 'all', 'page' => '', 'per_page' => '', 'max_depth' => '' );
$args = wp_parse_args( $args, $defaults );
if ( '' === $args['per_page'] && get_option('page_comments') )
$args['per_page'] = get_query_var('comments_per_page');
if ( empty($args['per_page']) ) {
$args['per_page'] = 0;
$args['page'] = 0;
}
if ( $args['per_page'] ) {
if ( '' == $args['page'] )
$args['page'] = ( !empty($in_comment_loop) ) ? get_query_var('cpage') : get_page_of_comment( $comment->comment_ID, $args );
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-' . $args['page'], 'comment' ) . '#comment-' . $comment->comment_ID;
else
return add_query_arg( 'cpage', $page, get_permalink( $comment->comment_post_ID ) ) . '#comment-' . $comment->comment_ID;
return add_query_arg( 'cpage', $args['page'], get_permalink( $comment->comment_post_ID ) ) . '#comment-' . $comment->comment_ID;
} else {
return get_permalink( $comment->comment_post_ID ) . '#comment-' . $comment->comment_ID;
}
@ -1162,7 +1181,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, $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>
<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>
<?php comment_text() ?>
@ -1209,7 +1228,9 @@ class Walker_Comment extends Walker {
* @param array $comments Optional array of comment objects. Defaults to $wp_query->comments
*/
function wp_list_comments($args = array(), $comments = null ) {
global $wp_query, $comment_alt, $comment_depth, $comment_thread_alt, $overridden_cpage;
global $wp_query, $comment_alt, $comment_depth, $comment_thread_alt, $overridden_cpage, $in_comment_loop;
$in_comment_loop = true;
$comment_alt = $comment_thread_alt = 0;
$comment_depth = 1;
@ -1285,6 +1306,8 @@ function wp_list_comments($args = array(), $comments = null ) {
$walker->paged_walk($_comments, $max_depth, $page, $per_page, $r);
$wp_query->max_num_comment_pages = $walker->max_pages;
$in_comment_loop = false;
}
?>
?>

View File

@ -548,37 +548,55 @@ function get_comment_pages_count( $comments = null, $per_page = null, $threaded
* @uses get_page_of_comment() Used to loop up to top level comment.
*
* @param int $comment_ID Comment ID.
* @param int $per_page Optional comments per page.
* @param array $args Optional args.
* @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, $args = array() ) {
global $wpdb;
if ( !$comment = get_comment( $comment_ID ) )
return;
if ( !get_option('page_comments') )
$defaults = array( 'type' => 'all', 'page' => '', 'per_page' => '', 'max_depth' => '' );
$args = wp_parse_args( $args, $defaults );
if ( '' === $args['per_page'] && get_option('page_comments') )
$args['per_page'] = get_query_var('comments_per_page');
if ( empty($args['per_page']) ) {
$args['per_page'] = 0;
$args['page'] = 0;
}
if ( $args['per_page'] < 1 )
return 1;
if ( null === $per_page )
$per_page = get_option('comments_per_page');
if ( null === $threaded )
$threaded = get_option('thread_comments');
if ( '' === $args['max_depth'] ) {
if ( get_option('thread_comments') )
$args['max_depth'] = get_option('thread_comments_depth');
else
$args['max_depth'] = -1;
}
// Find this comment's top level parent if threading is enabled
if ( $threaded && 0 != $comment->comment_parent )
return get_page_of_comment( $comment->comment_parent, $per_page, $threaded );
if ( $args['max_depth'] > 1 && 0 != $comment->comment_parent )
return get_page_of_comment( $comment->comment_parent, $args );
$allowedtypes = array(
'comment' => '',
'pingback' => 'pingback',
'trackback' => 'trackback',
);
$comtypewhere = ( 'all' != $args['type'] && isset($allowedtypes[$args['type']]) ) ? " AND comment_type = '" . $allowedtypes[$args['type']] . "'" : '';
// 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 ) );
$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'" . $comtypewhere, $comment->comment_post_ID, $comment->comment_date_gmt ) );
// No older comments? Then it's page #1.
if ( 0 == $oldercoms )
return 1;
// Divide comments older than this one by comments per page to get this comment's page number
return ceil( ( $oldercoms + 1 ) / $per_page );
return ceil( ( $oldercoms + 1 ) / $args['per_page'] );
}
/**