Make WordPress Core

Changeset 59966


Ignore:
Timestamp:
03/11/2025 11:10:00 AM (6 weeks ago)
Author:
audrasjb
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

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

    r59953 r59966  
    47144714        }
    47154715        $link .= $args['add_fragment'];
     4716        $link  = get_option( 'permalink_structure' ) ? user_trailingslashit( $link, 'paged' ) : $link;
    47164717
    47174718        $page_links[] = sprintf(
     
    47464747                }
    47474748                $link .= $args['add_fragment'];
     4749                $link  = get_option( 'permalink_structure' ) ? user_trailingslashit( $link, 'paged' ) : $link;
    47484750
    47494751                $page_links[] = sprintf(
     
    47704772        }
    47714773        $link .= $args['add_fragment'];
     4774        $link  = get_option( 'permalink_structure' ) ? user_trailingslashit( $link, 'paged' ) : $link;
    47724775
    47734776        $page_links[] = sprintf(
  • trunk/tests/phpunit/tests/general/paginateLinks.php

    r52010 r59966  
    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}
Note: See TracChangeset for help on using the changeset viewer.