From 635d3bb34e8e3c06f6d8729105f9395816809ee3 Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Mon, 28 Sep 2015 06:57:26 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@34659 git-svn-id: http://core.svn.wordpress.org/trunk@34623 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/query.php | 37 +++++++++++++++++++++++++++++++------ wp-includes/version.php | 2 +- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/wp-includes/query.php b/wp-includes/query.php index 7abd4d4d0e..28cf56f74a 100644 --- a/wp-includes/query.php +++ b/wp-includes/query.php @@ -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; diff --git a/wp-includes/version.php b/wp-includes/version.php index 3f7caa87e1..4ff0633cea 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.4-alpha-34658'; +$wp_version = '4.4-alpha-34659'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.