author | ymh <ymh.work@gmail.com> |
Wed, 21 Sep 2022 18:19:35 +0200 | |
changeset 18 | be944660c56a |
parent 16 | a86126ab1dd4 |
child 19 | 3d72ae0968f4 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* Canonical API to handle WordPress Redirecting |
|
4 |
* |
|
5 |
* Based on "Permalink Redirect" from Scott Yang and "Enforce www. Preference" |
|
6 |
* by Mark Jaquith |
|
7 |
* |
|
8 |
* @package WordPress |
|
9 |
* @since 2.3.0 |
|
10 |
*/ |
|
11 |
||
12 |
/** |
|
13 |
* Redirects incoming links to the proper URL based on the site url. |
|
14 |
* |
|
15 |
* Search engines consider www.somedomain.com and somedomain.com to be two |
|
16 |
* different URLs when they both go to the same location. This SEO enhancement |
|
17 |
* prevents penalty for duplicate content by redirecting all incoming links to |
|
18 |
* one or the other. |
|
19 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
20 |
* Prevents redirection for feeds, trackbacks, searches, and |
0 | 21 |
* admin URLs. Does not redirect on non-pretty-permalink-supporting IIS 7+, |
16 | 22 |
* page/post previews, WP admin, Trackbacks, robots.txt, favicon.ico, searches, |
23 |
* or on POST requests. |
|
0 | 24 |
* |
25 |
* Will also attempt to find the correct link when a user enters a URL that does |
|
26 |
* not exist based on exact WordPress query. Will instead try to parse the URL |
|
27 |
* or query in an attempt to figure the correct page to go to. |
|
28 |
* |
|
29 |
* @since 2.3.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
30 |
* |
16 | 31 |
* @global WP_Rewrite $wp_rewrite WordPress rewrite component. |
32 |
* @global bool $is_IIS |
|
33 |
* @global WP_Query $wp_query WordPress Query object. |
|
34 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
35 |
* @global WP $wp Current WordPress environment instance. |
|
0 | 36 |
* |
37 |
* @param string $requested_url Optional. The URL that was requested, used to |
|
16 | 38 |
* figure if redirect is needed. |
39 |
* @param bool $do_redirect Optional. Redirect to the new URL. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
40 |
* @return string|void The string of the URL, if redirect needed. |
0 | 41 |
*/ |
42 |
function redirect_canonical( $requested_url = null, $do_redirect = true ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
43 |
global $wp_rewrite, $is_IIS, $wp_query, $wpdb, $wp; |
0 | 44 |
|
16 | 45 |
if ( isset( $_SERVER['REQUEST_METHOD'] ) && ! in_array( strtoupper( $_SERVER['REQUEST_METHOD'] ), array( 'GET', 'HEAD' ), true ) ) { |
0 | 46 |
return; |
5 | 47 |
} |
48 |
||
49 |
// If we're not in wp-admin and the post has been published and preview nonce |
|
16 | 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' ) ) ) { |
|
5 | 52 |
if ( ! isset( $_GET['preview_id'] ) |
53 |
|| ! isset( $_GET['preview_nonce'] ) |
|
16 | 54 |
|| ! wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . (int) $_GET['preview_id'] ) |
55 |
) { |
|
5 | 56 |
$wp_query->is_preview = false; |
57 |
} |
|
58 |
} |
|
59 |
||
16 | 60 |
if ( is_admin() || is_search() || is_preview() || is_trackback() || is_favicon() |
61 |
|| ( $is_IIS && ! iis7_supports_permalinks() ) |
|
62 |
) { |
|
5 | 63 |
return; |
64 |
} |
|
0 | 65 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
66 |
if ( ! $requested_url && isset( $_SERVER['HTTP_HOST'] ) ) { |
16 | 67 |
// Build the URL in the address bar. |
0 | 68 |
$requested_url = is_ssl() ? 'https://' : 'http://'; |
69 |
$requested_url .= $_SERVER['HTTP_HOST']; |
|
70 |
$requested_url .= $_SERVER['REQUEST_URI']; |
|
71 |
} |
|
72 |
||
16 | 73 |
$original = parse_url( $requested_url ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
74 |
if ( false === $original ) { |
0 | 75 |
return; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
76 |
} |
0 | 77 |
|
9 | 78 |
$redirect = $original; |
0 | 79 |
$redirect_url = false; |
18 | 80 |
$redirect_obj = false; |
0 | 81 |
|
16 | 82 |
// Notice fixing. |
9 | 83 |
if ( ! isset( $redirect['path'] ) ) { |
0 | 84 |
$redirect['path'] = ''; |
9 | 85 |
} |
86 |
if ( ! isset( $redirect['query'] ) ) { |
|
0 | 87 |
$redirect['query'] = ''; |
9 | 88 |
} |
0 | 89 |
|
16 | 90 |
/* |
91 |
* If the original URL ended with non-breaking spaces, they were almost |
|
92 |
* certainly inserted by accident. Let's remove them, so the reader doesn't |
|
93 |
* see a 404 error with no obvious cause. |
|
94 |
*/ |
|
5 | 95 |
$redirect['path'] = preg_replace( '|(%C2%A0)+$|i', '', $redirect['path'] ); |
96 |
||
16 | 97 |
// It's not a preview, so remove it from URL. |
5 | 98 |
if ( get_query_var( 'preview' ) ) { |
99 |
$redirect['query'] = remove_query_arg( 'preview', $redirect['query'] ); |
|
100 |
} |
|
101 |
||
16 | 102 |
$post_id = get_query_var( 'p' ); |
103 |
||
104 |
if ( is_feed() && $post_id ) { |
|
105 |
$redirect_url = get_post_comments_feed_link( $post_id, get_query_var( 'feed' ) ); |
|
18 | 106 |
$redirect_obj = get_post( $post_id ); |
16 | 107 |
|
108 |
if ( $redirect_url ) { |
|
109 |
$redirect['query'] = _remove_qs_args_if_not_in_url( |
|
110 |
$redirect['query'], |
|
111 |
array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type', 'feed' ), |
|
112 |
$redirect_url |
|
113 |
); |
|
114 |
||
115 |
$redirect['path'] = parse_url( $redirect_url, PHP_URL_PATH ); |
|
0 | 116 |
} |
117 |
} |
|
118 |
||
16 | 119 |
if ( is_singular() && $wp_query->post_count < 1 && $post_id ) { |
0 | 120 |
|
16 | 121 |
$vars = $wpdb->get_results( $wpdb->prepare( "SELECT post_type, post_parent FROM $wpdb->posts WHERE ID = %d", $post_id ) ); |
0 | 122 |
|
16 | 123 |
if ( ! empty( $vars[0] ) ) { |
124 |
$vars = $vars[0]; |
|
125 |
||
126 |
if ( 'revision' === $vars->post_type && $vars->post_parent > 0 ) { |
|
127 |
$post_id = $vars->post_parent; |
|
9 | 128 |
} |
0 | 129 |
|
16 | 130 |
$redirect_url = get_permalink( $post_id ); |
18 | 131 |
$redirect_obj = get_post( $post_id ); |
16 | 132 |
|
133 |
if ( $redirect_url ) { |
|
134 |
$redirect['query'] = _remove_qs_args_if_not_in_url( |
|
135 |
$redirect['query'], |
|
136 |
array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), |
|
137 |
$redirect_url |
|
138 |
); |
|
9 | 139 |
} |
0 | 140 |
} |
141 |
} |
|
142 |
||
16 | 143 |
// These tests give us a WP-generated permalink. |
0 | 144 |
if ( is_404() ) { |
145 |
||
16 | 146 |
// Redirect ?page_id, ?p=, ?attachment_id= to their respective URLs. |
147 |
$post_id = max( get_query_var( 'p' ), get_query_var( 'page_id' ), get_query_var( 'attachment_id' ) ); |
|
148 |
||
149 |
$redirect_post = $post_id ? get_post( $post_id ) : false; |
|
150 |
||
151 |
if ( $redirect_post ) { |
|
9 | 152 |
$post_type_obj = get_post_type_object( $redirect_post->post_type ); |
16 | 153 |
|
18 | 154 |
if ( $post_type_obj && $post_type_obj->public && 'auto-draft' !== $redirect_post->post_status ) { |
16 | 155 |
$redirect_url = get_permalink( $redirect_post ); |
18 | 156 |
$redirect_obj = get_post( $redirect_post ); |
16 | 157 |
|
158 |
$redirect['query'] = _remove_qs_args_if_not_in_url( |
|
159 |
$redirect['query'], |
|
160 |
array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), |
|
161 |
$redirect_url |
|
162 |
); |
|
0 | 163 |
} |
164 |
} |
|
165 |
||
16 | 166 |
$year = get_query_var( 'year' ); |
167 |
$month = get_query_var( 'monthnum' ); |
|
168 |
$day = get_query_var( 'day' ); |
|
169 |
||
170 |
if ( $year && $month && $day ) { |
|
171 |
$date = sprintf( '%04d-%02d-%02d', $year, $month, $day ); |
|
172 |
||
0 | 173 |
if ( ! wp_checkdate( $month, $day, $year, $date ) ) { |
16 | 174 |
$redirect_url = get_month_link( $year, $month ); |
175 |
||
176 |
$redirect['query'] = _remove_qs_args_if_not_in_url( |
|
177 |
$redirect['query'], |
|
178 |
array( 'year', 'monthnum', 'day' ), |
|
179 |
$redirect_url |
|
180 |
); |
|
0 | 181 |
} |
16 | 182 |
} elseif ( $year && $month && $month > 12 ) { |
183 |
$redirect_url = get_year_link( $year ); |
|
184 |
||
185 |
$redirect['query'] = _remove_qs_args_if_not_in_url( |
|
186 |
$redirect['query'], |
|
187 |
array( 'year', 'monthnum' ), |
|
188 |
$redirect_url |
|
189 |
); |
|
190 |
} |
|
191 |
||
192 |
// Strip off non-existing <!--nextpage--> links from single posts or pages. |
|
193 |
if ( get_query_var( 'page' ) ) { |
|
194 |
$post_id = 0; |
|
195 |
||
196 |
if ( $wp_query->queried_object instanceof WP_Post ) { |
|
197 |
$post_id = $wp_query->queried_object->ID; |
|
198 |
} elseif ( $wp_query->post ) { |
|
199 |
$post_id = $wp_query->post->ID; |
|
200 |
} |
|
201 |
||
202 |
if ( $post_id ) { |
|
203 |
$redirect_url = get_permalink( $post_id ); |
|
18 | 204 |
$redirect_obj = get_post( $post_id ); |
16 | 205 |
|
206 |
$redirect['path'] = rtrim( $redirect['path'], (int) get_query_var( 'page' ) . '/' ); |
|
207 |
$redirect['query'] = remove_query_arg( 'page', $redirect['query'] ); |
|
208 |
} |
|
0 | 209 |
} |
210 |
||
211 |
if ( ! $redirect_url ) { |
|
16 | 212 |
$redirect_url = redirect_guess_404_permalink(); |
213 |
||
214 |
if ( $redirect_url ) { |
|
215 |
$redirect['query'] = _remove_qs_args_if_not_in_url( |
|
216 |
$redirect['query'], |
|
217 |
array( 'page', 'feed', 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), |
|
218 |
$redirect_url |
|
219 |
); |
|
0 | 220 |
} |
221 |
} |
|
16 | 222 |
} elseif ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) { |
0 | 223 |
|
16 | 224 |
// Rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101. |
225 |
if ( is_attachment() |
|
226 |
&& ! array_diff( array_keys( $wp->query_vars ), array( 'attachment', 'attachment_id' ) ) |
|
227 |
&& ! $redirect_url |
|
228 |
) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
229 |
if ( ! empty( $_GET['attachment_id'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
230 |
$redirect_url = get_attachment_link( get_query_var( 'attachment_id' ) ); |
18 | 231 |
$redirect_obj = get_post( get_query_var( 'attachment_id' ) ); |
16 | 232 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
233 |
if ( $redirect_url ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
234 |
$redirect['query'] = remove_query_arg( 'attachment_id', $redirect['query'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
235 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
236 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
237 |
$redirect_url = get_attachment_link(); |
18 | 238 |
$redirect_obj = get_post(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
239 |
} |
9 | 240 |
} elseif ( is_single() && ! empty( $_GET['p'] ) && ! $redirect_url ) { |
16 | 241 |
$redirect_url = get_permalink( get_query_var( 'p' ) ); |
18 | 242 |
$redirect_obj = get_post( get_query_var( 'p' ) ); |
16 | 243 |
|
244 |
if ( $redirect_url ) { |
|
9 | 245 |
$redirect['query'] = remove_query_arg( array( 'p', 'post_type' ), $redirect['query'] ); |
246 |
} |
|
247 |
} elseif ( is_single() && ! empty( $_GET['name'] ) && ! $redirect_url ) { |
|
16 | 248 |
$redirect_url = get_permalink( $wp_query->get_queried_object_id() ); |
18 | 249 |
$redirect_obj = get_post( $wp_query->get_queried_object_id() ); |
16 | 250 |
|
251 |
if ( $redirect_url ) { |
|
9 | 252 |
$redirect['query'] = remove_query_arg( 'name', $redirect['query'] ); |
253 |
} |
|
254 |
} elseif ( is_page() && ! empty( $_GET['page_id'] ) && ! $redirect_url ) { |
|
16 | 255 |
$redirect_url = get_permalink( get_query_var( 'page_id' ) ); |
18 | 256 |
$redirect_obj = get_post( get_query_var( 'page_id' ) ); |
16 | 257 |
|
258 |
if ( $redirect_url ) { |
|
9 | 259 |
$redirect['query'] = remove_query_arg( 'page_id', $redirect['query'] ); |
260 |
} |
|
16 | 261 |
} elseif ( is_page() && ! is_feed() && ! $redirect_url |
262 |
&& 'page' === get_option( 'show_on_front' ) && get_queried_object_id() === (int) get_option( 'page_on_front' ) |
|
263 |
) { |
|
9 | 264 |
$redirect_url = home_url( '/' ); |
16 | 265 |
} elseif ( is_home() && ! empty( $_GET['page_id'] ) && ! $redirect_url |
266 |
&& 'page' === get_option( 'show_on_front' ) && get_query_var( 'page_id' ) === (int) get_option( 'page_for_posts' ) |
|
267 |
) { |
|
268 |
$redirect_url = get_permalink( get_option( 'page_for_posts' ) ); |
|
18 | 269 |
$redirect_obj = get_post( get_option( 'page_for_posts' ) ); |
16 | 270 |
|
271 |
if ( $redirect_url ) { |
|
9 | 272 |
$redirect['query'] = remove_query_arg( 'page_id', $redirect['query'] ); |
273 |
} |
|
274 |
} elseif ( ! empty( $_GET['m'] ) && ( is_year() || is_month() || is_day() ) ) { |
|
275 |
$m = get_query_var( 'm' ); |
|
16 | 276 |
|
9 | 277 |
switch ( strlen( $m ) ) { |
16 | 278 |
case 4: // Yearly. |
9 | 279 |
$redirect_url = get_year_link( $m ); |
0 | 280 |
break; |
16 | 281 |
case 6: // Monthly. |
9 | 282 |
$redirect_url = get_month_link( substr( $m, 0, 4 ), substr( $m, 4, 2 ) ); |
0 | 283 |
break; |
16 | 284 |
case 8: // Daily. |
9 | 285 |
$redirect_url = get_day_link( substr( $m, 0, 4 ), substr( $m, 4, 2 ), substr( $m, 6, 2 ) ); |
0 | 286 |
break; |
287 |
} |
|
16 | 288 |
|
9 | 289 |
if ( $redirect_url ) { |
290 |
$redirect['query'] = remove_query_arg( 'm', $redirect['query'] ); |
|
291 |
} |
|
16 | 292 |
// Now moving on to non ?m=X year/month/day links. |
293 |
} elseif ( is_date() ) { |
|
294 |
$year = get_query_var( 'year' ); |
|
295 |
$month = get_query_var( 'monthnum' ); |
|
296 |
$day = get_query_var( 'day' ); |
|
297 |
||
298 |
if ( is_day() && $year && $month && ! empty( $_GET['day'] ) ) { |
|
299 |
$redirect_url = get_day_link( $year, $month, $day ); |
|
300 |
||
301 |
if ( $redirect_url ) { |
|
302 |
$redirect['query'] = remove_query_arg( array( 'year', 'monthnum', 'day' ), $redirect['query'] ); |
|
303 |
} |
|
304 |
} elseif ( is_month() && $year && ! empty( $_GET['monthnum'] ) ) { |
|
305 |
$redirect_url = get_month_link( $year, $month ); |
|
306 |
||
307 |
if ( $redirect_url ) { |
|
308 |
$redirect['query'] = remove_query_arg( array( 'year', 'monthnum' ), $redirect['query'] ); |
|
309 |
} |
|
310 |
} elseif ( is_year() && ! empty( $_GET['year'] ) ) { |
|
311 |
$redirect_url = get_year_link( $year ); |
|
312 |
||
313 |
if ( $redirect_url ) { |
|
314 |
$redirect['query'] = remove_query_arg( 'year', $redirect['query'] ); |
|
315 |
} |
|
9 | 316 |
} |
317 |
} elseif ( is_author() && ! empty( $_GET['author'] ) && preg_match( '|^[0-9]+$|', $_GET['author'] ) ) { |
|
318 |
$author = get_userdata( get_query_var( 'author' ) ); |
|
16 | 319 |
|
320 |
if ( false !== $author |
|
321 |
&& $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 ) ) |
|
322 |
) { |
|
323 |
$redirect_url = get_author_posts_url( $author->ID, $author->user_nicename ); |
|
18 | 324 |
$redirect_obj = $author; |
16 | 325 |
|
326 |
if ( $redirect_url ) { |
|
9 | 327 |
$redirect['query'] = remove_query_arg( 'author', $redirect['query'] ); |
328 |
} |
|
0 | 329 |
} |
16 | 330 |
} elseif ( is_category() || is_tag() || is_tax() ) { // Terms (tags/categories). |
331 |
$term_count = 0; |
|
0 | 332 |
|
9 | 333 |
foreach ( $wp_query->tax_query->queried_terms as $tax_query ) { |
0 | 334 |
$term_count += count( $tax_query['terms'] ); |
9 | 335 |
} |
0 | 336 |
|
337 |
$obj = $wp_query->get_queried_object(); |
|
16 | 338 |
|
339 |
if ( $term_count <= 1 && ! empty( $obj->term_id ) ) { |
|
340 |
$tax_url = get_term_link( (int) $obj->term_id, $obj->taxonomy ); |
|
341 |
||
342 |
if ( $tax_url && ! is_wp_error( $tax_url ) ) { |
|
343 |
if ( ! empty( $redirect['query'] ) ) { |
|
344 |
// Strip taxonomy query vars off the URL. |
|
345 |
$qv_remove = array( 'term', 'taxonomy' ); |
|
346 |
||
347 |
if ( is_category() ) { |
|
348 |
$qv_remove[] = 'category_name'; |
|
349 |
$qv_remove[] = 'cat'; |
|
350 |
} elseif ( is_tag() ) { |
|
351 |
$qv_remove[] = 'tag'; |
|
352 |
$qv_remove[] = 'tag_id'; |
|
353 |
} else { |
|
354 |
// Custom taxonomies will have a custom query var, remove those too. |
|
355 |
$tax_obj = get_taxonomy( $obj->taxonomy ); |
|
356 |
if ( false !== $tax_obj->query_var ) { |
|
357 |
$qv_remove[] = $tax_obj->query_var; |
|
358 |
} |
|
9 | 359 |
} |
0 | 360 |
|
16 | 361 |
$rewrite_vars = array_diff( array_keys( $wp_query->query ), array_keys( $_GET ) ); |
0 | 362 |
|
16 | 363 |
// Check to see if all the query vars are coming from the rewrite, none are set via $_GET. |
364 |
if ( ! array_diff( $rewrite_vars, array_keys( $_GET ) ) ) { |
|
365 |
// Remove all of the per-tax query vars. |
|
366 |
$redirect['query'] = remove_query_arg( $qv_remove, $redirect['query'] ); |
|
367 |
||
368 |
// Create the destination URL for this taxonomy. |
|
369 |
$tax_url = parse_url( $tax_url ); |
|
0 | 370 |
|
16 | 371 |
if ( ! empty( $tax_url['query'] ) ) { |
372 |
// Taxonomy accessible via ?taxonomy=...&term=... or any custom query var. |
|
373 |
parse_str( $tax_url['query'], $query_vars ); |
|
374 |
$redirect['query'] = add_query_arg( $query_vars, $redirect['query'] ); |
|
375 |
} else { |
|
376 |
// Taxonomy is accessible via a "pretty URL". |
|
377 |
$redirect['path'] = $tax_url['path']; |
|
378 |
} |
|
379 |
} else { |
|
380 |
// Some query vars are set via $_GET. Unset those from $_GET that exist via the rewrite. |
|
381 |
foreach ( $qv_remove as $_qv ) { |
|
382 |
if ( isset( $rewrite_vars[ $_qv ] ) ) { |
|
383 |
$redirect['query'] = remove_query_arg( $_qv, $redirect['query'] ); |
|
384 |
} |
|
9 | 385 |
} |
0 | 386 |
} |
387 |
} |
|
388 |
} |
|
389 |
} |
|
16 | 390 |
} elseif ( is_single() && strpos( $wp_rewrite->permalink_structure, '%category%' ) !== false ) { |
391 |
$category_name = get_query_var( 'category_name' ); |
|
392 |
||
393 |
if ( $category_name ) { |
|
394 |
$category = get_category_by_path( $category_name ); |
|
395 |
||
396 |
if ( ! $category || is_wp_error( $category ) |
|
397 |
|| ! has_term( $category->term_id, 'category', $wp_query->get_queried_object_id() ) |
|
398 |
) { |
|
399 |
$redirect_url = get_permalink( $wp_query->get_queried_object_id() ); |
|
18 | 400 |
$redirect_obj = get_post( $wp_query->get_queried_object_id() ); |
16 | 401 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
402 |
} |
0 | 403 |
} |
404 |
||
16 | 405 |
// Post paging. |
9 | 406 |
if ( is_singular() && get_query_var( 'page' ) ) { |
16 | 407 |
$page = get_query_var( 'page' ); |
408 |
||
9 | 409 |
if ( ! $redirect_url ) { |
0 | 410 |
$redirect_url = get_permalink( get_queried_object_id() ); |
18 | 411 |
$redirect_obj = get_post( get_queried_object_id() ); |
9 | 412 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
413 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
414 |
if ( $page > 1 ) { |
16 | 415 |
$redirect_url = trailingslashit( $redirect_url ); |
416 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
417 |
if ( is_front_page() ) { |
16 | 418 |
$redirect_url .= user_trailingslashit( "$wp_rewrite->pagination_base/$page", 'paged' ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
419 |
} else { |
16 | 420 |
$redirect_url .= user_trailingslashit( $page, 'single_paged' ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
421 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
422 |
} |
16 | 423 |
|
424 |
$redirect['query'] = remove_query_arg( 'page', $redirect['query'] ); |
|
0 | 425 |
} |
426 |
||
16 | 427 |
if ( get_query_var( 'sitemap' ) ) { |
428 |
$redirect_url = get_sitemap_url( get_query_var( 'sitemap' ), get_query_var( 'sitemap-subtype' ), get_query_var( 'paged' ) ); |
|
429 |
$redirect['query'] = remove_query_arg( array( 'sitemap', 'sitemap-subtype', 'paged' ), $redirect['query'] ); |
|
430 |
} elseif ( get_query_var( 'paged' ) || is_feed() || get_query_var( 'cpage' ) ) { |
|
431 |
// Paging and feeds. |
|
432 |
$paged = get_query_var( 'paged' ); |
|
433 |
$feed = get_query_var( 'feed' ); |
|
434 |
$cpage = get_query_var( 'cpage' ); |
|
435 |
||
436 |
while ( preg_match( "#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", $redirect['path'] ) |
|
437 |
|| preg_match( '#/(comments/?)?(feed|rss2?|rdf|atom)(/+)?$#', $redirect['path'] ) |
|
438 |
|| preg_match( "#/{$wp_rewrite->comments_pagination_base}-[0-9]+(/+)?$#", $redirect['path'] ) |
|
439 |
) { |
|
440 |
// Strip off any existing paging. |
|
441 |
$redirect['path'] = preg_replace( "#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", '/', $redirect['path'] ); |
|
442 |
// Strip off feed endings. |
|
443 |
$redirect['path'] = preg_replace( '#/(comments/?)?(feed|rss2?|rdf|atom)(/+|$)#', '/', $redirect['path'] ); |
|
444 |
// Strip off any existing comment paging. |
|
445 |
$redirect['path'] = preg_replace( "#/{$wp_rewrite->comments_pagination_base}-[0-9]+?(/+)?$#", '/', $redirect['path'] ); |
|
0 | 446 |
} |
447 |
||
16 | 448 |
$addl_path = ''; |
449 |
$default_feed = get_default_feed(); |
|
450 |
||
451 |
if ( is_feed() && in_array( $feed, $wp_rewrite->feeds, true ) ) { |
|
9 | 452 |
$addl_path = ! empty( $addl_path ) ? trailingslashit( $addl_path ) : ''; |
16 | 453 |
|
9 | 454 |
if ( ! is_singular() && get_query_var( 'withcomments' ) ) { |
0 | 455 |
$addl_path .= 'comments/'; |
9 | 456 |
} |
16 | 457 |
|
458 |
if ( ( 'rss' === $default_feed && 'feed' === $feed ) || 'rss' === $feed ) { |
|
459 |
$format = ( 'rss2' === $default_feed ) ? '' : 'rss2'; |
|
9 | 460 |
} else { |
16 | 461 |
$format = ( $default_feed === $feed || 'feed' === $feed ) ? '' : $feed; |
9 | 462 |
} |
16 | 463 |
|
464 |
$addl_path .= user_trailingslashit( 'feed/' . $format, 'feed' ); |
|
465 |
||
0 | 466 |
$redirect['query'] = remove_query_arg( 'feed', $redirect['query'] ); |
16 | 467 |
} elseif ( is_feed() && 'old' === $feed ) { |
0 | 468 |
$old_feed_files = array( |
469 |
'wp-atom.php' => 'atom', |
|
470 |
'wp-commentsrss2.php' => 'comments_rss2', |
|
16 | 471 |
'wp-feed.php' => $default_feed, |
0 | 472 |
'wp-rdf.php' => 'rdf', |
473 |
'wp-rss.php' => 'rss2', |
|
474 |
'wp-rss2.php' => 'rss2', |
|
475 |
); |
|
16 | 476 |
|
0 | 477 |
if ( isset( $old_feed_files[ basename( $redirect['path'] ) ] ) ) { |
478 |
$redirect_url = get_feed_link( $old_feed_files[ basename( $redirect['path'] ) ] ); |
|
16 | 479 |
|
0 | 480 |
wp_redirect( $redirect_url, 301 ); |
481 |
die(); |
|
482 |
} |
|
483 |
} |
|
484 |
||
16 | 485 |
if ( $paged > 0 ) { |
0 | 486 |
$redirect['query'] = remove_query_arg( 'paged', $redirect['query'] ); |
16 | 487 |
|
9 | 488 |
if ( ! is_feed() ) { |
16 | 489 |
if ( ! is_single() ) { |
9 | 490 |
$addl_path = ! empty( $addl_path ) ? trailingslashit( $addl_path ) : ''; |
16 | 491 |
|
492 |
if ( $paged > 1 ) { |
|
493 |
$addl_path .= user_trailingslashit( "$wp_rewrite->pagination_base/$paged", 'paged' ); |
|
494 |
} |
|
0 | 495 |
} |
496 |
} elseif ( $paged > 1 ) { |
|
497 |
$redirect['query'] = add_query_arg( 'paged', $paged, $redirect['query'] ); |
|
498 |
} |
|
499 |
} |
|
500 |
||
16 | 501 |
$default_comments_page = get_option( 'default_comments_page' ); |
502 |
||
503 |
if ( get_option( 'page_comments' ) |
|
504 |
&& ( 'newest' === $default_comments_page && $cpage > 0 |
|
505 |
|| 'newest' !== $default_comments_page && $cpage > 1 ) |
|
506 |
) { |
|
507 |
$addl_path = ( ! empty( $addl_path ) ? trailingslashit( $addl_path ) : '' ); |
|
508 |
$addl_path .= user_trailingslashit( $wp_rewrite->comments_pagination_base . '-' . $cpage, 'commentpaged' ); |
|
509 |
||
0 | 510 |
$redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] ); |
511 |
} |
|
512 |
||
16 | 513 |
// Strip off trailing /index.php/. |
514 |
$redirect['path'] = preg_replace( '|/' . preg_quote( $wp_rewrite->index, '|' ) . '/?$|', '/', $redirect['path'] ); |
|
515 |
$redirect['path'] = user_trailingslashit( $redirect['path'] ); |
|
516 |
||
517 |
if ( ! empty( $addl_path ) |
|
518 |
&& $wp_rewrite->using_index_permalinks() |
|
519 |
&& strpos( $redirect['path'], '/' . $wp_rewrite->index . '/' ) === false |
|
520 |
) { |
|
9 | 521 |
$redirect['path'] = trailingslashit( $redirect['path'] ) . $wp_rewrite->index . '/'; |
522 |
} |
|
16 | 523 |
|
9 | 524 |
if ( ! empty( $addl_path ) ) { |
525 |
$redirect['path'] = trailingslashit( $redirect['path'] ) . $addl_path; |
|
526 |
} |
|
16 | 527 |
|
0 | 528 |
$redirect_url = $redirect['scheme'] . '://' . $redirect['host'] . $redirect['path']; |
529 |
} |
|
530 |
||
16 | 531 |
if ( 'wp-register.php' === basename( $redirect['path'] ) ) { |
5 | 532 |
if ( is_multisite() ) { |
0 | 533 |
/** This filter is documented in wp-login.php */ |
534 |
$redirect_url = apply_filters( 'wp_signup_location', network_site_url( 'wp-signup.php' ) ); |
|
5 | 535 |
} else { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
536 |
$redirect_url = wp_registration_url(); |
5 | 537 |
} |
538 |
||
0 | 539 |
wp_redirect( $redirect_url, 301 ); |
540 |
die(); |
|
541 |
} |
|
542 |
} |
|
543 |
||
544 |
$redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] ); |
|
16 | 545 |
|
546 |
// Tack on any additional query vars. |
|
9 | 547 |
if ( $redirect_url && ! empty( $redirect['query'] ) ) { |
0 | 548 |
parse_str( $redirect['query'], $_parsed_query ); |
16 | 549 |
$redirect = parse_url( $redirect_url ); |
0 | 550 |
|
551 |
if ( ! empty( $_parsed_query['name'] ) && ! empty( $redirect['query'] ) ) { |
|
552 |
parse_str( $redirect['query'], $_parsed_redirect_query ); |
|
553 |
||
9 | 554 |
if ( empty( $_parsed_redirect_query['name'] ) ) { |
0 | 555 |
unset( $_parsed_query['name'] ); |
9 | 556 |
} |
0 | 557 |
} |
558 |
||
9 | 559 |
$_parsed_query = array_combine( |
560 |
rawurlencode_deep( array_keys( $_parsed_query ) ), |
|
561 |
rawurlencode_deep( array_values( $_parsed_query ) ) |
|
562 |
); |
|
16 | 563 |
|
564 |
$redirect_url = add_query_arg( $_parsed_query, $redirect_url ); |
|
0 | 565 |
} |
566 |
||
9 | 567 |
if ( $redirect_url ) { |
16 | 568 |
$redirect = parse_url( $redirect_url ); |
9 | 569 |
} |
0 | 570 |
|
16 | 571 |
// www.example.com vs. example.com |
572 |
$user_home = parse_url( home_url() ); |
|
573 |
||
9 | 574 |
if ( ! empty( $user_home['host'] ) ) { |
0 | 575 |
$redirect['host'] = $user_home['host']; |
9 | 576 |
} |
16 | 577 |
|
9 | 578 |
if ( empty( $user_home['path'] ) ) { |
0 | 579 |
$user_home['path'] = '/'; |
9 | 580 |
} |
0 | 581 |
|
16 | 582 |
// Handle ports. |
9 | 583 |
if ( ! empty( $user_home['port'] ) ) { |
0 | 584 |
$redirect['port'] = $user_home['port']; |
9 | 585 |
} else { |
586 |
unset( $redirect['port'] ); |
|
587 |
} |
|
0 | 588 |
|
16 | 589 |
// Trailing /index.php. |
9 | 590 |
$redirect['path'] = preg_replace( '|/' . preg_quote( $wp_rewrite->index, '|' ) . '/*?$|', '/', $redirect['path'] ); |
0 | 591 |
|
9 | 592 |
$punctuation_pattern = implode( |
593 |
'|', |
|
594 |
array_map( |
|
595 |
'preg_quote', |
|
596 |
array( |
|
597 |
' ', |
|
16 | 598 |
'%20', // Space. |
9 | 599 |
'!', |
16 | 600 |
'%21', // Exclamation mark. |
9 | 601 |
'"', |
16 | 602 |
'%22', // Double quote. |
9 | 603 |
"'", |
16 | 604 |
'%27', // Single quote. |
9 | 605 |
'(', |
16 | 606 |
'%28', // Opening bracket. |
9 | 607 |
')', |
16 | 608 |
'%29', // Closing bracket. |
9 | 609 |
',', |
16 | 610 |
'%2C', // Comma. |
9 | 611 |
'.', |
16 | 612 |
'%2E', // Period. |
9 | 613 |
';', |
16 | 614 |
'%3B', // Semicolon. |
9 | 615 |
'{', |
16 | 616 |
'%7B', // Opening curly bracket. |
9 | 617 |
'}', |
16 | 618 |
'%7D', // Closing curly bracket. |
619 |
'%E2%80%9C', // Opening curly quote. |
|
620 |
'%E2%80%9D', // Closing curly quote. |
|
9 | 621 |
) |
622 |
) |
|
623 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
624 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
625 |
// Remove trailing spaces and end punctuation from the path. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
626 |
$redirect['path'] = preg_replace( "#($punctuation_pattern)+$#", '', $redirect['path'] ); |
0 | 627 |
|
9 | 628 |
if ( ! empty( $redirect['query'] ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
629 |
// Remove trailing spaces and end punctuation from certain terminating query string args. |
16 | 630 |
$redirect['query'] = preg_replace( "#((^|&)(p|page_id|cat|tag)=[^&]*?)($punctuation_pattern)+$#", '$1', $redirect['query'] ); |
0 | 631 |
|
16 | 632 |
// Clean up empty query strings. |
9 | 633 |
$redirect['query'] = trim( preg_replace( '#(^|&)(p|page_id|cat|tag)=?(&|$)#', '&', $redirect['query'] ), '&' ); |
0 | 634 |
|
16 | 635 |
// Redirect obsolete feeds. |
0 | 636 |
$redirect['query'] = preg_replace( '#(^|&)feed=rss(&|$)#', '$1feed=rss2$2', $redirect['query'] ); |
637 |
||
16 | 638 |
// Remove redundant leading ampersands. |
0 | 639 |
$redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] ); |
640 |
} |
|
641 |
||
16 | 642 |
// Strip /index.php/ when we're not using PATHINFO permalinks. |
9 | 643 |
if ( ! $wp_rewrite->using_index_permalinks() ) { |
0 | 644 |
$redirect['path'] = str_replace( '/' . $wp_rewrite->index . '/', '/', $redirect['path'] ); |
9 | 645 |
} |
0 | 646 |
|
16 | 647 |
// Trailing slashes. |
648 |
if ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() |
|
649 |
&& ! is_404() && ( ! is_front_page() || is_front_page() && get_query_var( 'paged' ) > 1 ) |
|
650 |
) { |
|
0 | 651 |
$user_ts_type = ''; |
16 | 652 |
|
9 | 653 |
if ( get_query_var( 'paged' ) > 0 ) { |
0 | 654 |
$user_ts_type = 'paged'; |
655 |
} else { |
|
9 | 656 |
foreach ( array( 'single', 'category', 'page', 'day', 'month', 'year', 'home' ) as $type ) { |
0 | 657 |
$func = 'is_' . $type; |
9 | 658 |
if ( call_user_func( $func ) ) { |
0 | 659 |
$user_ts_type = $type; |
660 |
break; |
|
661 |
} |
|
662 |
} |
|
663 |
} |
|
16 | 664 |
|
9 | 665 |
$redirect['path'] = user_trailingslashit( $redirect['path'], $user_ts_type ); |
0 | 666 |
} elseif ( is_front_page() ) { |
9 | 667 |
$redirect['path'] = trailingslashit( $redirect['path'] ); |
0 | 668 |
} |
669 |
||
16 | 670 |
// Remove trailing slash for robots.txt or sitemap requests. |
671 |
if ( is_robots() |
|
672 |
|| ! empty( get_query_var( 'sitemap' ) ) || ! empty( get_query_var( 'sitemap-stylesheet' ) ) |
|
673 |
) { |
|
674 |
$redirect['path'] = untrailingslashit( $redirect['path'] ); |
|
675 |
} |
|
676 |
||
677 |
// Strip multiple slashes out of the URL. |
|
9 | 678 |
if ( strpos( $redirect['path'], '//' ) > -1 ) { |
679 |
$redirect['path'] = preg_replace( '|/+|', '/', $redirect['path'] ); |
|
680 |
} |
|
0 | 681 |
|
16 | 682 |
// Always trailing slash the Front Page URL. |
683 |
if ( trailingslashit( $redirect['path'] ) === trailingslashit( $user_home['path'] ) ) { |
|
9 | 684 |
$redirect['path'] = trailingslashit( $redirect['path'] ); |
685 |
} |
|
0 | 686 |
|
16 | 687 |
$original_host_low = strtolower( $original['host'] ); |
688 |
$redirect_host_low = strtolower( $redirect['host'] ); |
|
689 |
||
690 |
// Ignore differences in host capitalization, as this can lead to infinite redirects. |
|
691 |
// Only redirect no-www <=> yes-www. |
|
692 |
if ( $original_host_low === $redirect_host_low |
|
693 |
|| ( 'www.' . $original_host_low !== $redirect_host_low |
|
694 |
&& 'www.' . $redirect_host_low !== $original_host_low ) |
|
695 |
) { |
|
0 | 696 |
$redirect['host'] = $original['host']; |
9 | 697 |
} |
0 | 698 |
|
5 | 699 |
$compare_original = array( $original['host'], $original['path'] ); |
0 | 700 |
|
9 | 701 |
if ( ! empty( $original['port'] ) ) { |
0 | 702 |
$compare_original[] = $original['port']; |
9 | 703 |
} |
0 | 704 |
|
9 | 705 |
if ( ! empty( $original['query'] ) ) { |
0 | 706 |
$compare_original[] = $original['query']; |
9 | 707 |
} |
0 | 708 |
|
5 | 709 |
$compare_redirect = array( $redirect['host'], $redirect['path'] ); |
0 | 710 |
|
9 | 711 |
if ( ! empty( $redirect['port'] ) ) { |
0 | 712 |
$compare_redirect[] = $redirect['port']; |
9 | 713 |
} |
0 | 714 |
|
9 | 715 |
if ( ! empty( $redirect['query'] ) ) { |
0 | 716 |
$compare_redirect[] = $redirect['query']; |
9 | 717 |
} |
0 | 718 |
|
719 |
if ( $compare_original !== $compare_redirect ) { |
|
720 |
$redirect_url = $redirect['scheme'] . '://' . $redirect['host']; |
|
16 | 721 |
|
9 | 722 |
if ( ! empty( $redirect['port'] ) ) { |
0 | 723 |
$redirect_url .= ':' . $redirect['port']; |
9 | 724 |
} |
16 | 725 |
|
0 | 726 |
$redirect_url .= $redirect['path']; |
16 | 727 |
|
9 | 728 |
if ( ! empty( $redirect['query'] ) ) { |
0 | 729 |
$redirect_url .= '?' . $redirect['query']; |
9 | 730 |
} |
0 | 731 |
} |
732 |
||
16 | 733 |
if ( ! $redirect_url || $redirect_url === $requested_url ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
734 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
735 |
} |
0 | 736 |
|
737 |
// Hex encoded octets are case-insensitive. |
|
9 | 738 |
if ( false !== strpos( $requested_url, '%' ) ) { |
739 |
if ( ! function_exists( 'lowercase_octets' ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
740 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
741 |
* Converts the first hex-encoded octet match to lowercase. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
742 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
743 |
* @since 3.1.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
744 |
* @ignore |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
745 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
746 |
* @param array $matches Hex-encoded octet matches for the requested URL. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
747 |
* @return string Lowercased version of the first match. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
748 |
*/ |
9 | 749 |
function lowercase_octets( $matches ) { |
0 | 750 |
return strtolower( $matches[0] ); |
751 |
} |
|
752 |
} |
|
16 | 753 |
|
9 | 754 |
$requested_url = preg_replace_callback( '|%[a-fA-F0-9][a-fA-F0-9]|', 'lowercase_octets', $requested_url ); |
0 | 755 |
} |
756 |
||
18 | 757 |
if ( $redirect_obj instanceof WP_Post ) { |
758 |
$post_status_obj = get_post_status_object( get_post_status( $redirect_obj ) ); |
|
759 |
/* |
|
760 |
* Unset the redirect object and URL if they are not readable by the user. |
|
761 |
* This condition is a little confusing as the condition needs to pass if |
|
762 |
* the post is not readable by the user. That's why there are ! (not) conditions |
|
763 |
* throughout. |
|
764 |
*/ |
|
765 |
if ( |
|
766 |
// Private post statuses only redirect if the user can read them. |
|
767 |
! ( |
|
768 |
$post_status_obj->private && |
|
769 |
current_user_can( 'read_post', $redirect_obj->ID ) |
|
770 |
) && |
|
771 |
// For other posts, only redirect if publicly viewable. |
|
772 |
! is_post_publicly_viewable( $redirect_obj ) |
|
773 |
) { |
|
774 |
$redirect_obj = false; |
|
775 |
$redirect_url = false; |
|
776 |
} |
|
777 |
} |
|
778 |
||
0 | 779 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
780 |
* Filters the canonical redirect URL. |
0 | 781 |
* |
782 |
* Returning false to this filter will cancel the redirect. |
|
783 |
* |
|
784 |
* @since 2.3.0 |
|
785 |
* |
|
786 |
* @param string $redirect_url The redirect URL. |
|
787 |
* @param string $requested_url The requested URL. |
|
788 |
*/ |
|
789 |
$redirect_url = apply_filters( 'redirect_canonical', $redirect_url, $requested_url ); |
|
790 |
||
16 | 791 |
// Yes, again -- in case the filter aborted the request. |
792 |
if ( ! $redirect_url || strip_fragment_from_url( $redirect_url ) === strip_fragment_from_url( $requested_url ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
793 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
794 |
} |
0 | 795 |
|
796 |
if ( $do_redirect ) { |
|
16 | 797 |
// Protect against chained redirects. |
9 | 798 |
if ( ! redirect_canonical( $redirect_url, false ) ) { |
799 |
wp_redirect( $redirect_url, 301 ); |
|
16 | 800 |
exit; |
0 | 801 |
} else { |
16 | 802 |
// Debug. |
0 | 803 |
// die("1: $redirect_url<br />2: " . redirect_canonical( $redirect_url, false ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
804 |
return; |
0 | 805 |
} |
806 |
} else { |
|
807 |
return $redirect_url; |
|
808 |
} |
|
809 |
} |
|
810 |
||
811 |
/** |
|
812 |
* Removes arguments from a query string if they are not present in a URL |
|
813 |
* DO NOT use this in plugin code. |
|
814 |
* |
|
5 | 815 |
* @since 3.4.0 |
0 | 816 |
* @access private |
817 |
* |
|
5 | 818 |
* @param string $query_string |
16 | 819 |
* @param array $args_to_check |
5 | 820 |
* @param string $url |
0 | 821 |
* @return string The altered query string |
822 |
*/ |
|
9 | 823 |
function _remove_qs_args_if_not_in_url( $query_string, array $args_to_check, $url ) { |
16 | 824 |
$parsed_url = parse_url( $url ); |
825 |
||
0 | 826 |
if ( ! empty( $parsed_url['query'] ) ) { |
827 |
parse_str( $parsed_url['query'], $parsed_query ); |
|
16 | 828 |
|
0 | 829 |
foreach ( $args_to_check as $qv ) { |
9 | 830 |
if ( ! isset( $parsed_query[ $qv ] ) ) { |
0 | 831 |
$query_string = remove_query_arg( $qv, $query_string ); |
9 | 832 |
} |
0 | 833 |
} |
834 |
} else { |
|
835 |
$query_string = remove_query_arg( $args_to_check, $query_string ); |
|
836 |
} |
|
16 | 837 |
|
0 | 838 |
return $query_string; |
839 |
} |
|
840 |
||
841 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
842 |
* Strips the #fragment from a URL, if one is present. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
843 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
844 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
845 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
846 |
* @param string $url The URL to strip. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
847 |
* @return string The altered URL. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
848 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
849 |
function strip_fragment_from_url( $url ) { |
16 | 850 |
$parsed_url = parse_url( $url ); |
851 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
852 |
if ( ! empty( $parsed_url['host'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
853 |
// This mirrors code in redirect_canonical(). It does not handle every case. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
854 |
$url = $parsed_url['scheme'] . '://' . $parsed_url['host']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
855 |
if ( ! empty( $parsed_url['port'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
856 |
$url .= ':' . $parsed_url['port']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
857 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
858 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
859 |
if ( ! empty( $parsed_url['path'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
860 |
$url .= $parsed_url['path']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
861 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
862 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
863 |
if ( ! empty( $parsed_url['query'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
864 |
$url .= '?' . $parsed_url['query']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
865 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
866 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
867 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
868 |
return $url; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
869 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
870 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
871 |
/** |
16 | 872 |
* Attempts to guess the correct URL for a 404 request based on query vars. |
0 | 873 |
* |
874 |
* @since 2.3.0 |
|
5 | 875 |
* |
876 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
0 | 877 |
* |
16 | 878 |
* @return string|false The correct URL if one is found. False on failure. |
0 | 879 |
*/ |
880 |
function redirect_guess_404_permalink() { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
881 |
global $wpdb; |
0 | 882 |
|
16 | 883 |
/** |
884 |
* Filters whether to attempt to guess a redirect URL for a 404 request. |
|
885 |
* |
|
886 |
* Returning a false value from the filter will disable the URL guessing |
|
887 |
* and return early without performing a redirect. |
|
888 |
* |
|
889 |
* @since 5.5.0 |
|
890 |
* |
|
891 |
* @param bool $do_redirect_guess Whether to attempt to guess a redirect URL |
|
892 |
* for a 404 request. Default true. |
|
893 |
*/ |
|
894 |
if ( false === apply_filters( 'do_redirect_guess_404_permalink', true ) ) { |
|
895 |
return false; |
|
896 |
} |
|
897 |
||
898 |
/** |
|
899 |
* Short-circuits the redirect URL guessing for 404 requests. |
|
900 |
* |
|
901 |
* Returning a non-null value from the filter will effectively short-circuit |
|
902 |
* the URL guessing, returning the passed value instead. |
|
903 |
* |
|
904 |
* @since 5.5.0 |
|
905 |
* |
|
906 |
* @param null|string|false $pre Whether to short-circuit guessing the redirect for a 404. |
|
907 |
* Default null to continue with the URL guessing. |
|
908 |
*/ |
|
909 |
$pre = apply_filters( 'pre_redirect_guess_404_permalink', null ); |
|
910 |
if ( null !== $pre ) { |
|
911 |
return $pre; |
|
912 |
} |
|
913 |
||
9 | 914 |
if ( get_query_var( 'name' ) ) { |
16 | 915 |
/** |
916 |
* Filters whether to perform a strict guess for a 404 redirect. |
|
917 |
* |
|
918 |
* Returning a truthy value from the filter will redirect only exact post_name matches. |
|
919 |
* |
|
920 |
* @since 5.5.0 |
|
921 |
* |
|
922 |
* @param bool $strict_guess Whether to perform a strict guess. Default false (loose guess). |
|
923 |
*/ |
|
924 |
$strict_guess = apply_filters( 'strict_redirect_guess_404_permalink', false ); |
|
0 | 925 |
|
16 | 926 |
if ( $strict_guess ) { |
927 |
$where = $wpdb->prepare( 'post_name = %s', get_query_var( 'name' ) ); |
|
928 |
} else { |
|
929 |
$where = $wpdb->prepare( 'post_name LIKE %s', $wpdb->esc_like( get_query_var( 'name' ) ) . '%' ); |
|
930 |
} |
|
931 |
||
932 |
// If any of post_type, year, monthnum, or day are set, use them to refine the query. |
|
9 | 933 |
if ( get_query_var( 'post_type' ) ) { |
18 | 934 |
if ( is_array( get_query_var( 'post_type' ) ) ) { |
935 |
// phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare |
|
936 |
$where .= " AND post_type IN ('" . join( "', '", esc_sql( get_query_var( 'post_type' ) ) ) . "')"; |
|
937 |
} else { |
|
938 |
$where .= $wpdb->prepare( ' AND post_type = %s', get_query_var( 'post_type' ) ); |
|
939 |
} |
|
9 | 940 |
} else { |
0 | 941 |
$where .= " AND post_type IN ('" . implode( "', '", get_post_types( array( 'public' => true ) ) ) . "')"; |
9 | 942 |
} |
0 | 943 |
|
9 | 944 |
if ( get_query_var( 'year' ) ) { |
945 |
$where .= $wpdb->prepare( ' AND YEAR(post_date) = %d', get_query_var( 'year' ) ); |
|
946 |
} |
|
947 |
if ( get_query_var( 'monthnum' ) ) { |
|
948 |
$where .= $wpdb->prepare( ' AND MONTH(post_date) = %d', get_query_var( 'monthnum' ) ); |
|
949 |
} |
|
950 |
if ( get_query_var( 'day' ) ) { |
|
951 |
$where .= $wpdb->prepare( ' AND DAYOFMONTH(post_date) = %d', get_query_var( 'day' ) ); |
|
952 |
} |
|
0 | 953 |
|
16 | 954 |
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
9 | 955 |
$post_id = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'" ); |
16 | 956 |
|
9 | 957 |
if ( ! $post_id ) { |
0 | 958 |
return false; |
9 | 959 |
} |
16 | 960 |
|
9 | 961 |
if ( get_query_var( 'feed' ) ) { |
0 | 962 |
return get_post_comments_feed_link( $post_id, get_query_var( 'feed' ) ); |
16 | 963 |
} elseif ( get_query_var( 'page' ) > 1 ) { |
0 | 964 |
return trailingslashit( get_permalink( $post_id ) ) . user_trailingslashit( get_query_var( 'page' ), 'single_paged' ); |
9 | 965 |
} else { |
0 | 966 |
return get_permalink( $post_id ); |
9 | 967 |
} |
0 | 968 |
} |
969 |
||
970 |
return false; |
|
971 |
} |
|
972 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
973 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
974 |
* Redirects a variety of shorthand URLs to the admin. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
975 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
976 |
* If a user visits example.com/admin, they'll be redirected to /wp-admin. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
977 |
* Visiting /login redirects to /wp-login.php, and so on. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
978 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
979 |
* @since 3.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
980 |
* |
16 | 981 |
* @global WP_Rewrite $wp_rewrite WordPress rewrite component. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
982 |
*/ |
0 | 983 |
function wp_redirect_admin_locations() { |
984 |
global $wp_rewrite; |
|
16 | 985 |
|
9 | 986 |
if ( ! ( is_404() && $wp_rewrite->using_permalinks() ) ) { |
0 | 987 |
return; |
9 | 988 |
} |
0 | 989 |
|
990 |
$admins = array( |
|
991 |
home_url( 'wp-admin', 'relative' ), |
|
992 |
home_url( 'dashboard', 'relative' ), |
|
993 |
home_url( 'admin', 'relative' ), |
|
994 |
site_url( 'dashboard', 'relative' ), |
|
995 |
site_url( 'admin', 'relative' ), |
|
996 |
); |
|
16 | 997 |
|
998 |
if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $admins, true ) ) { |
|
0 | 999 |
wp_redirect( admin_url() ); |
1000 |
exit; |
|
1001 |
} |
|
1002 |
||
1003 |
$logins = array( |
|
1004 |
home_url( 'wp-login.php', 'relative' ), |
|
1005 |
home_url( 'login', 'relative' ), |
|
1006 |
site_url( 'login', 'relative' ), |
|
1007 |
); |
|
16 | 1008 |
|
1009 |
if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $logins, true ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1010 |
wp_redirect( wp_login_url() ); |
0 | 1011 |
exit; |
1012 |
} |
|
1013 |
} |