~canonical-sysadmins/wordpress/4.5.3

« back to all changes in this revision

Viewing changes to wp-includes/canonical.php

  • Committer: Manuel Seelaus
  • Date: 2015-12-09 17:47:18 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: manuel.seelaus@canonical.com-20151209174718-coxethm2swbeqksy
Merge WP4.4 from upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
 * @global WP_Rewrite $wp_rewrite
32
32
 * @global bool $is_IIS
33
33
 * @global WP_Query $wp_query
34
 
 * @global wpdb $wpdb
 
34
 * @global wpdb $wpdb WordPress database abstraction object.
35
35
 *
36
36
 * @param string $requested_url Optional. The URL that was requested, used to
37
37
 *              figure if redirect is needed.
39
39
 * @return string|void The string of the URL, if redirect needed.
40
40
 */
41
41
function redirect_canonical( $requested_url = null, $do_redirect = true ) {
42
 
        global $wp_rewrite, $is_IIS, $wp_query, $wpdb;
 
42
        global $wp_rewrite, $is_IIS, $wp_query, $wpdb, $wp;
43
43
 
44
44
        if ( isset( $_SERVER['REQUEST_METHOD'] ) && ! in_array( strtoupper( $_SERVER['REQUEST_METHOD'] ), array( 'GET', 'HEAD' ) ) ) {
45
45
                return;
59
59
                return;
60
60
        }
61
61
 
62
 
        if ( !$requested_url ) {
 
62
        if ( ! $requested_url && isset( $_SERVER['HTTP_HOST'] ) ) {
63
63
                // build the URL in the address bar
64
64
                $requested_url  = is_ssl() ? 'https://' : 'http://';
65
65
                $requested_url .= $_SERVER['HTTP_HOST'];
67
67
        }
68
68
 
69
69
        $original = @parse_url($requested_url);
70
 
        if ( false === $original )
 
70
        if ( false === $original ) {
71
71
                return;
72
 
 
73
 
        // Some PHP setups turn requests for / into /index.php in REQUEST_URI
74
 
        // See: https://core.trac.wordpress.org/ticket/5017
75
 
        // See: https://core.trac.wordpress.org/ticket/7173
76
 
        // Disabled, for now:
77
 
        // $original['path'] = preg_replace('|/index\.php$|', '/', $original['path']);
 
72
        }
78
73
 
79
74
        $redirect = $original;
80
75
        $redirect_url = false;
122
117
                $id = max( get_query_var('p'), get_query_var('page_id'), get_query_var('attachment_id') );
123
118
                if ( $id && $redirect_post = get_post($id) ) {
124
119
                        $post_type_obj = get_post_type_object($redirect_post->post_type);
125
 
                        if ( $post_type_obj->public ) {
 
120
                        if ( $post_type_obj->public && 'auto-draft' != $redirect_post->post_status ) {
126
121
                                $redirect_url = get_permalink($redirect_post);
127
122
                                $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url );
128
123
                        }
148
143
                        }
149
144
                }
150
145
 
 
146
                if ( get_query_var( 'page' ) && $wp_query->post &&
 
147
                        false !== strpos( $wp_query->post->post_content, '<!--nextpage-->' ) ) {
 
148
                        $redirect['path'] = rtrim( $redirect['path'], (int) get_query_var( 'page' ) . '/' );
 
149
                        $redirect['query'] = remove_query_arg( 'page', $redirect['query'] );
 
150
                        $redirect_url = get_permalink( $wp_query->post->ID );
 
151
                }
 
152
 
151
153
        } elseif ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) {
152
154
                // rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101
153
 
                if ( is_attachment() && !empty($_GET['attachment_id']) && ! $redirect_url ) {
154
 
                        if ( $redirect_url = get_attachment_link(get_query_var('attachment_id')) )
155
 
                                $redirect['query'] = remove_query_arg('attachment_id', $redirect['query']);
 
155
                if ( is_attachment() &&
 
156
                        ! array_diff( array_keys( $wp->query_vars ), array( 'attachment', 'attachment_id' ) ) &&
 
157
                        ! $redirect_url ) {
 
158
                        if ( ! empty( $_GET['attachment_id'] ) ) {
 
159
                                $redirect_url = get_attachment_link( get_query_var( 'attachment_id' ) );
 
160
                                if ( $redirect_url ) {
 
161
                                        $redirect['query'] = remove_query_arg( 'attachment_id', $redirect['query'] );
 
162
                                }
 
163
                        } else {
 
164
                                $redirect_url = get_attachment_link();
 
165
                        }
156
166
                } elseif ( is_single() && !empty($_GET['p']) && ! $redirect_url ) {
157
167
                        if ( $redirect_url = get_permalink(get_query_var('p')) )
158
168
                                $redirect['query'] = remove_query_arg(array('p', 'post_type'), $redirect['query']);
308
318
                                }
309
319
                        }
310
320
 
311
 
                        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 ) ) ) {
 
321
                        if ( get_option( 'page_comments' ) && (
 
322
                                ( 'newest' == get_option( 'default_comments_page' ) && get_query_var( 'cpage' ) > 0 ) ||
 
323
                                ( 'newest' != get_option( 'default_comments_page' ) && get_query_var( 'cpage' ) > 1 )
 
324
                        ) ) {
312
325
                                $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit( $wp_rewrite->comments_pagination_base . '-' . get_query_var('cpage'), 'commentpaged' );
313
326
                                $redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] );
314
327
                        }
326
339
                                /** This filter is documented in wp-login.php */
327
340
                                $redirect_url = apply_filters( 'wp_signup_location', network_site_url( 'wp-signup.php' ) );
328
341
                        } else {
329
 
                                $redirect_url = site_url( 'wp-login.php?action=register' );
 
342
                                $redirect_url = wp_registration_url();
330
343
                        }
331
344
 
332
345
                        wp_redirect( $redirect_url, 301 );
476
489
        $redirect_url = apply_filters( 'redirect_canonical', $redirect_url, $requested_url );
477
490
 
478
491
        // yes, again -- in case the filter aborted the request
479
 
        if ( ! $redirect_url || $redirect_url == $requested_url ) {
 
492
        if ( ! $redirect_url || strip_fragment_from_url( $redirect_url ) == strip_fragment_from_url( $requested_url ) ) {
480
493
                return;
481
494
        }
482
495
 
522
535
}
523
536
 
524
537
/**
 
538
 * Strips the #fragment from a URL, if one is present.
 
539
 *
 
540
 * @since 4.4.0
 
541
 *
 
542
 * @param string $url The URL to strip.
 
543
 * @return string The altered URL.
 
544
 */
 
545
function strip_fragment_from_url( $url ) {
 
546
        $parsed_url = @parse_url( $url );
 
547
        if ( ! empty( $parsed_url['host'] ) ) {
 
548
                // This mirrors code in redirect_canonical(). It does not handle every case.
 
549
                $url = $parsed_url['scheme'] . '://' . $parsed_url['host'];
 
550
                if ( ! empty( $parsed_url['port'] ) ) {
 
551
                        $url .= ':' . $parsed_url['port'];
 
552
                }
 
553
                $url .= $parsed_url['path'];
 
554
                if ( ! empty( $parsed_url['query'] ) ) {
 
555
                        $url .= '?' . $parsed_url['query'];
 
556
                }
 
557
        }
 
558
 
 
559
        return $url;
 
560
}
 
561
 
 
562
/**
525
563
 * Attempts to guess the correct URL based on query vars
526
564
 *
527
565
 * @since 2.3.0
528
566
 *
529
567
 * @global wpdb $wpdb WordPress database abstraction object.
530
 
 * @global WP_Rewrite $wp_rewrite
531
568
 *
532
569
 * @return false|string The correct URL if one is found. False on failure.
533
570
 */
534
571
function redirect_guess_404_permalink() {
535
 
        global $wpdb, $wp_rewrite;
 
572
        global $wpdb;
536
573
 
537
574
        if ( get_query_var('name') ) {
538
575
                $where = $wpdb->prepare("post_name LIKE %s", $wpdb->esc_like( get_query_var('name') ) . '%');
555
592
                        return false;
556
593
                if ( get_query_var( 'feed' ) )
557
594
                        return get_post_comments_feed_link( $post_id, get_query_var( 'feed' ) );
558
 
                elseif ( get_query_var( 'page' ) )
 
595
                elseif ( get_query_var( 'page' ) && 1 < get_query_var( 'page' ) )
559
596
                        return trailingslashit( get_permalink( $post_id ) ) . user_trailingslashit( get_query_var( 'page' ), 'single_paged' );
560
597
                else
561
598
                        return get_permalink( $post_id );
591
628
                site_url( 'login', 'relative' ),
592
629
        );
593
630
        if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $logins ) ) {
594
 
                wp_redirect( site_url( 'wp-login.php', 'login' ) );
 
631
                wp_redirect( wp_login_url() );
595
632
                exit;
596
633
        }
597
634
}