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
This commit is contained in:
parent
eb289835f7
commit
33275a0826
@ -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'];
|
||||
|
@ -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 =<<<EXPECTED
|
||||
<a class='page-numbers' href=''>1</a>
|
||||
<span class='page-numbers current'>1</span>
|
||||
<a class='page-numbers' href='$page2'>2</a>
|
||||
<a class='page-numbers' href='$page3'>3</a>
|
||||
<span class="page-numbers dots">…</span>
|
||||
<a class='page-numbers' href='?page=50'>50</a>
|
||||
<a class='page-numbers' href='$page50'>50</a>
|
||||
<a class="next page-numbers" href="$page2">Next »</a>
|
||||
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 =<<<EXPECTED
|
||||
<a class='page-numbers' href=''>1</a>
|
||||
<span class='page-numbers current'>1</span>
|
||||
<a class='page-numbers' href='$page2'>2</a>
|
||||
<a class='page-numbers' href='$page3'>3</a>
|
||||
<span class="page-numbers dots">…</span>
|
||||
<a class='page-numbers' href='/page/50/'>50</a>
|
||||
<a class='page-numbers' href='$page50'>50</a>
|
||||
<a class="next page-numbers" href="$page2">Next »</a>
|
||||
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 =<<<EXPECTED
|
||||
<a class='page-numbers' href=''>1</a>
|
||||
<a class='page-numbers' href='$home'>1</a>
|
||||
<span class='page-numbers current'>2</span>
|
||||
<a class='page-numbers' href='?page=3'>3</a>
|
||||
<a class='page-numbers' href='?page=4'>4</a>
|
||||
<a class='page-numbers' href='$page3'>3</a>
|
||||
<a class='page-numbers' href='$page4'>4</a>
|
||||
<span class="page-numbers dots">…</span>
|
||||
<a class='page-numbers' href='?page=50'>50</a>
|
||||
<a class='page-numbers' href='$page50'>50</a>
|
||||
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 =<<<EXPECTED
|
||||
<a class="prev page-numbers" href="">« Previous</a>
|
||||
<a class='page-numbers' href=''>1</a>
|
||||
<a class="prev page-numbers" href="$home">« Previous</a>
|
||||
<a class='page-numbers' href='$home'>1</a>
|
||||
<span class='page-numbers current'>2</span>
|
||||
<a class='page-numbers' href='?page=3'>3</a>
|
||||
<a class='page-numbers' href='?page=4'>4</a>
|
||||
<a class='page-numbers' href='$page3'>3</a>
|
||||
<a class='page-numbers' href='$page4'>4</a>
|
||||
<span class="page-numbers dots">…</span>
|
||||
<a class='page-numbers' href='?page=50'>50</a>
|
||||
<a class="next page-numbers" href="?page=3">Next »</a>
|
||||
<a class='page-numbers' href='$page50'>50</a>
|
||||
<a class="next page-numbers" href="$page3">Next »</a>
|
||||
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] );
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user