WordPress.org

Make WordPress Core

Changeset 34806


Ignore:
Timestamp:
10/03/2015 07:25:25 PM (4 years ago)
Author:
boonebgorges
Message:

Use 'comments_per_page' option as fallback in get_page_of_comment().

The function now uses the following order of precedence when calculating
comment pagination: 1. the 'per_page' value passed in the $args array,

  1. the 'comments_per_page' query var in $wp_query, and 3. the

'comments_per_page' setting from options-discussion.php. This change allows
get_page_of_comment() to return an accurate value before the main query
has been run.

Props laceous.
See #13939.

Location:
trunk
Files:
2 edited

Legend:

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

    r34805 r34806  
    856856    $args = wp_parse_args( $args, $defaults );
    857857
    858     if ( '' === $args['per_page'] )
    859         $args['per_page'] = get_query_var('comments_per_page');
     858    // Order of precedence: 1. `$args['per_page']`, 2. 'comments_per_page' query_var, 3. 'comments_per_page' option.
     859    if ( '' === $args['per_page'] ) {
     860        $args['per_page'] = get_query_var( 'comments_per_page' );
     861    }
     862
     863    if ( '' === $args['per_page'] ) {
     864        $args['per_page'] = get_option( 'comments_per_page' );
     865    }
     866
    860867    if ( empty($args['per_page']) ) {
    861868        $args['per_page'] = 0;
  • trunk/tests/phpunit/tests/comment/getPageOfComment.php

    r34805 r34806  
    227227        }
    228228    }
     229
     230    /**
     231     * @ticket 13939
     232     */
     233    public function test_comments_per_page_option_should_be_fallback_when_query_var_is_not_available() {
     234        $now = time();
     235
     236        $p = $this->factory->post->create();
     237        $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ) ) );
     238        $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 20 ) ) );
     239        $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 30 ) ) );
     240
     241        update_option( 'comments_per_page', 2 );
     242
     243        $this->assertEquals( 2, get_page_of_comment( $c1 ) );
     244    }
    229245}
Note: See TracChangeset for help on using the changeset viewer.