Make WordPress Core


Ignore:
File:
1 edited

Legend:

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

    r15708 r17031  
    3535 *              not needed or the string of the URL
    3636 */
    37 function redirect_canonical($requested_url=null, $do_redirect=true) {
    38         global $wp_rewrite, $is_IIS, $wp_query, $wpdb;
    39 
    40         if ( is_trackback() || is_search() || is_comments_popup() || is_admin() || $is_IIS || ( isset($_POST) && count($_POST) ) || is_preview() || is_robots() )
     37function redirect_canonical( $requested_url = null, $do_redirect = true ) {
     38        global $wp_rewrite, $is_iis7, $wp_query, $wpdb;
     39
     40        if ( is_trackback() || is_search() || is_comments_popup() || is_admin() || !empty($_POST) || is_preview() || is_robots() || ( $is_iis7 && !iis7_supports_permalinks() ) )
    4141                return;
    4242
     
    147147
    148148                        $term_count = 0;
    149                         foreach ( array('category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in',
    150                         'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and') as $key )
    151                                 $term_count += count($wp_query->query_vars[$key]);
     149                        foreach ( $wp_query->tax_query->queries as $tax_query )
     150                                $term_count += count( $tax_query['terms'] );
    152151
    153152                        $obj = $wp_query->get_queried_object();
    154 
    155                         if ( $term_count <= 1 && !empty($obj->term_id) && ( $tax_url = get_term_link((int)$obj->term_id, $obj->taxonomy) )
    156                                         && !is_wp_error($tax_url) && $redirect['query'] ) {
    157                                 if ( is_category() ) {
    158                                         $redirect['query'] = remove_query_arg( array( 'category_name', 'category', 'cat'), $redirect['query']);
    159                                 } 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']);
     153                        if ( $term_count <= 1 && !empty($obj->term_id) && ( $tax_url = get_term_link((int)$obj->term_id, $obj->taxonomy) ) && !is_wp_error($tax_url) ) {
     154                                if ( !empty($redirect['query']) ) {
     155                                        if ( is_category() ) {
     156                                                $redirect['query'] = remove_query_arg( array( 'category_name', 'category', 'cat'), $redirect['query']);
     157                                        } elseif ( is_tag() ) {
     158                                                $redirect['query'] = remove_query_arg( array( 'tag', 'tag_id'), $redirect['query']);
     159                                        } elseif ( is_tax() ) { // Custom taxonomies will have a custom query var, remove those too:
     160                                                $tax = get_taxonomy( $obj->taxonomy );
     161                                                if ( false !== $tax->query_var)
     162                                                        $redirect['query'] = remove_query_arg($tax->query_var, $redirect['query']);
     163                                                else
     164                                                        $redirect['query'] = remove_query_arg( array( 'term', 'taxonomy'), $redirect['query']);
     165                                        }
    167166                                }
    168 
    169167                                $tax_url = parse_url($tax_url);
    170168                                if ( ! empty($tax_url['query']) ) { // Custom taxonomies may only be accessable via ?taxonomy=..&term=..
     
    174172                                        $redirect['path'] = $tax_url['path'];
    175173                                }
    176 
    177174                        }
    178175                } elseif ( is_single() && strpos($wp_rewrite->permalink_structure, '%category%') !== false ) {
     
    191188                // paging and feeds
    192189                if ( get_query_var('paged') || is_feed() || get_query_var('cpage') ) {
    193                         if ( !$redirect_url )
    194                                 $redirect_url = $requested_url;
    195                         $paged_redirect = @parse_url($redirect_url);
    196                         while ( preg_match( '#/page/?[0-9]+?(/+)?$#', $paged_redirect['path'] ) || preg_match( '#/(comments/?)?(feed|rss|rdf|atom|rss2)(/+)?$#', $paged_redirect['path'] ) || preg_match( '#/comment-page-[0-9]+(/+)?$#', $paged_redirect['path'] ) ) {
     190                        while ( preg_match( "#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", $redirect['path'] ) || preg_match( '#/(comments/?)?(feed|rss|rdf|atom|rss2)(/+)?$#', $redirect['path'] ) || preg_match( '#/comment-page-[0-9]+(/+)?$#', $redirect['path'] ) ) {
    197191                                // Strip off paging and feed
    198                                 $paged_redirect['path'] = preg_replace('#/page/?[0-9]+?(/+)?$#', '/', $paged_redirect['path']); // strip off any existing paging
    199                                 $paged_redirect['path'] = preg_replace('#/(comments/?)?(feed|rss2?|rdf|atom)(/+|$)#', '/', $paged_redirect['path']); // strip off feed endings
    200                                 $paged_redirect['path'] = preg_replace('#/comment-page-[0-9]+?(/+)?$#', '/', $paged_redirect['path']); // strip off any existing comment paging
     192                                $redirect['path'] = preg_replace("#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", '/', $redirect['path']); // strip off any existing paging
     193                                $redirect['path'] = preg_replace('#/(comments/?)?(feed|rss2?|rdf|atom)(/+|$)#', '/', $redirect['path']); // strip off feed endings
     194                                $redirect['path'] = preg_replace('#/comment-page-[0-9]+?(/+)?$#', '/', $redirect['path']); // strip off any existing comment paging
    201195                        }
    202196
     
    215209                                if ( !is_feed() ) {
    216210                                        if ( $paged > 1 && !is_single() ) {
    217                                                 $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit("page/$paged", 'paged');
     211                                                $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit("$wp_rewrite->pagination_base/$paged", 'paged');
    218212                                        } elseif ( !is_single() ) {
    219213                                                $addl_path = !empty( $addl_path ) ? trailingslashit($addl_path) : '';
     
    229223                        }
    230224
    231                         $paged_redirect['path'] = user_trailingslashit( preg_replace('|/index.php/?$|', '/', $paged_redirect['path']) ); // strip off trailing /index.php/
    232                         if ( !empty( $addl_path ) && $wp_rewrite->using_index_permalinks() && strpos($paged_redirect['path'], '/index.php/') === false )
    233                                 $paged_redirect['path'] = trailingslashit($paged_redirect['path']) . 'index.php/';
     225                        $redirect['path'] = user_trailingslashit( preg_replace('|/index.php/?$|', '/', $redirect['path']) ); // strip off trailing /index.php/
     226                        if ( !empty( $addl_path ) && $wp_rewrite->using_index_permalinks() && strpos($redirect['path'], '/index.php/') === false )
     227                                $redirect['path'] = trailingslashit($redirect['path']) . 'index.php/';
    234228                        if ( !empty( $addl_path ) )
    235                                 $paged_redirect['path'] = trailingslashit($paged_redirect['path']) . $addl_path;
    236                         $redirect_url = $paged_redirect['scheme'] . '://' . $paged_redirect['host'] . $paged_redirect['path'];
    237                         $redirect['path'] = $paged_redirect['path'];
     229                                $redirect['path'] = trailingslashit($redirect['path']) . $addl_path;
     230                        $redirect_url = $redirect['scheme'] . '://' . $redirect['host'] . $redirect['path'];
    238231                }
    239232        }
     
    242235        $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] );
    243236        if ( $redirect_url && !empty($redirect['query']) ) {
    244                 if ( strpos($redirect_url, '?') !== false )
    245                         $redirect_url .= '&';
    246                 else
    247                         $redirect_url .= '?';
    248                 $redirect_url .= $redirect['query'];
     237                parse_str( $redirect['query'], $_parsed_query );
     238                $redirect = @parse_url($redirect_url);
     239
     240                if ( ! empty( $_parsed_query['name'] ) && ! empty( $redirect['query'] ) ) {
     241                        parse_str( $redirect['query'], $_parsed_redirect_query );
     242
     243                        if ( empty( $_parsed_redirect_query['name'] ) )
     244                                unset( $_parsed_query['name'] );
     245                }
     246
     247                $redirect_url = add_query_arg( $_parsed_query, $redirect_url );
    249248        }
    250249
     
    346345        if ( !$redirect_url || $redirect_url == $requested_url )
    347346                return false;
    348                
    349         // Hex encoded octets are case-insensitive. 
     347
     348        // Hex encoded octets are case-insensitive.
    350349        if ( false !== strpos($requested_url, '%') ) {
    351350                if ( !function_exists('lowercase_octets') ) {
    352                         function lowercase_octets($matches) { 
    353                                 return strtolower( $matches[0] ); 
    354                         } 
     351                        function lowercase_octets($matches) {
     352                                return strtolower( $matches[0] );
     353                        }
    355354                }
    356355                $requested_url = preg_replace_callback('|%[a-fA-F0-9][a-fA-F0-9]|', 'lowercase_octets', $requested_url);
     
    393392                return false;
    394393
    395         $where = $wpdb->prepare("post_name LIKE %s", get_query_var('name') . '%');
     394        $where = $wpdb->prepare("post_name LIKE %s", like_escape( get_query_var('name') ) . '%');
    396395
    397396        // if any of post_type, year, monthnum, or day are set, use them to refine the query
Note: See TracChangeset for help on using the changeset viewer.