17 * prevents penalty for duplicate content by redirecting all incoming links to |
17 * prevents penalty for duplicate content by redirecting all incoming links to |
18 * one or the other. |
18 * one or the other. |
19 * |
19 * |
20 * Prevents redirection for feeds, trackbacks, searches, and |
20 * Prevents redirection for feeds, trackbacks, searches, and |
21 * admin URLs. Does not redirect on non-pretty-permalink-supporting IIS 7+, |
21 * admin URLs. Does not redirect on non-pretty-permalink-supporting IIS 7+, |
22 * page/post previews, WP admin, Trackbacks, robots.txt, searches, or on POST |
22 * page/post previews, WP admin, Trackbacks, robots.txt, favicon.ico, searches, |
23 * requests. |
23 * or on POST requests. |
24 * |
24 * |
25 * Will also attempt to find the correct link when a user enters a URL that does |
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 |
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. |
27 * or query in an attempt to figure the correct page to go to. |
28 * |
28 * |
29 * @since 2.3.0 |
29 * @since 2.3.0 |
30 * |
30 * |
31 * @global WP_Rewrite $wp_rewrite |
31 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. |
32 * @global bool $is_IIS |
32 * @global bool $is_IIS |
33 * @global WP_Query $wp_query |
33 * @global WP_Query $wp_query WordPress Query object. |
34 * @global wpdb $wpdb WordPress database abstraction object. |
34 * @global wpdb $wpdb WordPress database abstraction object. |
35 * @global WP $wp Current WordPress environment instance. |
35 * @global WP $wp Current WordPress environment instance. |
36 * |
36 * |
37 * @param string $requested_url Optional. The URL that was requested, used to |
37 * @param string $requested_url Optional. The URL that was requested, used to |
38 * figure if redirect is needed. |
38 * figure if redirect is needed. |
39 * @param bool $do_redirect Optional. Redirect to the new URL. |
39 * @param bool $do_redirect Optional. Redirect to the new URL. |
40 * @return string|void The string of the URL, if redirect needed. |
40 * @return string|void The string of the URL, if redirect needed. |
41 */ |
41 */ |
42 function redirect_canonical( $requested_url = null, $do_redirect = true ) { |
42 function redirect_canonical( $requested_url = null, $do_redirect = true ) { |
43 global $wp_rewrite, $is_IIS, $wp_query, $wpdb, $wp; |
43 global $wp_rewrite, $is_IIS, $wp_query, $wpdb, $wp; |
44 |
44 |
45 if ( isset( $_SERVER['REQUEST_METHOD'] ) && ! in_array( strtoupper( $_SERVER['REQUEST_METHOD'] ), array( 'GET', 'HEAD' ) ) ) { |
45 if ( isset( $_SERVER['REQUEST_METHOD'] ) && ! in_array( strtoupper( $_SERVER['REQUEST_METHOD'] ), array( 'GET', 'HEAD' ), true ) ) { |
46 return; |
46 return; |
47 } |
47 } |
48 |
48 |
49 // If we're not in wp-admin and the post has been published and preview nonce |
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 if ( is_preview() && get_query_var( 'p' ) && 'publish' == get_post_status( get_query_var( 'p' ) ) ) { |
51 if ( is_preview() && get_query_var( 'p' ) && 'publish' === get_post_status( get_query_var( 'p' ) ) ) { |
52 if ( ! isset( $_GET['preview_id'] ) |
52 if ( ! isset( $_GET['preview_id'] ) |
53 || ! isset( $_GET['preview_nonce'] ) |
53 || ! isset( $_GET['preview_nonce'] ) |
54 || ! wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . (int) $_GET['preview_id'] ) ) { |
54 || ! wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . (int) $_GET['preview_id'] ) |
|
55 ) { |
55 $wp_query->is_preview = false; |
56 $wp_query->is_preview = false; |
56 } |
57 } |
57 } |
58 } |
58 |
59 |
59 if ( is_trackback() || is_search() || is_admin() || is_preview() || is_robots() || ( $is_IIS && ! iis7_supports_permalinks() ) ) { |
60 if ( is_admin() || is_search() || is_preview() || is_trackback() || is_favicon() |
|
61 || ( $is_IIS && ! iis7_supports_permalinks() ) |
|
62 ) { |
60 return; |
63 return; |
61 } |
64 } |
62 |
65 |
63 if ( ! $requested_url && isset( $_SERVER['HTTP_HOST'] ) ) { |
66 if ( ! $requested_url && isset( $_SERVER['HTTP_HOST'] ) ) { |
64 // build the URL in the address bar |
67 // Build the URL in the address bar. |
65 $requested_url = is_ssl() ? 'https://' : 'http://'; |
68 $requested_url = is_ssl() ? 'https://' : 'http://'; |
66 $requested_url .= $_SERVER['HTTP_HOST']; |
69 $requested_url .= $_SERVER['HTTP_HOST']; |
67 $requested_url .= $_SERVER['REQUEST_URI']; |
70 $requested_url .= $_SERVER['REQUEST_URI']; |
68 } |
71 } |
69 |
72 |
70 $original = @parse_url( $requested_url ); |
73 $original = parse_url( $requested_url ); |
71 if ( false === $original ) { |
74 if ( false === $original ) { |
72 return; |
75 return; |
73 } |
76 } |
74 |
77 |
75 $redirect = $original; |
78 $redirect = $original; |
76 $redirect_url = false; |
79 $redirect_url = false; |
77 |
80 |
78 // Notice fixing |
81 // Notice fixing. |
79 if ( ! isset( $redirect['path'] ) ) { |
82 if ( ! isset( $redirect['path'] ) ) { |
80 $redirect['path'] = ''; |
83 $redirect['path'] = ''; |
81 } |
84 } |
82 if ( ! isset( $redirect['query'] ) ) { |
85 if ( ! isset( $redirect['query'] ) ) { |
83 $redirect['query'] = ''; |
86 $redirect['query'] = ''; |
84 } |
87 } |
85 |
88 |
86 // If the original URL ended with non-breaking spaces, they were almost |
89 /* |
87 // certainly inserted by accident. Let's remove them, so the reader doesn't |
90 * If the original URL ended with non-breaking spaces, they were almost |
88 // see a 404 error with no obvious cause. |
91 * certainly inserted by accident. Let's remove them, so the reader doesn't |
|
92 * see a 404 error with no obvious cause. |
|
93 */ |
89 $redirect['path'] = preg_replace( '|(%C2%A0)+$|i', '', $redirect['path'] ); |
94 $redirect['path'] = preg_replace( '|(%C2%A0)+$|i', '', $redirect['path'] ); |
90 |
95 |
91 // It's not a preview, so remove it from URL |
96 // It's not a preview, so remove it from URL. |
92 if ( get_query_var( 'preview' ) ) { |
97 if ( get_query_var( 'preview' ) ) { |
93 $redirect['query'] = remove_query_arg( 'preview', $redirect['query'] ); |
98 $redirect['query'] = remove_query_arg( 'preview', $redirect['query'] ); |
94 } |
99 } |
95 |
100 |
96 if ( is_feed() && ( $id = get_query_var( 'p' ) ) ) { |
101 $post_id = get_query_var( 'p' ); |
97 if ( $redirect_url = get_post_comments_feed_link( $id, get_query_var( 'feed' ) ) ) { |
102 |
98 $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type', 'feed' ), $redirect_url ); |
103 if ( is_feed() && $post_id ) { |
99 $redirect['path'] = parse_url( $redirect_url, PHP_URL_PATH ); |
104 $redirect_url = get_post_comments_feed_link( $post_id, get_query_var( 'feed' ) ); |
100 } |
105 |
101 } |
106 if ( $redirect_url ) { |
102 |
107 $redirect['query'] = _remove_qs_args_if_not_in_url( |
103 if ( is_singular() && 1 > $wp_query->post_count && ( $id = get_query_var( 'p' ) ) ) { |
108 $redirect['query'], |
104 |
109 array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type', 'feed' ), |
105 $vars = $wpdb->get_results( $wpdb->prepare( "SELECT post_type, post_parent FROM $wpdb->posts WHERE ID = %d", $id ) ); |
110 $redirect_url |
106 |
111 ); |
107 if ( isset( $vars[0] ) && $vars = $vars[0] ) { |
112 |
108 if ( 'revision' == $vars->post_type && $vars->post_parent > 0 ) { |
113 $redirect['path'] = parse_url( $redirect_url, PHP_URL_PATH ); |
109 $id = $vars->post_parent; |
114 } |
110 } |
115 } |
111 |
116 |
112 if ( $redirect_url = get_permalink( $id ) ) { |
117 if ( is_singular() && $wp_query->post_count < 1 && $post_id ) { |
113 $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url ); |
118 |
114 } |
119 $vars = $wpdb->get_results( $wpdb->prepare( "SELECT post_type, post_parent FROM $wpdb->posts WHERE ID = %d", $post_id ) ); |
115 } |
120 |
116 } |
121 if ( ! empty( $vars[0] ) ) { |
117 |
122 $vars = $vars[0]; |
118 // These tests give us a WP-generated permalink |
123 |
|
124 if ( 'revision' === $vars->post_type && $vars->post_parent > 0 ) { |
|
125 $post_id = $vars->post_parent; |
|
126 } |
|
127 |
|
128 $redirect_url = get_permalink( $post_id ); |
|
129 |
|
130 if ( $redirect_url ) { |
|
131 $redirect['query'] = _remove_qs_args_if_not_in_url( |
|
132 $redirect['query'], |
|
133 array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), |
|
134 $redirect_url |
|
135 ); |
|
136 } |
|
137 } |
|
138 } |
|
139 |
|
140 // These tests give us a WP-generated permalink. |
119 if ( is_404() ) { |
141 if ( is_404() ) { |
120 |
142 |
121 // Redirect ?page_id, ?p=, ?attachment_id= to their respective url's |
143 // Redirect ?page_id, ?p=, ?attachment_id= to their respective URLs. |
122 $id = max( get_query_var( 'p' ), get_query_var( 'page_id' ), get_query_var( 'attachment_id' ) ); |
144 $post_id = max( get_query_var( 'p' ), get_query_var( 'page_id' ), get_query_var( 'attachment_id' ) ); |
123 if ( $id && $redirect_post = get_post( $id ) ) { |
145 |
|
146 $redirect_post = $post_id ? get_post( $post_id ) : false; |
|
147 |
|
148 if ( $redirect_post ) { |
124 $post_type_obj = get_post_type_object( $redirect_post->post_type ); |
149 $post_type_obj = get_post_type_object( $redirect_post->post_type ); |
125 if ( $post_type_obj->public && 'auto-draft' != $redirect_post->post_status ) { |
150 |
126 $redirect_url = get_permalink( $redirect_post ); |
151 if ( $post_type_obj->public && 'auto-draft' !== $redirect_post->post_status ) { |
127 $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url ); |
152 $redirect_url = get_permalink( $redirect_post ); |
128 } |
153 |
129 } |
154 $redirect['query'] = _remove_qs_args_if_not_in_url( |
130 |
155 $redirect['query'], |
131 if ( get_query_var( 'day' ) && get_query_var( 'monthnum' ) && get_query_var( 'year' ) ) { |
156 array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), |
|
157 $redirect_url |
|
158 ); |
|
159 } |
|
160 } |
|
161 |
|
162 $year = get_query_var( 'year' ); |
|
163 $month = get_query_var( 'monthnum' ); |
|
164 $day = get_query_var( 'day' ); |
|
165 |
|
166 if ( $year && $month && $day ) { |
|
167 $date = sprintf( '%04d-%02d-%02d', $year, $month, $day ); |
|
168 |
|
169 if ( ! wp_checkdate( $month, $day, $year, $date ) ) { |
|
170 $redirect_url = get_month_link( $year, $month ); |
|
171 |
|
172 $redirect['query'] = _remove_qs_args_if_not_in_url( |
|
173 $redirect['query'], |
|
174 array( 'year', 'monthnum', 'day' ), |
|
175 $redirect_url |
|
176 ); |
|
177 } |
|
178 } elseif ( $year && $month && $month > 12 ) { |
|
179 $redirect_url = get_year_link( $year ); |
|
180 |
|
181 $redirect['query'] = _remove_qs_args_if_not_in_url( |
|
182 $redirect['query'], |
|
183 array( 'year', 'monthnum' ), |
|
184 $redirect_url |
|
185 ); |
|
186 } |
|
187 |
|
188 // Strip off non-existing <!--nextpage--> links from single posts or pages. |
|
189 if ( get_query_var( 'page' ) ) { |
|
190 $post_id = 0; |
|
191 |
|
192 if ( $wp_query->queried_object instanceof WP_Post ) { |
|
193 $post_id = $wp_query->queried_object->ID; |
|
194 } elseif ( $wp_query->post ) { |
|
195 $post_id = $wp_query->post->ID; |
|
196 } |
|
197 |
|
198 if ( $post_id ) { |
|
199 $redirect_url = get_permalink( $post_id ); |
|
200 |
|
201 $redirect['path'] = rtrim( $redirect['path'], (int) get_query_var( 'page' ) . '/' ); |
|
202 $redirect['query'] = remove_query_arg( 'page', $redirect['query'] ); |
|
203 } |
|
204 } |
|
205 |
|
206 if ( ! $redirect_url ) { |
|
207 $redirect_url = redirect_guess_404_permalink(); |
|
208 |
|
209 if ( $redirect_url ) { |
|
210 $redirect['query'] = _remove_qs_args_if_not_in_url( |
|
211 $redirect['query'], |
|
212 array( 'page', 'feed', 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), |
|
213 $redirect_url |
|
214 ); |
|
215 } |
|
216 } |
|
217 } elseif ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) { |
|
218 |
|
219 // Rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101. |
|
220 if ( is_attachment() |
|
221 && ! array_diff( array_keys( $wp->query_vars ), array( 'attachment', 'attachment_id' ) ) |
|
222 && ! $redirect_url |
|
223 ) { |
|
224 if ( ! empty( $_GET['attachment_id'] ) ) { |
|
225 $redirect_url = get_attachment_link( get_query_var( 'attachment_id' ) ); |
|
226 |
|
227 if ( $redirect_url ) { |
|
228 $redirect['query'] = remove_query_arg( 'attachment_id', $redirect['query'] ); |
|
229 } |
|
230 } else { |
|
231 $redirect_url = get_attachment_link(); |
|
232 } |
|
233 } elseif ( is_single() && ! empty( $_GET['p'] ) && ! $redirect_url ) { |
|
234 $redirect_url = get_permalink( get_query_var( 'p' ) ); |
|
235 |
|
236 if ( $redirect_url ) { |
|
237 $redirect['query'] = remove_query_arg( array( 'p', 'post_type' ), $redirect['query'] ); |
|
238 } |
|
239 } elseif ( is_single() && ! empty( $_GET['name'] ) && ! $redirect_url ) { |
|
240 $redirect_url = get_permalink( $wp_query->get_queried_object_id() ); |
|
241 |
|
242 if ( $redirect_url ) { |
|
243 $redirect['query'] = remove_query_arg( 'name', $redirect['query'] ); |
|
244 } |
|
245 } elseif ( is_page() && ! empty( $_GET['page_id'] ) && ! $redirect_url ) { |
|
246 $redirect_url = get_permalink( get_query_var( 'page_id' ) ); |
|
247 |
|
248 if ( $redirect_url ) { |
|
249 $redirect['query'] = remove_query_arg( 'page_id', $redirect['query'] ); |
|
250 } |
|
251 } elseif ( is_page() && ! is_feed() && ! $redirect_url |
|
252 && 'page' === get_option( 'show_on_front' ) && get_queried_object_id() === (int) get_option( 'page_on_front' ) |
|
253 ) { |
|
254 $redirect_url = home_url( '/' ); |
|
255 } elseif ( is_home() && ! empty( $_GET['page_id'] ) && ! $redirect_url |
|
256 && 'page' === get_option( 'show_on_front' ) && get_query_var( 'page_id' ) === (int) get_option( 'page_for_posts' ) |
|
257 ) { |
|
258 $redirect_url = get_permalink( get_option( 'page_for_posts' ) ); |
|
259 |
|
260 if ( $redirect_url ) { |
|
261 $redirect['query'] = remove_query_arg( 'page_id', $redirect['query'] ); |
|
262 } |
|
263 } elseif ( ! empty( $_GET['m'] ) && ( is_year() || is_month() || is_day() ) ) { |
|
264 $m = get_query_var( 'm' ); |
|
265 |
|
266 switch ( strlen( $m ) ) { |
|
267 case 4: // Yearly. |
|
268 $redirect_url = get_year_link( $m ); |
|
269 break; |
|
270 case 6: // Monthly. |
|
271 $redirect_url = get_month_link( substr( $m, 0, 4 ), substr( $m, 4, 2 ) ); |
|
272 break; |
|
273 case 8: // Daily. |
|
274 $redirect_url = get_day_link( substr( $m, 0, 4 ), substr( $m, 4, 2 ), substr( $m, 6, 2 ) ); |
|
275 break; |
|
276 } |
|
277 |
|
278 if ( $redirect_url ) { |
|
279 $redirect['query'] = remove_query_arg( 'm', $redirect['query'] ); |
|
280 } |
|
281 // Now moving on to non ?m=X year/month/day links. |
|
282 } elseif ( is_date() ) { |
132 $year = get_query_var( 'year' ); |
283 $year = get_query_var( 'year' ); |
133 $month = get_query_var( 'monthnum' ); |
284 $month = get_query_var( 'monthnum' ); |
134 $day = get_query_var( 'day' ); |
285 $day = get_query_var( 'day' ); |
135 $date = sprintf( '%04d-%02d-%02d', $year, $month, $day ); |
286 |
136 if ( ! wp_checkdate( $month, $day, $year, $date ) ) { |
287 if ( is_day() && $year && $month && ! empty( $_GET['day'] ) ) { |
137 $redirect_url = get_month_link( $year, $month ); |
288 $redirect_url = get_day_link( $year, $month, $day ); |
138 $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'year', 'monthnum', 'day' ), $redirect_url ); |
289 |
139 } |
|
140 } elseif ( get_query_var( 'monthnum' ) && get_query_var( 'year' ) && 12 < get_query_var( 'monthnum' ) ) { |
|
141 $redirect_url = get_year_link( get_query_var( 'year' ) ); |
|
142 $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'year', 'monthnum' ), $redirect_url ); |
|
143 } |
|
144 |
|
145 if ( ! $redirect_url ) { |
|
146 if ( $redirect_url = redirect_guess_404_permalink() ) { |
|
147 $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'page', 'feed', 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url ); |
|
148 } |
|
149 } |
|
150 |
|
151 if ( get_query_var( 'page' ) && $wp_query->post && |
|
152 false !== strpos( $wp_query->post->post_content, '<!--nextpage-->' ) ) { |
|
153 $redirect['path'] = rtrim( $redirect['path'], (int) get_query_var( 'page' ) . '/' ); |
|
154 $redirect['query'] = remove_query_arg( 'page', $redirect['query'] ); |
|
155 $redirect_url = get_permalink( $wp_query->post->ID ); |
|
156 } |
|
157 } elseif ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) { |
|
158 // rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101 |
|
159 if ( is_attachment() && |
|
160 ! array_diff( array_keys( $wp->query_vars ), array( 'attachment', 'attachment_id' ) ) && |
|
161 ! $redirect_url ) { |
|
162 if ( ! empty( $_GET['attachment_id'] ) ) { |
|
163 $redirect_url = get_attachment_link( get_query_var( 'attachment_id' ) ); |
|
164 if ( $redirect_url ) { |
290 if ( $redirect_url ) { |
165 $redirect['query'] = remove_query_arg( 'attachment_id', $redirect['query'] ); |
291 $redirect['query'] = remove_query_arg( array( 'year', 'monthnum', 'day' ), $redirect['query'] ); |
166 } |
292 } |
167 } else { |
293 } elseif ( is_month() && $year && ! empty( $_GET['monthnum'] ) ) { |
168 $redirect_url = get_attachment_link(); |
294 $redirect_url = get_month_link( $year, $month ); |
169 } |
295 |
170 } elseif ( is_single() && ! empty( $_GET['p'] ) && ! $redirect_url ) { |
296 if ( $redirect_url ) { |
171 if ( $redirect_url = get_permalink( get_query_var( 'p' ) ) ) { |
297 $redirect['query'] = remove_query_arg( array( 'year', 'monthnum' ), $redirect['query'] ); |
172 $redirect['query'] = remove_query_arg( array( 'p', 'post_type' ), $redirect['query'] ); |
298 } |
173 } |
299 } elseif ( is_year() && ! empty( $_GET['year'] ) ) { |
174 } elseif ( is_single() && ! empty( $_GET['name'] ) && ! $redirect_url ) { |
300 $redirect_url = get_year_link( $year ); |
175 if ( $redirect_url = get_permalink( $wp_query->get_queried_object_id() ) ) { |
301 |
176 $redirect['query'] = remove_query_arg( 'name', $redirect['query'] ); |
302 if ( $redirect_url ) { |
177 } |
303 $redirect['query'] = remove_query_arg( 'year', $redirect['query'] ); |
178 } elseif ( is_page() && ! empty( $_GET['page_id'] ) && ! $redirect_url ) { |
304 } |
179 if ( $redirect_url = get_permalink( get_query_var( 'page_id' ) ) ) { |
|
180 $redirect['query'] = remove_query_arg( 'page_id', $redirect['query'] ); |
|
181 } |
|
182 } elseif ( is_page() && ! is_feed() && 'page' == get_option( 'show_on_front' ) && get_queried_object_id() == get_option( 'page_on_front' ) && ! $redirect_url ) { |
|
183 $redirect_url = home_url( '/' ); |
|
184 } 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 ) { |
|
185 if ( $redirect_url = get_permalink( get_option( 'page_for_posts' ) ) ) { |
|
186 $redirect['query'] = remove_query_arg( 'page_id', $redirect['query'] ); |
|
187 } |
|
188 } elseif ( ! empty( $_GET['m'] ) && ( is_year() || is_month() || is_day() ) ) { |
|
189 $m = get_query_var( 'm' ); |
|
190 switch ( strlen( $m ) ) { |
|
191 case 4: // Yearly |
|
192 $redirect_url = get_year_link( $m ); |
|
193 break; |
|
194 case 6: // Monthly |
|
195 $redirect_url = get_month_link( substr( $m, 0, 4 ), substr( $m, 4, 2 ) ); |
|
196 break; |
|
197 case 8: // Daily |
|
198 $redirect_url = get_day_link( substr( $m, 0, 4 ), substr( $m, 4, 2 ), substr( $m, 6, 2 ) ); |
|
199 break; |
|
200 } |
|
201 if ( $redirect_url ) { |
|
202 $redirect['query'] = remove_query_arg( 'm', $redirect['query'] ); |
|
203 } |
|
204 // now moving on to non ?m=X year/month/day links |
|
205 } elseif ( is_day() && get_query_var( 'year' ) && get_query_var( 'monthnum' ) && ! empty( $_GET['day'] ) ) { |
|
206 if ( $redirect_url = get_day_link( get_query_var( 'year' ), get_query_var( 'monthnum' ), get_query_var( 'day' ) ) ) { |
|
207 $redirect['query'] = remove_query_arg( array( 'year', 'monthnum', 'day' ), $redirect['query'] ); |
|
208 } |
|
209 } elseif ( is_month() && get_query_var( 'year' ) && ! empty( $_GET['monthnum'] ) ) { |
|
210 if ( $redirect_url = get_month_link( get_query_var( 'year' ), get_query_var( 'monthnum' ) ) ) { |
|
211 $redirect['query'] = remove_query_arg( array( 'year', 'monthnum' ), $redirect['query'] ); |
|
212 } |
|
213 } elseif ( is_year() && ! empty( $_GET['year'] ) ) { |
|
214 if ( $redirect_url = get_year_link( get_query_var( 'year' ) ) ) { |
|
215 $redirect['query'] = remove_query_arg( 'year', $redirect['query'] ); |
|
216 } |
305 } |
217 } elseif ( is_author() && ! empty( $_GET['author'] ) && preg_match( '|^[0-9]+$|', $_GET['author'] ) ) { |
306 } elseif ( is_author() && ! empty( $_GET['author'] ) && preg_match( '|^[0-9]+$|', $_GET['author'] ) ) { |
218 $author = get_userdata( get_query_var( 'author' ) ); |
307 $author = get_userdata( get_query_var( 'author' ) ); |
219 if ( ( false !== $author ) && $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 ) ) ) { |
308 |
220 if ( $redirect_url = get_author_posts_url( $author->ID, $author->user_nicename ) ) { |
309 if ( false !== $author |
|
310 && $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 ) ) |
|
311 ) { |
|
312 $redirect_url = get_author_posts_url( $author->ID, $author->user_nicename ); |
|
313 |
|
314 if ( $redirect_url ) { |
221 $redirect['query'] = remove_query_arg( 'author', $redirect['query'] ); |
315 $redirect['query'] = remove_query_arg( 'author', $redirect['query'] ); |
222 } |
316 } |
223 } |
317 } |
224 } elseif ( is_category() || is_tag() || is_tax() ) { // Terms (Tags/categories) |
318 } elseif ( is_category() || is_tag() || is_tax() ) { // Terms (tags/categories). |
225 |
|
226 $term_count = 0; |
319 $term_count = 0; |
|
320 |
227 foreach ( $wp_query->tax_query->queried_terms as $tax_query ) { |
321 foreach ( $wp_query->tax_query->queried_terms as $tax_query ) { |
228 $term_count += count( $tax_query['terms'] ); |
322 $term_count += count( $tax_query['terms'] ); |
229 } |
323 } |
230 |
324 |
231 $obj = $wp_query->get_queried_object(); |
325 $obj = $wp_query->get_queried_object(); |
232 if ( $term_count <= 1 && ! empty( $obj->term_id ) && ( $tax_url = get_term_link( (int) $obj->term_id, $obj->taxonomy ) ) && ! is_wp_error( $tax_url ) ) { |
326 |
233 if ( ! empty( $redirect['query'] ) ) { |
327 if ( $term_count <= 1 && ! empty( $obj->term_id ) ) { |
234 // Strip taxonomy query vars off the url. |
328 $tax_url = get_term_link( (int) $obj->term_id, $obj->taxonomy ); |
235 $qv_remove = array( 'term', 'taxonomy' ); |
329 |
236 if ( is_category() ) { |
330 if ( $tax_url && ! is_wp_error( $tax_url ) ) { |
237 $qv_remove[] = 'category_name'; |
331 if ( ! empty( $redirect['query'] ) ) { |
238 $qv_remove[] = 'cat'; |
332 // Strip taxonomy query vars off the URL. |
239 } elseif ( is_tag() ) { |
333 $qv_remove = array( 'term', 'taxonomy' ); |
240 $qv_remove[] = 'tag'; |
334 |
241 $qv_remove[] = 'tag_id'; |
335 if ( is_category() ) { |
242 } else { // Custom taxonomies will have a custom query var, remove those too: |
336 $qv_remove[] = 'category_name'; |
243 $tax_obj = get_taxonomy( $obj->taxonomy ); |
337 $qv_remove[] = 'cat'; |
244 if ( false !== $tax_obj->query_var ) { |
338 } elseif ( is_tag() ) { |
245 $qv_remove[] = $tax_obj->query_var; |
339 $qv_remove[] = 'tag'; |
|
340 $qv_remove[] = 'tag_id'; |
|
341 } else { |
|
342 // Custom taxonomies will have a custom query var, remove those too. |
|
343 $tax_obj = get_taxonomy( $obj->taxonomy ); |
|
344 if ( false !== $tax_obj->query_var ) { |
|
345 $qv_remove[] = $tax_obj->query_var; |
|
346 } |
246 } |
347 } |
247 } |
348 |
248 |
349 $rewrite_vars = array_diff( array_keys( $wp_query->query ), array_keys( $_GET ) ); |
249 $rewrite_vars = array_diff( array_keys( $wp_query->query ), array_keys( $_GET ) ); |
350 |
250 |
351 // Check to see if all the query vars are coming from the rewrite, none are set via $_GET. |
251 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 |
352 if ( ! array_diff( $rewrite_vars, array_keys( $_GET ) ) ) { |
252 $redirect['query'] = remove_query_arg( $qv_remove, $redirect['query'] ); //Remove all of the per-tax qv's |
353 // Remove all of the per-tax query vars. |
253 |
354 $redirect['query'] = remove_query_arg( $qv_remove, $redirect['query'] ); |
254 // Create the destination url for this taxonomy |
355 |
255 $tax_url = parse_url( $tax_url ); |
356 // Create the destination URL for this taxonomy. |
256 if ( ! empty( $tax_url['query'] ) ) { // Taxonomy accessible via ?taxonomy=..&term=.. or any custom qv.. |
357 $tax_url = parse_url( $tax_url ); |
257 parse_str( $tax_url['query'], $query_vars ); |
358 |
258 $redirect['query'] = add_query_arg( $query_vars, $redirect['query'] ); |
359 if ( ! empty( $tax_url['query'] ) ) { |
259 } else { // Taxonomy is accessible via a "pretty-URL" |
360 // Taxonomy accessible via ?taxonomy=...&term=... or any custom query var. |
260 $redirect['path'] = $tax_url['path']; |
361 parse_str( $tax_url['query'], $query_vars ); |
261 } |
362 $redirect['query'] = add_query_arg( $query_vars, $redirect['query'] ); |
262 } else { // Some query vars are set via $_GET. Unset those from $_GET that exist via the rewrite |
363 } else { |
263 foreach ( $qv_remove as $_qv ) { |
364 // Taxonomy is accessible via a "pretty URL". |
264 if ( isset( $rewrite_vars[ $_qv ] ) ) { |
365 $redirect['path'] = $tax_url['path']; |
265 $redirect['query'] = remove_query_arg( $_qv, $redirect['query'] ); |
366 } |
|
367 } else { |
|
368 // Some query vars are set via $_GET. Unset those from $_GET that exist via the rewrite. |
|
369 foreach ( $qv_remove as $_qv ) { |
|
370 if ( isset( $rewrite_vars[ $_qv ] ) ) { |
|
371 $redirect['query'] = remove_query_arg( $_qv, $redirect['query'] ); |
|
372 } |
266 } |
373 } |
267 } |
374 } |
268 } |
375 } |
269 } |
376 } |
270 } |
377 } |
271 } elseif ( is_single() && strpos( $wp_rewrite->permalink_structure, '%category%' ) !== false && $cat = get_query_var( 'category_name' ) ) { |
378 } elseif ( is_single() && strpos( $wp_rewrite->permalink_structure, '%category%' ) !== false ) { |
272 $category = get_category_by_path( $cat ); |
379 $category_name = get_query_var( 'category_name' ); |
273 if ( ( ! $category || is_wp_error( $category ) ) || ! has_term( $category->term_id, 'category', $wp_query->get_queried_object_id() ) ) { |
380 |
274 $redirect_url = get_permalink( $wp_query->get_queried_object_id() ); |
381 if ( $category_name ) { |
275 } |
382 $category = get_category_by_path( $category_name ); |
276 } |
383 |
277 |
384 if ( ! $category || is_wp_error( $category ) |
278 // Post Paging |
385 || ! has_term( $category->term_id, 'category', $wp_query->get_queried_object_id() ) |
|
386 ) { |
|
387 $redirect_url = get_permalink( $wp_query->get_queried_object_id() ); |
|
388 } |
|
389 } |
|
390 } |
|
391 |
|
392 // Post paging. |
279 if ( is_singular() && get_query_var( 'page' ) ) { |
393 if ( is_singular() && get_query_var( 'page' ) ) { |
|
394 $page = get_query_var( 'page' ); |
|
395 |
280 if ( ! $redirect_url ) { |
396 if ( ! $redirect_url ) { |
281 $redirect_url = get_permalink( get_queried_object_id() ); |
397 $redirect_url = get_permalink( get_queried_object_id() ); |
282 } |
398 } |
283 |
399 |
284 $page = get_query_var( 'page' ); |
|
285 if ( $page > 1 ) { |
400 if ( $page > 1 ) { |
|
401 $redirect_url = trailingslashit( $redirect_url ); |
|
402 |
286 if ( is_front_page() ) { |
403 if ( is_front_page() ) { |
287 $redirect_url = trailingslashit( $redirect_url ) . user_trailingslashit( "$wp_rewrite->pagination_base/$page", 'paged' ); |
404 $redirect_url .= user_trailingslashit( "$wp_rewrite->pagination_base/$page", 'paged' ); |
288 } else { |
405 } else { |
289 $redirect_url = trailingslashit( $redirect_url ) . user_trailingslashit( $page, 'single_paged' ); |
406 $redirect_url .= user_trailingslashit( $page, 'single_paged' ); |
290 } |
407 } |
291 } |
408 } |
292 $redirect['query'] = remove_query_arg( 'page', $redirect['query'] ); |
409 |
293 } |
410 $redirect['query'] = remove_query_arg( 'page', $redirect['query'] ); |
294 |
411 } |
295 // paging and feeds |
412 |
296 if ( get_query_var( 'paged' ) || is_feed() || get_query_var( 'cpage' ) ) { |
413 if ( get_query_var( 'sitemap' ) ) { |
297 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'] ) ) { |
414 $redirect_url = get_sitemap_url( get_query_var( 'sitemap' ), get_query_var( 'sitemap-subtype' ), get_query_var( 'paged' ) ); |
298 // Strip off paging and feed |
415 $redirect['query'] = remove_query_arg( array( 'sitemap', 'sitemap-subtype', 'paged' ), $redirect['query'] ); |
299 $redirect['path'] = preg_replace( "#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", '/', $redirect['path'] ); // strip off any existing paging |
416 } elseif ( get_query_var( 'paged' ) || is_feed() || get_query_var( 'cpage' ) ) { |
300 $redirect['path'] = preg_replace( '#/(comments/?)?(feed|rss2?|rdf|atom)(/+|$)#', '/', $redirect['path'] ); // strip off feed endings |
417 // Paging and feeds. |
301 $redirect['path'] = preg_replace( "#/{$wp_rewrite->comments_pagination_base}-[0-9]+?(/+)?$#", '/', $redirect['path'] ); // strip off any existing comment paging |
418 $paged = get_query_var( 'paged' ); |
302 } |
419 $feed = get_query_var( 'feed' ); |
303 |
420 $cpage = get_query_var( 'cpage' ); |
304 $addl_path = ''; |
421 |
305 if ( is_feed() && in_array( get_query_var( 'feed' ), $wp_rewrite->feeds ) ) { |
422 while ( preg_match( "#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", $redirect['path'] ) |
|
423 || preg_match( '#/(comments/?)?(feed|rss2?|rdf|atom)(/+)?$#', $redirect['path'] ) |
|
424 || preg_match( "#/{$wp_rewrite->comments_pagination_base}-[0-9]+(/+)?$#", $redirect['path'] ) |
|
425 ) { |
|
426 // Strip off any existing paging. |
|
427 $redirect['path'] = preg_replace( "#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", '/', $redirect['path'] ); |
|
428 // Strip off feed endings. |
|
429 $redirect['path'] = preg_replace( '#/(comments/?)?(feed|rss2?|rdf|atom)(/+|$)#', '/', $redirect['path'] ); |
|
430 // Strip off any existing comment paging. |
|
431 $redirect['path'] = preg_replace( "#/{$wp_rewrite->comments_pagination_base}-[0-9]+?(/+)?$#", '/', $redirect['path'] ); |
|
432 } |
|
433 |
|
434 $addl_path = ''; |
|
435 $default_feed = get_default_feed(); |
|
436 |
|
437 if ( is_feed() && in_array( $feed, $wp_rewrite->feeds, true ) ) { |
306 $addl_path = ! empty( $addl_path ) ? trailingslashit( $addl_path ) : ''; |
438 $addl_path = ! empty( $addl_path ) ? trailingslashit( $addl_path ) : ''; |
|
439 |
307 if ( ! is_singular() && get_query_var( 'withcomments' ) ) { |
440 if ( ! is_singular() && get_query_var( 'withcomments' ) ) { |
308 $addl_path .= 'comments/'; |
441 $addl_path .= 'comments/'; |
309 } |
442 } |
310 if ( ( 'rss' == get_default_feed() && 'feed' == get_query_var( 'feed' ) ) || 'rss' == get_query_var( 'feed' ) ) { |
443 |
311 $addl_path .= user_trailingslashit( 'feed/' . ( ( get_default_feed() == 'rss2' ) ? '' : 'rss2' ), 'feed' ); |
444 if ( ( 'rss' === $default_feed && 'feed' === $feed ) || 'rss' === $feed ) { |
|
445 $format = ( 'rss2' === $default_feed ) ? '' : 'rss2'; |
312 } else { |
446 } else { |
313 $addl_path .= user_trailingslashit( 'feed/' . ( ( get_default_feed() == get_query_var( 'feed' ) || 'feed' == get_query_var( 'feed' ) ) ? '' : get_query_var( 'feed' ) ), 'feed' ); |
447 $format = ( $default_feed === $feed || 'feed' === $feed ) ? '' : $feed; |
314 } |
448 } |
|
449 |
|
450 $addl_path .= user_trailingslashit( 'feed/' . $format, 'feed' ); |
|
451 |
315 $redirect['query'] = remove_query_arg( 'feed', $redirect['query'] ); |
452 $redirect['query'] = remove_query_arg( 'feed', $redirect['query'] ); |
316 } elseif ( is_feed() && 'old' == get_query_var( 'feed' ) ) { |
453 } elseif ( is_feed() && 'old' === $feed ) { |
317 $old_feed_files = array( |
454 $old_feed_files = array( |
318 'wp-atom.php' => 'atom', |
455 'wp-atom.php' => 'atom', |
319 'wp-commentsrss2.php' => 'comments_rss2', |
456 'wp-commentsrss2.php' => 'comments_rss2', |
320 'wp-feed.php' => get_default_feed(), |
457 'wp-feed.php' => $default_feed, |
321 'wp-rdf.php' => 'rdf', |
458 'wp-rdf.php' => 'rdf', |
322 'wp-rss.php' => 'rss2', |
459 'wp-rss.php' => 'rss2', |
323 'wp-rss2.php' => 'rss2', |
460 'wp-rss2.php' => 'rss2', |
324 ); |
461 ); |
|
462 |
325 if ( isset( $old_feed_files[ basename( $redirect['path'] ) ] ) ) { |
463 if ( isset( $old_feed_files[ basename( $redirect['path'] ) ] ) ) { |
326 $redirect_url = get_feed_link( $old_feed_files[ basename( $redirect['path'] ) ] ); |
464 $redirect_url = get_feed_link( $old_feed_files[ basename( $redirect['path'] ) ] ); |
|
465 |
327 wp_redirect( $redirect_url, 301 ); |
466 wp_redirect( $redirect_url, 301 ); |
328 die(); |
467 die(); |
329 } |
468 } |
330 } |
469 } |
331 |
470 |
332 if ( get_query_var( 'paged' ) > 0 ) { |
471 if ( $paged > 0 ) { |
333 $paged = get_query_var( 'paged' ); |
|
334 $redirect['query'] = remove_query_arg( 'paged', $redirect['query'] ); |
472 $redirect['query'] = remove_query_arg( 'paged', $redirect['query'] ); |
|
473 |
335 if ( ! is_feed() ) { |
474 if ( ! is_feed() ) { |
336 if ( $paged > 1 && ! is_single() ) { |
475 if ( ! is_single() ) { |
337 $addl_path = ( ! empty( $addl_path ) ? trailingslashit( $addl_path ) : '' ) . user_trailingslashit( "$wp_rewrite->pagination_base/$paged", 'paged' ); |
|
338 } elseif ( ! is_single() ) { |
|
339 $addl_path = ! empty( $addl_path ) ? trailingslashit( $addl_path ) : ''; |
476 $addl_path = ! empty( $addl_path ) ? trailingslashit( $addl_path ) : ''; |
|
477 |
|
478 if ( $paged > 1 ) { |
|
479 $addl_path .= user_trailingslashit( "$wp_rewrite->pagination_base/$paged", 'paged' ); |
|
480 } |
340 } |
481 } |
341 } elseif ( $paged > 1 ) { |
482 } elseif ( $paged > 1 ) { |
342 $redirect['query'] = add_query_arg( 'paged', $paged, $redirect['query'] ); |
483 $redirect['query'] = add_query_arg( 'paged', $paged, $redirect['query'] ); |
343 } |
484 } |
344 } |
485 } |
345 |
486 |
346 if ( get_option( 'page_comments' ) && ( |
487 $default_comments_page = get_option( 'default_comments_page' ); |
347 ( 'newest' == get_option( 'default_comments_page' ) && get_query_var( 'cpage' ) > 0 ) || |
488 |
348 ( 'newest' != get_option( 'default_comments_page' ) && get_query_var( 'cpage' ) > 1 ) |
489 if ( get_option( 'page_comments' ) |
349 ) ) { |
490 && ( 'newest' === $default_comments_page && $cpage > 0 |
350 $addl_path = ( ! empty( $addl_path ) ? trailingslashit( $addl_path ) : '' ) . user_trailingslashit( $wp_rewrite->comments_pagination_base . '-' . get_query_var( 'cpage' ), 'commentpaged' ); |
491 || 'newest' !== $default_comments_page && $cpage > 1 ) |
|
492 ) { |
|
493 $addl_path = ( ! empty( $addl_path ) ? trailingslashit( $addl_path ) : '' ); |
|
494 $addl_path .= user_trailingslashit( $wp_rewrite->comments_pagination_base . '-' . $cpage, 'commentpaged' ); |
|
495 |
351 $redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] ); |
496 $redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] ); |
352 } |
497 } |
353 |
498 |
354 $redirect['path'] = user_trailingslashit( preg_replace( '|/' . preg_quote( $wp_rewrite->index, '|' ) . '/?$|', '/', $redirect['path'] ) ); // strip off trailing /index.php/ |
499 // Strip off trailing /index.php/. |
355 if ( ! empty( $addl_path ) && $wp_rewrite->using_index_permalinks() && strpos( $redirect['path'], '/' . $wp_rewrite->index . '/' ) === false ) { |
500 $redirect['path'] = preg_replace( '|/' . preg_quote( $wp_rewrite->index, '|' ) . '/?$|', '/', $redirect['path'] ); |
|
501 $redirect['path'] = user_trailingslashit( $redirect['path'] ); |
|
502 |
|
503 if ( ! empty( $addl_path ) |
|
504 && $wp_rewrite->using_index_permalinks() |
|
505 && strpos( $redirect['path'], '/' . $wp_rewrite->index . '/' ) === false |
|
506 ) { |
356 $redirect['path'] = trailingslashit( $redirect['path'] ) . $wp_rewrite->index . '/'; |
507 $redirect['path'] = trailingslashit( $redirect['path'] ) . $wp_rewrite->index . '/'; |
357 } |
508 } |
|
509 |
358 if ( ! empty( $addl_path ) ) { |
510 if ( ! empty( $addl_path ) ) { |
359 $redirect['path'] = trailingslashit( $redirect['path'] ) . $addl_path; |
511 $redirect['path'] = trailingslashit( $redirect['path'] ) . $addl_path; |
360 } |
512 } |
|
513 |
361 $redirect_url = $redirect['scheme'] . '://' . $redirect['host'] . $redirect['path']; |
514 $redirect_url = $redirect['scheme'] . '://' . $redirect['host'] . $redirect['path']; |
362 } |
515 } |
363 |
516 |
364 if ( 'wp-register.php' == basename( $redirect['path'] ) ) { |
517 if ( 'wp-register.php' === basename( $redirect['path'] ) ) { |
365 if ( is_multisite() ) { |
518 if ( is_multisite() ) { |
366 /** This filter is documented in wp-login.php */ |
519 /** This filter is documented in wp-login.php */ |
367 $redirect_url = apply_filters( 'wp_signup_location', network_site_url( 'wp-signup.php' ) ); |
520 $redirect_url = apply_filters( 'wp_signup_location', network_site_url( 'wp-signup.php' ) ); |
368 } else { |
521 } else { |
369 $redirect_url = wp_registration_url(); |
522 $redirect_url = wp_registration_url(); |