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
This commit is contained in:
Sergey Biryukov 2014-06-05 02:09:12 +00:00
parent 5c071dbc16
commit 4fde71e75d
2 changed files with 40 additions and 6 deletions

View File

@ -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'];

View File

@ -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] );
}
}