From 4fde71e75d0ab4bdfcd3816e0291ca56bb8bd5cf Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Thu, 5 Jun 2014 02:09:12 +0000 Subject: [PATCH] Avoid an empty href attribute in paginate_links(). Add unit tests. props obenland, Nessworthy. fixes #24606. git-svn-id: https://develop.svn.wordpress.org/trunk@28671 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/general-template.php | 10 +++--- tests/phpunit/tests/general/paginateLinks.php | 36 +++++++++++++++++++ 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/general-template.php b/src/wp-includes/general-template.php index 1b08219206..34f7be10eb 100644 --- a/src/wp-includes/general-template.php +++ b/src/wp-includes/general-template.php @@ -2476,10 +2476,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( '%_%', 2 == $current ? '' : $args['format'], $args['base'] ); - $link = str_replace( '%#%', $current - 1, $link ); + $link = str_replace( '%#%', $current - 1, $base ); if ( $add_args ) $link = add_query_arg( $add_args, $link ); $link .= $args['add_fragment']; @@ -2499,8 +2499,7 @@ 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( '%_%', 1 == $n ? '' : $args['format'], $args['base'] ); - $link = str_replace( '%#%', $n, $link ); + $link = str_replace( '%#%', $n, $base ); if ( $add_args ) $link = add_query_arg( $add_args, $link ); $link .= $args['add_fragment']; @@ -2515,8 +2514,7 @@ function paginate_links( $args = '' ) { endif; endfor; if ( $args['prev_next'] && $current && ( $current < $total || -1 == $total ) ) : - $link = str_replace( '%_%', $args['format'], $args['base'] ); - $link = str_replace( '%#%', $current + 1, $link ); + $link = str_replace( '%#%', $current + 1, $base ); 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 034a434f0a..577bf05f05 100644 --- a/tests/phpunit/tests/general/paginateLinks.php +++ b/tests/phpunit/tests/general/paginateLinks.php @@ -79,4 +79,40 @@ EXPECTED; $this->assertEquals( 5, $this->i18n_count ); remove_filter( 'number_format_i18n', array( $this, 'increment_i18n_count' ) ); } + + /** + * @ticket 24606 + */ + function test_paginate_links_base_value() { + + // Current page: 2 + $links = paginate_links( array( + 'current' => 2, + 'total' => 5, + 'end_size' => 1, + 'mid_size' => 1, + 'type' => 'array', + ) ); + + // 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] ); + + // It's not supposed to have an empty href. + $this->assertNotTag( array( 'tag' => 'a', 'attributes' => array( 'class' => 'prev page-numbers', 'href' => '' ) ), $links[0] ); + $this->assertNotTag( array( 'tag' => 'a', 'attributes' => array( 'class' => 'page-numbers', 'href' => '' ) ), $links[1] ); + + // Current page: 1 + $links = paginate_links( array( + 'current' => 1, + 'total' => 5, + 'end_size' => 1, + 'mid_size' => 1, + 'type' => 'array', + ) ); + + $this->assertTag( array( 'tag' => 'span', 'attributes' => array( 'class' => 'current' ) ), $links[0] ); + $this->assertTag( array( 'tag' => 'a', 'attributes' => array( 'href' => '?page=2' ) ), $links[1] ); + } + }