Make WordPress Core

Changeset 20395


Ignore:
Timestamp:
04/07/2012 01:03:55 AM (13 years ago)
Author:
markjaquith
Message:

Better removal of query args in canonical redirects. Only remove them when they are not present in the redirect_url. fixes #20374

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/canonical.php

    r20386 r20395  
    7676
    7777            if ( $redirect_url = get_permalink($id) )
    78                 $redirect['query'] = remove_query_arg(array('p', 'page_id', 'attachment_id', 'post_type'), $redirect['query']);
     78                $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url );
    7979        }
    8080    }
     
    8989            if ( $post_type_obj->public ) {
    9090                $redirect_url = get_permalink($redirect_post);
    91                 $redirect['query'] = remove_query_arg(array('p', 'page_id', 'attachment_id', 'post_type'), $redirect['query']);
     91                $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url );
    9292            }
    9393        }
    9494
    9595        if ( ! $redirect_url ) {
    96             $redirect_url = redirect_guess_404_permalink( $requested_url );
    97             $redirect['query'] = remove_query_arg( array( 'post_type', 'pagename', 'name' ), $redirect['query'] );
     96            if ( $redirect_url = redirect_guess_404_permalink( $requested_url ) ) {
     97                $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url );
     98            }
    9899        }
    99100
     
    420421
    421422/**
     423 * Removes arguments from a query string if they are not present in a URL
     424 * DO NOT use this in plugin code.
     425 *
     426 * @since 3.4
     427 * @access private
     428 *
     429 * @return string The altered query string
     430 */
     431function _remove_qs_args_if_not_in_url( $query_string, Array $args_to_check, $url ) {
     432    $parsed_url = @parse_url( $url );
     433    parse_str( $parsed_url['query'], $parsed_query );
     434    foreach ( $args_to_check as $qv ) {
     435        if ( !isset( $parsed_query[$qv] ) )
     436            $query_string = remove_query_arg( $qv, $query_string );
     437    }
     438    return $query_string;
     439}
     440
     441/**
    422442 * Attempts to guess the correct URL from the current URL (that produced a 404) or
    423443 * the current query variables.
Note: See TracChangeset for help on using the changeset viewer.