Changeset 47122 for trunk/src/wp-includes/canonical.php
- Timestamp:
- 01/29/2020 12:43:23 AM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/canonical.php
r47060 r47122 48 48 49 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 50 // is non-existent or invalid then no need for preview in query. 51 51 if ( is_preview() && get_query_var( 'p' ) && 'publish' == get_post_status( get_query_var( 'p' ) ) ) { 52 52 if ( ! isset( $_GET['preview_id'] ) … … 62 62 63 63 if ( ! $requested_url && isset( $_SERVER['HTTP_HOST'] ) ) { 64 // build the URL in the address bar64 // Build the URL in the address bar. 65 65 $requested_url = is_ssl() ? 'https://' : 'http://'; 66 66 $requested_url .= $_SERVER['HTTP_HOST']; … … 76 76 $redirect_url = false; 77 77 78 // Notice fixing 78 // Notice fixing. 79 79 if ( ! isset( $redirect['path'] ) ) { 80 80 $redirect['path'] = ''; … … 84 84 } 85 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. 86 /* 87 * If the original URL ended with non-breaking spaces, they were almost 88 * certainly inserted by accident. Let's remove them, so the reader doesn't 89 * see a 404 error with no obvious cause. 90 */ 89 91 $redirect['path'] = preg_replace( '|(%C2%A0)+$|i', '', $redirect['path'] ); 90 92 91 // It's not a preview, so remove it from URL 93 // It's not a preview, so remove it from URL. 92 94 if ( get_query_var( 'preview' ) ) { 93 95 $redirect['query'] = remove_query_arg( 'preview', $redirect['query'] ); … … 121 123 } 122 124 123 // These tests give us a WP-generated permalink 125 // These tests give us a WP-generated permalink. 124 126 if ( is_404() ) { 125 127 126 // Redirect ?page_id, ?p=, ?attachment_id= to their respective url's128 // Redirect ?page_id, ?p=, ?attachment_id= to their respective URLs. 127 129 $id = max( get_query_var( 'p' ), get_query_var( 'page_id' ), get_query_var( 'attachment_id' ) ); 128 130 $redirect_post = $id ? get_post( $id ) : false; … … 163 165 } 164 166 } elseif ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) { 165 // rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101167 // Rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101. 166 168 if ( is_attachment() && 167 169 ! array_diff( array_keys( $wp->query_vars ), array( 'attachment', 'attachment_id' ) ) && … … 200 202 $m = get_query_var( 'm' ); 201 203 switch ( strlen( $m ) ) { 202 case 4: // Yearly 204 case 4: // Yearly. 203 205 $redirect_url = get_year_link( $m ); 204 206 break; 205 case 6: // Monthly 207 case 6: // Monthly. 206 208 $redirect_url = get_month_link( substr( $m, 0, 4 ), substr( $m, 4, 2 ) ); 207 209 break; 208 case 8: // Daily 210 case 8: // Daily. 209 211 $redirect_url = get_day_link( substr( $m, 0, 4 ), substr( $m, 4, 2 ), substr( $m, 6, 2 ) ); 210 212 break; … … 213 215 $redirect['query'] = remove_query_arg( 'm', $redirect['query'] ); 214 216 } 215 // now moving on to non ?m=X year/month/day links217 // Now moving on to non ?m=X year/month/day links. 216 218 } elseif ( is_day() && get_query_var( 'year' ) && get_query_var( 'monthnum' ) && ! empty( $_GET['day'] ) ) { 217 219 $redirect_url = get_day_link( get_query_var( 'year' ), get_query_var( 'monthnum' ), get_query_var( 'day' ) ); … … 237 239 } 238 240 } 239 } elseif ( is_category() || is_tag() || is_tax() ) { // Terms ( Tags/categories)241 } elseif ( is_category() || is_tag() || is_tax() ) { // Terms (tags/categories). 240 242 241 243 $term_count = 0; … … 249 251 if ( $tax_url && ! is_wp_error( $tax_url ) ) { 250 252 if ( ! empty( $redirect['query'] ) ) { 251 // Strip taxonomy query vars off the url.253 // Strip taxonomy query vars off the URL. 252 254 $qv_remove = array( 'term', 'taxonomy' ); 253 255 if ( is_category() ) { … … 257 259 $qv_remove[] = 'tag'; 258 260 $qv_remove[] = 'tag_id'; 259 } else { // Custom taxonomies will have a custom query var, remove those too: 261 } else { 262 // Custom taxonomies will have a custom query var, remove those too. 260 263 $tax_obj = get_taxonomy( $obj->taxonomy ); 261 264 if ( false !== $tax_obj->query_var ) { … … 266 269 $rewrite_vars = array_diff( array_keys( $wp_query->query ), array_keys( $_GET ) ); 267 270 268 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 269 $redirect['query'] = remove_query_arg( $qv_remove, $redirect['query'] ); //Remove all of the per-tax qv's 270 271 // Create the destination url for this taxonomy 271 // Check to see if all the query vars are coming from the rewrite, none are set via $_GET. 272 if ( ! array_diff( $rewrite_vars, array_keys( $_GET ) ) ) { 273 // Remove all of the per-tax query vars. 274 $redirect['query'] = remove_query_arg( $qv_remove, $redirect['query'] ); 275 276 // Create the destination URL for this taxonomy. 272 277 $tax_url = parse_url( $tax_url ); 273 if ( ! empty( $tax_url['query'] ) ) { // Taxonomy accessible via ?taxonomy=..&term=.. or any custom qv.. 278 if ( ! empty( $tax_url['query'] ) ) { 279 // Taxonomy accessible via ?taxonomy=...&term=... or any custom query var. 274 280 parse_str( $tax_url['query'], $query_vars ); 275 281 $redirect['query'] = add_query_arg( $query_vars, $redirect['query'] ); 276 } else { // Taxonomy is accessible via a "pretty-URL" 282 } else { 283 // Taxonomy is accessible via a "pretty URL". 277 284 $redirect['path'] = $tax_url['path']; 278 285 } 279 } else { // Some query vars are set via $_GET. Unset those from $_GET that exist via the rewrite 286 } else { 287 // Some query vars are set via $_GET. Unset those from $_GET that exist via the rewrite. 280 288 foreach ( $qv_remove as $_qv ) { 281 289 if ( isset( $rewrite_vars[ $_qv ] ) ) { … … 297 305 } 298 306 299 // Post Paging307 // Post paging. 300 308 if ( is_singular() && get_query_var( 'page' ) ) { 301 309 if ( ! $redirect_url ) { … … 314 322 } 315 323 316 // paging and feeds324 // Paging and feeds. 317 325 if ( get_query_var( 'paged' ) || is_feed() || get_query_var( 'cpage' ) ) { 318 326 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'] ) ) { 319 // Strip off paging and feed 320 $redirect['path'] = preg_replace( "#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", '/', $redirect['path'] ); // strip off any existing paging321 $redirect['path'] = preg_replace( '#/(comments/?)?(feed|rss2?|rdf|atom)(/+|$)#', '/', $redirect['path'] ); // strip off feed endings322 $redirect['path'] = preg_replace( "#/{$wp_rewrite->comments_pagination_base}-[0-9]+?(/+)?$#", '/', $redirect['path'] ); // strip off any existing comment paging327 // Strip off paging and feed. 328 $redirect['path'] = preg_replace( "#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", '/', $redirect['path'] ); // Strip off any existing paging. 329 $redirect['path'] = preg_replace( '#/(comments/?)?(feed|rss2?|rdf|atom)(/+|$)#', '/', $redirect['path'] ); // Strip off feed endings. 330 $redirect['path'] = preg_replace( "#/{$wp_rewrite->comments_pagination_base}-[0-9]+?(/+)?$#", '/', $redirect['path'] ); // Strip off any existing comment paging. 323 331 } 324 332 … … 373 381 } 374 382 375 $redirect['path'] = user_trailingslashit( preg_replace( '|/' . preg_quote( $wp_rewrite->index, '|' ) . '/?$|', '/', $redirect['path'] ) ); // strip off trailing /index.php/383 $redirect['path'] = user_trailingslashit( preg_replace( '|/' . preg_quote( $wp_rewrite->index, '|' ) . '/?$|', '/', $redirect['path'] ) ); // Strip off trailing /index.php/. 376 384 if ( ! empty( $addl_path ) && $wp_rewrite->using_index_permalinks() && strpos( $redirect['path'], '/' . $wp_rewrite->index . '/' ) === false ) { 377 385 $redirect['path'] = trailingslashit( $redirect['path'] ) . $wp_rewrite->index . '/'; … … 396 404 } 397 405 398 // tack on any additional query vars406 // Tack on any additional query vars. 399 407 $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] ); 400 408 if ( $redirect_url && ! empty( $redirect['query'] ) ) { … … 421 429 } 422 430 423 // www.example.com vs example.com431 // www.example.com vs. example.com 424 432 $user_home = @parse_url( home_url() ); 425 433 if ( ! empty( $user_home['host'] ) ) { … … 430 438 } 431 439 432 // Handle ports 440 // Handle ports. 433 441 if ( ! empty( $user_home['port'] ) ) { 434 442 $redirect['port'] = $user_home['port']; … … 437 445 } 438 446 439 // trailing /index.php447 // Trailing /index.php. 440 448 $redirect['path'] = preg_replace( '|/' . preg_quote( $wp_rewrite->index, '|' ) . '/*?$|', '/', $redirect['path'] ); 441 449 … … 446 454 array( 447 455 ' ', 448 '%20', // space456 '%20', // Space. 449 457 '!', 450 '%21', // exclamation mark458 '%21', // Exclamation mark. 451 459 '"', 452 '%22', // double quote460 '%22', // Double quote. 453 461 "'", 454 '%27', // single quote462 '%27', // Single quote. 455 463 '(', 456 '%28', // opening bracket464 '%28', // Opening bracket. 457 465 ')', 458 '%29', // closing bracket466 '%29', // Closing bracket. 459 467 ',', 460 '%2C', // comma468 '%2C', // Comma. 461 469 '.', 462 '%2E', // period470 '%2E', // Period. 463 471 ';', 464 '%3B', // semicolon472 '%3B', // Semicolon. 465 473 '{', 466 '%7B', // opening curly bracket474 '%7B', // Opening curly bracket. 467 475 '}', 468 '%7D', // closing curly bracket469 '%E2%80%9C', // opening curly quote470 '%E2%80%9D', // closing curly quote476 '%7D', // Closing curly bracket. 477 '%E2%80%9C', // Opening curly quote. 478 '%E2%80%9D', // Closing curly quote. 471 479 ) 472 480 ) … … 480 488 $redirect['query'] = preg_replace( "#((p|page_id|cat|tag)=[^&]*?)($punctuation_pattern)+$#", '$1', $redirect['query'] ); 481 489 482 // Clean up empty query strings 490 // Clean up empty query strings. 483 491 $redirect['query'] = trim( preg_replace( '#(^|&)(p|page_id|cat|tag)=?(&|$)#', '&', $redirect['query'] ), '&' ); 484 492 485 // Redirect obsolete feeds 493 // Redirect obsolete feeds. 486 494 $redirect['query'] = preg_replace( '#(^|&)feed=rss(&|$)#', '$1feed=rss2$2', $redirect['query'] ); 487 495 488 // Remove redundant leading ampersands 496 // Remove redundant leading ampersands. 489 497 $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] ); 490 498 } 491 499 492 // strip /index.php/ when we're not using PATHINFO permalinks500 // Strip /index.php/ when we're not using PATHINFO permalinks. 493 501 if ( ! $wp_rewrite->using_index_permalinks() ) { 494 502 $redirect['path'] = str_replace( '/' . $wp_rewrite->index . '/', '/', $redirect['path'] ); 495 503 } 496 504 497 // trailing slashes505 // Trailing slashes. 498 506 if ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() && ! is_404() && ( ! is_front_page() || ( is_front_page() && ( get_query_var( 'paged' ) > 1 ) ) ) ) { 499 507 $user_ts_type = ''; … … 514 522 } 515 523 516 // Strip multiple slashes out of the URL 524 // Strip multiple slashes out of the URL. 517 525 if ( strpos( $redirect['path'], '//' ) > -1 ) { 518 526 $redirect['path'] = preg_replace( '|/+|', '/', $redirect['path'] ); 519 527 } 520 528 521 // Always trailing slash the Front Page URL 529 // Always trailing slash the Front Page URL. 522 530 if ( trailingslashit( $redirect['path'] ) == trailingslashit( $user_home['path'] ) ) { 523 531 $redirect['path'] = trailingslashit( $redirect['path'] ); 524 532 } 525 533 526 // Ignore differences in host capitalization, as this can lead to infinite redirects 527 // Only redirect no-www <=> yes-www 534 // Ignore differences in host capitalization, as this can lead to infinite redirects. 535 // Only redirect no-www <=> yes-www. 528 536 if ( strtolower( $original['host'] ) == strtolower( $redirect['host'] ) || 529 537 ( strtolower( $original['host'] ) != 'www.' . strtolower( $redirect['host'] ) && 'www.' . strtolower( $original['host'] ) != strtolower( $redirect['host'] ) ) ) { … … 597 605 $redirect_url = apply_filters( 'redirect_canonical', $redirect_url, $requested_url ); 598 606 599 // yes, again -- in case the filter aborted the request607 // Yes, again -- in case the filter aborted the request. 600 608 if ( ! $redirect_url || strip_fragment_from_url( $redirect_url ) == strip_fragment_from_url( $requested_url ) ) { 601 609 return; … … 603 611 604 612 if ( $do_redirect ) { 605 // protect against chained redirects613 // Protect against chained redirects. 606 614 if ( ! redirect_canonical( $redirect_url, false ) ) { 607 615 wp_redirect( $redirect_url, 301 ); 608 616 exit(); 609 617 } else { 610 // Debug 618 // Debug. 611 619 // die("1: $redirect_url<br />2: " . redirect_canonical( $redirect_url, false ) ); 612 620 return; … … 688 696 $where = $wpdb->prepare( 'post_name LIKE %s', $wpdb->esc_like( get_query_var( 'name' ) ) . '%' ); 689 697 690 // if any of post_type, year, monthnum, or day are set, use them to refine the query698 // If any of post_type, year, monthnum, or day are set, use them to refine the query. 691 699 if ( get_query_var( 'post_type' ) ) { 692 700 $where .= $wpdb->prepare( ' AND post_type = %s', get_query_var( 'post_type' ) );
Note: See TracChangeset
for help on using the changeset viewer.