Make WordPress Core


Ignore:
Timestamp:
06/20/2014 05:11:14 PM (10 years ago)
Author:
wonderboymusic
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/general-template.php

    r28671 r28785  
    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,
     
    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 );
     
    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 );
     
    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 );
Note: See TracChangeset for help on using the changeset viewer.