Make WordPress Core

Ticket #17174: 17174.diff

File 17174.diff, 3.5 KB (added by dd32, 14 years ago)
  • wp-includes/canonical.php

     
    154154                        $obj = $wp_query->get_queried_object();
    155155                        if ( $term_count <= 1 && !empty($obj->term_id) && ( $tax_url = get_term_link((int)$obj->term_id, $obj->taxonomy) ) && !is_wp_error($tax_url) ) {
    156156                                if ( !empty($redirect['query']) ) {
     157                                        // Strip taxonomy query vars off the url.
     158                                        $qv_remove = array( 'term', 'taxonomy');
    157159                                        if ( is_category() ) {
    158                                                 $redirect['query'] = remove_query_arg( array( 'category_name', 'category', 'cat'), $redirect['query']);
     160                                                $qv_remove += array( 'category_name', 'category', 'cat');
    159161                                        } elseif ( is_tag() ) {
    160                                                 $redirect['query'] = remove_query_arg( array( 'tag', 'tag_id'), $redirect['query']);
    161                                         } elseif ( is_tax() ) { // Custom taxonomies will have a custom query var, remove those too:
    162                                                 $tax = get_taxonomy( $obj->taxonomy );
    163                                                 if ( false !== $tax->query_var)
    164                                                         $redirect['query'] = remove_query_arg($tax->query_var, $redirect['query']);
    165                                                 else
    166                                                         $redirect['query'] = remove_query_arg( array( 'term', 'taxonomy'), $redirect['query']);
     162                                                $qv_remove += array( 'tag', 'tag_id');
     163                                        } else { // Custom taxonomies will have a custom query var, remove those too:
     164                                                $tax_obj = get_taxonomy( $obj->taxonomy );
     165                                                if ( false !== $tax_obj->query_var)
     166                                                        $qv_remove += array($tax_obj->query_var);
    167167                                        }
    168                                 }
    169168
    170                                 $tax_obj = get_taxonomy( $obj->taxonomy );
    171                                 $tax_query_vars = array_diff( array_keys( $wp_query->query ), array_keys( $_GET ), array( 'taxonomy', 'term', $tax_obj->query_var ) );
     169                                        $rewrite_vars = array_diff( array_keys($wp_query->query), array_keys($_GET) );
    172170
    173                                 $tax_url = parse_url($tax_url);
    174                                 if ( ! empty($tax_query_vars) ) {
    175                                         foreach ( array( 'taxonomy', 'term', $tax_obj->query_var ) as $qv ) {
    176                                                 if ( !empty($wp_query->query[$qv]) )
    177                                                         $redirect['query'] = add_query_arg($qv, $wp_query->query[$qv], $redirect['query']);
     171                                        if ( !array_diff($rewrite_vars, array_keys($_GET))  ) { // Check to see if all the Query vars are coming from the rewrite, none are set via $_GET
     172                                                $redirect['query'] = remove_query_arg($qv_remove, $redirect['query']); //Remove all of the per-tax qv's
     173
     174                                                // Create the destination url for this taxonomy
     175                                                $tax_url = parse_url($tax_url);
     176                                                if ( ! empty($tax_url['query']) ) { // Taxonomy accessable via ?taxonomy=..&term=.. or any custom qv..
     177                                                        parse_str($tax_url['query'], $query_vars);
     178                                                        $redirect['query'] = add_query_arg($query_vars, $redirect['query']);
     179                                                } else { // Taxonomy is accessable via a "pretty-URL"
     180                                                        $redirect['path'] = $tax_url['path'];
     181                                                }
     182
     183                                        } else { // Some query vars are set via $_GET. Unset those from $_GET that exist via the rewrite
     184                                                foreach ( $qv_remove as $_qv ) {
     185                                                        if ( isset($rewrite_vars[$_qv]) )
     186                                                                $redirect['query'] = remove_query_arg($_qv, $redirect['query']);
     187                                                }
    178188                                        }
    179                                                
    180                                 } elseif ( ! empty($tax_url['query']) ) { // Custom taxonomies may only be accessable via ?taxonomy=..&term=..
    181                                         parse_str($tax_url['query'], $query_vars);
    182                                         $redirect['query'] = add_query_arg($query_vars, $redirect['query']);
    183                                 } else { // Taxonomy is accessable via a "pretty-URL"
    184                                         $redirect['path'] = $tax_url['path'];
    185189                                }
     190
    186191                        }
    187192                } elseif ( is_single() && strpos($wp_rewrite->permalink_structure, '%category%') !== false ) {
    188193                        $category = get_category_by_path(get_query_var('category_name'));