Make WordPress Core

Changeset 36356 for branches/4.4


Ignore:
Timestamp:
01/20/2016 05:23:02 AM (11 years ago)
Author:
dd32
Message:

Comments: Always respect $comments array passed to wp_list_comments().

[36157] fixed a bug whereby wp_list_comments() would not properly recognize
custom pagination arguments. See #35175. However, it inadvertently introduced
a bug that caused any $comments array explicitly passed to the function to be
ignored, when that array was accompanied by pagination arguments that differ
from those in $wp_query. We address this bug by moving the logic introduced
in [36157] inside a block that only fires when no $comments array has been
provided to the function.

Merges [36276] to the 4.4 branch.
Props ivankristianto, boonebgorges.
Fixes #35356.

Location:
branches/4.4
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/4.4

  • branches/4.4/src/wp-includes/comment-template.php

    r36353 r36356  
    19351935        $r = apply_filters( 'wp_list_comments_args', $r );
    19361936
    1937         /*
    1938          * If 'page' or 'per_page' has been passed, and does not match what's in $wp_query,
    1939          * perform a separate comment query and allow Walker_Comment to paginate.
    1940          */
    1941         if ( is_singular() && ( $r['page'] || $r['per_page'] ) ) {
    1942                 $current_cpage = get_query_var( 'cpage' );
    1943                 if ( ! $current_cpage ) {
    1944                         $current_cpage = 'newest' === get_option( 'default_comments_page' ) ? 1 : $wp_query->max_num_comment_pages;
    1945                 }
    1946 
    1947                 $current_per_page = get_query_var( 'comments_per_page' );
    1948                 if ( $r['page'] != $current_cpage || $r['per_page'] != $current_per_page ) {
    1949                         $comments = get_comments( array(
    1950                                 'post_id' => get_queried_object_id(),
    1951                                 'orderby' => 'comment_date_gmt',
    1952                                 'order' => 'ASC',
    1953                                 'status' => 'all',
    1954                         ) );
    1955                 }
    1956         }
    1957 
    19581937        // Figure out what comments we'll be looping through ($_comments)
    19591938        if ( null !== $comments ) {
     
    19701949                }
    19711950        } else {
    1972                 if ( empty($wp_query->comments) )
    1973                         return;
    1974                 if ( 'all' != $r['type'] ) {
    1975                         if ( empty($wp_query->comments_by_type) )
    1976                                 $wp_query->comments_by_type = separate_comments($wp_query->comments);
    1977                         if ( empty($wp_query->comments_by_type[$r['type']]) )
     1951                /*
     1952                 * If 'page' or 'per_page' has been passed, and does not match what's in $wp_query,
     1953                 * perform a separate comment query and allow Walker_Comment to paginate.
     1954                 */
     1955                if ( is_singular() && ( $r['page'] || $r['per_page'] ) ) {
     1956                        $current_cpage = get_query_var( 'cpage' );
     1957                        if ( ! $current_cpage ) {
     1958                                $current_cpage = 'newest' === get_option( 'default_comments_page' ) ? 1 : $wp_query->max_num_comment_pages;
     1959                        }
     1960
     1961                        $current_per_page = get_query_var( 'comments_per_page' );
     1962                        if ( $r['page'] != $current_cpage || $r['per_page'] != $current_per_page ) {
     1963                                $comments = get_comments( array(
     1964                                        'post_id' => get_queried_object_id(),
     1965                                        'orderby' => 'comment_date_gmt',
     1966                                        'order' => 'ASC',
     1967                                        'status' => 'all',
     1968                                ) );
     1969
     1970                                if ( 'all' != $r['type'] ) {
     1971                                        $comments_by_type = separate_comments( $comments );
     1972                                        if ( empty( $comments_by_type[ $r['type'] ] ) ) {
     1973                                                return;
     1974                                        }
     1975
     1976                                        $_comments = $comments_by_type[ $r['type'] ];
     1977                                } else {
     1978                                        $_comments = $comments;
     1979                                }
     1980                        }
     1981
     1982                // Otherwise, fall back on the comments from `$wp_query->comments`.
     1983                } else {
     1984                        if ( empty($wp_query->comments) )
    19781985                                return;
    1979                         $_comments = $wp_query->comments_by_type[$r['type']];
    1980                 } else {
    1981                         $_comments = $wp_query->comments;
    1982                 }
    1983 
    1984                 // Pagination is already handled by `WP_Comment_Query`, so we tell Walker not to bother.
    1985                 if ( $wp_query->max_num_comment_pages ) {
    1986                         $default_comments_page = get_option( 'default_comments_page' );
    1987                         $cpage = get_query_var( 'cpage' );
    1988                         if ( 'newest' === $default_comments_page ) {
    1989                                 $r['cpage'] = $cpage;
    1990 
    1991                         // When first page shows oldest comments, post permalink is the same as the comment permalink.
    1992                         } elseif ( $cpage == 1 ) {
    1993                                 $r['cpage'] = '';
     1986                        if ( 'all' != $r['type'] ) {
     1987                                if ( empty($wp_query->comments_by_type) )
     1988                                        $wp_query->comments_by_type = separate_comments($wp_query->comments);
     1989                                if ( empty($wp_query->comments_by_type[$r['type']]) )
     1990                                        return;
     1991                                $_comments = $wp_query->comments_by_type[$r['type']];
    19941992                        } else {
    1995                                 $r['cpage'] = $cpage;
     1993                                $_comments = $wp_query->comments;
    19961994                        }
    19971995
    1998                         $r['page'] = 0;
    1999                         $r['per_page'] = 0;
     1996                        if ( $wp_query->max_num_comment_pages ) {
     1997                                $default_comments_page = get_option( 'default_comments_page' );
     1998                                $cpage = get_query_var( 'cpage' );
     1999                                if ( 'newest' === $default_comments_page ) {
     2000                                        $r['cpage'] = $cpage;
     2001
     2002                                /*
     2003                                 * When first page shows oldest comments, post permalink is the same as
     2004                                 * the comment permalink.
     2005                                 */
     2006                                } elseif ( $cpage == 1 ) {
     2007                                        $r['cpage'] = '';
     2008                                } else {
     2009                                        $r['cpage'] = $cpage;
     2010                                }
     2011
     2012                                $r['page'] = 0;
     2013                                $r['per_page'] = 0;
     2014                        }
    20002015                }
    20012016        }
  • branches/4.4/tests/phpunit/tests/comment/wpListComments.php

    r36158 r36356  
    111111                $this->assertSame( array( $comments[1], $comments[0] ), array_map( 'intval', $matches[1] ) );
    112112        }
     113
     114        /**
     115         * @ticket 35356
     116         * @ticket 35175
     117         */
     118        public function test_comments_param_should_be_respected_when_custom_pagination_params_are_passed() {
     119                $p = self::factory()->post->create();
     120
     121                $comments = array();
     122                $now = time();
     123                for ( $i = 0; $i <= 5; $i++ ) {
     124                        $comments[] = self::factory()->comment->create( array(
     125                                'comment_post_ID' => $p,
     126                                'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - $i ),
     127                                'comment_author' => 'Commenter ' . $i,
     128                        ) );
     129                }
     130
     131                update_option( 'page_comments', true );
     132                update_option( 'comments_per_page', 2 );
     133
     134                $_comments = array( get_comment( $comments[1] ), get_comment( $comments[3] ) );
     135
     136                // Populate `$wp_query->comments` in order to show that it doesn't override `$_comments`.
     137                $this->go_to( get_permalink( $p ) );
     138                get_echo( 'comments_template' );
     139
     140                $found = wp_list_comments( array(
     141                        'echo' => false,
     142                        'per_page' => 1,
     143                        'page' => 2,
     144                ), $_comments );
     145
     146                preg_match_all( '|id="comment\-([0-9]+)"|', $found, $matches );
     147                $this->assertSame( array( $comments[3] ), array_map( 'intval', $matches[1] ) );
     148        }
    113149}
Note: See TracChangeset for help on using the changeset viewer.