diff --git a/src/wp-includes/canonical.php b/src/wp-includes/canonical.php
index 4caaa65..1f385ba 100644
a
|
b
|
function redirect_canonical( $requested_url = null, $do_redirect = true ) { |
59 | 59 | return; |
60 | 60 | } |
61 | 61 | |
| 62 | // Redirect to the canonical URL using JavaScript's history.replaceState() |
| 63 | // function. If this is false, a 301 redirect will be used. |
| 64 | $use_javascript_history_api = true; |
| 65 | |
62 | 66 | if ( ! $requested_url && isset( $_SERVER['HTTP_HOST'] ) ) { |
63 | 67 | // build the URL in the address bar |
64 | 68 | $requested_url = is_ssl() ? 'https://' : 'http://'; |
… |
… |
function redirect_canonical( $requested_url = null, $do_redirect = true ) { |
96 | 100 | } |
97 | 101 | |
98 | 102 | if ( is_feed() && ( $id = get_query_var( 'p' ) ) ) { |
| 103 | $use_javascript_history_api = false; |
99 | 104 | if ( $redirect_url = get_post_comments_feed_link( $id, get_query_var( 'feed' ) ) ) { |
100 | 105 | $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type', 'feed'), $redirect_url ); |
101 | 106 | $redirect['path'] = parse_url( $redirect_url, PHP_URL_PATH ); |
… |
… |
function redirect_canonical( $requested_url = null, $do_redirect = true ) { |
117 | 122 | |
118 | 123 | // These tests give us a WP-generated permalink |
119 | 124 | if ( is_404() ) { |
| 125 | $use_javascript_history_api = false; |
120 | 126 | |
121 | 127 | // Redirect ?page_id, ?p=, ?attachment_id= to their respective url's |
122 | 128 | $id = max( get_query_var('p'), get_query_var('page_id'), get_query_var('attachment_id') ); |
… |
… |
function redirect_canonical( $requested_url = null, $do_redirect = true ) { |
371 | 377 | $redirect['path'] = preg_replace('|/' . preg_quote( $wp_rewrite->index, '|' ) . '/*?$|', '/', $redirect['path']); |
372 | 378 | |
373 | 379 | // Remove trailing spaces from the path |
374 | | $redirect['path'] = preg_replace( '#(%20| )+$#', '', $redirect['path'] ); |
| 380 | $count = 0; |
| 381 | $redirect['path'] = preg_replace( '#(%20| )+$#', '', $redirect['path'], -1, $count ); |
| 382 | if ( $count > 0 ) { |
| 383 | $use_javascript_history_api = false; |
| 384 | } |
375 | 385 | |
376 | 386 | if ( !empty( $redirect['query'] ) ) { |
377 | 387 | // Remove trailing spaces from certain terminating query string args |
378 | | $redirect['query'] = preg_replace( '#((p|page_id|cat|tag)=[^&]*?)(%20| )+$#', '$1', $redirect['query'] ); |
| 388 | $count = 0; |
| 389 | $redirect['query'] = preg_replace( '#((p|page_id|cat|tag)=[^&]*?)(%20| )+$#', '$1', $redirect['query'], -1, $count ); |
| 390 | if ( $count > 0 ) { |
| 391 | $use_javascript_history_api = false; |
| 392 | } |
379 | 393 | |
380 | 394 | // Clean up empty query strings |
381 | 395 | $redirect['query'] = trim(preg_replace( '#(^|&)(p|page_id|cat|tag)=?(&|$)#', '&', $redirect['query']), '&'); |
382 | 396 | |
383 | 397 | // Redirect obsolete feeds |
384 | | $redirect['query'] = preg_replace( '#(^|&)feed=rss(&|$)#', '$1feed=rss2$2', $redirect['query'] ); |
| 398 | $count = 0; |
| 399 | $redirect['query'] = preg_replace( '#(^|&)feed=rss(&|$)#', '$1feed=rss2$2', $redirect['query'], -1, $count ); |
| 400 | if ( $count > 0 ) { |
| 401 | $use_javascript_history_api = false; |
| 402 | } |
385 | 403 | |
386 | 404 | // Remove redundant leading ampersands |
387 | 405 | $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] ); |
… |
… |
function redirect_canonical( $requested_url = null, $do_redirect = true ) { |
411 | 429 | } |
412 | 430 | |
413 | 431 | // Strip multiple slashes out of the URL |
414 | | if ( strpos($redirect['path'], '//') > -1 ) |
| 432 | if ( strpos($redirect['path'], '//') > -1 ) { |
| 433 | $use_javascript_history_api = false; |
415 | 434 | $redirect['path'] = preg_replace('|/+|', '/', $redirect['path']); |
| 435 | } |
416 | 436 | |
417 | 437 | // Always trailing slash the Front Page URL |
418 | 438 | if ( trailingslashit( $redirect['path'] ) == trailingslashit( $user_home['path'] ) ) |
… |
… |
function redirect_canonical( $requested_url = null, $do_redirect = true ) { |
429 | 449 | if ( !empty( $original['port'] ) ) |
430 | 450 | $compare_original[] = $original['port']; |
431 | 451 | |
| 452 | $compare_original_origin = $compare_original; |
| 453 | |
432 | 454 | if ( !empty( $original['query'] ) ) |
433 | 455 | $compare_original[] = $original['query']; |
434 | 456 | |
… |
… |
function redirect_canonical( $requested_url = null, $do_redirect = true ) { |
437 | 459 | if ( !empty( $redirect['port'] ) ) |
438 | 460 | $compare_redirect[] = $redirect['port']; |
439 | 461 | |
| 462 | $compare_redirect_origin = $compare_redirect; |
| 463 | |
440 | 464 | if ( !empty( $redirect['query'] ) ) |
441 | 465 | $compare_redirect[] = $redirect['query']; |
442 | 466 | |
… |
… |
function redirect_canonical( $requested_url = null, $do_redirect = true ) { |
449 | 473 | $redirect_url .= '?' . $redirect['query']; |
450 | 474 | } |
451 | 475 | |
| 476 | if ( $compare_redirect_origin != $compare_original_origin ) { |
| 477 | $use_javascript_history_api = false; |
| 478 | } |
| 479 | |
452 | 480 | if ( ! $redirect_url || $redirect_url == $requested_url ) { |
453 | 481 | return; |
454 | 482 | } |
… |
… |
function redirect_canonical( $requested_url = null, $do_redirect = true ) { |
463 | 491 | $requested_url = preg_replace_callback('|%[a-fA-F0-9][a-fA-F0-9]|', 'lowercase_octets', $requested_url); |
464 | 492 | } |
465 | 493 | |
| 494 | $pre_filter_redirect_url = $redirect_url; |
| 495 | |
466 | 496 | /** |
467 | 497 | * Filter the canonical redirect URL. |
468 | 498 | * |
… |
… |
function redirect_canonical( $requested_url = null, $do_redirect = true ) { |
475 | 505 | */ |
476 | 506 | $redirect_url = apply_filters( 'redirect_canonical', $redirect_url, $requested_url ); |
477 | 507 | |
| 508 | if ( $pre_filter_redirect_url !== $redirect_url ) { |
| 509 | $use_javascript_history_api = false; |
| 510 | } |
| 511 | |
478 | 512 | // yes, again -- in case the filter aborted the request |
479 | 513 | if ( ! $redirect_url || $redirect_url == $requested_url ) { |
480 | 514 | return; |
481 | 515 | } |
482 | 516 | |
483 | | if ( $do_redirect ) { |
| 517 | if ( $do_redirect && $use_javascript_history_api ) { |
| 518 | add_action( 'wp_head', 'redirect_canonical_history_replace', 5 ); |
| 519 | } |
| 520 | elseif ( $do_redirect ) { |
484 | 521 | // protect against chained redirects |
485 | 522 | if ( !redirect_canonical($redirect_url, false) ) { |
486 | 523 | wp_redirect($redirect_url, 301); |
… |
… |
function wp_redirect_admin_locations() { |
595 | 632 | exit; |
596 | 633 | } |
597 | 634 | } |
| 635 | |
| 636 | /** |
| 637 | * Replace the URL as displayed in the browser with the canonical URL |
| 638 | */ |
| 639 | function redirect_canonical_history_replace() { |
| 640 | $canonical_url = redirect_canonical( null, false ); |
| 641 | ?> |
| 642 | <script> |
| 643 | (function( window, url ){ |
| 644 | var history = window.history; |
| 645 | if ( history.replaceState ) { |
| 646 | history.replaceState( {}, '', url+window.location.hash ); |
| 647 | } |
| 648 | }( window, '<?php echo $canonical_url; ?>' )); |
| 649 | </script> |
| 650 | <?php |
| 651 | } |
| 652 | No newline at end of file |