root/trunk/wp-includes/canonical.php

Revision 7990, 9.5 kB (checked in by ryan, 1 month ago)

phpdoc updates from jacobsantos. see #7038

  • Property svn:eol-style set to native
Line 
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  * @author Scott Yang
9  * @author Mark Jaquith
10  * @package WordPress
11  * @since 2.3
12  */
13
14 /**
15  * Redirects incoming links to the proper URL based on the site url
16  *
17  * Search engines consider www.somedomain.com and somedomain.com to be two
18  * different URLs when they both go to the same location. This SEO enhancement
19  * prevents penality for duplicate content by redirecting all incoming links to
20  * one or the other.
21  *
22  * Prevents redirection for feeds, trackbacks, searches, comment popup, and
23  * admin URLs. Does not redirect on IIS, page/post previews, and on form data.
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
30  * @uses $wp_rewrite
31  * @uses $is_IIS
32  *
33  * @param string $requested_url Optional. The URL that was requested, used to
34  *        figure if redirect is needed.
35  * @param bool $do_redirect Optional. Redirect to the new URL.
36  * @return null|false|string Null, if redirect not needed. False, if redirect
37  *        not needed or the string of the URL
38  */
39 function redirect_canonical($requested_url=null, $do_redirect=true) {
40     global $wp_rewrite, $is_IIS;
41
42     if ( is_feed() || is_trackback() || is_search() || is_comments_popup() || is_admin() || $is_IIS || ( isset($_POST) && count($_POST) ) || is_preview() || is_robots() )
43         return;
44
45     if ( !$requested_url ) {
46         // build the URL in the address bar
47         $requested_url  = ( isset($_SERVER['HTTPS'] ) && strtolower($_SERVER['HTTPS']) == 'on' ) ? 'https://' : 'http://';
48         $requested_url .= $_SERVER['HTTP_HOST'];
49         $requested_url .= $_SERVER['REQUEST_URI'];
50     }
51
52     $original = @parse_url($requested_url);
53     if ( false === $original )
54         return;
55
56     // Some PHP setups turn requests for / into /index.php in REQUEST_URI
57     $original['path'] = preg_replace('|/index\.php$|', '/', $original['path']);
58
59     $redirect = $original;
60     $redirect_url = false;
61
62     // These tests give us a WP-generated permalink
63     if ( is_404() ) {
64         $redirect_url = redirect_guess_404_permalink();
65     } elseif ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) {
66         // rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101
67         if ( is_single() && isset($_GET['p']) ) {
68             if ( $redirect_url = get_permalink(get_query_var('p')) )
69                 $redirect['query'] = remove_query_arg('p', $redirect['query']);
70         } elseif ( is_page() && isset($_GET['page_id']) ) {
71             if ( $redirect_url = get_permalink(get_query_var('page_id')) )
72                 $redirect['query'] = remove_query_arg('page_id', $redirect['query']);
73         } elseif ( isset($_GET['m']) && ( is_year() || is_month() || is_day() ) ) {
74             $m = get_query_var('m');
75             switch ( strlen($m) ) {
76                 case 4: // Yearly
77                     $redirect_url = get_year_link($m);
78                     break;
79                 case 6: // Monthly
80                     $redirect_url = get_month_link( substr($m, 0, 4), substr($m, 4, 2) );
81                     break;
82                 case 8: // Daily
83                     $redirect_url = get_day_link(substr($m, 0, 4), substr($m, 4, 2), substr($m, 6, 2));
84                     break;
85             }
86             if ( $redirect_url )
87                 $redirect['query'] = remove_query_arg('m', $redirect['query']);
88         // now moving on to non ?m=X year/month/day links
89         } elseif ( is_day() && get_query_var('year') && get_query_var('monthnum') && isset($_GET['day']) ) {
90             if ( $redirect_url = get_day_link(get_query_var('year'), get_query_var('monthnum'), get_query_var('day')) )
91                 $redirect['query'] = remove_query_arg(array('year', 'monthnum', 'day'), $redirect['query']);
92         } elseif ( is_month() && get_query_var('year') && isset($_GET['monthnum']) ) {
93             if ( $redirect_url = get_month_link(get_query_var('year'), get_query_var('monthnum')) )
94                 $redirect['query'] = remove_query_arg(array('year', 'monthnum'), $redirect['query']);
95         } elseif ( is_year() && isset($_GET['year']) ) {
96             if ( $redirect_url = get_year_link(get_query_var('year')) )
97                 $redirect['query'] = remove_query_arg('year', $redirect['query']);
98         } elseif ( is_category() && isset($_GET['cat']) ) {
99             if ( $redirect_url = get_category_link(get_query_var('cat')) )
100                 $redirect['query'] = remove_query_arg('cat', $redirect['query']);
101         } elseif ( is_author() && isset($_GET['author']) ) {
102             $author = get_userdata(get_query_var('author'));
103             if ( false !== $author && $redirect_url = get_author_link(false, $author->ID, $author->user_nicename) )
104                 $redirect['query'] = remove_query_arg('author', $redirect['author']);
105         }
106
107     // paging
108         if ( $paged = get_query_var('paged') ) {
109             if ( $paged > 0 ) {
110                 if ( !$redirect_url )
111                     $redirect_url = $requested_url;
112                 $paged_redirect = @parse_url($redirect_url);
113                 $paged_redirect['path'] = preg_replace('|/page/[0-9]+?(/+)?$|', '/', $paged_redirect['path']); // strip off any existing paging
114                 $paged_redirect['path'] = preg_replace('|/index.php/?$|', '/', $paged_redirect['path']); // strip off trailing /index.php/
115                 if ( $paged > 1 && !is_single() ) {
116                     $paged_redirect['path'] = trailingslashit($paged_redirect['path']);
117                     if ( $wp_rewrite->using_index_permalinks() && strpos($paged_redirect['path'], '/index.php/') === false )
118                         $paged_redirect['path'] .= 'index.php/';
119                     $paged_redirect['path'] .= user_trailingslashit("page/$paged", 'paged');
120                 } elseif ( !is_home() && !is_single() ){
121                     $paged_redirect['path'] = user_trailingslashit($paged_redirect['path'], 'paged');
122                 }
123                 $redirect_url = $paged_redirect['scheme'] . '://' . $paged_redirect['host'] . $paged_redirect['path'];
124                 $redirect['path'] = $paged_redirect['path'];
125             }
126             $redirect['query'] = remove_query_arg('paged', $redirect['query']);
127         }
128     }
129
130     // tack on any additional query vars
131     if ( $redirect_url && $redirect['query'] ) {
132         if ( strpos($redirect_url, '?') !== false )
133             $redirect_url .= '&';
134         else
135             $redirect_url .= '?';
136         $redirect_url .= $redirect['query'];
137     }
138
139     if ( $redirect_url )
140         $redirect = @parse_url($redirect_url);
141
142     // www.example.com vs example.com
143     $user_home = @parse_url(get_option('home'));
144     if ( isset($user_home['host']) )
145         $redirect['host'] = $user_home['host'];
146
147     // Handle ports
148     if ( isset($user_home['port']) )
149         $redirect['port'] = $user_home['port'];
150     else
151         unset($redirect['port']);
152
153     // trailing /index.php/
154     $redirect['path'] = preg_replace('|/index.php/$|', '/', $redirect['path']);
155
156     // strip /index.php/ when we're not using PATHINFO permalinks
157     if ( !$wp_rewrite->using_index_permalinks() )
158         $redirect['path'] = str_replace('/index.php/', '/', $redirect['path']);
159
160     // trailing slashes
161     if ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() && !is_404() && (!is_home() || ( is_home() && (get_query_var('paged') > 1) ) ) ) {
162         $user_ts_type = '';
163         if ( get_query_var('paged') > 0 ) {
164             $user_ts_type = 'paged';
165         } else {
166             foreach ( array('single', 'category', 'page', 'day', 'month', 'year') as $type ) {
167                 $func = 'is_' . $type;
168                 if ( call_user_func($func) )
169                     $user_ts_type = $type;
170                     break;
171                 }
172             }
173         $redirect['path'] = user_trailingslashit($redirect['path'], $user_ts_type);
174     } elseif ( is_home() ) {
175         $redirect['path'] = trailingslashit($redirect['path']);
176     }
177
178     // Always trailing slash the 'home' URL
179     if ( $redirect['path'] == $user_home['path'] )
180         $redirect['path'] = trailingslashit($redirect['path']);
181
182     // Ignore differences in host capitalization, as this can lead to infinite redirects
183     if ( strtolower($original['host']) == strtolower($redirect['host']) )
184         $redirect['host'] = $original['host'];
185
186     if ( array($original['host'], $original['port'], $original['path'], $original['query']) !== array($redirect['host'], $redirect['port'], $redirect['path'], $redirect['query']) ) {
187         $redirect_url = $redirect['scheme'] . '://' . $redirect['host'];
188         if ( isset($redirect['port']) )
189              $redirect_url .= ':' . $redirect['port'];
190         $redirect_url .= $redirect['path'];
191         if ( $redirect['query'] )
192             $redirect_url .= '?' . $redirect['query'];
193     }
194
195     if ( !$redirect_url || $redirect_url == $requested_url )
196          return false;
197
198     // Note that you can use the "redirect_canonical" filter to cancel a canonical redirect for whatever reason by returning FALSE
199     $redirect_url = apply_filters('redirect_canonical', $redirect_url, $requested_url);
200
201     if ( !$redirect_url || $redirect_url == $requested_url ) // yes, again -- in case the filter aborted the request
202          return false;
203
204     if ( $do_redirect ) {
205         // protect against chained redirects
206         if ( !redirect_canonical($redirect_url, false) ) {
207             wp_redirect($redirect_url, 301);
208             exit();
209         } else {
210             return false;
211         }
212     } else {
213         return $redirect_url;
214     }
215 }
216
217 /**
218  * Attempts to guess correct post based on query vars
219  *
220  * @since 2.3
221  * @uses $wpdb
222  *
223  * @return bool|string Returns False, if it can't find post, returns correct
224  *        location on success.
225  */
226 function redirect_guess_404_permalink() {
227     global $wpdb;
228     if ( !get_query_var('name') )
229         return false;
230
231     $where = $wpdb->prepare("post_name LIKE %s", get_query_var('name') . '%');
232
233     // if any of year, monthnum, or day are set, use them to refine the query
234     if ( get_query_var('year') )
235         $where .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var('year'));
236     if ( get_query_var('monthnum') )
237         $where .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var('monthnum'));
238     if ( get_query_var('day') )
239         $where .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var('day'));
240
241     $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'");
242     if ( !$post_id )
243         return false;
244     return get_permalink($post_id);
245 }
246
247 add_action('template_redirect', 'redirect_canonical');
248
249 ?>
250
Note: See TracBrowser for help on using the browser.