Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to paginate_links to apply format on all pages #1540

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/wp-includes/general-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 »'.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes are unrelated, but noticed the wrong type.

* @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.
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
Expand Down
17 changes: 17 additions & 0 deletions tests/phpunit/tests/general/paginateLinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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( "<a class=\"page-numbers\" href=\"$page_2_url\">2</a>", $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] );
}
}