Rewrite: When redirecting old slugs, include URL endpoints.

Historically, `wp_old_slug_redirect()` has only ever redirected the old slug of posts, it hasn't included URL endpoints, or worked with comment feed URLs. By adding support for these, we ensure a greater range of URLs aren't killed when the slug changes.

Props swissspdy.

Fixes #33920.



git-svn-id: https://develop.svn.wordpress.org/trunk@34659 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Gary Pendergast
2015-09-28 06:56:54 +00:00
parent b59df508cf
commit 6d87c4cecb
2 changed files with 181 additions and 6 deletions

View File

@@ -0,0 +1,150 @@
<?php
/**
* @group rewrite
* @ticket 33920
*/
class Tests_Rewrite_OldSlugRedirect extends WP_UnitTestCase {
protected $old_slug_redirect_url;
protected $post_id;
public function setUp() {
parent::setUp();
$this->post_id = $this->factory->post->create( array(
'post_title' => 'Foo Bar',
'post_name' => 'foo-bar',
) );
add_filter( 'old_slug_redirect_url', array( $this, 'filter_old_slug_redirect_url' ), 10, 1 );
global $wp_rewrite;
$wp_rewrite->init();
$wp_rewrite->set_permalink_structure( '/%postname%/' );
add_rewrite_endpoint( 'custom-endpoint', EP_PERMALINK );
add_rewrite_endpoint( 'second-endpoint', EP_PERMALINK, 'custom' );
$wp_rewrite->flush_rules();
}
public function tearDown() {
parent::tearDown();
$this->old_slug_redirect_url = null;
remove_filter( 'old_slug_redirect_url', array( $this, 'filter_old_slug_redirect_url' ), 10 );
global $wp_rewrite;
$wp_rewrite->set_permalink_structure( '' );
$wp_rewrite->init();
}
public function test_old_slug_redirect() {
$old_permalink = user_trailingslashit( get_permalink( $this->post_id ) );
wp_update_post( array(
'ID' => $this->post_id,
'post_name' => 'bar-baz',
) );
$permalink = user_trailingslashit( get_permalink( $this->post_id ) );
$this->go_to( $old_permalink );
wp_old_slug_redirect();
$this->assertEquals( $permalink, $this->old_slug_redirect_url );
}
public function test_old_slug_redirect_endpoint() {
$old_permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'custom-endpoint' );
wp_update_post( array(
'ID' => $this->post_id,
'post_name' => 'bar-baz',
) );
$permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'custom-endpoint' );
$this->go_to( $old_permalink );
$GLOBALS['wp_query']->query_vars['custom-endpoint'] = true;
wp_old_slug_redirect();
$this->assertEquals( $permalink, $this->old_slug_redirect_url );
}
public function test_old_slug_redirect_endpoint_custom_query_var() {
$old_permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'second-endpoint' );
wp_update_post( array(
'ID' => $this->post_id,
'post_name' => 'bar-baz',
) );
$permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'second-endpoint' );
$this->go_to( $old_permalink );
$GLOBALS['wp_query']->query_vars['custom'] = true;
wp_old_slug_redirect();
$this->assertEquals( $permalink, $this->old_slug_redirect_url );
}
public function test_old_slug_redirect_feed() {
$old_permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'feed' );
wp_update_post( array(
'ID' => $this->post_id,
'post_name' => 'bar-baz',
) );
$permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'feed' );
$this->go_to( $old_permalink );
wp_old_slug_redirect();
$this->assertEquals( $permalink, $this->old_slug_redirect_url );
}
public function test_old_slug_redirect_attachment() {
$file = DIR_TESTDATA . '/images/canola.jpg';
$attachment_id = $this->factory->attachment->create_object( $file, $this->post_id, array(
'post_mime_type' => 'image/jpeg',
'post_name' => 'my-attachment',
) );
$old_permalink = get_attachment_link( $attachment_id );
wp_update_post( array(
'ID' => $this->post_id,
'post_name' => 'bar-baz',
) );
$this->go_to( $old_permalink );
wp_old_slug_redirect();
$this->assertNull( $this->old_slug_redirect_url );
$this->assertQueryTrue( 'is_attachment', 'is_singular', 'is_single' );
}
public function test_old_slug_redirect_paged() {
wp_update_post( array(
'ID' => $this->post_id,
'post_content' => 'Test<!--nextpage-->Test',
) );
$old_permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'page/2' );
wp_update_post( array(
'ID' => $this->post_id,
'post_name' => 'bar-baz',
) );
$permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'page/2' );
$this->go_to( $old_permalink );
wp_old_slug_redirect();
$this->assertEquals( $permalink, $this->old_slug_redirect_url );
}
public function filter_old_slug_redirect_url( $url ) {
$this->old_slug_redirect_url = $url;
return false;
}
}