From 00589684999669aedb8b97ade0ffa7b0509ff065 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Wed, 23 Mar 2016 21:53:44 +0000 Subject: [PATCH] Canonical / Query: Restore the `is_404()` check in `wp_old_slug_redirect()` which was removed in [34659]. This reverts part of [34659] due to excessive canonical problems it's caused in 4.4.x. Remove the unit tests which are no longer supported. This also removes the `is_feed()` code to avoid confusion - only pages & embeds will be redirected. Merges [36280] and [36281] to trunk. Props dd32. See #21602, #35344. git-svn-id: https://develop.svn.wordpress.org/trunk@37075 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/query.php | 20 ++------ .../phpunit/tests/rewrite/oldSlugRedirect.php | 47 ------------------- 2 files changed, 3 insertions(+), 64 deletions(-) diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php index 7bebfb4932..988787a96e 100644 --- a/src/wp-includes/query.php +++ b/src/wp-includes/query.php @@ -4885,16 +4885,11 @@ class WP_Query { * * @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, $wp_rewrite; + global $wp_query; - if ( get_queried_object() ) { - return; - } - - if ( '' !== $wp_query->query_vars['name'] ) : + if ( is_404() && '' !== $wp_query->query_vars['name'] ) : global $wpdb; // Guess the current post_type based on the query vars. @@ -4936,19 +4931,10 @@ function wp_old_slug_redirect() { $link = get_permalink( $id ); - if ( is_feed() ) { - $link = user_trailingslashit( trailingslashit( $link ) . 'feed' ); - } elseif ( isset( $GLOBALS['wp_query']->query_vars['paged'] ) && $GLOBALS['wp_query']->query_vars['paged'] > 1 ) { + if ( 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_embed() ) { $link = user_trailingslashit( trailingslashit( $link ) . 'embed' ); - } 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] ); - } - } } /** diff --git a/tests/phpunit/tests/rewrite/oldSlugRedirect.php b/tests/phpunit/tests/rewrite/oldSlugRedirect.php index da74548403..8ab0eb0fca 100644 --- a/tests/phpunit/tests/rewrite/oldSlugRedirect.php +++ b/tests/phpunit/tests/rewrite/oldSlugRedirect.php @@ -50,53 +50,6 @@ class Tests_Rewrite_OldSlugRedirect extends WP_UnitTestCase { $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 = self::factory()->attachment->create_object( $file, $this->post_id, array(