WordPress.org

Make WordPress Core

Changeset 36276


Ignore:
Timestamp:
01/13/2016 03:26:31 AM (4 years ago)
Author:
boonebgorges
Message:

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.

Props ivankristianto.
Fixes #35356.

Location:
trunk
Files:
2 edited

Legend:

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

    r36275 r36276  
    19111911    $r = apply_filters( 'wp_list_comments_args', $r );
    19121912
    1913     /*
    1914      * If 'page' or 'per_page' has been passed, and does not match what's in $wp_query,
    1915      * perform a separate comment query and allow Walker_Comment to paginate.
    1916      */
    1917     if ( is_singular() && ( $r['page'] || $r['per_page'] ) ) {
    1918         $current_cpage = get_query_var( 'cpage' );
    1919         if ( ! $current_cpage ) {
    1920             $current_cpage = 'newest' === get_option( 'default_comments_page' ) ? 1 : $wp_query->max_num_comment_pages;
    1921         }
    1922 
    1923         $current_per_page = get_query_var( 'comments_per_page' );
    1924         if ( $r['page'] != $current_cpage || $r['per_page'] != $current_per_page ) {
    1925             $comments = get_comments( array(
    1926                 'post_id' => get_queried_object_id(),
    1927                 'orderby' => 'comment_date_gmt',
    1928                 'order' => 'ASC',
    1929                 'status' => 'all',
    1930             ) );
    1931         }
    1932     }
    1933 
    19341913    // Figure out what comments we'll be looping through ($_comments)
    19351914    if ( null !== $comments ) {
     
    19461925        }
    19471926    } else {
    1948         if ( empty($wp_query->comments) )
    1949             return;
    1950         if ( 'all' != $r['type'] ) {
    1951             if ( empty($wp_query->comments_by_type) )
    1952                 $wp_query->comments_by_type = separate_comments($wp_query->comments);
    1953             if ( empty($wp_query->comments_by_type[$r['type']]) )
     1927        /*
     1928         * If 'page' or 'per_page' has been passed, and does not match what's in $wp_query,
     1929         * perform a separate comment query and allow Walker_Comment to paginate.
     1930         */
     1931        if ( is_singular() && ( $r['page'] || $r['per_page'] ) ) {
     1932            $current_cpage = get_query_var( 'cpage' );
     1933            if ( ! $current_cpage ) {
     1934                $current_cpage = 'newest' === get_option( 'default_comments_page' ) ? 1 : $wp_query->max_num_comment_pages;
     1935            }
     1936
     1937            $current_per_page = get_query_var( 'comments_per_page' );
     1938            if ( $r['page'] != $current_cpage || $r['per_page'] != $current_per_page ) {
     1939                $comments = get_comments( array(
     1940                    'post_id' => get_queried_object_id(),
     1941                    'orderby' => 'comment_date_gmt',
     1942                    'order' => 'ASC',
     1943                    'status' => 'all',
     1944                ) );
     1945
     1946                if ( 'all' != $r['type'] ) {
     1947                    $comments_by_type = separate_comments( $comments );
     1948                    if ( empty( $comments_by_type[ $r['type'] ] ) ) {
     1949                        return;
     1950                    }
     1951
     1952                    $_comments = $comments_by_type[ $r['type'] ];
     1953                } else {
     1954                    $_comments = $comments;
     1955                }
     1956            }
     1957
     1958        // Otherwise, fall back on the comments from `$wp_query->comments`.
     1959        } else {
     1960            if ( empty($wp_query->comments) )
    19541961                return;
    1955             $_comments = $wp_query->comments_by_type[$r['type']];
    1956         } else {
    1957             $_comments = $wp_query->comments;
    1958         }
    1959 
    1960         // Pagination is already handled by `WP_Comment_Query`, so we tell Walker not to bother.
    1961         if ( $wp_query->max_num_comment_pages ) {
    1962             $default_comments_page = get_option( 'default_comments_page' );
    1963             $cpage = get_query_var( 'cpage' );
    1964             if ( 'newest' === $default_comments_page ) {
    1965                 $r['cpage'] = $cpage;
    1966 
    1967             // When first page shows oldest comments, post permalink is the same as the comment permalink.
    1968             } elseif ( $cpage == 1 ) {
    1969                 $r['cpage'] = '';
     1962            if ( 'all' != $r['type'] ) {
     1963                if ( empty($wp_query->comments_by_type) )
     1964                    $wp_query->comments_by_type = separate_comments($wp_query->comments);
     1965                if ( empty($wp_query->comments_by_type[$r['type']]) )
     1966                    return;
     1967                $_comments = $wp_query->comments_by_type[$r['type']];
    19701968            } else {
    1971                 $r['cpage'] = $cpage;
     1969                $_comments = $wp_query->comments;
    19721970            }
    19731971
    1974             $r['page'] = 0;
    1975             $r['per_page'] = 0;
     1972            if ( $wp_query->max_num_comment_pages ) {
     1973                $default_comments_page = get_option( 'default_comments_page' );
     1974                $cpage = get_query_var( 'cpage' );
     1975                if ( 'newest' === $default_comments_page ) {
     1976                    $r['cpage'] = $cpage;
     1977
     1978                /*
     1979                 * When first page shows oldest comments, post permalink is the same as
     1980                 * the comment permalink.
     1981                 */
     1982                } elseif ( $cpage == 1 ) {
     1983                    $r['cpage'] = '';
     1984                } else {
     1985                    $r['cpage'] = $cpage;
     1986                }
     1987
     1988                $r['page'] = 0;
     1989                $r['per_page'] = 0;
     1990            }
    19761991        }
    19771992    }
  • trunk/tests/phpunit/tests/comment/wpListComments.php

    r36157 r36276  
    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.