Make WordPress Core

Ticket #33581: 33581.2.diff

File 33581.2.diff, 7.8 KB (added by peterwilsoncc, 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  
    1010 */
    1111
    1212/**
     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 */
     25function 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/**
    1358 * Redirects incoming links to the proper URL based on the site url.
    1459 *
    1560 * Search engines consider www.somedomain.com and somedomain.com to be two
    function redirect_canonical( $requested_url = null, $do_redirect = true ) { 
    96141        }
    97142
    98143        if ( is_feed() && ( $id = get_query_var( 'p' ) ) ) {
     144                add_filter( 'wp_javascript_redirect_canonical', '__return_false' );
    99145                if ( $redirect_url = get_post_comments_feed_link( $id, get_query_var( 'feed' ) ) ) {
    100146                        $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type', 'feed'), $redirect_url );
    101147                        $redirect['path'] = parse_url( $redirect_url, PHP_URL_PATH );
    function redirect_canonical( $requested_url = null, $do_redirect = true ) { 
    117163
    118164        // These tests give us a WP-generated permalink
    119165        if ( is_404() ) {
     166                add_filter( 'wp_javascript_redirect_canonical', '__return_false' );
    120167
    121168                // Redirect ?page_id, ?p=, ?attachment_id= to their respective url's
    122169                $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 ) { 
    371418        $redirect['path'] = preg_replace('|/' . preg_quote( $wp_rewrite->index, '|' ) . '/*?$|', '/', $redirect['path']);
    372419
    373420        // 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        }
    375426
    376427        if ( !empty( $redirect['query'] ) ) {
    377428                // 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                }
    379434
    380435                // Clean up empty query strings
    381436                $redirect['query'] = trim(preg_replace( '#(^|&)(p|page_id|cat|tag)=?(&|$)#', '&', $redirect['query']), '&');
    382437
    383438                // 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                }
    385444
    386445                // Remove redundant leading ampersands
    387446                $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] );
    function redirect_canonical( $requested_url = null, $do_redirect = true ) { 
    411470        }
    412471
    413472        // 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' );
    415475                $redirect['path'] = preg_replace('|/+|', '/', $redirect['path']);
     476        }
    416477
    417478        // Always trailing slash the Front Page URL
    418479        if ( trailingslashit( $redirect['path'] ) == trailingslashit( $user_home['path'] ) )
    function redirect_canonical( $requested_url = null, $do_redirect = true ) { 
    425486                $redirect['host'] = $original['host'];
    426487
    427488        $compare_original = array( $original['host'], $original['path'] );
     489        $original_origin = array( $original['scheme'], $original['host'] );
    428490
    429         if ( !empty( $original['port'] ) )
     491        if ( !empty( $original['port'] ) ) {
    430492                $compare_original[] = $original['port'];
     493                $original_origin[] = $original['port'];
     494        }
    431495
    432496        if ( !empty( $original['query'] ) )
    433497                $compare_original[] = $original['query'];
    434498
    435499        $compare_redirect = array( $redirect['host'], $redirect['path'] );
     500        $redirect_origin = array( $redirect['scheme'], $redirect['host'] );
    436501
    437         if ( !empty( $redirect['port'] ) )
     502        if ( !empty( $redirect['port'] ) ) {
    438503                $compare_redirect[] = $redirect['port'];
     504                $redirect_origin[] = $redirect['port'];
     505        }
    439506
    440507        if ( !empty( $redirect['query'] ) )
    441508                $compare_redirect[] = $redirect['query'];
    function redirect_canonical( $requested_url = null, $do_redirect = true ) { 
    449516                        $redirect_url .= '?' . $redirect['query'];
    450517        }
    451518
     519        if ( $redirect_origin != $original_origin ) {
     520                add_filter( 'wp_javascript_redirect_canonical', '__return_false' );
     521        }
     522
    452523        if ( ! $redirect_url || $redirect_url == $requested_url ) {
    453524                return;
    454525        }
    function redirect_canonical( $requested_url = null, $do_redirect = true ) { 
    463534                $requested_url = preg_replace_callback('|%[a-fA-F0-9][a-fA-F0-9]|', 'lowercase_octets', $requested_url);
    464535        }
    465536
     537        $pre_filter_redirect_url = $redirect_url;
     538
    466539        /**
    467540         * Filter the canonical redirect URL.
    468541         *
    function redirect_canonical( $requested_url = null, $do_redirect = true ) { 
    475548         */
    476549        $redirect_url = apply_filters( 'redirect_canonical', $redirect_url, $requested_url );
    477550
     551        if ( $pre_filter_redirect_url !== $redirect_url ) {
     552                add_filter( 'wp_javascript_redirect_canonical', '__return_false' );
     553        }
     554
    478555        // yes, again -- in case the filter aborted the request
    479556        if ( ! $redirect_url || $redirect_url == $requested_url ) {
    480557                return;
    function wp_redirect_admin_locations() { 
    595672                exit;
    596673        }
    597674}
     675
     676/**
     677 * Replace the URL as displayed in the browser with the canonical URL
     678 */
     679function 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 ); 
    389389add_action( 'init', 'create_initial_taxonomies', 0 ); // highest priority
    390390
    391391// Canonical
    392 add_action( 'template_redirect', 'redirect_canonical' );
     392add_action( 'template_redirect', 'maybe_redirect_canonical' );
    393393add_action( 'template_redirect', 'wp_redirect_admin_locations', 1000 );
    394394
    395395// Shortcodes