Ticket #25229: wp-includes-canonical.2.diff
File wp-includes-canonical.2.diff, 29.2 KB (added by , 11 years ago) |
---|
-
wp-includes/canonical.php
39 39 function redirect_canonical( $requested_url = null, $do_redirect = true ) { 40 40 global $wp_rewrite, $is_IIS, $wp_query, $wpdb; 41 41 42 if ( is_trackback() || is_search() || is_comments_popup() || is_admin() || ! empty($_POST) || is_preview() || is_robots() || ( $is_IIS && !iis7_supports_permalinks() ) )42 if ( is_trackback() || is_search() || is_comments_popup() || is_admin() || ! empty($_POST) || is_preview() || is_robots() || ( $is_IIS && ! iis7_supports_permalinks() ) ) 43 43 return; 44 44 45 if ( ! $requested_url ) {45 if ( ! $requested_url ) { 46 46 // build the URL in the address bar 47 47 $requested_url = is_ssl() ? 'https://' : 'http://'; 48 48 $requested_url .= $_SERVER['HTTP_HOST']; 49 49 $requested_url .= $_SERVER['REQUEST_URI']; 50 50 } 51 51 52 $original = @parse_url( $requested_url);52 $original = @parse_url( $requested_url ); 53 53 if ( false === $original ) 54 54 return; 55 55 … … 63 63 $redirect_url = false; 64 64 65 65 // Notice fixing 66 if ( ! isset($redirect['path']) )66 if ( ! isset( $redirect['path'] ) ) 67 67 $redirect['path'] = ''; 68 if ( ! isset($redirect['query']) )68 if ( ! isset( $redirect['query'] ) ) 69 69 $redirect['query'] = ''; 70 70 71 71 if ( is_feed() && ( $id = get_query_var( 'p' ) ) ) { … … 75 75 } 76 76 } 77 77 78 if ( is_singular() && 1 > $wp_query->post_count && ( $id = get_query_var('p')) ) {78 if ( is_singular() && 1 > $wp_query->post_count && ( $id = get_query_var( 'p' ) ) ) { 79 79 80 $vars = $wpdb->get_results( $wpdb->prepare( "SELECT post_type, post_parent FROM $wpdb->posts WHERE ID = %d", $id) );80 $vars = $wpdb->get_results( $wpdb->prepare( "SELECT post_type, post_parent FROM $wpdb->posts WHERE ID = %d", $id ) ); 81 81 82 if ( isset( $vars[0]) && $vars = $vars[0] ) {82 if ( isset( $vars[0] ) && $vars = $vars[0] ) { 83 83 if ( 'revision' == $vars->post_type && $vars->post_parent > 0 ) 84 84 $id = $vars->post_parent; 85 85 … … 92 92 if ( is_404() ) { 93 93 94 94 // Redirect ?page_id, ?p=, ?attachment_id= to their respective url's 95 $id = max( get_query_var( 'p'), get_query_var('page_id'), get_query_var('attachment_id') );96 if ( $id && $redirect_post = get_post( $id) ) {97 $post_type_obj = get_post_type_object( $redirect_post->post_type);95 $id = max( get_query_var( 'p' ), get_query_var( 'page_id' ), get_query_var( 'attachment_id' ) ); 96 if ( $id && $redirect_post = get_post( $id ) ) { 97 $post_type_obj = get_post_type_object( $redirect_post->post_type ); 98 98 if ( $post_type_obj->public ) { 99 $redirect_url = get_permalink( $redirect_post);99 $redirect_url = get_permalink( $redirect_post ); 100 100 $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url ); 101 101 } 102 102 } … … 121 121 } 122 122 } 123 123 124 } elseif ( is_object( $wp_rewrite) && $wp_rewrite->using_permalinks() ) {124 } elseif ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) { 125 125 // rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101 126 if ( is_attachment() && ! empty($_GET['attachment_id']) && ! $redirect_url ) {127 if ( $redirect_url = get_attachment_link( get_query_var('attachment_id')) )128 $redirect['query'] = remove_query_arg( 'attachment_id', $redirect['query']);129 } elseif ( is_single() && ! empty($_GET['p']) && ! $redirect_url ) {130 if ( $redirect_url = get_permalink( get_query_var('p')) )131 $redirect['query'] = remove_query_arg( array('p', 'post_type'), $redirect['query']);132 } elseif ( is_single() && ! empty($_GET['name']) && ! $redirect_url ) {126 if ( is_attachment() && ! empty( $_GET['attachment_id'] ) && ! $redirect_url ) { 127 if ( $redirect_url = get_attachment_link( get_query_var( 'attachment_id' ) ) ) 128 $redirect['query'] = remove_query_arg( 'attachment_id', $redirect['query'] ); 129 } elseif ( is_single() && ! empty( $_GET['p'] ) && ! $redirect_url ) { 130 if ( $redirect_url = get_permalink( get_query_var( 'p' ) ) ) 131 $redirect['query'] = remove_query_arg( array( 'p', 'post_type' ), $redirect['query']); 132 } elseif ( is_single() && ! empty( $_GET['name'] ) && ! $redirect_url ) { 133 133 if ( $redirect_url = get_permalink( $wp_query->get_queried_object_id() ) ) 134 134 $redirect['query'] = remove_query_arg('name', $redirect['query']); 135 } elseif ( is_page() && ! empty($_GET['page_id']) && ! $redirect_url ) {136 if ( $redirect_url = get_permalink( get_query_var('page_id')) )137 $redirect['query'] = remove_query_arg( 'page_id', $redirect['query']);138 } elseif ( is_page() && ! is_feed() && isset($wp_query->queried_object) && 'page' == get_option('show_on_front') && $wp_query->queried_object->ID == get_option('page_on_front') && ! $redirect_url ) {139 $redirect_url = home_url( '/');140 } elseif ( is_home() && ! empty($_GET['page_id']) && 'page' == get_option('show_on_front') && get_query_var('page_id') == get_option('page_for_posts') && ! $redirect_url ) {141 if ( $redirect_url = get_permalink( get_option('page_for_posts')) )142 $redirect['query'] = remove_query_arg( 'page_id', $redirect['query']);143 } elseif ( ! empty($_GET['m']) && ( is_year() || is_month() || is_day() ) ) {144 $m = get_query_var( 'm');145 switch ( strlen( $m) ) {135 } elseif ( is_page() && ! empty( $_GET['page_id'] ) && ! $redirect_url ) { 136 if ( $redirect_url = get_permalink( get_query_var( 'page_id' ) ) ) 137 $redirect['query'] = remove_query_arg( 'page_id', $redirect['query'] ); 138 } elseif ( is_page() && ! is_feed() && isset( $wp_query->queried_object ) && 'page' == get_option( 'show_on_front' ) && $wp_query->queried_object->ID == get_option( 'page_on_front' ) && ! $redirect_url ) { 139 $redirect_url = home_url( '/' ); 140 } elseif ( is_home() && ! empty( $_GET['page_id'] ) && 'page' == get_option( 'show_on_front' ) && get_query_var( 'page_id' ) == get_option( 'page_for_posts' ) && ! $redirect_url ) { 141 if ( $redirect_url = get_permalink( get_option( 'page_for_posts' ) ) ) 142 $redirect['query'] = remove_query_arg( 'page_id', $redirect['query'] ); 143 } elseif ( ! empty( $_GET['m'] ) && ( is_year() || is_month() || is_day() ) ) { 144 $m = get_query_var( 'm' ); 145 switch ( strlen( $m ) ) { 146 146 case 4: // Yearly 147 $redirect_url = get_year_link( $m);147 $redirect_url = get_year_link( $m ); 148 148 break; 149 149 case 6: // Monthly 150 $redirect_url = get_month_link( substr( $m, 0, 4), substr($m, 4, 2) );150 $redirect_url = get_month_link( substr( $m, 0, 4 ), substr( $m, 4, 2 ) ); 151 151 break; 152 152 case 8: // Daily 153 $redirect_url = get_day_link( substr($m, 0, 4), substr($m, 4, 2), substr($m, 6, 2));153 $redirect_url = get_day_link( substr( $m, 0, 4 ), substr( $m, 4, 2 ), substr( $m, 6, 2 ) ); 154 154 break; 155 155 } 156 156 if ( $redirect_url ) 157 $redirect['query'] = remove_query_arg( 'm', $redirect['query']);157 $redirect['query'] = remove_query_arg( 'm', $redirect['query'] ); 158 158 // now moving on to non ?m=X year/month/day links 159 } elseif ( is_day() && get_query_var( 'year') && get_query_var('monthnum') && !empty($_GET['day']) ) {160 if ( $redirect_url = get_day_link( get_query_var('year'), get_query_var('monthnum'), get_query_var('day')) )161 $redirect['query'] = remove_query_arg( array('year', 'monthnum', 'day'), $redirect['query']);162 } elseif ( is_month() && get_query_var( 'year') && !empty($_GET['monthnum']) ) {163 if ( $redirect_url = get_month_link( get_query_var('year'), get_query_var('monthnum')) )164 $redirect['query'] = remove_query_arg( array('year', 'monthnum'), $redirect['query']);165 } elseif ( is_year() && ! empty($_GET['year']) ) {166 if ( $redirect_url = get_year_link( get_query_var('year')) )167 $redirect['query'] = remove_query_arg( 'year', $redirect['query']);168 } elseif ( is_author() && ! empty($_GET['author']) && preg_match( '|^[0-9]+$|', $_GET['author'] ) ) {169 $author = get_userdata( get_query_var('author'));159 } elseif ( is_day() && get_query_var( 'year' ) && get_query_var( 'monthnum' ) && ! empty( $_GET['day'] ) ) { 160 if ( $redirect_url = get_day_link( get_query_var( 'year' ), get_query_var( 'monthnum' ), get_query_var( 'day' ) ) ) 161 $redirect['query'] = remove_query_arg( array( 'year', 'monthnum', 'day' ), $redirect['query'] ); 162 } elseif ( is_month() && get_query_var( 'year' ) && ! empty( $_GET['monthnum'] ) ) { 163 if ( $redirect_url = get_month_link( get_query_var( 'year' ), get_query_var( 'monthnum' ) ) ) 164 $redirect['query'] = remove_query_arg( array( 'year', 'monthnum' ), $redirect['query'] ); 165 } elseif ( is_year() && ! empty( $_GET['year'] ) ) { 166 if ( $redirect_url = get_year_link( get_query_var( 'year' ) ) ) 167 $redirect['query'] = remove_query_arg( 'year', $redirect['query'] ); 168 } elseif ( is_author() && ! empty( $_GET['author'] ) && preg_match( '|^[0-9]+$|', $_GET['author'] ) ) { 169 $author = get_userdata( get_query_var( 'author' ) ); 170 170 if ( ( false !== $author ) && $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE $wpdb->posts.post_author = %d AND $wpdb->posts.post_status = 'publish' LIMIT 1", $author->ID ) ) ) { 171 if ( $redirect_url = get_author_posts_url( $author->ID, $author->user_nicename) )172 $redirect['query'] = remove_query_arg( 'author', $redirect['query']);171 if ( $redirect_url = get_author_posts_url( $author->ID, $author->user_nicename ) ) 172 $redirect['query'] = remove_query_arg( 'author', $redirect['query'] ); 173 173 } 174 174 } elseif ( is_category() || is_tag() || is_tax() ) { // Terms (Tags/categories) 175 175 … … 178 178 $term_count += count( $tax_query['terms'] ); 179 179 180 180 $obj = $wp_query->get_queried_object(); 181 if ( $term_count <= 1 && ! empty($obj->term_id) && ( $tax_url = get_term_link((int)$obj->term_id, $obj->taxonomy) ) && !is_wp_error($tax_url) ) {182 if ( ! empty($redirect['query']) ) {181 if ( $term_count <= 1 && ! empty( $obj->term_id ) && ( $tax_url = get_term_link( ( int )$obj->term_id, $obj->taxonomy ) ) && ! is_wp_error( $tax_url ) ) { 182 if ( ! empty( $redirect['query'] ) ) { 183 183 // Strip taxonomy query vars off the url. 184 $qv_remove = array( 'term', 'taxonomy' );184 $qv_remove = array( 'term', 'taxonomy' ); 185 185 if ( is_category() ) { 186 186 $qv_remove[] = 'category_name'; 187 187 $qv_remove[] = 'cat'; … … 194 194 $qv_remove[] = $tax_obj->query_var; 195 195 } 196 196 197 $rewrite_vars = array_diff( array_keys( $wp_query->query), array_keys($_GET) );197 $rewrite_vars = array_diff( array_keys( $wp_query->query ), array_keys( $_GET ) ); 198 198 199 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 $_GET200 $redirect['query'] = remove_query_arg( $qv_remove, $redirect['query']); //Remove all of the per-tax qv's199 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 200 $redirect['query'] = remove_query_arg( $qv_remove, $redirect['query'] ); //Remove all of the per-tax qv's 201 201 202 202 // Create the destination url for this taxonomy 203 $tax_url = parse_url( $tax_url);204 if ( ! empty( $tax_url['query']) ) { // Taxonomy accessible via ?taxonomy=..&term=.. or any custom qv..205 parse_str( $tax_url['query'], $query_vars);206 $redirect['query'] = add_query_arg( $query_vars, $redirect['query']);203 $tax_url = parse_url( $tax_url ); 204 if ( ! empty( $tax_url['query'] ) ) { // Taxonomy accessible via ?taxonomy=..&term=.. or any custom qv.. 205 parse_str( $tax_url['query'], $query_vars ); 206 $redirect['query'] = add_query_arg( $query_vars, $redirect['query'] ); 207 207 } else { // Taxonomy is accessible via a "pretty-URL" 208 208 $redirect['path'] = $tax_url['path']; 209 209 } 210 210 211 211 } else { // Some query vars are set via $_GET. Unset those from $_GET that exist via the rewrite 212 212 foreach ( $qv_remove as $_qv ) { 213 if ( isset( $rewrite_vars[$_qv]) )214 $redirect['query'] = remove_query_arg( $_qv, $redirect['query']);213 if ( isset( $rewrite_vars[$_qv] ) ) 214 $redirect['query'] = remove_query_arg( $_qv, $redirect['query'] ); 215 215 } 216 216 } 217 217 } 218 218 219 219 } 220 } elseif ( is_single() && strpos( $wp_rewrite->permalink_structure, '%category%') !== false && $cat = get_query_var( 'category_name' ) ) {220 } elseif ( is_single() && strpos( $wp_rewrite->permalink_structure, '%category%' ) !== false && $cat = get_query_var( 'category_name' ) ) { 221 221 $category = get_category_by_path( $cat ); 222 $post_terms = wp_get_object_terms( $wp_query->get_queried_object_id(), 'category', array('fields' => 'tt_ids'));223 if ( ( !$category || is_wp_error($category)) || ( !is_wp_error($post_terms) && !empty($post_terms) && !in_array($category->term_taxonomy_id, $post_terms) ) )224 $redirect_url = get_permalink( $wp_query->get_queried_object_id());222 $post_terms = wp_get_object_terms( $wp_query->get_queried_object_id(), 'category', array( 'fields' => 'tt_ids' ) ); 223 if ( ( ! $category || is_wp_error( $category ) ) || ( ! is_wp_error( $post_terms ) && ! empty( $post_terms ) && ! in_array( $category->term_taxonomy_id, $post_terms ) ) ) 224 $redirect_url = get_permalink( $wp_query->get_queried_object_id() ); 225 225 } 226 226 227 227 // Post Paging 228 228 if ( is_singular() && ! is_front_page() && get_query_var('page') ) { 229 if ( ! $redirect_url )229 if ( ! $redirect_url ) 230 230 $redirect_url = get_permalink( get_queried_object_id() ); 231 231 $redirect_url = trailingslashit( $redirect_url ) . user_trailingslashit( get_query_var( 'page' ), 'single_paged' ); 232 232 $redirect['query'] = remove_query_arg( 'page', $redirect['query'] ); 233 233 } 234 234 235 235 // paging and feeds 236 if ( get_query_var( 'paged') || is_feed() || get_query_var('cpage') ) {236 if ( get_query_var( 'paged' ) || is_feed() || get_query_var( 'cpage' ) ) { 237 237 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'] ) ) { 238 238 // Strip off paging and feed 239 $redirect['path'] = preg_replace(" #/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", '/', $redirect['path']); // strip off any existing paging240 $redirect['path'] = preg_replace( '#/(comments/?)?(feed|rss2?|rdf|atom)(/+|$)#', '/', $redirect['path']); // strip off feed endings241 $redirect['path'] = preg_replace( '#/comment-page-[0-9]+?(/+)?$#', '/', $redirect['path']); // strip off any existing comment paging239 $redirect['path'] = preg_replace(" #/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", '/', $redirect['path'] ); // strip off any existing paging 240 $redirect['path'] = preg_replace( '#/(comments/?)?(feed|rss2?|rdf|atom)(/+|$)#', '/', $redirect['path'] ); // strip off feed endings 241 $redirect['path'] = preg_replace( '#/comment-page-[0-9]+?(/+)?$#', '/', $redirect['path'] ); // strip off any existing comment paging 242 242 } 243 243 244 244 $addl_path = ''; 245 245 if ( is_feed() && in_array( get_query_var('feed'), $wp_rewrite->feeds ) ) { 246 $addl_path = ! empty( $addl_path ) ? trailingslashit($addl_path) : '';247 if ( ! is_singular() && get_query_var( 'withcomments' ) )246 $addl_path = ! empty( $addl_path ) ? trailingslashit( $addl_path ) : ''; 247 if ( ! is_singular() && get_query_var( 'withcomments' ) ) 248 248 $addl_path .= 'comments/'; 249 if ( ( 'rss' == get_default_feed() && 'feed' == get_query_var( 'feed') ) || 'rss' == get_query_var('feed') )249 if ( ( 'rss' == get_default_feed() && 'feed' == get_query_var( 'feed' ) ) || 'rss' == get_query_var( 'feed' ) ) 250 250 $addl_path .= user_trailingslashit( 'feed/' . ( ( get_default_feed() == 'rss2' ) ? '' : 'rss2' ), 'feed' ); 251 251 else 252 $addl_path .= user_trailingslashit( 'feed/' . ( ( get_default_feed() == get_query_var( 'feed') || 'feed' == get_query_var('feed') ) ? '' : get_query_var('feed') ), 'feed' );252 $addl_path .= user_trailingslashit( 'feed/' . ( ( get_default_feed() == get_query_var( 'feed' ) || 'feed' == get_query_var( 'feed' ) ) ? '' : get_query_var( 'feed' ) ), 'feed' ); 253 253 $redirect['query'] = remove_query_arg( 'feed', $redirect['query'] ); 254 } elseif ( is_feed() && 'old' == get_query_var( 'feed') ) {254 } elseif ( is_feed() && 'old' == get_query_var( 'feed' ) ) { 255 255 $old_feed_files = array( 256 256 'wp-atom.php' => 'atom', 257 257 'wp-commentsrss2.php' => 'comments_rss2', … … 267 267 } 268 268 } 269 269 270 if ( get_query_var( 'paged') > 0 ) {271 $paged = get_query_var( 'paged');270 if ( get_query_var( 'paged' ) > 0 ) { 271 $paged = get_query_var( 'paged' ); 272 272 $redirect['query'] = remove_query_arg( 'paged', $redirect['query'] ); 273 if ( ! is_feed() ) {274 if ( $paged > 1 && ! is_single() ) {275 $addl_path = ( ! empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit("$wp_rewrite->pagination_base/$paged", 'paged');276 } elseif ( ! is_single() ) {277 $addl_path = ! empty( $addl_path ) ? trailingslashit($addl_path) : '';273 if ( ! is_feed() ) { 274 if ( $paged > 1 && ! is_single() ) { 275 $addl_path = ( ! empty( $addl_path ) ? trailingslashit( $addl_path ) : '' ) . user_trailingslashit( "$wp_rewrite->pagination_base/$paged", 'paged' ); 276 } elseif ( ! is_single() ) { 277 $addl_path = ! empty( $addl_path ) ? trailingslashit( $addl_path ) : ''; 278 278 } 279 279 } elseif ( $paged > 1 ) { 280 280 $redirect['query'] = add_query_arg( 'paged', $paged, $redirect['query'] ); 281 281 } 282 282 } 283 283 284 if ( get_option( 'page_comments') && ( ( 'newest' == get_option('default_comments_page') && get_query_var('cpage') > 0 ) || ( 'newest' != get_option('default_comments_page') && get_query_var('cpage') > 1 ) ) ) {285 $addl_path = ( ! empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit( 'comment-page-' . get_query_var('cpage'), 'commentpaged' );284 if ( get_option( 'page_comments' ) && ( ( 'newest' == get_option( 'default_comments_page' ) && get_query_var( 'cpage' ) > 0 ) || ( 'newest' != get_option( 'default_comments_page' ) && get_query_var( 'cpage' ) > 1 ) ) ) { 285 $addl_path = ( ! empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit( 'comment-page-' . get_query_var( 'cpage' ), 'commentpaged' ); 286 286 $redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] ); 287 287 } 288 288 289 289 $redirect['path'] = user_trailingslashit( preg_replace('|/' . preg_quote( $wp_rewrite->index, '|' ) . '/?$|', '/', $redirect['path']) ); // strip off trailing /index.php/ 290 if ( ! empty( $addl_path ) && $wp_rewrite->using_index_permalinks() && strpos($redirect['path'], '/' . $wp_rewrite->index . '/') === false )290 if ( ! empty( $addl_path ) && $wp_rewrite->using_index_permalinks() && strpos($redirect['path'], '/' . $wp_rewrite->index . '/') === false ) 291 291 $redirect['path'] = trailingslashit($redirect['path']) . $wp_rewrite->index . '/'; 292 if ( ! empty( $addl_path ) )292 if ( ! empty( $addl_path ) ) 293 293 $redirect['path'] = trailingslashit($redirect['path']) . $addl_path; 294 294 $redirect_url = $redirect['scheme'] . '://' . $redirect['host'] . $redirect['path']; 295 295 } 296 296 297 297 if ( 'wp-register.php' == basename( $redirect['path'] ) ) { 298 298 if ( is_multisite() ) 299 //duplicate_hook 299 300 $redirect_url = apply_filters( 'wp_signup_location', network_site_url( 'wp-signup.php' ) ); 300 301 else 301 302 $redirect_url = site_url( 'wp-login.php?action=register' ); … … 306 307 307 308 // tack on any additional query vars 308 309 $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] ); 309 if ( $redirect_url && ! empty($redirect['query']) ) {310 if ( $redirect_url && ! empty( $redirect['query'] ) ) { 310 311 parse_str( $redirect['query'], $_parsed_query ); 311 $redirect = @parse_url( $redirect_url);312 $redirect = @parse_url( $redirect_url ); 312 313 313 314 if ( ! empty( $_parsed_query['name'] ) && ! empty( $redirect['query'] ) ) { 314 315 parse_str( $redirect['query'], $_parsed_redirect_query ); … … 322 323 } 323 324 324 325 if ( $redirect_url ) 325 $redirect = @parse_url( $redirect_url);326 $redirect = @parse_url( $redirect_url ); 326 327 327 328 // www.example.com vs example.com 328 $user_home = @parse_url( home_url());329 if ( ! empty($user_home['host']) )329 $user_home = @parse_url( home_url() ); 330 if ( ! empty( $user_home['host'] ) ) 330 331 $redirect['host'] = $user_home['host']; 331 if ( empty( $user_home['path']) )332 if ( empty( $user_home['path'] ) ) 332 333 $user_home['path'] = '/'; 333 334 334 335 // Handle ports 335 if ( ! empty($user_home['port']) )336 if ( ! empty( $user_home['port'] ) ) 336 337 $redirect['port'] = $user_home['port']; 337 338 else 338 unset( $redirect['port']);339 unset( $redirect['port'] ); 339 340 340 341 // trailing /index.php 341 $redirect['path'] = preg_replace( '|/' . preg_quote( $wp_rewrite->index, '|' ) . '/*?$|', '/', $redirect['path']);342 $redirect['path'] = preg_replace( '|/' . preg_quote( $wp_rewrite->index, '|' ) . '/*?$|', '/', $redirect['path'] ); 342 343 343 344 // Remove trailing spaces from the path 344 345 $redirect['path'] = preg_replace( '#(%20| )+$#', '', $redirect['path'] ); 345 346 346 if ( ! empty( $redirect['query'] ) ) {347 if ( ! empty( $redirect['query'] ) ) { 347 348 // Remove trailing spaces from certain terminating query string args 348 349 $redirect['query'] = preg_replace( '#((p|page_id|cat|tag)=[^&]*?)(%20| )+$#', '$1', $redirect['query'] ); 349 350 350 351 // Clean up empty query strings 351 $redirect['query'] = trim( preg_replace( '#(^|&)(p|page_id|cat|tag)=?(&|$)#', '&', $redirect['query']), '&');352 $redirect['query'] = trim( preg_replace( '#(^|&)(p|page_id|cat|tag)=?(&|$)#', '&', $redirect['query']) , '&'); 352 353 353 354 // Redirect obsolete feeds 354 355 $redirect['query'] = preg_replace( '#(^|&)feed=rss(&|$)#', '$1feed=rss2$2', $redirect['query'] ); … … 358 359 } 359 360 360 361 // strip /index.php/ when we're not using PATHINFO permalinks 361 if ( ! $wp_rewrite->using_index_permalinks() )362 if ( ! $wp_rewrite->using_index_permalinks() ) 362 363 $redirect['path'] = str_replace( '/' . $wp_rewrite->index . '/', '/', $redirect['path'] ); 363 364 364 365 // trailing slashes 365 if ( is_object( $wp_rewrite) && $wp_rewrite->using_permalinks() && !is_404() && (!is_front_page() || ( is_front_page() && (get_query_var('paged') > 1) ) ) ) {366 if ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() && ! is_404() && ( ! is_front_page() || ( is_front_page() && ( get_query_var( 'paged' ) > 1) ) ) ) { 366 367 $user_ts_type = ''; 367 if ( get_query_var( 'paged') > 0 ) {368 if ( get_query_var( 'paged' ) > 0 ) { 368 369 $user_ts_type = 'paged'; 369 370 } else { 370 foreach ( array( 'single', 'category', 'page', 'day', 'month', 'year', 'home') as $type ) {371 foreach ( array( 'single', 'category', 'page', 'day', 'month', 'year', 'home' ) as $type ) { 371 372 $func = 'is_' . $type; 372 if ( call_user_func( $func) ) {373 if ( call_user_func( $func ) ) { 373 374 $user_ts_type = $type; 374 375 break; 375 376 } 376 377 } 377 378 } 378 $redirect['path'] = user_trailingslashit( $redirect['path'], $user_ts_type);379 $redirect['path'] = user_trailingslashit( $redirect['path'], $user_ts_type ); 379 380 } elseif ( is_front_page() ) { 380 $redirect['path'] = trailingslashit( $redirect['path']);381 $redirect['path'] = trailingslashit( $redirect['path'] ); 381 382 } 382 383 383 384 // Strip multiple slashes out of the URL 384 if ( strpos( $redirect['path'], '//') > -1 )385 $redirect['path'] = preg_replace( '|/+|', '/', $redirect['path']);385 if ( strpos( $redirect['path'], '//' ) > -1 ) 386 $redirect['path'] = preg_replace( '|/+|', '/', $redirect['path'] ); 386 387 387 388 // Always trailing slash the Front Page URL 388 389 if ( trailingslashit( $redirect['path'] ) == trailingslashit( $user_home['path'] ) ) 389 $redirect['path'] = trailingslashit( $redirect['path']);390 $redirect['path'] = trailingslashit( $redirect['path'] ); 390 391 391 392 // Ignore differences in host capitalization, as this can lead to infinite redirects 392 393 // Only redirect no-www <=> yes-www 393 if ( strtolower( $original['host']) == strtolower($redirect['host']) ||394 ( strtolower( $original['host']) != 'www.' . strtolower($redirect['host']) && 'www.' . strtolower($original['host']) != strtolower($redirect['host']) ) )394 if ( strtolower( $original['host'] ) == strtolower( $redirect['host'] ) || 395 ( strtolower( $original['host'] ) != 'www.' . strtolower( $redirect['host'] ) && 'www.' . strtolower( $original['host'] ) != strtolower( $redirect['host'] ) ) ) 395 396 $redirect['host'] = $original['host']; 396 397 397 $compare_original = array( $original['host'], $original['path']);398 $compare_original = array( $original['host'], $original['path'] ); 398 399 399 if ( ! empty( $original['port'] ) )400 if ( ! empty( $original['port'] ) ) 400 401 $compare_original[] = $original['port']; 401 402 402 if ( ! empty( $original['query'] ) )403 if ( ! empty( $original['query'] ) ) 403 404 $compare_original[] = $original['query']; 404 405 405 $compare_redirect = array( $redirect['host'], $redirect['path']);406 $compare_redirect = array( $redirect['host'], $redirect['path'] ); 406 407 407 if ( ! empty( $redirect['port'] ) )408 if ( ! empty( $redirect['port'] ) ) 408 409 $compare_redirect[] = $redirect['port']; 409 410 410 if ( ! empty( $redirect['query'] ) )411 if ( ! empty( $redirect['query'] ) ) 411 412 $compare_redirect[] = $redirect['query']; 412 413 413 414 if ( $compare_original !== $compare_redirect ) { 414 415 $redirect_url = $redirect['scheme'] . '://' . $redirect['host']; 415 if ( ! empty($redirect['port']) )416 if ( ! empty( $redirect['port'] ) ) 416 417 $redirect_url .= ':' . $redirect['port']; 417 418 $redirect_url .= $redirect['path']; 418 if ( ! empty($redirect['query']) )419 if ( ! empty( $redirect['query'] ) ) 419 420 $redirect_url .= '?' . $redirect['query']; 420 421 } 421 422 422 if ( ! $redirect_url || $redirect_url == $requested_url )423 if ( ! $redirect_url || $redirect_url == $requested_url ) 423 424 return false; 424 425 425 426 // Hex encoded octets are case-insensitive. 426 if ( false !== strpos( $requested_url, '%') ) {427 if ( ! function_exists('lowercase_octets') ) {428 function lowercase_octets( $matches) {427 if ( false !== strpos( $requested_url, '%' ) ) { 428 if ( ! function_exists( 'lowercase_octets' ) ) { 429 function lowercase_octets( $matches ) { 429 430 return strtolower( $matches[0] ); 430 431 } 431 432 } 432 $requested_url = preg_replace_callback( '|%[a-fA-F0-9][a-fA-F0-9]|', 'lowercase_octets', $requested_url);433 $requested_url = preg_replace_callback( '|%[a-fA-F0-9][a-fA-F0-9]|', 'lowercase_octets', $requested_url ); 433 434 } 434 435 435 // Note that you can use the "redirect_canonical" filter to cancel a canonical redirect for whatever reason by returning false 436 $redirect_url = apply_filters('redirect_canonical', $redirect_url, $requested_url); 436 /** 437 * Filter the URLs for canonical redirects. 438 * 439 * The requested url and the url that will be redirected 440 * to can be filtered. Filter can also be used to cancel 441 * a canonical redirect by returning false. 442 * 443 * @since 2.3.0 444 * 445 * @param string $redirect_url The url that the user will be redirected to. 446 * @param string $requested_url The url that is requested and the redirect will be performed upon. 447 */ 448 $redirect_url = apply_filters( 'redirect_canonical', $redirect_url, $requested_url ); 437 449 438 if ( ! $redirect_url || $redirect_url == $requested_url ) // yes, again -- in case the filter aborted the request450 if ( ! $redirect_url || $redirect_url == $requested_url ) // yes, again -- in case the filter aborted the request 439 451 return false; 440 452 441 453 if ( $do_redirect ) { 442 454 // protect against chained redirects 443 if ( ! redirect_canonical($redirect_url, false) ) {444 wp_redirect( $redirect_url, 301);455 if ( ! redirect_canonical( $redirect_url, false ) ) { 456 wp_redirect( $redirect_url, 301 ); 445 457 exit(); 446 458 } else { 447 459 // Debug … … 467 479 if ( ! empty( $parsed_url['query'] ) ) { 468 480 parse_str( $parsed_url['query'], $parsed_query ); 469 481 foreach ( $args_to_check as $qv ) { 470 if ( ! isset( $parsed_query[$qv] ) )482 if ( ! isset( $parsed_query[$qv] ) ) 471 483 $query_string = remove_query_arg( $qv, $query_string ); 472 484 } 473 485 } else { … … 487 499 function redirect_guess_404_permalink() { 488 500 global $wpdb, $wp_rewrite; 489 501 490 if ( get_query_var( 'name') ) {491 $where = $wpdb->prepare( "post_name LIKE %s", like_escape( get_query_var('name') ) . '%');502 if ( get_query_var( 'name' ) ) { 503 $where = $wpdb->prepare( "post_name LIKE %s", like_escape( get_query_var( 'name' ) ) . '%' ); 492 504 493 505 // if any of post_type, year, monthnum, or day are set, use them to refine the query 494 if ( get_query_var( 'post_type') )495 $where .= $wpdb->prepare( " AND post_type = %s", get_query_var('post_type'));506 if ( get_query_var( 'post_type' ) ) 507 $where .= $wpdb->prepare( " AND post_type = %s", get_query_var( 'post_type' ) ); 496 508 else 497 509 $where .= " AND post_type IN ('" . implode( "', '", get_post_types( array( 'public' => true ) ) ) . "')"; 498 510 499 if ( get_query_var( 'year') )500 $where .= $wpdb->prepare( " AND YEAR(post_date) = %d", get_query_var('year'));501 if ( get_query_var( 'monthnum') )502 $where .= $wpdb->prepare( " AND MONTH(post_date) = %d", get_query_var('monthnum'));503 if ( get_query_var( 'day') )504 $where .= $wpdb->prepare( " AND DAYOFMONTH(post_date) = %d", get_query_var('day'));511 if ( get_query_var( 'year' ) ) 512 $where .= $wpdb->prepare( " AND YEAR(post_date) = %d", get_query_var( 'year' ) ); 513 if ( get_query_var( 'monthnum' ) ) 514 $where .= $wpdb->prepare( " AND MONTH(post_date) = %d", get_query_var( 'monthnum' ) ); 515 if ( get_query_var( 'day' ) ) 516 $where .= $wpdb->prepare( " AND DAYOFMONTH(post_date) = %d", get_query_var( 'day' ) ); 505 517 506 $post_id = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'");518 $post_id = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'" ); 507 519 if ( ! $post_id ) 508 520 return false; 509 521 if ( get_query_var( 'feed' ) ) … … 517 529 return false; 518 530 } 519 531 520 add_action( 'template_redirect', 'redirect_canonical');532 add_action( 'template_redirect', 'redirect_canonical' ); 521 533 522 534 function wp_redirect_admin_locations() { 523 535 global $wp_rewrite;