From 3b89b3ca2825d0f565adb355e5f2d60eecec6f98 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Sat, 3 Oct 2015 19:25:25 +0000 Subject: [PATCH] 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, 2. 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. git-svn-id: https://develop.svn.wordpress.org/trunk@34806 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/comment-functions.php | 11 +++++++++-- tests/phpunit/tests/comment/getPageOfComment.php | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/comment-functions.php b/src/wp-includes/comment-functions.php index ce6c2d0e82..280e15035e 100644 --- a/src/wp-includes/comment-functions.php +++ b/src/wp-includes/comment-functions.php @@ -855,8 +855,15 @@ function get_page_of_comment( $comment_ID, $args = array() ) { $defaults = array( 'type' => 'all', 'page' => '', 'per_page' => '', 'max_depth' => '' ); $args = wp_parse_args( $args, $defaults ); - if ( '' === $args['per_page'] ) - $args['per_page'] = get_query_var('comments_per_page'); + // Order of precedence: 1. `$args['per_page']`, 2. 'comments_per_page' query_var, 3. 'comments_per_page' option. + if ( '' === $args['per_page'] ) { + $args['per_page'] = get_query_var( 'comments_per_page' ); + } + + if ( '' === $args['per_page'] ) { + $args['per_page'] = get_option( 'comments_per_page' ); + } + if ( empty($args['per_page']) ) { $args['per_page'] = 0; $args['page'] = 0; diff --git a/tests/phpunit/tests/comment/getPageOfComment.php b/tests/phpunit/tests/comment/getPageOfComment.php index b61a6a62af..78151d24f0 100644 --- a/tests/phpunit/tests/comment/getPageOfComment.php +++ b/tests/phpunit/tests/comment/getPageOfComment.php @@ -226,4 +226,20 @@ class Tests_Comment_GetPageOfComment extends WP_UnitTestCase { $this->assertSame( 2, (int) get_page_of_comment( $comment_children[ $p2i ], $args ) ); } } + + /** + * @ticket 13939 + */ + public function test_comments_per_page_option_should_be_fallback_when_query_var_is_not_available() { + $now = time(); + + $p = $this->factory->post->create(); + $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ) ) ); + $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 20 ) ) ); + $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 30 ) ) ); + + update_option( 'comments_per_page', 2 ); + + $this->assertEquals( 2, get_page_of_comment( $c1 ) ); + } }