Make WordPress Core

Ticket #24606: 24606.5.diff

File 24606.5.diff, 6.6 KB (added by SergeyBiryukov, 11 years ago)
  • src/wp-includes/general-template.php

     
    24382438 * @return array|string String of page links or array of page links.
    24392439 */
    24402440function paginate_links( $args = '' ) {
     2441        global $wp_rewrite;
     2442
     2443        $pagenum_link = html_entity_decode( get_pagenum_link() );
     2444        $query_args   = array();
     2445        $url_parts    = explode( '?', $pagenum_link );
     2446
     2447        if ( isset( $url_parts[1] ) ) {
     2448                wp_parse_str( $url_parts[1], $query_args );
     2449        }
     2450
     2451        $pagenum_link = remove_query_arg( array_keys( $query_args ), $pagenum_link );
     2452        $pagenum_link = trailingslashit( $pagenum_link ) . '%_%';
     2453
     2454        $format  = $wp_rewrite->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : '';
     2455        $format .= $wp_rewrite->using_permalinks() ? user_trailingslashit( $wp_rewrite->pagination_base . '/%#%', 'paged' ) : '?page=%#%';
     2456
    24412457        $defaults = array(
    2442                 'base' => '%_%', // http://example.com/all_posts.php%_% : %_% is replaced by format (below)
    2443                 'format' => '?page=%#%', // ?page=%#% : %#% is replaced by the page number
     2458                'base' => $pagenum_link, // http://example.com/all_posts.php%_% : %_% is replaced by format (below)
     2459                'format' => $format, // ?page=%#% : %#% is replaced by the page number
    24442460                'total' => 1,
    24452461                'current' => 0,
    24462462                'show_all' => false,
     
    24762492        $r = '';
    24772493        $page_links = array();
    24782494        $dots = false;
    2479         $base = str_replace( '%_%', $args['format'], $args['base'] );
    24802495
    24812496        if ( $args['prev_next'] && $current && 1 < $current ) :
    2482                 $link = str_replace( '%#%', $current - 1, $base );
     2497                $link = str_replace( '%_%', 2 == $current ? '' : $args['format'], $args['base'] );
     2498                $link = str_replace( '%#%', $current - 1, $link );
    24832499                if ( $add_args )
    24842500                        $link = add_query_arg( $add_args, $link );
    24852501                $link .= $args['add_fragment'];
     
    24992515                        $dots = true;
    25002516                else :
    25012517                        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 );
     2518                                $link = str_replace( '%_%', 1 == $n ? '' : $args['format'], $args['base'] );
     2519                                $link = str_replace( '%#%', $n, $link );
    25032520                                if ( $add_args )
    25042521                                        $link = add_query_arg( $add_args, $link );
    25052522                                $link .= $args['add_fragment'];
     
    25142531                endif;
    25152532        endfor;
    25162533        if ( $args['prev_next'] && $current && ( $current < $total || -1 == $total ) ) :
    2517                 $link = str_replace( '%#%', $current + 1, $base );
     2534                $link = str_replace( '%_%', $args['format'], $args['base'] );
     2535                $link = str_replace( '%#%', $current + 1, $link );
    25182536                if ( $add_args )
    25192537                        $link = add_query_arg( $add_args, $link );
    25202538                $link .= $args['add_fragment'];
  • tests/phpunit/tests/general/paginateLinks.php

     
    66
    77        function test_defaults() {
    88                $expected =<<<EXPECTED
    9 <a class='page-numbers' href=''>1</a>
     9<a class='page-numbers' href='http://example.org/'>1</a>
    1010<span class="page-numbers dots">&hellip;</span>
    11 <a class='page-numbers' href='?page=50'>50</a>
     11<a class='page-numbers' href='http://example.org/?page=50'>50</a>
    1212EXPECTED;
    1313
    1414                $links = paginate_links( array( 'total' => 50 ) );
     
    1717
    1818        function test_format() {
    1919                $expected =<<<EXPECTED
    20 <a class='page-numbers' href=''>1</a>
     20<a class='page-numbers' href='http://example.org/'>1</a>
    2121<span class="page-numbers dots">&hellip;</span>
    22 <a class='page-numbers' href='/page/50/'>50</a>
     22<a class='page-numbers' href='http://example.org/page/50/'>50</a>
    2323EXPECTED;
    2424
    25                 $links = paginate_links( array( 'total' => 50, 'format' => '/page/%#%/' ) );
     25                $links = paginate_links( array( 'total' => 50, 'format' => 'page/%#%/' ) );
    2626                $this->assertEquals( $expected, $links );
    2727        }
    2828
    2929        function test_prev_next_false() {
    3030                $expected =<<<EXPECTED
    31 <a class='page-numbers' href=''>1</a>
     31<a class='page-numbers' href='http://example.org/'>1</a>
    3232<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>
     33<a class='page-numbers' href='http://example.org/?page=3'>3</a>
     34<a class='page-numbers' href='http://example.org/?page=4'>4</a>
    3535<span class="page-numbers dots">&hellip;</span>
    36 <a class='page-numbers' href='?page=50'>50</a>
     36<a class='page-numbers' href='http://example.org/?page=50'>50</a>
    3737EXPECTED;
    3838
    3939                $links = paginate_links( array( 'total' => 50, 'prev_next' => false, 'current' => 2 ) );
     
    4242
    4343        function test_prev_next_true() {
    4444                $expected =<<<EXPECTED
    45 <a class="prev page-numbers" href="">&laquo; Previous</a>
    46 <a class='page-numbers' href=''>1</a>
     45<a class="prev page-numbers" href="http://example.org/">&laquo; Previous</a>
     46<a class='page-numbers' href='http://example.org/'>1</a>
    4747<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>
     48<a class='page-numbers' href='http://example.org/?page=3'>3</a>
     49<a class='page-numbers' href='http://example.org/?page=4'>4</a>
    5050<span class="page-numbers dots">&hellip;</span>
    51 <a class='page-numbers' href='?page=50'>50</a>
    52 <a class="next page-numbers" href="?page=3">Next &raquo;</a>
     51<a class='page-numbers' href='http://example.org/?page=50'>50</a>
     52<a class="next page-numbers" href="http://example.org/?page=3">Next &raquo;</a>
    5353EXPECTED;
    5454
    5555                $links = paginate_links( array( 'total' => 50, 'prev_next' => true, 'current' => 2 ) );
     
    9595                ) );
    9696
    9797                // 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] );
     98                $this->assertTag( array( 'tag' => 'a', 'attributes' => array( 'href' => 'http://example.org/' ) ), $links[0] );
     99                $this->assertTag( array( 'tag' => 'a', 'attributes' => array( 'href' => 'http://example.org/' ) ), $links[1] );
    100100
    101101                // It's not supposed to have an empty href.
    102102                $this->assertNotTag( array( 'tag' => 'a', 'attributes' => array( 'class' => 'prev page-numbers', 'href' => '' ) ), $links[0] );
     
    112112                ) );
    113113
    114114                $this->assertTag( array( 'tag' => 'span', 'attributes' => array( 'class' => 'current' ) ), $links[0] );
    115                 $this->assertTag( array( 'tag' => 'a',    'attributes' => array( 'href' => '?page=2'  ) ), $links[1] );
     115                $this->assertTag( array( 'tag' => 'a',    'attributes' => array( 'href' => 'http://example.org/?page=2'  ) ), $links[1] );
    116116        }
    117117
    118118}