]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/canonical.php
Wordpress 2.3.3
[autoinstalls/wordpress.git] / wp-includes / canonical.php
1 <?php
2 // Based on "Permalink Redirect" from Scott Yang and "Enforce www. Preference" by Mark Jaquith
3
4 function redirect_canonical($requested_url=NULL, $do_redirect=true) {
5         global $wp_rewrite, $posts, $is_IIS;
6
7         if ( is_feed() || is_trackback() || is_search() || is_comments_popup() || is_admin() || $is_IIS || ( isset($_POST) && count($_POST) ) || is_preview() )
8                 return;
9
10         if ( !$requested_url ) {
11                 // build the URL in the address bar
12                 $requested_url  = ( isset($_SERVER['HTTPS'] ) && strtolower($_SERVER['HTTPS']) == 'on' ) ? 'https://' : 'http://';
13                 $requested_url .= $_SERVER['HTTP_HOST'];
14                 $requested_url .= $_SERVER['REQUEST_URI'];
15         }
16
17         $original = @parse_url($requested_url);
18         if ( false === $original )
19                 return;
20
21         // Some PHP setups turn requests for / into /index.php in REQUEST_URI
22         $original['path'] = preg_replace('|/index\.php$|', '/', $original['path']);
23
24         $redirect = $original;
25         $redirect_url = false;
26
27         // These tests give us a WP-generated permalink
28         if ( is_404() ) {
29                 $redirect_url = redirect_guess_404_permalink();
30         } elseif ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) {
31                 // rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101
32                 if ( is_single() && isset($_GET['p']) ) {
33                         if ( $redirect_url = get_permalink(get_query_var('p')) )
34                                 $redirect['query'] = remove_query_arg('p', $redirect['query']);
35                 } elseif ( is_page() && isset($_GET['page_id']) ) {
36                         if ( $redirect_url = get_permalink(get_query_var('page_id')) )
37                                 $redirect['query'] = remove_query_arg('page_id', $redirect['query']);
38                 } elseif ( isset($_GET['m']) && ( is_year() || is_month() || is_day() ) ) {
39                         $m = get_query_var('m');
40                         switch ( strlen($m) ) {
41                                 case 4: // Yearly
42                                         $redirect_url = get_year_link($m);
43                                         break;
44                                 case 6: // Monthly
45                                         $redirect_url = get_month_link( substr($m, 0, 4), substr($m, 4, 2) );
46                                         break;
47                                 case 8: // Daily
48                                         $redirect_url = get_day_link(substr($m, 0, 4), substr($m, 4, 2), substr($m, 6, 2));
49                                         break;
50                         }
51                         if ( $redirect_url )
52                                 $redirect['query'] = remove_query_arg('m', $redirect['query']);
53                 // now moving on to non ?m=X year/month/day links
54                 } elseif ( is_day() && get_query_var('year') && get_query_var('monthnum') && isset($_GET['day']) ) {
55                         if ( $redirect_url = get_day_link(get_query_var('year'), get_query_var('monthnum'), get_query_var('day')) )
56                                 $redirect['query'] = remove_query_arg(array('year', 'monthnum', 'day'), $redirect['query']);
57                 } elseif ( is_month() && get_query_var('year') && isset($_GET['monthnum']) ) {
58                         if ( $redirect_url = get_month_link(get_query_var('year'), get_query_var('monthnum')) )
59                                 $redirect['query'] = remove_query_arg(array('year', 'monthnum'), $redirect['query']);
60                 } elseif ( is_year() && isset($_GET['year']) ) {
61                         if ( $redirect_url = get_year_link(get_query_var('year')) )
62                                 $redirect['query'] = remove_query_arg('year', $redirect['query']);
63                 } elseif ( is_category() && isset($_GET['cat']) ) {
64                         if ( $redirect_url = get_category_link(get_query_var('cat')) )
65                                 $redirect['query'] = remove_query_arg('cat', $redirect['query']);
66                 } elseif ( is_author() && isset($_GET['author']) ) {
67                         $author = get_userdata(get_query_var('author'));
68                         if ( false !== $author && $redirect_url = get_author_link(false, $author->ID, $author->user_nicename) )
69                                 $redirect['query'] = remove_query_arg('author', $redirect['author']);
70                 }
71
72         // paging
73                 if ( $paged = get_query_var('paged') ) {
74                         if ( $paged > 0 ) {
75                                 if ( !$redirect_url )
76                                         $redirect_url = $requested_url;
77                                 $paged_redirect = @parse_url($redirect_url);
78                                 $paged_redirect['path'] = preg_replace('|/page/[0-9]+?(/+)?$|', '/', $paged_redirect['path']); // strip off any existing paging
79                                 $paged_redirect['path'] = preg_replace('|/index.php/?$|', '/', $paged_redirect['path']); // strip off trailing /index.php/
80                                 if ( $paged > 1 && !is_single() ) {
81                                         $paged_redirect['path'] = trailingslashit($paged_redirect['path']);
82                                         if ( $wp_rewrite->using_index_permalinks() && strpos($paged_redirect['path'], '/index.php/') === false )
83                                                 $paged_redirect['path'] .= 'index.php/';
84                                         $paged_redirect['path'] .= user_trailingslashit("page/$paged", 'paged');
85                                 } elseif ( !is_home() && !is_single() ){
86                                         $paged_redirect['path'] = user_trailingslashit($paged_redirect['path'], 'paged');
87                                 }
88                                 $redirect_url = $paged_redirect['scheme'] . '://' . $paged_redirect['host'] . $paged_redirect['path'];
89                                 $redirect['path'] = $paged_redirect['path'];
90                         }
91                         $redirect['query'] = remove_query_arg('paged', $redirect['query']);
92                 }
93         }
94
95         // tack on any additional query vars
96         if ( $redirect_url && $redirect['query'] ) {
97                 if ( strpos($redirect_url, '?') !== false )
98                         $redirect_url .= '&';
99                 else
100                         $redirect_url .= '?';
101                 $redirect_url .= $redirect['query'];
102         }
103
104         if ( $redirect_url )
105                 $redirect = @parse_url($redirect_url);
106
107         // www.example.com vs example.com
108         $user_home = @parse_url(get_option('home'));
109         $redirect['host'] = $user_home['host'];
110
111         // Handle ports
112         if ( isset($user_home['port']) )
113                 $redirect['port'] = $user_home['port'];
114         else
115                 unset($redirect['port']);
116
117         // trailing /index.php/
118         $redirect['path'] = preg_replace('|/index.php/$|', '/', $redirect['path']);
119
120         // strip /index.php/ when we're not using PATHINFO permalinks
121         if ( !$wp_rewrite->using_index_permalinks() )
122                 $redirect['path'] = str_replace('/index.php/', '/', $redirect['path']);
123
124         // trailing slashes
125         if ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() && !is_404() && (!is_home() || ( is_home() && (get_query_var('paged') > 1) ) ) ) {
126                 $user_ts_type = '';
127                 if ( get_query_var('paged') > 0 ) {
128                         $user_ts_type = 'paged';
129                 } else {
130                         foreach ( array('single', 'category', 'page', 'day', 'month', 'year') as $type ) {
131                                 $func = 'is_' . $type;
132                                 if ( call_user_func($func) )
133                                         $user_ts_type = $type;
134                                         break;
135                                 }
136                         }
137                 $redirect['path'] = user_trailingslashit($redirect['path'], $user_ts_type);
138         } elseif ( is_home() ) {
139                 $redirect['path'] = trailingslashit($redirect['path']);
140         }
141
142         // Always trailing slash the 'home' URL
143         if ( $redirect['path'] == $user_home['path'] )
144                 $redirect['path'] = trailingslashit($redirect['path']);
145
146         // Ignore differences in host capitalization, as this can lead to infinite redirects
147         if ( strtolower($original['host']) == strtolower($redirect['host']) )
148                 $redirect['host'] = $original['host'];
149
150         if ( array($original['host'], $original['port'], $original['path'], $original['query']) !== array($redirect['host'], $redirect['port'], $redirect['path'], $redirect['query']) ) {
151                 $redirect_url = $redirect['scheme'] . '://' . $redirect['host'];
152                 if ( isset($redirect['port']) )
153                         $redirect_url .= ':' . $redirect['port'];
154                 $redirect_url .= $redirect['path'];
155                 if ( $redirect['query'] )
156                         $redirect_url .= '?' . $redirect['query'];
157         }
158
159         if ( $redirect_url && $redirect_url != $requested_url ) {
160                 // var_dump($redirect_url); die();
161                 $redirect_url = apply_filters('redirect_canonical', $redirect_url, $requested_url);
162                 if ( $do_redirect) {
163                         // protect against chained redirects
164                         if ( !redirect_canonical($redirect_url, false) ) {
165                                 wp_redirect($redirect_url, 301);
166                                 exit();
167                         } else {
168                                 return false;
169                         }
170                 } else {
171                         return $redirect_url;
172                 }
173         } else {
174                 return false;
175         }
176 }
177
178 function redirect_guess_404_permalink() {
179         global $wp_query, $wpdb;
180         if ( !get_query_var('name') )
181                 return false;
182
183         $where = "post_name LIKE '" . $wpdb->escape(get_query_var('name')) . "%'";
184
185         // if any of year, monthnum, or day are set, use them to refine the query
186         if ( get_query_var('year') )
187                 $where .= " AND YEAR(post_date) = '" . $wpdb->escape(get_query_var('year')) . "'";
188         if ( get_query_var('monthnum') )
189                 $where .= " AND MONTH(post_date) = '" . $wpdb->escape(get_query_var('monthnum')) . "'";
190         if ( get_query_var('day') )
191                 $where .= " AND DAYOFMONTH(post_date) = '" . $wpdb->escape(get_query_var('day')) . "'";
192
193         $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'");
194         if ( !$post_id )
195                 return false;
196         return get_permalink($post_id);
197 }
198
199 add_action('template_redirect', 'redirect_canonical');
200
201 ?>