Make WordPress Core

Ticket #35356: 35356.diff

File 35356.diff, 6.2 KB (added by boonebgorges, 9 years ago)
  • src/wp-includes/comment-template.php

    diff --git src/wp-includes/comment-template.php src/wp-includes/comment-template.php
    index e1dbd24..f1ab58d 100644
    function wp_comment_form_unfiltered_html_nonce() { 
    12421242 * @global int        $user_ID
    12431243 * @global string     $user_identity
    12441244 * @global bool       $overridden_cpage
    1245  * @global bool       $withcomments 
     1245 * @global bool       $withcomments
    12461246 *
    12471247 * @param string $file              Optional. The file to load. Default '/comments.php'.
    12481248 * @param bool   $separate_comments Optional. Whether to separate the comments by comment type.
    function wp_list_comments( $args = array(), $comments = null ) { 
    19071907         */
    19081908        $r = apply_filters( 'wp_list_comments_args', $r );
    19091909
    1910         /*
    1911          * If 'page' or 'per_page' has been passed, and does not match what's in $wp_query,
    1912          * perform a separate comment query and allow Walker_Comment to paginate.
    1913          */
    1914         if ( is_singular() && ( $r['page'] || $r['per_page'] ) ) {
    1915                 $current_cpage = get_query_var( 'cpage' );
    1916                 if ( ! $current_cpage ) {
    1917                         $current_cpage = 'newest' === get_option( 'default_comments_page' ) ? 1 : $wp_query->max_num_comment_pages;
    1918                 }
    1919 
    1920                 $current_per_page = get_query_var( 'comments_per_page' );
    1921                 if ( $r['page'] != $current_cpage || $r['per_page'] != $current_per_page ) {
    1922                         $comments = get_comments( array(
    1923                                 'post_id' => get_queried_object_id(),
    1924                                 'orderby' => 'comment_date_gmt',
    1925                                 'order' => 'ASC',
    1926                                 'status' => 'all',
    1927                         ) );
    1928                 }
    1929         }
    1930 
    19311910        // Figure out what comments we'll be looping through ($_comments)
    19321911        if ( null !== $comments ) {
    19331912                $comments = (array) $comments;
    function wp_list_comments( $args = array(), $comments = null ) { 
    19421921                        $_comments = $comments;
    19431922                }
    19441923        } else {
    1945                 if ( empty($wp_query->comments) )
    1946                         return;
    1947                 if ( 'all' != $r['type'] ) {
    1948                         if ( empty($wp_query->comments_by_type) )
    1949                                 $wp_query->comments_by_type = separate_comments($wp_query->comments);
    1950                         if ( empty($wp_query->comments_by_type[$r['type']]) )
    1951                                 return;
    1952                         $_comments = $wp_query->comments_by_type[$r['type']];
    1953                 } else {
    1954                         $_comments = $wp_query->comments;
    1955                 }
     1924                /*
     1925                 * If 'page' or 'per_page' has been passed, and does not match what's in $wp_query,
     1926                 * perform a separate comment query and allow Walker_Comment to paginate.
     1927                 */
     1928                if ( is_singular() && ( $r['page'] || $r['per_page'] ) ) {
     1929                        $current_cpage = get_query_var( 'cpage' );
     1930                        if ( ! $current_cpage ) {
     1931                                $current_cpage = 'newest' === get_option( 'default_comments_page' ) ? 1 : $wp_query->max_num_comment_pages;
     1932                        }
    19561933
    1957                 // Pagination is already handled by `WP_Comment_Query`, so we tell Walker not to bother.
    1958                 if ( $wp_query->max_num_comment_pages ) {
    1959                         $default_comments_page = get_option( 'default_comments_page' );
    1960                         $cpage = get_query_var( 'cpage' );
    1961                         if ( 'newest' === $default_comments_page ) {
    1962                                 $r['cpage'] = $cpage;
     1934                        $current_per_page = get_query_var( 'comments_per_page' );
     1935                        if ( $r['page'] != $current_cpage || $r['per_page'] != $current_per_page ) {
     1936                                $comments = get_comments( array(
     1937                                        'post_id' => get_queried_object_id(),
     1938                                        'orderby' => 'comment_date_gmt',
     1939                                        'order' => 'ASC',
     1940                                        'status' => 'all',
     1941                                ) );
     1942
     1943                                if ( 'all' != $r['type'] ) {
     1944                                        $comments_by_type = separate_comments( $comments );
     1945                                        if ( empty( $comments_by_type[ $r['type'] ] ) ) {
     1946                                                return;
     1947                                        }
     1948
     1949                                        $_comments = $comments_by_type[ $r['type'] ];
     1950                                } else {
     1951                                        $_comments = $comments;
     1952                                }
     1953                        }
    19631954
    1964                         // When first page shows oldest comments, post permalink is the same as the comment permalink.
    1965                         } elseif ( $cpage == 1 ) {
    1966                                 $r['cpage'] = '';
     1955                // Otherwise, fall back on the comments from `$wp_query->comments`.
     1956                } else {
     1957                        if ( empty($wp_query->comments) )
     1958                                return;
     1959                        if ( 'all' != $r['type'] ) {
     1960                                if ( empty($wp_query->comments_by_type) )
     1961                                        $wp_query->comments_by_type = separate_comments($wp_query->comments);
     1962                                if ( empty($wp_query->comments_by_type[$r['type']]) )
     1963                                        return;
     1964                                $_comments = $wp_query->comments_by_type[$r['type']];
    19671965                        } else {
    1968                                 $r['cpage'] = $cpage;
     1966                                $_comments = $wp_query->comments;
    19691967                        }
    19701968
    1971                         $r['page'] = 0;
    1972                         $r['per_page'] = 0;
     1969                        if ( $wp_query->max_num_comment_pages ) {
     1970                                $default_comments_page = get_option( 'default_comments_page' );
     1971                                $cpage = get_query_var( 'cpage' );
     1972                                if ( 'newest' === $default_comments_page ) {
     1973                                        $r['cpage'] = $cpage;
     1974
     1975                                /*
     1976                                 * When first page shows oldest comments, post permalink is the same as
     1977                                 * the comment permalink.
     1978                                 */
     1979                                } elseif ( $cpage == 1 ) {
     1980                                        $r['cpage'] = '';
     1981                                } else {
     1982                                        $r['cpage'] = $cpage;
     1983                                }
     1984
     1985                                $r['page'] = 0;
     1986                                $r['per_page'] = 0;
     1987                        }
    19731988                }
    19741989        }
    19751990
  • tests/phpunit/tests/comment/wpListComments.php

    diff --git tests/phpunit/tests/comment/wpListComments.php tests/phpunit/tests/comment/wpListComments.php
    index 84f6e4c..d8214f4 100644
    class Tests_Comment_WpListComments extends WP_UnitTestCase { 
    110110                preg_match_all( '|id="comment\-([0-9]+)"|', $found2, $matches );
    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}