Skip to content
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
3 changes: 2 additions & 1 deletion src/wp-includes/canonical.php
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,8 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
$redirect['query'] = remove_query_arg( 'paged', $redirect['query'] );

if ( ! is_feed() ) {
if ( ! is_single() ) {
// Singular posts and pages do not support the 'paged' query var.
if ( ! is_singular() ) {
$addl_path = ! empty( $addl_path ) ? trailingslashit( $addl_path ) : '';

if ( $paged > 1 ) {
Expand Down
119 changes: 119 additions & 0 deletions tests/phpunit/tests/canonical/paged.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,123 @@ public function test_redirect_canonical_with_nextpage_pagination() {
// Non-existing page should redirect to the permalink.
$this->assertCanonical( $link . '4/', $link );
}

/**
* Tests that paged requests for pages redirect to the page permalink.
*
* Pages do not support the `paged` query var, so `/page/{n}/` appended
* to a page URL should not serve the page content under another URL.
*
* @ticket 28081
*
* @dataProvider data_redirect_canonical_paged_requests_for_pages
*
* @param string $test_url Test URL.
* @param string $expected Expected URL after the canonical redirect.
*/
public function test_redirect_canonical_paged_requests_for_pages( $test_url, $expected ) {
self::factory()->post->create(
array(
'post_type' => 'page',
'post_name' => 'paged-page',
'post_title' => 'Paged Page',
)
);

$this->assertCanonical( $test_url, $expected );
}

/**
* Data provider.
*
* @return array[]
*/
public function data_redirect_canonical_paged_requests_for_pages() {
return array(
'pretty paged URL, page 2' => array( '/paged-page/page/2/', '/paged-page/' ),
'pretty paged URL, large page' => array( '/paged-page/page/2323/', '/paged-page/' ),
'pretty paged URL, page 1' => array( '/paged-page/page/1/', '/paged-page/' ),
'paged query string' => array( '/paged-page/?paged=2', '/paged-page/' ),
'paged query string, extra arg' => array( '/paged-page/page/2/?foo=bar', '/paged-page/?foo=bar' ),
);
}

/**
* Tests that paged requests for a child page redirect to the child page permalink.
*
* @ticket 28081
*/
public function test_redirect_canonical_paged_request_for_child_page() {
$parent_id = self::factory()->post->create(
array(
'post_type' => 'page',
'post_name' => 'paged-parent',
)
);
self::factory()->post->create(
array(
'post_type' => 'page',
'post_name' => 'paged-child',
'post_parent' => $parent_id,
)
);

$this->assertCanonical( '/paged-parent/paged-child/page/3/', '/paged-parent/paged-child/' );
}

/**
* Tests that comment pagination is preserved when the `paged` query var is stripped from a page.
*
* @ticket 28081
*/
public function test_redirect_canonical_paged_request_for_page_keeps_comment_pagination() {
update_option( 'page_comments', 1 );
update_option( 'default_comments_page', 'oldest' );

self::factory()->post->create(
array(
'post_type' => 'page',
'post_name' => 'paged-comments-page',
)
);

$this->assertCanonical(
'/paged-comments-page/comment-page-2/?paged=2',
'/paged-comments-page/comment-page-2/'
);
}

/**
* Tests that paged requests for single posts still redirect to the post permalink.
*
* @ticket 28081
*/
public function test_redirect_canonical_paged_request_for_single_post() {
$post_id = self::factory()->post->create( array( 'post_name' => 'paged-single-post' ) );

$link = parse_url( get_permalink( $post_id ), PHP_URL_PATH );

$this->assertCanonical( $link . 'page/2/', $link );
}

/**
* Tests that paged requests for the posts page are left alone.
*
* @ticket 28081
*/
public function test_redirect_canonical_paged_request_for_posts_page_is_not_redirected() {
$page_id = self::factory()->post->create(
array(
'post_type' => 'page',
'post_name' => 'paged-blog',
)
);
self::factory()->post->create_many( 3 );

update_option( 'show_on_front', 'page' );
update_option( 'page_for_posts', $page_id );
update_option( 'posts_per_page', 1 );

$this->assertCanonical( '/paged-blog/page/2/', '/paged-blog/page/2/' );
}
}
Loading