Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (8 years ago)
Author:
pento
Message:

Code is Poetry.
WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.

File:
1 edited

Legend:

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

    r41991 r42343  
    3333 * @global WP_Query $wp_query
    3434 * @global wpdb $wpdb WordPress database abstraction object.
    35  * @global WP $wp Current WordPress environment instance. 
     35 * @global WP $wp Current WordPress environment instance.
    3636 *
    3737 * @param string $requested_url Optional. The URL that was requested, used to
    38  *      figure if redirect is needed.
     38 *      figure if redirect is needed.
    3939 * @param bool $do_redirect Optional. Redirect to the new URL.
    4040 * @return string|void The string of the URL, if redirect needed.
     
    5757    }
    5858
    59     if ( is_trackback() || is_search() || is_admin() || is_preview() || is_robots() || ( $is_IIS && !iis7_supports_permalinks() ) ) {
     59    if ( is_trackback() || is_search() || is_admin() || is_preview() || is_robots() || ( $is_IIS && ! iis7_supports_permalinks() ) ) {
    6060        return;
    6161    }
     
    6868    }
    6969
    70     $original = @parse_url($requested_url);
     70    $original = @parse_url( $requested_url );
    7171    if ( false === $original ) {
    7272        return;
    7373    }
    7474
    75     $redirect = $original;
     75    $redirect     = $original;
    7676    $redirect_url = false;
    7777
    7878    // Notice fixing
    79     if ( !isset($redirect['path']) )
     79    if ( ! isset( $redirect['path'] ) ) {
    8080        $redirect['path'] = '';
    81     if ( !isset($redirect['query']) )
     81    }
     82    if ( ! isset( $redirect['query'] ) ) {
    8283        $redirect['query'] = '';
     84    }
    8385
    8486    // If the original URL ended with non-breaking spaces, they were almost
     
    9496    if ( is_feed() && ( $id = get_query_var( 'p' ) ) ) {
    9597        if ( $redirect_url = get_post_comments_feed_link( $id, get_query_var( 'feed' ) ) ) {
    96             $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type', 'feed'), $redirect_url );
    97             $redirect['path'] = parse_url( $redirect_url, PHP_URL_PATH );
    98         }
    99     }
    100 
    101     if ( is_singular() && 1 > $wp_query->post_count && ($id = get_query_var('p')) ) {
    102 
    103         $vars = $wpdb->get_results( $wpdb->prepare("SELECT post_type, post_parent FROM $wpdb->posts WHERE ID = %d", $id) );
    104 
    105         if ( isset($vars[0]) && $vars = $vars[0] ) {
    106             if ( 'revision' == $vars->post_type && $vars->post_parent > 0 )
     98            $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type', 'feed' ), $redirect_url );
     99            $redirect['path']  = parse_url( $redirect_url, PHP_URL_PATH );
     100        }
     101    }
     102
     103    if ( is_singular() && 1 > $wp_query->post_count && ( $id = get_query_var( 'p' ) ) ) {
     104
     105        $vars = $wpdb->get_results( $wpdb->prepare( "SELECT post_type, post_parent FROM $wpdb->posts WHERE ID = %d", $id ) );
     106
     107        if ( isset( $vars[0] ) && $vars = $vars[0] ) {
     108            if ( 'revision' == $vars->post_type && $vars->post_parent > 0 ) {
    107109                $id = $vars->post_parent;
    108 
    109             if ( $redirect_url = get_permalink($id) )
     110            }
     111
     112            if ( $redirect_url = get_permalink( $id ) ) {
    110113                $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url );
     114            }
    111115        }
    112116    }
     
    116120
    117121        // Redirect ?page_id, ?p=, ?attachment_id= to their respective url's
    118         $id = max( get_query_var('p'), get_query_var('page_id'), get_query_var('attachment_id') );
    119         if ( $id && $redirect_post = get_post($id) ) {
    120             $post_type_obj = get_post_type_object($redirect_post->post_type);
     122        $id = max( get_query_var( 'p' ), get_query_var( 'page_id' ), get_query_var( 'attachment_id' ) );
     123        if ( $id && $redirect_post = get_post( $id ) ) {
     124            $post_type_obj = get_post_type_object( $redirect_post->post_type );
    121125            if ( $post_type_obj->public && 'auto-draft' != $redirect_post->post_status ) {
    122                 $redirect_url = get_permalink($redirect_post);
     126                $redirect_url      = get_permalink( $redirect_post );
    123127                $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url );
    124128            }
     
    131135            $date  = sprintf( '%04d-%02d-%02d', $year, $month, $day );
    132136            if ( ! wp_checkdate( $month, $day, $year, $date ) ) {
    133                 $redirect_url = get_month_link( $year, $month );
     137                $redirect_url      = get_month_link( $year, $month );
    134138                $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'year', 'monthnum', 'day' ), $redirect_url );
    135139            }
    136140        } elseif ( get_query_var( 'monthnum' ) && get_query_var( 'year' ) && 12 < get_query_var( 'monthnum' ) ) {
    137             $redirect_url = get_year_link( get_query_var( 'year' ) );
     141            $redirect_url      = get_year_link( get_query_var( 'year' ) );
    138142            $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'year', 'monthnum' ), $redirect_url );
    139143        }
     
    147151        if ( get_query_var( 'page' ) && $wp_query->post &&
    148152            false !== strpos( $wp_query->post->post_content, '<!--nextpage-->' ) ) {
    149             $redirect['path'] = rtrim( $redirect['path'], (int) get_query_var( 'page' ) . '/' );
     153            $redirect['path']  = rtrim( $redirect['path'], (int) get_query_var( 'page' ) . '/' );
    150154            $redirect['query'] = remove_query_arg( 'page', $redirect['query'] );
    151             $redirect_url = get_permalink( $wp_query->post->ID );
    152         }
    153 
    154     } elseif ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) {
     155            $redirect_url      = get_permalink( $wp_query->post->ID );
     156        }
     157    } elseif ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) {
    155158        // rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101
    156159        if ( is_attachment() &&
     
    165168                $redirect_url = get_attachment_link();
    166169            }
    167         } elseif ( is_single() && !empty($_GET['p']) && ! $redirect_url ) {
    168             if ( $redirect_url = get_permalink(get_query_var('p')) )
    169                 $redirect['query'] = remove_query_arg(array('p', 'post_type'), $redirect['query']);
    170         } elseif ( is_single() && !empty($_GET['name'])  && ! $redirect_url ) {
    171             if ( $redirect_url = get_permalink( $wp_query->get_queried_object_id() ) )
    172                 $redirect['query'] = remove_query_arg('name', $redirect['query']);
    173         } elseif ( is_page() && !empty($_GET['page_id']) && ! $redirect_url ) {
    174             if ( $redirect_url = get_permalink(get_query_var('page_id')) )
    175                 $redirect['query'] = remove_query_arg('page_id', $redirect['query']);
    176         } elseif ( is_page() && !is_feed() && 'page' == get_option('show_on_front') && get_queried_object_id() == get_option('page_on_front')  && ! $redirect_url ) {
    177             $redirect_url = home_url('/');
    178         } elseif ( is_home() && !empty($_GET['page_id']) && 'page' == get_option('show_on_front') && get_query_var('page_id') == get_option('page_for_posts')  && ! $redirect_url ) {
    179             if ( $redirect_url = get_permalink(get_option('page_for_posts')) )
    180                 $redirect['query'] = remove_query_arg('page_id', $redirect['query']);
    181         } elseif ( !empty($_GET['m']) && ( is_year() || is_month() || is_day() ) ) {
    182             $m = get_query_var('m');
    183             switch ( strlen($m) ) {
     170        } elseif ( is_single() && ! empty( $_GET['p'] ) && ! $redirect_url ) {
     171            if ( $redirect_url = get_permalink( get_query_var( 'p' ) ) ) {
     172                $redirect['query'] = remove_query_arg( array( 'p', 'post_type' ), $redirect['query'] );
     173            }
     174        } elseif ( is_single() && ! empty( $_GET['name'] ) && ! $redirect_url ) {
     175            if ( $redirect_url = get_permalink( $wp_query->get_queried_object_id() ) ) {
     176                $redirect['query'] = remove_query_arg( 'name', $redirect['query'] );
     177            }
     178        } elseif ( is_page() && ! empty( $_GET['page_id'] ) && ! $redirect_url ) {
     179            if ( $redirect_url = get_permalink( get_query_var( 'page_id' ) ) ) {
     180                $redirect['query'] = remove_query_arg( 'page_id', $redirect['query'] );
     181            }
     182        } elseif ( is_page() && ! is_feed() && 'page' == get_option( 'show_on_front' ) && get_queried_object_id() == get_option( 'page_on_front' ) && ! $redirect_url ) {
     183            $redirect_url = home_url( '/' );
     184        } elseif ( is_home() && ! empty( $_GET['page_id'] ) && 'page' == get_option( 'show_on_front' ) && get_query_var( 'page_id' ) == get_option( 'page_for_posts' ) && ! $redirect_url ) {
     185            if ( $redirect_url = get_permalink( get_option( 'page_for_posts' ) ) ) {
     186                $redirect['query'] = remove_query_arg( 'page_id', $redirect['query'] );
     187            }
     188        } elseif ( ! empty( $_GET['m'] ) && ( is_year() || is_month() || is_day() ) ) {
     189            $m = get_query_var( 'm' );
     190            switch ( strlen( $m ) ) {
    184191                case 4: // Yearly
    185                     $redirect_url = get_year_link($m);
     192                    $redirect_url = get_year_link( $m );
    186193                    break;
    187194                case 6: // Monthly
    188                     $redirect_url = get_month_link( substr($m, 0, 4), substr($m, 4, 2) );
     195                    $redirect_url = get_month_link( substr( $m, 0, 4 ), substr( $m, 4, 2 ) );
    189196                    break;
    190197                case 8: // Daily
    191                     $redirect_url = get_day_link(substr($m, 0, 4), substr($m, 4, 2), substr($m, 6, 2));
     198                    $redirect_url = get_day_link( substr( $m, 0, 4 ), substr( $m, 4, 2 ), substr( $m, 6, 2 ) );
    192199                    break;
    193200            }
    194             if ( $redirect_url )
    195                 $redirect['query'] = remove_query_arg('m', $redirect['query']);
    196         // now moving on to non ?m=X year/month/day links
    197         } elseif ( is_day() && get_query_var('year') && get_query_var('monthnum') && !empty($_GET['day']) ) {
    198             if ( $redirect_url = get_day_link(get_query_var('year'), get_query_var('monthnum'), get_query_var('day')) )
    199                 $redirect['query'] = remove_query_arg(array('year', 'monthnum', 'day'), $redirect['query']);
    200         } elseif ( is_month() && get_query_var('year') && !empty($_GET['monthnum']) ) {
    201             if ( $redirect_url = get_month_link(get_query_var('year'), get_query_var('monthnum')) )
    202                 $redirect['query'] = remove_query_arg(array('year', 'monthnum'), $redirect['query']);
    203         } elseif ( is_year() && !empty($_GET['year']) ) {
    204             if ( $redirect_url = get_year_link(get_query_var('year')) )
    205                 $redirect['query'] = remove_query_arg('year', $redirect['query']);
    206         } elseif ( is_author() && !empty($_GET['author']) && preg_match( '|^[0-9]+$|', $_GET['author'] ) ) {
    207             $author = get_userdata(get_query_var('author'));
     201            if ( $redirect_url ) {
     202                $redirect['query'] = remove_query_arg( 'm', $redirect['query'] );
     203            }
     204            // now moving on to non ?m=X year/month/day links
     205        } elseif ( is_day() && get_query_var( 'year' ) && get_query_var( 'monthnum' ) && ! empty( $_GET['day'] ) ) {
     206            if ( $redirect_url = get_day_link( get_query_var( 'year' ), get_query_var( 'monthnum' ), get_query_var( 'day' ) ) ) {
     207                $redirect['query'] = remove_query_arg( array( 'year', 'monthnum', 'day' ), $redirect['query'] );
     208            }
     209        } elseif ( is_month() && get_query_var( 'year' ) && ! empty( $_GET['monthnum'] ) ) {
     210            if ( $redirect_url = get_month_link( get_query_var( 'year' ), get_query_var( 'monthnum' ) ) ) {
     211                $redirect['query'] = remove_query_arg( array( 'year', 'monthnum' ), $redirect['query'] );
     212            }
     213        } elseif ( is_year() && ! empty( $_GET['year'] ) ) {
     214            if ( $redirect_url = get_year_link( get_query_var( 'year' ) ) ) {
     215                $redirect['query'] = remove_query_arg( 'year', $redirect['query'] );
     216            }
     217        } elseif ( is_author() && ! empty( $_GET['author'] ) && preg_match( '|^[0-9]+$|', $_GET['author'] ) ) {
     218            $author = get_userdata( get_query_var( 'author' ) );
    208219            if ( ( false !== $author ) && $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE $wpdb->posts.post_author = %d AND $wpdb->posts.post_status = 'publish' LIMIT 1", $author->ID ) ) ) {
    209                 if ( $redirect_url = get_author_posts_url($author->ID, $author->user_nicename) )
    210                     $redirect['query'] = remove_query_arg('author', $redirect['query']);
     220                if ( $redirect_url = get_author_posts_url( $author->ID, $author->user_nicename ) ) {
     221                    $redirect['query'] = remove_query_arg( 'author', $redirect['query'] );
     222                }
    211223            }
    212224        } elseif ( is_category() || is_tag() || is_tax() ) { // Terms (Tags/categories)
    213225
    214226            $term_count = 0;
    215             foreach ( $wp_query->tax_query->queried_terms as $tax_query )
     227            foreach ( $wp_query->tax_query->queried_terms as $tax_query ) {
    216228                $term_count += count( $tax_query['terms'] );
     229            }
    217230
    218231            $obj = $wp_query->get_queried_object();
    219             if ( $term_count <= 1 && !empty($obj->term_id) && ( $tax_url = get_term_link((int)$obj->term_id, $obj->taxonomy) ) && !is_wp_error($tax_url) ) {
    220                 if ( !empty($redirect['query']) ) {
     232            if ( $term_count <= 1 && ! empty( $obj->term_id ) && ( $tax_url = get_term_link( (int) $obj->term_id, $obj->taxonomy ) ) && ! is_wp_error( $tax_url ) ) {
     233                if ( ! empty( $redirect['query'] ) ) {
    221234                    // Strip taxonomy query vars off the url.
    222                     $qv_remove = array( 'term', 'taxonomy');
     235                    $qv_remove = array( 'term', 'taxonomy' );
    223236                    if ( is_category() ) {
    224237                        $qv_remove[] = 'category_name';
     
    229242                    } else { // Custom taxonomies will have a custom query var, remove those too:
    230243                        $tax_obj = get_taxonomy( $obj->taxonomy );
    231                         if ( false !== $tax_obj->query_var )
     244                        if ( false !== $tax_obj->query_var ) {
    232245                            $qv_remove[] = $tax_obj->query_var;
     246                        }
    233247                    }
    234248
    235                     $rewrite_vars = array_diff( array_keys($wp_query->query), array_keys($_GET) );
    236 
    237                     if ( !array_diff($rewrite_vars, array_keys($_GET)) ) { // Check to see if all the Query vars are coming from the rewrite, none are set via $_GET
    238                         $redirect['query'] = remove_query_arg($qv_remove, $redirect['query']); //Remove all of the per-tax qv's
     249                    $rewrite_vars = array_diff( array_keys( $wp_query->query ), array_keys( $_GET ) );
     250
     251                    if ( ! array_diff( $rewrite_vars, array_keys( $_GET ) ) ) { // Check to see if all the Query vars are coming from the rewrite, none are set via $_GET
     252                        $redirect['query'] = remove_query_arg( $qv_remove, $redirect['query'] ); //Remove all of the per-tax qv's
    239253
    240254                        // Create the destination url for this taxonomy
    241                         $tax_url = parse_url($tax_url);
    242                         if ( ! empty($tax_url['query']) ) { // Taxonomy accessible via ?taxonomy=..&term=.. or any custom qv..
    243                             parse_str($tax_url['query'], $query_vars);
    244                             $redirect['query'] = add_query_arg($query_vars, $redirect['query']);
     255                        $tax_url = parse_url( $tax_url );
     256                        if ( ! empty( $tax_url['query'] ) ) { // Taxonomy accessible via ?taxonomy=..&term=.. or any custom qv..
     257                            parse_str( $tax_url['query'], $query_vars );
     258                            $redirect['query'] = add_query_arg( $query_vars, $redirect['query'] );
    245259                        } else { // Taxonomy is accessible via a "pretty-URL"
    246260                            $redirect['path'] = $tax_url['path'];
    247261                        }
    248 
    249262                    } else { // Some query vars are set via $_GET. Unset those from $_GET that exist via the rewrite
    250263                        foreach ( $qv_remove as $_qv ) {
    251                             if ( isset($rewrite_vars[$_qv]) )
    252                                 $redirect['query'] = remove_query_arg($_qv, $redirect['query']);
     264                            if ( isset( $rewrite_vars[ $_qv ] ) ) {
     265                                $redirect['query'] = remove_query_arg( $_qv, $redirect['query'] );
     266                            }
    253267                        }
    254268                    }
    255269                }
    256 
    257             }
    258         } elseif ( is_single() && strpos($wp_rewrite->permalink_structure, '%category%') !== false && $cat = get_query_var( 'category_name' ) ) {
     270            }
     271        } elseif ( is_single() && strpos( $wp_rewrite->permalink_structure, '%category%' ) !== false && $cat = get_query_var( 'category_name' ) ) {
    259272            $category = get_category_by_path( $cat );
    260273            if ( ( ! $category || is_wp_error( $category ) ) || ! has_term( $category->term_id, 'category', $wp_query->get_queried_object_id() ) ) {
    261                 $redirect_url = get_permalink($wp_query->get_queried_object_id());
    262             }
    263         }
    264 
    265         // Post Paging
    266         if ( is_singular() && get_query_var('page') ) {
    267             if ( !$redirect_url )
     274                $redirect_url = get_permalink( $wp_query->get_queried_object_id() );
     275            }
     276        }
     277
     278            // Post Paging
     279        if ( is_singular() && get_query_var( 'page' ) ) {
     280            if ( ! $redirect_url ) {
    268281                $redirect_url = get_permalink( get_queried_object_id() );
     282            }
    269283
    270284            $page = get_query_var( 'page' );
     
    276290                }
    277291            }
    278             $redirect['query'] = remove_query_arg( 'page', $redirect['query'] );
    279         }
    280 
    281         // paging and feeds
    282         if ( get_query_var('paged') || is_feed() || get_query_var('cpage') ) {
     292                $redirect['query'] = remove_query_arg( 'page', $redirect['query'] );
     293        }
     294
     295            // paging and feeds
     296        if ( get_query_var( 'paged' ) || is_feed() || get_query_var( 'cpage' ) ) {
    283297            while ( preg_match( "#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", $redirect['path'] ) || preg_match( '#/(comments/?)?(feed|rss|rdf|atom|rss2)(/+)?$#', $redirect['path'] ) || preg_match( "#/{$wp_rewrite->comments_pagination_base}-[0-9]+(/+)?$#", $redirect['path'] ) ) {
    284298                // Strip off paging and feed
    285                 $redirect['path'] = preg_replace("#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", '/', $redirect['path']); // strip off any existing paging
    286                 $redirect['path'] = preg_replace('#/(comments/?)?(feed|rss2?|rdf|atom)(/+|$)#', '/', $redirect['path']); // strip off feed endings
    287                 $redirect['path'] = preg_replace("#/{$wp_rewrite->comments_pagination_base}-[0-9]+?(/+)?$#", '/', $redirect['path']); // strip off any existing comment paging
     299                $redirect['path'] = preg_replace( "#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", '/', $redirect['path'] ); // strip off any existing paging
     300                $redirect['path'] = preg_replace( '#/(comments/?)?(feed|rss2?|rdf|atom)(/+|$)#', '/', $redirect['path'] ); // strip off feed endings
     301                $redirect['path'] = preg_replace( "#/{$wp_rewrite->comments_pagination_base}-[0-9]+?(/+)?$#", '/', $redirect['path'] ); // strip off any existing comment paging
    288302            }
    289303
    290304            $addl_path = '';
    291             if ( is_feed() && in_array( get_query_var('feed'), $wp_rewrite->feeds ) ) {
    292                 $addl_path = !empty( $addl_path ) ? trailingslashit($addl_path) : '';
    293                 if ( !is_singular() && get_query_var( 'withcomments' ) )
     305            if ( is_feed() && in_array( get_query_var( 'feed' ), $wp_rewrite->feeds ) ) {
     306                $addl_path = ! empty( $addl_path ) ? trailingslashit( $addl_path ) : '';
     307                if ( ! is_singular() && get_query_var( 'withcomments' ) ) {
    294308                    $addl_path .= 'comments/';
    295                 if ( ( 'rss' == get_default_feed() && 'feed' == get_query_var('feed') ) || 'rss' == get_query_var('feed') )
     309                }
     310                if ( ( 'rss' == get_default_feed() && 'feed' == get_query_var( 'feed' ) ) || 'rss' == get_query_var( 'feed' ) ) {
    296311                    $addl_path .= user_trailingslashit( 'feed/' . ( ( get_default_feed() == 'rss2' ) ? '' : 'rss2' ), 'feed' );
    297                 else
    298                     $addl_path .= user_trailingslashit( 'feed/' . ( ( get_default_feed() ==  get_query_var('feed') || 'feed' == get_query_var('feed') ) ? '' : get_query_var('feed') ), 'feed' );
     312                } else {
     313                    $addl_path .= user_trailingslashit( 'feed/' . ( ( get_default_feed() == get_query_var( 'feed' ) || 'feed' == get_query_var( 'feed' ) ) ? '' : get_query_var( 'feed' ) ), 'feed' );
     314                }
    299315                $redirect['query'] = remove_query_arg( 'feed', $redirect['query'] );
    300             } elseif ( is_feed() && 'old' == get_query_var('feed') ) {
     316            } elseif ( is_feed() && 'old' == get_query_var( 'feed' ) ) {
    301317                $old_feed_files = array(
    302318                    'wp-atom.php'         => 'atom',
     
    314330            }
    315331
    316             if ( get_query_var('paged') > 0 ) {
    317                 $paged = get_query_var('paged');
     332            if ( get_query_var( 'paged' ) > 0 ) {
     333                $paged             = get_query_var( 'paged' );
    318334                $redirect['query'] = remove_query_arg( 'paged', $redirect['query'] );
    319                 if ( !is_feed() ) {
    320                     if ( $paged > 1 && !is_single() ) {
    321                         $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit("$wp_rewrite->pagination_base/$paged", 'paged');
    322                     } elseif ( !is_single() ) {
    323                         $addl_path = !empty( $addl_path ) ? trailingslashit($addl_path) : '';
     335                if ( ! is_feed() ) {
     336                    if ( $paged > 1 && ! is_single() ) {
     337                        $addl_path = ( ! empty( $addl_path ) ? trailingslashit( $addl_path ) : '' ) . user_trailingslashit( "$wp_rewrite->pagination_base/$paged", 'paged' );
     338                    } elseif ( ! is_single() ) {
     339                        $addl_path = ! empty( $addl_path ) ? trailingslashit( $addl_path ) : '';
    324340                    }
    325341                } elseif ( $paged > 1 ) {
     
    329345
    330346            if ( get_option( 'page_comments' ) && (
    331                 ( 'newest' == get_option( 'default_comments_page' ) && get_query_var( 'cpage' ) > 0 ) ||
    332                 ( 'newest' != get_option( 'default_comments_page' ) && get_query_var( 'cpage' ) > 1 )
     347            ( 'newest' == get_option( 'default_comments_page' ) && get_query_var( 'cpage' ) > 0 ) ||
     348            ( 'newest' != get_option( 'default_comments_page' ) && get_query_var( 'cpage' ) > 1 )
    333349            ) ) {
    334                 $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit( $wp_rewrite->comments_pagination_base . '-' . get_query_var('cpage'), 'commentpaged' );
     350                $addl_path         = ( ! empty( $addl_path ) ? trailingslashit( $addl_path ) : '' ) . user_trailingslashit( $wp_rewrite->comments_pagination_base . '-' . get_query_var( 'cpage' ), 'commentpaged' );
    335351                $redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] );
    336352            }
    337353
    338             $redirect['path'] = user_trailingslashit( preg_replace('|/' . preg_quote( $wp_rewrite->index, '|' ) . '/?$|', '/', $redirect['path']) ); // strip off trailing /index.php/
    339             if ( !empty( $addl_path ) && $wp_rewrite->using_index_permalinks() && strpos($redirect['path'], '/' . $wp_rewrite->index . '/') === false )
    340                 $redirect['path'] = trailingslashit($redirect['path']) . $wp_rewrite->index . '/';
    341             if ( !empty( $addl_path ) )
    342                 $redirect['path'] = trailingslashit($redirect['path']) . $addl_path;
     354            $redirect['path'] = user_trailingslashit( preg_replace( '|/' . preg_quote( $wp_rewrite->index, '|' ) . '/?$|', '/', $redirect['path'] ) ); // strip off trailing /index.php/
     355            if ( ! empty( $addl_path ) && $wp_rewrite->using_index_permalinks() && strpos( $redirect['path'], '/' . $wp_rewrite->index . '/' ) === false ) {
     356                $redirect['path'] = trailingslashit( $redirect['path'] ) . $wp_rewrite->index . '/';
     357            }
     358            if ( ! empty( $addl_path ) ) {
     359                $redirect['path'] = trailingslashit( $redirect['path'] ) . $addl_path;
     360            }
    343361            $redirect_url = $redirect['scheme'] . '://' . $redirect['host'] . $redirect['path'];
    344362        }
     
    359377    // tack on any additional query vars
    360378    $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] );
    361     if ( $redirect_url && !empty($redirect['query']) ) {
     379    if ( $redirect_url && ! empty( $redirect['query'] ) ) {
    362380        parse_str( $redirect['query'], $_parsed_query );
    363         $redirect = @parse_url($redirect_url);
     381        $redirect = @parse_url( $redirect_url );
    364382
    365383        if ( ! empty( $_parsed_query['name'] ) && ! empty( $redirect['query'] ) ) {
    366384            parse_str( $redirect['query'], $_parsed_redirect_query );
    367385
    368             if ( empty( $_parsed_redirect_query['name'] ) )
     386            if ( empty( $_parsed_redirect_query['name'] ) ) {
    369387                unset( $_parsed_query['name'] );
     388            }
    370389        }
    371390
    372391        $_parsed_query = rawurlencode_deep( $_parsed_query );
    373         $redirect_url = add_query_arg( $_parsed_query, $redirect_url );
    374     }
    375 
    376     if ( $redirect_url )
    377         $redirect = @parse_url($redirect_url);
     392        $redirect_url  = add_query_arg( $_parsed_query, $redirect_url );
     393    }
     394
     395    if ( $redirect_url ) {
     396        $redirect = @parse_url( $redirect_url );
     397    }
    378398
    379399    // www.example.com vs example.com
    380     $user_home = @parse_url(home_url());
    381     if ( !empty($user_home['host']) )
     400    $user_home = @parse_url( home_url() );
     401    if ( ! empty( $user_home['host'] ) ) {
    382402        $redirect['host'] = $user_home['host'];
    383     if ( empty($user_home['path']) )
     403    }
     404    if ( empty( $user_home['path'] ) ) {
    384405        $user_home['path'] = '/';
     406    }
    385407
    386408    // Handle ports
    387     if ( !empty($user_home['port']) )
     409    if ( ! empty( $user_home['port'] ) ) {
    388410        $redirect['port'] = $user_home['port'];
    389     else
    390         unset($redirect['port']);
     411    } else {
     412        unset( $redirect['port'] );
     413    }
    391414
    392415    // trailing /index.php
    393     $redirect['path'] = preg_replace('|/' . preg_quote( $wp_rewrite->index, '|' ) . '/*?$|', '/', $redirect['path']);
    394 
    395     $punctuation_pattern = implode( '|', array_map( 'preg_quote', array(
    396         ' ', '%20',  // space
    397         '!', '%21',  // exclamation mark
    398         '"', '%22',  // double quote
    399         "'", '%27',  // single quote
    400         '(', '%28',  // opening bracket
    401         ')', '%29',  // closing bracket
    402         ',', '%2C',  // comma
    403         '.', '%2E',  // period
    404         ';', '%3B',  // semicolon
    405         '{', '%7B',  // opening curly bracket
    406         '}', '%7D',  // closing curly bracket
    407         '%E2%80%9C', // opening curly quote
    408         '%E2%80%9D', // closing curly quote
    409     ) ) );
     416    $redirect['path'] = preg_replace( '|/' . preg_quote( $wp_rewrite->index, '|' ) . '/*?$|', '/', $redirect['path'] );
     417
     418    $punctuation_pattern = implode(
     419        '|', array_map(
     420            'preg_quote', array(
     421                ' ',
     422                '%20',  // space
     423                '!',
     424                '%21',  // exclamation mark
     425                '"',
     426                '%22',  // double quote
     427                "'",
     428                '%27',  // single quote
     429                '(',
     430                '%28',  // opening bracket
     431                ')',
     432                '%29',  // closing bracket
     433                ',',
     434                '%2C',  // comma
     435                '.',
     436                '%2E',  // period
     437                ';',
     438                '%3B',  // semicolon
     439                '{',
     440                '%7B',  // opening curly bracket
     441                '}',
     442                '%7D',  // closing curly bracket
     443                '%E2%80%9C', // opening curly quote
     444                '%E2%80%9D', // closing curly quote
     445            )
     446        )
     447    );
    410448
    411449    // Remove trailing spaces and end punctuation from the path.
    412450    $redirect['path'] = preg_replace( "#($punctuation_pattern)+$#", '', $redirect['path'] );
    413451
    414     if ( !empty( $redirect['query'] ) ) {
     452    if ( ! empty( $redirect['query'] ) ) {
    415453        // Remove trailing spaces and end punctuation from certain terminating query string args.
    416454        $redirect['query'] = preg_replace( "#((p|page_id|cat|tag)=[^&]*?)($punctuation_pattern)+$#", '$1', $redirect['query'] );
    417455
    418456        // Clean up empty query strings
    419         $redirect['query'] = trim(preg_replace( '#(^|&)(p|page_id|cat|tag)=?(&|$)#', '&', $redirect['query']), '&');
     457        $redirect['query'] = trim( preg_replace( '#(^|&)(p|page_id|cat|tag)=?(&|$)#', '&', $redirect['query'] ), '&' );
    420458
    421459        // Redirect obsolete feeds
     
    427465
    428466    // strip /index.php/ when we're not using PATHINFO permalinks
    429     if ( !$wp_rewrite->using_index_permalinks() )
     467    if ( ! $wp_rewrite->using_index_permalinks() ) {
    430468        $redirect['path'] = str_replace( '/' . $wp_rewrite->index . '/', '/', $redirect['path'] );
     469    }
    431470
    432471    // trailing slashes
    433     if ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() && !is_404() && (!is_front_page() || ( is_front_page() && (get_query_var('paged') > 1) ) ) ) {
     472    if ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() && ! is_404() && ( ! is_front_page() || ( is_front_page() && ( get_query_var( 'paged' ) > 1 ) ) ) ) {
    434473        $user_ts_type = '';
    435         if ( get_query_var('paged') > 0 ) {
     474        if ( get_query_var( 'paged' ) > 0 ) {
    436475            $user_ts_type = 'paged';
    437476        } else {
    438             foreach ( array('single', 'category', 'page', 'day', 'month', 'year', 'home') as $type ) {
     477            foreach ( array( 'single', 'category', 'page', 'day', 'month', 'year', 'home' ) as $type ) {
    439478                $func = 'is_' . $type;
    440                 if ( call_user_func($func) ) {
     479                if ( call_user_func( $func ) ) {
    441480                    $user_ts_type = $type;
    442481                    break;
     
    444483            }
    445484        }
    446         $redirect['path'] = user_trailingslashit($redirect['path'], $user_ts_type);
     485        $redirect['path'] = user_trailingslashit( $redirect['path'], $user_ts_type );
    447486    } elseif ( is_front_page() ) {
    448         $redirect['path'] = trailingslashit($redirect['path']);
     487        $redirect['path'] = trailingslashit( $redirect['path'] );
    449488    }
    450489
    451490    // Strip multiple slashes out of the URL
    452     if ( strpos($redirect['path'], '//') > -1 )
    453         $redirect['path'] = preg_replace('|/+|', '/', $redirect['path']);
     491    if ( strpos( $redirect['path'], '//' ) > -1 ) {
     492        $redirect['path'] = preg_replace( '|/+|', '/', $redirect['path'] );
     493    }
    454494
    455495    // Always trailing slash the Front Page URL
    456     if ( trailingslashit( $redirect['path'] ) == trailingslashit( $user_home['path'] ) )
    457         $redirect['path'] = trailingslashit($redirect['path']);
     496    if ( trailingslashit( $redirect['path'] ) == trailingslashit( $user_home['path'] ) ) {
     497        $redirect['path'] = trailingslashit( $redirect['path'] );
     498    }
    458499
    459500    // Ignore differences in host capitalization, as this can lead to infinite redirects
    460501    // Only redirect no-www <=> yes-www
    461     if ( strtolower($original['host']) == strtolower($redirect['host']) ||
    462         ( strtolower($original['host']) != 'www.' . strtolower($redirect['host']) && 'www.' . strtolower($original['host']) != strtolower($redirect['host']) ) )
     502    if ( strtolower( $original['host'] ) == strtolower( $redirect['host'] ) ||
     503        ( strtolower( $original['host'] ) != 'www.' . strtolower( $redirect['host'] ) && 'www.' . strtolower( $original['host'] ) != strtolower( $redirect['host'] ) ) ) {
    463504        $redirect['host'] = $original['host'];
     505    }
    464506
    465507    $compare_original = array( $original['host'], $original['path'] );
    466508
    467     if ( !empty( $original['port'] ) )
     509    if ( ! empty( $original['port'] ) ) {
    468510        $compare_original[] = $original['port'];
    469 
    470     if ( !empty( $original['query'] ) )
     511    }
     512
     513    if ( ! empty( $original['query'] ) ) {
    471514        $compare_original[] = $original['query'];
     515    }
    472516
    473517    $compare_redirect = array( $redirect['host'], $redirect['path'] );
    474518
    475     if ( !empty( $redirect['port'] ) )
     519    if ( ! empty( $redirect['port'] ) ) {
    476520        $compare_redirect[] = $redirect['port'];
    477 
    478     if ( !empty( $redirect['query'] ) )
     521    }
     522
     523    if ( ! empty( $redirect['query'] ) ) {
    479524        $compare_redirect[] = $redirect['query'];
     525    }
    480526
    481527    if ( $compare_original !== $compare_redirect ) {
    482528        $redirect_url = $redirect['scheme'] . '://' . $redirect['host'];
    483         if ( !empty($redirect['port']) )
     529        if ( ! empty( $redirect['port'] ) ) {
    484530            $redirect_url .= ':' . $redirect['port'];
     531        }
    485532        $redirect_url .= $redirect['path'];
    486         if ( !empty($redirect['query']) )
     533        if ( ! empty( $redirect['query'] ) ) {
    487534            $redirect_url .= '?' . $redirect['query'];
     535        }
    488536    }
    489537
     
    493541
    494542    // Hex encoded octets are case-insensitive.
    495     if ( false !== strpos($requested_url, '%') ) {
    496         if ( !function_exists('lowercase_octets') ) {
     543    if ( false !== strpos( $requested_url, '%' ) ) {
     544        if ( ! function_exists( 'lowercase_octets' ) ) {
    497545            /**
    498546             * Converts the first hex-encoded octet match to lowercase.
     
    504552             * @return string Lowercased version of the first match.
    505553             */
    506             function lowercase_octets($matches) {
     554            function lowercase_octets( $matches ) {
    507555                return strtolower( $matches[0] );
    508556            }
    509557        }
    510         $requested_url = preg_replace_callback('|%[a-fA-F0-9][a-fA-F0-9]|', 'lowercase_octets', $requested_url);
     558        $requested_url = preg_replace_callback( '|%[a-fA-F0-9][a-fA-F0-9]|', 'lowercase_octets', $requested_url );
    511559    }
    512560
     
    530578    if ( $do_redirect ) {
    531579        // protect against chained redirects
    532         if ( !redirect_canonical($redirect_url, false) ) {
    533             wp_redirect($redirect_url, 301);
     580        if ( ! redirect_canonical( $redirect_url, false ) ) {
     581            wp_redirect( $redirect_url, 301 );
    534582            exit();
    535583        } else {
     
    555603 * @return string The altered query string
    556604 */
    557 function _remove_qs_args_if_not_in_url( $query_string, Array $args_to_check, $url ) {
     605function _remove_qs_args_if_not_in_url( $query_string, array $args_to_check, $url ) {
    558606    $parsed_url = @parse_url( $url );
    559607    if ( ! empty( $parsed_url['query'] ) ) {
    560608        parse_str( $parsed_url['query'], $parsed_query );
    561609        foreach ( $args_to_check as $qv ) {
    562             if ( !isset( $parsed_query[$qv] ) )
     610            if ( ! isset( $parsed_query[ $qv ] ) ) {
    563611                $query_string = remove_query_arg( $qv, $query_string );
     612            }
    564613        }
    565614    } else {
     
    610659    global $wpdb;
    611660
    612     if ( get_query_var('name') ) {
    613         $where = $wpdb->prepare("post_name LIKE %s", $wpdb->esc_like( get_query_var('name') ) . '%');
     661    if ( get_query_var( 'name' ) ) {
     662        $where = $wpdb->prepare( 'post_name LIKE %s', $wpdb->esc_like( get_query_var( 'name' ) ) . '%' );
    614663
    615664        // if any of post_type, year, monthnum, or day are set, use them to refine the query
    616         if ( get_query_var('post_type') )
    617             $where .= $wpdb->prepare(" AND post_type = %s", get_query_var('post_type'));
    618         else
     665        if ( get_query_var( 'post_type' ) ) {
     666            $where .= $wpdb->prepare( ' AND post_type = %s', get_query_var( 'post_type' ) );
     667        } else {
    619668            $where .= " AND post_type IN ('" . implode( "', '", get_post_types( array( 'public' => true ) ) ) . "')";
    620 
    621         if ( get_query_var('year') )
    622             $where .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var('year'));
    623         if ( get_query_var('monthnum') )
    624             $where .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var('monthnum'));
    625         if ( get_query_var('day') )
    626             $where .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var('day'));
    627 
    628         $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'");
    629         if ( ! $post_id )
     669        }
     670
     671        if ( get_query_var( 'year' ) ) {
     672            $where .= $wpdb->prepare( ' AND YEAR(post_date) = %d', get_query_var( 'year' ) );
     673        }
     674        if ( get_query_var( 'monthnum' ) ) {
     675            $where .= $wpdb->prepare( ' AND MONTH(post_date) = %d', get_query_var( 'monthnum' ) );
     676        }
     677        if ( get_query_var( 'day' ) ) {
     678            $where .= $wpdb->prepare( ' AND DAYOFMONTH(post_date) = %d', get_query_var( 'day' ) );
     679        }
     680
     681        $post_id = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'" );
     682        if ( ! $post_id ) {
    630683            return false;
    631         if ( get_query_var( 'feed' ) )
     684        }
     685        if ( get_query_var( 'feed' ) ) {
    632686            return get_post_comments_feed_link( $post_id, get_query_var( 'feed' ) );
    633         elseif ( get_query_var( 'page' ) && 1 < get_query_var( 'page' ) )
     687        } elseif ( get_query_var( 'page' ) && 1 < get_query_var( 'page' ) ) {
    634688            return trailingslashit( get_permalink( $post_id ) ) . user_trailingslashit( get_query_var( 'page' ), 'single_paged' );
    635         else
     689        } else {
    636690            return get_permalink( $post_id );
     691        }
    637692    }
    638693
     
    652707function wp_redirect_admin_locations() {
    653708    global $wp_rewrite;
    654     if ( ! ( is_404() && $wp_rewrite->using_permalinks() ) )
     709    if ( ! ( is_404() && $wp_rewrite->using_permalinks() ) ) {
    655710        return;
     711    }
    656712
    657713    $admins = array(
Note: See TracChangeset for help on using the changeset viewer.