diff --git a/src/wp-includes/link-template.php b/src/wp-includes/link-template.php index 8ac4a4de45..7caeaa9300 100644 --- a/src/wp-includes/link-template.php +++ b/src/wp-includes/link-template.php @@ -2466,6 +2466,10 @@ function get_next_comments_link( $label = '', $max_page = 0 ) { $page = get_query_var('cpage'); + if ( ! $page ) { + $page = 1; + } + $nextpage = intval($page) + 1; if ( empty($max_page) ) diff --git a/tests/phpunit/tests/link/getNextCommentsLink.php b/tests/phpunit/tests/link/getNextCommentsLink.php new file mode 100644 index 0000000000..c877a56892 --- /dev/null +++ b/tests/phpunit/tests/link/getNextCommentsLink.php @@ -0,0 +1,51 @@ +set_permalink_structure( '' ); + $wp_rewrite->flush_rules(); + } + + public function test_page_should_respect_value_of_cpage_query_var() { + update_option( 'page_comments', '1' ); + $p = $this->factory->post->create(); + $this->go_to( get_permalink( $p ) ); + + $cpage = get_query_var( 'cpage' ); + set_query_var( 'cpage', 3 ); + + $link = get_next_comments_link( 'Next', 5 ); + + $this->assertContains( 'cpage=4', $link ); + + set_query_var( 'cpage', $cpage ); + } + + /** + * @ticket 20319 + */ + public function test_page_should_default_to_1_when_no_cpage_query_var_is_found() { + update_option( 'page_comments', '1' ); + $p = $this->factory->post->create(); + $this->go_to( get_permalink( $p ) ); + + $cpage = get_query_var( 'cpage' ); + set_query_var( 'cpage', '' ); + + $link = get_next_comments_link( 'Next', 5 ); + + $this->assertContains( 'cpage=2', $link ); + + set_query_var( 'cpage', $cpage ); + } + +} diff --git a/tests/phpunit/tests/link/getPreviousCommentsLink.php b/tests/phpunit/tests/link/getPreviousCommentsLink.php new file mode 100644 index 0000000000..10741055f9 --- /dev/null +++ b/tests/phpunit/tests/link/getPreviousCommentsLink.php @@ -0,0 +1,49 @@ +set_permalink_structure( '' ); + $wp_rewrite->flush_rules(); + } + + public function test_page_should_respect_value_of_cpage_query_var() { + update_option( 'page_comments', '1' ); + $p = $this->factory->post->create(); + $this->go_to( get_permalink( $p ) ); + + $cpage = get_query_var( 'cpage' ); + set_query_var( 'cpage', 3 ); + + $link = get_previous_comments_link( 'Next' ); + + $this->assertContains( 'cpage=2', $link ); + + set_query_var( 'cpage', $cpage ); + } + + public function test_page_should_default_to_1_when_no_cpage_query_var_is_found() { + update_option( 'page_comments', '1' ); + $p = $this->factory->post->create(); + $this->go_to( get_permalink( $p ) ); + + $cpage = get_query_var( 'cpage' ); + set_query_var( 'cpage', '' ); + + $link = get_previous_comments_link( 'Next', 5 ); + + // Technically, it returns null here. + $this->assertEquals( '', $link ); + + set_query_var( 'cpage', $cpage ); + } + +}