Make WordPress Core

Changeset 56939

Timestamp:
10/16/2023 12:05:28 AM (10 months ago)
Author:
costdev
Message:

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.

Location:
trunk
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/link-template.php

    r56767 r56939  
    24322432        preg_match( $qs_regex, $request, $qs_match );
    24332433
     2434
     2435
     2436
    24342437        if ( ! empty( $qs_match[0] ) ) {
    24352438            $query_string = $qs_match[0];
     
    24432446        $request = ltrim( $request, '/' );
    24442447
    2445         $base = trailingslashit( get_bloginfo( 'url' ) );
    2446 
    24472448        if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' !== $request ) ) {
    2448             $base .= $wp_rewrite->index . '/';
    2449         }
     2449            $parts[] = $wp_rewrite->index;
     2450        }
     2451
     2452        $parts[] = untrailingslashit( $request );
    24502453
    24512454        if ( $pagenum > 1 ) {
    2452             $request = ( ( ! empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( $wp_rewrite->pagination_base . '/' . $pagenum, 'paged' );
    2453         }
    2454 
    2455         $result = $base . $request . $query_string;
     2455            $parts[] = $wp_rewrite->pagination_base;
     2456            $parts[] = $pagenum;
     2457        }
     2458
     2459        $result = user_trailingslashit( implode( '/', array_filter( $parts ) ), 'paged' );
     2460        if ( ! empty( $query_string ) ) {
     2461            $result .= $query_string;
     2462        }
    24562463    }
    24572464
  • trunk/tests/phpunit/tests/link.php

    r52010 r56939  
    44 */
    55class Tests_Link extends WP_UnitTestCase {
    6 
    7     public function get_pagenum_link_cb( $url ) {
    8         return $url . '/WooHoo';
    9     }
    10 
    11     /**
    12      * @ticket 8847
    13      */
    14     public function test_get_pagenum_link_case_insensitivity() {
    15         $old_req_uri = $_SERVER['REQUEST_URI'];
    16 
    17         $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
    18 
    19         add_filter( 'home_url', array( $this, 'get_pagenum_link_cb' ) );
    20         $_SERVER['REQUEST_URI'] = '/woohoo';
    21         $paged                  = get_pagenum_link( 2 );
    22 
    23         remove_filter( 'home_url', array( $this, 'get_pagenum_link_cb' ) );
    24         $this->assertSame( $paged, home_url( '/WooHoo/page/2/' ) );
    25 
    26         $_SERVER['REQUEST_URI'] = $old_req_uri;
    27     }
    286
    297    public function test_wp_get_shortlink() {
Note: See TracChangeset for help on using the changeset viewer.