91 $redirect['query'] = remove_query_arg( 'preview', $redirect['query'] ); |
93 $redirect['query'] = remove_query_arg( 'preview', $redirect['query'] ); |
92 } |
94 } |
93 |
95 |
94 if ( is_feed() && ( $id = get_query_var( 'p' ) ) ) { |
96 if ( is_feed() && ( $id = get_query_var( 'p' ) ) ) { |
95 if ( $redirect_url = get_post_comments_feed_link( $id, get_query_var( 'feed' ) ) ) { |
97 if ( $redirect_url = get_post_comments_feed_link( $id, get_query_var( 'feed' ) ) ) { |
96 $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type', 'feed'), $redirect_url ); |
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 ); |
97 $redirect['path'] = parse_url( $redirect_url, PHP_URL_PATH ); |
99 $redirect['path'] = parse_url( $redirect_url, PHP_URL_PATH ); |
98 } |
100 } |
99 } |
101 } |
100 |
102 |
101 if ( is_singular() && 1 > $wp_query->post_count && ($id = get_query_var('p')) ) { |
103 if ( is_singular() && 1 > $wp_query->post_count && ( $id = get_query_var( 'p' ) ) ) { |
102 |
104 |
103 $vars = $wpdb->get_results( $wpdb->prepare("SELECT post_type, post_parent FROM $wpdb->posts WHERE ID = %d", $id) ); |
105 $vars = $wpdb->get_results( $wpdb->prepare( "SELECT post_type, post_parent FROM $wpdb->posts WHERE ID = %d", $id ) ); |
104 |
106 |
105 if ( isset($vars[0]) && $vars = $vars[0] ) { |
107 if ( isset( $vars[0] ) && $vars = $vars[0] ) { |
106 if ( 'revision' == $vars->post_type && $vars->post_parent > 0 ) |
108 if ( 'revision' == $vars->post_type && $vars->post_parent > 0 ) { |
107 $id = $vars->post_parent; |
109 $id = $vars->post_parent; |
108 |
110 } |
109 if ( $redirect_url = get_permalink($id) ) |
111 |
|
112 if ( $redirect_url = get_permalink( $id ) ) { |
110 $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url ); |
113 $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url ); |
|
114 } |
111 } |
115 } |
112 } |
116 } |
113 |
117 |
114 // These tests give us a WP-generated permalink |
118 // These tests give us a WP-generated permalink |
115 if ( is_404() ) { |
119 if ( is_404() ) { |
116 |
120 |
117 // Redirect ?page_id, ?p=, ?attachment_id= to their respective url's |
121 // Redirect ?page_id, ?p=, ?attachment_id= to their respective url's |
118 $id = max( get_query_var('p'), get_query_var('page_id'), get_query_var('attachment_id') ); |
122 $id = max( get_query_var( 'p' ), get_query_var( 'page_id' ), get_query_var( 'attachment_id' ) ); |
119 if ( $id && $redirect_post = get_post($id) ) { |
123 if ( $id && $redirect_post = get_post( $id ) ) { |
120 $post_type_obj = get_post_type_object($redirect_post->post_type); |
124 $post_type_obj = get_post_type_object( $redirect_post->post_type ); |
121 if ( $post_type_obj->public && 'auto-draft' != $redirect_post->post_status ) { |
125 if ( $post_type_obj->public && 'auto-draft' != $redirect_post->post_status ) { |
122 $redirect_url = get_permalink($redirect_post); |
126 $redirect_url = get_permalink( $redirect_post ); |
123 $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url ); |
127 $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url ); |
124 } |
128 } |
125 } |
129 } |
126 |
130 |
127 if ( get_query_var( 'day' ) && get_query_var( 'monthnum' ) && get_query_var( 'year' ) ) { |
131 if ( get_query_var( 'day' ) && get_query_var( 'monthnum' ) && get_query_var( 'year' ) ) { |
128 $year = get_query_var( 'year' ); |
132 $year = get_query_var( 'year' ); |
129 $month = get_query_var( 'monthnum' ); |
133 $month = get_query_var( 'monthnum' ); |
130 $day = get_query_var( 'day' ); |
134 $day = get_query_var( 'day' ); |
131 $date = sprintf( '%04d-%02d-%02d', $year, $month, $day ); |
135 $date = sprintf( '%04d-%02d-%02d', $year, $month, $day ); |
132 if ( ! wp_checkdate( $month, $day, $year, $date ) ) { |
136 if ( ! wp_checkdate( $month, $day, $year, $date ) ) { |
133 $redirect_url = get_month_link( $year, $month ); |
137 $redirect_url = get_month_link( $year, $month ); |
134 $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'year', 'monthnum', 'day' ), $redirect_url ); |
138 $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'year', 'monthnum', 'day' ), $redirect_url ); |
135 } |
139 } |
136 } elseif ( get_query_var( 'monthnum' ) && get_query_var( 'year' ) && 12 < get_query_var( 'monthnum' ) ) { |
140 } elseif ( get_query_var( 'monthnum' ) && get_query_var( 'year' ) && 12 < get_query_var( 'monthnum' ) ) { |
137 $redirect_url = get_year_link( get_query_var( 'year' ) ); |
141 $redirect_url = get_year_link( get_query_var( 'year' ) ); |
138 $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'year', 'monthnum' ), $redirect_url ); |
142 $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'year', 'monthnum' ), $redirect_url ); |
139 } |
143 } |
140 |
144 |
141 if ( ! $redirect_url ) { |
145 if ( ! $redirect_url ) { |
142 if ( $redirect_url = redirect_guess_404_permalink() ) { |
146 if ( $redirect_url = redirect_guess_404_permalink() ) { |
162 $redirect['query'] = remove_query_arg( 'attachment_id', $redirect['query'] ); |
165 $redirect['query'] = remove_query_arg( 'attachment_id', $redirect['query'] ); |
163 } |
166 } |
164 } else { |
167 } else { |
165 $redirect_url = get_attachment_link(); |
168 $redirect_url = get_attachment_link(); |
166 } |
169 } |
167 } elseif ( is_single() && !empty($_GET['p']) && ! $redirect_url ) { |
170 } elseif ( is_single() && ! empty( $_GET['p'] ) && ! $redirect_url ) { |
168 if ( $redirect_url = get_permalink(get_query_var('p')) ) |
171 if ( $redirect_url = get_permalink( get_query_var( 'p' ) ) ) { |
169 $redirect['query'] = remove_query_arg(array('p', 'post_type'), $redirect['query']); |
172 $redirect['query'] = remove_query_arg( array( 'p', 'post_type' ), $redirect['query'] ); |
170 } elseif ( is_single() && !empty($_GET['name']) && ! $redirect_url ) { |
173 } |
171 if ( $redirect_url = get_permalink( $wp_query->get_queried_object_id() ) ) |
174 } elseif ( is_single() && ! empty( $_GET['name'] ) && ! $redirect_url ) { |
172 $redirect['query'] = remove_query_arg('name', $redirect['query']); |
175 if ( $redirect_url = get_permalink( $wp_query->get_queried_object_id() ) ) { |
173 } elseif ( is_page() && !empty($_GET['page_id']) && ! $redirect_url ) { |
176 $redirect['query'] = remove_query_arg( 'name', $redirect['query'] ); |
174 if ( $redirect_url = get_permalink(get_query_var('page_id')) ) |
177 } |
175 $redirect['query'] = remove_query_arg('page_id', $redirect['query']); |
178 } elseif ( is_page() && ! empty( $_GET['page_id'] ) && ! $redirect_url ) { |
176 } elseif ( is_page() && !is_feed() && 'page' == get_option('show_on_front') && get_queried_object_id() == get_option('page_on_front') && ! $redirect_url ) { |
179 if ( $redirect_url = get_permalink( get_query_var( 'page_id' ) ) ) { |
177 $redirect_url = home_url('/'); |
180 $redirect['query'] = remove_query_arg( 'page_id', $redirect['query'] ); |
178 } 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 ) { |
181 } |
179 if ( $redirect_url = get_permalink(get_option('page_for_posts')) ) |
182 } elseif ( is_page() && ! is_feed() && 'page' == get_option( 'show_on_front' ) && get_queried_object_id() == get_option( 'page_on_front' ) && ! $redirect_url ) { |
180 $redirect['query'] = remove_query_arg('page_id', $redirect['query']); |
183 $redirect_url = home_url( '/' ); |
181 } elseif ( !empty($_GET['m']) && ( is_year() || is_month() || is_day() ) ) { |
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 ) { |
182 $m = get_query_var('m'); |
185 if ( $redirect_url = get_permalink( get_option( 'page_for_posts' ) ) ) { |
183 switch ( strlen($m) ) { |
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 ) ) { |
184 case 4: // Yearly |
191 case 4: // Yearly |
185 $redirect_url = get_year_link($m); |
192 $redirect_url = get_year_link( $m ); |
186 break; |
193 break; |
187 case 6: // Monthly |
194 case 6: // Monthly |
188 $redirect_url = get_month_link( substr($m, 0, 4), substr($m, 4, 2) ); |
195 $redirect_url = get_month_link( substr( $m, 0, 4 ), substr( $m, 4, 2 ) ); |
189 break; |
196 break; |
190 case 8: // Daily |
197 case 8: // Daily |
191 $redirect_url = get_day_link(substr($m, 0, 4), substr($m, 4, 2), substr($m, 6, 2)); |
198 $redirect_url = get_day_link( substr( $m, 0, 4 ), substr( $m, 4, 2 ), substr( $m, 6, 2 ) ); |
192 break; |
199 break; |
193 } |
200 } |
194 if ( $redirect_url ) |
201 if ( $redirect_url ) { |
195 $redirect['query'] = remove_query_arg('m', $redirect['query']); |
202 $redirect['query'] = remove_query_arg( 'm', $redirect['query'] ); |
196 // now moving on to non ?m=X year/month/day links |
203 } |
197 } elseif ( is_day() && get_query_var('year') && get_query_var('monthnum') && !empty($_GET['day']) ) { |
204 // now moving on to non ?m=X year/month/day links |
198 if ( $redirect_url = get_day_link(get_query_var('year'), get_query_var('monthnum'), get_query_var('day')) ) |
205 } elseif ( is_day() && get_query_var( 'year' ) && get_query_var( 'monthnum' ) && ! empty( $_GET['day'] ) ) { |
199 $redirect['query'] = remove_query_arg(array('year', 'monthnum', 'day'), $redirect['query']); |
206 if ( $redirect_url = get_day_link( get_query_var( 'year' ), get_query_var( 'monthnum' ), get_query_var( 'day' ) ) ) { |
200 } elseif ( is_month() && get_query_var('year') && !empty($_GET['monthnum']) ) { |
207 $redirect['query'] = remove_query_arg( array( 'year', 'monthnum', 'day' ), $redirect['query'] ); |
201 if ( $redirect_url = get_month_link(get_query_var('year'), get_query_var('monthnum')) ) |
208 } |
202 $redirect['query'] = remove_query_arg(array('year', 'monthnum'), $redirect['query']); |
209 } elseif ( is_month() && get_query_var( 'year' ) && ! empty( $_GET['monthnum'] ) ) { |
203 } elseif ( is_year() && !empty($_GET['year']) ) { |
210 if ( $redirect_url = get_month_link( get_query_var( 'year' ), get_query_var( 'monthnum' ) ) ) { |
204 if ( $redirect_url = get_year_link(get_query_var('year')) ) |
211 $redirect['query'] = remove_query_arg( array( 'year', 'monthnum' ), $redirect['query'] ); |
205 $redirect['query'] = remove_query_arg('year', $redirect['query']); |
212 } |
206 } elseif ( is_author() && !empty($_GET['author']) && preg_match( '|^[0-9]+$|', $_GET['author'] ) ) { |
213 } elseif ( is_year() && ! empty( $_GET['year'] ) ) { |
207 $author = get_userdata(get_query_var('author')); |
214 if ( $redirect_url = get_year_link( get_query_var( 'year' ) ) ) { |
|
215 $redirect['query'] = remove_query_arg( 'year', $redirect['query'] ); |
|
216 } |
|
217 } elseif ( is_author() && ! empty( $_GET['author'] ) && preg_match( '|^[0-9]+$|', $_GET['author'] ) ) { |
|
218 $author = get_userdata( get_query_var( 'author' ) ); |
208 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 ) ) ) { |
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 ) ) ) { |
209 if ( $redirect_url = get_author_posts_url($author->ID, $author->user_nicename) ) |
220 if ( $redirect_url = get_author_posts_url( $author->ID, $author->user_nicename ) ) { |
210 $redirect['query'] = remove_query_arg('author', $redirect['query']); |
221 $redirect['query'] = remove_query_arg( 'author', $redirect['query'] ); |
|
222 } |
211 } |
223 } |
212 } elseif ( is_category() || is_tag() || is_tax() ) { // Terms (Tags/categories) |
224 } elseif ( is_category() || is_tag() || is_tax() ) { // Terms (Tags/categories) |
213 |
225 |
214 $term_count = 0; |
226 $term_count = 0; |
215 foreach ( $wp_query->tax_query->queried_terms as $tax_query ) |
227 foreach ( $wp_query->tax_query->queried_terms as $tax_query ) { |
216 $term_count += count( $tax_query['terms'] ); |
228 $term_count += count( $tax_query['terms'] ); |
|
229 } |
217 |
230 |
218 $obj = $wp_query->get_queried_object(); |
231 $obj = $wp_query->get_queried_object(); |
219 if ( $term_count <= 1 && !empty($obj->term_id) && ( $tax_url = get_term_link((int)$obj->term_id, $obj->taxonomy) ) && !is_wp_error($tax_url) ) { |
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 ) ) { |
220 if ( !empty($redirect['query']) ) { |
233 if ( ! empty( $redirect['query'] ) ) { |
221 // Strip taxonomy query vars off the url. |
234 // Strip taxonomy query vars off the url. |
222 $qv_remove = array( 'term', 'taxonomy'); |
235 $qv_remove = array( 'term', 'taxonomy' ); |
223 if ( is_category() ) { |
236 if ( is_category() ) { |
224 $qv_remove[] = 'category_name'; |
237 $qv_remove[] = 'category_name'; |
225 $qv_remove[] = 'cat'; |
238 $qv_remove[] = 'cat'; |
226 } elseif ( is_tag() ) { |
239 } elseif ( is_tag() ) { |
227 $qv_remove[] = 'tag'; |
240 $qv_remove[] = 'tag'; |
228 $qv_remove[] = 'tag_id'; |
241 $qv_remove[] = 'tag_id'; |
229 } else { // Custom taxonomies will have a custom query var, remove those too: |
242 } else { // Custom taxonomies will have a custom query var, remove those too: |
230 $tax_obj = get_taxonomy( $obj->taxonomy ); |
243 $tax_obj = get_taxonomy( $obj->taxonomy ); |
231 if ( false !== $tax_obj->query_var ) |
244 if ( false !== $tax_obj->query_var ) { |
232 $qv_remove[] = $tax_obj->query_var; |
245 $qv_remove[] = $tax_obj->query_var; |
|
246 } |
233 } |
247 } |
234 |
248 |
235 $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 ) ); |
236 |
250 |
237 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 |
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 |
238 $redirect['query'] = remove_query_arg($qv_remove, $redirect['query']); //Remove all of the per-tax qv's |
252 $redirect['query'] = remove_query_arg( $qv_remove, $redirect['query'] ); //Remove all of the per-tax qv's |
239 |
253 |
240 // Create the destination url for this taxonomy |
254 // Create the destination url for this taxonomy |
241 $tax_url = parse_url($tax_url); |
255 $tax_url = parse_url( $tax_url ); |
242 if ( ! empty($tax_url['query']) ) { // Taxonomy accessible via ?taxonomy=..&term=.. or any custom qv.. |
256 if ( ! empty( $tax_url['query'] ) ) { // Taxonomy accessible via ?taxonomy=..&term=.. or any custom qv.. |
243 parse_str($tax_url['query'], $query_vars); |
257 parse_str( $tax_url['query'], $query_vars ); |
244 $redirect['query'] = add_query_arg($query_vars, $redirect['query']); |
258 $redirect['query'] = add_query_arg( $query_vars, $redirect['query'] ); |
245 } else { // Taxonomy is accessible via a "pretty-URL" |
259 } else { // Taxonomy is accessible via a "pretty-URL" |
246 $redirect['path'] = $tax_url['path']; |
260 $redirect['path'] = $tax_url['path']; |
247 } |
261 } |
248 |
|
249 } else { // Some query vars are set via $_GET. Unset those from $_GET that exist via the rewrite |
262 } else { // Some query vars are set via $_GET. Unset those from $_GET that exist via the rewrite |
250 foreach ( $qv_remove as $_qv ) { |
263 foreach ( $qv_remove as $_qv ) { |
251 if ( isset($rewrite_vars[$_qv]) ) |
264 if ( isset( $rewrite_vars[ $_qv ] ) ) { |
252 $redirect['query'] = remove_query_arg($_qv, $redirect['query']); |
265 $redirect['query'] = remove_query_arg( $_qv, $redirect['query'] ); |
|
266 } |
253 } |
267 } |
254 } |
268 } |
255 } |
269 } |
256 |
270 } |
257 } |
271 } elseif ( is_single() && strpos( $wp_rewrite->permalink_structure, '%category%' ) !== false && $cat = get_query_var( 'category_name' ) ) { |
258 } elseif ( is_single() && strpos($wp_rewrite->permalink_structure, '%category%') !== false && $cat = get_query_var( 'category_name' ) ) { |
|
259 $category = get_category_by_path( $cat ); |
272 $category = get_category_by_path( $cat ); |
260 if ( ( ! $category || is_wp_error( $category ) ) || ! has_term( $category->term_id, 'category', $wp_query->get_queried_object_id() ) ) { |
273 if ( ( ! $category || is_wp_error( $category ) ) || ! has_term( $category->term_id, 'category', $wp_query->get_queried_object_id() ) ) { |
261 $redirect_url = get_permalink($wp_query->get_queried_object_id()); |
274 $redirect_url = get_permalink( $wp_query->get_queried_object_id() ); |
262 } |
275 } |
263 } |
276 } |
264 |
277 |
265 // Post Paging |
278 // Post Paging |
266 if ( is_singular() && get_query_var('page') ) { |
279 if ( is_singular() && get_query_var( 'page' ) ) { |
267 if ( !$redirect_url ) |
280 if ( ! $redirect_url ) { |
268 $redirect_url = get_permalink( get_queried_object_id() ); |
281 $redirect_url = get_permalink( get_queried_object_id() ); |
|
282 } |
269 |
283 |
270 $page = get_query_var( 'page' ); |
284 $page = get_query_var( 'page' ); |
271 if ( $page > 1 ) { |
285 if ( $page > 1 ) { |
272 if ( is_front_page() ) { |
286 if ( is_front_page() ) { |
273 $redirect_url = trailingslashit( $redirect_url ) . user_trailingslashit( "$wp_rewrite->pagination_base/$page", 'paged' ); |
287 $redirect_url = trailingslashit( $redirect_url ) . user_trailingslashit( "$wp_rewrite->pagination_base/$page", 'paged' ); |
274 } else { |
288 } else { |
275 $redirect_url = trailingslashit( $redirect_url ) . user_trailingslashit( $page, 'single_paged' ); |
289 $redirect_url = trailingslashit( $redirect_url ) . user_trailingslashit( $page, 'single_paged' ); |
276 } |
290 } |
277 } |
291 } |
278 $redirect['query'] = remove_query_arg( 'page', $redirect['query'] ); |
292 $redirect['query'] = remove_query_arg( 'page', $redirect['query'] ); |
279 } |
293 } |
280 |
294 |
281 // paging and feeds |
295 // paging and feeds |
282 if ( get_query_var('paged') || is_feed() || get_query_var('cpage') ) { |
296 if ( get_query_var( 'paged' ) || is_feed() || get_query_var( 'cpage' ) ) { |
283 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'] ) ) { |
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'] ) ) { |
284 // Strip off paging and feed |
298 // Strip off paging and feed |
285 $redirect['path'] = preg_replace("#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", '/', $redirect['path']); // strip off any existing paging |
299 $redirect['path'] = preg_replace( "#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", '/', $redirect['path'] ); // strip off any existing paging |
286 $redirect['path'] = preg_replace('#/(comments/?)?(feed|rss2?|rdf|atom)(/+|$)#', '/', $redirect['path']); // strip off feed endings |
300 $redirect['path'] = preg_replace( '#/(comments/?)?(feed|rss2?|rdf|atom)(/+|$)#', '/', $redirect['path'] ); // strip off feed endings |
287 $redirect['path'] = preg_replace("#/{$wp_rewrite->comments_pagination_base}-[0-9]+?(/+)?$#", '/', $redirect['path']); // strip off any existing comment paging |
301 $redirect['path'] = preg_replace( "#/{$wp_rewrite->comments_pagination_base}-[0-9]+?(/+)?$#", '/', $redirect['path'] ); // strip off any existing comment paging |
288 } |
302 } |
289 |
303 |
290 $addl_path = ''; |
304 $addl_path = ''; |
291 if ( is_feed() && in_array( get_query_var('feed'), $wp_rewrite->feeds ) ) { |
305 if ( is_feed() && in_array( get_query_var( 'feed' ), $wp_rewrite->feeds ) ) { |
292 $addl_path = !empty( $addl_path ) ? trailingslashit($addl_path) : ''; |
306 $addl_path = ! empty( $addl_path ) ? trailingslashit( $addl_path ) : ''; |
293 if ( !is_singular() && get_query_var( 'withcomments' ) ) |
307 if ( ! is_singular() && get_query_var( 'withcomments' ) ) { |
294 $addl_path .= 'comments/'; |
308 $addl_path .= 'comments/'; |
295 if ( ( 'rss' == get_default_feed() && 'feed' == get_query_var('feed') ) || 'rss' == get_query_var('feed') ) |
309 } |
|
310 if ( ( 'rss' == get_default_feed() && 'feed' == get_query_var( 'feed' ) ) || 'rss' == get_query_var( 'feed' ) ) { |
296 $addl_path .= user_trailingslashit( 'feed/' . ( ( get_default_feed() == 'rss2' ) ? '' : 'rss2' ), 'feed' ); |
311 $addl_path .= user_trailingslashit( 'feed/' . ( ( get_default_feed() == 'rss2' ) ? '' : 'rss2' ), 'feed' ); |
297 else |
312 } else { |
298 $addl_path .= user_trailingslashit( 'feed/' . ( ( get_default_feed() == get_query_var('feed') || 'feed' == get_query_var('feed') ) ? '' : get_query_var('feed') ), 'feed' ); |
313 $addl_path .= user_trailingslashit( 'feed/' . ( ( get_default_feed() == get_query_var( 'feed' ) || 'feed' == get_query_var( 'feed' ) ) ? '' : get_query_var( 'feed' ) ), 'feed' ); |
|
314 } |
299 $redirect['query'] = remove_query_arg( 'feed', $redirect['query'] ); |
315 $redirect['query'] = remove_query_arg( 'feed', $redirect['query'] ); |
300 } elseif ( is_feed() && 'old' == get_query_var('feed') ) { |
316 } elseif ( is_feed() && 'old' == get_query_var( 'feed' ) ) { |
301 $old_feed_files = array( |
317 $old_feed_files = array( |
302 'wp-atom.php' => 'atom', |
318 'wp-atom.php' => 'atom', |
303 'wp-commentsrss2.php' => 'comments_rss2', |
319 'wp-commentsrss2.php' => 'comments_rss2', |
304 'wp-feed.php' => get_default_feed(), |
320 'wp-feed.php' => get_default_feed(), |
305 'wp-rdf.php' => 'rdf', |
321 'wp-rdf.php' => 'rdf', |
311 wp_redirect( $redirect_url, 301 ); |
327 wp_redirect( $redirect_url, 301 ); |
312 die(); |
328 die(); |
313 } |
329 } |
314 } |
330 } |
315 |
331 |
316 if ( get_query_var('paged') > 0 ) { |
332 if ( get_query_var( 'paged' ) > 0 ) { |
317 $paged = get_query_var('paged'); |
333 $paged = get_query_var( 'paged' ); |
318 $redirect['query'] = remove_query_arg( 'paged', $redirect['query'] ); |
334 $redirect['query'] = remove_query_arg( 'paged', $redirect['query'] ); |
319 if ( !is_feed() ) { |
335 if ( ! is_feed() ) { |
320 if ( $paged > 1 && !is_single() ) { |
336 if ( $paged > 1 && ! is_single() ) { |
321 $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit("$wp_rewrite->pagination_base/$paged", 'paged'); |
337 $addl_path = ( ! empty( $addl_path ) ? trailingslashit( $addl_path ) : '' ) . user_trailingslashit( "$wp_rewrite->pagination_base/$paged", 'paged' ); |
322 } elseif ( !is_single() ) { |
338 } elseif ( ! is_single() ) { |
323 $addl_path = !empty( $addl_path ) ? trailingslashit($addl_path) : ''; |
339 $addl_path = ! empty( $addl_path ) ? trailingslashit( $addl_path ) : ''; |
324 } |
340 } |
325 } elseif ( $paged > 1 ) { |
341 } elseif ( $paged > 1 ) { |
326 $redirect['query'] = add_query_arg( 'paged', $paged, $redirect['query'] ); |
342 $redirect['query'] = add_query_arg( 'paged', $paged, $redirect['query'] ); |
327 } |
343 } |
328 } |
344 } |
329 |
345 |
330 if ( get_option( 'page_comments' ) && ( |
346 if ( get_option( 'page_comments' ) && ( |
331 ( 'newest' == get_option( 'default_comments_page' ) && get_query_var( 'cpage' ) > 0 ) || |
347 ( 'newest' == get_option( 'default_comments_page' ) && get_query_var( 'cpage' ) > 0 ) || |
332 ( 'newest' != get_option( 'default_comments_page' ) && get_query_var( 'cpage' ) > 1 ) |
348 ( 'newest' != get_option( 'default_comments_page' ) && get_query_var( 'cpage' ) > 1 ) |
333 ) ) { |
349 ) ) { |
334 $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit( $wp_rewrite->comments_pagination_base . '-' . get_query_var('cpage'), 'commentpaged' ); |
350 $addl_path = ( ! empty( $addl_path ) ? trailingslashit( $addl_path ) : '' ) . user_trailingslashit( $wp_rewrite->comments_pagination_base . '-' . get_query_var( 'cpage' ), 'commentpaged' ); |
335 $redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] ); |
351 $redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] ); |
336 } |
352 } |
337 |
353 |
338 $redirect['path'] = user_trailingslashit( preg_replace('|/' . preg_quote( $wp_rewrite->index, '|' ) . '/?$|', '/', $redirect['path']) ); // strip off trailing /index.php/ |
354 $redirect['path'] = user_trailingslashit( preg_replace( '|/' . preg_quote( $wp_rewrite->index, '|' ) . '/?$|', '/', $redirect['path'] ) ); // strip off trailing /index.php/ |
339 if ( !empty( $addl_path ) && $wp_rewrite->using_index_permalinks() && strpos($redirect['path'], '/' . $wp_rewrite->index . '/') === false ) |
355 if ( ! empty( $addl_path ) && $wp_rewrite->using_index_permalinks() && strpos( $redirect['path'], '/' . $wp_rewrite->index . '/' ) === false ) { |
340 $redirect['path'] = trailingslashit($redirect['path']) . $wp_rewrite->index . '/'; |
356 $redirect['path'] = trailingslashit( $redirect['path'] ) . $wp_rewrite->index . '/'; |
341 if ( !empty( $addl_path ) ) |
357 } |
342 $redirect['path'] = trailingslashit($redirect['path']) . $addl_path; |
358 if ( ! empty( $addl_path ) ) { |
|
359 $redirect['path'] = trailingslashit( $redirect['path'] ) . $addl_path; |
|
360 } |
343 $redirect_url = $redirect['scheme'] . '://' . $redirect['host'] . $redirect['path']; |
361 $redirect_url = $redirect['scheme'] . '://' . $redirect['host'] . $redirect['path']; |
344 } |
362 } |
345 |
363 |
346 if ( 'wp-register.php' == basename( $redirect['path'] ) ) { |
364 if ( 'wp-register.php' == basename( $redirect['path'] ) ) { |
347 if ( is_multisite() ) { |
365 if ( is_multisite() ) { |
356 } |
374 } |
357 } |
375 } |
358 |
376 |
359 // tack on any additional query vars |
377 // tack on any additional query vars |
360 $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] ); |
378 $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] ); |
361 if ( $redirect_url && !empty($redirect['query']) ) { |
379 if ( $redirect_url && ! empty( $redirect['query'] ) ) { |
362 parse_str( $redirect['query'], $_parsed_query ); |
380 parse_str( $redirect['query'], $_parsed_query ); |
363 $redirect = @parse_url($redirect_url); |
381 $redirect = @parse_url( $redirect_url ); |
364 |
382 |
365 if ( ! empty( $_parsed_query['name'] ) && ! empty( $redirect['query'] ) ) { |
383 if ( ! empty( $_parsed_query['name'] ) && ! empty( $redirect['query'] ) ) { |
366 parse_str( $redirect['query'], $_parsed_redirect_query ); |
384 parse_str( $redirect['query'], $_parsed_redirect_query ); |
367 |
385 |
368 if ( empty( $_parsed_redirect_query['name'] ) ) |
386 if ( empty( $_parsed_redirect_query['name'] ) ) { |
369 unset( $_parsed_query['name'] ); |
387 unset( $_parsed_query['name'] ); |
370 } |
388 } |
371 |
389 } |
372 $_parsed_query = rawurlencode_deep( $_parsed_query ); |
390 |
373 $redirect_url = add_query_arg( $_parsed_query, $redirect_url ); |
391 $_parsed_query = array_combine( |
374 } |
392 rawurlencode_deep( array_keys( $_parsed_query ) ), |
375 |
393 rawurlencode_deep( array_values( $_parsed_query ) ) |
376 if ( $redirect_url ) |
394 ); |
377 $redirect = @parse_url($redirect_url); |
395 $redirect_url = add_query_arg( $_parsed_query, $redirect_url ); |
|
396 } |
|
397 |
|
398 if ( $redirect_url ) { |
|
399 $redirect = @parse_url( $redirect_url ); |
|
400 } |
378 |
401 |
379 // www.example.com vs example.com |
402 // www.example.com vs example.com |
380 $user_home = @parse_url(home_url()); |
403 $user_home = @parse_url( home_url() ); |
381 if ( !empty($user_home['host']) ) |
404 if ( ! empty( $user_home['host'] ) ) { |
382 $redirect['host'] = $user_home['host']; |
405 $redirect['host'] = $user_home['host']; |
383 if ( empty($user_home['path']) ) |
406 } |
|
407 if ( empty( $user_home['path'] ) ) { |
384 $user_home['path'] = '/'; |
408 $user_home['path'] = '/'; |
|
409 } |
385 |
410 |
386 // Handle ports |
411 // Handle ports |
387 if ( !empty($user_home['port']) ) |
412 if ( ! empty( $user_home['port'] ) ) { |
388 $redirect['port'] = $user_home['port']; |
413 $redirect['port'] = $user_home['port']; |
389 else |
414 } else { |
390 unset($redirect['port']); |
415 unset( $redirect['port'] ); |
|
416 } |
391 |
417 |
392 // trailing /index.php |
418 // trailing /index.php |
393 $redirect['path'] = preg_replace('|/' . preg_quote( $wp_rewrite->index, '|' ) . '/*?$|', '/', $redirect['path']); |
419 $redirect['path'] = preg_replace( '|/' . preg_quote( $wp_rewrite->index, '|' ) . '/*?$|', '/', $redirect['path'] ); |
394 |
420 |
395 $punctuation_pattern = implode( '|', array_map( 'preg_quote', array( |
421 $punctuation_pattern = implode( |
396 ' ', '%20', // space |
422 '|', |
397 '!', '%21', // exclamation mark |
423 array_map( |
398 '"', '%22', // double quote |
424 'preg_quote', |
399 "'", '%27', // single quote |
425 array( |
400 '(', '%28', // opening bracket |
426 ' ', |
401 ')', '%29', // closing bracket |
427 '%20', // space |
402 ',', '%2C', // comma |
428 '!', |
403 '.', '%2E', // period |
429 '%21', // exclamation mark |
404 ';', '%3B', // semicolon |
430 '"', |
405 '{', '%7B', // opening curly bracket |
431 '%22', // double quote |
406 '}', '%7D', // closing curly bracket |
432 "'", |
407 '%E2%80%9C', // opening curly quote |
433 '%27', // single quote |
408 '%E2%80%9D', // closing curly quote |
434 '(', |
409 ) ) ); |
435 '%28', // opening bracket |
|
436 ')', |
|
437 '%29', // closing bracket |
|
438 ',', |
|
439 '%2C', // comma |
|
440 '.', |
|
441 '%2E', // period |
|
442 ';', |
|
443 '%3B', // semicolon |
|
444 '{', |
|
445 '%7B', // opening curly bracket |
|
446 '}', |
|
447 '%7D', // closing curly bracket |
|
448 '%E2%80%9C', // opening curly quote |
|
449 '%E2%80%9D', // closing curly quote |
|
450 ) |
|
451 ) |
|
452 ); |
410 |
453 |
411 // Remove trailing spaces and end punctuation from the path. |
454 // Remove trailing spaces and end punctuation from the path. |
412 $redirect['path'] = preg_replace( "#($punctuation_pattern)+$#", '', $redirect['path'] ); |
455 $redirect['path'] = preg_replace( "#($punctuation_pattern)+$#", '', $redirect['path'] ); |
413 |
456 |
414 if ( !empty( $redirect['query'] ) ) { |
457 if ( ! empty( $redirect['query'] ) ) { |
415 // Remove trailing spaces and end punctuation from certain terminating query string args. |
458 // Remove trailing spaces and end punctuation from certain terminating query string args. |
416 $redirect['query'] = preg_replace( "#((p|page_id|cat|tag)=[^&]*?)($punctuation_pattern)+$#", '$1', $redirect['query'] ); |
459 $redirect['query'] = preg_replace( "#((p|page_id|cat|tag)=[^&]*?)($punctuation_pattern)+$#", '$1', $redirect['query'] ); |
417 |
460 |
418 // Clean up empty query strings |
461 // Clean up empty query strings |
419 $redirect['query'] = trim(preg_replace( '#(^|&)(p|page_id|cat|tag)=?(&|$)#', '&', $redirect['query']), '&'); |
462 $redirect['query'] = trim( preg_replace( '#(^|&)(p|page_id|cat|tag)=?(&|$)#', '&', $redirect['query'] ), '&' ); |
420 |
463 |
421 // Redirect obsolete feeds |
464 // Redirect obsolete feeds |
422 $redirect['query'] = preg_replace( '#(^|&)feed=rss(&|$)#', '$1feed=rss2$2', $redirect['query'] ); |
465 $redirect['query'] = preg_replace( '#(^|&)feed=rss(&|$)#', '$1feed=rss2$2', $redirect['query'] ); |
423 |
466 |
424 // Remove redundant leading ampersands |
467 // Remove redundant leading ampersands |
425 $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] ); |
468 $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] ); |
426 } |
469 } |
427 |
470 |
428 // strip /index.php/ when we're not using PATHINFO permalinks |
471 // strip /index.php/ when we're not using PATHINFO permalinks |
429 if ( !$wp_rewrite->using_index_permalinks() ) |
472 if ( ! $wp_rewrite->using_index_permalinks() ) { |
430 $redirect['path'] = str_replace( '/' . $wp_rewrite->index . '/', '/', $redirect['path'] ); |
473 $redirect['path'] = str_replace( '/' . $wp_rewrite->index . '/', '/', $redirect['path'] ); |
|
474 } |
431 |
475 |
432 // trailing slashes |
476 // trailing slashes |
433 if ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() && !is_404() && (!is_front_page() || ( is_front_page() && (get_query_var('paged') > 1) ) ) ) { |
477 if ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() && ! is_404() && ( ! is_front_page() || ( is_front_page() && ( get_query_var( 'paged' ) > 1 ) ) ) ) { |
434 $user_ts_type = ''; |
478 $user_ts_type = ''; |
435 if ( get_query_var('paged') > 0 ) { |
479 if ( get_query_var( 'paged' ) > 0 ) { |
436 $user_ts_type = 'paged'; |
480 $user_ts_type = 'paged'; |
437 } else { |
481 } else { |
438 foreach ( array('single', 'category', 'page', 'day', 'month', 'year', 'home') as $type ) { |
482 foreach ( array( 'single', 'category', 'page', 'day', 'month', 'year', 'home' ) as $type ) { |
439 $func = 'is_' . $type; |
483 $func = 'is_' . $type; |
440 if ( call_user_func($func) ) { |
484 if ( call_user_func( $func ) ) { |
441 $user_ts_type = $type; |
485 $user_ts_type = $type; |
442 break; |
486 break; |
443 } |
487 } |
444 } |
488 } |
445 } |
489 } |
446 $redirect['path'] = user_trailingslashit($redirect['path'], $user_ts_type); |
490 $redirect['path'] = user_trailingslashit( $redirect['path'], $user_ts_type ); |
447 } elseif ( is_front_page() ) { |
491 } elseif ( is_front_page() ) { |
448 $redirect['path'] = trailingslashit($redirect['path']); |
492 $redirect['path'] = trailingslashit( $redirect['path'] ); |
449 } |
493 } |
450 |
494 |
451 // Strip multiple slashes out of the URL |
495 // Strip multiple slashes out of the URL |
452 if ( strpos($redirect['path'], '//') > -1 ) |
496 if ( strpos( $redirect['path'], '//' ) > -1 ) { |
453 $redirect['path'] = preg_replace('|/+|', '/', $redirect['path']); |
497 $redirect['path'] = preg_replace( '|/+|', '/', $redirect['path'] ); |
|
498 } |
454 |
499 |
455 // Always trailing slash the Front Page URL |
500 // Always trailing slash the Front Page URL |
456 if ( trailingslashit( $redirect['path'] ) == trailingslashit( $user_home['path'] ) ) |
501 if ( trailingslashit( $redirect['path'] ) == trailingslashit( $user_home['path'] ) ) { |
457 $redirect['path'] = trailingslashit($redirect['path']); |
502 $redirect['path'] = trailingslashit( $redirect['path'] ); |
|
503 } |
458 |
504 |
459 // Ignore differences in host capitalization, as this can lead to infinite redirects |
505 // Ignore differences in host capitalization, as this can lead to infinite redirects |
460 // Only redirect no-www <=> yes-www |
506 // Only redirect no-www <=> yes-www |
461 if ( strtolower($original['host']) == strtolower($redirect['host']) || |
507 if ( strtolower( $original['host'] ) == strtolower( $redirect['host'] ) || |
462 ( strtolower($original['host']) != 'www.' . strtolower($redirect['host']) && 'www.' . strtolower($original['host']) != strtolower($redirect['host']) ) ) |
508 ( strtolower( $original['host'] ) != 'www.' . strtolower( $redirect['host'] ) && 'www.' . strtolower( $original['host'] ) != strtolower( $redirect['host'] ) ) ) { |
463 $redirect['host'] = $original['host']; |
509 $redirect['host'] = $original['host']; |
|
510 } |
464 |
511 |
465 $compare_original = array( $original['host'], $original['path'] ); |
512 $compare_original = array( $original['host'], $original['path'] ); |
466 |
513 |
467 if ( !empty( $original['port'] ) ) |
514 if ( ! empty( $original['port'] ) ) { |
468 $compare_original[] = $original['port']; |
515 $compare_original[] = $original['port']; |
469 |
516 } |
470 if ( !empty( $original['query'] ) ) |
517 |
|
518 if ( ! empty( $original['query'] ) ) { |
471 $compare_original[] = $original['query']; |
519 $compare_original[] = $original['query']; |
|
520 } |
472 |
521 |
473 $compare_redirect = array( $redirect['host'], $redirect['path'] ); |
522 $compare_redirect = array( $redirect['host'], $redirect['path'] ); |
474 |
523 |
475 if ( !empty( $redirect['port'] ) ) |
524 if ( ! empty( $redirect['port'] ) ) { |
476 $compare_redirect[] = $redirect['port']; |
525 $compare_redirect[] = $redirect['port']; |
477 |
526 } |
478 if ( !empty( $redirect['query'] ) ) |
527 |
|
528 if ( ! empty( $redirect['query'] ) ) { |
479 $compare_redirect[] = $redirect['query']; |
529 $compare_redirect[] = $redirect['query']; |
|
530 } |
480 |
531 |
481 if ( $compare_original !== $compare_redirect ) { |
532 if ( $compare_original !== $compare_redirect ) { |
482 $redirect_url = $redirect['scheme'] . '://' . $redirect['host']; |
533 $redirect_url = $redirect['scheme'] . '://' . $redirect['host']; |
483 if ( !empty($redirect['port']) ) |
534 if ( ! empty( $redirect['port'] ) ) { |
484 $redirect_url .= ':' . $redirect['port']; |
535 $redirect_url .= ':' . $redirect['port']; |
|
536 } |
485 $redirect_url .= $redirect['path']; |
537 $redirect_url .= $redirect['path']; |
486 if ( !empty($redirect['query']) ) |
538 if ( ! empty( $redirect['query'] ) ) { |
487 $redirect_url .= '?' . $redirect['query']; |
539 $redirect_url .= '?' . $redirect['query']; |
|
540 } |
488 } |
541 } |
489 |
542 |
490 if ( ! $redirect_url || $redirect_url == $requested_url ) { |
543 if ( ! $redirect_url || $redirect_url == $requested_url ) { |
491 return; |
544 return; |
492 } |
545 } |
493 |
546 |
494 // Hex encoded octets are case-insensitive. |
547 // Hex encoded octets are case-insensitive. |
495 if ( false !== strpos($requested_url, '%') ) { |
548 if ( false !== strpos( $requested_url, '%' ) ) { |
496 if ( !function_exists('lowercase_octets') ) { |
549 if ( ! function_exists( 'lowercase_octets' ) ) { |
497 /** |
550 /** |
498 * Converts the first hex-encoded octet match to lowercase. |
551 * Converts the first hex-encoded octet match to lowercase. |
499 * |
552 * |
500 * @since 3.1.0 |
553 * @since 3.1.0 |
501 * @ignore |
554 * @ignore |
502 * |
555 * |
503 * @param array $matches Hex-encoded octet matches for the requested URL. |
556 * @param array $matches Hex-encoded octet matches for the requested URL. |
504 * @return string Lowercased version of the first match. |
557 * @return string Lowercased version of the first match. |
505 */ |
558 */ |
506 function lowercase_octets($matches) { |
559 function lowercase_octets( $matches ) { |
507 return strtolower( $matches[0] ); |
560 return strtolower( $matches[0] ); |
508 } |
561 } |
509 } |
562 } |
510 $requested_url = preg_replace_callback('|%[a-fA-F0-9][a-fA-F0-9]|', 'lowercase_octets', $requested_url); |
563 $requested_url = preg_replace_callback( '|%[a-fA-F0-9][a-fA-F0-9]|', 'lowercase_octets', $requested_url ); |
511 } |
564 } |
512 |
565 |
513 /** |
566 /** |
514 * Filters the canonical redirect URL. |
567 * Filters the canonical redirect URL. |
515 * |
568 * |