author | ymh <ymh.work@gmail.com> |
Mon, 14 Oct 2019 17:39:30 +0200 | |
changeset 7 | cf61fcea0001 |
parent 5 | 5e2f62d02dcd |
child 9 | 177826044cd9 |
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+, |
22 |
* page/post previews, WP admin, Trackbacks, robots.txt, searches, or on POST |
|
23 |
* requests. |
|
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 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
31 |
* @global WP_Rewrite $wp_rewrite |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
32 |
* @global bool $is_IIS |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
33 |
* @global WP_Query $wp_query |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
34 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
35 |
* @global WP $wp Current WordPress environment instance. |
0 | 36 |
* |
37 |
* @param string $requested_url Optional. The URL that was requested, used to |
|
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 |
|
5 | 45 |
if ( isset( $_SERVER['REQUEST_METHOD'] ) && ! in_array( strtoupper( $_SERVER['REQUEST_METHOD'] ), array( 'GET', 'HEAD' ) ) ) { |
0 | 46 |
return; |
5 | 47 |
} |
48 |
||
49 |
// If we're not in wp-admin and the post has been published and preview nonce |
|
50 |
// is non-existent or invalid then no need for preview in query |
|
51 |
if ( is_preview() && get_query_var( 'p' ) && 'publish' == get_post_status( get_query_var( 'p' ) ) ) { |
|
52 |
if ( ! isset( $_GET['preview_id'] ) |
|
53 |
|| ! isset( $_GET['preview_nonce'] ) |
|
54 |
|| ! wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . (int) $_GET['preview_id'] ) ) { |
|
55 |
$wp_query->is_preview = false; |
|
56 |
} |
|
57 |
} |
|
58 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
59 |
if ( is_trackback() || is_search() || is_admin() || is_preview() || is_robots() || ( $is_IIS && !iis7_supports_permalinks() ) ) { |
5 | 60 |
return; |
61 |
} |
|
0 | 62 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
63 |
if ( ! $requested_url && isset( $_SERVER['HTTP_HOST'] ) ) { |
0 | 64 |
// build the URL in the address bar |
65 |
$requested_url = is_ssl() ? 'https://' : 'http://'; |
|
66 |
$requested_url .= $_SERVER['HTTP_HOST']; |
|
67 |
$requested_url .= $_SERVER['REQUEST_URI']; |
|
68 |
} |
|
69 |
||
70 |
$original = @parse_url($requested_url); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
71 |
if ( false === $original ) { |
0 | 72 |
return; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
73 |
} |
0 | 74 |
|
75 |
$redirect = $original; |
|
76 |
$redirect_url = false; |
|
77 |
||
78 |
// Notice fixing |
|
79 |
if ( !isset($redirect['path']) ) |
|
80 |
$redirect['path'] = ''; |
|
81 |
if ( !isset($redirect['query']) ) |
|
82 |
$redirect['query'] = ''; |
|
83 |
||
5 | 84 |
// If the original URL ended with non-breaking spaces, they were almost |
85 |
// certainly inserted by accident. Let's remove them, so the reader doesn't |
|
86 |
// see a 404 error with no obvious cause. |
|
87 |
$redirect['path'] = preg_replace( '|(%C2%A0)+$|i', '', $redirect['path'] ); |
|
88 |
||
89 |
// It's not a preview, so remove it from URL |
|
90 |
if ( get_query_var( 'preview' ) ) { |
|
91 |
$redirect['query'] = remove_query_arg( 'preview', $redirect['query'] ); |
|
92 |
} |
|
93 |
||
0 | 94 |
if ( is_feed() && ( $id = get_query_var( 'p' ) ) ) { |
95 |
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 ); |
|
97 |
$redirect['path'] = parse_url( $redirect_url, PHP_URL_PATH ); |
|
98 |
} |
|
99 |
} |
|
100 |
||
101 |
if ( is_singular() && 1 > $wp_query->post_count && ($id = get_query_var('p')) ) { |
|
102 |
||
103 |
$vars = $wpdb->get_results( $wpdb->prepare("SELECT post_type, post_parent FROM $wpdb->posts WHERE ID = %d", $id) ); |
|
104 |
||
105 |
if ( isset($vars[0]) && $vars = $vars[0] ) { |
|
106 |
if ( 'revision' == $vars->post_type && $vars->post_parent > 0 ) |
|
107 |
$id = $vars->post_parent; |
|
108 |
||
109 |
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 ); |
|
111 |
} |
|
112 |
} |
|
113 |
||
114 |
// These tests give us a WP-generated permalink |
|
115 |
if ( is_404() ) { |
|
116 |
||
117 |
// 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') ); |
|
119 |
if ( $id && $redirect_post = get_post($id) ) { |
|
120 |
$post_type_obj = get_post_type_object($redirect_post->post_type); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
121 |
if ( $post_type_obj->public && 'auto-draft' != $redirect_post->post_status ) { |
0 | 122 |
$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 ); |
|
124 |
} |
|
125 |
} |
|
126 |
||
127 |
if ( get_query_var( 'day' ) && get_query_var( 'monthnum' ) && get_query_var( 'year' ) ) { |
|
128 |
$year = get_query_var( 'year' ); |
|
129 |
$month = get_query_var( 'monthnum' ); |
|
130 |
$day = get_query_var( 'day' ); |
|
131 |
$date = sprintf( '%04d-%02d-%02d', $year, $month, $day ); |
|
132 |
if ( ! wp_checkdate( $month, $day, $year, $date ) ) { |
|
133 |
$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 ); |
|
135 |
} |
|
136 |
} elseif ( get_query_var( 'monthnum' ) && get_query_var( 'year' ) && 12 < get_query_var( 'monthnum' ) ) { |
|
137 |
$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 ); |
|
139 |
} |
|
140 |
||
141 |
if ( ! $redirect_url ) { |
|
142 |
if ( $redirect_url = redirect_guess_404_permalink() ) { |
|
143 |
$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 ); |
|
144 |
} |
|
145 |
} |
|
146 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
147 |
if ( get_query_var( 'page' ) && $wp_query->post && |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
148 |
false !== strpos( $wp_query->post->post_content, '<!--nextpage-->' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
149 |
$redirect['path'] = rtrim( $redirect['path'], (int) get_query_var( 'page' ) . '/' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
150 |
$redirect['query'] = remove_query_arg( 'page', $redirect['query'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
151 |
$redirect_url = get_permalink( $wp_query->post->ID ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
152 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
153 |
|
0 | 154 |
} elseif ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) { |
155 |
// rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
156 |
if ( is_attachment() && |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
157 |
! array_diff( array_keys( $wp->query_vars ), array( 'attachment', 'attachment_id' ) ) && |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
158 |
! $redirect_url ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
159 |
if ( ! empty( $_GET['attachment_id'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
160 |
$redirect_url = get_attachment_link( get_query_var( 'attachment_id' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
161 |
if ( $redirect_url ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
162 |
$redirect['query'] = remove_query_arg( 'attachment_id', $redirect['query'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
163 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
164 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
165 |
$redirect_url = get_attachment_link(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
166 |
} |
0 | 167 |
} elseif ( is_single() && !empty($_GET['p']) && ! $redirect_url ) { |
168 |
if ( $redirect_url = get_permalink(get_query_var('p')) ) |
|
169 |
$redirect['query'] = remove_query_arg(array('p', 'post_type'), $redirect['query']); |
|
170 |
} elseif ( is_single() && !empty($_GET['name']) && ! $redirect_url ) { |
|
171 |
if ( $redirect_url = get_permalink( $wp_query->get_queried_object_id() ) ) |
|
172 |
$redirect['query'] = remove_query_arg('name', $redirect['query']); |
|
173 |
} elseif ( is_page() && !empty($_GET['page_id']) && ! $redirect_url ) { |
|
174 |
if ( $redirect_url = get_permalink(get_query_var('page_id')) ) |
|
175 |
$redirect['query'] = remove_query_arg('page_id', $redirect['query']); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
176 |
} elseif ( is_page() && !is_feed() && 'page' == get_option('show_on_front') && get_queried_object_id() == get_option('page_on_front') && ! $redirect_url ) { |
0 | 177 |
$redirect_url = home_url('/'); |
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 ) { |
|
179 |
if ( $redirect_url = get_permalink(get_option('page_for_posts')) ) |
|
180 |
$redirect['query'] = remove_query_arg('page_id', $redirect['query']); |
|
181 |
} elseif ( !empty($_GET['m']) && ( is_year() || is_month() || is_day() ) ) { |
|
182 |
$m = get_query_var('m'); |
|
183 |
switch ( strlen($m) ) { |
|
184 |
case 4: // Yearly |
|
185 |
$redirect_url = get_year_link($m); |
|
186 |
break; |
|
187 |
case 6: // Monthly |
|
188 |
$redirect_url = get_month_link( substr($m, 0, 4), substr($m, 4, 2) ); |
|
189 |
break; |
|
190 |
case 8: // Daily |
|
191 |
$redirect_url = get_day_link(substr($m, 0, 4), substr($m, 4, 2), substr($m, 6, 2)); |
|
192 |
break; |
|
193 |
} |
|
194 |
if ( $redirect_url ) |
|
195 |
$redirect['query'] = remove_query_arg('m', $redirect['query']); |
|
196 |
// now moving on to non ?m=X year/month/day links |
|
197 |
} elseif ( is_day() && get_query_var('year') && get_query_var('monthnum') && !empty($_GET['day']) ) { |
|
198 |
if ( $redirect_url = get_day_link(get_query_var('year'), get_query_var('monthnum'), get_query_var('day')) ) |
|
199 |
$redirect['query'] = remove_query_arg(array('year', 'monthnum', 'day'), $redirect['query']); |
|
200 |
} elseif ( is_month() && get_query_var('year') && !empty($_GET['monthnum']) ) { |
|
201 |
if ( $redirect_url = get_month_link(get_query_var('year'), get_query_var('monthnum')) ) |
|
202 |
$redirect['query'] = remove_query_arg(array('year', 'monthnum'), $redirect['query']); |
|
203 |
} elseif ( is_year() && !empty($_GET['year']) ) { |
|
204 |
if ( $redirect_url = get_year_link(get_query_var('year')) ) |
|
205 |
$redirect['query'] = remove_query_arg('year', $redirect['query']); |
|
206 |
} elseif ( is_author() && !empty($_GET['author']) && preg_match( '|^[0-9]+$|', $_GET['author'] ) ) { |
|
207 |
$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 ) ) ) { |
|
209 |
if ( $redirect_url = get_author_posts_url($author->ID, $author->user_nicename) ) |
|
210 |
$redirect['query'] = remove_query_arg('author', $redirect['query']); |
|
211 |
} |
|
212 |
} elseif ( is_category() || is_tag() || is_tax() ) { // Terms (Tags/categories) |
|
213 |
||
214 |
$term_count = 0; |
|
5 | 215 |
foreach ( $wp_query->tax_query->queried_terms as $tax_query ) |
0 | 216 |
$term_count += count( $tax_query['terms'] ); |
217 |
||
218 |
$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) ) { |
|
220 |
if ( !empty($redirect['query']) ) { |
|
221 |
// Strip taxonomy query vars off the url. |
|
222 |
$qv_remove = array( 'term', 'taxonomy'); |
|
223 |
if ( is_category() ) { |
|
224 |
$qv_remove[] = 'category_name'; |
|
225 |
$qv_remove[] = 'cat'; |
|
226 |
} elseif ( is_tag() ) { |
|
227 |
$qv_remove[] = 'tag'; |
|
228 |
$qv_remove[] = 'tag_id'; |
|
229 |
} else { // Custom taxonomies will have a custom query var, remove those too: |
|
230 |
$tax_obj = get_taxonomy( $obj->taxonomy ); |
|
231 |
if ( false !== $tax_obj->query_var ) |
|
232 |
$qv_remove[] = $tax_obj->query_var; |
|
233 |
} |
|
234 |
||
235 |
$rewrite_vars = array_diff( array_keys($wp_query->query), array_keys($_GET) ); |
|
236 |
||
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 |
|
238 |
$redirect['query'] = remove_query_arg($qv_remove, $redirect['query']); //Remove all of the per-tax qv's |
|
239 |
||
240 |
// Create the destination url for this taxonomy |
|
241 |
$tax_url = parse_url($tax_url); |
|
242 |
if ( ! empty($tax_url['query']) ) { // Taxonomy accessible via ?taxonomy=..&term=.. or any custom qv.. |
|
243 |
parse_str($tax_url['query'], $query_vars); |
|
244 |
$redirect['query'] = add_query_arg($query_vars, $redirect['query']); |
|
245 |
} else { // Taxonomy is accessible via a "pretty-URL" |
|
246 |
$redirect['path'] = $tax_url['path']; |
|
247 |
} |
|
248 |
||
249 |
} else { // Some query vars are set via $_GET. Unset those from $_GET that exist via the rewrite |
|
250 |
foreach ( $qv_remove as $_qv ) { |
|
251 |
if ( isset($rewrite_vars[$_qv]) ) |
|
252 |
$redirect['query'] = remove_query_arg($_qv, $redirect['query']); |
|
253 |
} |
|
254 |
} |
|
255 |
} |
|
256 |
||
257 |
} |
|
258 |
} elseif ( is_single() && strpos($wp_rewrite->permalink_structure, '%category%') !== false && $cat = get_query_var( 'category_name' ) ) { |
|
259 |
$category = get_category_by_path( $cat ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
260 |
if ( ( ! $category || is_wp_error( $category ) ) || ! has_term( $category->term_id, 'category', $wp_query->get_queried_object_id() ) ) { |
0 | 261 |
$redirect_url = get_permalink($wp_query->get_queried_object_id()); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
262 |
} |
0 | 263 |
} |
264 |
||
265 |
// Post Paging |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
266 |
if ( is_singular() && get_query_var('page') ) { |
0 | 267 |
if ( !$redirect_url ) |
268 |
$redirect_url = get_permalink( get_queried_object_id() ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
269 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
270 |
$page = get_query_var( 'page' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
271 |
if ( $page > 1 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
272 |
if ( is_front_page() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
273 |
$redirect_url = trailingslashit( $redirect_url ) . user_trailingslashit( "$wp_rewrite->pagination_base/$page", 'paged' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
274 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
275 |
$redirect_url = trailingslashit( $redirect_url ) . user_trailingslashit( $page, 'single_paged' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
276 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
277 |
} |
0 | 278 |
$redirect['query'] = remove_query_arg( 'page', $redirect['query'] ); |
279 |
} |
|
280 |
||
281 |
// paging and feeds |
|
282 |
if ( get_query_var('paged') || is_feed() || get_query_var('cpage') ) { |
|
5 | 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'] ) ) { |
0 | 284 |
// Strip off paging and feed |
285 |
$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 |
|
5 | 287 |
$redirect['path'] = preg_replace("#/{$wp_rewrite->comments_pagination_base}-[0-9]+?(/+)?$#", '/', $redirect['path']); // strip off any existing comment paging |
0 | 288 |
} |
289 |
||
290 |
$addl_path = ''; |
|
291 |
if ( is_feed() && in_array( get_query_var('feed'), $wp_rewrite->feeds ) ) { |
|
292 |
$addl_path = !empty( $addl_path ) ? trailingslashit($addl_path) : ''; |
|
293 |
if ( !is_singular() && get_query_var( 'withcomments' ) ) |
|
294 |
$addl_path .= 'comments/'; |
|
295 |
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' ); |
|
297 |
else |
|
298 |
$addl_path .= user_trailingslashit( 'feed/' . ( ( get_default_feed() == get_query_var('feed') || 'feed' == get_query_var('feed') ) ? '' : get_query_var('feed') ), 'feed' ); |
|
299 |
$redirect['query'] = remove_query_arg( 'feed', $redirect['query'] ); |
|
300 |
} elseif ( is_feed() && 'old' == get_query_var('feed') ) { |
|
301 |
$old_feed_files = array( |
|
302 |
'wp-atom.php' => 'atom', |
|
303 |
'wp-commentsrss2.php' => 'comments_rss2', |
|
304 |
'wp-feed.php' => get_default_feed(), |
|
305 |
'wp-rdf.php' => 'rdf', |
|
306 |
'wp-rss.php' => 'rss2', |
|
307 |
'wp-rss2.php' => 'rss2', |
|
308 |
); |
|
309 |
if ( isset( $old_feed_files[ basename( $redirect['path'] ) ] ) ) { |
|
310 |
$redirect_url = get_feed_link( $old_feed_files[ basename( $redirect['path'] ) ] ); |
|
311 |
wp_redirect( $redirect_url, 301 ); |
|
312 |
die(); |
|
313 |
} |
|
314 |
} |
|
315 |
||
316 |
if ( get_query_var('paged') > 0 ) { |
|
317 |
$paged = get_query_var('paged'); |
|
318 |
$redirect['query'] = remove_query_arg( 'paged', $redirect['query'] ); |
|
319 |
if ( !is_feed() ) { |
|
320 |
if ( $paged > 1 && !is_single() ) { |
|
321 |
$addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit("$wp_rewrite->pagination_base/$paged", 'paged'); |
|
322 |
} elseif ( !is_single() ) { |
|
323 |
$addl_path = !empty( $addl_path ) ? trailingslashit($addl_path) : ''; |
|
324 |
} |
|
325 |
} elseif ( $paged > 1 ) { |
|
326 |
$redirect['query'] = add_query_arg( 'paged', $paged, $redirect['query'] ); |
|
327 |
} |
|
328 |
} |
|
329 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
330 |
if ( get_option( 'page_comments' ) && ( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
331 |
( 'newest' == get_option( 'default_comments_page' ) && get_query_var( 'cpage' ) > 0 ) || |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
332 |
( 'newest' != get_option( 'default_comments_page' ) && get_query_var( 'cpage' ) > 1 ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
333 |
) ) { |
5 | 334 |
$addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit( $wp_rewrite->comments_pagination_base . '-' . get_query_var('cpage'), 'commentpaged' ); |
0 | 335 |
$redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] ); |
336 |
} |
|
337 |
||
338 |
$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 ) |
|
340 |
$redirect['path'] = trailingslashit($redirect['path']) . $wp_rewrite->index . '/'; |
|
341 |
if ( !empty( $addl_path ) ) |
|
342 |
$redirect['path'] = trailingslashit($redirect['path']) . $addl_path; |
|
343 |
$redirect_url = $redirect['scheme'] . '://' . $redirect['host'] . $redirect['path']; |
|
344 |
} |
|
345 |
||
346 |
if ( 'wp-register.php' == basename( $redirect['path'] ) ) { |
|
5 | 347 |
if ( is_multisite() ) { |
0 | 348 |
/** This filter is documented in wp-login.php */ |
349 |
$redirect_url = apply_filters( 'wp_signup_location', network_site_url( 'wp-signup.php' ) ); |
|
5 | 350 |
} else { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
351 |
$redirect_url = wp_registration_url(); |
5 | 352 |
} |
353 |
||
0 | 354 |
wp_redirect( $redirect_url, 301 ); |
355 |
die(); |
|
356 |
} |
|
357 |
} |
|
358 |
||
359 |
// tack on any additional query vars |
|
360 |
$redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] ); |
|
361 |
if ( $redirect_url && !empty($redirect['query']) ) { |
|
362 |
parse_str( $redirect['query'], $_parsed_query ); |
|
363 |
$redirect = @parse_url($redirect_url); |
|
364 |
||
365 |
if ( ! empty( $_parsed_query['name'] ) && ! empty( $redirect['query'] ) ) { |
|
366 |
parse_str( $redirect['query'], $_parsed_redirect_query ); |
|
367 |
||
368 |
if ( empty( $_parsed_redirect_query['name'] ) ) |
|
369 |
unset( $_parsed_query['name'] ); |
|
370 |
} |
|
371 |
||
372 |
$_parsed_query = rawurlencode_deep( $_parsed_query ); |
|
373 |
$redirect_url = add_query_arg( $_parsed_query, $redirect_url ); |
|
374 |
} |
|
375 |
||
376 |
if ( $redirect_url ) |
|
377 |
$redirect = @parse_url($redirect_url); |
|
378 |
||
379 |
// www.example.com vs example.com |
|
380 |
$user_home = @parse_url(home_url()); |
|
381 |
if ( !empty($user_home['host']) ) |
|
382 |
$redirect['host'] = $user_home['host']; |
|
383 |
if ( empty($user_home['path']) ) |
|
384 |
$user_home['path'] = '/'; |
|
385 |
||
386 |
// Handle ports |
|
387 |
if ( !empty($user_home['port']) ) |
|
388 |
$redirect['port'] = $user_home['port']; |
|
389 |
else |
|
390 |
unset($redirect['port']); |
|
391 |
||
392 |
// trailing /index.php |
|
393 |
$redirect['path'] = preg_replace('|/' . preg_quote( $wp_rewrite->index, '|' ) . '/*?$|', '/', $redirect['path']); |
|
394 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
395 |
$punctuation_pattern = implode( '|', array_map( 'preg_quote', array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
396 |
' ', '%20', // space |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
397 |
'!', '%21', // exclamation mark |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
398 |
'"', '%22', // double quote |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
399 |
"'", '%27', // single quote |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
400 |
'(', '%28', // opening bracket |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
401 |
')', '%29', // closing bracket |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
402 |
',', '%2C', // comma |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
403 |
'.', '%2E', // period |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
404 |
';', '%3B', // semicolon |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
405 |
'{', '%7B', // opening curly bracket |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
406 |
'}', '%7D', // closing curly bracket |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
407 |
'%E2%80%9C', // opening curly quote |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
408 |
'%E2%80%9D', // closing curly quote |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
409 |
) ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
410 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
411 |
// Remove trailing spaces and end punctuation from the path. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
412 |
$redirect['path'] = preg_replace( "#($punctuation_pattern)+$#", '', $redirect['path'] ); |
0 | 413 |
|
414 |
if ( !empty( $redirect['query'] ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
415 |
// Remove trailing spaces and end punctuation from certain terminating query string args. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
416 |
$redirect['query'] = preg_replace( "#((p|page_id|cat|tag)=[^&]*?)($punctuation_pattern)+$#", '$1', $redirect['query'] ); |
0 | 417 |
|
418 |
// Clean up empty query strings |
|
419 |
$redirect['query'] = trim(preg_replace( '#(^|&)(p|page_id|cat|tag)=?(&|$)#', '&', $redirect['query']), '&'); |
|
420 |
||
421 |
// Redirect obsolete feeds |
|
422 |
$redirect['query'] = preg_replace( '#(^|&)feed=rss(&|$)#', '$1feed=rss2$2', $redirect['query'] ); |
|
423 |
||
424 |
// Remove redundant leading ampersands |
|
425 |
$redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] ); |
|
426 |
} |
|
427 |
||
428 |
// strip /index.php/ when we're not using PATHINFO permalinks |
|
429 |
if ( !$wp_rewrite->using_index_permalinks() ) |
|
430 |
$redirect['path'] = str_replace( '/' . $wp_rewrite->index . '/', '/', $redirect['path'] ); |
|
431 |
||
432 |
// 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) ) ) ) { |
|
434 |
$user_ts_type = ''; |
|
435 |
if ( get_query_var('paged') > 0 ) { |
|
436 |
$user_ts_type = 'paged'; |
|
437 |
} else { |
|
438 |
foreach ( array('single', 'category', 'page', 'day', 'month', 'year', 'home') as $type ) { |
|
439 |
$func = 'is_' . $type; |
|
440 |
if ( call_user_func($func) ) { |
|
441 |
$user_ts_type = $type; |
|
442 |
break; |
|
443 |
} |
|
444 |
} |
|
445 |
} |
|
446 |
$redirect['path'] = user_trailingslashit($redirect['path'], $user_ts_type); |
|
447 |
} elseif ( is_front_page() ) { |
|
448 |
$redirect['path'] = trailingslashit($redirect['path']); |
|
449 |
} |
|
450 |
||
451 |
// Strip multiple slashes out of the URL |
|
452 |
if ( strpos($redirect['path'], '//') > -1 ) |
|
453 |
$redirect['path'] = preg_replace('|/+|', '/', $redirect['path']); |
|
454 |
||
455 |
// Always trailing slash the Front Page URL |
|
456 |
if ( trailingslashit( $redirect['path'] ) == trailingslashit( $user_home['path'] ) ) |
|
457 |
$redirect['path'] = trailingslashit($redirect['path']); |
|
458 |
||
459 |
// Ignore differences in host capitalization, as this can lead to infinite redirects |
|
460 |
// Only redirect no-www <=> yes-www |
|
461 |
if ( strtolower($original['host']) == strtolower($redirect['host']) || |
|
462 |
( strtolower($original['host']) != 'www.' . strtolower($redirect['host']) && 'www.' . strtolower($original['host']) != strtolower($redirect['host']) ) ) |
|
463 |
$redirect['host'] = $original['host']; |
|
464 |
||
5 | 465 |
$compare_original = array( $original['host'], $original['path'] ); |
0 | 466 |
|
467 |
if ( !empty( $original['port'] ) ) |
|
468 |
$compare_original[] = $original['port']; |
|
469 |
||
470 |
if ( !empty( $original['query'] ) ) |
|
471 |
$compare_original[] = $original['query']; |
|
472 |
||
5 | 473 |
$compare_redirect = array( $redirect['host'], $redirect['path'] ); |
0 | 474 |
|
475 |
if ( !empty( $redirect['port'] ) ) |
|
476 |
$compare_redirect[] = $redirect['port']; |
|
477 |
||
478 |
if ( !empty( $redirect['query'] ) ) |
|
479 |
$compare_redirect[] = $redirect['query']; |
|
480 |
||
481 |
if ( $compare_original !== $compare_redirect ) { |
|
482 |
$redirect_url = $redirect['scheme'] . '://' . $redirect['host']; |
|
483 |
if ( !empty($redirect['port']) ) |
|
484 |
$redirect_url .= ':' . $redirect['port']; |
|
485 |
$redirect_url .= $redirect['path']; |
|
486 |
if ( !empty($redirect['query']) ) |
|
487 |
$redirect_url .= '?' . $redirect['query']; |
|
488 |
} |
|
489 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
490 |
if ( ! $redirect_url || $redirect_url == $requested_url ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
491 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
492 |
} |
0 | 493 |
|
494 |
// Hex encoded octets are case-insensitive. |
|
495 |
if ( false !== strpos($requested_url, '%') ) { |
|
496 |
if ( !function_exists('lowercase_octets') ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
497 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
498 |
* Converts the first hex-encoded octet match to lowercase. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
499 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
500 |
* @since 3.1.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
501 |
* @ignore |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
502 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
503 |
* @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
|
504 |
* @return string Lowercased version of the first match. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
505 |
*/ |
0 | 506 |
function lowercase_octets($matches) { |
507 |
return strtolower( $matches[0] ); |
|
508 |
} |
|
509 |
} |
|
510 |
$requested_url = preg_replace_callback('|%[a-fA-F0-9][a-fA-F0-9]|', 'lowercase_octets', $requested_url); |
|
511 |
} |
|
512 |
||
513 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
514 |
* Filters the canonical redirect URL. |
0 | 515 |
* |
516 |
* Returning false to this filter will cancel the redirect. |
|
517 |
* |
|
518 |
* @since 2.3.0 |
|
519 |
* |
|
520 |
* @param string $redirect_url The redirect URL. |
|
521 |
* @param string $requested_url The requested URL. |
|
522 |
*/ |
|
523 |
$redirect_url = apply_filters( 'redirect_canonical', $redirect_url, $requested_url ); |
|
524 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
525 |
// yes, again -- in case the filter aborted the request |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
526 |
if ( ! $redirect_url || strip_fragment_from_url( $redirect_url ) == strip_fragment_from_url( $requested_url ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
527 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
528 |
} |
0 | 529 |
|
530 |
if ( $do_redirect ) { |
|
531 |
// protect against chained redirects |
|
532 |
if ( !redirect_canonical($redirect_url, false) ) { |
|
533 |
wp_redirect($redirect_url, 301); |
|
534 |
exit(); |
|
535 |
} else { |
|
536 |
// Debug |
|
537 |
// 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
|
538 |
return; |
0 | 539 |
} |
540 |
} else { |
|
541 |
return $redirect_url; |
|
542 |
} |
|
543 |
} |
|
544 |
||
545 |
/** |
|
546 |
* Removes arguments from a query string if they are not present in a URL |
|
547 |
* DO NOT use this in plugin code. |
|
548 |
* |
|
5 | 549 |
* @since 3.4.0 |
0 | 550 |
* @access private |
551 |
* |
|
5 | 552 |
* @param string $query_string |
553 |
* @param array $args_to_check |
|
554 |
* @param string $url |
|
0 | 555 |
* @return string The altered query string |
556 |
*/ |
|
557 |
function _remove_qs_args_if_not_in_url( $query_string, Array $args_to_check, $url ) { |
|
558 |
$parsed_url = @parse_url( $url ); |
|
559 |
if ( ! empty( $parsed_url['query'] ) ) { |
|
560 |
parse_str( $parsed_url['query'], $parsed_query ); |
|
561 |
foreach ( $args_to_check as $qv ) { |
|
562 |
if ( !isset( $parsed_query[$qv] ) ) |
|
563 |
$query_string = remove_query_arg( $qv, $query_string ); |
|
564 |
} |
|
565 |
} else { |
|
566 |
$query_string = remove_query_arg( $args_to_check, $query_string ); |
|
567 |
} |
|
568 |
return $query_string; |
|
569 |
} |
|
570 |
||
571 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
572 |
* 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
|
573 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
574 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
575 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
576 |
* @param string $url The URL to strip. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
577 |
* @return string The altered URL. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
578 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
579 |
function strip_fragment_from_url( $url ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
580 |
$parsed_url = @parse_url( $url ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
581 |
if ( ! empty( $parsed_url['host'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
582 |
// 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
|
583 |
$url = $parsed_url['scheme'] . '://' . $parsed_url['host']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
584 |
if ( ! empty( $parsed_url['port'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
585 |
$url .= ':' . $parsed_url['port']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
586 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
587 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
588 |
if ( ! empty( $parsed_url['path'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
589 |
$url .= $parsed_url['path']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
590 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
591 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
592 |
if ( ! empty( $parsed_url['query'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
593 |
$url .= '?' . $parsed_url['query']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
594 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
595 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
596 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
597 |
return $url; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
598 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
599 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
600 |
/** |
0 | 601 |
* Attempts to guess the correct URL based on query vars |
602 |
* |
|
603 |
* @since 2.3.0 |
|
5 | 604 |
* |
605 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
0 | 606 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
607 |
* @return false|string The correct URL if one is found. False on failure. |
0 | 608 |
*/ |
609 |
function redirect_guess_404_permalink() { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
610 |
global $wpdb; |
0 | 611 |
|
612 |
if ( get_query_var('name') ) { |
|
5 | 613 |
$where = $wpdb->prepare("post_name LIKE %s", $wpdb->esc_like( get_query_var('name') ) . '%'); |
0 | 614 |
|
615 |
// if any of post_type, year, monthnum, or day are set, use them to refine the query |
|
616 |
if ( get_query_var('post_type') ) |
|
617 |
$where .= $wpdb->prepare(" AND post_type = %s", get_query_var('post_type')); |
|
618 |
else |
|
619 |
$where .= " AND post_type IN ('" . implode( "', '", get_post_types( array( 'public' => true ) ) ) . "')"; |
|
620 |
||
621 |
if ( get_query_var('year') ) |
|
622 |
$where .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var('year')); |
|
623 |
if ( get_query_var('monthnum') ) |
|
624 |
$where .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var('monthnum')); |
|
625 |
if ( get_query_var('day') ) |
|
626 |
$where .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var('day')); |
|
627 |
||
628 |
$post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'"); |
|
629 |
if ( ! $post_id ) |
|
630 |
return false; |
|
631 |
if ( get_query_var( 'feed' ) ) |
|
632 |
return get_post_comments_feed_link( $post_id, get_query_var( 'feed' ) ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
633 |
elseif ( get_query_var( 'page' ) && 1 < get_query_var( 'page' ) ) |
0 | 634 |
return trailingslashit( get_permalink( $post_id ) ) . user_trailingslashit( get_query_var( 'page' ), 'single_paged' ); |
635 |
else |
|
636 |
return get_permalink( $post_id ); |
|
637 |
} |
|
638 |
||
639 |
return false; |
|
640 |
} |
|
641 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
642 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
643 |
* Redirects a variety of shorthand URLs to the admin. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
644 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
645 |
* 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
|
646 |
* 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
|
647 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
648 |
* @since 3.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
649 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
650 |
* @global WP_Rewrite $wp_rewrite |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
651 |
*/ |
0 | 652 |
function wp_redirect_admin_locations() { |
653 |
global $wp_rewrite; |
|
654 |
if ( ! ( is_404() && $wp_rewrite->using_permalinks() ) ) |
|
655 |
return; |
|
656 |
||
657 |
$admins = array( |
|
658 |
home_url( 'wp-admin', 'relative' ), |
|
659 |
home_url( 'dashboard', 'relative' ), |
|
660 |
home_url( 'admin', 'relative' ), |
|
661 |
site_url( 'dashboard', 'relative' ), |
|
662 |
site_url( 'admin', 'relative' ), |
|
663 |
); |
|
664 |
if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $admins ) ) { |
|
665 |
wp_redirect( admin_url() ); |
|
666 |
exit; |
|
667 |
} |
|
668 |
||
669 |
$logins = array( |
|
670 |
home_url( 'wp-login.php', 'relative' ), |
|
671 |
home_url( 'login', 'relative' ), |
|
672 |
site_url( 'login', 'relative' ), |
|
673 |
); |
|
674 |
if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $logins ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
675 |
wp_redirect( wp_login_url() ); |
0 | 676 |
exit; |
677 |
} |
|
678 |
} |