Changeset 28785
- Timestamp:
- 06/20/2014 05:11:14 PM (10 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/general-template.php
r28671 r28785 2439 2439 */ 2440 2440 function paginate_links( $args = '' ) { 2441 global $wp_query, $wp_rewrite; 2442 2443 $total = ( isset( $wp_query->max_num_pages ) ) ? $wp_query->max_num_pages : 1; 2444 $current = ( get_query_var( 'paged' ) ) ? intval( get_query_var( 'paged' ) ) : 1; 2445 $pagenum_link = html_entity_decode( get_pagenum_link() ); 2446 $query_args = array(); 2447 $url_parts = explode( '?', $pagenum_link ); 2448 2449 if ( isset( $url_parts[1] ) ) { 2450 wp_parse_str( $url_parts[1], $query_args ); 2451 } 2452 2453 $pagenum_link = remove_query_arg( array_keys( $query_args ), $pagenum_link ); 2454 $pagenum_link = trailingslashit( $pagenum_link ) . '%_%'; 2455 2456 $format = $wp_rewrite->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : ''; 2457 $format .= $wp_rewrite->using_permalinks() ? user_trailingslashit( $wp_rewrite->pagination_base . '/%#%', 'paged' ) : '?paged=%#%'; 2458 2441 2459 $defaults = array( 2442 'base' => '%_%', // http://example.com/all_posts.php%_% : %_% is replaced by format (below)2443 'format' => '?page=%#%', // ?page=%#% : %#% is replaced by the page number2444 'total' => 1,2445 'current' => 0,2460 'base' => $pagenum_link, // http://example.com/all_posts.php%_% : %_% is replaced by format (below) 2461 'format' => $format, // ?page=%#% : %#% is replaced by the page number 2462 'total' => $total, 2463 'current' => $current, 2446 2464 'show_all' => false, 2447 2465 'prev_next' => true, … … 2477 2495 $page_links = array(); 2478 2496 $dots = false; 2479 $base = str_replace( '%_%', $args['format'], $args['base'] );2480 2497 2481 2498 if ( $args['prev_next'] && $current && 1 < $current ) : 2482 $link = str_replace( '%#%', $current - 1, $base ); 2499 $link = str_replace( '%_%', 2 == $current ? '' : $args['format'], $args['base'] ); 2500 $link = str_replace( '%#%', $current - 1, $link ); 2483 2501 if ( $add_args ) 2484 2502 $link = add_query_arg( $add_args, $link ); … … 2500 2518 else : 2501 2519 if ( $args['show_all'] || ( $n <= $end_size || ( $current && $n >= $current - $mid_size && $n <= $current + $mid_size ) || $n > $total - $end_size ) ) : 2502 $link = str_replace( '%#%', $n, $base ); 2520 $link = str_replace( '%_%', 1 == $n ? '' : $args['format'], $args['base'] ); 2521 $link = str_replace( '%#%', $n, $link ); 2503 2522 if ( $add_args ) 2504 2523 $link = add_query_arg( $add_args, $link ); … … 2515 2534 endfor; 2516 2535 if ( $args['prev_next'] && $current && ( $current < $total || -1 == $total ) ) : 2517 $link = str_replace( '%#%', $current + 1, $base ); 2536 $link = str_replace( '%_%', $args['format'], $args['base'] ); 2537 $link = str_replace( '%#%', $current + 1, $link ); 2518 2538 if ( $add_args ) 2519 2539 $link = add_query_arg( $add_args, $link ); -
trunk/tests/phpunit/tests/general/paginateLinks.php
r28671 r28785 5 5 private $i18n_count = 0; 6 6 7 function setUp() { 8 parent::setUp(); 9 10 $this->go_to( home_url( '/' ) ); 11 } 12 7 13 function test_defaults() { 14 $page2 = get_pagenum_link( 2 ); 15 $page3 = get_pagenum_link( 3 ); 16 $page50 = get_pagenum_link( 50 ); 17 8 18 $expected =<<<EXPECTED 9 <a class='page-numbers' href=''>1</a> 19 <span class='page-numbers current'>1</span> 20 <a class='page-numbers' href='$page2'>2</a> 21 <a class='page-numbers' href='$page3'>3</a> 10 22 <span class="page-numbers dots">…</span> 11 <a class='page-numbers' href='?page=50'>50</a> 23 <a class='page-numbers' href='$page50'>50</a> 24 <a class="next page-numbers" href="$page2">Next »</a> 12 25 EXPECTED; 13 26 … … 17 30 18 31 function test_format() { 32 $page2 = home_url( '/page/2/' ); 33 $page3 = home_url( '/page/3/' ); 34 $page50 = home_url( '/page/50/' ); 35 19 36 $expected =<<<EXPECTED 20 <a class='page-numbers' href=''>1</a> 37 <span class='page-numbers current'>1</span> 38 <a class='page-numbers' href='$page2'>2</a> 39 <a class='page-numbers' href='$page3'>3</a> 21 40 <span class="page-numbers dots">…</span> 22 <a class='page-numbers' href='/page/50/'>50</a> 41 <a class='page-numbers' href='$page50'>50</a> 42 <a class="next page-numbers" href="$page2">Next »</a> 23 43 EXPECTED; 24 44 25 $links = paginate_links( array( 'total' => 50, 'format' => ' /page/%#%/' ) );45 $links = paginate_links( array( 'total' => 50, 'format' => 'page/%#%/' ) ); 26 46 $this->assertEquals( $expected, $links ); 27 47 } 28 48 29 49 function test_prev_next_false() { 50 $home = home_url( '/' ); 51 $page3 = get_pagenum_link( 3 ); 52 $page4 = get_pagenum_link( 4 ); 53 $page50 = get_pagenum_link( 50 ); 54 30 55 $expected =<<<EXPECTED 31 <a class='page-numbers' href=' '>1</a>56 <a class='page-numbers' href='$home'>1</a> 32 57 <span class='page-numbers current'>2</span> 33 <a class='page-numbers' href=' ?page=3'>3</a>34 <a class='page-numbers' href=' ?page=4'>4</a>58 <a class='page-numbers' href='$page3'>3</a> 59 <a class='page-numbers' href='$page4'>4</a> 35 60 <span class="page-numbers dots">…</span> 36 <a class='page-numbers' href=' ?page=50'>50</a>61 <a class='page-numbers' href='$page50'>50</a> 37 62 EXPECTED; 38 63 … … 42 67 43 68 function test_prev_next_true() { 69 $home = home_url( '/' ); 70 $page3 = get_pagenum_link( 3 ); 71 $page4 = get_pagenum_link( 4 ); 72 $page50 = get_pagenum_link( 50 ); 73 44 74 $expected =<<<EXPECTED 45 <a class="prev page-numbers" href=" ">« Previous</a>46 <a class='page-numbers' href=' '>1</a>75 <a class="prev page-numbers" href="$home">« Previous</a> 76 <a class='page-numbers' href='$home'>1</a> 47 77 <span class='page-numbers current'>2</span> 48 <a class='page-numbers' href=' ?page=3'>3</a>49 <a class='page-numbers' href=' ?page=4'>4</a>78 <a class='page-numbers' href='$page3'>3</a> 79 <a class='page-numbers' href='$page4'>4</a> 50 80 <span class="page-numbers dots">…</span> 51 <a class='page-numbers' href=' ?page=50'>50</a>52 <a class="next page-numbers" href=" ?page=3">Next »</a>81 <a class='page-numbers' href='$page50'>50</a> 82 <a class="next page-numbers" href="$page3">Next »</a> 53 83 EXPECTED; 54 84 … … 96 126 97 127 // It's supposed to link to page 1: 98 $this->assertTag( array( 'tag' => 'a', 'attributes' => array( 'href' => ' ?page=1' ) ), $links[0] );99 $this->assertTag( array( 'tag' => 'a', 'attributes' => array( 'href' => ' ?page=1' ) ), $links[1] );128 $this->assertTag( array( 'tag' => 'a', 'attributes' => array( 'href' => 'http://' . WP_TESTS_DOMAIN . '/' ) ), $links[0] ); 129 $this->assertTag( array( 'tag' => 'a', 'attributes' => array( 'href' => 'http://' . WP_TESTS_DOMAIN . '/' ) ), $links[1] ); 100 130 101 131 // It's not supposed to have an empty href. … … 113 143 114 144 $this->assertTag( array( 'tag' => 'span', 'attributes' => array( 'class' => 'current' ) ), $links[0] ); 115 $this->assertTag( array( 'tag' => 'a', 'attributes' => array( 'href' => '?page=2') ), $links[1] );145 $this->assertTag( array( 'tag' => 'a', 'attributes' => array( 'href' => get_pagenum_link( 2 ) ) ), $links[1] ); 116 146 } 117 147
Note: See TracChangeset
for help on using the changeset viewer.