mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-01-29 21:17:39 +00:00
Eliminate use of extract() in paginate_links(). Adds unit tests. Moves tests/general/template.php (which only had one method) to tests/general/paginateLinks.php.
See #22400. git-svn-id: https://develop.svn.wordpress.org/trunk@28397 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
46f15a84ab
commit
9e07b1037e
@ -2447,26 +2447,32 @@ function paginate_links( $args = '' ) {
|
||||
);
|
||||
|
||||
$args = wp_parse_args( $args, $defaults );
|
||||
extract($args, EXTR_SKIP);
|
||||
|
||||
// Who knows what else people pass in $args
|
||||
$total = (int) $total;
|
||||
if ( $total < 2 )
|
||||
$total = (int) $args['total'];
|
||||
if ( $total < 2 ) {
|
||||
return;
|
||||
$current = (int) $current;
|
||||
$end_size = 0 < (int) $end_size ? (int) $end_size : 1; // Out of bounds? Make it the default.
|
||||
$mid_size = 0 <= (int) $mid_size ? (int) $mid_size : 2;
|
||||
$add_args = is_array($add_args) ? $add_args : false;
|
||||
}
|
||||
$current = (int) $args['current'];
|
||||
$end_size = (int) $args['end_size']; // Out of bounds? Make it the default.
|
||||
if ( $end_size < 1 ) {
|
||||
$end_size = 1;
|
||||
}
|
||||
$mid_size = (int) $args['mid_size'];
|
||||
if ( $mid_size < 0 ) {
|
||||
$mid_size = 2;
|
||||
}
|
||||
$add_args = is_array( $args['add_args'] ) ? $args['add_args'] : false;
|
||||
$r = '';
|
||||
$page_links = array();
|
||||
$dots = false;
|
||||
|
||||
if ( $prev_next && $current && 1 < $current ) :
|
||||
$link = str_replace('%_%', 2 == $current ? '' : $format, $base);
|
||||
$link = str_replace('%#%', $current - 1, $link);
|
||||
if ( $args['prev_next'] && $current && 1 < $current ) :
|
||||
$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 .= $add_fragment;
|
||||
$link .= $args['add_fragment'];
|
||||
|
||||
/**
|
||||
* Filter the paginated links for the given archive pages.
|
||||
@ -2475,40 +2481,40 @@ function paginate_links( $args = '' ) {
|
||||
*
|
||||
* @param string $link The paginated link URL.
|
||||
*/
|
||||
$page_links[] = '<a class="prev page-numbers" href="' . esc_url( apply_filters( 'paginate_links', $link ) ) . '">' . $prev_text . '</a>';
|
||||
$page_links[] = '<a class="prev page-numbers" href="' . esc_url( apply_filters( 'paginate_links', $link ) ) . '">' . $args['prev_text'] . '</a>';
|
||||
endif;
|
||||
for ( $n = 1; $n <= $total; $n++ ) :
|
||||
if ( $n == $current ) :
|
||||
$page_links[] = "<span class='page-numbers current'>" . $before_page_number . number_format_i18n( $n ) . $after_page_number . "</span>";
|
||||
$page_links[] = "<span class='page-numbers current'>" . $args['before_page_number'] . number_format_i18n( $n ) . $args['after_page_number'] . "</span>";
|
||||
$dots = true;
|
||||
else :
|
||||
if ( $show_all || ( $n <= $end_size || ( $current && $n >= $current - $mid_size && $n <= $current + $mid_size ) || $n > $total - $end_size ) ) :
|
||||
$link = str_replace('%_%', 1 == $n ? '' : $format, $base);
|
||||
$link = str_replace('%#%', $n, $link);
|
||||
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 );
|
||||
if ( $add_args )
|
||||
$link = add_query_arg( $add_args, $link );
|
||||
$link .= $add_fragment;
|
||||
$link .= $args['add_fragment'];
|
||||
|
||||
/** This filter is documented in wp-includes/general-template.php */
|
||||
$page_links[] = "<a class='page-numbers' href='" . esc_url( apply_filters( 'paginate_links', $link ) ) . "'>" . $before_page_number . number_format_i18n( $n ) . $after_page_number . "</a>";
|
||||
$page_links[] = "<a class='page-numbers' href='" . esc_url( apply_filters( 'paginate_links', $link ) ) . "'>" . $args['before_page_number'] . number_format_i18n( $n ) . $args['after_page_number'] . "</a>";
|
||||
$dots = true;
|
||||
elseif ( $dots && !$show_all ) :
|
||||
elseif ( $dots && ! $args['show_all'] ) :
|
||||
$page_links[] = '<span class="page-numbers dots">' . __( '…' ) . '</span>';
|
||||
$dots = false;
|
||||
endif;
|
||||
endif;
|
||||
endfor;
|
||||
if ( $prev_next && $current && ( $current < $total || -1 == $total ) ) :
|
||||
$link = str_replace('%_%', $format, $base);
|
||||
$link = str_replace('%#%', $current + 1, $link);
|
||||
if ( $args['prev_next'] && $current && ( $current < $total || -1 == $total ) ) :
|
||||
$link = str_replace( '%_%', $args['format'], $args['base'] );
|
||||
$link = str_replace( '%#%', $current + 1, $link );
|
||||
if ( $add_args )
|
||||
$link = add_query_arg( $add_args, $link );
|
||||
$link .= $add_fragment;
|
||||
$link .= $args['add_fragment'];
|
||||
|
||||
/** This filter is documented in wp-includes/general-template.php */
|
||||
$page_links[] = '<a class="next page-numbers" href="' . esc_url( apply_filters( 'paginate_links', $link ) ) . '">' . $next_text . '</a>';
|
||||
$page_links[] = '<a class="next page-numbers" href="' . esc_url( apply_filters( 'paginate_links', $link ) ) . '">' . $args['next_text'] . '</a>';
|
||||
endif;
|
||||
switch ( $type ) {
|
||||
switch ( $args['type'] ) {
|
||||
case 'array' :
|
||||
return $page_links;
|
||||
|
||||
|
||||
82
tests/phpunit/tests/general/paginateLinks.php
Normal file
82
tests/phpunit/tests/general/paginateLinks.php
Normal file
@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
class Tests_Paginate_Links extends WP_UnitTestCase {
|
||||
|
||||
private $i18n_count = 0;
|
||||
|
||||
function test_defaults() {
|
||||
$expected =<<<EXPECTED
|
||||
<a class='page-numbers' href=''>1</a>
|
||||
<span class="page-numbers dots">…</span>
|
||||
<a class='page-numbers' href='?page=50'>50</a>
|
||||
EXPECTED;
|
||||
|
||||
$links = paginate_links( array( 'total' => 50 ) );
|
||||
$this->assertEquals( $expected, $links );
|
||||
}
|
||||
|
||||
function test_format() {
|
||||
$expected =<<<EXPECTED
|
||||
<a class='page-numbers' href=''>1</a>
|
||||
<span class="page-numbers dots">…</span>
|
||||
<a class='page-numbers' href='/page/50/'>50</a>
|
||||
EXPECTED;
|
||||
|
||||
$links = paginate_links( array( 'total' => 50, 'format' => '/page/%#%/' ) );
|
||||
$this->assertEquals( $expected, $links );
|
||||
}
|
||||
|
||||
function test_prev_next_false() {
|
||||
$expected =<<<EXPECTED
|
||||
<a class='page-numbers' href=''>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>
|
||||
<span class="page-numbers dots">…</span>
|
||||
<a class='page-numbers' href='?page=50'>50</a>
|
||||
EXPECTED;
|
||||
|
||||
$links = paginate_links( array( 'total' => 50, 'prev_next' => false, 'current' => 2 ) );
|
||||
$this->assertEquals( $expected, $links );
|
||||
}
|
||||
|
||||
function test_prev_next_true() {
|
||||
$expected =<<<EXPECTED
|
||||
<a class="prev page-numbers" href="">« Previous</a>
|
||||
<a class='page-numbers' href=''>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>
|
||||
<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>
|
||||
EXPECTED;
|
||||
|
||||
$links = paginate_links( array( 'total' => 50, 'prev_next' => true, 'current' => 2 ) );
|
||||
$this->assertEquals( $expected, $links );
|
||||
}
|
||||
|
||||
function increment_i18n_count() {
|
||||
$this->i18n_count += 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 25735
|
||||
*/
|
||||
function test_paginate_links_number_format() {
|
||||
$this->i18n_count = 0;
|
||||
add_filter( 'number_format_i18n', array( $this, 'increment_i18n_count' ) );
|
||||
paginate_links( array(
|
||||
'total' => 100,
|
||||
'current' => 50,
|
||||
'show_all' => false,
|
||||
'prev_next' => true,
|
||||
'end_size' => 1,
|
||||
'mid_size' => 1,
|
||||
) );
|
||||
// The links should be:
|
||||
// < Previous 1 ... 49 50 51 ... 100 Next >
|
||||
$this->assertEquals( 5, $this->i18n_count );
|
||||
remove_filter( 'number_format_i18n', array( $this, 'increment_i18n_count' ) );
|
||||
}
|
||||
}
|
||||
@ -1,31 +0,0 @@
|
||||
<?php
|
||||
|
||||
class Tests_General_Template extends WP_UnitTestCase {
|
||||
|
||||
private $i18n_count = 0;
|
||||
|
||||
function increment_i18n_count() {
|
||||
$this->i18n_count += 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 25735
|
||||
*/
|
||||
function test_paginate_links_number_format() {
|
||||
$this->i18n_count = 0;
|
||||
add_filter( 'number_format_i18n', array( $this, 'increment_i18n_count' ) );
|
||||
paginate_links( array(
|
||||
'total' => 100,
|
||||
'current' => 50,
|
||||
'show_all' => false,
|
||||
'prev_next' => true,
|
||||
'end_size' => 1,
|
||||
'mid_size' => 1,
|
||||
) );
|
||||
// The links should be:
|
||||
// < Previous 1 ... 49 50 51 ... 100 Next >
|
||||
$this->assertEquals( 5, $this->i18n_count );
|
||||
remove_filter( 'number_format_i18n', array( $this, 'increment_i18n_count' ) );
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user