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

@ -4725,12 +4725,14 @@ class WP_Query {
*
* @since 2.1.0
*
* @global WP_Query $wp_query Global WP_Query instance.
* @global wpdb $wpdb WordPress database abstraction object.
* @global WP_Query $wp_query Global WP_Query instance.
* @global wpdb $wpdb WordPress database abstraction object.
* @global WP_Rewrite $wp_rewrite WordPress rewrite component.
*/
function wp_old_slug_redirect() {
global $wp_query;
if ( is_404() && '' != $wp_query->query_vars['name'] ) :
global $wp_query, $wp_rewrite;
if ( '' !== $wp_query->query_vars['name'] ) :
global $wpdb;
// Guess the current post_type based on the query vars.
@ -4767,10 +4769,33 @@ function wp_old_slug_redirect() {
if ( ! $id )
return;
$link = get_permalink($id);
$link = get_permalink( $id );
if ( !$link )
if ( is_feed() ) {
$link = user_trailingslashit( trailingslashit( $link ) . 'feed' );
} elseif ( isset( $GLOBALS['wp_query']->query_vars['paged'] ) && $GLOBALS['wp_query']->query_vars['paged'] > 1 ) {
$link = user_trailingslashit( trailingslashit( $link ) . 'page/' . $GLOBALS['wp_query']->query_vars['paged'] );
} elseif ( is_404() ) {
// Add rewrite endpoints if necessary.
foreach ( $wp_rewrite->endpoints as $endpoint ) {
if ( $endpoint[2] && false !== get_query_var( $endpoint[2], false ) ) {
$link = user_trailingslashit( trailingslashit( $link ) . $endpoint[1] );
}
}
}
/**
* Filter the old slug redirect URL.
*
* @since 4.4.0
*
* @param string $link The redirect URL.
*/
$link = apply_filters( 'old_slug_redirect_url', $link );
if ( ! $link ) {
return;
}
wp_redirect( $link, 301 ); // Permanent redirect
exit;

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;
}
}