From 33275a0826d33a267e02af0c94cc288a9c28f2e8 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Fri, 20 Jun 2014 17:11:14 +0000 Subject: [PATCH] Cleanup after [28671]: * Set better defaults in `paginate_links()`, so that themes don't have to calculate them on their own, like Twenty Fourteen does now. * Don't set page 1 to `?page=1` or `/page/1/` - that will force a canonical redirect. * Add and cleanup unit tests Props obenland, SergeyBiryukov, wonderboymusic. Fixes #24606. git-svn-id: https://develop.svn.wordpress.org/trunk@28785 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/general-template.php | 36 +++++++--- tests/phpunit/tests/general/paginateLinks.php | 66 ++++++++++++++----- 2 files changed, 76 insertions(+), 26 deletions(-) diff --git a/src/wp-includes/general-template.php b/src/wp-includes/general-template.php index 34f7be10eb..0df696a479 100644 --- a/src/wp-includes/general-template.php +++ b/src/wp-includes/general-template.php @@ -2438,11 +2438,29 @@ function language_attributes($doctype = 'html') { * @return array|string String of page links or array of page links. */ function paginate_links( $args = '' ) { + global $wp_query, $wp_rewrite; + + $total = ( isset( $wp_query->max_num_pages ) ) ? $wp_query->max_num_pages : 1; + $current = ( get_query_var( 'paged' ) ) ? intval( get_query_var( 'paged' ) ) : 1; + $pagenum_link = html_entity_decode( get_pagenum_link() ); + $query_args = array(); + $url_parts = explode( '?', $pagenum_link ); + + if ( isset( $url_parts[1] ) ) { + wp_parse_str( $url_parts[1], $query_args ); + } + + $pagenum_link = remove_query_arg( array_keys( $query_args ), $pagenum_link ); + $pagenum_link = trailingslashit( $pagenum_link ) . '%_%'; + + $format = $wp_rewrite->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : ''; + $format .= $wp_rewrite->using_permalinks() ? user_trailingslashit( $wp_rewrite->pagination_base . '/%#%', 'paged' ) : '?paged=%#%'; + $defaults = array( - 'base' => '%_%', // http://example.com/all_posts.php%_% : %_% is replaced by format (below) - 'format' => '?page=%#%', // ?page=%#% : %#% is replaced by the page number - 'total' => 1, - 'current' => 0, + 'base' => $pagenum_link, // http://example.com/all_posts.php%_% : %_% is replaced by format (below) + 'format' => $format, // ?page=%#% : %#% is replaced by the page number + 'total' => $total, + 'current' => $current, 'show_all' => false, 'prev_next' => true, 'prev_text' => __('« Previous'), @@ -2476,10 +2494,10 @@ function paginate_links( $args = '' ) { $r = ''; $page_links = array(); $dots = false; - $base = str_replace( '%_%', $args['format'], $args['base'] ); if ( $args['prev_next'] && $current && 1 < $current ) : - $link = str_replace( '%#%', $current - 1, $base ); + $link = str_replace( '%_%', 2 == $current ? '' : $args['format'], $args['base'] ); + $link = str_replace( '%#%', $current - 1, $link ); if ( $add_args ) $link = add_query_arg( $add_args, $link ); $link .= $args['add_fragment']; @@ -2499,7 +2517,8 @@ function paginate_links( $args = '' ) { $dots = true; else : if ( $args['show_all'] || ( $n <= $end_size || ( $current && $n >= $current - $mid_size && $n <= $current + $mid_size ) || $n > $total - $end_size ) ) : - $link = str_replace( '%#%', $n, $base ); + $link = str_replace( '%_%', 1 == $n ? '' : $args['format'], $args['base'] ); + $link = str_replace( '%#%', $n, $link ); if ( $add_args ) $link = add_query_arg( $add_args, $link ); $link .= $args['add_fragment']; @@ -2514,7 +2533,8 @@ function paginate_links( $args = '' ) { endif; endfor; if ( $args['prev_next'] && $current && ( $current < $total || -1 == $total ) ) : - $link = str_replace( '%#%', $current + 1, $base ); + $link = str_replace( '%_%', $args['format'], $args['base'] ); + $link = str_replace( '%#%', $current + 1, $link ); if ( $add_args ) $link = add_query_arg( $add_args, $link ); $link .= $args['add_fragment']; diff --git a/tests/phpunit/tests/general/paginateLinks.php b/tests/phpunit/tests/general/paginateLinks.php index 577bf05f05..da674d4807 100644 --- a/tests/phpunit/tests/general/paginateLinks.php +++ b/tests/phpunit/tests/general/paginateLinks.php @@ -4,11 +4,24 @@ class Tests_Paginate_Links extends WP_UnitTestCase { private $i18n_count = 0; + function setUp() { + parent::setUp(); + + $this->go_to( home_url( '/' ) ); + } + function test_defaults() { + $page2 = get_pagenum_link( 2 ); + $page3 = get_pagenum_link( 3 ); + $page50 = get_pagenum_link( 50 ); + $expected =<<1 +1 +2 +3 -50 +50 + EXPECTED; $links = paginate_links( array( 'total' => 50 ) ); @@ -16,24 +29,36 @@ EXPECTED; } function test_format() { + $page2 = home_url( '/page/2/' ); + $page3 = home_url( '/page/3/' ); + $page50 = home_url( '/page/50/' ); + $expected =<<1 +1 +2 +3 -50 +50 + EXPECTED; - $links = paginate_links( array( 'total' => 50, 'format' => '/page/%#%/' ) ); + $links = paginate_links( array( 'total' => 50, 'format' => 'page/%#%/' ) ); $this->assertEquals( $expected, $links ); } function test_prev_next_false() { + $home = home_url( '/' ); + $page3 = get_pagenum_link( 3 ); + $page4 = get_pagenum_link( 4 ); + $page50 = get_pagenum_link( 50 ); + $expected =<<1 +1 2 -3 -4 +3 +4 -50 +50 EXPECTED; $links = paginate_links( array( 'total' => 50, 'prev_next' => false, 'current' => 2 ) ); @@ -41,15 +66,20 @@ EXPECTED; } function test_prev_next_true() { + $home = home_url( '/' ); + $page3 = get_pagenum_link( 3 ); + $page4 = get_pagenum_link( 4 ); + $page50 = get_pagenum_link( 50 ); + $expected =<<« Previous -1 + +1 2 -3 -4 +3 +4 -50 - +50 + EXPECTED; $links = paginate_links( array( 'total' => 50, 'prev_next' => true, 'current' => 2 ) ); @@ -95,8 +125,8 @@ EXPECTED; ) ); // It's supposed to link to page 1: - $this->assertTag( array( 'tag' => 'a', 'attributes' => array( 'href' => '?page=1' ) ), $links[0] ); - $this->assertTag( array( 'tag' => 'a', 'attributes' => array( 'href' => '?page=1' ) ), $links[1] ); + $this->assertTag( array( 'tag' => 'a', 'attributes' => array( 'href' => 'http://' . WP_TESTS_DOMAIN . '/' ) ), $links[0] ); + $this->assertTag( array( 'tag' => 'a', 'attributes' => array( 'href' => 'http://' . WP_TESTS_DOMAIN . '/' ) ), $links[1] ); // It's not supposed to have an empty href. $this->assertNotTag( array( 'tag' => 'a', 'attributes' => array( 'class' => 'prev page-numbers', 'href' => '' ) ), $links[0] ); @@ -112,7 +142,7 @@ EXPECTED; ) ); $this->assertTag( array( 'tag' => 'span', 'attributes' => array( 'class' => 'current' ) ), $links[0] ); - $this->assertTag( array( 'tag' => 'a', 'attributes' => array( 'href' => '?page=2' ) ), $links[1] ); + $this->assertTag( array( 'tag' => 'a', 'attributes' => array( 'href' => get_pagenum_link( 2 ) ) ), $links[1] ); } }