Make WordPress Core

Changeset 18079


Ignore:
Timestamp:
05/31/2011 06:13:27 AM (15 years ago)
Author:
dd32
Message:

More specific Tazonomy canonicalisation; Allows custom vars through, strips all per-taxonomy query vars. See #17174

File:
1 edited

Legend:

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

    r17991 r18079  
    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[] = 'category_name';
     161                                                $qv_remove[] = 'cat';
    159162                                        } 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']);
     163                                                $qv_remove[] = 'tag';
     164                                                $qv_remove[] = 'tag_id';
     165                                        } else { // Custom taxonomies will have a custom query var, remove those too:
     166                                                $tax_obj = get_taxonomy( $obj->taxonomy );
     167                                                if ( false !== $tax_obj->query_var )
     168                                                        $qv_remove[] = $tax_obj->query_var;
     169                                        }
     170
     171                                        $rewrite_vars = array_diff( array_keys($wp_query->query), array_keys($_GET) );
     172
     173                                        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
     174                                                $redirect['query'] = remove_query_arg($qv_remove, $redirect['query']); //Remove all of the per-tax qv's
     175
     176                                                // Create the destination url for this taxonomy
     177                                                $tax_url = parse_url($tax_url);
     178                                                if ( ! empty($tax_url['query']) ) { // Taxonomy accessable via ?taxonomy=..&term=.. or any custom qv..
     179                                                        parse_str($tax_url['query'], $query_vars);
     180                                                        $redirect['query'] = add_query_arg($query_vars, $redirect['query']);
     181                                                } else { // Taxonomy is accessable via a "pretty-URL"
     182                                                        $redirect['path'] = $tax_url['path'];
     183                                                }
     184
     185                                        } else { // Some query vars are set via $_GET. Unset those from $_GET that exist via the rewrite
     186                                                foreach ( $qv_remove as $_qv ) {
     187                                                        if ( isset($rewrite_vars[$_qv]) )
     188                                                                $redirect['query'] = remove_query_arg($_qv, $redirect['query']);
     189                                                }
    167190                                        }
    168191                                }
    169192
    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 ) );
    172 
    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']);
    178                                         }
    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'];
    185                                 }
    186193                        }
    187194                } elseif ( is_single() && strpos($wp_rewrite->permalink_structure, '%category%') !== false ) {
Note: See TracChangeset for help on using the changeset viewer.