PageRenderTime 50ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/canonical.php

https://bitbucket.org/aqge/deptandashboard
PHP | 454 lines | 309 code | 65 blank | 80 comment | 185 complexity | 0cf338ce93c60b16a0590ad654ff2447 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1
  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_singular() && 1 > $wp_query->post_count && ($id = get_query_var('p')) ) {
  62. $vars = $wpdb->get_results( $wpdb->prepare("SELECT post_type, post_parent FROM $wpdb->posts WHERE ID = %d", $id) );
  63. if ( isset($vars[0]) && $vars = $vars[0] ) {
  64. if ( 'revision' == $vars->post_type && $vars->post_parent > 0 )
  65. $id = $vars->post_parent;
  66. if ( $redirect_url = get_permalink($id) )
  67. $redirect['query'] = remove_query_arg(array('p', 'page_id', 'attachment_id', 'post_type'), $redirect['query']);
  68. }
  69. }
  70. // These tests give us a WP-generated permalink
  71. if ( is_404() ) {
  72. // Redirect ?page_id, ?p=, ?attachment_id= to their respective url's
  73. $id = max( get_query_var('p'), get_query_var('page_id'), get_query_var('attachment_id') );
  74. if ( $id && $redirect_post = get_post($id) ) {
  75. $post_type_obj = get_post_type_object($redirect_post->post_type);
  76. if ( $post_type_obj->public ) {
  77. $redirect_url = get_permalink($redirect_post);
  78. $redirect['query'] = remove_query_arg(array('p', 'page_id', 'attachment_id', 'post_type'), $redirect['query']);
  79. }
  80. }
  81. if ( ! $redirect_url )
  82. $redirect_url = redirect_guess_404_permalink();
  83. } elseif ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) {
  84. // rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101
  85. if ( is_attachment() && !empty($_GET['attachment_id']) && ! $redirect_url ) {
  86. if ( $redirect_url = get_attachment_link(get_query_var('attachment_id')) )
  87. $redirect['query'] = remove_query_arg('attachment_id', $redirect['query']);
  88. } elseif ( is_single() && !empty($_GET['p']) && ! $redirect_url ) {
  89. if ( $redirect_url = get_permalink(get_query_var('p')) )
  90. $redirect['query'] = remove_query_arg(array('p', 'post_type'), $redirect['query']);
  91. } elseif ( is_single() && !empty($_GET['name']) && ! $redirect_url ) {
  92. if ( $redirect_url = get_permalink( $wp_query->get_queried_object_id() ) )
  93. $redirect['query'] = remove_query_arg('name', $redirect['query']);
  94. } elseif ( is_page() && !empty($_GET['page_id']) && ! $redirect_url ) {
  95. if ( $redirect_url = get_permalink(get_query_var('page_id')) )
  96. $redirect['query'] = remove_query_arg('page_id', $redirect['query']);
  97. } 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 ) {
  98. $redirect_url = home_url('/');
  99. } 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 ) {
  100. if ( $redirect_url = get_permalink(get_option('page_for_posts')) )
  101. $redirect['query'] = remove_query_arg('page_id', $redirect['query']);
  102. } elseif ( !empty($_GET['m']) && ( is_year() || is_month() || is_day() ) ) {
  103. $m = get_query_var('m');
  104. switch ( strlen($m) ) {
  105. case 4: // Yearly
  106. $redirect_url = get_year_link($m);
  107. break;
  108. case 6: // Monthly
  109. $redirect_url = get_month_link( substr($m, 0, 4), substr($m, 4, 2) );
  110. break;
  111. case 8: // Daily
  112. $redirect_url = get_day_link(substr($m, 0, 4), substr($m, 4, 2), substr($m, 6, 2));
  113. break;
  114. }
  115. if ( $redirect_url )
  116. $redirect['query'] = remove_query_arg('m', $redirect['query']);
  117. // now moving on to non ?m=X year/month/day links
  118. } elseif ( is_day() && get_query_var('year') && get_query_var('monthnum') && !empty($_GET['day']) ) {
  119. if ( $redirect_url = get_day_link(get_query_var('year'), get_query_var('monthnum'), get_query_var('day')) )
  120. $redirect['query'] = remove_query_arg(array('year', 'monthnum', 'day'), $redirect['query']);
  121. } elseif ( is_month() && get_query_var('year') && !empty($_GET['monthnum']) ) {
  122. if ( $redirect_url = get_month_link(get_query_var('year'), get_query_var('monthnum')) )
  123. $redirect['query'] = remove_query_arg(array('year', 'monthnum'), $redirect['query']);
  124. } elseif ( is_year() && !empty($_GET['year']) ) {
  125. if ( $redirect_url = get_year_link(get_query_var('year')) )
  126. $redirect['query'] = remove_query_arg('year', $redirect['query']);
  127. } elseif ( is_author() && !empty($_GET['author']) && preg_match( '|^[0-9]+$|', $_GET['author'] ) ) {
  128. $author = get_userdata(get_query_var('author'));
  129. 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 ) ) ) {
  130. if ( $redirect_url = get_author_posts_url($author->ID, $author->user_nicename) )
  131. $redirect['query'] = remove_query_arg('author', $redirect['query']);
  132. }
  133. } elseif ( is_category() || is_tag() || is_tax() ) { // Terms (Tags/categories)
  134. $term_count = 0;
  135. foreach ( $wp_query->tax_query->queries as $tax_query )
  136. $term_count += count( $tax_query['terms'] );
  137. $obj = $wp_query->get_queried_object();
  138. if ( $term_count <= 1 && !empty($obj->term_id) && ( $tax_url = get_term_link((int)$obj->term_id, $obj->taxonomy) ) && !is_wp_error($tax_url) ) {
  139. if ( !empty($redirect['query']) ) {
  140. // Strip taxonomy query vars off the url.
  141. $qv_remove = array( 'term', 'taxonomy');
  142. if ( is_category() ) {
  143. $qv_remove[] = 'category_name';
  144. $qv_remove[] = 'cat';
  145. } elseif ( is_tag() ) {
  146. $qv_remove[] = 'tag';
  147. $qv_remove[] = 'tag_id';
  148. } else { // Custom taxonomies will have a custom query var, remove those too:
  149. $tax_obj = get_taxonomy( $obj->taxonomy );
  150. if ( false !== $tax_obj->query_var )
  151. $qv_remove[] = $tax_obj->query_var;
  152. }
  153. $rewrite_vars = array_diff( array_keys($wp_query->query), array_keys($_GET) );
  154. 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
  155. $redirect['query'] = remove_query_arg($qv_remove, $redirect['query']); //Remove all of the per-tax qv's
  156. // Create the destination url for this taxonomy
  157. $tax_url = parse_url($tax_url);
  158. if ( ! empty($tax_url['query']) ) { // Taxonomy accessible via ?taxonomy=..&term=.. or any custom qv..
  159. parse_str($tax_url['query'], $query_vars);
  160. $redirect['query'] = add_query_arg($query_vars, $redirect['query']);
  161. } else { // Taxonomy is accessible via a "pretty-URL"
  162. $redirect['path'] = $tax_url['path'];
  163. }
  164. } else { // Some query vars are set via $_GET. Unset those from $_GET that exist via the rewrite
  165. foreach ( $qv_remove as $_qv ) {
  166. if ( isset($rewrite_vars[$_qv]) )
  167. $redirect['query'] = remove_query_arg($_qv, $redirect['query']);
  168. }
  169. }
  170. }
  171. }
  172. } elseif ( is_single() && strpos($wp_rewrite->permalink_structure, '%category%') !== false ) {
  173. $category = get_category_by_path(get_query_var('category_name'));
  174. $post_terms = wp_get_object_terms($wp_query->get_queried_object_id(), 'category', array('fields' => 'tt_ids'));
  175. if ( (!$category || is_wp_error($category)) || ( !is_wp_error($post_terms) && !empty($post_terms) && !in_array($category->term_taxonomy_id, $post_terms) ) )
  176. $redirect_url = get_permalink($wp_query->get_queried_object_id());
  177. }
  178. // Post Paging
  179. if ( is_singular() && get_query_var('page') && $redirect_url ) {
  180. $redirect_url = trailingslashit( $redirect_url ) . user_trailingslashit( get_query_var( 'page' ), 'single_paged' );
  181. $redirect['query'] = remove_query_arg( 'page', $redirect['query'] );
  182. }
  183. // paging and feeds
  184. if ( get_query_var('paged') || is_feed() || get_query_var('cpage') ) {
  185. 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'] ) ) {
  186. // Strip off paging and feed
  187. $redirect['path'] = preg_replace("#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", '/', $redirect['path']); // strip off any existing paging
  188. $redirect['path'] = preg_replace('#/(comments/?)?(feed|rss2?|rdf|atom)(/+|$)#', '/', $redirect['path']); // strip off feed endings
  189. $redirect['path'] = preg_replace('#/comment-page-[0-9]+?(/+)?$#', '/', $redirect['path']); // strip off any existing comment paging
  190. }
  191. $addl_path = '';
  192. if ( is_feed() && in_array( get_query_var('feed'), $wp_rewrite->feeds ) ) {
  193. $addl_path = !empty( $addl_path ) ? trailingslashit($addl_path) : '';
  194. if ( get_query_var( 'withcomments' ) )
  195. $addl_path .= 'comments/';
  196. if ( ( 'rss' == get_default_feed() && 'feed' == get_query_var('feed') ) || 'rss' == get_query_var('feed') )
  197. $addl_path .= user_trailingslashit( 'feed/' . ( ( get_default_feed() == 'rss2' ) ? '' : 'rss2' ), 'feed' );
  198. else
  199. $addl_path .= user_trailingslashit( 'feed/' . ( ( get_default_feed() == get_query_var('feed') || 'feed' == get_query_var('feed') ) ? '' : get_query_var('feed') ), 'feed' );
  200. $redirect['query'] = remove_query_arg( 'feed', $redirect['query'] );
  201. } elseif ( is_feed() && 'old' == get_query_var('feed') ) {
  202. $old_feed_files = array(
  203. 'wp-atom.php' => 'atom',
  204. 'wp-commentsrss2.php' => 'comments_rss2',
  205. 'wp-feed.php' => get_default_feed(),
  206. 'wp-rdf.php' => 'rdf',
  207. 'wp-rss.php' => 'rss2',
  208. 'wp-rss2.php' => 'rss2',
  209. );
  210. if ( isset( $old_feed_files[ basename( $redirect['path'] ) ] ) ) {
  211. $redirect_url = get_feed_link( $old_feed_files[ basename( $redirect['path'] ) ] );
  212. wp_redirect( $redirect_url, 301 );
  213. die();
  214. }
  215. }
  216. if ( get_query_var('paged') > 0 ) {
  217. $paged = get_query_var('paged');
  218. $redirect['query'] = remove_query_arg( 'paged', $redirect['query'] );
  219. if ( !is_feed() ) {
  220. if ( $paged > 1 && !is_single() ) {
  221. $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit("$wp_rewrite->pagination_base/$paged", 'paged');
  222. } elseif ( !is_single() ) {
  223. $addl_path = !empty( $addl_path ) ? trailingslashit($addl_path) : '';
  224. }
  225. } elseif ( $paged > 1 ) {
  226. $redirect['query'] = add_query_arg( 'paged', $paged, $redirect['query'] );
  227. }
  228. }
  229. 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 ) ) ) {
  230. $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit( 'comment-page-' . get_query_var('cpage'), 'commentpaged' );
  231. $redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] );
  232. }
  233. $redirect['path'] = user_trailingslashit( preg_replace('|/index.php/?$|', '/', $redirect['path']) ); // strip off trailing /index.php/
  234. if ( !empty( $addl_path ) && $wp_rewrite->using_index_permalinks() && strpos($redirect['path'], '/index.php/') === false )
  235. $redirect['path'] = trailingslashit($redirect['path']) . 'index.php/';
  236. if ( !empty( $addl_path ) )
  237. $redirect['path'] = trailingslashit($redirect['path']) . $addl_path;
  238. $redirect_url = $redirect['scheme'] . '://' . $redirect['host'] . $redirect['path'];
  239. }
  240. }
  241. // tack on any additional query vars
  242. $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] );
  243. if ( $redirect_url && !empty($redirect['query']) ) {
  244. parse_str( $redirect['query'], $_parsed_query );
  245. $redirect = @parse_url($redirect_url);
  246. if ( ! empty( $_parsed_query['name'] ) && ! empty( $redirect['query'] ) ) {
  247. parse_str( $redirect['query'], $_parsed_redirect_query );
  248. if ( empty( $_parsed_redirect_query['name'] ) )
  249. unset( $_parsed_query['name'] );
  250. }
  251. $_parsed_query = array_map( 'rawurlencode', $_parsed_query );
  252. $redirect_url = add_query_arg( $_parsed_query, $redirect_url );
  253. }
  254. if ( $redirect_url )
  255. $redirect = @parse_url($redirect_url);
  256. // www.example.com vs example.com
  257. $user_home = @parse_url(home_url());
  258. if ( !empty($user_home['host']) )
  259. $redirect['host'] = $user_home['host'];
  260. if ( empty($user_home['path']) )
  261. $user_home['path'] = '/';
  262. // Handle ports
  263. if ( !empty($user_home['port']) )
  264. $redirect['port'] = $user_home['port'];
  265. else
  266. unset($redirect['port']);
  267. // trailing /index.php
  268. $redirect['path'] = preg_replace('|/index.php/*?$|', '/', $redirect['path']);
  269. // Remove trailing spaces from the path
  270. $redirect['path'] = preg_replace( '#(%20| )+$#', '', $redirect['path'] );
  271. if ( !empty( $redirect['query'] ) ) {
  272. // Remove trailing spaces from certain terminating query string args
  273. $redirect['query'] = preg_replace( '#((p|page_id|cat|tag)=[^&]*?)(%20| )+$#', '$1', $redirect['query'] );
  274. // Clean up empty query strings
  275. $redirect['query'] = trim(preg_replace( '#(^|&)(p|page_id|cat|tag)=?(&|$)#', '&', $redirect['query']), '&');
  276. // Redirect obsolete feeds
  277. $redirect['query'] = preg_replace( '#(^|&)feed=rss(&|$)#', '$1feed=rss2$3', $redirect['query'] );
  278. // Remove redundant leading ampersands
  279. $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] );
  280. }
  281. // strip /index.php/ when we're not using PATHINFO permalinks
  282. if ( !$wp_rewrite->using_index_permalinks() )
  283. $redirect['path'] = str_replace('/index.php/', '/', $redirect['path']);
  284. // trailing slashes
  285. if ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() && !is_404() && (!is_front_page() || ( is_front_page() && (get_query_var('paged') > 1) ) ) ) {
  286. $user_ts_type = '';
  287. if ( get_query_var('paged') > 0 ) {
  288. $user_ts_type = 'paged';
  289. } else {
  290. foreach ( array('single', 'category', 'page', 'day', 'month', 'year', 'home') as $type ) {
  291. $func = 'is_' . $type;
  292. if ( call_user_func($func) ) {
  293. $user_ts_type = $type;
  294. break;
  295. }
  296. }
  297. }
  298. $redirect['path'] = user_trailingslashit($redirect['path'], $user_ts_type);
  299. } elseif ( is_front_page() ) {
  300. $redirect['path'] = trailingslashit($redirect['path']);
  301. }
  302. // Strip multiple slashes out of the URL
  303. if ( strpos($redirect['path'], '//') > -1 )
  304. $redirect['path'] = preg_replace('|/+|', '/', $redirect['path']);
  305. // Always trailing slash the Front Page URL
  306. if ( trailingslashit( $redirect['path'] ) == trailingslashit( $user_home['path'] ) )
  307. $redirect['path'] = trailingslashit($redirect['path']);
  308. // Ignore differences in host capitalization, as this can lead to infinite redirects
  309. // Only redirect no-www <=> yes-www
  310. if ( strtolower($original['host']) == strtolower($redirect['host']) ||
  311. ( strtolower($original['host']) != 'www.' . strtolower($redirect['host']) && 'www.' . strtolower($original['host']) != strtolower($redirect['host']) ) )
  312. $redirect['host'] = $original['host'];
  313. $compare_original = array($original['host'], $original['path']);
  314. if ( !empty( $original['port'] ) )
  315. $compare_original[] = $original['port'];
  316. if ( !empty( $original['query'] ) )
  317. $compare_original[] = $original['query'];
  318. $compare_redirect = array($redirect['host'], $redirect['path']);
  319. if ( !empty( $redirect['port'] ) )
  320. $compare_redirect[] = $redirect['port'];
  321. if ( !empty( $redirect['query'] ) )
  322. $compare_redirect[] = $redirect['query'];
  323. if ( $compare_original !== $compare_redirect ) {
  324. $redirect_url = $redirect['scheme'] . '://' . $redirect['host'];
  325. if ( !empty($redirect['port']) )
  326. $redirect_url .= ':' . $redirect['port'];
  327. $redirect_url .= $redirect['path'];
  328. if ( !empty($redirect['query']) )
  329. $redirect_url .= '?' . $redirect['query'];
  330. }
  331. if ( !$redirect_url || $redirect_url == $requested_url )
  332. return false;
  333. // Hex encoded octets are case-insensitive.
  334. if ( false !== strpos($requested_url, '%') ) {
  335. if ( !function_exists('lowercase_octets') ) {
  336. function lowercase_octets($matches) {
  337. return strtolower( $matches[0] );
  338. }
  339. }
  340. $requested_url = preg_replace_callback('|%[a-fA-F0-9][a-fA-F0-9]|', 'lowercase_octets', $requested_url);
  341. }
  342. // Note that you can use the "redirect_canonical" filter to cancel a canonical redirect for whatever reason by returning FALSE
  343. $redirect_url = apply_filters('redirect_canonical', $redirect_url, $requested_url);
  344. if ( !$redirect_url || $redirect_url == $requested_url ) // yes, again -- in case the filter aborted the request
  345. return false;
  346. if ( $do_redirect ) {
  347. // protect against chained redirects
  348. if ( !redirect_canonical($redirect_url, false) ) {
  349. wp_redirect($redirect_url, 301);
  350. exit();
  351. } else {
  352. // Debug
  353. // die("1: $redirect_url<br />2: " . redirect_canonical( $redirect_url, false ) );
  354. return false;
  355. }
  356. } else {
  357. return $redirect_url;
  358. }
  359. }
  360. /**
  361. * Attempts to guess correct post based on query vars.
  362. *
  363. * @since 2.3.0
  364. * @uses $wpdb
  365. *
  366. * @return bool|string Returns False, if it can't find post, returns correct
  367. * location on success.
  368. */
  369. function redirect_guess_404_permalink() {
  370. global $wpdb;
  371. if ( !get_query_var('name') )
  372. return false;
  373. $where = $wpdb->prepare("post_name LIKE %s", like_escape( get_query_var('name') ) . '%');
  374. // if any of post_type, year, monthnum, or day are set, use them to refine the query
  375. if ( get_query_var('post_type') )
  376. $where .= $wpdb->prepare(" AND post_type = %s", get_query_var('post_type'));
  377. if ( get_query_var('year') )
  378. $where .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var('year'));
  379. if ( get_query_var('monthnum') )
  380. $where .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var('monthnum'));
  381. if ( get_query_var('day') )
  382. $where .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var('day'));
  383. $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'");
  384. if ( !$post_id )
  385. return false;
  386. return get_permalink($post_id);
  387. }
  388. add_action('template_redirect', 'redirect_canonical');
  389. ?>