Posts, Post Types: Don't force trailing slash in `get_pagenum_link()`.

Previously, a trailing slash was appended to the link returned from `get_pagenum_link()`. If the permalink structure didn't contain a trailing slash, this link could fail.

This change removes trailing slashes and only appends one if the site is set for adding trailing slashes.

This adds a new test file for the accompanying tests, `tests/phpunit/tests/link/getPagenumLink.php`, and moves an existing test for `get_pagenum_link()` to the same file.

Props davemad-davenet, darkfate, Nazgul, scribu, nacin, obenland, chriscct7, jesin, matthewppelsheimer, audrasjb, petitphp, mukesh27, oglekler, mai21, webtechpooja, tejwanihemant, nicolefurlan, hellofromTonya, costdev.
Fixes #2877.
Built from https://develop.svn.wordpress.org/trunk@56939


git-svn-id: http://core.svn.wordpress.org/trunk@56450 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
costdev 2023-10-16 00:07:26 +00:00
parent 464868a475
commit 13854cd7f5
2 changed files with 13 additions and 6 deletions

View File

@ -2431,6 +2431,9 @@ function get_pagenum_link( $pagenum = 1, $escape = true ) {
$qs_regex = '|\?.*?$|'; $qs_regex = '|\?.*?$|';
preg_match( $qs_regex, $request, $qs_match ); preg_match( $qs_regex, $request, $qs_match );
$parts = array();
$parts[] = untrailingslashit( get_bloginfo( 'url' ) );
if ( ! empty( $qs_match[0] ) ) { if ( ! empty( $qs_match[0] ) ) {
$query_string = $qs_match[0]; $query_string = $qs_match[0];
$request = preg_replace( $qs_regex, '', $request ); $request = preg_replace( $qs_regex, '', $request );
@ -2442,17 +2445,21 @@ function get_pagenum_link( $pagenum = 1, $escape = true ) {
$request = preg_replace( '|^' . preg_quote( $wp_rewrite->index, '|' ) . '|i', '', $request ); $request = preg_replace( '|^' . preg_quote( $wp_rewrite->index, '|' ) . '|i', '', $request );
$request = ltrim( $request, '/' ); $request = ltrim( $request, '/' );
$base = trailingslashit( get_bloginfo( 'url' ) );
if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' !== $request ) ) { if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' !== $request ) ) {
$base .= $wp_rewrite->index . '/'; $parts[] = $wp_rewrite->index;
} }
$parts[] = untrailingslashit( $request );
if ( $pagenum > 1 ) { if ( $pagenum > 1 ) {
$request = ( ( ! empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( $wp_rewrite->pagination_base . '/' . $pagenum, 'paged' ); $parts[] = $wp_rewrite->pagination_base;
$parts[] = $pagenum;
} }
$result = $base . $request . $query_string; $result = user_trailingslashit( implode( '/', array_filter( $parts ) ), 'paged' );
if ( ! empty( $query_string ) ) {
$result .= $query_string;
}
} }
/** /**

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.4-beta4-56938'; $wp_version = '6.4-beta4-56939';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.