PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wordpress3.4.2/wp-includes/canonical.php

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