WordPress 3.0 beta 1 documentation kindly provided to you by Hay Kranen
| [ Index ] |
PHP Cross Reference of WordPress 3.0 beta 1 |
|
| [ Index ] [ Variables ] [ Functions ] [ Classes ] [ Constants ] [ Statistics ] | ||
[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 penality for duplicate content by redirecting all incoming links to 18 * one or the other. 19 * 20 * Prevents redirection for feeds, trackbacks, searches, comment popup, and 21 * admin URLs. Does not redirect on IIS, page/post previews, and on form data. 22 * 23 * Will also attempt to find the correct link when a user enters a URL that does 24 * not exist based on exact WordPress query. Will instead try to parse the URL 25 * or query in an attempt to figure the correct page to go to. 26 * 27 * @since 2.3.0 28 * @uses $wp_rewrite 29 * @uses $is_IIS 30 * 31 * @param string $requested_url Optional. The URL that was requested, used to 32 * figure if redirect is needed. 33 * @param bool $do_redirect Optional. Redirect to the new URL. 34 * @return null|false|string Null, if redirect not needed. False, if redirect 35 * not needed or the string of the URL 36 */ 37 function redirect_canonical($requested_url=null, $do_redirect=true) { 38 global $wp_rewrite, $is_IIS, $wp_query, $wpdb; 39 40 if ( is_trackback() || is_search() || is_comments_popup() || is_admin() || $is_IIS || ( isset($_POST) && count($_POST) ) || is_preview() || is_robots() ) 41 return; 42 43 if ( !$requested_url ) { 44 // build the URL in the address bar 45 $requested_url = is_ssl() ? 'https://' : 'http://'; 46 $requested_url .= $_SERVER['HTTP_HOST']; 47 $requested_url .= $_SERVER['REQUEST_URI']; 48 } 49 50 $original = @parse_url($requested_url); 51 if ( false === $original ) 52 return; 53 54 // Some PHP setups turn requests for / into /index.php in REQUEST_URI 55 // See: http://trac.wordpress.org/ticket/5017 56 // See: http://trac.wordpress.org/ticket/7173 57 // Disabled, for now: 58 // $original['path'] = preg_replace('|/index\.php$|', '/', $original['path']); 59 60 $redirect = $original; 61 $redirect_url = false; 62 63 // Notice fixing 64 if ( !isset($redirect['path']) ) 65 $redirect['path'] = ''; 66 if ( !isset($redirect['query']) ) 67 $redirect['query'] = ''; 68 69 if ( is_singular() && 1 > $wp_query->post_count && ($id = get_query_var('p')) ) { 70 71 $vars = $wpdb->get_results( $wpdb->prepare("SELECT post_type, post_parent FROM $wpdb->posts WHERE ID = %d", $id) ); 72 73 if ( isset($vars[0]) && $vars = $vars[0] ) { 74 if ( 'revision' == $vars->post_type && $vars->post_parent > 0 ) 75 $id = $vars->post_parent; 76 77 if ( $redirect_url = get_permalink($id) ) 78 $redirect['query'] = remove_query_arg(array('p', 'page_id', 'attachment_id'), $redirect['query']); 79 } 80 } 81 82 // These tests give us a WP-generated permalink 83 if ( is_404() ) { 84 $redirect_url = redirect_guess_404_permalink(); 85 } elseif ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) { 86 // rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101 87 if ( is_attachment() && !empty($_GET['attachment_id']) && ! $redirect_url ) { 88 if ( $redirect_url = get_attachment_link(get_query_var('attachment_id')) ) 89 $redirect['query'] = remove_query_arg('attachment_id', $redirect['query']); 90 } elseif ( is_single() && !empty($_GET['p']) && ! $redirect_url ) { 91 if ( $redirect_url = get_permalink(get_query_var('p')) ) 92 $redirect['query'] = remove_query_arg('p', $redirect['query']); 93 if ( get_query_var( 'page' ) ) { 94 $redirect_url = trailingslashit( $redirect_url ) . user_trailingslashit( get_query_var( 'page' ), 'single_paged' ); 95 $redirect['query'] = remove_query_arg( 'page', $redirect['query'] ); 96 } 97 } elseif ( is_single() && !empty($_GET['name']) && ! $redirect_url ) { 98 if ( $redirect_url = get_permalink( $wp_query->get_queried_object_id() ) ) 99 $redirect['query'] = remove_query_arg('name', $redirect['query']); 100 } elseif ( is_page() && !empty($_GET['page_id']) && ! $redirect_url ) { 101 if ( $redirect_url = get_permalink(get_query_var('page_id')) ) 102 $redirect['query'] = remove_query_arg('page_id', $redirect['query']); 103 } elseif ( is_page() && isset($wp_query->queried_object) && 'page' == get_option('show_on_front') && $wp_query->queried_object->ID == get_option('page_on_front') && ! $redirect_url ) { 104 $redirect_url = home_url('/'); 105 } 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 ) { 106 if ( $redirect_url = get_permalink(get_option('page_for_posts')) ) 107 $redirect['query'] = remove_query_arg('page_id', $redirect['query']); 108 } elseif ( !empty($_GET['m']) && ( is_year() || is_month() || is_day() ) ) { 109 $m = get_query_var('m'); 110 switch ( strlen($m) ) { 111 case 4: // Yearly 112 $redirect_url = get_year_link($m); 113 break; 114 case 6: // Monthly 115 $redirect_url = get_month_link( substr($m, 0, 4), substr($m, 4, 2) ); 116 break; 117 case 8: // Daily 118 $redirect_url = get_day_link(substr($m, 0, 4), substr($m, 4, 2), substr($m, 6, 2)); 119 break; 120 } 121 if ( $redirect_url ) 122 $redirect['query'] = remove_query_arg('m', $redirect['query']); 123 // now moving on to non ?m=X year/month/day links 124 } elseif ( is_day() && get_query_var('year') && get_query_var('monthnum') && !empty($_GET['day']) ) { 125 if ( $redirect_url = get_day_link(get_query_var('year'), get_query_var('monthnum'), get_query_var('day')) ) 126 $redirect['query'] = remove_query_arg(array('year', 'monthnum', 'day'), $redirect['query']); 127 } elseif ( is_month() && get_query_var('year') && !empty($_GET['monthnum']) ) { 128 if ( $redirect_url = get_month_link(get_query_var('year'), get_query_var('monthnum')) ) 129 $redirect['query'] = remove_query_arg(array('year', 'monthnum'), $redirect['query']); 130 } elseif ( is_year() && !empty($_GET['year']) ) { 131 if ( $redirect_url = get_year_link(get_query_var('year')) ) 132 $redirect['query'] = remove_query_arg('year', $redirect['query']); 133 } elseif ( is_author() && !empty($_GET['author']) && preg_match( '|^[0-9]+$|', $_GET['author'] ) ) { 134 $author = get_userdata(get_query_var('author')); 135 if ( false !== $author && $redirect_url = get_author_posts_url($author->ID, $author->user_nicename) ) 136 $redirect['query'] = remove_query_arg('author', $redirect['query']); 137 } elseif ( is_category() || is_tag() || is_tax() ) { // Terms (Tags/categories) 138 139 $term_count = 0; 140 foreach ( array('category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in', 141 'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and') as $key ) 142 $term_count += count($wp_query->query_vars[$key]); 143 144 $obj = $wp_query->get_queried_object(); 145 146 if ( $term_count <= 1 && !empty($obj->term_id) && ( $tax_url = get_term_link((int)$obj->term_id, $obj->taxonomy) ) && !is_wp_error($tax_url) ) { 147 148 if ( is_category() ) { 149 $redirect['query'] = remove_query_arg( array( 'category_name', 'category', 'cat'), $redirect['query']); 150 } elseif ( is_tag() ) { 151 $redirect['query'] = remove_query_arg( array( 'tag', 'tag_id'), $redirect['query']); 152 } elseif ( is_tax() ) { // Custom taxonomies will have a custom query var, remove those too: 153 $tax = get_taxonomy( $obj->taxonomy ); 154 if ( false !== $tax->query_var) 155 $redirect['query'] = remove_query_arg($tax->query_var, $redirect['query']); 156 else 157 $redirect['query'] = remove_query_arg( array( 'term', 'taxonomy'), $redirect['query']); 158 } 159 160 $tax_url = parse_url($tax_url); 161 if ( ! empty($tax_url['query']) ) { // Custom taxonomies may only be accessable via ?taxonomy=..&term=.. 162 parse_str($tax_url['query'], $query_vars); 163 $redirect['query'] = add_query_arg($query_vars, $redirect['query']); 164 } else { // Taxonomy is accessable via a "pretty-URL" 165 $redirect['path'] = $tax_url['path']; 166 } 167 168 } 169 } elseif ( is_single() && strpos($wp_rewrite->permalink_structure, '%category%') !== false ) { 170 $category = get_term_by('slug', get_query_var('category_name'), 'category'); 171 $post_terms = wp_get_object_terms($wp_query->get_queried_object_id(), 'category'); 172 if ( (!$category || is_wp_error($category)) || ( !is_wp_error($post_terms) && !empty($post_terms) && !in_array($category, $post_terms) ) ) 173 $redirect_url = get_permalink($wp_query->get_queried_object_id()); 174 } 175 176 // paging and feeds 177 if ( get_query_var('paged') || is_feed() || get_query_var('cpage') ) { 178 if ( !$redirect_url ) 179 $redirect_url = $requested_url; 180 $paged_redirect = @parse_url($redirect_url); 181 while ( preg_match( '#/page/?[0-9]+?(/+)?$#', $paged_redirect['path'] ) || preg_match( '#/(comments/?)?(feed|rss|rdf|atom|rss2)(/+)?$#', $paged_redirect['path'] ) || preg_match( '#/comment-page-[0-9]+(/+)?$#', $paged_redirect['path'] ) ) { 182 // Strip off paging and feed 183 $paged_redirect['path'] = preg_replace('#/page/?[0-9]+?(/+)?$#', '/', $paged_redirect['path']); // strip off any existing paging 184 $paged_redirect['path'] = preg_replace('#/(comments/?)?(feed|rss2?|rdf|atom)(/+|$)#', '/', $paged_redirect['path']); // strip off feed endings 185 $paged_redirect['path'] = preg_replace('#/comment-page-[0-9]+?(/+)?$#', '/', $paged_redirect['path']); // strip off any existing comment paging 186 } 187 188 $addl_path = ''; 189 if ( is_feed() ) { 190 $addl_path = !empty( $addl_path ) ? trailingslashit($addl_path) : ''; 191 if ( get_query_var( 'withcomments' ) ) 192 $addl_path .= 'comments/'; 193 $addl_path .= user_trailingslashit( 'feed/' . ( ( 'rss2' == get_query_var('feed') || 'feed' == get_query_var('feed') ) ? '' : get_query_var('feed') ), 'feed' ); 194 $redirect['query'] = remove_query_arg( 'feed', $redirect['query'] ); 195 } 196 197 if ( get_query_var('paged') > 0 ) { 198 $paged = get_query_var('paged'); 199 $redirect['query'] = remove_query_arg( 'paged', $redirect['query'] ); 200 if ( !is_feed() ) { 201 if ( $paged > 1 && !is_single() ) { 202 $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit("page/$paged", 'paged'); 203 } elseif ( !is_single() ) { 204 $addl_path = !empty( $addl_path ) ? trailingslashit($addl_path) : ''; 205 } 206 } elseif ( $paged > 1 ) { 207 $redirect['query'] = add_query_arg( 'paged', $paged, $redirect['query'] ); 208 } 209 } 210 211 if ( get_option('page_comments') && ( ( 'newest' == get_option('default_comments_page') && get_query_var('cpage') > 0 ) || ( 'newest' != get_option('default_comments_page') && get_query_var('cpage') > 1 ) ) ) { 212 $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit( 'comment-page-' . get_query_var('cpage'), 'commentpaged' ); 213 $redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] ); 214 } 215 216 $paged_redirect['path'] = user_trailingslashit( preg_replace('|/index.php/?$|', '/', $paged_redirect['path']) ); // strip off trailing /index.php/ 217 if ( !empty( $addl_path ) && $wp_rewrite->using_index_permalinks() && strpos($paged_redirect['path'], '/index.php/') === false ) 218 $paged_redirect['path'] = trailingslashit($paged_redirect['path']) . 'index.php/'; 219 if ( !empty( $addl_path ) ) 220 $paged_redirect['path'] = trailingslashit($paged_redirect['path']) . $addl_path; 221 $redirect_url = $paged_redirect['scheme'] . '://' . $paged_redirect['host'] . $paged_redirect['path']; 222 $redirect['path'] = $paged_redirect['path']; 223 } 224 } 225 226 // tack on any additional query vars 227 $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] ); 228 if ( $redirect_url && !empty($redirect['query']) ) { 229 if ( strpos($redirect_url, '?') !== false ) 230 $redirect_url .= '&'; 231 else 232 $redirect_url .= '?'; 233 $redirect_url .= $redirect['query']; 234 } 235 236 if ( $redirect_url ) 237 $redirect = @parse_url($redirect_url); 238 239 // www.example.com vs example.com 240 $user_home = @parse_url(home_url()); 241 if ( !empty($user_home['host']) ) 242 $redirect['host'] = $user_home['host']; 243 if ( empty($user_home['path']) ) 244 $user_home['path'] = '/'; 245 246 // Handle ports 247 if ( !empty($user_home['port']) ) 248 $redirect['port'] = $user_home['port']; 249 else 250 unset($redirect['port']); 251 252 // trailing /index.php 253 $redirect['path'] = preg_replace('|/index.php/*?$|', '/', $redirect['path']); 254 255 // Remove trailing spaces from the path 256 $redirect['path'] = preg_replace( '#(%20| )+$#', '', $redirect['path'] ); 257 258 if ( !empty( $redirect['query'] ) ) { 259 // Remove trailing spaces from certain terminating query string args 260 $redirect['query'] = preg_replace( '#((p|page_id|cat|tag)=[^&]*?)(%20| )+$#', '$1', $redirect['query'] ); 261 262 // Clean up empty query strings 263 $redirect['query'] = trim(preg_replace( '#(^|&)(p|page_id|cat|tag)=?(&|$)#', '&', $redirect['query']), '&'); 264 265 // Remove redundant leading ampersands 266 $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] ); 267 } 268 269 // strip /index.php/ when we're not using PATHINFO permalinks 270 if ( !$wp_rewrite->using_index_permalinks() ) 271 $redirect['path'] = str_replace('/index.php/', '/', $redirect['path']); 272 273 // trailing slashes 274 if ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() && !is_404() && (!is_front_page() || ( is_front_page() && (get_query_var('paged') > 1) ) ) ) { 275 $user_ts_type = ''; 276 if ( get_query_var('paged') > 0 ) { 277 $user_ts_type = 'paged'; 278 } else { 279 foreach ( array('single', 'category', 'page', 'day', 'month', 'year', 'home') as $type ) { 280 $func = 'is_' . $type; 281 if ( call_user_func($func) ) { 282 $user_ts_type = $type; 283 break; 284 } 285 } 286 } 287 $redirect['path'] = user_trailingslashit($redirect['path'], $user_ts_type); 288 } elseif ( is_front_page() ) { 289 $redirect['path'] = trailingslashit($redirect['path']); 290 } 291 292 // Strip multiple slashes out of the URL 293 if ( strpos($redirect['path'], '//') > -1 ) 294 $redirect['path'] = preg_replace('|/+|', '/', $redirect['path']); 295 296 // Always trailing slash the Front Page URL 297 if ( trailingslashit( $redirect['path'] ) == trailingslashit( $user_home['path'] ) ) 298 $redirect['path'] = trailingslashit($redirect['path']); 299 300 // Ignore differences in host capitalization, as this can lead to infinite redirects 301 // Only redirect no-www <=> yes-www 302 if ( strtolower($original['host']) == strtolower($redirect['host']) || 303 ( strtolower($original['host']) != 'www.' . strtolower($redirect['host']) && 'www.' . strtolower($original['host']) != strtolower($redirect['host']) ) ) 304 $redirect['host'] = $original['host']; 305 306 $compare_original = array($original['host'], $original['path']); 307 308 if ( !empty( $original['port'] ) ) 309 $compare_original[] = $original['port']; 310 311 if ( !empty( $original['query'] ) ) 312 $compare_original[] = $original['query']; 313 314 $compare_redirect = array($redirect['host'], $redirect['path']); 315 316 if ( !empty( $redirect['port'] ) ) 317 $compare_redirect[] = $redirect['port']; 318 319 if ( !empty( $redirect['query'] ) ) 320 $compare_redirect[] = $redirect['query']; 321 322 if ( $compare_original !== $compare_redirect ) { 323 $redirect_url = $redirect['scheme'] . '://' . $redirect['host']; 324 if ( !empty($redirect['port']) ) 325 $redirect_url .= ':' . $redirect['port']; 326 $redirect_url .= $redirect['path']; 327 if ( !empty($redirect['query']) ) 328 $redirect_url .= '?' . $redirect['query']; 329 } 330 331 if ( !$redirect_url || $redirect_url == $requested_url ) 332 return false; 333 334 // Note that you can use the "redirect_canonical" filter to cancel a canonical redirect for whatever reason by returning FALSE 335 $redirect_url = apply_filters('redirect_canonical', $redirect_url, $requested_url); 336 337 if ( !$redirect_url || $redirect_url == $requested_url ) // yes, again -- in case the filter aborted the request 338 return false; 339 340 if ( $do_redirect ) { 341 // protect against chained redirects 342 if ( !redirect_canonical($redirect_url, false) ) { 343 wp_redirect($redirect_url, 301); 344 exit(); 345 } else { 346 // Debug 347 // die("1: $redirect_url<br />2: " . redirect_canonical( $redirect_url, false ) ); 348 return false; 349 } 350 } else { 351 return $redirect_url; 352 } 353 } 354 355 /** 356 * Attempts to guess correct post based on query vars. 357 * 358 * @since 2.3.0 359 * @uses $wpdb 360 * 361 * @return bool|string Returns False, if it can't find post, returns correct 362 * location on success. 363 */ 364 function redirect_guess_404_permalink() { 365 global $wpdb; 366 367 if ( !get_query_var('name') ) 368 return false; 369 370 $where = $wpdb->prepare("post_name LIKE %s", get_query_var('name') . '%'); 371 372 // if any of post_type, year, monthnum, or day are set, use them to refine the query 373 if ( get_query_var('post_type') ) 374 $where .= $wpdb->prepare(" AND post_type = %s", get_query_var('post_type')); 375 if ( get_query_var('year') ) 376 $where .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var('year')); 377 if ( get_query_var('monthnum') ) 378 $where .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var('monthnum')); 379 if ( get_query_var('day') ) 380 $where .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var('day')); 381 382 $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'"); 383 if ( !$post_id ) 384 return false; 385 return get_permalink($post_id); 386 } 387 388 add_action('template_redirect', 'redirect_canonical'); 389 390 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Apr 5 14:26:09 2010 | Cross-referenced by PHPXref 0.7 |