37 * not needed or the string of the URL |
37 * not needed or the string of the URL |
38 */ |
38 */ |
39 function redirect_canonical( $requested_url = null, $do_redirect = true ) { |
39 function redirect_canonical( $requested_url = null, $do_redirect = true ) { |
40 global $wp_rewrite, $is_IIS, $wp_query, $wpdb; |
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 ( isset( $_SERVER['REQUEST_METHOD'] ) && ! in_array( strtoupper( $_SERVER['REQUEST_METHOD'] ), array( 'GET', 'HEAD' ) ) ) { |
43 return; |
43 return; |
|
44 } |
|
45 |
|
46 // If we're not in wp-admin and the post has been published and preview nonce |
|
47 // is non-existent or invalid then no need for preview in query |
|
48 if ( is_preview() && get_query_var( 'p' ) && 'publish' == get_post_status( get_query_var( 'p' ) ) ) { |
|
49 if ( ! isset( $_GET['preview_id'] ) |
|
50 || ! isset( $_GET['preview_nonce'] ) |
|
51 || ! wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . (int) $_GET['preview_id'] ) ) { |
|
52 $wp_query->is_preview = false; |
|
53 } |
|
54 } |
|
55 |
|
56 if ( is_trackback() || is_search() || is_comments_popup() || is_admin() || is_preview() || is_robots() || ( $is_IIS && !iis7_supports_permalinks() ) ) { |
|
57 return; |
|
58 } |
44 |
59 |
45 if ( !$requested_url ) { |
60 if ( !$requested_url ) { |
46 // build the URL in the address bar |
61 // build the URL in the address bar |
47 $requested_url = is_ssl() ? 'https://' : 'http://'; |
62 $requested_url = is_ssl() ? 'https://' : 'http://'; |
48 $requested_url .= $_SERVER['HTTP_HOST']; |
63 $requested_url .= $_SERVER['HTTP_HOST']; |
52 $original = @parse_url($requested_url); |
67 $original = @parse_url($requested_url); |
53 if ( false === $original ) |
68 if ( false === $original ) |
54 return; |
69 return; |
55 |
70 |
56 // Some PHP setups turn requests for / into /index.php in REQUEST_URI |
71 // Some PHP setups turn requests for / into /index.php in REQUEST_URI |
57 // See: http://trac.wordpress.org/ticket/5017 |
72 // See: https://core.trac.wordpress.org/ticket/5017 |
58 // See: http://trac.wordpress.org/ticket/7173 |
73 // See: https://core.trac.wordpress.org/ticket/7173 |
59 // Disabled, for now: |
74 // Disabled, for now: |
60 // $original['path'] = preg_replace('|/index\.php$|', '/', $original['path']); |
75 // $original['path'] = preg_replace('|/index\.php$|', '/', $original['path']); |
61 |
76 |
62 $redirect = $original; |
77 $redirect = $original; |
63 $redirect_url = false; |
78 $redirect_url = false; |
65 // Notice fixing |
80 // Notice fixing |
66 if ( !isset($redirect['path']) ) |
81 if ( !isset($redirect['path']) ) |
67 $redirect['path'] = ''; |
82 $redirect['path'] = ''; |
68 if ( !isset($redirect['query']) ) |
83 if ( !isset($redirect['query']) ) |
69 $redirect['query'] = ''; |
84 $redirect['query'] = ''; |
|
85 |
|
86 // If the original URL ended with non-breaking spaces, they were almost |
|
87 // certainly inserted by accident. Let's remove them, so the reader doesn't |
|
88 // see a 404 error with no obvious cause. |
|
89 $redirect['path'] = preg_replace( '|(%C2%A0)+$|i', '', $redirect['path'] ); |
|
90 |
|
91 // It's not a preview, so remove it from URL |
|
92 if ( get_query_var( 'preview' ) ) { |
|
93 $redirect['query'] = remove_query_arg( 'preview', $redirect['query'] ); |
|
94 } |
70 |
95 |
71 if ( is_feed() && ( $id = get_query_var( 'p' ) ) ) { |
96 if ( is_feed() && ( $id = get_query_var( 'p' ) ) ) { |
72 if ( $redirect_url = get_post_comments_feed_link( $id, get_query_var( 'feed' ) ) ) { |
97 if ( $redirect_url = get_post_comments_feed_link( $id, get_query_var( 'feed' ) ) ) { |
73 $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type', 'feed'), $redirect_url ); |
98 $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type', 'feed'), $redirect_url ); |
74 $redirect['path'] = parse_url( $redirect_url, PHP_URL_PATH ); |
99 $redirect['path'] = parse_url( $redirect_url, PHP_URL_PATH ); |
172 $redirect['query'] = remove_query_arg('author', $redirect['query']); |
197 $redirect['query'] = remove_query_arg('author', $redirect['query']); |
173 } |
198 } |
174 } elseif ( is_category() || is_tag() || is_tax() ) { // Terms (Tags/categories) |
199 } elseif ( is_category() || is_tag() || is_tax() ) { // Terms (Tags/categories) |
175 |
200 |
176 $term_count = 0; |
201 $term_count = 0; |
177 foreach ( $wp_query->tax_query->queries as $tax_query ) |
202 foreach ( $wp_query->tax_query->queried_terms as $tax_query ) |
178 $term_count += count( $tax_query['terms'] ); |
203 $term_count += count( $tax_query['terms'] ); |
179 |
204 |
180 $obj = $wp_query->get_queried_object(); |
205 $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) ) { |
206 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']) ) { |
207 if ( !empty($redirect['query']) ) { |
232 $redirect['query'] = remove_query_arg( 'page', $redirect['query'] ); |
257 $redirect['query'] = remove_query_arg( 'page', $redirect['query'] ); |
233 } |
258 } |
234 |
259 |
235 // paging and feeds |
260 // paging and feeds |
236 if ( get_query_var('paged') || is_feed() || get_query_var('cpage') ) { |
261 if ( get_query_var('paged') || is_feed() || get_query_var('cpage') ) { |
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'] ) ) { |
262 while ( preg_match( "#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", $redirect['path'] ) || preg_match( '#/(comments/?)?(feed|rss|rdf|atom|rss2)(/+)?$#', $redirect['path'] ) || preg_match( "#/{$wp_rewrite->comments_pagination_base}-[0-9]+(/+)?$#", $redirect['path'] ) ) { |
238 // Strip off paging and feed |
263 // Strip off paging and feed |
239 $redirect['path'] = preg_replace("#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", '/', $redirect['path']); // strip off any existing paging |
264 $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 |
265 $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 |
266 $redirect['path'] = preg_replace("#/{$wp_rewrite->comments_pagination_base}-[0-9]+?(/+)?$#", '/', $redirect['path']); // strip off any existing comment paging |
242 } |
267 } |
243 |
268 |
244 $addl_path = ''; |
269 $addl_path = ''; |
245 if ( is_feed() && in_array( get_query_var('feed'), $wp_rewrite->feeds ) ) { |
270 if ( is_feed() && in_array( get_query_var('feed'), $wp_rewrite->feeds ) ) { |
246 $addl_path = !empty( $addl_path ) ? trailingslashit($addl_path) : ''; |
271 $addl_path = !empty( $addl_path ) ? trailingslashit($addl_path) : ''; |
280 $redirect['query'] = add_query_arg( 'paged', $paged, $redirect['query'] ); |
305 $redirect['query'] = add_query_arg( 'paged', $paged, $redirect['query'] ); |
281 } |
306 } |
282 } |
307 } |
283 |
308 |
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 ) ) ) { |
309 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' ); |
310 $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit( $wp_rewrite->comments_pagination_base . '-' . get_query_var('cpage'), 'commentpaged' ); |
286 $redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] ); |
311 $redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] ); |
287 } |
312 } |
288 |
313 |
289 $redirect['path'] = user_trailingslashit( preg_replace('|/' . preg_quote( $wp_rewrite->index, '|' ) . '/?$|', '/', $redirect['path']) ); // strip off trailing /index.php/ |
314 $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 ) |
315 if ( !empty( $addl_path ) && $wp_rewrite->using_index_permalinks() && strpos($redirect['path'], '/' . $wp_rewrite->index . '/') === false ) |
293 $redirect['path'] = trailingslashit($redirect['path']) . $addl_path; |
318 $redirect['path'] = trailingslashit($redirect['path']) . $addl_path; |
294 $redirect_url = $redirect['scheme'] . '://' . $redirect['host'] . $redirect['path']; |
319 $redirect_url = $redirect['scheme'] . '://' . $redirect['host'] . $redirect['path']; |
295 } |
320 } |
296 |
321 |
297 if ( 'wp-register.php' == basename( $redirect['path'] ) ) { |
322 if ( 'wp-register.php' == basename( $redirect['path'] ) ) { |
298 if ( is_multisite() ) |
323 if ( is_multisite() ) { |
299 /** This filter is documented in wp-login.php */ |
324 /** This filter is documented in wp-login.php */ |
300 $redirect_url = apply_filters( 'wp_signup_location', network_site_url( 'wp-signup.php' ) ); |
325 $redirect_url = apply_filters( 'wp_signup_location', network_site_url( 'wp-signup.php' ) ); |
301 else |
326 } else { |
302 $redirect_url = site_url( 'wp-login.php?action=register' ); |
327 $redirect_url = site_url( 'wp-login.php?action=register' ); |
|
328 } |
|
329 |
303 wp_redirect( $redirect_url, 301 ); |
330 wp_redirect( $redirect_url, 301 ); |
304 die(); |
331 die(); |
305 } |
332 } |
306 } |
333 } |
307 |
334 |
393 // Only redirect no-www <=> yes-www |
420 // Only redirect no-www <=> yes-www |
394 if ( strtolower($original['host']) == strtolower($redirect['host']) || |
421 if ( strtolower($original['host']) == strtolower($redirect['host']) || |
395 ( strtolower($original['host']) != 'www.' . strtolower($redirect['host']) && 'www.' . strtolower($original['host']) != strtolower($redirect['host']) ) ) |
422 ( strtolower($original['host']) != 'www.' . strtolower($redirect['host']) && 'www.' . strtolower($original['host']) != strtolower($redirect['host']) ) ) |
396 $redirect['host'] = $original['host']; |
423 $redirect['host'] = $original['host']; |
397 |
424 |
398 $compare_original = array($original['host'], $original['path']); |
425 $compare_original = array( $original['host'], $original['path'] ); |
399 |
426 |
400 if ( !empty( $original['port'] ) ) |
427 if ( !empty( $original['port'] ) ) |
401 $compare_original[] = $original['port']; |
428 $compare_original[] = $original['port']; |
402 |
429 |
403 if ( !empty( $original['query'] ) ) |
430 if ( !empty( $original['query'] ) ) |
404 $compare_original[] = $original['query']; |
431 $compare_original[] = $original['query']; |
405 |
432 |
406 $compare_redirect = array($redirect['host'], $redirect['path']); |
433 $compare_redirect = array( $redirect['host'], $redirect['path'] ); |
407 |
434 |
408 if ( !empty( $redirect['port'] ) ) |
435 if ( !empty( $redirect['port'] ) ) |
409 $compare_redirect[] = $redirect['port']; |
436 $compare_redirect[] = $redirect['port']; |
410 |
437 |
411 if ( !empty( $redirect['query'] ) ) |
438 if ( !empty( $redirect['query'] ) ) |
465 |
492 |
466 /** |
493 /** |
467 * Removes arguments from a query string if they are not present in a URL |
494 * Removes arguments from a query string if they are not present in a URL |
468 * DO NOT use this in plugin code. |
495 * DO NOT use this in plugin code. |
469 * |
496 * |
470 * @since 3.4 |
497 * @since 3.4.0 |
471 * @access private |
498 * @access private |
472 * |
499 * |
|
500 * @param string $query_string |
|
501 * @param array $args_to_check |
|
502 * @param string $url |
473 * @return string The altered query string |
503 * @return string The altered query string |
474 */ |
504 */ |
475 function _remove_qs_args_if_not_in_url( $query_string, Array $args_to_check, $url ) { |
505 function _remove_qs_args_if_not_in_url( $query_string, Array $args_to_check, $url ) { |
476 $parsed_url = @parse_url( $url ); |
506 $parsed_url = @parse_url( $url ); |
477 if ( ! empty( $parsed_url['query'] ) ) { |
507 if ( ! empty( $parsed_url['query'] ) ) { |
488 |
518 |
489 /** |
519 /** |
490 * Attempts to guess the correct URL based on query vars |
520 * Attempts to guess the correct URL based on query vars |
491 * |
521 * |
492 * @since 2.3.0 |
522 * @since 2.3.0 |
493 * @uses $wpdb |
523 * |
|
524 * @global wpdb $wpdb WordPress database abstraction object. |
494 * |
525 * |
495 * @return bool|string The correct URL if one is found. False on failure. |
526 * @return bool|string The correct URL if one is found. False on failure. |
496 */ |
527 */ |
497 function redirect_guess_404_permalink() { |
528 function redirect_guess_404_permalink() { |
498 global $wpdb, $wp_rewrite; |
529 global $wpdb, $wp_rewrite; |
499 |
530 |
500 if ( get_query_var('name') ) { |
531 if ( get_query_var('name') ) { |
501 $where = $wpdb->prepare("post_name LIKE %s", like_escape( get_query_var('name') ) . '%'); |
532 $where = $wpdb->prepare("post_name LIKE %s", $wpdb->esc_like( get_query_var('name') ) . '%'); |
502 |
533 |
503 // if any of post_type, year, monthnum, or day are set, use them to refine the query |
534 // if any of post_type, year, monthnum, or day are set, use them to refine the query |
504 if ( get_query_var('post_type') ) |
535 if ( get_query_var('post_type') ) |
505 $where .= $wpdb->prepare(" AND post_type = %s", get_query_var('post_type')); |
536 $where .= $wpdb->prepare(" AND post_type = %s", get_query_var('post_type')); |
506 else |
537 else |