Make WordPress Core


Ignore:
Timestamp:
10/01/2015 05:12:39 AM (9 years ago)
Author:
boonebgorges
Message:

Ensure that comment permalinks reflect pagination.

After [34561], wp_list_comments() no longer passed all of a post's comments
to Walker_Comments. As a result, calls to get_comment_link() occurring
inside the comment loop had insufficient context to determine the proper
'cpage' value to use when generating comment permalinks. This, in turn, caused
comment permalinks to behave erratically.

The current changeset addresses the problem as follows:

  • get_comment_link() now accepts a 'cpage' parameter. When present, 'cpage' will be used to build the comment permalink - no automatic calculation will take place.
  • When called within the main loop, wp_list_comments() calculates the proper 'cpage' value for comments in the loop, and passes it down to get_comment_link().
  • cpage and comment-page-x query vars are generally required in comment permalinks (see #34068), but an exception is made when 'default_comment_page=oldest': the bare post permalink will always be the same as cpage=1, so cpage is excluded in this case.

Props peterwilsoncc for assiduous spreadsheeting.
Fixes #34073.

File:
1 edited

Legend:

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

    r34729 r34735  
    660660 *
    661661 * @since 1.5.0
    662  * @since 4.4.0 Added the ability for `$comment` to also accept a WP_Comment object.
     662 * @since 4.4.0 Added the ability for `$comment` to also accept a WP_Comment object. Added `$cpage` argument.
    663663 *
    664664 * @see get_page_of_comment()
     
    671671 *     An array of optional arguments to override the defaults.
    672672 *
    673  *     @type string $type      Passed to {@see get_page_of_comment()}.
    674  *     @type int    $page      Current page of comments, for calculating comment pagination.
    675  *     @type int    $per_page  Per-page value for comment pagination.
    676  *     @type int    $max_depth Passed to {@see get_page_of_comment()}.
     673 *     @type string     $type      Passed to {@see get_page_of_comment()}.
     674 *     @type int        $page      Current page of comments, for calculating comment pagination.
     675 *     @type int        $per_page  Per-page value for comment pagination.
     676 *     @type int        $max_depth Passed to {@see get_page_of_comment()}.
     677 *     @type int|string $cpage     Value to use for the comment's "comment-page" or "cpage" value. If provided, this
     678 *                                 value overrides any value calculated from `$page` and `$per_page`.
    677679 * }
    678680 * @return string The permalink to the given comment.
     
    688690    }
    689691
    690     $defaults = array( 'type' => 'all', 'page' => '', 'per_page' => '', 'max_depth' => '' );
     692    $defaults = array(
     693        'type'      => 'all',
     694        'page'      => '',
     695        'per_page'  => '',
     696        'max_depth' => '',
     697        'cpage'     => null,
     698    );
    691699    $args = wp_parse_args( $args, $defaults );
    692700
    693     if ( '' === $args['per_page'] )
    694         $args['per_page'] = get_option('comments_per_page');
    695 
    696     if ( empty($args['per_page']) ) {
    697         $args['per_page'] = 0;
    698         $args['page'] = 0;
    699     }
    700 
    701     if ( $args['per_page'] ) {
    702         if ( '' == $args['page'] )
    703             $args['page'] = ( !empty($in_comment_loop) ) ? get_query_var('cpage') : get_page_of_comment( $comment->comment_ID, $args );
    704 
    705         if ( $wp_rewrite->using_permalinks() )
    706             $link = user_trailingslashit( trailingslashit( get_permalink( $comment->comment_post_ID ) ) . $wp_rewrite->comments_pagination_base . '-' . $args['page'], 'comment' );
    707         else
    708             $link = add_query_arg( 'cpage', $args['page'], get_permalink( $comment->comment_post_ID ) );
     701    $link = get_permalink( $comment->comment_post_ID );
     702
     703    // The 'cpage' param takes precedence.
     704    if ( ! is_null( $args['cpage'] ) ) {
     705        $cpage = $args['cpage'];
     706
     707    // No 'cpage' is provided, so we calculate one.
    709708    } else {
    710         $link = get_permalink( $comment->comment_post_ID );
     709        if ( '' === $args['per_page'] ) {
     710            $args['per_page'] = get_option('comments_per_page');
     711        }
     712
     713        if ( empty( $args['per_page'] ) ) {
     714            $args['per_page'] = 0;
     715            $args['page'] = 0;
     716        }
     717
     718        $cpage = $args['page'];
     719
     720        if ( '' == $cpage ) {
     721            if ( ! empty( $in_comment_loop ) ) {
     722                $cpage = get_query_var( 'cpage' );
     723            } else {
     724                // Requires a database hit, so we only do it when we can't figure out from context.
     725                $cpage = get_page_of_comment( $comment->comment_ID, $args );
     726            }
     727        }
     728
     729        // Drop the 'page' var if we're on the default page.
     730        $comment_post = get_post( $comment->comment_post_ID );
     731        if ( $args['per_page'] ) {
     732            $total_pages = ceil( $comment_post->comment_count / $args['per_page'] );
     733        } else {
     734            $total_pages = 1;
     735        }
     736
     737        /*
     738         * If the default page displays the oldest comments, the permalinks for comments on the default page
     739         * do not need a 'cpage' query var.
     740         */
     741        $default_comments_page = get_option( 'default_comments_page' );
     742        if ( 'oldest' === get_option( 'default_comments_page' ) && 1 === $cpage ) {
     743            $cpage = '';
     744        }
     745    }
     746
     747    if ( $cpage ) {
     748        if ( $wp_rewrite->using_permalinks() ) {
     749            if ( $cpage ) {
     750                $link = trailingslashit( $link ) . $wp_rewrite->comments_pagination_base . '-' . $cpage;
     751            }
     752
     753            $link = user_trailingslashit( $link, 'comment' );
     754        } elseif ( $cpage ) {
     755            $link = add_query_arg( 'cpage', $cpage, $link );
     756        }
     757
     758    }
     759
     760    if ( $wp_rewrite->using_permalinks() ) {
     761        $link = user_trailingslashit( $link, 'comment' );
    711762    }
    712763
    713764    $link = $link . '#comment-' . $comment->comment_ID;
     765
    714766    /**
    715767     * Filter the returned single comment permalink.
    716768     *
    717769     * @since 2.8.0
     770     * @since 4.4.0 Added the `$cpage` parameter.
    718771     *
    719772     * @see get_page_of_comment()
     
    722775     * @param WP_Comment $comment The current comment object.
    723776     * @param array      $args    An array of arguments to override the defaults.
    724      */
    725     return apply_filters( 'get_comment_link', $link, $comment, $args );
     777     * @param int        $cpage   The calculated 'cpage' value.
     778     */
     779    return apply_filters( 'get_comment_link', $link, $comment, $args, $cpage );
    726780}
    727781
     
    18781932
    18791933        // Pagination is already handled by `WP_Comment_Query`, so we tell Walker not to bother.
    1880         if ( 1 < $wp_query->max_num_comment_pages ) {
    1881             $r['page'] = 1;
     1934        if ( $wp_query->max_num_comment_pages ) {
     1935            $default_comments_page = get_option( 'default_comments_page' );
     1936            $cpage = get_query_var( 'cpage' );
     1937            if ( 'newest' === $default_comments_page ) {
     1938                $r['cpage'] = $cpage;
     1939
     1940            // When first page shows oldest comments, post permalink is the same as the comment permalink.
     1941            } elseif ( $cpage == 1 ) {
     1942                $r['cpage'] = '';
     1943            } else {
     1944                $r['cpage'] = $cpage;
     1945            }
     1946
     1947            $r['page'] = 0;
     1948            $r['per_page'] = 0;
    18821949        }
    18831950    }
Note: See TracChangeset for help on using the changeset viewer.