From 378b4cd89637e39d3db3e4bd4e29c026e1be4407 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Fri, 20 Jun 2014 17:12:15 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@28785 git-svn-id: http://core.svn.wordpress.org/trunk@28598 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/general-template.php | 36 +++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/wp-includes/general-template.php b/wp-includes/general-template.php index 34f7be10eb..0df696a479 100644 --- a/wp-includes/general-template.php +++ b/wp-includes/general-template.php @@ -2438,11 +2438,29 @@ function language_attributes($doctype = 'html') { * @return array|string String of page links or array of page links. */ function paginate_links( $args = '' ) { + global $wp_query, $wp_rewrite; + + $total = ( isset( $wp_query->max_num_pages ) ) ? $wp_query->max_num_pages : 1; + $current = ( get_query_var( 'paged' ) ) ? intval( get_query_var( 'paged' ) ) : 1; + $pagenum_link = html_entity_decode( get_pagenum_link() ); + $query_args = array(); + $url_parts = explode( '?', $pagenum_link ); + + if ( isset( $url_parts[1] ) ) { + wp_parse_str( $url_parts[1], $query_args ); + } + + $pagenum_link = remove_query_arg( array_keys( $query_args ), $pagenum_link ); + $pagenum_link = trailingslashit( $pagenum_link ) . '%_%'; + + $format = $wp_rewrite->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : ''; + $format .= $wp_rewrite->using_permalinks() ? user_trailingslashit( $wp_rewrite->pagination_base . '/%#%', 'paged' ) : '?paged=%#%'; + $defaults = array( - 'base' => '%_%', // http://example.com/all_posts.php%_% : %_% is replaced by format (below) - 'format' => '?page=%#%', // ?page=%#% : %#% is replaced by the page number - 'total' => 1, - 'current' => 0, + 'base' => $pagenum_link, // http://example.com/all_posts.php%_% : %_% is replaced by format (below) + 'format' => $format, // ?page=%#% : %#% is replaced by the page number + 'total' => $total, + 'current' => $current, 'show_all' => false, 'prev_next' => true, 'prev_text' => __('« Previous'), @@ -2476,10 +2494,10 @@ function paginate_links( $args = '' ) { $r = ''; $page_links = array(); $dots = false; - $base = str_replace( '%_%', $args['format'], $args['base'] ); if ( $args['prev_next'] && $current && 1 < $current ) : - $link = str_replace( '%#%', $current - 1, $base ); + $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 .= $args['add_fragment']; @@ -2499,7 +2517,8 @@ function paginate_links( $args = '' ) { $dots = true; else : if ( $args['show_all'] || ( $n <= $end_size || ( $current && $n >= $current - $mid_size && $n <= $current + $mid_size ) || $n > $total - $end_size ) ) : - $link = str_replace( '%#%', $n, $base ); + $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 .= $args['add_fragment']; @@ -2514,7 +2533,8 @@ function paginate_links( $args = '' ) { endif; endfor; if ( $args['prev_next'] && $current && ( $current < $total || -1 == $total ) ) : - $link = str_replace( '%#%', $current + 1, $base ); + $link = str_replace( '%_%', $args['format'], $args['base'] ); + $link = str_replace( '%#%', $current + 1, $link ); if ( $add_args ) $link = add_query_arg( $add_args, $link ); $link .= $args['add_fragment'];