From 0fab3196a5e1bf3adc8f2bf4c0949733daf5dcf4 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Tue, 3 Aug 2021 14:30:50 +0300 Subject: [PATCH] Add option to `paginate_links` to apply format on all pages --- src/wp-includes/general-template.php | 8 +++++--- tests/phpunit/tests/general/paginateLinks.php | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/general-template.php b/src/wp-includes/general-template.php index ab8877f7be63e..82fdaed9bbe5c 100644 --- a/src/wp-includes/general-template.php +++ b/src/wp-includes/general-template.php @@ -4161,14 +4161,15 @@ function language_attributes( $doctype = 'html' ) { * Default 1. * @type int $mid_size How many numbers to either side of the current pages. Default 2. * @type bool $prev_next Whether to include the previous and next links in the list. Default true. - * @type bool $prev_text The previous page text. Default '« Previous'. - * @type bool $next_text The next page text. Default 'Next »'. + * @type string $prev_text The previous page text. Default '« Previous'. + * @type string $next_text The next page text. Default 'Next »'. * @type string $type Controls format of the returned value. Possible values are 'plain', * 'array' and 'list'. Default is 'plain'. * @type array $add_args An array of query args to add. Default false. * @type string $add_fragment A string to append to each link. Default empty. * @type string $before_page_number A string to appear before the page number. Default empty. * @type string $after_page_number A string to append after the page number. Default empty. + * @type bool $format_all_pages Whether to apply the provided `format` on all page's links. Default false. * } * @return string|array|void String of page links or array of page links, depending on 'type' argument. * Void if total number of pages is less than 2. @@ -4208,6 +4209,7 @@ function paginate_links( $args = '' ) { 'add_fragment' => '', 'before_page_number' => '', 'after_page_number' => '', + 'format_all_pages' => false, ); $args = wp_parse_args( $args, $defaults ); @@ -4287,7 +4289,7 @@ 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( '%_%', 1 == $n ? '' : $args['format'], $args['base'] ); + $link = str_replace( '%_%', 1 == $n && ! $args['format_all_pages'] ? '' : $args['format'], $args['base'] ); $link = str_replace( '%#%', $n, $link ); if ( $add_args ) { $link = add_query_arg( $add_args, $link ); diff --git a/tests/phpunit/tests/general/paginateLinks.php b/tests/phpunit/tests/general/paginateLinks.php index 3d992e3998e25..fc9f06ca79a49 100644 --- a/tests/phpunit/tests/general/paginateLinks.php +++ b/tests/phpunit/tests/general/paginateLinks.php @@ -362,4 +362,21 @@ public function test_custom_base_query_arg_should_be_stripped_from_current_url_b $page_2_url = home_url() . '?foo=2'; $this->assertContains( "2", $links ); } + + /** + * @ticket 53868 + */ + function test_paginate_links_format_all_pages() { + $links = paginate_links( + array( + 'current' => 2, + 'total' => 5, + 'prev_next' => false, + 'format_all_pages' => true, + 'type' => 'array', + ) + ); + + $this->assertStringContainsString( '?paged=1', $links[0] ); + } }