Make WordPress Core


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/canonical.php

    r19364 r20688  
    6767                $redirect['query'] = '';
    6868
     69        if ( is_feed() && ( $id = get_query_var( 'p' ) ) ) {
     70                if ( $redirect_url = get_post_comments_feed_link( $id, get_query_var( 'feed' ) ) ) {
     71                        $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type', 'feed'), $redirect_url );
     72                        $redirect['path'] = parse_url( $redirect_url, PHP_URL_PATH );
     73                }
     74        }
     75
    6976        if ( is_singular() && 1 > $wp_query->post_count && ($id = get_query_var('p')) ) {
    7077
    … …  
    7683
    7784                        if ( $redirect_url = get_permalink($id) )
    78                                 $redirect['query'] = remove_query_arg(array('p', 'page_id', 'attachment_id', 'post_type'), $redirect['query']);
     85                                $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url );
    7986                }
    8087        }
    … …  
    8996                        if ( $post_type_obj->public ) {
    9097                                $redirect_url = get_permalink($redirect_post);
    91                                 $redirect['query'] = remove_query_arg(array('p', 'page_id', 'attachment_id', 'post_type'), $redirect['query']);
    92                         }
    93                 }
    94 
    95                 if ( ! $redirect_url )
    96                         $redirect_url = redirect_guess_404_permalink();
     98                                $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url );
     99                        }
     100                }
     101
     102                if ( ! $redirect_url ) {
     103                        if ( $redirect_url = redirect_guess_404_permalink( $requested_url ) ) {
     104                                $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'page', 'feed', 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url );
     105                        }
     106                }
    97107
    98108        } elseif ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) {
    … …  
    192202
    193203                        }
    194                 } elseif ( is_single() && strpos($wp_rewrite->permalink_structure, '%category%') !== false ) {
    195                         $category = get_category_by_path(get_query_var('category_name'));
     204                } elseif ( is_single() && strpos($wp_rewrite->permalink_structure, '%category%') !== false && $cat = get_query_var( 'category_name' ) ) {
     205                        $category = get_category_by_path( $cat );
    196206                        $post_terms = wp_get_object_terms($wp_query->get_queried_object_id(), 'category', array('fields' => 'tt_ids'));
    197207                        if ( (!$category || is_wp_error($category)) || ( !is_wp_error($post_terms) && !empty($post_terms) && !in_array($category->term_taxonomy_id, $post_terms) ) )
    … …  
    200210
    201211                // Post Paging
    202                 if ( is_singular() && get_query_var('page') && $redirect_url ) {
     212                if ( is_singular() && ! is_front_page() && get_query_var('page') ) {
     213                        if ( !$redirect_url )
     214                                $redirect_url = get_permalink( get_queried_object_id() );
    203215                        $redirect_url = trailingslashit( $redirect_url ) . user_trailingslashit( get_query_var( 'page' ), 'single_paged' );
    204216                        $redirect['query'] = remove_query_arg( 'page', $redirect['query'] );
    … …  
    217229                        if ( is_feed() && in_array( get_query_var('feed'), $wp_rewrite->feeds ) ) {
    218230                                $addl_path = !empty( $addl_path ) ? trailingslashit($addl_path) : '';
    219                                 if ( get_query_var( 'withcomments' ) )
     231                                if ( !is_singular() && get_query_var( 'withcomments' ) )
    220232                                        $addl_path .= 'comments/';
    221233                                if ( ( 'rss' == get_default_feed() && 'feed' == get_query_var('feed') ) || 'rss' == get_query_var('feed') )
    … …  
    266278                        $redirect_url = $redirect['scheme'] . '://' . $redirect['host'] . $redirect['path'];
    267279                }
     280
     281                if ( 'wp-register.php' == basename( $redirect['path'] ) ) {
     282                        if ( is_multisite() )
     283                                $redirect_url = apply_filters( 'wp_signup_location', site_url( 'wp-signup.php' ) );
     284                        else
     285                                $redirect_url = site_url( 'wp-login.php?action=register' );
     286                        wp_redirect( $redirect_url, 301 );
     287                        die();
     288                }
    268289        }
    269290
    … …  
    281302                }
    282303
    283                 $_parsed_query = array_map( 'rawurlencode', $_parsed_query );
     304                $_parsed_query = rawurlencode_deep( $_parsed_query );
    284305                $redirect_url = add_query_arg( $_parsed_query, $redirect_url );
    285306        }
    … …  
    396417        }
    397418
    398         // Note that you can use the "redirect_canonical" filter to cancel a canonical redirect for whatever reason by returning FALSE
     419        // Note that you can use the "redirect_canonical" filter to cancel a canonical redirect for whatever reason by returning false
    399420        $redirect_url = apply_filters('redirect_canonical', $redirect_url, $requested_url);
    400421
    … …  
    418439
    419440/**
    420  * Attempts to guess correct post based on query vars.
     441 * Removes arguments from a query string if they are not present in a URL
     442 * DO NOT use this in plugin code.
     443 *
     444 * @since 3.4
     445 * @access private
     446 *
     447 * @return string The altered query string
     448 */
     449function _remove_qs_args_if_not_in_url( $query_string, Array $args_to_check, $url ) {
     450        $parsed_url = @parse_url( $url );
     451        if ( ! empty( $parsed_url['query'] ) ) {
     452                parse_str( $parsed_url['query'], $parsed_query );
     453                foreach ( $args_to_check as $qv ) {
     454                        if ( !isset( $parsed_query[$qv] ) )
     455                                $query_string = remove_query_arg( $qv, $query_string );
     456                }
     457        } else {
     458                $query_string = remove_query_arg( $args_to_check, $query_string );
     459        }
     460        return $query_string;
     461}
     462
     463/**
     464 * Attempts to guess the correct URL from the current URL (that produced a 404) or
     465 * the current query variables.
    421466 *
    422467 * @since 2.3.0
    423468 * @uses $wpdb
    424469 *
    425  * @return bool|string Returns False, if it can't find post, returns correct
    426  *              location on success.
     470 * @param string $current_url Optional. The URL that has 404'd.
     471 * @return bool|string The correct URL if one is found. False on failure.
    427472 */
    428 function redirect_guess_404_permalink() {
    429         global $wpdb;
    430 
    431         if ( !get_query_var('name') )
    432                 return false;
    433 
    434         $where = $wpdb->prepare("post_name LIKE %s", like_escape( get_query_var('name') ) . '%');
    435 
    436         // if any of post_type, year, monthnum, or day are set, use them to refine the query
    437         if ( get_query_var('post_type') )
    438                 $where .= $wpdb->prepare(" AND post_type = %s", get_query_var('post_type'));
    439         if ( get_query_var('year') )
    440                 $where .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var('year'));
    441         if ( get_query_var('monthnum') )
    442                 $where .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var('monthnum'));
    443         if ( get_query_var('day') )
    444                 $where .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var('day'));
    445 
    446         $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'");
    447         if ( !$post_id )
    448                 return false;
    449         return get_permalink($post_id);
     473function redirect_guess_404_permalink( $current_url = '' ) {
     474        global $wpdb, $wp_rewrite;
     475
     476        if ( ! empty( $current_url ) )
     477                $parsed_url = @parse_url( $current_url );
     478
     479        // Attempt to redirect bare category slugs if the permalink structure starts
     480        // with the %category% tag.
     481        if ( isset( $parsed_url['path'] )
     482                && preg_match( '#^[^%]+%category%#', $wp_rewrite->permalink_structure )
     483                && $cat = get_category_by_path( $parsed_url['path'] )
     484        ) {
     485                if ( ! is_wp_error( $cat ) )
     486                        return get_term_link( $cat );
     487        }
     488
     489        if ( get_query_var('name') ) {
     490                $where = $wpdb->prepare("post_name LIKE %s", like_escape( get_query_var('name') ) . '%');
     491
     492                // if any of post_type, year, monthnum, or day are set, use them to refine the query
     493                if ( get_query_var('post_type') )
     494                        $where .= $wpdb->prepare(" AND post_type = %s", get_query_var('post_type'));
     495                else
     496                        $where .= " AND post_type IN ('" . implode( "', '", get_post_types( array( 'public' => true ) ) ) . "')";
     497
     498                if ( get_query_var('year') )
     499                        $where .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var('year'));
     500                if ( get_query_var('monthnum') )
     501                        $where .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var('monthnum'));
     502                if ( get_query_var('day') )
     503                        $where .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var('day'));
     504
     505                $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'");
     506                if ( ! $post_id )
     507                        return false;
     508                if ( get_query_var( 'feed' ) )
     509                        return get_post_comments_feed_link( $post_id, get_query_var( 'feed' ) );
     510                elseif ( get_query_var( 'page' ) )
     511                        return trailingslashit( get_permalink( $post_id ) ) . user_trailingslashit( get_query_var( 'page' ), 'single_paged' );
     512                else
     513                        return get_permalink( $post_id );
     514        }
     515
     516        return false;
    450517}
    451518
    452519add_action('template_redirect', 'redirect_canonical');
    453520
    454 ?>
     521function wp_redirect_admin_locations() {
     522        global $wp_rewrite;
     523        if ( ! ( is_404() && $wp_rewrite->using_permalinks() ) )
     524                return;
     525
     526        $admins = array(
     527                home_url( 'wp-admin', 'relative' ),
     528                home_url( 'dashboard', 'relative' ),
     529                home_url( 'admin', 'relative' ),
     530                site_url( 'dashboard', 'relative' ),
     531                site_url( 'admin', 'relative' ),
     532        );
     533        if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $admins ) ) {
     534                wp_redirect( admin_url() );
     535                exit;
     536        }
     537
     538        $logins = array(
     539                home_url( 'wp-login.php', 'relative' ),
     540                home_url( 'login', 'relative' ),
     541                site_url( 'login', 'relative' ),
     542        );
     543        if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $logins ) ) {
     544                wp_redirect( site_url( 'wp-login.php', 'login' ) );
     545                exit;
     546        }
     547}
     548
     549add_action( 'template_redirect', 'wp_redirect_admin_locations', 1000 );
Note: See TracChangeset for help on using the changeset viewer.