Ticket #33581: 33581.2.diff
File 33581.2.diff, 7.8 KB (added by , 9 years ago) |
---|
-
src/wp-includes/canonical.php
diff --git a/src/wp-includes/canonical.php b/src/wp-includes/canonical.php index 4c981f9..7c79e76 100644
a b 10 10 */ 11 11 12 12 /** 13 * Display the proper URL in the browser. 14 * 15 * In cituations a history.replaceState is possible, the URL will be 16 * rewritten using JavaScript. Otherwise a 301 redirect will be used 17 * to send the use to the correct URL. 18 * 19 * @param string $requested_url Optional. The URL that was requested, used to 20 * figure if redirect is needed. 21 * @param bool $do_redirect Optional. Redirect to the new URL. 22 * 23 * @return string|void The string of the URL, if redirect needed. 24 */ 25 function maybe_redirect_canonical( $requested_url = null, $maybe_redirect = true ) { 26 27 $redirect_url = redirect_canonical( $requested_url, false ); 28 29 /** 30 * Filter whether JavaScript's history API can be used for canonical 31 * redirects. 32 * 33 * @param bool Use the JavaScript history API. 34 */ 35 $use_javascript_history_api = apply_filters( 'wp_javascript_redirect_canonical', true ); 36 37 if ( $maybe_redirect && $use_javascript_history_api ) { 38 add_action( 'wp_head', 'redirect_canonical_history_replace', 5 ); 39 return; 40 } 41 elseif ( $maybe_redirect ) { 42 // protect against chained redirects 43 if ( !redirect_canonical($redirect_url, false) ) { 44 wp_redirect($redirect_url, 301); 45 exit(); 46 } else { 47 // Debug 48 // die("1: $redirect_url<br />2: " . redirect_canonical( $redirect_url, false ) ); 49 return; 50 } 51 } else { 52 return $redirect_url; 53 } 54 } 55 56 57 /** 13 58 * Redirects incoming links to the proper URL based on the site url. 14 59 * 15 60 * Search engines consider www.somedomain.com and somedomain.com to be two … … function redirect_canonical( $requested_url = null, $do_redirect = true ) { 96 141 } 97 142 98 143 if ( is_feed() && ( $id = get_query_var( 'p' ) ) ) { 144 add_filter( 'wp_javascript_redirect_canonical', '__return_false' ); 99 145 if ( $redirect_url = get_post_comments_feed_link( $id, get_query_var( 'feed' ) ) ) { 100 146 $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 147 $redirect['path'] = parse_url( $redirect_url, PHP_URL_PATH ); … … function redirect_canonical( $requested_url = null, $do_redirect = true ) { 117 163 118 164 // These tests give us a WP-generated permalink 119 165 if ( is_404() ) { 166 add_filter( 'wp_javascript_redirect_canonical', '__return_false' ); 120 167 121 168 // Redirect ?page_id, ?p=, ?attachment_id= to their respective url's 122 169 $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 418 $redirect['path'] = preg_replace('|/' . preg_quote( $wp_rewrite->index, '|' ) . '/*?$|', '/', $redirect['path']); 372 419 373 420 // Remove trailing spaces from the path 374 $redirect['path'] = preg_replace( '#(%20| )+$#', '', $redirect['path'] ); 421 $count = 0; 422 $redirect['path'] = preg_replace( '#(%20| )+$#', '', $redirect['path'], -1, $count ); 423 if ( $count > 0 ) { 424 add_filter( 'wp_javascript_redirect_canonical', '__return_false' ); 425 } 375 426 376 427 if ( !empty( $redirect['query'] ) ) { 377 428 // Remove trailing spaces from certain terminating query string args 378 $redirect['query'] = preg_replace( '#((p|page_id|cat|tag)=[^&]*?)(%20| )+$#', '$1', $redirect['query'] ); 429 $count = 0; 430 $redirect['query'] = preg_replace( '#((p|page_id|cat|tag)=[^&]*?)(%20| )+$#', '$1', $redirect['query'], -1, $count ); 431 if ( $count > 0 ) { 432 add_filter( 'wp_javascript_redirect_canonical', '__return_false' ); 433 } 379 434 380 435 // Clean up empty query strings 381 436 $redirect['query'] = trim(preg_replace( '#(^|&)(p|page_id|cat|tag)=?(&|$)#', '&', $redirect['query']), '&'); 382 437 383 438 // Redirect obsolete feeds 384 $redirect['query'] = preg_replace( '#(^|&)feed=rss(&|$)#', '$1feed=rss2$2', $redirect['query'] ); 439 $count = 0; 440 $redirect['query'] = preg_replace( '#(^|&)feed=rss(&|$)#', '$1feed=rss2$2', $redirect['query'], -1, $count ); 441 if ( $count > 0 ) { 442 add_filter( 'wp_javascript_redirect_canonical', '__return_false' ); 443 } 385 444 386 445 // Remove redundant leading ampersands 387 446 $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] ); … … function redirect_canonical( $requested_url = null, $do_redirect = true ) { 411 470 } 412 471 413 472 // Strip multiple slashes out of the URL 414 if ( strpos($redirect['path'], '//') > -1 ) 473 if ( strpos($redirect['path'], '//') > -1 ) { 474 add_filter( 'wp_javascript_redirect_canonical', '__return_false' ); 415 475 $redirect['path'] = preg_replace('|/+|', '/', $redirect['path']); 476 } 416 477 417 478 // Always trailing slash the Front Page URL 418 479 if ( trailingslashit( $redirect['path'] ) == trailingslashit( $user_home['path'] ) ) … … function redirect_canonical( $requested_url = null, $do_redirect = true ) { 425 486 $redirect['host'] = $original['host']; 426 487 427 488 $compare_original = array( $original['host'], $original['path'] ); 489 $original_origin = array( $original['scheme'], $original['host'] ); 428 490 429 if ( !empty( $original['port'] ) ) 491 if ( !empty( $original['port'] ) ) { 430 492 $compare_original[] = $original['port']; 493 $original_origin[] = $original['port']; 494 } 431 495 432 496 if ( !empty( $original['query'] ) ) 433 497 $compare_original[] = $original['query']; 434 498 435 499 $compare_redirect = array( $redirect['host'], $redirect['path'] ); 500 $redirect_origin = array( $redirect['scheme'], $redirect['host'] ); 436 501 437 if ( !empty( $redirect['port'] ) ) 502 if ( !empty( $redirect['port'] ) ) { 438 503 $compare_redirect[] = $redirect['port']; 504 $redirect_origin[] = $redirect['port']; 505 } 439 506 440 507 if ( !empty( $redirect['query'] ) ) 441 508 $compare_redirect[] = $redirect['query']; … … function redirect_canonical( $requested_url = null, $do_redirect = true ) { 449 516 $redirect_url .= '?' . $redirect['query']; 450 517 } 451 518 519 if ( $redirect_origin != $original_origin ) { 520 add_filter( 'wp_javascript_redirect_canonical', '__return_false' ); 521 } 522 452 523 if ( ! $redirect_url || $redirect_url == $requested_url ) { 453 524 return; 454 525 } … … function redirect_canonical( $requested_url = null, $do_redirect = true ) { 463 534 $requested_url = preg_replace_callback('|%[a-fA-F0-9][a-fA-F0-9]|', 'lowercase_octets', $requested_url); 464 535 } 465 536 537 $pre_filter_redirect_url = $redirect_url; 538 466 539 /** 467 540 * Filter the canonical redirect URL. 468 541 * … … function redirect_canonical( $requested_url = null, $do_redirect = true ) { 475 548 */ 476 549 $redirect_url = apply_filters( 'redirect_canonical', $redirect_url, $requested_url ); 477 550 551 if ( $pre_filter_redirect_url !== $redirect_url ) { 552 add_filter( 'wp_javascript_redirect_canonical', '__return_false' ); 553 } 554 478 555 // yes, again -- in case the filter aborted the request 479 556 if ( ! $redirect_url || $redirect_url == $requested_url ) { 480 557 return; … … function wp_redirect_admin_locations() { 595 672 exit; 596 673 } 597 674 } 675 676 /** 677 * Replace the URL as displayed in the browser with the canonical URL 678 */ 679 function redirect_canonical_history_replace() { 680 $canonical_url = redirect_canonical( null, false ); 681 ?> 682 <script> 683 (function( window, url ){ 684 var history = window.history; 685 if ( history.replaceState ) { 686 history.replaceState( {}, '', url+window.location.hash ); 687 } 688 }( window, '<?php echo esc_js( $canonical_url ); ?>' )); 689 </script> 690 <?php 691 } 692 No newline at end of file -
src/wp-includes/default-filters.php
diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index f9fd9c4..f2bdfee 100644
a b add_filter( 'style_loader_src', 'wp_style_loader_src', 10, 2 ); 389 389 add_action( 'init', 'create_initial_taxonomies', 0 ); // highest priority 390 390 391 391 // Canonical 392 add_action( 'template_redirect', ' redirect_canonical' );392 add_action( 'template_redirect', 'maybe_redirect_canonical' ); 393 393 add_action( 'template_redirect', 'wp_redirect_admin_locations', 1000 ); 394 394 395 395 // Shortcodes