Skip to content

Commit 173ee18

Browse files
committed
Permalinks: Ensure pagination links are consistent with permalink structure.
This changeset ensures that pagination links stay consistent with the chosen permalink structure. When the permalink structure uses a trailing slash, pagination permalinks contain one as well, but when the permalink structure doesn't use trailing slash, then pagination links should not use a trailing slash. This makes use of `user_trailingslashit()` with a `paged` value for the `type_of_url` parameter. Props hmbashar, huzaifaalmesbah, rejaulalomkhan, mai21, rahulsprajapati, martinkrcho, ankitkumarshah, adamsilverstein, sourabhjain. Fixes #61393. git-svn-id: https://develop.svn.wordpress.org/trunk@59966 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 5564c70 commit 173ee18

2 files changed

Lines changed: 79 additions & 0 deletions

File tree

src/wp-includes/general-template.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4713,6 +4713,7 @@ function paginate_links( $args = '' ) {
47134713
$link = add_query_arg( $add_args, $link );
47144714
}
47154715
$link .= $args['add_fragment'];
4716+
$link = get_option( 'permalink_structure' ) ? user_trailingslashit( $link, 'paged' ) : $link;
47164717

47174718
$page_links[] = sprintf(
47184719
'<a class="prev page-numbers" href="%s">%s</a>',
@@ -4745,6 +4746,7 @@ function paginate_links( $args = '' ) {
47454746
$link = add_query_arg( $add_args, $link );
47464747
}
47474748
$link .= $args['add_fragment'];
4749+
$link = get_option( 'permalink_structure' ) ? user_trailingslashit( $link, 'paged' ) : $link;
47484750

47494751
$page_links[] = sprintf(
47504752
'<a class="page-numbers" href="%s">%s</a>',
@@ -4769,6 +4771,7 @@ function paginate_links( $args = '' ) {
47694771
$link = add_query_arg( $add_args, $link );
47704772
}
47714773
$link .= $args['add_fragment'];
4774+
$link = get_option( 'permalink_structure' ) ? user_trailingslashit( $link, 'paged' ) : $link;
47724775

47734776
$page_links[] = sprintf(
47744777
'<a class="next page-numbers" href="%s">%s</a>',

tests/phpunit/tests/general/paginateLinks.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,4 +362,80 @@ public function test_custom_base_query_arg_should_be_stripped_from_current_url_b
362362
$page_2_url = home_url() . '?foo=2';
363363
$this->assertContains( "<a class=\"page-numbers\" href=\"$page_2_url\">2</a>", $links );
364364
}
365+
366+
/**
367+
* @ticket 61393
368+
*/
369+
public function test_pagination_links_with_trailing_slash() {
370+
$this->set_permalink_structure( '/%postname%/' );
371+
372+
$args = array(
373+
'base' => 'http://example.org/category/test/%_%',
374+
'format' => 'page/%#%',
375+
'total' => 5,
376+
'current' => 2,
377+
'prev_next' => true,
378+
);
379+
380+
$links = paginate_links( $args );
381+
382+
// Test page 1 link (should have trailing slash)
383+
$this->assertStringContainsString(
384+
'href="http://example.org/category/test/"',
385+
$links,
386+
'Page 1 link should have trailing slash when permalink structure has trailing slash'
387+
);
388+
389+
// Test page 3 link (should have trailing slash)
390+
$this->assertStringContainsString(
391+
'href="http://example.org/category/test/page/3/"',
392+
$links,
393+
'Page 3 link should have trailing slash when permalink structure has trailing slash'
394+
);
395+
396+
// Test previous link (should have trailing slash)
397+
$this->assertStringContainsString(
398+
'class="prev page-numbers" href="http://example.org/category/test/"',
399+
$links,
400+
'Previous link should have trailing slash when permalink structure has trailing slash'
401+
);
402+
}
403+
404+
/**
405+
* @ticket 61393
406+
*/
407+
public function test_pagination_links_without_trailing_slash() {
408+
$this->set_permalink_structure( '/%postname%' );
409+
410+
$args = array(
411+
'base' => 'http://example.org/category/test/%_%',
412+
'format' => 'page/%#%',
413+
'total' => 5,
414+
'current' => 2,
415+
'prev_next' => true,
416+
);
417+
418+
$links = paginate_links( $args );
419+
420+
// Test page 1 link (should not have trailing slash)
421+
$this->assertStringContainsString(
422+
'href="http://example.org/category/test"',
423+
$links,
424+
'Page 1 link should not have trailing slash when permalink structure has no trailing slash'
425+
);
426+
427+
// Test page 3 link (should not have trailing slash)
428+
$this->assertStringContainsString(
429+
'href="http://example.org/category/test/page/3"',
430+
$links,
431+
'Page 3 link should not have trailing slash when permalink structure has no trailing slash'
432+
);
433+
434+
// Test previous link (should not have trailing slash)
435+
$this->assertStringContainsString(
436+
'class="prev page-numbers" href="http://example.org/category/test"',
437+
$links,
438+
'Previous link should not have trailing slash when permalink structure has no trailing slash'
439+
);
440+
}
365441
}

0 commit comments

Comments
 (0)