From 153ba4a83223f76a0373afda5c4c3593a97a3014 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Wed, 31 Aug 2016 06:21:41 +0000 Subject: [PATCH] Query: in `wp_old_slug_redirect()`, use `get_query_var()` instead of importing and touching the global `$wp_query` directly. See #37699. Built from https://develop.svn.wordpress.org/trunk@38463 git-svn-id: http://core.svn.wordpress.org/trunk@38404 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/query.php | 43 ++++++++++++++++++++++------------------- wp-includes/version.php | 2 +- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/wp-includes/query.php b/wp-includes/query.php index d8468ffcff..744610f425 100644 --- a/wp-includes/query.php +++ b/wp-includes/query.php @@ -844,13 +844,10 @@ function the_comment() { * * @since 2.1.0 * - * @global WP_Query $wp_query Global WP_Query instance. - * @global wpdb $wpdb WordPress database abstraction object. + * @global wpdb $wpdb WordPress database abstraction object. */ function wp_old_slug_redirect() { - global $wp_query; - - if ( is_404() && '' !== $wp_query->query_vars['name'] ) : + if ( is_404() && '' !== get_query_var( 'name' ) ) { global $wpdb; // Guess the current post_type based on the query vars. @@ -858,42 +855,48 @@ function wp_old_slug_redirect() { $post_type = get_query_var( 'post_type' ); } elseif ( get_query_var( 'attachment' ) ) { $post_type = 'attachment'; - } elseif ( ! empty( $wp_query->query_vars['pagename'] ) ) { + } elseif ( get_query_var( 'pagename' ) ) { $post_type = 'page'; } else { $post_type = 'post'; } if ( is_array( $post_type ) ) { - if ( count( $post_type ) > 1 ) + if ( count( $post_type ) > 1 ) { return; + } $post_type = reset( $post_type ); } // Do not attempt redirect for hierarchical post types - if ( is_post_type_hierarchical( $post_type ) ) + if ( is_post_type_hierarchical( $post_type ) ) { return; + } - $query = $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta, $wpdb->posts WHERE ID = post_id AND post_type = %s AND meta_key = '_wp_old_slug' AND meta_value = %s", $post_type, $wp_query->query_vars['name']); + $query = $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta, $wpdb->posts WHERE ID = post_id AND post_type = %s AND meta_key = '_wp_old_slug' AND meta_value = %s", $post_type, get_query_var( 'name' ) ); // if year, monthnum, or day have been specified, make our query more precise // just in case there are multiple identical _wp_old_slug values - if ( '' != $wp_query->query_vars['year'] ) - $query .= $wpdb->prepare(" AND YEAR(post_date) = %d", $wp_query->query_vars['year']); - if ( '' != $wp_query->query_vars['monthnum'] ) - $query .= $wpdb->prepare(" AND MONTH(post_date) = %d", $wp_query->query_vars['monthnum']); - if ( '' != $wp_query->query_vars['day'] ) - $query .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", $wp_query->query_vars['day']); + if ( get_query_var( 'year' ) ) { + $query .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var( 'year' ) ); + } + if ( get_query_var( 'monthnum' ) ) { + $query .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var( 'monthnum' ) ); + } + if ( get_query_var( 'day' ) ) { + $query .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var( 'day' ) ); + } - $id = (int) $wpdb->get_var($query); + $id = (int) $wpdb->get_var( $query ); - if ( ! $id ) + if ( ! $id ) { return; + } $link = get_permalink( $id ); - 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'] ); + if ( get_query_var( 'paged' ) > 1 ) { + $link = user_trailingslashit( trailingslashit( $link ) . 'page/' . get_query_var( 'paged' ) ); } elseif( is_embed() ) { $link = user_trailingslashit( trailingslashit( $link ) . 'embed' ); } @@ -913,7 +916,7 @@ function wp_old_slug_redirect() { wp_redirect( $link, 301 ); // Permanent redirect exit; - endif; + } } /** diff --git a/wp-includes/version.php b/wp-includes/version.php index e64a31f19e..1908ec005a 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.7-alpha-38462'; +$wp_version = '4.7-alpha-38463'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.