[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

/wp-includes/ -> canonical.php (source)

   1  <?php
   2  /**
   3   * Canonical API to handle WordPress Redirecting
   4   *
   5   * Based on "Permalink Redirect" from Scott Yang and "Enforce www. Preference"
   6   * by Mark Jaquith
   7   *
   8   * @package WordPress
   9   * @since 2.3.0
  10   */
  11  
  12  /**
  13   * Redirects incoming links to the proper URL based on the site url.
  14   *
  15   * Search engines consider www.somedomain.com and somedomain.com to be two
  16   * different URLs when they both go to the same location. This SEO enhancement
  17   * prevents penalty for duplicate content by redirecting all incoming links to
  18   * one or the other.
  19   *
  20   * Prevents redirection for feeds, trackbacks, searches, and
  21   * admin URLs. Does not redirect on non-pretty-permalink-supporting IIS 7+,
  22   * page/post previews, WP admin, Trackbacks, robots.txt, searches, or on POST
  23   * requests.
  24   *
  25   * Will also attempt to find the correct link when a user enters a URL that does
  26   * not exist based on exact WordPress query. Will instead try to parse the URL
  27   * or query in an attempt to figure the correct page to go to.
  28   *
  29   * @since 2.3.0
  30   *
  31   * @global WP_Rewrite $wp_rewrite
  32   * @global bool $is_IIS
  33   * @global WP_Query $wp_query
  34   * @global wpdb $wpdb WordPress database abstraction object.
  35   * @global WP $wp Current WordPress environment instance.
  36   *
  37   * @param string $requested_url Optional. The URL that was requested, used to
  38   *      figure if redirect is needed.
  39   * @param bool $do_redirect Optional. Redirect to the new URL.
  40   * @return string|void The string of the URL, if redirect needed.
  41   */
  42  function redirect_canonical( $requested_url = null, $do_redirect = true ) {
  43      global $wp_rewrite, $is_IIS, $wp_query, $wpdb, $wp;
  44  
  45      if ( isset( $_SERVER['REQUEST_METHOD'] ) && ! in_array( strtoupper( $_SERVER['REQUEST_METHOD'] ), array( 'GET', 'HEAD' ) ) ) {
  46          return;
  47      }
  48  
  49      // If we're not in wp-admin and the post has been published and preview nonce
  50      // 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' ) ) ) {
  52          if ( ! isset( $_GET['preview_id'] )
  53              || ! isset( $_GET['preview_nonce'] )
  54              || ! wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . (int) $_GET['preview_id'] ) ) {
  55              $wp_query->is_preview = false;
  56          }
  57      }
  58  
  59      if ( is_trackback() || is_search() || is_admin() || is_preview() || is_robots() || ( $is_IIS && ! iis7_supports_permalinks() ) ) {
  60          return;
  61      }
  62  
  63      if ( ! $requested_url && isset( $_SERVER['HTTP_HOST'] ) ) {
  64          // build the URL in the address bar
  65          $requested_url  = is_ssl() ? 'https://' : 'http://';
  66          $requested_url .= $_SERVER['HTTP_HOST'];
  67          $requested_url .= $_SERVER['REQUEST_URI'];
  68      }
  69  
  70      $original = @parse_url( $requested_url );
  71      if ( false === $original ) {
  72          return;
  73      }
  74  
  75      $redirect     = $original;
  76      $redirect_url = false;
  77  
  78      // Notice fixing
  79      if ( ! isset( $redirect['path'] ) ) {
  80          $redirect['path'] = '';
  81      }
  82      if ( ! isset( $redirect['query'] ) ) {
  83          $redirect['query'] = '';
  84      }
  85  
  86      // If the original URL ended with non-breaking spaces, they were almost
  87      // certainly inserted by accident. Let's remove them, so the reader doesn't
  88      // see a 404 error with no obvious cause.
  89      $redirect['path'] = preg_replace( '|(%C2%A0)+$|i', '', $redirect['path'] );
  90  
  91      // It's not a preview, so remove it from URL
  92      if ( get_query_var( 'preview' ) ) {
  93          $redirect['query'] = remove_query_arg( 'preview', $redirect['query'] );
  94      }
  95  
  96      if ( is_feed() && ( $id = get_query_var( 'p' ) ) ) {
  97          if ( $redirect_url = get_post_comments_feed_link( $id, get_query_var( 'feed' ) ) ) {
  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 ) {
 109                  $id = $vars->post_parent;
 110              }
 111  
 112              if ( $redirect_url = get_permalink( $id ) ) {
 113                  $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url );
 114              }
 115          }
 116      }
 117  
 118      // These tests give us a WP-generated permalink
 119      if ( is_404() ) {
 120  
 121          // Redirect ?page_id, ?p=, ?attachment_id= to their respective url's
 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 );
 125              if ( $post_type_obj->public && 'auto-draft' != $redirect_post->post_status ) {
 126                  $redirect_url      = get_permalink( $redirect_post );
 127                  $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url );
 128              }
 129          }
 130  
 131          if ( get_query_var( 'day' ) && get_query_var( 'monthnum' ) && get_query_var( 'year' ) ) {
 132              $year  = get_query_var( 'year' );
 133              $month = get_query_var( 'monthnum' );
 134              $day   = get_query_var( 'day' );
 135              $date  = sprintf( '%04d-%02d-%02d', $year, $month, $day );
 136              if ( ! wp_checkdate( $month, $day, $year, $date ) ) {
 137                  $redirect_url      = get_month_link( $year, $month );
 138                  $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'year', 'monthnum', 'day' ), $redirect_url );
 139              }
 140          } elseif ( get_query_var( 'monthnum' ) && get_query_var( 'year' ) && 12 < get_query_var( 'monthnum' ) ) {
 141              $redirect_url      = get_year_link( get_query_var( 'year' ) );
 142              $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'year', 'monthnum' ), $redirect_url );
 143          }
 144  
 145          if ( ! $redirect_url ) {
 146              if ( $redirect_url = redirect_guess_404_permalink() ) {
 147                  $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 );
 148              }
 149          }
 150  
 151          if ( get_query_var( 'page' ) && $wp_query->post &&
 152              false !== strpos( $wp_query->post->post_content, '<!--nextpage-->' ) ) {
 153              $redirect['path']  = rtrim( $redirect['path'], (int) get_query_var( 'page' ) . '/' );
 154              $redirect['query'] = remove_query_arg( 'page', $redirect['query'] );
 155              $redirect_url      = get_permalink( $wp_query->post->ID );
 156          }
 157      } elseif ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) {
 158          // rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101
 159          if ( is_attachment() &&
 160              ! array_diff( array_keys( $wp->query_vars ), array( 'attachment', 'attachment_id' ) ) &&
 161              ! $redirect_url ) {
 162              if ( ! empty( $_GET['attachment_id'] ) ) {
 163                  $redirect_url = get_attachment_link( get_query_var( 'attachment_id' ) );
 164                  if ( $redirect_url ) {
 165                      $redirect['query'] = remove_query_arg( 'attachment_id', $redirect['query'] );
 166                  }
 167              } else {
 168                  $redirect_url = get_attachment_link();
 169              }
 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 ) ) {
 191                  case 4: // Yearly
 192                      $redirect_url = get_year_link( $m );
 193                      break;
 194                  case 6: // Monthly
 195                      $redirect_url = get_month_link( substr( $m, 0, 4 ), substr( $m, 4, 2 ) );
 196                      break;
 197                  case 8: // Daily
 198                      $redirect_url = get_day_link( substr( $m, 0, 4 ), substr( $m, 4, 2 ), substr( $m, 6, 2 ) );
 199                      break;
 200              }
 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' ) );
 219              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 ) ) ) {
 220                  if ( $redirect_url = get_author_posts_url( $author->ID, $author->user_nicename ) ) {
 221                      $redirect['query'] = remove_query_arg( 'author', $redirect['query'] );
 222                  }
 223              }
 224          } elseif ( is_category() || is_tag() || is_tax() ) { // Terms (Tags/categories)
 225  
 226              $term_count = 0;
 227              foreach ( $wp_query->tax_query->queried_terms as $tax_query ) {
 228                  $term_count += count( $tax_query['terms'] );
 229              }
 230  
 231              $obj = $wp_query->get_queried_object();
 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'] ) ) {
 234                      // Strip taxonomy query vars off the url.
 235                      $qv_remove = array( 'term', 'taxonomy' );
 236                      if ( is_category() ) {
 237                          $qv_remove[] = 'category_name';
 238                          $qv_remove[] = 'cat';
 239                      } elseif ( is_tag() ) {
 240                          $qv_remove[] = 'tag';
 241                          $qv_remove[] = 'tag_id';
 242                      } else { // Custom taxonomies will have a custom query var, remove those too:
 243                          $tax_obj = get_taxonomy( $obj->taxonomy );
 244                          if ( false !== $tax_obj->query_var ) {
 245                              $qv_remove[] = $tax_obj->query_var;
 246                          }
 247                      }
 248  
 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
 253  
 254                          // Create the destination url for this taxonomy
 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'] );
 259                          } else { // Taxonomy is accessible via a "pretty-URL"
 260                              $redirect['path'] = $tax_url['path'];
 261                          }
 262                      } else { // Some query vars are set via $_GET. Unset those from $_GET that exist via the rewrite
 263                          foreach ( $qv_remove as $_qv ) {
 264                              if ( isset( $rewrite_vars[ $_qv ] ) ) {
 265                                  $redirect['query'] = remove_query_arg( $_qv, $redirect['query'] );
 266                              }
 267                          }
 268                      }
 269                  }
 270              }
 271          } elseif ( is_single() && strpos( $wp_rewrite->permalink_structure, '%category%' ) !== false && $cat = get_query_var( 'category_name' ) ) {
 272              $category = get_category_by_path( $cat );
 273              if ( ( ! $category || is_wp_error( $category ) ) || ! has_term( $category->term_id, 'category', $wp_query->get_queried_object_id() ) ) {
 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 ) {
 281                  $redirect_url = get_permalink( get_queried_object_id() );
 282              }
 283  
 284              $page = get_query_var( 'page' );
 285              if ( $page > 1 ) {
 286                  if ( is_front_page() ) {
 287                      $redirect_url = trailingslashit( $redirect_url ) . user_trailingslashit( "$wp_rewrite->pagination_base/$page", 'paged' );
 288                  } else {
 289                      $redirect_url = trailingslashit( $redirect_url ) . user_trailingslashit( $page, 'single_paged' );
 290                  }
 291              }
 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' ) ) {
 297              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'] ) ) {
 298                  // Strip off paging and feed
 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
 302              }
 303  
 304              $addl_path = '';
 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' ) ) {
 308                      $addl_path .= 'comments/';
 309                  }
 310                  if ( ( 'rss' == get_default_feed() && 'feed' == get_query_var( 'feed' ) ) || 'rss' == get_query_var( 'feed' ) ) {
 311                      $addl_path .= user_trailingslashit( 'feed/' . ( ( get_default_feed() == 'rss2' ) ? '' : 'rss2' ), '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                  }
 315                  $redirect['query'] = remove_query_arg( 'feed', $redirect['query'] );
 316              } elseif ( is_feed() && 'old' == get_query_var( 'feed' ) ) {
 317                  $old_feed_files = array(
 318                      'wp-atom.php'         => 'atom',
 319                      'wp-commentsrss2.php' => 'comments_rss2',
 320                      'wp-feed.php'         => get_default_feed(),
 321                      'wp-rdf.php'          => 'rdf',
 322                      'wp-rss.php'          => 'rss2',
 323                      'wp-rss2.php'         => 'rss2',
 324                  );
 325                  if ( isset( $old_feed_files[ basename( $redirect['path'] ) ] ) ) {
 326                      $redirect_url = get_feed_link( $old_feed_files[ basename( $redirect['path'] ) ] );
 327                      wp_redirect( $redirect_url, 301 );
 328                      die();
 329                  }
 330              }
 331  
 332              if ( get_query_var( 'paged' ) > 0 ) {
 333                  $paged             = get_query_var( 'paged' );
 334                  $redirect['query'] = remove_query_arg( 'paged', $redirect['query'] );
 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 ) : '';
 340                      }
 341                  } elseif ( $paged > 1 ) {
 342                      $redirect['query'] = add_query_arg( 'paged', $paged, $redirect['query'] );
 343                  }
 344              }
 345  
 346              if ( get_option( 'page_comments' ) && (
 347              ( 'newest' == get_option( 'default_comments_page' ) && get_query_var( 'cpage' ) > 0 ) ||
 348              ( 'newest' != get_option( 'default_comments_page' ) && get_query_var( 'cpage' ) > 1 )
 349              ) ) {
 350                  $addl_path         = ( ! empty( $addl_path ) ? trailingslashit( $addl_path ) : '' ) . user_trailingslashit( $wp_rewrite->comments_pagination_base . '-' . get_query_var( 'cpage' ), 'commentpaged' );
 351                  $redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] );
 352              }
 353  
 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              }
 361              $redirect_url = $redirect['scheme'] . '://' . $redirect['host'] . $redirect['path'];
 362          }
 363  
 364          if ( 'wp-register.php' == basename( $redirect['path'] ) ) {
 365              if ( is_multisite() ) {
 366                  /** This filter is documented in wp-login.php */
 367                  $redirect_url = apply_filters( 'wp_signup_location', network_site_url( 'wp-signup.php' ) );
 368              } else {
 369                  $redirect_url = wp_registration_url();
 370              }
 371  
 372              wp_redirect( $redirect_url, 301 );
 373              die();
 374          }
 375      }
 376  
 377      // tack on any additional query vars
 378      $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] );
 379      if ( $redirect_url && ! empty( $redirect['query'] ) ) {
 380          parse_str( $redirect['query'], $_parsed_query );
 381          $redirect = @parse_url( $redirect_url );
 382  
 383          if ( ! empty( $_parsed_query['name'] ) && ! empty( $redirect['query'] ) ) {
 384              parse_str( $redirect['query'], $_parsed_redirect_query );
 385  
 386              if ( empty( $_parsed_redirect_query['name'] ) ) {
 387                  unset( $_parsed_query['name'] );
 388              }
 389          }
 390  
 391          $_parsed_query = array_combine(
 392              rawurlencode_deep( array_keys( $_parsed_query ) ),
 393              rawurlencode_deep( array_values( $_parsed_query ) )
 394          );
 395          $redirect_url  = add_query_arg( $_parsed_query, $redirect_url );
 396      }
 397  
 398      if ( $redirect_url ) {
 399          $redirect = @parse_url( $redirect_url );
 400      }
 401  
 402      // www.example.com vs example.com
 403      $user_home = @parse_url( home_url() );
 404      if ( ! empty( $user_home['host'] ) ) {
 405          $redirect['host'] = $user_home['host'];
 406      }
 407      if ( empty( $user_home['path'] ) ) {
 408          $user_home['path'] = '/';
 409      }
 410  
 411      // Handle ports
 412      if ( ! empty( $user_home['port'] ) ) {
 413          $redirect['port'] = $user_home['port'];
 414      } else {
 415          unset( $redirect['port'] );
 416      }
 417  
 418      // trailing /index.php
 419      $redirect['path'] = preg_replace( '|/' . preg_quote( $wp_rewrite->index, '|' ) . '/*?$|', '/', $redirect['path'] );
 420  
 421      $punctuation_pattern = implode(
 422          '|',
 423          array_map(
 424              'preg_quote',
 425              array(
 426                  ' ',
 427                  '%20',  // space
 428                  '!',
 429                  '%21',  // exclamation mark
 430                  '"',
 431                  '%22',  // double quote
 432                  "'",
 433                  '%27',  // single quote
 434                  '(',
 435                  '%28',  // opening bracket
 436                  ')',
 437                  '%29',  // closing bracket
 438                  ',',
 439                  '%2C',  // comma
 440                  '.',
 441                  '%2E',  // period
 442                  ';',
 443                  '%3B',  // semicolon
 444                  '{',
 445                  '%7B',  // opening curly bracket
 446                  '}',
 447                  '%7D',  // closing curly bracket
 448                  '%E2%80%9C', // opening curly quote
 449                  '%E2%80%9D', // closing curly quote
 450              )
 451          )
 452      );
 453  
 454      // Remove trailing spaces and end punctuation from the path.
 455      $redirect['path'] = preg_replace( "#($punctuation_pattern)+$#", '', $redirect['path'] );
 456  
 457      if ( ! empty( $redirect['query'] ) ) {
 458          // Remove trailing spaces and end punctuation from certain terminating query string args.
 459          $redirect['query'] = preg_replace( "#((p|page_id|cat|tag)=[^&]*?)($punctuation_pattern)+$#", '$1', $redirect['query'] );
 460  
 461          // Clean up empty query strings
 462          $redirect['query'] = trim( preg_replace( '#(^|&)(p|page_id|cat|tag)=?(&|$)#', '&', $redirect['query'] ), '&' );
 463  
 464          // Redirect obsolete feeds
 465          $redirect['query'] = preg_replace( '#(^|&)feed=rss(&|$)#', '$1feed=rss2$2', $redirect['query'] );
 466  
 467          // Remove redundant leading ampersands
 468          $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] );
 469      }
 470  
 471      // strip /index.php/ when we're not using PATHINFO permalinks
 472      if ( ! $wp_rewrite->using_index_permalinks() ) {
 473          $redirect['path'] = str_replace( '/' . $wp_rewrite->index . '/', '/', $redirect['path'] );
 474      }
 475  
 476      // trailing slashes
 477      if ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() && ! is_404() && ( ! is_front_page() || ( is_front_page() && ( get_query_var( 'paged' ) > 1 ) ) ) ) {
 478          $user_ts_type = '';
 479          if ( get_query_var( 'paged' ) > 0 ) {
 480              $user_ts_type = 'paged';
 481          } else {
 482              foreach ( array( 'single', 'category', 'page', 'day', 'month', 'year', 'home' ) as $type ) {
 483                  $func = 'is_' . $type;
 484                  if ( call_user_func( $func ) ) {
 485                      $user_ts_type = $type;
 486                      break;
 487                  }
 488              }
 489          }
 490          $redirect['path'] = user_trailingslashit( $redirect['path'], $user_ts_type );
 491      } elseif ( is_front_page() ) {
 492          $redirect['path'] = trailingslashit( $redirect['path'] );
 493      }
 494  
 495      // Strip multiple slashes out of the URL
 496      if ( strpos( $redirect['path'], '//' ) > -1 ) {
 497          $redirect['path'] = preg_replace( '|/+|', '/', $redirect['path'] );
 498      }
 499  
 500      // Always trailing slash the Front Page URL
 501      if ( trailingslashit( $redirect['path'] ) == trailingslashit( $user_home['path'] ) ) {
 502          $redirect['path'] = trailingslashit( $redirect['path'] );
 503      }
 504  
 505      // Ignore differences in host capitalization, as this can lead to infinite redirects
 506      // Only redirect no-www <=> yes-www
 507      if ( strtolower( $original['host'] ) == strtolower( $redirect['host'] ) ||
 508          ( strtolower( $original['host'] ) != 'www.' . strtolower( $redirect['host'] ) && 'www.' . strtolower( $original['host'] ) != strtolower( $redirect['host'] ) ) ) {
 509          $redirect['host'] = $original['host'];
 510      }
 511  
 512      $compare_original = array( $original['host'], $original['path'] );
 513  
 514      if ( ! empty( $original['port'] ) ) {
 515          $compare_original[] = $original['port'];
 516      }
 517  
 518      if ( ! empty( $original['query'] ) ) {
 519          $compare_original[] = $original['query'];
 520      }
 521  
 522      $compare_redirect = array( $redirect['host'], $redirect['path'] );
 523  
 524      if ( ! empty( $redirect['port'] ) ) {
 525          $compare_redirect[] = $redirect['port'];
 526      }
 527  
 528      if ( ! empty( $redirect['query'] ) ) {
 529          $compare_redirect[] = $redirect['query'];
 530      }
 531  
 532      if ( $compare_original !== $compare_redirect ) {
 533          $redirect_url = $redirect['scheme'] . '://' . $redirect['host'];
 534          if ( ! empty( $redirect['port'] ) ) {
 535              $redirect_url .= ':' . $redirect['port'];
 536          }
 537          $redirect_url .= $redirect['path'];
 538          if ( ! empty( $redirect['query'] ) ) {
 539              $redirect_url .= '?' . $redirect['query'];
 540          }
 541      }
 542  
 543      if ( ! $redirect_url || $redirect_url == $requested_url ) {
 544          return;
 545      }
 546  
 547      // Hex encoded octets are case-insensitive.
 548      if ( false !== strpos( $requested_url, '%' ) ) {
 549          if ( ! function_exists( 'lowercase_octets' ) ) {
 550              /**
 551               * Converts the first hex-encoded octet match to lowercase.
 552               *
 553               * @since 3.1.0
 554               * @ignore
 555               *
 556               * @param array $matches Hex-encoded octet matches for the requested URL.
 557               * @return string Lowercased version of the first match.
 558               */
 559  			function lowercase_octets( $matches ) {
 560                  return strtolower( $matches[0] );
 561              }
 562          }
 563          $requested_url = preg_replace_callback( '|%[a-fA-F0-9][a-fA-F0-9]|', 'lowercase_octets', $requested_url );
 564      }
 565  
 566      /**
 567       * Filters the canonical redirect URL.
 568       *
 569       * Returning false to this filter will cancel the redirect.
 570       *
 571       * @since 2.3.0
 572       *
 573       * @param string $redirect_url  The redirect URL.
 574       * @param string $requested_url The requested URL.
 575       */
 576      $redirect_url = apply_filters( 'redirect_canonical', $redirect_url, $requested_url );
 577  
 578      // yes, again -- in case the filter aborted the request
 579      if ( ! $redirect_url || strip_fragment_from_url( $redirect_url ) == strip_fragment_from_url( $requested_url ) ) {
 580          return;
 581      }
 582  
 583      if ( $do_redirect ) {
 584          // protect against chained redirects
 585          if ( ! redirect_canonical( $redirect_url, false ) ) {
 586              wp_redirect( $redirect_url, 301 );
 587              exit();
 588          } else {
 589              // Debug
 590              // die("1: $redirect_url<br />2: " . redirect_canonical( $redirect_url, false ) );
 591              return;
 592          }
 593      } else {
 594          return $redirect_url;
 595      }
 596  }
 597  
 598  /**
 599   * Removes arguments from a query string if they are not present in a URL
 600   * DO NOT use this in plugin code.
 601   *
 602   * @since 3.4.0
 603   * @access private
 604   *
 605   * @param string $query_string
 606   * @param array $args_to_check
 607   * @param string $url
 608   * @return string The altered query string
 609   */
 610  function _remove_qs_args_if_not_in_url( $query_string, array $args_to_check, $url ) {
 611      $parsed_url = @parse_url( $url );
 612      if ( ! empty( $parsed_url['query'] ) ) {
 613          parse_str( $parsed_url['query'], $parsed_query );
 614          foreach ( $args_to_check as $qv ) {
 615              if ( ! isset( $parsed_query[ $qv ] ) ) {
 616                  $query_string = remove_query_arg( $qv, $query_string );
 617              }
 618          }
 619      } else {
 620          $query_string = remove_query_arg( $args_to_check, $query_string );
 621      }
 622      return $query_string;
 623  }
 624  
 625  /**
 626   * Strips the #fragment from a URL, if one is present.
 627   *
 628   * @since 4.4.0
 629   *
 630   * @param string $url The URL to strip.
 631   * @return string The altered URL.
 632   */
 633  function strip_fragment_from_url( $url ) {
 634      $parsed_url = @parse_url( $url );
 635      if ( ! empty( $parsed_url['host'] ) ) {
 636          // This mirrors code in redirect_canonical(). It does not handle every case.
 637          $url = $parsed_url['scheme'] . '://' . $parsed_url['host'];
 638          if ( ! empty( $parsed_url['port'] ) ) {
 639              $url .= ':' . $parsed_url['port'];
 640          }
 641  
 642          if ( ! empty( $parsed_url['path'] ) ) {
 643              $url .= $parsed_url['path'];
 644          }
 645  
 646          if ( ! empty( $parsed_url['query'] ) ) {
 647              $url .= '?' . $parsed_url['query'];
 648          }
 649      }
 650  
 651      return $url;
 652  }
 653  
 654  /**
 655   * Attempts to guess the correct URL based on query vars
 656   *
 657   * @since 2.3.0
 658   *
 659   * @global wpdb $wpdb WordPress database abstraction object.
 660   *
 661   * @return false|string The correct URL if one is found. False on failure.
 662   */
 663  function redirect_guess_404_permalink() {
 664      global $wpdb;
 665  
 666      if ( get_query_var( 'name' ) ) {
 667          $where = $wpdb->prepare( 'post_name LIKE %s', $wpdb->esc_like( get_query_var( 'name' ) ) . '%' );
 668  
 669          // if any of post_type, year, monthnum, or day are set, use them to refine the query
 670          if ( get_query_var( 'post_type' ) ) {
 671              $where .= $wpdb->prepare( ' AND post_type = %s', get_query_var( 'post_type' ) );
 672          } else {
 673              $where .= " AND post_type IN ('" . implode( "', '", get_post_types( array( 'public' => true ) ) ) . "')";
 674          }
 675  
 676          if ( get_query_var( 'year' ) ) {
 677              $where .= $wpdb->prepare( ' AND YEAR(post_date) = %d', get_query_var( 'year' ) );
 678          }
 679          if ( get_query_var( 'monthnum' ) ) {
 680              $where .= $wpdb->prepare( ' AND MONTH(post_date) = %d', get_query_var( 'monthnum' ) );
 681          }
 682          if ( get_query_var( 'day' ) ) {
 683              $where .= $wpdb->prepare( ' AND DAYOFMONTH(post_date) = %d', get_query_var( 'day' ) );
 684          }
 685  
 686          $post_id = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'" );
 687          if ( ! $post_id ) {
 688              return false;
 689          }
 690          if ( get_query_var( 'feed' ) ) {
 691              return get_post_comments_feed_link( $post_id, get_query_var( 'feed' ) );
 692          } elseif ( get_query_var( 'page' ) && 1 < get_query_var( 'page' ) ) {
 693              return trailingslashit( get_permalink( $post_id ) ) . user_trailingslashit( get_query_var( 'page' ), 'single_paged' );
 694          } else {
 695              return get_permalink( $post_id );
 696          }
 697      }
 698  
 699      return false;
 700  }
 701  
 702  /**
 703   * Redirects a variety of shorthand URLs to the admin.
 704   *
 705   * If a user visits example.com/admin, they'll be redirected to /wp-admin.
 706   * Visiting /login redirects to /wp-login.php, and so on.
 707   *
 708   * @since 3.4.0
 709   *
 710   * @global WP_Rewrite $wp_rewrite
 711   */
 712  function wp_redirect_admin_locations() {
 713      global $wp_rewrite;
 714      if ( ! ( is_404() && $wp_rewrite->using_permalinks() ) ) {
 715          return;
 716      }
 717  
 718      $admins = array(
 719          home_url( 'wp-admin', 'relative' ),
 720          home_url( 'dashboard', 'relative' ),
 721          home_url( 'admin', 'relative' ),
 722          site_url( 'dashboard', 'relative' ),
 723          site_url( 'admin', 'relative' ),
 724      );
 725      if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $admins ) ) {
 726          wp_redirect( admin_url() );
 727          exit;
 728      }
 729  
 730      $logins = array(
 731          home_url( 'wp-login.php', 'relative' ),
 732          home_url( 'login', 'relative' ),
 733          site_url( 'login', 'relative' ),
 734      );
 735      if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $logins ) ) {
 736          wp_redirect( wp_login_url() );
 737          exit;
 738      }
 739  }


Generated: Mon Jun 17 08:20:02 2019 Cross-referenced by PHPXref 0.7