Make WordPress Core

Ticket #24606: 24606.6.diff

File 24606.6.diff, 7.9 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_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
    24412459        $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
    2444                 '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,
    24462464                'show_all' => false,
    24472465                'prev_next' => true,
    24482466                'prev_text' => __('« Previous'),
     
    24762494        $r = '';
    24772495        $page_links = array();
    24782496        $dots = false;
    2479         $base = str_replace( '%_%', $args['format'], $args['base'] );
    24802497
    24812498        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 );
    24832501                if ( $add_args )
    24842502                        $link = add_query_arg( $add_args, $link );
    24852503                $link .= $args['add_fragment'];
     
    24992517                        $dots = true;
    25002518                else :
    25012519                        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 );
    25032522                                if ( $add_args )
    25042523                                        $link = add_query_arg( $add_args, $link );
    25052524                                $link .= $args['add_fragment'];
     
    25142533                endif;
    25152534        endfor;
    25162535        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 );
    25182538                if ( $add_args )
    25192539                        $link = add_query_arg( $add_args, $link );
    25202540                $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<span class='page-numbers current'>1</span>
     10<a class='page-numbers' href='http://{WP_TESTS_DOMAIN}/?paged=2'>2</a>
     11<a class='page-numbers' href='http://{WP_TESTS_DOMAIN}/?paged=3'>3</a>
    1012<span class="page-numbers dots">&hellip;</span>
    11 <a class='page-numbers' href='?page=50'>50</a>
     13<a class='page-numbers' href='http://{WP_TESTS_DOMAIN}/?paged=50'>50</a>
     14<a class="next page-numbers" href="http://{WP_TESTS_DOMAIN}/?paged=2">Next &raquo;</a>
    1215EXPECTED;
    1316
     17                $expected = str_replace( '{WP_TESTS_DOMAIN}', WP_TESTS_DOMAIN, $expected );
    1418                $links = paginate_links( array( 'total' => 50 ) );
    1519                $this->assertEquals( $expected, $links );
    1620        }
     
    1721
    1822        function test_format() {
    1923                $expected =<<<EXPECTED
    20 <a class='page-numbers' href=''>1</a>
     24<span class='page-numbers current'>1</span>
     25<a class='page-numbers' href='http://{WP_TESTS_DOMAIN}/page/2/'>2</a>
     26<a class='page-numbers' href='http://{WP_TESTS_DOMAIN}/page/3/'>3</a>
    2127<span class="page-numbers dots">&hellip;</span>
    22 <a class='page-numbers' href='/page/50/'>50</a>
     28<a class='page-numbers' href='http://{WP_TESTS_DOMAIN}/page/50/'>50</a>
     29<a class="next page-numbers" href="http://{WP_TESTS_DOMAIN}/page/2/">Next &raquo;</a>
    2330EXPECTED;
    2431
    25                 $links = paginate_links( array( 'total' => 50, 'format' => '/page/%#%/' ) );
     32                $expected = str_replace( '{WP_TESTS_DOMAIN}', WP_TESTS_DOMAIN, $expected );
     33                $links = paginate_links( array( 'total' => 50, 'format' => 'page/%#%/' ) );
    2634                $this->assertEquals( $expected, $links );
    2735        }
    2836
    2937        function test_prev_next_false() {
    3038                $expected =<<<EXPECTED
    31 <a class='page-numbers' href=''>1</a>
     39<a class='page-numbers' href='http://{WP_TESTS_DOMAIN}/'>1</a>
    3240<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>
     41<a class='page-numbers' href='http://{WP_TESTS_DOMAIN}/?paged=3'>3</a>
     42<a class='page-numbers' href='http://{WP_TESTS_DOMAIN}/?paged=4'>4</a>
    3543<span class="page-numbers dots">&hellip;</span>
    36 <a class='page-numbers' href='?page=50'>50</a>
     44<a class='page-numbers' href='http://{WP_TESTS_DOMAIN}/?paged=50'>50</a>
    3745EXPECTED;
    3846
     47                $expected = str_replace( '{WP_TESTS_DOMAIN}', WP_TESTS_DOMAIN, $expected );
    3948                $links = paginate_links( array( 'total' => 50, 'prev_next' => false, 'current' => 2 ) );
    4049                $this->assertEquals( $expected, $links );
    4150        }
     
    4251
    4352        function test_prev_next_true() {
    4453                $expected =<<<EXPECTED
    45 <a class="prev page-numbers" href="">&laquo; Previous</a>
    46 <a class='page-numbers' href=''>1</a>
     54<a class="prev page-numbers" href="http://{WP_TESTS_DOMAIN}/">&laquo; Previous</a>
     55<a class='page-numbers' href='http://{WP_TESTS_DOMAIN}/'>1</a>
    4756<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>
     57<a class='page-numbers' href='http://{WP_TESTS_DOMAIN}/?paged=3'>3</a>
     58<a class='page-numbers' href='http://{WP_TESTS_DOMAIN}/?paged=4'>4</a>
    5059<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>
     60<a class='page-numbers' href='http://{WP_TESTS_DOMAIN}/?paged=50'>50</a>
     61<a class="next page-numbers" href="http://{WP_TESTS_DOMAIN}/?paged=3">Next &raquo;</a>
    5362EXPECTED;
    5463
     64                $expected = str_replace( '{WP_TESTS_DOMAIN}', WP_TESTS_DOMAIN, $expected );
    5565                $links = paginate_links( array( 'total' => 50, 'prev_next' => true, 'current' => 2 ) );
    5666                $this->assertEquals( $expected, $links );
    5767        }
     
    95105                ) );
    96106
    97107                // 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] );
     108                $this->assertTag( array( 'tag' => 'a', 'attributes' => array( 'href' => 'http://' . WP_TESTS_DOMAIN . '/' ) ), $links[0] );
     109                $this->assertTag( array( 'tag' => 'a', 'attributes' => array( 'href' => 'http://' . WP_TESTS_DOMAIN . '/' ) ), $links[1] );
    100110
    101111                // It's not supposed to have an empty href.
    102112                $this->assertNotTag( array( 'tag' => 'a', 'attributes' => array( 'class' => 'prev page-numbers', 'href' => '' ) ), $links[0] );
     
    112122                ) );
    113123
    114124                $this->assertTag( array( 'tag' => 'span', 'attributes' => array( 'class' => 'current' ) ), $links[0] );
    115                 $this->assertTag( array( 'tag' => 'a',    'attributes' => array( 'href' => '?page=2' ) ), $links[1] );
     125                $this->assertTag( array( 'tag' => 'a',    'attributes' => array( 'href' => 'http://' . WP_TESTS_DOMAIN . '/?paged=2' ) ), $links[1] );
    116126        }
    117127
    118128}