[ Index ] |
PHP Cross Reference of WordPress |
[Summary view] [Print] [Text view]
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, favicon.ico, searches, 23 * or on POST 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 WordPress rewrite component. 32 * @global bool $is_IIS 33 * @global WP_Query $wp_query WordPress Query object. 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' ), true ) ) { 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 ) { 56 $wp_query->is_preview = false; 57 } 58 } 59 60 if ( is_admin() || is_search() || is_preview() || is_trackback() || is_favicon() 61 || ( $is_IIS && ! iis7_supports_permalinks() ) 62 ) { 63 return; 64 } 65 66 if ( ! $requested_url && isset( $_SERVER['HTTP_HOST'] ) ) { 67 // Build the URL in the address bar. 68 $requested_url = is_ssl() ? 'https://' : 'http://'; 69 $requested_url .= $_SERVER['HTTP_HOST']; 70 $requested_url .= $_SERVER['REQUEST_URI']; 71 } 72 73 $original = parse_url( $requested_url ); 74 if ( false === $original ) { 75 return; 76 } 77 78 $redirect = $original; 79 $redirect_url = false; 80 $redirect_obj = false; 81 82 // Notice fixing. 83 if ( ! isset( $redirect['path'] ) ) { 84 $redirect['path'] = ''; 85 } 86 if ( ! isset( $redirect['query'] ) ) { 87 $redirect['query'] = ''; 88 } 89 90 /* 91 * If the original URL ended with non-breaking spaces, they were almost 92 * certainly inserted by accident. Let's remove them, so the reader doesn't 93 * see a 404 error with no obvious cause. 94 */ 95 $redirect['path'] = preg_replace( '|(%C2%A0)+$|i', '', $redirect['path'] ); 96 97 // It's not a preview, so remove it from URL. 98 if ( get_query_var( 'preview' ) ) { 99 $redirect['query'] = remove_query_arg( 'preview', $redirect['query'] ); 100 } 101 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 $redirect_obj = get_post( $post_id ); 107 108 if ( $redirect_url ) { 109 $redirect['query'] = _remove_qs_args_if_not_in_url( 110 $redirect['query'], 111 array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type', 'feed' ), 112 $redirect_url 113 ); 114 115 $redirect['path'] = parse_url( $redirect_url, PHP_URL_PATH ); 116 } 117 } 118 119 if ( is_singular() && $wp_query->post_count < 1 && $post_id ) { 120 121 $vars = $wpdb->get_results( $wpdb->prepare( "SELECT post_type, post_parent FROM $wpdb->posts WHERE ID = %d", $post_id ) ); 122 123 if ( ! empty( $vars[0] ) ) { 124 $vars = $vars[0]; 125 126 if ( 'revision' === $vars->post_type && $vars->post_parent > 0 ) { 127 $post_id = $vars->post_parent; 128 } 129 130 $redirect_url = get_permalink( $post_id ); 131 $redirect_obj = get_post( $post_id ); 132 133 if ( $redirect_url ) { 134 $redirect['query'] = _remove_qs_args_if_not_in_url( 135 $redirect['query'], 136 array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), 137 $redirect_url 138 ); 139 } 140 } 141 } 142 143 // These tests give us a WP-generated permalink. 144 if ( is_404() ) { 145 146 // Redirect ?page_id, ?p=, ?attachment_id= to their respective URLs. 147 $post_id = max( get_query_var( 'p' ), get_query_var( 'page_id' ), get_query_var( 'attachment_id' ) ); 148 149 $redirect_post = $post_id ? get_post( $post_id ) : false; 150 151 if ( $redirect_post ) { 152 $post_type_obj = get_post_type_object( $redirect_post->post_type ); 153 154 if ( $post_type_obj && $post_type_obj->public && 'auto-draft' !== $redirect_post->post_status ) { 155 $redirect_url = get_permalink( $redirect_post ); 156 $redirect_obj = get_post( $redirect_post ); 157 158 $redirect['query'] = _remove_qs_args_if_not_in_url( 159 $redirect['query'], 160 array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), 161 $redirect_url 162 ); 163 } 164 } 165 166 $year = get_query_var( 'year' ); 167 $month = get_query_var( 'monthnum' ); 168 $day = get_query_var( 'day' ); 169 170 if ( $year && $month && $day ) { 171 $date = sprintf( '%04d-%02d-%02d', $year, $month, $day ); 172 173 if ( ! wp_checkdate( $month, $day, $year, $date ) ) { 174 $redirect_url = get_month_link( $year, $month ); 175 176 $redirect['query'] = _remove_qs_args_if_not_in_url( 177 $redirect['query'], 178 array( 'year', 'monthnum', 'day' ), 179 $redirect_url 180 ); 181 } 182 } elseif ( $year && $month && $month > 12 ) { 183 $redirect_url = get_year_link( $year ); 184 185 $redirect['query'] = _remove_qs_args_if_not_in_url( 186 $redirect['query'], 187 array( 'year', 'monthnum' ), 188 $redirect_url 189 ); 190 } 191 192 // Strip off non-existing <!--nextpage--> links from single posts or pages. 193 if ( get_query_var( 'page' ) ) { 194 $post_id = 0; 195 196 if ( $wp_query->queried_object instanceof WP_Post ) { 197 $post_id = $wp_query->queried_object->ID; 198 } elseif ( $wp_query->post ) { 199 $post_id = $wp_query->post->ID; 200 } 201 202 if ( $post_id ) { 203 $redirect_url = get_permalink( $post_id ); 204 $redirect_obj = get_post( $post_id ); 205 206 $redirect['path'] = rtrim( $redirect['path'], (int) get_query_var( 'page' ) . '/' ); 207 $redirect['query'] = remove_query_arg( 'page', $redirect['query'] ); 208 } 209 } 210 211 if ( ! $redirect_url ) { 212 $redirect_url = redirect_guess_404_permalink(); 213 214 if ( $redirect_url ) { 215 $redirect['query'] = _remove_qs_args_if_not_in_url( 216 $redirect['query'], 217 array( 'page', 'feed', 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), 218 $redirect_url 219 ); 220 } 221 } 222 } elseif ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) { 223 224 // Rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101. 225 if ( is_attachment() 226 && ! array_diff( array_keys( $wp->query_vars ), array( 'attachment', 'attachment_id' ) ) 227 && ! $redirect_url 228 ) { 229 if ( ! empty( $_GET['attachment_id'] ) ) { 230 $redirect_url = get_attachment_link( get_query_var( 'attachment_id' ) ); 231 $redirect_obj = get_post( get_query_var( 'attachment_id' ) ); 232 233 if ( $redirect_url ) { 234 $redirect['query'] = remove_query_arg( 'attachment_id', $redirect['query'] ); 235 } 236 } else { 237 $redirect_url = get_attachment_link(); 238 $redirect_obj = get_post(); 239 } 240 } elseif ( is_single() && ! empty( $_GET['p'] ) && ! $redirect_url ) { 241 $redirect_url = get_permalink( get_query_var( 'p' ) ); 242 $redirect_obj = get_post( get_query_var( 'p' ) ); 243 244 if ( $redirect_url ) { 245 $redirect['query'] = remove_query_arg( array( 'p', 'post_type' ), $redirect['query'] ); 246 } 247 } elseif ( is_single() && ! empty( $_GET['name'] ) && ! $redirect_url ) { 248 $redirect_url = get_permalink( $wp_query->get_queried_object_id() ); 249 $redirect_obj = get_post( $wp_query->get_queried_object_id() ); 250 251 if ( $redirect_url ) { 252 $redirect['query'] = remove_query_arg( 'name', $redirect['query'] ); 253 } 254 } elseif ( is_page() && ! empty( $_GET['page_id'] ) && ! $redirect_url ) { 255 $redirect_url = get_permalink( get_query_var( 'page_id' ) ); 256 $redirect_obj = get_post( get_query_var( 'page_id' ) ); 257 258 if ( $redirect_url ) { 259 $redirect['query'] = remove_query_arg( 'page_id', $redirect['query'] ); 260 } 261 } elseif ( is_page() && ! is_feed() && ! $redirect_url 262 && 'page' === get_option( 'show_on_front' ) && get_queried_object_id() === (int) get_option( 'page_on_front' ) 263 ) { 264 $redirect_url = home_url( '/' ); 265 } elseif ( is_home() && ! empty( $_GET['page_id'] ) && ! $redirect_url 266 && 'page' === get_option( 'show_on_front' ) && get_query_var( 'page_id' ) === (int) get_option( 'page_for_posts' ) 267 ) { 268 $redirect_url = get_permalink( get_option( 'page_for_posts' ) ); 269 $redirect_obj = get_post( get_option( 'page_for_posts' ) ); 270 271 if ( $redirect_url ) { 272 $redirect['query'] = remove_query_arg( 'page_id', $redirect['query'] ); 273 } 274 } elseif ( ! empty( $_GET['m'] ) && ( is_year() || is_month() || is_day() ) ) { 275 $m = get_query_var( 'm' ); 276 277 switch ( strlen( $m ) ) { 278 case 4: // Yearly. 279 $redirect_url = get_year_link( $m ); 280 break; 281 case 6: // Monthly. 282 $redirect_url = get_month_link( substr( $m, 0, 4 ), substr( $m, 4, 2 ) ); 283 break; 284 case 8: // Daily. 285 $redirect_url = get_day_link( substr( $m, 0, 4 ), substr( $m, 4, 2 ), substr( $m, 6, 2 ) ); 286 break; 287 } 288 289 if ( $redirect_url ) { 290 $redirect['query'] = remove_query_arg( 'm', $redirect['query'] ); 291 } 292 // Now moving on to non ?m=X year/month/day links. 293 } elseif ( is_date() ) { 294 $year = get_query_var( 'year' ); 295 $month = get_query_var( 'monthnum' ); 296 $day = get_query_var( 'day' ); 297 298 if ( is_day() && $year && $month && ! empty( $_GET['day'] ) ) { 299 $redirect_url = get_day_link( $year, $month, $day ); 300 301 if ( $redirect_url ) { 302 $redirect['query'] = remove_query_arg( array( 'year', 'monthnum', 'day' ), $redirect['query'] ); 303 } 304 } elseif ( is_month() && $year && ! empty( $_GET['monthnum'] ) ) { 305 $redirect_url = get_month_link( $year, $month ); 306 307 if ( $redirect_url ) { 308 $redirect['query'] = remove_query_arg( array( 'year', 'monthnum' ), $redirect['query'] ); 309 } 310 } elseif ( is_year() && ! empty( $_GET['year'] ) ) { 311 $redirect_url = get_year_link( $year ); 312 313 if ( $redirect_url ) { 314 $redirect['query'] = remove_query_arg( 'year', $redirect['query'] ); 315 } 316 } 317 } elseif ( is_author() && ! empty( $_GET['author'] ) && preg_match( '|^[0-9]+$|', $_GET['author'] ) ) { 318 $author = get_userdata( get_query_var( 'author' ) ); 319 320 if ( false !== $author 321 && $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 ) ) 322 ) { 323 $redirect_url = get_author_posts_url( $author->ID, $author->user_nicename ); 324 $redirect_obj = $author; 325 326 if ( $redirect_url ) { 327 $redirect['query'] = remove_query_arg( 'author', $redirect['query'] ); 328 } 329 } 330 } elseif ( is_category() || is_tag() || is_tax() ) { // Terms (tags/categories). 331 $term_count = 0; 332 333 foreach ( $wp_query->tax_query->queried_terms as $tax_query ) { 334 $term_count += count( $tax_query['terms'] ); 335 } 336 337 $obj = $wp_query->get_queried_object(); 338 339 if ( $term_count <= 1 && ! empty( $obj->term_id ) ) { 340 $tax_url = get_term_link( (int) $obj->term_id, $obj->taxonomy ); 341 342 if ( $tax_url && ! is_wp_error( $tax_url ) ) { 343 if ( ! empty( $redirect['query'] ) ) { 344 // Strip taxonomy query vars off the URL. 345 $qv_remove = array( 'term', 'taxonomy' ); 346 347 if ( is_category() ) { 348 $qv_remove[] = 'category_name'; 349 $qv_remove[] = 'cat'; 350 } elseif ( is_tag() ) { 351 $qv_remove[] = 'tag'; 352 $qv_remove[] = 'tag_id'; 353 } else { 354 // Custom taxonomies will have a custom query var, remove those too. 355 $tax_obj = get_taxonomy( $obj->taxonomy ); 356 if ( false !== $tax_obj->query_var ) { 357 $qv_remove[] = $tax_obj->query_var; 358 } 359 } 360 361 $rewrite_vars = array_diff( array_keys( $wp_query->query ), array_keys( $_GET ) ); 362 363 // Check to see if all the query vars are coming from the rewrite, none are set via $_GET. 364 if ( ! array_diff( $rewrite_vars, array_keys( $_GET ) ) ) { 365 // Remove all of the per-tax query vars. 366 $redirect['query'] = remove_query_arg( $qv_remove, $redirect['query'] ); 367 368 // Create the destination URL for this taxonomy. 369 $tax_url = parse_url( $tax_url ); 370 371 if ( ! empty( $tax_url['query'] ) ) { 372 // Taxonomy accessible via ?taxonomy=...&term=... or any custom query var. 373 parse_str( $tax_url['query'], $query_vars ); 374 $redirect['query'] = add_query_arg( $query_vars, $redirect['query'] ); 375 } else { 376 // Taxonomy is accessible via a "pretty URL". 377 $redirect['path'] = $tax_url['path']; 378 } 379 } else { 380 // Some query vars are set via $_GET. Unset those from $_GET that exist via the rewrite. 381 foreach ( $qv_remove as $_qv ) { 382 if ( isset( $rewrite_vars[ $_qv ] ) ) { 383 $redirect['query'] = remove_query_arg( $_qv, $redirect['query'] ); 384 } 385 } 386 } 387 } 388 } 389 } 390 } elseif ( is_single() && strpos( $wp_rewrite->permalink_structure, '%category%' ) !== false ) { 391 $category_name = get_query_var( 'category_name' ); 392 393 if ( $category_name ) { 394 $category = get_category_by_path( $category_name ); 395 396 if ( ! $category || is_wp_error( $category ) 397 || ! has_term( $category->term_id, 'category', $wp_query->get_queried_object_id() ) 398 ) { 399 $redirect_url = get_permalink( $wp_query->get_queried_object_id() ); 400 $redirect_obj = get_post( $wp_query->get_queried_object_id() ); 401 } 402 } 403 } 404 405 // Post paging. 406 if ( is_singular() && get_query_var( 'page' ) ) { 407 $page = get_query_var( 'page' ); 408 409 if ( ! $redirect_url ) { 410 $redirect_url = get_permalink( get_queried_object_id() ); 411 $redirect_obj = get_post( get_queried_object_id() ); 412 } 413 414 if ( $page > 1 ) { 415 $redirect_url = trailingslashit( $redirect_url ); 416 417 if ( is_front_page() ) { 418 $redirect_url .= user_trailingslashit( "$wp_rewrite->pagination_base/$page", 'paged' ); 419 } else { 420 $redirect_url .= user_trailingslashit( $page, 'single_paged' ); 421 } 422 } 423 424 $redirect['query'] = remove_query_arg( 'page', $redirect['query'] ); 425 } 426 427 if ( get_query_var( 'sitemap' ) ) { 428 $redirect_url = get_sitemap_url( get_query_var( 'sitemap' ), get_query_var( 'sitemap-subtype' ), get_query_var( 'paged' ) ); 429 $redirect['query'] = remove_query_arg( array( 'sitemap', 'sitemap-subtype', 'paged' ), $redirect['query'] ); 430 } elseif ( get_query_var( 'paged' ) || is_feed() || get_query_var( 'cpage' ) ) { 431 // Paging and feeds. 432 $paged = get_query_var( 'paged' ); 433 $feed = get_query_var( 'feed' ); 434 $cpage = get_query_var( 'cpage' ); 435 436 while ( preg_match( "#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", $redirect['path'] ) 437 || preg_match( '#/(comments/?)?(feed|rss2?|rdf|atom)(/+)?$#', $redirect['path'] ) 438 || preg_match( "#/{$wp_rewrite->comments_pagination_base}-[0-9]+(/+)?$#", $redirect['path'] ) 439 ) { 440 // Strip off any existing paging. 441 $redirect['path'] = preg_replace( "#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", '/', $redirect['path'] ); 442 // Strip off feed endings. 443 $redirect['path'] = preg_replace( '#/(comments/?)?(feed|rss2?|rdf|atom)(/+|$)#', '/', $redirect['path'] ); 444 // Strip off any existing comment paging. 445 $redirect['path'] = preg_replace( "#/{$wp_rewrite->comments_pagination_base}-[0-9]+?(/+)?$#", '/', $redirect['path'] ); 446 } 447 448 $addl_path = ''; 449 $default_feed = get_default_feed(); 450 451 if ( is_feed() && in_array( $feed, $wp_rewrite->feeds, true ) ) { 452 $addl_path = ! empty( $addl_path ) ? trailingslashit( $addl_path ) : ''; 453 454 if ( ! is_singular() && get_query_var( 'withcomments' ) ) { 455 $addl_path .= 'comments/'; 456 } 457 458 if ( ( 'rss' === $default_feed && 'feed' === $feed ) || 'rss' === $feed ) { 459 $format = ( 'rss2' === $default_feed ) ? '' : 'rss2'; 460 } else { 461 $format = ( $default_feed === $feed || 'feed' === $feed ) ? '' : $feed; 462 } 463 464 $addl_path .= user_trailingslashit( 'feed/' . $format, 'feed' ); 465 466 $redirect['query'] = remove_query_arg( 'feed', $redirect['query'] ); 467 } elseif ( is_feed() && 'old' === $feed ) { 468 $old_feed_files = array( 469 'wp-atom.php' => 'atom', 470 'wp-commentsrss2.php' => 'comments_rss2', 471 'wp-feed.php' => $default_feed, 472 'wp-rdf.php' => 'rdf', 473 'wp-rss.php' => 'rss2', 474 'wp-rss2.php' => 'rss2', 475 ); 476 477 if ( isset( $old_feed_files[ basename( $redirect['path'] ) ] ) ) { 478 $redirect_url = get_feed_link( $old_feed_files[ basename( $redirect['path'] ) ] ); 479 480 wp_redirect( $redirect_url, 301 ); 481 die(); 482 } 483 } 484 485 if ( $paged > 0 ) { 486 $redirect['query'] = remove_query_arg( 'paged', $redirect['query'] ); 487 488 if ( ! is_feed() ) { 489 if ( ! is_single() ) { 490 $addl_path = ! empty( $addl_path ) ? trailingslashit( $addl_path ) : ''; 491 492 if ( $paged > 1 ) { 493 $addl_path .= user_trailingslashit( "$wp_rewrite->pagination_base/$paged", 'paged' ); 494 } 495 } 496 } elseif ( $paged > 1 ) { 497 $redirect['query'] = add_query_arg( 'paged', $paged, $redirect['query'] ); 498 } 499 } 500 501 $default_comments_page = get_option( 'default_comments_page' ); 502 503 if ( get_option( 'page_comments' ) 504 && ( 'newest' === $default_comments_page && $cpage > 0 505 || 'newest' !== $default_comments_page && $cpage > 1 ) 506 ) { 507 $addl_path = ( ! empty( $addl_path ) ? trailingslashit( $addl_path ) : '' ); 508 $addl_path .= user_trailingslashit( $wp_rewrite->comments_pagination_base . '-' . $cpage, 'commentpaged' ); 509 510 $redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] ); 511 } 512 513 // Strip off trailing /index.php/. 514 $redirect['path'] = preg_replace( '|/' . preg_quote( $wp_rewrite->index, '|' ) . '/?$|', '/', $redirect['path'] ); 515 $redirect['path'] = user_trailingslashit( $redirect['path'] ); 516 517 if ( ! empty( $addl_path ) 518 && $wp_rewrite->using_index_permalinks() 519 && strpos( $redirect['path'], '/' . $wp_rewrite->index . '/' ) === false 520 ) { 521 $redirect['path'] = trailingslashit( $redirect['path'] ) . $wp_rewrite->index . '/'; 522 } 523 524 if ( ! empty( $addl_path ) ) { 525 $redirect['path'] = trailingslashit( $redirect['path'] ) . $addl_path; 526 } 527 528 $redirect_url = $redirect['scheme'] . '://' . $redirect['host'] . $redirect['path']; 529 } 530 531 if ( 'wp-register.php' === basename( $redirect['path'] ) ) { 532 if ( is_multisite() ) { 533 /** This filter is documented in wp-login.php */ 534 $redirect_url = apply_filters( 'wp_signup_location', network_site_url( 'wp-signup.php' ) ); 535 } else { 536 $redirect_url = wp_registration_url(); 537 } 538 539 wp_redirect( $redirect_url, 301 ); 540 die(); 541 } 542 } 543 544 $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] ); 545 546 // Tack on any additional query vars. 547 if ( $redirect_url && ! empty( $redirect['query'] ) ) { 548 parse_str( $redirect['query'], $_parsed_query ); 549 $redirect = parse_url( $redirect_url ); 550 551 if ( ! empty( $_parsed_query['name'] ) && ! empty( $redirect['query'] ) ) { 552 parse_str( $redirect['query'], $_parsed_redirect_query ); 553 554 if ( empty( $_parsed_redirect_query['name'] ) ) { 555 unset( $_parsed_query['name'] ); 556 } 557 } 558 559 $_parsed_query = array_combine( 560 rawurlencode_deep( array_keys( $_parsed_query ) ), 561 rawurlencode_deep( array_values( $_parsed_query ) ) 562 ); 563 564 $redirect_url = add_query_arg( $_parsed_query, $redirect_url ); 565 } 566 567 if ( $redirect_url ) { 568 $redirect = parse_url( $redirect_url ); 569 } 570 571 // www.example.com vs. example.com 572 $user_home = parse_url( home_url() ); 573 574 if ( ! empty( $user_home['host'] ) ) { 575 $redirect['host'] = $user_home['host']; 576 } 577 578 if ( empty( $user_home['path'] ) ) { 579 $user_home['path'] = '/'; 580 } 581 582 // Handle ports. 583 if ( ! empty( $user_home['port'] ) ) { 584 $redirect['port'] = $user_home['port']; 585 } else { 586 unset( $redirect['port'] ); 587 } 588 589 // Trailing /index.php. 590 $redirect['path'] = preg_replace( '|/' . preg_quote( $wp_rewrite->index, '|' ) . '/*?$|', '/', $redirect['path'] ); 591 592 $punctuation_pattern = implode( 593 '|', 594 array_map( 595 'preg_quote', 596 array( 597 ' ', 598 '%20', // Space. 599 '!', 600 '%21', // Exclamation mark. 601 '"', 602 '%22', // Double quote. 603 "'", 604 '%27', // Single quote. 605 '(', 606 '%28', // Opening bracket. 607 ')', 608 '%29', // Closing bracket. 609 ',', 610 '%2C', // Comma. 611 '.', 612 '%2E', // Period. 613 ';', 614 '%3B', // Semicolon. 615 '{', 616 '%7B', // Opening curly bracket. 617 '}', 618 '%7D', // Closing curly bracket. 619 '%E2%80%9C', // Opening curly quote. 620 '%E2%80%9D', // Closing curly quote. 621 ) 622 ) 623 ); 624 625 // Remove trailing spaces and end punctuation from the path. 626 $redirect['path'] = preg_replace( "#($punctuation_pattern)+$#", '', $redirect['path'] ); 627 628 if ( ! empty( $redirect['query'] ) ) { 629 // Remove trailing spaces and end punctuation from certain terminating query string args. 630 $redirect['query'] = preg_replace( "#((^|&)(p|page_id|cat|tag)=[^&]*?)($punctuation_pattern)+$#", '$1', $redirect['query'] ); 631 632 // Clean up empty query strings. 633 $redirect['query'] = trim( preg_replace( '#(^|&)(p|page_id|cat|tag)=?(&|$)#', '&', $redirect['query'] ), '&' ); 634 635 // Redirect obsolete feeds. 636 $redirect['query'] = preg_replace( '#(^|&)feed=rss(&|$)#', '$1feed=rss2$2', $redirect['query'] ); 637 638 // Remove redundant leading ampersands. 639 $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] ); 640 } 641 642 // Strip /index.php/ when we're not using PATHINFO permalinks. 643 if ( ! $wp_rewrite->using_index_permalinks() ) { 644 $redirect['path'] = str_replace( '/' . $wp_rewrite->index . '/', '/', $redirect['path'] ); 645 } 646 647 // Trailing slashes. 648 if ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() 649 && ! is_404() && ( ! is_front_page() || is_front_page() && get_query_var( 'paged' ) > 1 ) 650 ) { 651 $user_ts_type = ''; 652 653 if ( get_query_var( 'paged' ) > 0 ) { 654 $user_ts_type = 'paged'; 655 } else { 656 foreach ( array( 'single', 'category', 'page', 'day', 'month', 'year', 'home' ) as $type ) { 657 $func = 'is_' . $type; 658 if ( call_user_func( $func ) ) { 659 $user_ts_type = $type; 660 break; 661 } 662 } 663 } 664 665 $redirect['path'] = user_trailingslashit( $redirect['path'], $user_ts_type ); 666 } elseif ( is_front_page() ) { 667 $redirect['path'] = trailingslashit( $redirect['path'] ); 668 } 669 670 // Remove trailing slash for robots.txt or sitemap requests. 671 if ( is_robots() 672 || ! empty( get_query_var( 'sitemap' ) ) || ! empty( get_query_var( 'sitemap-stylesheet' ) ) 673 ) { 674 $redirect['path'] = untrailingslashit( $redirect['path'] ); 675 } 676 677 // Strip multiple slashes out of the URL. 678 if ( strpos( $redirect['path'], '//' ) > -1 ) { 679 $redirect['path'] = preg_replace( '|/+|', '/', $redirect['path'] ); 680 } 681 682 // Always trailing slash the Front Page URL. 683 if ( trailingslashit( $redirect['path'] ) === trailingslashit( $user_home['path'] ) ) { 684 $redirect['path'] = trailingslashit( $redirect['path'] ); 685 } 686 687 $original_host_low = strtolower( $original['host'] ); 688 $redirect_host_low = strtolower( $redirect['host'] ); 689 690 // Ignore differences in host capitalization, as this can lead to infinite redirects. 691 // Only redirect no-www <=> yes-www. 692 if ( $original_host_low === $redirect_host_low 693 || ( 'www.' . $original_host_low !== $redirect_host_low 694 && 'www.' . $redirect_host_low !== $original_host_low ) 695 ) { 696 $redirect['host'] = $original['host']; 697 } 698 699 $compare_original = array( $original['host'], $original['path'] ); 700 701 if ( ! empty( $original['port'] ) ) { 702 $compare_original[] = $original['port']; 703 } 704 705 if ( ! empty( $original['query'] ) ) { 706 $compare_original[] = $original['query']; 707 } 708 709 $compare_redirect = array( $redirect['host'], $redirect['path'] ); 710 711 if ( ! empty( $redirect['port'] ) ) { 712 $compare_redirect[] = $redirect['port']; 713 } 714 715 if ( ! empty( $redirect['query'] ) ) { 716 $compare_redirect[] = $redirect['query']; 717 } 718 719 if ( $compare_original !== $compare_redirect ) { 720 $redirect_url = $redirect['scheme'] . '://' . $redirect['host']; 721 722 if ( ! empty( $redirect['port'] ) ) { 723 $redirect_url .= ':' . $redirect['port']; 724 } 725 726 $redirect_url .= $redirect['path']; 727 728 if ( ! empty( $redirect['query'] ) ) { 729 $redirect_url .= '?' . $redirect['query']; 730 } 731 } 732 733 if ( ! $redirect_url || $redirect_url === $requested_url ) { 734 return; 735 } 736 737 // Hex encoded octets are case-insensitive. 738 if ( false !== strpos( $requested_url, '%' ) ) { 739 if ( ! function_exists( 'lowercase_octets' ) ) { 740 /** 741 * Converts the first hex-encoded octet match to lowercase. 742 * 743 * @since 3.1.0 744 * @ignore 745 * 746 * @param array $matches Hex-encoded octet matches for the requested URL. 747 * @return string Lowercased version of the first match. 748 */ 749 function lowercase_octets( $matches ) { 750 return strtolower( $matches[0] ); 751 } 752 } 753 754 $requested_url = preg_replace_callback( '|%[a-fA-F0-9][a-fA-F0-9]|', 'lowercase_octets', $requested_url ); 755 } 756 757 if ( $redirect_obj instanceof WP_Post ) { 758 $post_status_obj = get_post_status_object( get_post_status( $redirect_obj ) ); 759 /* 760 * Unset the redirect object and URL if they are not readable by the user. 761 * This condition is a little confusing as the condition needs to pass if 762 * the post is not readable by the user. That's why there are ! (not) conditions 763 * throughout. 764 */ 765 if ( 766 // Private post statuses only redirect if the user can read them. 767 ! ( 768 $post_status_obj->private && 769 current_user_can( 'read_post', $redirect_obj->ID ) 770 ) && 771 // For other posts, only redirect if publicly viewable. 772 ! is_post_publicly_viewable( $redirect_obj ) 773 ) { 774 $redirect_obj = false; 775 $redirect_url = false; 776 } 777 } 778 779 /** 780 * Filters the canonical redirect URL. 781 * 782 * Returning false to this filter will cancel the redirect. 783 * 784 * @since 2.3.0 785 * 786 * @param string $redirect_url The redirect URL. 787 * @param string $requested_url The requested URL. 788 */ 789 $redirect_url = apply_filters( 'redirect_canonical', $redirect_url, $requested_url ); 790 791 // Yes, again -- in case the filter aborted the request. 792 if ( ! $redirect_url || strip_fragment_from_url( $redirect_url ) === strip_fragment_from_url( $requested_url ) ) { 793 return; 794 } 795 796 if ( $do_redirect ) { 797 // Protect against chained redirects. 798 if ( ! redirect_canonical( $redirect_url, false ) ) { 799 wp_redirect( $redirect_url, 301 ); 800 exit; 801 } else { 802 // Debug. 803 // die("1: $redirect_url<br />2: " . redirect_canonical( $redirect_url, false ) ); 804 return; 805 } 806 } else { 807 return $redirect_url; 808 } 809 } 810 811 /** 812 * Removes arguments from a query string if they are not present in a URL 813 * DO NOT use this in plugin code. 814 * 815 * @since 3.4.0 816 * @access private 817 * 818 * @param string $query_string 819 * @param array $args_to_check 820 * @param string $url 821 * @return string The altered query string 822 */ 823 function _remove_qs_args_if_not_in_url( $query_string, array $args_to_check, $url ) { 824 $parsed_url = parse_url( $url ); 825 826 if ( ! empty( $parsed_url['query'] ) ) { 827 parse_str( $parsed_url['query'], $parsed_query ); 828 829 foreach ( $args_to_check as $qv ) { 830 if ( ! isset( $parsed_query[ $qv ] ) ) { 831 $query_string = remove_query_arg( $qv, $query_string ); 832 } 833 } 834 } else { 835 $query_string = remove_query_arg( $args_to_check, $query_string ); 836 } 837 838 return $query_string; 839 } 840 841 /** 842 * Strips the #fragment from a URL, if one is present. 843 * 844 * @since 4.4.0 845 * 846 * @param string $url The URL to strip. 847 * @return string The altered URL. 848 */ 849 function strip_fragment_from_url( $url ) { 850 $parsed_url = wp_parse_url( $url ); 851 852 if ( ! empty( $parsed_url['host'] ) ) { 853 $url = ''; 854 855 if ( ! empty( $parsed_url['scheme'] ) ) { 856 $url = $parsed_url['scheme'] . ':'; 857 } 858 859 $url .= '//' . $parsed_url['host']; 860 861 if ( ! empty( $parsed_url['port'] ) ) { 862 $url .= ':' . $parsed_url['port']; 863 } 864 865 if ( ! empty( $parsed_url['path'] ) ) { 866 $url .= $parsed_url['path']; 867 } 868 869 if ( ! empty( $parsed_url['query'] ) ) { 870 $url .= '?' . $parsed_url['query']; 871 } 872 } 873 874 return $url; 875 } 876 877 /** 878 * Attempts to guess the correct URL for a 404 request based on query vars. 879 * 880 * @since 2.3.0 881 * 882 * @global wpdb $wpdb WordPress database abstraction object. 883 * 884 * @return string|false The correct URL if one is found. False on failure. 885 */ 886 function redirect_guess_404_permalink() { 887 global $wpdb; 888 889 /** 890 * Filters whether to attempt to guess a redirect URL for a 404 request. 891 * 892 * Returning a false value from the filter will disable the URL guessing 893 * and return early without performing a redirect. 894 * 895 * @since 5.5.0 896 * 897 * @param bool $do_redirect_guess Whether to attempt to guess a redirect URL 898 * for a 404 request. Default true. 899 */ 900 if ( false === apply_filters( 'do_redirect_guess_404_permalink', true ) ) { 901 return false; 902 } 903 904 /** 905 * Short-circuits the redirect URL guessing for 404 requests. 906 * 907 * Returning a non-null value from the filter will effectively short-circuit 908 * the URL guessing, returning the passed value instead. 909 * 910 * @since 5.5.0 911 * 912 * @param null|string|false $pre Whether to short-circuit guessing the redirect for a 404. 913 * Default null to continue with the URL guessing. 914 */ 915 $pre = apply_filters( 'pre_redirect_guess_404_permalink', null ); 916 if ( null !== $pre ) { 917 return $pre; 918 } 919 920 if ( get_query_var( 'name' ) ) { 921 /** 922 * Filters whether to perform a strict guess for a 404 redirect. 923 * 924 * Returning a truthy value from the filter will redirect only exact post_name matches. 925 * 926 * @since 5.5.0 927 * 928 * @param bool $strict_guess Whether to perform a strict guess. Default false (loose guess). 929 */ 930 $strict_guess = apply_filters( 'strict_redirect_guess_404_permalink', false ); 931 932 if ( $strict_guess ) { 933 $where = $wpdb->prepare( 'post_name = %s', get_query_var( 'name' ) ); 934 } else { 935 $where = $wpdb->prepare( 'post_name LIKE %s', $wpdb->esc_like( get_query_var( 'name' ) ) . '%' ); 936 } 937 938 // If any of post_type, year, monthnum, or day are set, use them to refine the query. 939 if ( get_query_var( 'post_type' ) ) { 940 if ( is_array( get_query_var( 'post_type' ) ) ) { 941 // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare 942 $where .= " AND post_type IN ('" . join( "', '", esc_sql( get_query_var( 'post_type' ) ) ) . "')"; 943 } else { 944 $where .= $wpdb->prepare( ' AND post_type = %s', get_query_var( 'post_type' ) ); 945 } 946 } else { 947 $where .= " AND post_type IN ('" . implode( "', '", get_post_types( array( 'public' => true ) ) ) . "')"; 948 } 949 950 if ( get_query_var( 'year' ) ) { 951 $where .= $wpdb->prepare( ' AND YEAR(post_date) = %d', get_query_var( 'year' ) ); 952 } 953 if ( get_query_var( 'monthnum' ) ) { 954 $where .= $wpdb->prepare( ' AND MONTH(post_date) = %d', get_query_var( 'monthnum' ) ); 955 } 956 if ( get_query_var( 'day' ) ) { 957 $where .= $wpdb->prepare( ' AND DAYOFMONTH(post_date) = %d', get_query_var( 'day' ) ); 958 } 959 960 $publicly_viewable_statuses = array_filter( get_post_stati(), 'is_post_status_viewable' ); 961 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared 962 $post_id = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE $where AND post_status IN ('" . implode( "', '", esc_sql( $publicly_viewable_statuses ) ) . "')" ); 963 964 if ( ! $post_id ) { 965 return false; 966 } 967 968 if ( get_query_var( 'feed' ) ) { 969 return get_post_comments_feed_link( $post_id, get_query_var( 'feed' ) ); 970 } elseif ( get_query_var( 'page' ) > 1 ) { 971 return trailingslashit( get_permalink( $post_id ) ) . user_trailingslashit( get_query_var( 'page' ), 'single_paged' ); 972 } else { 973 return get_permalink( $post_id ); 974 } 975 } 976 977 return false; 978 } 979 980 /** 981 * Redirects a variety of shorthand URLs to the admin. 982 * 983 * If a user visits example.com/admin, they'll be redirected to /wp-admin. 984 * Visiting /login redirects to /wp-login.php, and so on. 985 * 986 * @since 3.4.0 987 * 988 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. 989 */ 990 function wp_redirect_admin_locations() { 991 global $wp_rewrite; 992 993 if ( ! ( is_404() && $wp_rewrite->using_permalinks() ) ) { 994 return; 995 } 996 997 $admins = array( 998 home_url( 'wp-admin', 'relative' ), 999 home_url( 'dashboard', 'relative' ), 1000 home_url( 'admin', 'relative' ), 1001 site_url( 'dashboard', 'relative' ), 1002 site_url( 'admin', 'relative' ), 1003 ); 1004 1005 if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $admins, true ) ) { 1006 wp_redirect( admin_url() ); 1007 exit; 1008 } 1009 1010 $logins = array( 1011 home_url( 'wp-login.php', 'relative' ), 1012 home_url( 'login', 'relative' ), 1013 site_url( 'login', 'relative' ), 1014 ); 1015 1016 if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $logins, true ) ) { 1017 wp_redirect( wp_login_url() ); 1018 exit; 1019 } 1020 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Nov 21 01:00:03 2024 | Cross-referenced by PHPXref 0.7.1 |