61 |
59 |
62 $redirect = $original; |
60 $redirect = $original; |
63 $redirect_url = false; |
61 $redirect_url = false; |
64 |
62 |
65 // Notice fixing |
63 // Notice fixing |
66 if ( !isset($redirect['path']) ) $redirect['path'] = ''; |
64 if ( !isset($redirect['path']) ) |
67 if ( !isset($redirect['query']) ) $redirect['query'] = ''; |
65 $redirect['path'] = ''; |
|
66 if ( !isset($redirect['query']) ) |
|
67 $redirect['query'] = ''; |
|
68 |
|
69 if ( is_feed() && ( $id = get_query_var( 'p' ) ) ) { |
|
70 if ( $redirect_url = get_post_comments_feed_link( $id, get_query_var( 'feed' ) ) ) { |
|
71 $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type', 'feed'), $redirect_url ); |
|
72 $redirect['path'] = parse_url( $redirect_url, PHP_URL_PATH ); |
|
73 } |
|
74 } |
68 |
75 |
69 if ( is_singular() && 1 > $wp_query->post_count && ($id = get_query_var('p')) ) { |
76 if ( is_singular() && 1 > $wp_query->post_count && ($id = get_query_var('p')) ) { |
70 |
77 |
71 $vars = $wpdb->get_results( $wpdb->prepare("SELECT post_type, post_parent FROM $wpdb->posts WHERE ID = %d", $id) ); |
78 $vars = $wpdb->get_results( $wpdb->prepare("SELECT post_type, post_parent FROM $wpdb->posts WHERE ID = %d", $id) ); |
72 |
79 |
73 if ( isset($vars[0]) && $vars = $vars[0] ) { |
80 if ( isset($vars[0]) && $vars = $vars[0] ) { |
74 if ( 'revision' == $vars->post_type && $vars->post_parent > 0 ) |
81 if ( 'revision' == $vars->post_type && $vars->post_parent > 0 ) |
75 $id = $vars->post_parent; |
82 $id = $vars->post_parent; |
76 |
83 |
77 if ( $redirect_url = get_permalink($id) ) |
84 if ( $redirect_url = get_permalink($id) ) |
78 $redirect['query'] = remove_query_arg(array('p', 'page_id', 'attachment_id'), $redirect['query']); |
85 $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url ); |
79 } |
86 } |
80 } |
87 } |
81 |
88 |
82 // These tests give us a WP-generated permalink |
89 // These tests give us a WP-generated permalink |
83 if ( is_404() ) { |
90 if ( is_404() ) { |
84 $redirect_url = redirect_guess_404_permalink(); |
91 |
|
92 // Redirect ?page_id, ?p=, ?attachment_id= to their respective url's |
|
93 $id = max( get_query_var('p'), get_query_var('page_id'), get_query_var('attachment_id') ); |
|
94 if ( $id && $redirect_post = get_post($id) ) { |
|
95 $post_type_obj = get_post_type_object($redirect_post->post_type); |
|
96 if ( $post_type_obj->public ) { |
|
97 $redirect_url = get_permalink($redirect_post); |
|
98 $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url ); |
|
99 } |
|
100 } |
|
101 |
|
102 if ( ! $redirect_url ) { |
|
103 if ( $redirect_url = redirect_guess_404_permalink() ) { |
|
104 $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 ); |
|
105 } |
|
106 } |
|
107 |
85 } elseif ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) { |
108 } elseif ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) { |
86 // rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101 |
109 // rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101 |
87 if ( is_attachment() && !empty($_GET['attachment_id']) && ! $redirect_url ) { |
110 if ( is_attachment() && !empty($_GET['attachment_id']) && ! $redirect_url ) { |
88 if ( $redirect_url = get_attachment_link(get_query_var('attachment_id')) ) |
111 if ( $redirect_url = get_attachment_link(get_query_var('attachment_id')) ) |
89 $redirect['query'] = remove_query_arg('attachment_id', $redirect['query']); |
112 $redirect['query'] = remove_query_arg('attachment_id', $redirect['query']); |
90 } elseif ( is_single() && !empty($_GET['p']) && ! $redirect_url ) { |
113 } elseif ( is_single() && !empty($_GET['p']) && ! $redirect_url ) { |
91 if ( $redirect_url = get_permalink(get_query_var('p')) ) |
114 if ( $redirect_url = get_permalink(get_query_var('p')) ) |
92 $redirect['query'] = remove_query_arg('p', $redirect['query']); |
115 $redirect['query'] = remove_query_arg(array('p', 'post_type'), $redirect['query']); |
93 if ( get_query_var( 'page' ) ) { |
|
94 $redirect_url = trailingslashit( $redirect_url ) . user_trailingslashit( get_query_var( 'page' ), 'single_paged' ); |
|
95 $redirect['query'] = remove_query_arg( 'page', $redirect['query'] ); |
|
96 } |
|
97 } elseif ( is_single() && !empty($_GET['name']) && ! $redirect_url ) { |
116 } elseif ( is_single() && !empty($_GET['name']) && ! $redirect_url ) { |
98 if ( $redirect_url = get_permalink( $wp_query->get_queried_object_id() ) ) |
117 if ( $redirect_url = get_permalink( $wp_query->get_queried_object_id() ) ) |
99 $redirect['query'] = remove_query_arg('name', $redirect['query']); |
118 $redirect['query'] = remove_query_arg('name', $redirect['query']); |
100 } elseif ( is_page() && !empty($_GET['page_id']) && ! $redirect_url ) { |
119 } elseif ( is_page() && !empty($_GET['page_id']) && ! $redirect_url ) { |
101 if ( $redirect_url = get_permalink(get_query_var('page_id')) ) |
120 if ( $redirect_url = get_permalink(get_query_var('page_id')) ) |
|
121 $redirect['query'] = remove_query_arg('page_id', $redirect['query']); |
|
122 } elseif ( is_page() && !is_feed() && isset($wp_query->queried_object) && 'page' == get_option('show_on_front') && $wp_query->queried_object->ID == get_option('page_on_front') && ! $redirect_url ) { |
|
123 $redirect_url = home_url('/'); |
|
124 } 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 ) { |
|
125 if ( $redirect_url = get_permalink(get_option('page_for_posts')) ) |
102 $redirect['query'] = remove_query_arg('page_id', $redirect['query']); |
126 $redirect['query'] = remove_query_arg('page_id', $redirect['query']); |
103 } elseif ( !empty($_GET['m']) && ( is_year() || is_month() || is_day() ) ) { |
127 } elseif ( !empty($_GET['m']) && ( is_year() || is_month() || is_day() ) ) { |
104 $m = get_query_var('m'); |
128 $m = get_query_var('m'); |
105 switch ( strlen($m) ) { |
129 switch ( strlen($m) ) { |
106 case 4: // Yearly |
130 case 4: // Yearly |
123 if ( $redirect_url = get_month_link(get_query_var('year'), get_query_var('monthnum')) ) |
147 if ( $redirect_url = get_month_link(get_query_var('year'), get_query_var('monthnum')) ) |
124 $redirect['query'] = remove_query_arg(array('year', 'monthnum'), $redirect['query']); |
148 $redirect['query'] = remove_query_arg(array('year', 'monthnum'), $redirect['query']); |
125 } elseif ( is_year() && !empty($_GET['year']) ) { |
149 } elseif ( is_year() && !empty($_GET['year']) ) { |
126 if ( $redirect_url = get_year_link(get_query_var('year')) ) |
150 if ( $redirect_url = get_year_link(get_query_var('year')) ) |
127 $redirect['query'] = remove_query_arg('year', $redirect['query']); |
151 $redirect['query'] = remove_query_arg('year', $redirect['query']); |
128 } elseif ( is_category() && !empty($_GET['cat']) && preg_match( '|^[0-9]+$|', $_GET['cat'] ) ) { |
|
129 if ( $redirect_url = get_category_link(get_query_var('cat')) ) |
|
130 $redirect['query'] = remove_query_arg('cat', $redirect['query']); |
|
131 } elseif ( is_author() && !empty($_GET['author']) && preg_match( '|^[0-9]+$|', $_GET['author'] ) ) { |
152 } elseif ( is_author() && !empty($_GET['author']) && preg_match( '|^[0-9]+$|', $_GET['author'] ) ) { |
132 $author = get_userdata(get_query_var('author')); |
153 $author = get_userdata(get_query_var('author')); |
133 if ( false !== $author && $redirect_url = get_author_posts_url($author->ID, $author->user_nicename) ) |
154 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 ) ) ) { |
134 $redirect['query'] = remove_query_arg('author', $redirect['author']); |
155 if ( $redirect_url = get_author_posts_url($author->ID, $author->user_nicename) ) |
135 } |
156 $redirect['query'] = remove_query_arg('author', $redirect['query']); |
136 |
157 } |
137 // paging and feeds |
158 } elseif ( is_category() || is_tag() || is_tax() ) { // Terms (Tags/categories) |
|
159 |
|
160 $term_count = 0; |
|
161 foreach ( $wp_query->tax_query->queries as $tax_query ) |
|
162 $term_count += count( $tax_query['terms'] ); |
|
163 |
|
164 $obj = $wp_query->get_queried_object(); |
|
165 if ( $term_count <= 1 && !empty($obj->term_id) && ( $tax_url = get_term_link((int)$obj->term_id, $obj->taxonomy) ) && !is_wp_error($tax_url) ) { |
|
166 if ( !empty($redirect['query']) ) { |
|
167 // Strip taxonomy query vars off the url. |
|
168 $qv_remove = array( 'term', 'taxonomy'); |
|
169 if ( is_category() ) { |
|
170 $qv_remove[] = 'category_name'; |
|
171 $qv_remove[] = 'cat'; |
|
172 } elseif ( is_tag() ) { |
|
173 $qv_remove[] = 'tag'; |
|
174 $qv_remove[] = 'tag_id'; |
|
175 } else { // Custom taxonomies will have a custom query var, remove those too: |
|
176 $tax_obj = get_taxonomy( $obj->taxonomy ); |
|
177 if ( false !== $tax_obj->query_var ) |
|
178 $qv_remove[] = $tax_obj->query_var; |
|
179 } |
|
180 |
|
181 $rewrite_vars = array_diff( array_keys($wp_query->query), array_keys($_GET) ); |
|
182 |
|
183 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 |
|
184 $redirect['query'] = remove_query_arg($qv_remove, $redirect['query']); //Remove all of the per-tax qv's |
|
185 |
|
186 // Create the destination url for this taxonomy |
|
187 $tax_url = parse_url($tax_url); |
|
188 if ( ! empty($tax_url['query']) ) { // Taxonomy accessible via ?taxonomy=..&term=.. or any custom qv.. |
|
189 parse_str($tax_url['query'], $query_vars); |
|
190 $redirect['query'] = add_query_arg($query_vars, $redirect['query']); |
|
191 } else { // Taxonomy is accessible via a "pretty-URL" |
|
192 $redirect['path'] = $tax_url['path']; |
|
193 } |
|
194 |
|
195 } else { // Some query vars are set via $_GET. Unset those from $_GET that exist via the rewrite |
|
196 foreach ( $qv_remove as $_qv ) { |
|
197 if ( isset($rewrite_vars[$_qv]) ) |
|
198 $redirect['query'] = remove_query_arg($_qv, $redirect['query']); |
|
199 } |
|
200 } |
|
201 } |
|
202 |
|
203 } |
|
204 } elseif ( is_single() && strpos($wp_rewrite->permalink_structure, '%category%') !== false && $cat = get_query_var( 'category_name' ) ) { |
|
205 $category = get_category_by_path( $cat ); |
|
206 $post_terms = wp_get_object_terms($wp_query->get_queried_object_id(), 'category', array('fields' => 'tt_ids')); |
|
207 if ( (!$category || is_wp_error($category)) || ( !is_wp_error($post_terms) && !empty($post_terms) && !in_array($category->term_taxonomy_id, $post_terms) ) ) |
|
208 $redirect_url = get_permalink($wp_query->get_queried_object_id()); |
|
209 } |
|
210 |
|
211 // Post Paging |
|
212 if ( is_singular() && ! is_front_page() && get_query_var('page') ) { |
|
213 if ( !$redirect_url ) |
|
214 $redirect_url = get_permalink( get_queried_object_id() ); |
|
215 $redirect_url = trailingslashit( $redirect_url ) . user_trailingslashit( get_query_var( 'page' ), 'single_paged' ); |
|
216 $redirect['query'] = remove_query_arg( 'page', $redirect['query'] ); |
|
217 } |
|
218 |
|
219 // paging and feeds |
138 if ( get_query_var('paged') || is_feed() || get_query_var('cpage') ) { |
220 if ( get_query_var('paged') || is_feed() || get_query_var('cpage') ) { |
139 if ( !$redirect_url ) |
221 while ( preg_match( "#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", $redirect['path'] ) || preg_match( '#/(comments/?)?(feed|rss|rdf|atom|rss2)(/+)?$#', $redirect['path'] ) || preg_match( '#/comment-page-[0-9]+(/+)?$#', $redirect['path'] ) ) { |
140 $redirect_url = $requested_url; |
|
141 $paged_redirect = @parse_url($redirect_url); |
|
142 while ( preg_match( '#/page/[0-9]+?(/+)?$#', $paged_redirect['path'] ) || preg_match( '#/(comments/?)?(feed|rss|rdf|atom|rss2)(/+)?$#', $paged_redirect['path'] ) || preg_match( '#/comment-page-[0-9]+(/+)?$#', $paged_redirect['path'] ) ) { |
|
143 // Strip off paging and feed |
222 // Strip off paging and feed |
144 $paged_redirect['path'] = preg_replace('#/page/[0-9]+?(/+)?$#', '/', $paged_redirect['path']); // strip off any existing paging |
223 $redirect['path'] = preg_replace("#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", '/', $redirect['path']); // strip off any existing paging |
145 $paged_redirect['path'] = preg_replace('#/(comments/?)?(feed|rss2?|rdf|atom)(/+|$)#', '/', $paged_redirect['path']); // strip off feed endings |
224 $redirect['path'] = preg_replace('#/(comments/?)?(feed|rss2?|rdf|atom)(/+|$)#', '/', $redirect['path']); // strip off feed endings |
146 $paged_redirect['path'] = preg_replace('#/comment-page-[0-9]+?(/+)?$#', '/', $paged_redirect['path']); // strip off any existing comment paging |
225 $redirect['path'] = preg_replace('#/comment-page-[0-9]+?(/+)?$#', '/', $redirect['path']); // strip off any existing comment paging |
147 } |
226 } |
148 |
227 |
149 $addl_path = ''; |
228 $addl_path = ''; |
150 if ( is_feed() ) { |
229 if ( is_feed() && in_array( get_query_var('feed'), $wp_rewrite->feeds ) ) { |
151 $addl_path = !empty( $addl_path ) ? trailingslashit($addl_path) : ''; |
230 $addl_path = !empty( $addl_path ) ? trailingslashit($addl_path) : ''; |
152 if ( get_query_var( 'withcomments' ) ) |
231 if ( !is_singular() && get_query_var( 'withcomments' ) ) |
153 $addl_path .= 'comments/'; |
232 $addl_path .= 'comments/'; |
154 $addl_path .= user_trailingslashit( 'feed/' . ( ( 'rss2' == get_query_var('feed') || 'feed' == get_query_var('feed') ) ? '' : get_query_var('feed') ), 'feed' ); |
233 if ( ( 'rss' == get_default_feed() && 'feed' == get_query_var('feed') ) || 'rss' == get_query_var('feed') ) |
|
234 $addl_path .= user_trailingslashit( 'feed/' . ( ( get_default_feed() == 'rss2' ) ? '' : 'rss2' ), 'feed' ); |
|
235 else |
|
236 $addl_path .= user_trailingslashit( 'feed/' . ( ( get_default_feed() == get_query_var('feed') || 'feed' == get_query_var('feed') ) ? '' : get_query_var('feed') ), 'feed' ); |
155 $redirect['query'] = remove_query_arg( 'feed', $redirect['query'] ); |
237 $redirect['query'] = remove_query_arg( 'feed', $redirect['query'] ); |
|
238 } elseif ( is_feed() && 'old' == get_query_var('feed') ) { |
|
239 $old_feed_files = array( |
|
240 'wp-atom.php' => 'atom', |
|
241 'wp-commentsrss2.php' => 'comments_rss2', |
|
242 'wp-feed.php' => get_default_feed(), |
|
243 'wp-rdf.php' => 'rdf', |
|
244 'wp-rss.php' => 'rss2', |
|
245 'wp-rss2.php' => 'rss2', |
|
246 ); |
|
247 if ( isset( $old_feed_files[ basename( $redirect['path'] ) ] ) ) { |
|
248 $redirect_url = get_feed_link( $old_feed_files[ basename( $redirect['path'] ) ] ); |
|
249 wp_redirect( $redirect_url, 301 ); |
|
250 die(); |
|
251 } |
156 } |
252 } |
157 |
253 |
158 if ( get_query_var('paged') > 0 ) { |
254 if ( get_query_var('paged') > 0 ) { |
159 $paged = get_query_var('paged'); |
255 $paged = get_query_var('paged'); |
160 $redirect['query'] = remove_query_arg( 'paged', $redirect['query'] ); |
256 $redirect['query'] = remove_query_arg( 'paged', $redirect['query'] ); |
161 if ( !is_feed() ) { |
257 if ( !is_feed() ) { |
162 if ( $paged > 1 && !is_single() ) { |
258 if ( $paged > 1 && !is_single() ) { |
163 $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit("page/$paged", 'paged'); |
259 $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit("$wp_rewrite->pagination_base/$paged", 'paged'); |
164 } elseif ( !is_single() ) { |
260 } elseif ( !is_single() ) { |
165 $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit($paged_redirect['path'], 'paged'); |
261 $addl_path = !empty( $addl_path ) ? trailingslashit($addl_path) : ''; |
166 } |
262 } |
167 } elseif ( $paged > 1 ) { |
263 } elseif ( $paged > 1 ) { |
168 $redirect['query'] = add_query_arg( 'paged', $paged, $redirect['query'] ); |
264 $redirect['query'] = add_query_arg( 'paged', $paged, $redirect['query'] ); |
169 } |
265 } |
170 } |
266 } |
172 if ( get_option('page_comments') && ( ( 'newest' == get_option('default_comments_page') && get_query_var('cpage') > 0 ) || ( 'newest' != get_option('default_comments_page') && get_query_var('cpage') > 1 ) ) ) { |
268 if ( get_option('page_comments') && ( ( 'newest' == get_option('default_comments_page') && get_query_var('cpage') > 0 ) || ( 'newest' != get_option('default_comments_page') && get_query_var('cpage') > 1 ) ) ) { |
173 $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit( 'comment-page-' . get_query_var('cpage'), 'commentpaged' ); |
269 $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit( 'comment-page-' . get_query_var('cpage'), 'commentpaged' ); |
174 $redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] ); |
270 $redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] ); |
175 } |
271 } |
176 |
272 |
177 $paged_redirect['path'] = user_trailingslashit( preg_replace('|/index.php/?$|', '/', $paged_redirect['path']) ); // strip off trailing /index.php/ |
273 $redirect['path'] = user_trailingslashit( preg_replace('|/index.php/?$|', '/', $redirect['path']) ); // strip off trailing /index.php/ |
178 if ( !empty( $addl_path ) && $wp_rewrite->using_index_permalinks() && strpos($paged_redirect['path'], '/index.php/') === false ) |
274 if ( !empty( $addl_path ) && $wp_rewrite->using_index_permalinks() && strpos($redirect['path'], '/index.php/') === false ) |
179 $paged_redirect['path'] = trailingslashit($paged_redirect['path']) . 'index.php/'; |
275 $redirect['path'] = trailingslashit($redirect['path']) . 'index.php/'; |
180 if ( !empty( $addl_path ) ) |
276 if ( !empty( $addl_path ) ) |
181 $paged_redirect['path'] = trailingslashit($paged_redirect['path']) . $addl_path; |
277 $redirect['path'] = trailingslashit($redirect['path']) . $addl_path; |
182 $redirect_url = $paged_redirect['scheme'] . '://' . $paged_redirect['host'] . $paged_redirect['path']; |
278 $redirect_url = $redirect['scheme'] . '://' . $redirect['host'] . $redirect['path']; |
183 $redirect['path'] = $paged_redirect['path']; |
279 } |
|
280 |
|
281 if ( 'wp-register.php' == basename( $redirect['path'] ) ) { |
|
282 if ( is_multisite() ) |
|
283 $redirect_url = apply_filters( 'wp_signup_location', site_url( 'wp-signup.php' ) ); |
|
284 else |
|
285 $redirect_url = site_url( 'wp-login.php?action=register' ); |
|
286 wp_redirect( $redirect_url, 301 ); |
|
287 die(); |
184 } |
288 } |
185 } |
289 } |
186 |
290 |
187 // tack on any additional query vars |
291 // tack on any additional query vars |
188 $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] ); |
292 $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] ); |
189 if ( $redirect_url && !empty($redirect['query']) ) { |
293 if ( $redirect_url && !empty($redirect['query']) ) { |
190 if ( strpos($redirect_url, '?') !== false ) |
294 parse_str( $redirect['query'], $_parsed_query ); |
191 $redirect_url .= '&'; |
295 $redirect = @parse_url($redirect_url); |
192 else |
296 |
193 $redirect_url .= '?'; |
297 if ( ! empty( $_parsed_query['name'] ) && ! empty( $redirect['query'] ) ) { |
194 $redirect_url .= $redirect['query']; |
298 parse_str( $redirect['query'], $_parsed_redirect_query ); |
|
299 |
|
300 if ( empty( $_parsed_redirect_query['name'] ) ) |
|
301 unset( $_parsed_query['name'] ); |
|
302 } |
|
303 |
|
304 $_parsed_query = rawurlencode_deep( $_parsed_query ); |
|
305 $redirect_url = add_query_arg( $_parsed_query, $redirect_url ); |
195 } |
306 } |
196 |
307 |
197 if ( $redirect_url ) |
308 if ( $redirect_url ) |
198 $redirect = @parse_url($redirect_url); |
309 $redirect = @parse_url($redirect_url); |
199 |
310 |
200 // www.example.com vs example.com |
311 // www.example.com vs example.com |
201 $user_home = @parse_url(get_option('home')); |
312 $user_home = @parse_url(home_url()); |
202 if ( !empty($user_home['host']) ) |
313 if ( !empty($user_home['host']) ) |
203 $redirect['host'] = $user_home['host']; |
314 $redirect['host'] = $user_home['host']; |
204 if ( empty($user_home['path']) ) |
315 if ( empty($user_home['path']) ) |
205 $user_home['path'] = '/'; |
316 $user_home['path'] = '/'; |
206 |
317 |
308 return $redirect_url; |
436 return $redirect_url; |
309 } |
437 } |
310 } |
438 } |
311 |
439 |
312 /** |
440 /** |
313 * Attempts to guess correct post based on query vars. |
441 * Removes arguments from a query string if they are not present in a URL |
|
442 * DO NOT use this in plugin code. |
|
443 * |
|
444 * @since 3.4 |
|
445 * @access private |
|
446 * |
|
447 * @return string The altered query string |
|
448 */ |
|
449 function _remove_qs_args_if_not_in_url( $query_string, Array $args_to_check, $url ) { |
|
450 $parsed_url = @parse_url( $url ); |
|
451 if ( ! empty( $parsed_url['query'] ) ) { |
|
452 parse_str( $parsed_url['query'], $parsed_query ); |
|
453 foreach ( $args_to_check as $qv ) { |
|
454 if ( !isset( $parsed_query[$qv] ) ) |
|
455 $query_string = remove_query_arg( $qv, $query_string ); |
|
456 } |
|
457 } else { |
|
458 $query_string = remove_query_arg( $args_to_check, $query_string ); |
|
459 } |
|
460 return $query_string; |
|
461 } |
|
462 |
|
463 /** |
|
464 * Attempts to guess the correct URL based on query vars |
314 * |
465 * |
315 * @since 2.3.0 |
466 * @since 2.3.0 |
316 * @uses $wpdb |
467 * @uses $wpdb |
317 * |
468 * |
318 * @return bool|string Returns False, if it can't find post, returns correct |
469 * @return bool|string The correct URL if one is found. False on failure. |
319 * location on success. |
|
320 */ |
470 */ |
321 function redirect_guess_404_permalink() { |
471 function redirect_guess_404_permalink() { |
322 global $wpdb; |
472 global $wpdb, $wp_rewrite; |
323 |
473 |
324 if ( !get_query_var('name') ) |
474 if ( get_query_var('name') ) { |
325 return false; |
475 $where = $wpdb->prepare("post_name LIKE %s", like_escape( get_query_var('name') ) . '%'); |
326 |
476 |
327 $where = $wpdb->prepare("post_name LIKE %s", get_query_var('name') . '%'); |
477 // if any of post_type, year, monthnum, or day are set, use them to refine the query |
328 |
478 if ( get_query_var('post_type') ) |
329 // if any of year, monthnum, or day are set, use them to refine the query |
479 $where .= $wpdb->prepare(" AND post_type = %s", get_query_var('post_type')); |
330 if ( get_query_var('year') ) |
480 else |
331 $where .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var('year')); |
481 $where .= " AND post_type IN ('" . implode( "', '", get_post_types( array( 'public' => true ) ) ) . "')"; |
332 if ( get_query_var('monthnum') ) |
482 |
333 $where .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var('monthnum')); |
483 if ( get_query_var('year') ) |
334 if ( get_query_var('day') ) |
484 $where .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var('year')); |
335 $where .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var('day')); |
485 if ( get_query_var('monthnum') ) |
336 |
486 $where .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var('monthnum')); |
337 $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'"); |
487 if ( get_query_var('day') ) |
338 if ( !$post_id ) |
488 $where .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var('day')); |
339 return false; |
489 |
340 return get_permalink($post_id); |
490 $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'"); |
|
491 if ( ! $post_id ) |
|
492 return false; |
|
493 if ( get_query_var( 'feed' ) ) |
|
494 return get_post_comments_feed_link( $post_id, get_query_var( 'feed' ) ); |
|
495 elseif ( get_query_var( 'page' ) ) |
|
496 return trailingslashit( get_permalink( $post_id ) ) . user_trailingslashit( get_query_var( 'page' ), 'single_paged' ); |
|
497 else |
|
498 return get_permalink( $post_id ); |
|
499 } |
|
500 |
|
501 return false; |
341 } |
502 } |
342 |
503 |
343 add_action('template_redirect', 'redirect_canonical'); |
504 add_action('template_redirect', 'redirect_canonical'); |
344 |
505 |
345 ?> |
506 function wp_redirect_admin_locations() { |
|
507 global $wp_rewrite; |
|
508 if ( ! ( is_404() && $wp_rewrite->using_permalinks() ) ) |
|
509 return; |
|
510 |
|
511 $admins = array( |
|
512 home_url( 'wp-admin', 'relative' ), |
|
513 home_url( 'dashboard', 'relative' ), |
|
514 home_url( 'admin', 'relative' ), |
|
515 site_url( 'dashboard', 'relative' ), |
|
516 site_url( 'admin', 'relative' ), |
|
517 ); |
|
518 if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $admins ) ) { |
|
519 wp_redirect( admin_url() ); |
|
520 exit; |
|
521 } |
|
522 |
|
523 $logins = array( |
|
524 home_url( 'wp-login.php', 'relative' ), |
|
525 home_url( 'login', 'relative' ), |
|
526 site_url( 'login', 'relative' ), |
|
527 ); |
|
528 if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $logins ) ) { |
|
529 wp_redirect( site_url( 'wp-login.php', 'login' ) ); |
|
530 exit; |
|
531 } |
|
532 } |
|
533 |
|
534 add_action( 'template_redirect', 'wp_redirect_admin_locations', 1000 ); |