WordPress.org

Make WordPress Core

Changeset 38463


Ignore:
Timestamp:
08/31/2016 06:20:03 AM (3 years ago)
Author:
wonderboymusic
Message:

Query: in wp_old_slug_redirect(), use get_query_var() instead of importing and touching the global $wp_query directly.

See #37699.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/query.php

    r38351 r38463  
    845845 * @since 2.1.0
    846846 *
    847  * @global WP_Query   $wp_query   Global WP_Query instance.
    848  * @global wpdb       $wpdb       WordPress database abstraction object.
     847 * @global wpdb $wpdb WordPress database abstraction object.
    849848 */
    850849function wp_old_slug_redirect() {
    851     global $wp_query;
    852 
    853     if ( is_404() && '' !== $wp_query->query_vars['name'] ) :
     850    if ( is_404() && '' !== get_query_var( 'name' ) ) {
    854851        global $wpdb;
    855852
     
    859856        } elseif ( get_query_var( 'attachment' ) ) {
    860857            $post_type = 'attachment';
    861         } elseif ( ! empty( $wp_query->query_vars['pagename'] ) ) {
     858        } elseif ( get_query_var( 'pagename' ) ) {
    862859            $post_type = 'page';
    863860        } else {
     
    866863
    867864        if ( is_array( $post_type ) ) {
    868             if ( count( $post_type ) > 1 )
     865            if ( count( $post_type ) > 1 ) {
    869866                return;
     867            }
    870868            $post_type = reset( $post_type );
    871869        }
    872870
    873871        // Do not attempt redirect for hierarchical post types
    874         if ( is_post_type_hierarchical( $post_type ) )
     872        if ( is_post_type_hierarchical( $post_type ) ) {
    875873            return;
    876 
    877         $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']);
     874        }
     875
     876        $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' ) );
    878877
    879878        // if year, monthnum, or day have been specified, make our query more precise
    880879        // just in case there are multiple identical _wp_old_slug values
    881         if ( '' != $wp_query->query_vars['year'] )
    882             $query .= $wpdb->prepare(" AND YEAR(post_date) = %d", $wp_query->query_vars['year']);
    883         if ( '' != $wp_query->query_vars['monthnum'] )
    884             $query .= $wpdb->prepare(" AND MONTH(post_date) = %d", $wp_query->query_vars['monthnum']);
    885         if ( '' != $wp_query->query_vars['day'] )
    886             $query .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", $wp_query->query_vars['day']);
    887 
    888         $id = (int) $wpdb->get_var($query);
    889 
    890         if ( ! $id )
     880        if ( get_query_var( 'year' ) ) {
     881            $query .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var( 'year' ) );
     882        }
     883        if ( get_query_var( 'monthnum' ) ) {
     884            $query .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var( 'monthnum' ) );
     885        }
     886        if ( get_query_var( 'day' ) ) {
     887            $query .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var( 'day' ) );
     888        }
     889
     890        $id = (int) $wpdb->get_var( $query );
     891
     892        if ( ! $id ) {
    891893            return;
     894        }
    892895
    893896        $link = get_permalink( $id );
    894897
    895         if ( isset( $GLOBALS['wp_query']->query_vars['paged'] ) && $GLOBALS['wp_query']->query_vars['paged'] > 1 ) {
    896             $link = user_trailingslashit( trailingslashit( $link ) . 'page/' . $GLOBALS['wp_query']->query_vars['paged'] );
     898        if ( get_query_var( 'paged' ) > 1 ) {
     899            $link = user_trailingslashit( trailingslashit( $link ) . 'page/' . get_query_var( 'paged' ) );
    897900        } elseif( is_embed() ) {
    898901            $link = user_trailingslashit( trailingslashit( $link ) . 'embed' );
     
    914917        wp_redirect( $link, 301 ); // Permanent redirect
    915918        exit;
    916     endif;
     919    }
    917920}
    918921
Note: See TracChangeset for help on using the changeset viewer.