From a9dcce70602ab9acc02fd1c049b4539a2217e73d Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Thu, 31 Dec 2015 03:00:33 +0000 Subject: [PATCH] Redirects: Prevent redirects if a queried object exists. After [34659], it became possible to cause an incorrect redirect, by changing the slug of a post, then creating a new post with the old slug. The correct behaviour is to prevent redirecting to the old post. Props dd32, pento. Fixes #35031 for trunk. git-svn-id: https://develop.svn.wordpress.org/trunk@36128 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/query.php | 4 +++ .../phpunit/tests/rewrite/oldSlugRedirect.php | 25 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php index 881b9959c6..96148ba20e 100644 --- a/src/wp-includes/query.php +++ b/src/wp-includes/query.php @@ -4956,6 +4956,10 @@ class WP_Query { function wp_old_slug_redirect() { global $wp_query, $wp_rewrite; + if ( get_queried_object() ) { + return; + } + if ( '' !== $wp_query->query_vars['name'] ) : global $wpdb; diff --git a/tests/phpunit/tests/rewrite/oldSlugRedirect.php b/tests/phpunit/tests/rewrite/oldSlugRedirect.php index 71920132e9..da74548403 100644 --- a/tests/phpunit/tests/rewrite/oldSlugRedirect.php +++ b/tests/phpunit/tests/rewrite/oldSlugRedirect.php @@ -150,6 +150,31 @@ class Tests_Rewrite_OldSlugRedirect extends WP_UnitTestCase { $this->assertEquals( $permalink, $this->old_slug_redirect_url ); } + /** + * @ticket 35031 + */ + public function test_old_slug_doesnt_redirect_when_reused() { + $old_permalink = user_trailingslashit( get_permalink( $this->post_id ) ); + + wp_update_post( array( + 'ID' => $this->post_id, + 'post_name' => 'bar-baz', + ) ); + + $new_post_id = self::factory()->post->create( array( + 'post_title' => 'Foo Bar', + 'post_name' => 'foo-bar', + ) ); + + $permalink = user_trailingslashit( get_permalink( $new_post_id ) ); + + $this->assertEquals( $old_permalink, $permalink ); + + $this->go_to( $old_permalink ); + wp_old_slug_redirect(); + $this->assertNull( $this->old_slug_redirect_url ); + } + public function filter_old_slug_redirect_url( $url ) { $this->old_slug_redirect_url = $url; return false;