PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/Websites/webkit.org/blog/wp-includes/canonical.php

https://bitbucket.org/zenoalbisser/webkit
PHP | 339 lines | 216 code | 48 blank | 75 comment | 127 complexity | c3dcba0d529b893cd0419f2bf2c624f6 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. * @author Scott Yang
  9. * @author Mark Jaquith
  10. * @package WordPress
  11. * @since 2.3.0
  12. */
  13. /**
  14. * Redirects incoming links to the proper URL based on the site url.
  15. *
  16. * Search engines consider www.somedomain.com and somedomain.com to be two
  17. * different URLs when they both go to the same location. This SEO enhancement
  18. * prevents penality for duplicate content by redirecting all incoming links to
  19. * one or the other.
  20. *
  21. * Prevents redirection for feeds, trackbacks, searches, comment popup, and
  22. * admin URLs. Does not redirect on IIS, page/post previews, and on form data.
  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() || $is_IIS || ( isset($_POST) && count($_POST) ) || is_preview() || is_robots() )
  41. return;
  42. if ( !$requested_url ) {
  43. // build the URL in the address bar
  44. $requested_url = ( !empty($_SERVER['HTTPS'] ) && strtolower($_SERVER['HTTPS']) == 'on' ) ? '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']) ) $redirect['path'] = '';
  60. if ( !isset($redirect['query']) ) $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'), $redirect['query']);
  68. }
  69. }
  70. // These tests give us a WP-generated permalink
  71. if ( is_404() ) {
  72. $redirect_url = redirect_guess_404_permalink();
  73. } elseif ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) {
  74. // rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101
  75. if ( is_single() && !empty($_GET['p']) && ! $redirect_url ) {
  76. if ( $redirect_url = get_permalink(get_query_var('p')) )
  77. $redirect['query'] = remove_query_arg('p', $redirect['query']);
  78. if ( get_query_var( 'page' ) ) {
  79. $redirect_url = trailingslashit( $redirect_url ) . user_trailingslashit( get_query_var( 'page' ), 'single_paged' );
  80. $redirect['query'] = remove_query_arg( 'page', $redirect['query'] );
  81. }
  82. } elseif ( is_page() && !empty($_GET['page_id']) && ! $redirect_url ) {
  83. if ( $redirect_url = get_permalink(get_query_var('page_id')) )
  84. $redirect['query'] = remove_query_arg('page_id', $redirect['query']);
  85. } elseif ( !empty($_GET['m']) && ( is_year() || is_month() || is_day() ) ) {
  86. $m = get_query_var('m');
  87. switch ( strlen($m) ) {
  88. case 4: // Yearly
  89. $redirect_url = get_year_link($m);
  90. break;
  91. case 6: // Monthly
  92. $redirect_url = get_month_link( substr($m, 0, 4), substr($m, 4, 2) );
  93. break;
  94. case 8: // Daily
  95. $redirect_url = get_day_link(substr($m, 0, 4), substr($m, 4, 2), substr($m, 6, 2));
  96. break;
  97. }
  98. if ( $redirect_url )
  99. $redirect['query'] = remove_query_arg('m', $redirect['query']);
  100. // now moving on to non ?m=X year/month/day links
  101. } elseif ( is_day() && get_query_var('year') && get_query_var('monthnum') && !empty($_GET['day']) ) {
  102. if ( $redirect_url = get_day_link(get_query_var('year'), get_query_var('monthnum'), get_query_var('day')) )
  103. $redirect['query'] = remove_query_arg(array('year', 'monthnum', 'day'), $redirect['query']);
  104. } elseif ( is_month() && get_query_var('year') && !empty($_GET['monthnum']) ) {
  105. if ( $redirect_url = get_month_link(get_query_var('year'), get_query_var('monthnum')) )
  106. $redirect['query'] = remove_query_arg(array('year', 'monthnum'), $redirect['query']);
  107. } elseif ( is_year() && !empty($_GET['year']) ) {
  108. if ( $redirect_url = get_year_link(get_query_var('year')) )
  109. $redirect['query'] = remove_query_arg('year', $redirect['query']);
  110. } elseif ( is_category() && !empty($_GET['cat']) && preg_match( '|^[0-9]+$|', $_GET['cat'] ) ) {
  111. if ( $redirect_url = get_category_link(get_query_var('cat')) )
  112. $redirect['query'] = remove_query_arg('cat', $redirect['query']);
  113. } elseif ( is_author() && !empty($_GET['author']) ) {
  114. $author = get_userdata(get_query_var('author'));
  115. if ( false !== $author && $redirect_url = get_author_posts_url($author->ID, $author->user_nicename) )
  116. $redirect['query'] = remove_query_arg('author', $redirect['author']);
  117. }
  118. // paging and feeds
  119. if ( get_query_var('paged') || is_feed() || get_query_var('cpage') ) {
  120. if ( !$redirect_url )
  121. $redirect_url = $requested_url;
  122. $paged_redirect = @parse_url($redirect_url);
  123. while ( preg_match( '#/page/[0-9]+?(/+)?$#', $paged_redirect['path'] ) || preg_match( '#/(comments/?)?(feed|rss|rdf|atom|rss2)(/+)?$#', $paged_redirect['path'] ) || preg_match( '#/comment-page-[0-9]+(/+)?$#', $paged_redirect['path'] ) ) {
  124. // Strip off paging and feed
  125. $paged_redirect['path'] = preg_replace('#/page/[0-9]+?(/+)?$#', '/', $paged_redirect['path']); // strip off any existing paging
  126. $paged_redirect['path'] = preg_replace('#/(comments/?)?(feed|rss2?|rdf|atom)(/+|$)#', '/', $paged_redirect['path']); // strip off feed endings
  127. $paged_redirect['path'] = preg_replace('#/comment-page-[0-9]+?(/+)?$#', '/', $paged_redirect['path']); // strip off any existing comment paging
  128. }
  129. $addl_path = '';
  130. if ( is_feed() ) {
  131. $addl_path = !empty( $addl_path ) ? trailingslashit($addl_path) : '';
  132. if ( get_query_var( 'withcomments' ) )
  133. $addl_path .= 'comments/';
  134. $addl_path .= user_trailingslashit( 'feed/' . ( ( 'rss2' == get_query_var('feed') || 'feed' == get_query_var('feed') ) ? '' : get_query_var('feed') ), 'feed' );
  135. $redirect['query'] = remove_query_arg( 'feed', $redirect['query'] );
  136. }
  137. if ( get_query_var('paged') > 0 ) {
  138. $paged = get_query_var('paged');
  139. $redirect['query'] = remove_query_arg( 'paged', $redirect['query'] );
  140. if ( !is_feed() ) {
  141. if ( $paged > 1 && !is_single() ) {
  142. $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit("page/$paged", 'paged');
  143. } elseif ( !is_single() ) {
  144. $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit($paged_redirect['path'], 'paged');
  145. }
  146. } elseif ( $paged > 1 ) {
  147. $redirect['query'] = add_query_arg( 'paged', $paged, $redirect['query'] );
  148. }
  149. }
  150. 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 ) ) ) {
  151. $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit( 'comment-page-' . get_query_var('cpage'), 'commentpaged' );
  152. $redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] );
  153. }
  154. $paged_redirect['path'] = user_trailingslashit( preg_replace('|/index.php/?$|', '/', $paged_redirect['path']) ); // strip off trailing /index.php/
  155. if ( !empty( $addl_path ) && $wp_rewrite->using_index_permalinks() && strpos($paged_redirect['path'], '/index.php/') === false )
  156. $paged_redirect['path'] = trailingslashit($paged_redirect['path']) . 'index.php/';
  157. if ( !empty( $addl_path ) )
  158. $paged_redirect['path'] = trailingslashit($paged_redirect['path']) . $addl_path;
  159. $redirect_url = $paged_redirect['scheme'] . '://' . $paged_redirect['host'] . $paged_redirect['path'];
  160. $redirect['path'] = $paged_redirect['path'];
  161. }
  162. }
  163. // tack on any additional query vars
  164. $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] );
  165. if ( $redirect_url && !empty($redirect['query']) ) {
  166. if ( strpos($redirect_url, '?') !== false )
  167. $redirect_url .= '&';
  168. else
  169. $redirect_url .= '?';
  170. $redirect_url .= $redirect['query'];
  171. }
  172. if ( $redirect_url )
  173. $redirect = @parse_url($redirect_url);
  174. // www.example.com vs example.com
  175. $user_home = @parse_url(get_option('home'));
  176. if ( !empty($user_home['host']) )
  177. $redirect['host'] = $user_home['host'];
  178. if ( empty($user_home['path']) )
  179. $user_home['path'] = '/';
  180. // Handle ports
  181. if ( !empty($user_home['port']) )
  182. $redirect['port'] = $user_home['port'];
  183. else
  184. unset($redirect['port']);
  185. // trailing /index.php
  186. $redirect['path'] = preg_replace('|/index.php/*?$|', '/', $redirect['path']);
  187. // Remove trailing spaces from the path
  188. $redirect['path'] = preg_replace( '#(%20| )+$#', '', $redirect['path'] );
  189. if ( !empty( $redirect['query'] ) ) {
  190. // Remove trailing spaces from certain terminating query string args
  191. $redirect['query'] = preg_replace( '#((p|page_id|cat|tag)=[^&]*?)(%20| )+$#', '$1', $redirect['query'] );
  192. // Clean up empty query strings
  193. $redirect['query'] = trim(preg_replace( '#(^|&)(p|page_id|cat|tag)=?(&|$)#', '&', $redirect['query']), '&');
  194. // Remove redundant leading ampersands
  195. $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] );
  196. }
  197. // strip /index.php/ when we're not using PATHINFO permalinks
  198. if ( !$wp_rewrite->using_index_permalinks() )
  199. $redirect['path'] = str_replace('/index.php/', '/', $redirect['path']);
  200. // trailing slashes
  201. if ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() && !is_404() && (!is_front_page() || ( is_front_page() && (get_query_var('paged') > 1) ) ) ) {
  202. $user_ts_type = '';
  203. if ( get_query_var('paged') > 0 ) {
  204. $user_ts_type = 'paged';
  205. } else {
  206. foreach ( array('single', 'category', 'page', 'day', 'month', 'year', 'home') as $type ) {
  207. $func = 'is_' . $type;
  208. if ( call_user_func($func) ) {
  209. $user_ts_type = $type;
  210. break;
  211. }
  212. }
  213. }
  214. $redirect['path'] = user_trailingslashit($redirect['path'], $user_ts_type);
  215. } elseif ( is_front_page() ) {
  216. $redirect['path'] = trailingslashit($redirect['path']);
  217. }
  218. // Always trailing slash the Front Page URL
  219. if ( trailingslashit( $redirect['path'] ) == trailingslashit( $user_home['path'] ) )
  220. $redirect['path'] = trailingslashit($redirect['path']);
  221. // Ignore differences in host capitalization, as this can lead to infinite redirects
  222. // Only redirect no-www <=> yes-www
  223. if ( strtolower($original['host']) == strtolower($redirect['host']) ||
  224. ( strtolower($original['host']) != 'www.' . strtolower($redirect['host']) && 'www.' . strtolower($original['host']) != strtolower($redirect['host']) ) )
  225. $redirect['host'] = $original['host'];
  226. $compare_original = array($original['host'], $original['path']);
  227. if ( !empty( $original['port'] ) )
  228. $compare_original[] = $original['port'];
  229. if ( !empty( $original['query'] ) )
  230. $compare_original[] = $original['query'];
  231. $compare_redirect = array($redirect['host'], $redirect['path']);
  232. if ( !empty( $redirect['port'] ) )
  233. $compare_redirect[] = $redirect['port'];
  234. if ( !empty( $redirect['query'] ) )
  235. $compare_redirect[] = $redirect['query'];
  236. if ( $compare_original !== $compare_redirect ) {
  237. $redirect_url = $redirect['scheme'] . '://' . $redirect['host'];
  238. if ( !empty($redirect['port']) )
  239. $redirect_url .= ':' . $redirect['port'];
  240. $redirect_url .= $redirect['path'];
  241. if ( !empty($redirect['query']) )
  242. $redirect_url .= '?' . $redirect['query'];
  243. }
  244. if ( $redirect_url == $requested_url )
  245. return false;
  246. // Note that you can use the "redirect_canonical" filter to cancel a canonical redirect for whatever reason by returning FALSE
  247. $redirect_url = apply_filters('redirect_canonical', $redirect_url, $requested_url);
  248. if ( !$redirect_url || $redirect_url == $requested_url ) // yes, again -- in case the filter aborted the request
  249. return false;
  250. if ( $do_redirect ) {
  251. // protect against chained redirects
  252. if ( !redirect_canonical($redirect_url, false) ) {
  253. wp_redirect($redirect_url, 301);
  254. exit();
  255. } else {
  256. // Debug
  257. // die("1: $redirect_url<br />2: " . redirect_canonical( $redirect_url, false ) );
  258. return false;
  259. }
  260. } else {
  261. return $redirect_url;
  262. }
  263. }
  264. /**
  265. * Attempts to guess correct post based on query vars.
  266. *
  267. * @since 2.3.0
  268. * @uses $wpdb
  269. *
  270. * @return bool|string Returns False, if it can't find post, returns correct
  271. * location on success.
  272. */
  273. function redirect_guess_404_permalink() {
  274. global $wpdb;
  275. if ( !get_query_var('name') )
  276. return false;
  277. $where = $wpdb->prepare("post_name LIKE %s", get_query_var('name') . '%');
  278. // if any of year, monthnum, or day are set, use them to refine the query
  279. if ( get_query_var('year') )
  280. $where .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var('year'));
  281. if ( get_query_var('monthnum') )
  282. $where .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var('monthnum'));
  283. if ( get_query_var('day') )
  284. $where .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var('day'));
  285. $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'");
  286. if ( !$post_id )
  287. return false;
  288. return get_permalink($post_id);
  289. }
  290. add_action('template_redirect', 'redirect_canonical');
  291. ?>