PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/www/wp-includes/canonical.php

https://github.com/navbains/gitwordpress
PHP | 560 lines | 387 code | 73 blank | 100 comment | 210 complexity | 4fda06ecbf41592f194b0fc824db40bd MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.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. * Redirects incoming links to the proper URL based on the site url.
  13. *
  14. * Search engines consider www.somedomain.com and somedomain.com to be two
  15. * different URLs when they both go to the same location. This SEO enhancement
  16. * prevents penalty for duplicate content by redirecting all incoming links to
  17. * one or the other.
  18. *
  19. * Prevents redirection for feeds, trackbacks, searches, comment popup, and
  20. * admin URLs. Does not redirect on non-pretty-permalink-supporting IIS 7+,
  21. * page/post previews, WP admin, Trackbacks, robots.txt, searches, or on POST
  22. * requests.
  23. *
  24. * Will also attempt to find the correct link when a user enters a URL that does
  25. * not exist based on exact WordPress query. Will instead try to parse the URL
  26. * or query in an attempt to figure the correct page to go to.
  27. *
  28. * @since 2.3.0
  29. * @uses $wp_rewrite
  30. * @uses $is_IIS
  31. *
  32. * @param string $requested_url Optional. The URL that was requested, used to
  33. * figure if redirect is needed.
  34. * @param bool $do_redirect Optional. Redirect to the new URL.
  35. * @return null|false|string Null, if redirect not needed. False, if redirect
  36. * not needed or the string of the URL
  37. */
  38. function redirect_canonical( $requested_url = null, $do_redirect = true ) {
  39. global $wp_rewrite, $is_IIS, $wp_query, $wpdb;
  40. if ( is_trackback() || is_search() || is_comments_popup() || is_admin() || !empty($_POST) || is_preview() || is_robots() || ( $is_IIS && !iis7_supports_permalinks() ) )
  41. return;
  42. if ( !$requested_url ) {
  43. // build the URL in the address bar
  44. $requested_url = is_ssl() ? 'https://' : 'http://';
  45. $requested_url .= $_SERVER['HTTP_HOST'];
  46. $requested_url .= $_SERVER['REQUEST_URI'];
  47. }
  48. $original = @parse_url($requested_url);
  49. if ( false === $original )
  50. return;
  51. // Some PHP setups turn requests for / into /index.php in REQUEST_URI
  52. // See: http://trac.wordpress.org/ticket/5017
  53. // See: http://trac.wordpress.org/ticket/7173
  54. // Disabled, for now:
  55. // $original['path'] = preg_replace('|/index\.php$|', '/', $original['path']);
  56. $redirect = $original;
  57. $redirect_url = false;
  58. // Notice fixing
  59. if ( !isset($redirect['path']) )
  60. $redirect['path'] = '';
  61. if ( !isset($redirect['query']) )
  62. $redirect['query'] = '';
  63. if ( is_feed() && ( $id = get_query_var( 'p' ) ) ) {
  64. if ( $redirect_url = get_post_comments_feed_link( $id, get_query_var( 'feed' ) ) ) {
  65. $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type', 'feed'), $redirect_url );
  66. $redirect['path'] = parse_url( $redirect_url, PHP_URL_PATH );
  67. }
  68. }
  69. if ( is_singular() && 1 > $wp_query->post_count && ($id = get_query_var('p')) ) {
  70. $vars = $wpdb->get_results( $wpdb->prepare("SELECT post_type, post_parent FROM $wpdb->posts WHERE ID = %d", $id) );
  71. if ( isset($vars[0]) && $vars = $vars[0] ) {
  72. if ( 'revision' == $vars->post_type && $vars->post_parent > 0 )
  73. $id = $vars->post_parent;
  74. if ( $redirect_url = get_permalink($id) )
  75. $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url );
  76. }
  77. }
  78. // These tests give us a WP-generated permalink
  79. if ( is_404() ) {
  80. // Redirect ?page_id, ?p=, ?attachment_id= to their respective url's
  81. $id = max( get_query_var('p'), get_query_var('page_id'), get_query_var('attachment_id') );
  82. if ( $id && $redirect_post = get_post($id) ) {
  83. $post_type_obj = get_post_type_object($redirect_post->post_type);
  84. if ( $post_type_obj->public ) {
  85. $redirect_url = get_permalink($redirect_post);
  86. $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url );
  87. }
  88. }
  89. if ( get_query_var( 'day' ) && get_query_var( 'monthnum' ) && get_query_var( 'year' ) ) {
  90. $year = get_query_var( 'year' );
  91. $month = get_query_var( 'monthnum' );
  92. $day = get_query_var( 'day' );
  93. $date = sprintf( '%04d-%02d-%02d', $year, $month, $day );
  94. if ( ! wp_checkdate( $month, $day, $year, $date ) ) {
  95. $redirect_url = get_month_link( $year, $month );
  96. $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'year', 'monthnum', 'day' ), $redirect_url );
  97. }
  98. } elseif ( get_query_var( 'monthnum' ) && get_query_var( 'year' ) && 12 < get_query_var( 'monthnum' ) ) {
  99. $redirect_url = get_year_link( get_query_var( 'year' ) );
  100. $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'year', 'monthnum' ), $redirect_url );
  101. }
  102. if ( ! $redirect_url ) {
  103. if ( $redirect_url = redirect_guess_404_permalink() ) {
  104. $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'page', 'feed', 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url );
  105. }
  106. }
  107. } elseif ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) {
  108. // rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101
  109. if ( is_attachment() && !empty($_GET['attachment_id']) && ! $redirect_url ) {
  110. if ( $redirect_url = get_attachment_link(get_query_var('attachment_id')) )
  111. $redirect['query'] = remove_query_arg('attachment_id', $redirect['query']);
  112. } elseif ( is_single() && !empty($_GET['p']) && ! $redirect_url ) {
  113. if ( $redirect_url = get_permalink(get_query_var('p')) )
  114. $redirect['query'] = remove_query_arg(array('p', 'post_type'), $redirect['query']);
  115. } elseif ( is_single() && !empty($_GET['name']) && ! $redirect_url ) {
  116. if ( $redirect_url = get_permalink( $wp_query->get_queried_object_id() ) )
  117. $redirect['query'] = remove_query_arg('name', $redirect['query']);
  118. } elseif ( is_page() && !empty($_GET['page_id']) && ! $redirect_url ) {
  119. if ( $redirect_url = get_permalink(get_query_var('page_id')) )
  120. $redirect['query'] = remove_query_arg('page_id', $redirect['query']);
  121. } elseif ( is_page() && !is_feed() && isset($wp_query->queried_object) && 'page' == get_option('show_on_front') && $wp_query->queried_object->ID == get_option('page_on_front') && ! $redirect_url ) {
  122. $redirect_url = home_url('/');
  123. } 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 ) {
  124. if ( $redirect_url = get_permalink(get_option('page_for_posts')) )
  125. $redirect['query'] = remove_query_arg('page_id', $redirect['query']);
  126. } elseif ( !empty($_GET['m']) && ( is_year() || is_month() || is_day() ) ) {
  127. $m = get_query_var('m');
  128. switch ( strlen($m) ) {
  129. case 4: // Yearly
  130. $redirect_url = get_year_link($m);
  131. break;
  132. case 6: // Monthly
  133. $redirect_url = get_month_link( substr($m, 0, 4), substr($m, 4, 2) );
  134. break;
  135. case 8: // Daily
  136. $redirect_url = get_day_link(substr($m, 0, 4), substr($m, 4, 2), substr($m, 6, 2));
  137. break;
  138. }
  139. if ( $redirect_url )
  140. $redirect['query'] = remove_query_arg('m', $redirect['query']);
  141. // now moving on to non ?m=X year/month/day links
  142. } elseif ( is_day() && get_query_var('year') && get_query_var('monthnum') && !empty($_GET['day']) ) {
  143. if ( $redirect_url = get_day_link(get_query_var('year'), get_query_var('monthnum'), get_query_var('day')) )
  144. $redirect['query'] = remove_query_arg(array('year', 'monthnum', 'day'), $redirect['query']);
  145. } elseif ( is_month() && get_query_var('year') && !empty($_GET['monthnum']) ) {
  146. if ( $redirect_url = get_month_link(get_query_var('year'), get_query_var('monthnum')) )
  147. $redirect['query'] = remove_query_arg(array('year', 'monthnum'), $redirect['query']);
  148. } elseif ( is_year() && !empty($_GET['year']) ) {
  149. if ( $redirect_url = get_year_link(get_query_var('year')) )
  150. $redirect['query'] = remove_query_arg('year', $redirect['query']);
  151. } elseif ( is_author() && !empty($_GET['author']) && preg_match( '|^[0-9]+$|', $_GET['author'] ) ) {
  152. $author = get_userdata(get_query_var('author'));
  153. 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 ) ) ) {
  154. if ( $redirect_url = get_author_posts_url($author->ID, $author->user_nicename) )
  155. $redirect['query'] = remove_query_arg('author', $redirect['query']);
  156. }
  157. } elseif ( is_category() || is_tag() || is_tax() ) { // Terms (Tags/categories)
  158. $term_count = 0;
  159. foreach ( $wp_query->tax_query->queries as $tax_query )
  160. $term_count += count( $tax_query['terms'] );
  161. $obj = $wp_query->get_queried_object();
  162. if ( $term_count <= 1 && !empty($obj->term_id) && ( $tax_url = get_term_link((int)$obj->term_id, $obj->taxonomy) ) && !is_wp_error($tax_url) ) {
  163. if ( !empty($redirect['query']) ) {
  164. // Strip taxonomy query vars off the url.
  165. $qv_remove = array( 'term', 'taxonomy');
  166. if ( is_category() ) {
  167. $qv_remove[] = 'category_name';
  168. $qv_remove[] = 'cat';
  169. } elseif ( is_tag() ) {
  170. $qv_remove[] = 'tag';
  171. $qv_remove[] = 'tag_id';
  172. } else { // Custom taxonomies will have a custom query var, remove those too:
  173. $tax_obj = get_taxonomy( $obj->taxonomy );
  174. if ( false !== $tax_obj->query_var )
  175. $qv_remove[] = $tax_obj->query_var;
  176. }
  177. $rewrite_vars = array_diff( array_keys($wp_query->query), array_keys($_GET) );
  178. 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
  179. $redirect['query'] = remove_query_arg($qv_remove, $redirect['query']); //Remove all of the per-tax qv's
  180. // Create the destination url for this taxonomy
  181. $tax_url = parse_url($tax_url);
  182. if ( ! empty($tax_url['query']) ) { // Taxonomy accessible via ?taxonomy=..&term=.. or any custom qv..
  183. parse_str($tax_url['query'], $query_vars);
  184. $redirect['query'] = add_query_arg($query_vars, $redirect['query']);
  185. } else { // Taxonomy is accessible via a "pretty-URL"
  186. $redirect['path'] = $tax_url['path'];
  187. }
  188. } else { // Some query vars are set via $_GET. Unset those from $_GET that exist via the rewrite
  189. foreach ( $qv_remove as $_qv ) {
  190. if ( isset($rewrite_vars[$_qv]) )
  191. $redirect['query'] = remove_query_arg($_qv, $redirect['query']);
  192. }
  193. }
  194. }
  195. }
  196. } elseif ( is_single() && strpos($wp_rewrite->permalink_structure, '%category%') !== false && $cat = get_query_var( 'category_name' ) ) {
  197. $category = get_category_by_path( $cat );
  198. $post_terms = wp_get_object_terms($wp_query->get_queried_object_id(), 'category', array('fields' => 'tt_ids'));
  199. if ( (!$category || is_wp_error($category)) || ( !is_wp_error($post_terms) && !empty($post_terms) && !in_array($category->term_taxonomy_id, $post_terms) ) )
  200. $redirect_url = get_permalink($wp_query->get_queried_object_id());
  201. }
  202. // Post Paging
  203. if ( is_singular() && ! is_front_page() && get_query_var('page') ) {
  204. if ( !$redirect_url )
  205. $redirect_url = get_permalink( get_queried_object_id() );
  206. $redirect_url = trailingslashit( $redirect_url ) . user_trailingslashit( get_query_var( 'page' ), 'single_paged' );
  207. $redirect['query'] = remove_query_arg( 'page', $redirect['query'] );
  208. }
  209. // paging and feeds
  210. if ( get_query_var('paged') || is_feed() || get_query_var('cpage') ) {
  211. while ( preg_match( "#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", $redirect['path'] ) || preg_match( '#/(comments/?)?(feed|rss|rdf|atom|rss2)(/+)?$#', $redirect['path'] ) || preg_match( '#/comment-page-[0-9]+(/+)?$#', $redirect['path'] ) ) {
  212. // Strip off paging and feed
  213. $redirect['path'] = preg_replace("#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", '/', $redirect['path']); // strip off any existing paging
  214. $redirect['path'] = preg_replace('#/(comments/?)?(feed|rss2?|rdf|atom)(/+|$)#', '/', $redirect['path']); // strip off feed endings
  215. $redirect['path'] = preg_replace('#/comment-page-[0-9]+?(/+)?$#', '/', $redirect['path']); // strip off any existing comment paging
  216. }
  217. $addl_path = '';
  218. if ( is_feed() && in_array( get_query_var('feed'), $wp_rewrite->feeds ) ) {
  219. $addl_path = !empty( $addl_path ) ? trailingslashit($addl_path) : '';
  220. if ( !is_singular() && get_query_var( 'withcomments' ) )
  221. $addl_path .= 'comments/';
  222. if ( ( 'rss' == get_default_feed() && 'feed' == get_query_var('feed') ) || 'rss' == get_query_var('feed') )
  223. $addl_path .= user_trailingslashit( 'feed/' . ( ( get_default_feed() == 'rss2' ) ? '' : 'rss2' ), 'feed' );
  224. else
  225. $addl_path .= user_trailingslashit( 'feed/' . ( ( get_default_feed() == get_query_var('feed') || 'feed' == get_query_var('feed') ) ? '' : get_query_var('feed') ), 'feed' );
  226. $redirect['query'] = remove_query_arg( 'feed', $redirect['query'] );
  227. } elseif ( is_feed() && 'old' == get_query_var('feed') ) {
  228. $old_feed_files = array(
  229. 'wp-atom.php' => 'atom',
  230. 'wp-commentsrss2.php' => 'comments_rss2',
  231. 'wp-feed.php' => get_default_feed(),
  232. 'wp-rdf.php' => 'rdf',
  233. 'wp-rss.php' => 'rss2',
  234. 'wp-rss2.php' => 'rss2',
  235. );
  236. if ( isset( $old_feed_files[ basename( $redirect['path'] ) ] ) ) {
  237. $redirect_url = get_feed_link( $old_feed_files[ basename( $redirect['path'] ) ] );
  238. wp_redirect( $redirect_url, 301 );
  239. die();
  240. }
  241. }
  242. if ( get_query_var('paged') > 0 ) {
  243. $paged = get_query_var('paged');
  244. $redirect['query'] = remove_query_arg( 'paged', $redirect['query'] );
  245. if ( !is_feed() ) {
  246. if ( $paged > 1 && !is_single() ) {
  247. $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit("$wp_rewrite->pagination_base/$paged", 'paged');
  248. } elseif ( !is_single() ) {
  249. $addl_path = !empty( $addl_path ) ? trailingslashit($addl_path) : '';
  250. }
  251. } elseif ( $paged > 1 ) {
  252. $redirect['query'] = add_query_arg( 'paged', $paged, $redirect['query'] );
  253. }
  254. }
  255. if ( get_option('page_comments') && ( ( 'newest' == get_option('default_comments_page') && get_query_var('cpage') > 0 ) || ( 'newest' != get_option('default_comments_page') && get_query_var('cpage') > 1 ) ) ) {
  256. $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit( 'comment-page-' . get_query_var('cpage'), 'commentpaged' );
  257. $redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] );
  258. }
  259. $redirect['path'] = user_trailingslashit( preg_replace('|/' . preg_quote( $wp_rewrite->index, '|' ) . '/?$|', '/', $redirect['path']) ); // strip off trailing /index.php/
  260. if ( !empty( $addl_path ) && $wp_rewrite->using_index_permalinks() && strpos($redirect['path'], '/' . $wp_rewrite->index . '/') === false )
  261. $redirect['path'] = trailingslashit($redirect['path']) . $wp_rewrite->index . '/';
  262. if ( !empty( $addl_path ) )
  263. $redirect['path'] = trailingslashit($redirect['path']) . $addl_path;
  264. $redirect_url = $redirect['scheme'] . '://' . $redirect['host'] . $redirect['path'];
  265. }
  266. if ( 'wp-register.php' == basename( $redirect['path'] ) ) {
  267. if ( is_multisite() )
  268. /** This filter is documented in wp-login.php */
  269. $redirect_url = apply_filters( 'wp_signup_location', network_site_url( 'wp-signup.php' ) );
  270. else
  271. $redirect_url = site_url( 'wp-login.php?action=register' );
  272. wp_redirect( $redirect_url, 301 );
  273. die();
  274. }
  275. }
  276. // tack on any additional query vars
  277. $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] );
  278. if ( $redirect_url && !empty($redirect['query']) ) {
  279. parse_str( $redirect['query'], $_parsed_query );
  280. $redirect = @parse_url($redirect_url);
  281. if ( ! empty( $_parsed_query['name'] ) && ! empty( $redirect['query'] ) ) {
  282. parse_str( $redirect['query'], $_parsed_redirect_query );
  283. if ( empty( $_parsed_redirect_query['name'] ) )
  284. unset( $_parsed_query['name'] );
  285. }
  286. $_parsed_query = rawurlencode_deep( $_parsed_query );
  287. $redirect_url = add_query_arg( $_parsed_query, $redirect_url );
  288. }
  289. if ( $redirect_url )
  290. $redirect = @parse_url($redirect_url);
  291. // www.example.com vs example.com
  292. $user_home = @parse_url(home_url());
  293. if ( !empty($user_home['host']) )
  294. $redirect['host'] = $user_home['host'];
  295. if ( empty($user_home['path']) )
  296. $user_home['path'] = '/';
  297. // Handle ports
  298. if ( !empty($user_home['port']) )
  299. $redirect['port'] = $user_home['port'];
  300. else
  301. unset($redirect['port']);
  302. // trailing /index.php
  303. $redirect['path'] = preg_replace('|/' . preg_quote( $wp_rewrite->index, '|' ) . '/*?$|', '/', $redirect['path']);
  304. // Remove trailing spaces from the path
  305. $redirect['path'] = preg_replace( '#(%20| )+$#', '', $redirect['path'] );
  306. if ( !empty( $redirect['query'] ) ) {
  307. // Remove trailing spaces from certain terminating query string args
  308. $redirect['query'] = preg_replace( '#((p|page_id|cat|tag)=[^&]*?)(%20| )+$#', '$1', $redirect['query'] );
  309. // Clean up empty query strings
  310. $redirect['query'] = trim(preg_replace( '#(^|&)(p|page_id|cat|tag)=?(&|$)#', '&', $redirect['query']), '&');
  311. // Redirect obsolete feeds
  312. $redirect['query'] = preg_replace( '#(^|&)feed=rss(&|$)#', '$1feed=rss2$2', $redirect['query'] );
  313. // Remove redundant leading ampersands
  314. $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] );
  315. }
  316. // strip /index.php/ when we're not using PATHINFO permalinks
  317. if ( !$wp_rewrite->using_index_permalinks() )
  318. $redirect['path'] = str_replace( '/' . $wp_rewrite->index . '/', '/', $redirect['path'] );
  319. // trailing slashes
  320. if ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() && !is_404() && (!is_front_page() || ( is_front_page() && (get_query_var('paged') > 1) ) ) ) {
  321. $user_ts_type = '';
  322. if ( get_query_var('paged') > 0 ) {
  323. $user_ts_type = 'paged';
  324. } else {
  325. foreach ( array('single', 'category', 'page', 'day', 'month', 'year', 'home') as $type ) {
  326. $func = 'is_' . $type;
  327. if ( call_user_func($func) ) {
  328. $user_ts_type = $type;
  329. break;
  330. }
  331. }
  332. }
  333. $redirect['path'] = user_trailingslashit($redirect['path'], $user_ts_type);
  334. } elseif ( is_front_page() ) {
  335. $redirect['path'] = trailingslashit($redirect['path']);
  336. }
  337. // Strip multiple slashes out of the URL
  338. if ( strpos($redirect['path'], '//') > -1 )
  339. $redirect['path'] = preg_replace('|/+|', '/', $redirect['path']);
  340. // Always trailing slash the Front Page URL
  341. if ( trailingslashit( $redirect['path'] ) == trailingslashit( $user_home['path'] ) )
  342. $redirect['path'] = trailingslashit($redirect['path']);
  343. // Ignore differences in host capitalization, as this can lead to infinite redirects
  344. // Only redirect no-www <=> yes-www
  345. if ( strtolower($original['host']) == strtolower($redirect['host']) ||
  346. ( strtolower($original['host']) != 'www.' . strtolower($redirect['host']) && 'www.' . strtolower($original['host']) != strtolower($redirect['host']) ) )
  347. $redirect['host'] = $original['host'];
  348. $compare_original = array($original['host'], $original['path']);
  349. if ( !empty( $original['port'] ) )
  350. $compare_original[] = $original['port'];
  351. if ( !empty( $original['query'] ) )
  352. $compare_original[] = $original['query'];
  353. $compare_redirect = array($redirect['host'], $redirect['path']);
  354. if ( !empty( $redirect['port'] ) )
  355. $compare_redirect[] = $redirect['port'];
  356. if ( !empty( $redirect['query'] ) )
  357. $compare_redirect[] = $redirect['query'];
  358. if ( $compare_original !== $compare_redirect ) {
  359. $redirect_url = $redirect['scheme'] . '://' . $redirect['host'];
  360. if ( !empty($redirect['port']) )
  361. $redirect_url .= ':' . $redirect['port'];
  362. $redirect_url .= $redirect['path'];
  363. if ( !empty($redirect['query']) )
  364. $redirect_url .= '?' . $redirect['query'];
  365. }
  366. if ( !$redirect_url || $redirect_url == $requested_url )
  367. return false;
  368. // Hex encoded octets are case-insensitive.
  369. if ( false !== strpos($requested_url, '%') ) {
  370. if ( !function_exists('lowercase_octets') ) {
  371. function lowercase_octets($matches) {
  372. return strtolower( $matches[0] );
  373. }
  374. }
  375. $requested_url = preg_replace_callback('|%[a-fA-F0-9][a-fA-F0-9]|', 'lowercase_octets', $requested_url);
  376. }
  377. /**
  378. * Filter the canonical redirect URL.
  379. *
  380. * Returning false to this filter will cancel the redirect.
  381. *
  382. * @since 2.3.0
  383. *
  384. * @param string $redirect_url The redirect URL.
  385. * @param string $requested_url The requested URL.
  386. */
  387. $redirect_url = apply_filters( 'redirect_canonical', $redirect_url, $requested_url );
  388. if ( !$redirect_url || $redirect_url == $requested_url ) // yes, again -- in case the filter aborted the request
  389. return false;
  390. if ( $do_redirect ) {
  391. // protect against chained redirects
  392. if ( !redirect_canonical($redirect_url, false) ) {
  393. wp_redirect($redirect_url, 301);
  394. exit();
  395. } else {
  396. // Debug
  397. // die("1: $redirect_url<br />2: " . redirect_canonical( $redirect_url, false ) );
  398. return false;
  399. }
  400. } else {
  401. return $redirect_url;
  402. }
  403. }
  404. /**
  405. * Removes arguments from a query string if they are not present in a URL
  406. * DO NOT use this in plugin code.
  407. *
  408. * @since 3.4.0
  409. * @access private
  410. *
  411. * @return string The altered query string
  412. */
  413. function _remove_qs_args_if_not_in_url( $query_string, Array $args_to_check, $url ) {
  414. $parsed_url = @parse_url( $url );
  415. if ( ! empty( $parsed_url['query'] ) ) {
  416. parse_str( $parsed_url['query'], $parsed_query );
  417. foreach ( $args_to_check as $qv ) {
  418. if ( !isset( $parsed_query[$qv] ) )
  419. $query_string = remove_query_arg( $qv, $query_string );
  420. }
  421. } else {
  422. $query_string = remove_query_arg( $args_to_check, $query_string );
  423. }
  424. return $query_string;
  425. }
  426. /**
  427. * Attempts to guess the correct URL based on query vars
  428. *
  429. * @since 2.3.0
  430. * @uses $wpdb
  431. *
  432. * @return bool|string The correct URL if one is found. False on failure.
  433. */
  434. function redirect_guess_404_permalink() {
  435. global $wpdb, $wp_rewrite;
  436. if ( get_query_var('name') ) {
  437. $where = $wpdb->prepare("post_name LIKE %s", like_escape( get_query_var('name') ) . '%');
  438. // if any of post_type, year, monthnum, or day are set, use them to refine the query
  439. if ( get_query_var('post_type') )
  440. $where .= $wpdb->prepare(" AND post_type = %s", get_query_var('post_type'));
  441. else
  442. $where .= " AND post_type IN ('" . implode( "', '", get_post_types( array( 'public' => true ) ) ) . "')";
  443. if ( get_query_var('year') )
  444. $where .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var('year'));
  445. if ( get_query_var('monthnum') )
  446. $where .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var('monthnum'));
  447. if ( get_query_var('day') )
  448. $where .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var('day'));
  449. $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'");
  450. if ( ! $post_id )
  451. return false;
  452. if ( get_query_var( 'feed' ) )
  453. return get_post_comments_feed_link( $post_id, get_query_var( 'feed' ) );
  454. elseif ( get_query_var( 'page' ) )
  455. return trailingslashit( get_permalink( $post_id ) ) . user_trailingslashit( get_query_var( 'page' ), 'single_paged' );
  456. else
  457. return get_permalink( $post_id );
  458. }
  459. return false;
  460. }
  461. add_action('template_redirect', 'redirect_canonical');
  462. function wp_redirect_admin_locations() {
  463. global $wp_rewrite;
  464. if ( ! ( is_404() && $wp_rewrite->using_permalinks() ) )
  465. return;
  466. $admins = array(
  467. home_url( 'wp-admin', 'relative' ),
  468. home_url( 'dashboard', 'relative' ),
  469. home_url( 'admin', 'relative' ),
  470. site_url( 'dashboard', 'relative' ),
  471. site_url( 'admin', 'relative' ),
  472. );
  473. if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $admins ) ) {
  474. wp_redirect( admin_url() );
  475. exit;
  476. }
  477. $logins = array(
  478. home_url( 'wp-login.php', 'relative' ),
  479. home_url( 'login', 'relative' ),
  480. site_url( 'login', 'relative' ),
  481. );
  482. if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $logins ) ) {
  483. wp_redirect( site_url( 'wp-login.php', 'login' ) );
  484. exit;
  485. }
  486. }
  487. add_action( 'template_redirect', 'wp_redirect_admin_locations', 1000 );