Make WordPress Core

Changeset 9808


Ignore:
Timestamp:
11/20/2008 06:41:55 AM (17 years ago)
Author:
markjaquith
Message:

Improvements for get_comment_link() from Viper007Bond. fixes #8287

Location:
trunk/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/comment-template.php

    r9781 r9808  
    442442 *
    443443 * @param object|string|int $comment Comment to retrieve.
    444  * @param string|int $page The comment's page if known. Optional. Avoids extra database query.
     444 * @param array $args Optional args.
    445445 * @return string The permalink to the current comment
    446446 */
    447 function get_comment_link( $comment = null, $page = null ) {
    448     global $wp_rewrite;
     447function get_comment_link( $comment = null, $args = array() ) {
     448    global $wp_rewrite, $in_comment_loop;
    449449
    450450    $comment = get_comment($comment);
    451451
    452     if ( get_option('page_comments') ) {
    453         $page = ( null !== $page ) ? (int) $page : get_page_of_comment( $comment->comment_ID );
     452    // Backwards compat
     453    if ( !is_array($args) ) {
     454        $page = $args;
     455        $args = array();
     456        $args['page'] = $page;
     457    }
     458
     459    $defaults = array( 'type' => 'all', 'page' => '', 'per_page' => '', 'max_depth' => '' );
     460    $args = wp_parse_args( $args, $defaults );
     461
     462    if ( '' === $args['per_page'] && get_option('page_comments') )
     463        $args['per_page'] = get_query_var('comments_per_page');
     464
     465    if ( empty($args['per_page']) ) {
     466        $args['per_page'] = 0;
     467        $args['page'] = 0;
     468    }
     469
     470    if ( $args['per_page'] ) {
     471        if ( '' == $args['page'] )
     472            $args['page'] = ( !empty($in_comment_loop) ) ? get_query_var('cpage') : get_page_of_comment( $comment->comment_ID, $args );
    454473
    455474        if ( $wp_rewrite->using_permalinks() )
    456             return user_trailingslashit( trailingslashit( get_permalink( $comment->comment_post_ID ) ) . "comment-page-$page", 'comment' ) . '#comment-' . $comment->comment_ID;
     475            return user_trailingslashit( trailingslashit( get_permalink( $comment->comment_post_ID ) ) . 'comment-page-' . $args['page'], 'comment' ) . '#comment-' . $comment->comment_ID;
    457476        else
    458             return add_query_arg( 'cpage', $page, get_permalink( $comment->comment_post_ID ) ) . '#comment-' . $comment->comment_ID;
     477            return add_query_arg( 'cpage', $args['page'], get_permalink( $comment->comment_post_ID ) ) . '#comment-' . $comment->comment_ID;
    459478    } else {
    460479        return get_permalink( $comment->comment_post_ID ) . '#comment-' . $comment->comment_ID;
     
    11631182<?php endif; ?>
    11641183
    1165         <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>
     1184        <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>
    11661185
    11671186        <?php comment_text() ?>
     
    12101229 */
    12111230function wp_list_comments($args = array(), $comments = null ) {
    1212     global $wp_query, $comment_alt, $comment_depth, $comment_thread_alt, $overridden_cpage;
     1231    global $wp_query, $comment_alt, $comment_depth, $comment_thread_alt, $overridden_cpage, $in_comment_loop;
     1232
     1233    $in_comment_loop = true;
    12131234
    12141235    $comment_alt = $comment_thread_alt = 0;
     
    12861307    $walker->paged_walk($_comments, $max_depth, $page, $per_page, $r);
    12871308    $wp_query->max_num_comment_pages = $walker->max_pages;
     1309
     1310    $in_comment_loop = false;
    12881311}
    12891312
  • trunk/wp-includes/comment.php

    r9782 r9808  
    549549 *
    550550 * @param int $comment_ID Comment ID.
    551  * @param int $per_page Optional comments per page.
     551 * @param array $args Optional args.
    552552 * @return int|null Comment page number or null on error.
    553553 */
    554 function get_page_of_comment( $comment_ID, $per_page = null, $threaded = null ) {
     554function get_page_of_comment( $comment_ID, $args = array() ) {
    555555    global $wpdb;
    556556
     
    558558        return;
    559559
    560     if ( !get_option('page_comments') )
     560    $defaults = array( 'type' => 'all', 'page' => '', 'per_page' => '', 'max_depth' => '' );
     561    $args = wp_parse_args( $args, $defaults );
     562
     563    if ( '' === $args['per_page'] && get_option('page_comments') )
     564        $args['per_page'] = get_query_var('comments_per_page');
     565    if ( empty($args['per_page']) ) {
     566        $args['per_page'] = 0;
     567        $args['page'] = 0;
     568    }
     569    if ( $args['per_page'] < 1 )
    561570        return 1;
    562571
    563     if ( null === $per_page )
    564         $per_page = get_option('comments_per_page');
    565 
    566     if ( null === $threaded )
    567         $threaded = get_option('thread_comments');
     572    if ( '' === $args['max_depth'] ) {
     573        if ( get_option('thread_comments') )
     574            $args['max_depth'] = get_option('thread_comments_depth');
     575        else
     576            $args['max_depth'] = -1;
     577    }
    568578
    569579    // Find this comment's top level parent if threading is enabled
    570     if ( $threaded && 0 != $comment->comment_parent )
    571         return get_page_of_comment( $comment->comment_parent, $per_page, $threaded );
     580    if ( $args['max_depth'] > 1 && 0 != $comment->comment_parent )
     581        return get_page_of_comment( $comment->comment_parent, $args );
     582
     583    $allowedtypes = array(
     584        'comment' => '',
     585        'pingback' => 'pingback',
     586        'trackback' => 'trackback',
     587    );
     588
     589    $comtypewhere = ( 'all' != $args['type'] && isset($allowedtypes[$args['type']]) ) ? " AND comment_type = '" . $allowedtypes[$args['type']] . "'" : '';
    572590
    573591    // Count comments older than this one
    574     $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 ) );
     592    $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 ) );
    575593
    576594    // No older comments? Then it's page #1.
     
    579597
    580598    // Divide comments older than this one by comments per page to get this comment's page number
    581     return ceil( ( $oldercoms + 1 ) / $per_page );
     599    return ceil( ( $oldercoms + 1 ) / $args['per_page'] );
    582600}
    583601
Note: See TracChangeset for help on using the changeset viewer.