Make WordPress Core


Ignore:
Timestamp:
05/04/2020 09:03:11 AM (6 years ago)
Author:
SergeyBiryukov
Message:

Coding Standards: Fix WPCS issues in wp-includes/canonical.php.

Reformat long conditions and dense code blocks for better readability.

See #49542.

File:
1 edited

Legend:

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

    r47727 r47759  
    4949        // If we're not in wp-admin and the post has been published and preview nonce
    5050        // is non-existent or invalid then no need for preview in query.
    51         if ( is_preview() && get_query_var( 'p' ) && 'publish' == get_post_status( get_query_var( 'p' ) ) ) {
     51        if ( is_preview() && get_query_var( 'p' ) && 'publish' === get_post_status( get_query_var( 'p' ) ) ) {
    5252                if ( ! isset( $_GET['preview_id'] )
    5353                        || ! isset( $_GET['preview_nonce'] )
    54                         || ! wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . (int) $_GET['preview_id'] ) ) {
     54                        || ! wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . (int) $_GET['preview_id'] )
     55                ) {
    5556                        $wp_query->is_preview = false;
    5657                }
    5758        }
    5859
    59         if ( is_trackback() || is_search() || is_admin() || is_preview() || is_robots() || is_favicon() || ( $is_IIS && ! iis7_supports_permalinks() ) ) {
     60        if ( is_admin() || is_search() || is_preview() || is_trackback()
     61                || is_robots() || is_favicon()
     62                || ( $is_IIS && ! iis7_supports_permalinks() )
     63        ) {
    6064                return;
    6165        }
     
    96100        }
    97101
    98         $id = get_query_var( 'p' );
    99 
    100         if ( is_feed() && $id ) {
    101                 $redirect_url = get_post_comments_feed_link( $id, get_query_var( 'feed' ) );
     102        $post_id = get_query_var( 'p' );
     103
     104        if ( is_feed() && $post_id ) {
     105                $redirect_url = get_post_comments_feed_link( $post_id, get_query_var( 'feed' ) );
     106
    102107                if ( $redirect_url ) {
    103                         $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type', 'feed' ), $redirect_url );
    104                         $redirect['path']  = parse_url( $redirect_url, PHP_URL_PATH );
    105                 }
    106         }
    107 
    108         if ( is_singular() && 1 > $wp_query->post_count && $id ) {
    109 
    110                 $vars = $wpdb->get_results( $wpdb->prepare( "SELECT post_type, post_parent FROM $wpdb->posts WHERE ID = %d", $id ) );
     108                        $redirect['query'] = _remove_qs_args_if_not_in_url(
     109                                $redirect['query'],
     110                                array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type', 'feed' ),
     111                                $redirect_url
     112                        );
     113
     114                        $redirect['path'] = parse_url( $redirect_url, PHP_URL_PATH );
     115                }
     116        }
     117
     118        if ( is_singular() && $wp_query->post_count < 1 && $post_id ) {
     119
     120                $vars = $wpdb->get_results( $wpdb->prepare( "SELECT post_type, post_parent FROM $wpdb->posts WHERE ID = %d", $post_id ) );
    111121
    112122                if ( ! empty( $vars[0] ) ) {
    113123                        $vars = $vars[0];
    114                         if ( 'revision' == $vars->post_type && $vars->post_parent > 0 ) {
    115                                 $id = $vars->post_parent;
    116                         }
    117 
    118                         $redirect_url = get_permalink( $id );
     124
     125                        if ( 'revision' === $vars->post_type && $vars->post_parent > 0 ) {
     126                                $post_id = $vars->post_parent;
     127                        }
     128
     129                        $redirect_url = get_permalink( $post_id );
     130
    119131                        if ( $redirect_url ) {
    120                                 $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url );
     132                                $redirect['query'] = _remove_qs_args_if_not_in_url(
     133                                        $redirect['query'],
     134                                        array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ),
     135                                        $redirect_url
     136                                );
    121137                        }
    122138                }
     
    127143
    128144                // Redirect ?page_id, ?p=, ?attachment_id= to their respective URLs.
    129                 $id            = max( get_query_var( 'p' ), get_query_var( 'page_id' ), get_query_var( 'attachment_id' ) );
    130                 $redirect_post = $id ? get_post( $id ) : false;
     145                $post_id = max( get_query_var( 'p' ), get_query_var( 'page_id' ), get_query_var( 'attachment_id' ) );
     146
     147                $redirect_post = $post_id ? get_post( $post_id ) : false;
     148
    131149                if ( $redirect_post ) {
    132150                        $post_type_obj = get_post_type_object( $redirect_post->post_type );
    133                         if ( $post_type_obj->public && 'auto-draft' != $redirect_post->post_status ) {
    134                                 $redirect_url      = get_permalink( $redirect_post );
    135                                 $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url );
    136                         }
    137                 }
    138 
    139                 if ( get_query_var( 'day' ) && get_query_var( 'monthnum' ) && get_query_var( 'year' ) ) {
    140                         $year  = get_query_var( 'year' );
    141                         $month = get_query_var( 'monthnum' );
    142                         $day   = get_query_var( 'day' );
    143                         $date  = sprintf( '%04d-%02d-%02d', $year, $month, $day );
     151
     152                        if ( $post_type_obj->public && 'auto-draft' !== $redirect_post->post_status ) {
     153                                $redirect_url = get_permalink( $redirect_post );
     154
     155                                $redirect['query'] = _remove_qs_args_if_not_in_url(
     156                                        $redirect['query'],
     157                                        array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ),
     158                                        $redirect_url
     159                                );
     160                        }
     161                }
     162
     163                $year  = get_query_var( 'year' );
     164                $month = get_query_var( 'monthnum' );
     165                $day   = get_query_var( 'day' );
     166
     167                if ( $year && $month && $day ) {
     168                        $date = sprintf( '%04d-%02d-%02d', $year, $month, $day );
     169
    144170                        if ( ! wp_checkdate( $month, $day, $year, $date ) ) {
    145                                 $redirect_url      = get_month_link( $year, $month );
    146                                 $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'year', 'monthnum', 'day' ), $redirect_url );
    147                         }
    148                 } elseif ( get_query_var( 'monthnum' ) && get_query_var( 'year' ) && 12 < get_query_var( 'monthnum' ) ) {
    149                         $redirect_url      = get_year_link( get_query_var( 'year' ) );
    150                         $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'year', 'monthnum' ), $redirect_url );
     171                                $redirect_url = get_month_link( $year, $month );
     172
     173                                $redirect['query'] = _remove_qs_args_if_not_in_url(
     174                                        $redirect['query'],
     175                                        array( 'year', 'monthnum', 'day' ),
     176                                        $redirect_url
     177                                );
     178                        }
     179                } elseif ( $year && $month && $month > 12 ) {
     180                        $redirect_url = get_year_link( $year );
     181
     182                        $redirect['query'] = _remove_qs_args_if_not_in_url(
     183                                $redirect['query'],
     184                                array( 'year', 'monthnum' ),
     185                                $redirect_url
     186                        );
    151187                }
    152188
    153189                if ( ! $redirect_url ) {
    154190                        $redirect_url = redirect_guess_404_permalink();
     191
    155192                        if ( $redirect_url ) {
    156                                 $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 );
    157                         }
    158                 }
    159 
     193                                $redirect['query'] = _remove_qs_args_if_not_in_url(
     194                                        $redirect['query'],
     195                                        array( 'page', 'feed', 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ),
     196                                        $redirect_url
     197                                );
     198                        }
     199                }
     200
     201                // Strip off non-existing page links from single posts or pages.
    160202                if ( get_query_var( 'page' ) && $wp_query->post ) {
    161203                        $redirect['path']  = rtrim( $redirect['path'], (int) get_query_var( 'page' ) . '/' );
    162204                        $redirect['query'] = remove_query_arg( 'page', $redirect['query'] );
    163                         $redirect_url      = get_permalink( $wp_query->post->ID );
     205
     206                        $redirect_url = get_permalink( $wp_query->post->ID );
    164207                }
    165208        } elseif ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) {
     209
    166210                // Rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101.
    167                 if ( is_attachment() &&
    168                         ! array_diff( array_keys( $wp->query_vars ), array( 'attachment', 'attachment_id' ) ) &&
    169                         ! $redirect_url ) {
     211                if ( is_attachment()
     212                        && ! array_diff( array_keys( $wp->query_vars ), array( 'attachment', 'attachment_id' ) )
     213                        && ! $redirect_url
     214                ) {
    170215                        if ( ! empty( $_GET['attachment_id'] ) ) {
    171216                                $redirect_url = get_attachment_link( get_query_var( 'attachment_id' ) );
     217
    172218                                if ( $redirect_url ) {
    173219                                        $redirect['query'] = remove_query_arg( 'attachment_id', $redirect['query'] );
     
    178224                } elseif ( is_single() && ! empty( $_GET['p'] ) && ! $redirect_url ) {
    179225                        $redirect_url = get_permalink( get_query_var( 'p' ) );
     226
    180227                        if ( $redirect_url ) {
    181228                                $redirect['query'] = remove_query_arg( array( 'p', 'post_type' ), $redirect['query'] );
     
    183230                } elseif ( is_single() && ! empty( $_GET['name'] ) && ! $redirect_url ) {
    184231                        $redirect_url = get_permalink( $wp_query->get_queried_object_id() );
     232
    185233                        if ( $redirect_url ) {
    186234                                $redirect['query'] = remove_query_arg( 'name', $redirect['query'] );
     
    188236                } elseif ( is_page() && ! empty( $_GET['page_id'] ) && ! $redirect_url ) {
    189237                        $redirect_url = get_permalink( get_query_var( 'page_id' ) );
     238
    190239                        if ( $redirect_url ) {
    191240                                $redirect['query'] = remove_query_arg( 'page_id', $redirect['query'] );
    192241                        }
    193                 } elseif ( is_page() && ! is_feed() && 'page' == get_option( 'show_on_front' ) && get_queried_object_id() == get_option( 'page_on_front' ) && ! $redirect_url ) {
     242                } elseif ( is_page() && ! is_feed() && ! $redirect_url
     243                        && 'page' === get_option( 'show_on_front' ) && get_queried_object_id() === (int) get_option( 'page_on_front' )
     244                ) {
    194245                        $redirect_url = home_url( '/' );
    195                 } 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 ) {
     246                } elseif ( is_home() && ! empty( $_GET['page_id'] ) && ! $redirect_url
     247                        && 'page' === get_option( 'show_on_front' ) && get_query_var( 'page_id' ) === (int) get_option( 'page_for_posts' )
     248                ) {
    196249                        $redirect_url = get_permalink( get_option( 'page_for_posts' ) );
     250
    197251                        if ( $redirect_url ) {
    198252                                $redirect['query'] = remove_query_arg( 'page_id', $redirect['query'] );
     
    200254                } elseif ( ! empty( $_GET['m'] ) && ( is_year() || is_month() || is_day() ) ) {
    201255                        $m = get_query_var( 'm' );
     256
    202257                        switch ( strlen( $m ) ) {
    203258                                case 4: // Yearly.
     
    211266                                        break;
    212267                        }
     268
    213269                        if ( $redirect_url ) {
    214270                                $redirect['query'] = remove_query_arg( 'm', $redirect['query'] );
    215271                        }
    216272                        // Now moving on to non ?m=X year/month/day links.
    217                 } elseif ( is_day() && get_query_var( 'year' ) && get_query_var( 'monthnum' ) && ! empty( $_GET['day'] ) ) {
    218                         $redirect_url = get_day_link( get_query_var( 'year' ), get_query_var( 'monthnum' ), get_query_var( 'day' ) );
    219                         if ( $redirect_url ) {
    220                                 $redirect['query'] = remove_query_arg( array( 'year', 'monthnum', 'day' ), $redirect['query'] );
    221                         }
    222                 } elseif ( is_month() && get_query_var( 'year' ) && ! empty( $_GET['monthnum'] ) ) {
    223                         $redirect_url = get_month_link( get_query_var( 'year' ), get_query_var( 'monthnum' ) );
    224                         if ( $redirect_url ) {
    225                                 $redirect['query'] = remove_query_arg( array( 'year', 'monthnum' ), $redirect['query'] );
    226                         }
    227                 } elseif ( is_year() && ! empty( $_GET['year'] ) ) {
    228                         $redirect_url = get_year_link( get_query_var( 'year' ) );
    229                         if ( $redirect_url ) {
    230                                 $redirect['query'] = remove_query_arg( 'year', $redirect['query'] );
     273                } elseif ( is_date() ) {
     274                        $year  = get_query_var( 'year' );
     275                        $month = get_query_var( 'monthnum' );
     276                        $day   = get_query_var( 'day' );
     277
     278                        if ( is_day() && $year && $month && ! empty( $_GET['day'] ) ) {
     279                                $redirect_url = get_day_link( $year, $month, $day );
     280
     281                                if ( $redirect_url ) {
     282                                        $redirect['query'] = remove_query_arg( array( 'year', 'monthnum', 'day' ), $redirect['query'] );
     283                                }
     284                        } elseif ( is_month() && $year && ! empty( $_GET['monthnum'] ) ) {
     285                                $redirect_url = get_month_link( $year, $month );
     286
     287                                if ( $redirect_url ) {
     288                                        $redirect['query'] = remove_query_arg( array( 'year', 'monthnum' ), $redirect['query'] );
     289                                }
     290                        } elseif ( is_year() && ! empty( $_GET['year'] ) ) {
     291                                $redirect_url = get_year_link( $year );
     292
     293                                if ( $redirect_url ) {
     294                                        $redirect['query'] = remove_query_arg( 'year', $redirect['query'] );
     295                                }
    231296                        }
    232297                } elseif ( is_author() && ! empty( $_GET['author'] ) && preg_match( '|^[0-9]+$|', $_GET['author'] ) ) {
    233298                        $author = get_userdata( get_query_var( 'author' ) );
    234                         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 ) ) ) {
     299
     300                        if ( false !== $author
     301                                && $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 ) )
     302                        ) {
    235303                                $redirect_url = get_author_posts_url( $author->ID, $author->user_nicename );
     304
    236305                                if ( $redirect_url ) {
    237306                                        $redirect['query'] = remove_query_arg( 'author', $redirect['query'] );
     
    239308                        }
    240309                } elseif ( is_category() || is_tag() || is_tax() ) { // Terms (tags/categories).
    241 
    242310                        $term_count = 0;
     311
    243312                        foreach ( $wp_query->tax_query->queried_terms as $tax_query ) {
    244313                                $term_count += count( $tax_query['terms'] );
     
    246315
    247316                        $obj = $wp_query->get_queried_object();
     317
    248318                        if ( $term_count <= 1 && ! empty( $obj->term_id ) ) {
    249319                                $tax_url = get_term_link( (int) $obj->term_id, $obj->taxonomy );
     320
    250321                                if ( $tax_url && ! is_wp_error( $tax_url ) ) {
    251322                                        if ( ! empty( $redirect['query'] ) ) {
    252323                                                // Strip taxonomy query vars off the URL.
    253324                                                $qv_remove = array( 'term', 'taxonomy' );
     325
    254326                                                if ( is_category() ) {
    255327                                                        $qv_remove[] = 'category_name';
     
    275347                                                        // Create the destination URL for this taxonomy.
    276348                                                        $tax_url = parse_url( $tax_url );
     349
    277350                                                        if ( ! empty( $tax_url['query'] ) ) {
    278351                                                                // Taxonomy accessible via ?taxonomy=...&term=... or any custom query var.
     
    295368                        }
    296369                } elseif ( is_single() && strpos( $wp_rewrite->permalink_structure, '%category%' ) !== false ) {
    297                         $cat = get_query_var( 'category_name' );
    298                         if ( $cat ) {
    299                                 $category = get_category_by_path( $cat );
    300                                 if ( ( ! $category || is_wp_error( $category ) ) || ! has_term( $category->term_id, 'category', $wp_query->get_queried_object_id() ) ) {
     370                        $category_name = get_query_var( 'category_name' );
     371
     372                        if ( $category_name ) {
     373                                $category = get_category_by_path( $category_name );
     374
     375                                if ( ! $category || is_wp_error( $category )
     376                                        || ! has_term( $category->term_id, 'category', $wp_query->get_queried_object_id() )
     377                                ) {
    301378                                        $redirect_url = get_permalink( $wp_query->get_queried_object_id() );
    302379                                }
     
    304381                }
    305382
    306                         // Post paging.
     383                // Post paging.
    307384                if ( is_singular() && get_query_var( 'page' ) ) {
     385                        $page = get_query_var( 'page' );
     386
    308387                        if ( ! $redirect_url ) {
    309388                                $redirect_url = get_permalink( get_queried_object_id() );
    310389                        }
    311390
    312                         $page = get_query_var( 'page' );
    313391                        if ( $page > 1 ) {
     392                                $redirect_url = trailingslashit( $redirect_url );
     393
    314394                                if ( is_front_page() ) {
    315                                         $redirect_url = trailingslashit( $redirect_url ) . user_trailingslashit( "$wp_rewrite->pagination_base/$page", 'paged' );
     395                                        $redirect_url .= user_trailingslashit( "$wp_rewrite->pagination_base/$page", 'paged' );
    316396                                } else {
    317                                         $redirect_url = trailingslashit( $redirect_url ) . user_trailingslashit( $page, 'single_paged' );
    318                                 }
    319                         }
    320                                 $redirect['query'] = remove_query_arg( 'page', $redirect['query'] );
    321                 }
    322 
    323                         // Paging and feeds.
     397                                        $redirect_url .= user_trailingslashit( $page, 'single_paged' );
     398                                }
     399                        }
     400
     401                        $redirect['query'] = remove_query_arg( 'page', $redirect['query'] );
     402                }
     403
     404                // Paging and feeds.
    324405                if ( get_query_var( 'paged' ) || is_feed() || get_query_var( 'cpage' ) ) {
    325                         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'] ) ) {
    326                                 // Strip off paging and feed.
    327                                 $redirect['path'] = preg_replace( "#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", '/', $redirect['path'] ); // Strip off any existing paging.
    328                                 $redirect['path'] = preg_replace( '#/(comments/?)?(feed|rss2?|rdf|atom)(/+|$)#', '/', $redirect['path'] ); // Strip off feed endings.
    329                                 $redirect['path'] = preg_replace( "#/{$wp_rewrite->comments_pagination_base}-[0-9]+?(/+)?$#", '/', $redirect['path'] ); // Strip off any existing comment paging.
    330                         }
    331 
    332                         $addl_path = '';
    333                         if ( is_feed() && in_array( get_query_var( 'feed' ), $wp_rewrite->feeds, true ) ) {
     406                        $paged = get_query_var( 'paged' );
     407                        $feed  = get_query_var( 'feed' );
     408                        $cpage = get_query_var( 'cpage' );
     409
     410                        while ( preg_match( "#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", $redirect['path'] )
     411                                || preg_match( '#/(comments/?)?(feed|rss2?|rdf|atom)(/+)?$#', $redirect['path'] )
     412                                || preg_match( "#/{$wp_rewrite->comments_pagination_base}-[0-9]+(/+)?$#", $redirect['path'] )
     413                        ) {
     414                                // Strip off any existing paging.
     415                                $redirect['path'] = preg_replace( "#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", '/', $redirect['path'] );
     416                                // Strip off feed endings.
     417                                $redirect['path'] = preg_replace( '#/(comments/?)?(feed|rss2?|rdf|atom)(/+|$)#', '/', $redirect['path'] );
     418                                // Strip off any existing comment paging.
     419                                $redirect['path'] = preg_replace( "#/{$wp_rewrite->comments_pagination_base}-[0-9]+?(/+)?$#", '/', $redirect['path'] );
     420                        }
     421
     422                        $addl_path    = '';
     423                        $default_feed = get_default_feed();
     424
     425                        if ( is_feed() && in_array( $feed, $wp_rewrite->feeds, true ) ) {
    334426                                $addl_path = ! empty( $addl_path ) ? trailingslashit( $addl_path ) : '';
     427
    335428                                if ( ! is_singular() && get_query_var( 'withcomments' ) ) {
    336429                                        $addl_path .= 'comments/';
    337430                                }
    338                                 if ( ( 'rss' == get_default_feed() && 'feed' == get_query_var( 'feed' ) ) || 'rss' == get_query_var( 'feed' ) ) {
    339                                         $addl_path .= user_trailingslashit( 'feed/' . ( ( get_default_feed() == 'rss2' ) ? '' : 'rss2' ), 'feed' );
     431
     432                                if ( ( 'rss' === $default_feed && 'feed' === $feed ) || 'rss' === $feed ) {
     433                                        $format = ( 'rss2' === $default_feed ) ? '' : 'rss2';
    340434                                } else {
    341                                         $addl_path .= user_trailingslashit( 'feed/' . ( ( get_default_feed() == get_query_var( 'feed' ) || 'feed' == get_query_var( 'feed' ) ) ? '' : get_query_var( 'feed' ) ), 'feed' );
    342                                 }
     435                                        $format = ( $default_feed === $feed || 'feed' === $feed ) ? '' : $feed;
     436                                }
     437
     438                                $addl_path .= user_trailingslashit( 'feed/' . $format, 'feed' );
     439
    343440                                $redirect['query'] = remove_query_arg( 'feed', $redirect['query'] );
    344                         } elseif ( is_feed() && 'old' == get_query_var( 'feed' ) ) {
     441                        } elseif ( is_feed() && 'old' === $feed ) {
    345442                                $old_feed_files = array(
    346443                                        'wp-atom.php'         => 'atom',
    347444                                        'wp-commentsrss2.php' => 'comments_rss2',
    348                                         'wp-feed.php'         => get_default_feed(),
     445                                        'wp-feed.php'         => $default_feed,
    349446                                        'wp-rdf.php'          => 'rdf',
    350447                                        'wp-rss.php'          => 'rss2',
    351448                                        'wp-rss2.php'         => 'rss2',
    352449                                );
     450
    353451                                if ( isset( $old_feed_files[ basename( $redirect['path'] ) ] ) ) {
    354452                                        $redirect_url = get_feed_link( $old_feed_files[ basename( $redirect['path'] ) ] );
     453
    355454                                        wp_redirect( $redirect_url, 301 );
    356455                                        die();
     
    358457                        }
    359458
    360                         if ( get_query_var( 'paged' ) > 0 ) {
    361                                 $paged             = get_query_var( 'paged' );
     459                        if ( $paged > 0 ) {
    362460                                $redirect['query'] = remove_query_arg( 'paged', $redirect['query'] );
     461
    363462                                if ( ! is_feed() ) {
    364                                         if ( $paged > 1 && ! is_single() ) {
    365                                                 $addl_path = ( ! empty( $addl_path ) ? trailingslashit( $addl_path ) : '' ) . user_trailingslashit( "$wp_rewrite->pagination_base/$paged", 'paged' );
    366                                         } elseif ( ! is_single() ) {
     463                                        if ( ! is_single() ) {
    367464                                                $addl_path = ! empty( $addl_path ) ? trailingslashit( $addl_path ) : '';
     465
     466                                                if ( $paged > 1 ) {
     467                                                        $addl_path .= user_trailingslashit( "$wp_rewrite->pagination_base/$paged", 'paged' );
     468                                                }
    368469                                        }
    369470                                } elseif ( $paged > 1 ) {
     
    372473                        }
    373474
    374                         if ( get_option( 'page_comments' ) && (
    375                         ( 'newest' == get_option( 'default_comments_page' ) && get_query_var( 'cpage' ) > 0 ) ||
    376                         ( 'newest' != get_option( 'default_comments_page' ) && get_query_var( 'cpage' ) > 1 )
    377                         ) ) {
    378                                 $addl_path         = ( ! empty( $addl_path ) ? trailingslashit( $addl_path ) : '' ) . user_trailingslashit( $wp_rewrite->comments_pagination_base . '-' . get_query_var( 'cpage' ), 'commentpaged' );
     475                        $default_comments_page = get_option( 'default_comments_page' );
     476
     477                        if ( get_option( 'page_comments' )
     478                                && ( 'newest' === $default_comments_page && $cpage > 0
     479                                        || 'newest' !== $default_comments_page && $cpage > 1 )
     480                        ) {
     481                                $addl_path  = ( ! empty( $addl_path ) ? trailingslashit( $addl_path ) : '' );
     482                                $addl_path .= user_trailingslashit( $wp_rewrite->comments_pagination_base . '-' . $cpage, 'commentpaged' );
     483
    379484                                $redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] );
    380485                        }
    381486
    382                         $redirect['path'] = user_trailingslashit( preg_replace( '|/' . preg_quote( $wp_rewrite->index, '|' ) . '/?$|', '/', $redirect['path'] ) ); // Strip off trailing /index.php/.
    383                         if ( ! empty( $addl_path ) && $wp_rewrite->using_index_permalinks() && strpos( $redirect['path'], '/' . $wp_rewrite->index . '/' ) === false ) {
     487                        // Strip off trailing /index.php/.
     488                        $redirect['path'] = preg_replace( '|/' . preg_quote( $wp_rewrite->index, '|' ) . '/?$|', '/', $redirect['path'] );
     489                        $redirect['path'] = user_trailingslashit( $redirect['path'] );
     490
     491                        if ( ! empty( $addl_path )
     492                                && $wp_rewrite->using_index_permalinks()
     493                                && strpos( $redirect['path'], '/' . $wp_rewrite->index . '/' ) === false
     494                        ) {
    384495                                $redirect['path'] = trailingslashit( $redirect['path'] ) . $wp_rewrite->index . '/';
    385496                        }
     497
    386498                        if ( ! empty( $addl_path ) ) {
    387499                                $redirect['path'] = trailingslashit( $redirect['path'] ) . $addl_path;
    388500                        }
     501
    389502                        $redirect_url = $redirect['scheme'] . '://' . $redirect['host'] . $redirect['path'];
    390503                }
    391504
    392                 if ( 'wp-register.php' == basename( $redirect['path'] ) ) {
     505                if ( 'wp-register.php' === basename( $redirect['path'] ) ) {
    393506                        if ( is_multisite() ) {
    394507                                /** This filter is documented in wp-login.php */
     
    403516        }
    404517
     518        $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] );
     519
    405520        // Tack on any additional query vars.
    406         $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] );
    407521        if ( $redirect_url && ! empty( $redirect['query'] ) ) {
    408522                parse_str( $redirect['query'], $_parsed_query );
     
    421535                        rawurlencode_deep( array_values( $_parsed_query ) )
    422536                );
    423                 $redirect_url  = add_query_arg( $_parsed_query, $redirect_url );
     537
     538                $redirect_url = add_query_arg( $_parsed_query, $redirect_url );
    424539        }
    425540
     
    430545        // www.example.com vs. example.com
    431546        $user_home = parse_url( home_url() );
     547
    432548        if ( ! empty( $user_home['host'] ) ) {
    433549                $redirect['host'] = $user_home['host'];
    434550        }
     551
    435552        if ( empty( $user_home['path'] ) ) {
    436553                $user_home['path'] = '/';
     
    503620
    504621        // Trailing slashes.
    505         if ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() && ! is_404() && ( ! is_front_page() || ( is_front_page() && ( get_query_var( 'paged' ) > 1 ) ) ) ) {
     622        if ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks()
     623                && ! is_404() && ( ! is_front_page() || is_front_page() && get_query_var( 'paged' ) > 1 )
     624        ) {
    506625                $user_ts_type = '';
     626
    507627                if ( get_query_var( 'paged' ) > 0 ) {
    508628                        $user_ts_type = 'paged';
     
    516636                        }
    517637                }
     638
    518639                $redirect['path'] = user_trailingslashit( $redirect['path'], $user_ts_type );
    519640        } elseif ( is_front_page() ) {
     
    527648
    528649        // Always trailing slash the Front Page URL.
    529         if ( trailingslashit( $redirect['path'] ) == trailingslashit( $user_home['path'] ) ) {
     650        if ( trailingslashit( $redirect['path'] ) === trailingslashit( $user_home['path'] ) ) {
    530651                $redirect['path'] = trailingslashit( $redirect['path'] );
    531652        }
     653
     654        $original_host_low = strtolower( $original['host'] );
     655        $redirect_host_low = strtolower( $redirect['host'] );
    532656
    533657        // Ignore differences in host capitalization, as this can lead to infinite redirects.
    534658        // Only redirect no-www <=> yes-www.
    535         if ( strtolower( $original['host'] ) == strtolower( $redirect['host'] ) ||
    536                 ( strtolower( $original['host'] ) != 'www.' . strtolower( $redirect['host'] ) && 'www.' . strtolower( $original['host'] ) != strtolower( $redirect['host'] ) ) ) {
     659        if ( $original_host_low === $redirect_host_low
     660                || ( 'www.' . $original_host_low !== $redirect_host_low
     661                        && 'www.' . $redirect_host_low !== $original_host_low )
     662        ) {
    537663                $redirect['host'] = $original['host'];
    538664        }
     
    560686        if ( $compare_original !== $compare_redirect ) {
    561687                $redirect_url = $redirect['scheme'] . '://' . $redirect['host'];
     688
    562689                if ( ! empty( $redirect['port'] ) ) {
    563690                        $redirect_url .= ':' . $redirect['port'];
    564691                }
     692
    565693                $redirect_url .= $redirect['path'];
     694
    566695                if ( ! empty( $redirect['query'] ) ) {
    567696                        $redirect_url .= '?' . $redirect['query'];
     
    569698        }
    570699
    571         if ( ! $redirect_url || $redirect_url == $requested_url ) {
     700        if ( ! $redirect_url || $redirect_url === $requested_url ) {
    572701                return;
    573702        }
     
    589718                        }
    590719                }
     720
    591721                $requested_url = preg_replace_callback( '|%[a-fA-F0-9][a-fA-F0-9]|', 'lowercase_octets', $requested_url );
    592722        }
     
    605735
    606736        // Yes, again -- in case the filter aborted the request.
    607         if ( ! $redirect_url || strip_fragment_from_url( $redirect_url ) == strip_fragment_from_url( $requested_url ) ) {
     737        if ( ! $redirect_url || strip_fragment_from_url( $redirect_url ) === strip_fragment_from_url( $requested_url ) ) {
    608738                return;
    609739        }
     
    638768function _remove_qs_args_if_not_in_url( $query_string, array $args_to_check, $url ) {
    639769        $parsed_url = parse_url( $url );
     770
    640771        if ( ! empty( $parsed_url['query'] ) ) {
    641772                parse_str( $parsed_url['query'], $parsed_query );
     773
    642774                foreach ( $args_to_check as $qv ) {
    643775                        if ( ! isset( $parsed_query[ $qv ] ) ) {
     
    648780                $query_string = remove_query_arg( $args_to_check, $query_string );
    649781        }
     782
    650783        return $query_string;
    651784}
     
    661794function strip_fragment_from_url( $url ) {
    662795        $parsed_url = parse_url( $url );
     796
    663797        if ( ! empty( $parsed_url['host'] ) ) {
    664798                // This mirrors code in redirect_canonical(). It does not handle every case.
     
    712846                }
    713847
     848                // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
    714849                $post_id = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'" );
     850
    715851                if ( ! $post_id ) {
    716852                        return false;
    717853                }
     854
    718855                if ( get_query_var( 'feed' ) ) {
    719856                        return get_post_comments_feed_link( $post_id, get_query_var( 'feed' ) );
    720                 } elseif ( get_query_var( 'page' ) && 1 < get_query_var( 'page' ) ) {
     857                } elseif ( get_query_var( 'page' ) > 1 ) {
    721858                        return trailingslashit( get_permalink( $post_id ) ) . user_trailingslashit( get_query_var( 'page' ), 'single_paged' );
    722859                } else {
     
    740877function wp_redirect_admin_locations() {
    741878        global $wp_rewrite;
     879
    742880        if ( ! ( is_404() && $wp_rewrite->using_permalinks() ) ) {
    743881                return;
     
    751889                site_url( 'admin', 'relative' ),
    752890        );
     891
    753892        if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $admins, true ) ) {
    754893                wp_redirect( admin_url() );
     
    761900                site_url( 'login', 'relative' ),
    762901        );
     902
    763903        if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $logins, true ) ) {
    764904                wp_redirect( wp_login_url() );
Note: See TracChangeset for help on using the changeset viewer.