PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/canonical.php

https://gitlab.com/endomorphosis/reservationtelco
PHP | 403 lines | 270 code | 58 blank | 75 comment | 166 complexity | a400eb55a090de5cbc0c15385e9f9f92 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 penality 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() || $is_IIS || ( isset($_POST) && count($_POST) ) || is_preview() || is_robots() )
  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. if ( get_query_var( 'page' ) ) {
  92. $redirect_url = trailingslashit( $redirect_url ) . user_trailingslashit( get_query_var( 'page' ), 'single_paged' );
  93. $redirect['query'] = remove_query_arg( 'page', $redirect['query'] );
  94. }
  95. } elseif ( is_single() && !empty($_GET['name']) && ! $redirect_url ) {
  96. if ( $redirect_url = get_permalink( $wp_query->get_queried_object_id() ) )
  97. $redirect['query'] = remove_query_arg('name', $redirect['query']);
  98. } elseif ( is_page() && !empty($_GET['page_id']) && ! $redirect_url ) {
  99. if ( $redirect_url = get_permalink(get_query_var('page_id')) )
  100. $redirect['query'] = remove_query_arg('page_id', $redirect['query']);
  101. } 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 ) {
  102. $redirect_url = home_url('/');
  103. } 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 ) {
  104. if ( $redirect_url = get_permalink(get_option('page_for_posts')) )
  105. $redirect['query'] = remove_query_arg('page_id', $redirect['query']);
  106. } elseif ( !empty($_GET['m']) && ( is_year() || is_month() || is_day() ) ) {
  107. $m = get_query_var('m');
  108. switch ( strlen($m) ) {
  109. case 4: // Yearly
  110. $redirect_url = get_year_link($m);
  111. break;
  112. case 6: // Monthly
  113. $redirect_url = get_month_link( substr($m, 0, 4), substr($m, 4, 2) );
  114. break;
  115. case 8: // Daily
  116. $redirect_url = get_day_link(substr($m, 0, 4), substr($m, 4, 2), substr($m, 6, 2));
  117. break;
  118. }
  119. if ( $redirect_url )
  120. $redirect['query'] = remove_query_arg('m', $redirect['query']);
  121. // now moving on to non ?m=X year/month/day links
  122. } elseif ( is_day() && get_query_var('year') && get_query_var('monthnum') && !empty($_GET['day']) ) {
  123. if ( $redirect_url = get_day_link(get_query_var('year'), get_query_var('monthnum'), get_query_var('day')) )
  124. $redirect['query'] = remove_query_arg(array('year', 'monthnum', 'day'), $redirect['query']);
  125. } elseif ( is_month() && get_query_var('year') && !empty($_GET['monthnum']) ) {
  126. if ( $redirect_url = get_month_link(get_query_var('year'), get_query_var('monthnum')) )
  127. $redirect['query'] = remove_query_arg(array('year', 'monthnum'), $redirect['query']);
  128. } elseif ( is_year() && !empty($_GET['year']) ) {
  129. if ( $redirect_url = get_year_link(get_query_var('year')) )
  130. $redirect['query'] = remove_query_arg('year', $redirect['query']);
  131. } elseif ( is_author() && !empty($_GET['author']) && preg_match( '|^[0-9]+$|', $_GET['author'] ) ) {
  132. $author = get_userdata(get_query_var('author'));
  133. if ( false !== $author && $redirect_url = get_author_posts_url($author->ID, $author->user_nicename) )
  134. $redirect['query'] = remove_query_arg('author', $redirect['query']);
  135. } elseif ( is_category() || is_tag() || is_tax() ) { // Terms (Tags/categories)
  136. $term_count = 0;
  137. foreach ( array('category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in',
  138. 'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and') as $key )
  139. $term_count += count($wp_query->query_vars[$key]);
  140. $obj = $wp_query->get_queried_object();
  141. if ( $term_count <= 1 && !empty($obj->term_id) && ( $tax_url = get_term_link((int)$obj->term_id, $obj->taxonomy) ) && !is_wp_error($tax_url) ) {
  142. if ( is_category() ) {
  143. $redirect['query'] = remove_query_arg( array( 'category_name', 'category', 'cat'), $redirect['query']);
  144. } elseif ( is_tag() ) {
  145. $redirect['query'] = remove_query_arg( array( 'tag', 'tag_id'), $redirect['query']);
  146. } elseif ( is_tax() ) { // Custom taxonomies will have a custom query var, remove those too:
  147. $tax = get_taxonomy( $obj->taxonomy );
  148. if ( false !== $tax->query_var)
  149. $redirect['query'] = remove_query_arg($tax->query_var, $redirect['query']);
  150. else
  151. $redirect['query'] = remove_query_arg( array( 'term', 'taxonomy'), $redirect['query']);
  152. }
  153. $tax_url = parse_url($tax_url);
  154. if ( ! empty($tax_url['query']) ) { // Custom taxonomies may only be accessable via ?taxonomy=..&term=..
  155. parse_str($tax_url['query'], $query_vars);
  156. $redirect['query'] = add_query_arg($query_vars, $redirect['query']);
  157. } else { // Taxonomy is accessable via a "pretty-URL"
  158. $redirect['path'] = $tax_url['path'];
  159. }
  160. }
  161. } elseif ( is_single() && strpos($wp_rewrite->permalink_structure, '%category%') !== false ) {
  162. $category = get_term_by('slug', get_query_var('category_name'), 'category');
  163. $post_terms = wp_get_object_terms($wp_query->get_queried_object_id(), 'category', array('fields' => 'tt_ids'));
  164. if ( (!$category || is_wp_error($category)) || ( !is_wp_error($post_terms) && !empty($post_terms) && !in_array($category->term_taxonomy_id, $post_terms) ) )
  165. $redirect_url = get_permalink($wp_query->get_queried_object_id());
  166. }
  167. // paging and feeds
  168. if ( get_query_var('paged') || is_feed() || get_query_var('cpage') ) {
  169. if ( !$redirect_url )
  170. $redirect_url = $requested_url;
  171. $paged_redirect = @parse_url($redirect_url);
  172. 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'] ) ) {
  173. // Strip off paging and feed
  174. $paged_redirect['path'] = preg_replace('#/page/?[0-9]+?(/+)?$#', '/', $paged_redirect['path']); // strip off any existing paging
  175. $paged_redirect['path'] = preg_replace('#/(comments/?)?(feed|rss2?|rdf|atom)(/+|$)#', '/', $paged_redirect['path']); // strip off feed endings
  176. $paged_redirect['path'] = preg_replace('#/comment-page-[0-9]+?(/+)?$#', '/', $paged_redirect['path']); // strip off any existing comment paging
  177. }
  178. $addl_path = '';
  179. if ( is_feed() ) {
  180. $addl_path = !empty( $addl_path ) ? trailingslashit($addl_path) : '';
  181. if ( get_query_var( 'withcomments' ) )
  182. $addl_path .= 'comments/';
  183. $addl_path .= user_trailingslashit( 'feed/' . ( ( get_default_feed() == get_query_var('feed') || 'feed' == get_query_var('feed') ) ? '' : get_query_var('feed') ), 'feed' );
  184. $redirect['query'] = remove_query_arg( 'feed', $redirect['query'] );
  185. }
  186. if ( get_query_var('paged') > 0 ) {
  187. $paged = get_query_var('paged');
  188. $redirect['query'] = remove_query_arg( 'paged', $redirect['query'] );
  189. if ( !is_feed() ) {
  190. if ( $paged > 1 && !is_single() ) {
  191. $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit("page/$paged", 'paged');
  192. } elseif ( !is_single() ) {
  193. $addl_path = !empty( $addl_path ) ? trailingslashit($addl_path) : '';
  194. }
  195. } elseif ( $paged > 1 ) {
  196. $redirect['query'] = add_query_arg( 'paged', $paged, $redirect['query'] );
  197. }
  198. }
  199. 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 ) ) ) {
  200. $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit( 'comment-page-' . get_query_var('cpage'), 'commentpaged' );
  201. $redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] );
  202. }
  203. $paged_redirect['path'] = user_trailingslashit( preg_replace('|/index.php/?$|', '/', $paged_redirect['path']) ); // strip off trailing /index.php/
  204. if ( !empty( $addl_path ) && $wp_rewrite->using_index_permalinks() && strpos($paged_redirect['path'], '/index.php/') === false )
  205. $paged_redirect['path'] = trailingslashit($paged_redirect['path']) . 'index.php/';
  206. if ( !empty( $addl_path ) )
  207. $paged_redirect['path'] = trailingslashit($paged_redirect['path']) . $addl_path;
  208. $redirect_url = $paged_redirect['scheme'] . '://' . $paged_redirect['host'] . $paged_redirect['path'];
  209. $redirect['path'] = $paged_redirect['path'];
  210. }
  211. }
  212. // tack on any additional query vars
  213. $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] );
  214. if ( $redirect_url && !empty($redirect['query']) ) {
  215. if ( strpos($redirect_url, '?') !== false )
  216. $redirect_url .= '&';
  217. else
  218. $redirect_url .= '?';
  219. $redirect_url .= $redirect['query'];
  220. }
  221. if ( $redirect_url )
  222. $redirect = @parse_url($redirect_url);
  223. // www.example.com vs example.com
  224. $user_home = @parse_url(home_url());
  225. if ( !empty($user_home['host']) )
  226. $redirect['host'] = $user_home['host'];
  227. if ( empty($user_home['path']) )
  228. $user_home['path'] = '/';
  229. // Handle ports
  230. if ( !empty($user_home['port']) )
  231. $redirect['port'] = $user_home['port'];
  232. else
  233. unset($redirect['port']);
  234. // trailing /index.php
  235. $redirect['path'] = preg_replace('|/index.php/*?$|', '/', $redirect['path']);
  236. // Remove trailing spaces from the path
  237. $redirect['path'] = preg_replace( '#(%20| )+$#', '', $redirect['path'] );
  238. if ( !empty( $redirect['query'] ) ) {
  239. // Remove trailing spaces from certain terminating query string args
  240. $redirect['query'] = preg_replace( '#((p|page_id|cat|tag)=[^&]*?)(%20| )+$#', '$1', $redirect['query'] );
  241. // Clean up empty query strings
  242. $redirect['query'] = trim(preg_replace( '#(^|&)(p|page_id|cat|tag)=?(&|$)#', '&', $redirect['query']), '&');
  243. // Remove redundant leading ampersands
  244. $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] );
  245. }
  246. // strip /index.php/ when we're not using PATHINFO permalinks
  247. if ( !$wp_rewrite->using_index_permalinks() )
  248. $redirect['path'] = str_replace('/index.php/', '/', $redirect['path']);
  249. // trailing slashes
  250. if ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() && !is_404() && (!is_front_page() || ( is_front_page() && (get_query_var('paged') > 1) ) ) ) {
  251. $user_ts_type = '';
  252. if ( get_query_var('paged') > 0 ) {
  253. $user_ts_type = 'paged';
  254. } else {
  255. foreach ( array('single', 'category', 'page', 'day', 'month', 'year', 'home') as $type ) {
  256. $func = 'is_' . $type;
  257. if ( call_user_func($func) ) {
  258. $user_ts_type = $type;
  259. break;
  260. }
  261. }
  262. }
  263. $redirect['path'] = user_trailingslashit($redirect['path'], $user_ts_type);
  264. } elseif ( is_front_page() ) {
  265. $redirect['path'] = trailingslashit($redirect['path']);
  266. }
  267. // Strip multiple slashes out of the URL
  268. if ( strpos($redirect['path'], '//') > -1 )
  269. $redirect['path'] = preg_replace('|/+|', '/', $redirect['path']);
  270. // Always trailing slash the Front Page URL
  271. if ( trailingslashit( $redirect['path'] ) == trailingslashit( $user_home['path'] ) )
  272. $redirect['path'] = trailingslashit($redirect['path']);
  273. // Ignore differences in host capitalization, as this can lead to infinite redirects
  274. // Only redirect no-www <=> yes-www
  275. if ( strtolower($original['host']) == strtolower($redirect['host']) ||
  276. ( strtolower($original['host']) != 'www.' . strtolower($redirect['host']) && 'www.' . strtolower($original['host']) != strtolower($redirect['host']) ) )
  277. $redirect['host'] = $original['host'];
  278. $compare_original = array($original['host'], $original['path']);
  279. if ( !empty( $original['port'] ) )
  280. $compare_original[] = $original['port'];
  281. if ( !empty( $original['query'] ) )
  282. $compare_original[] = $original['query'];
  283. $compare_redirect = array($redirect['host'], $redirect['path']);
  284. if ( !empty( $redirect['port'] ) )
  285. $compare_redirect[] = $redirect['port'];
  286. if ( !empty( $redirect['query'] ) )
  287. $compare_redirect[] = $redirect['query'];
  288. if ( $compare_original !== $compare_redirect ) {
  289. $redirect_url = $redirect['scheme'] . '://' . $redirect['host'];
  290. if ( !empty($redirect['port']) )
  291. $redirect_url .= ':' . $redirect['port'];
  292. $redirect_url .= $redirect['path'];
  293. if ( !empty($redirect['query']) )
  294. $redirect_url .= '?' . $redirect['query'];
  295. }
  296. if ( !$redirect_url || $redirect_url == $requested_url )
  297. return false;
  298. // Note that you can use the "redirect_canonical" filter to cancel a canonical redirect for whatever reason by returning FALSE
  299. $redirect_url = apply_filters('redirect_canonical', $redirect_url, $requested_url);
  300. if ( !$redirect_url || $redirect_url == $requested_url ) // yes, again -- in case the filter aborted the request
  301. return false;
  302. if ( $do_redirect ) {
  303. // protect against chained redirects
  304. if ( !redirect_canonical($redirect_url, false) ) {
  305. wp_redirect($redirect_url, 301);
  306. exit();
  307. } else {
  308. // Debug
  309. // die("1: $redirect_url<br />2: " . redirect_canonical( $redirect_url, false ) );
  310. return false;
  311. }
  312. } else {
  313. return $redirect_url;
  314. }
  315. }
  316. /**
  317. * Attempts to guess correct post based on query vars.
  318. *
  319. * @since 2.3.0
  320. * @uses $wpdb
  321. *
  322. * @return bool|string Returns False, if it can't find post, returns correct
  323. * location on success.
  324. */
  325. function redirect_guess_404_permalink() {
  326. global $wpdb;
  327. if ( !get_query_var('name') )
  328. return false;
  329. $where = $wpdb->prepare("post_name LIKE %s", get_query_var('name') . '%');
  330. // if any of post_type, year, monthnum, or day are set, use them to refine the query
  331. if ( get_query_var('post_type') )
  332. $where .= $wpdb->prepare(" AND post_type = %s", get_query_var('post_type'));
  333. if ( get_query_var('year') )
  334. $where .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var('year'));
  335. if ( get_query_var('monthnum') )
  336. $where .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var('monthnum'));
  337. if ( get_query_var('day') )
  338. $where .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var('day'));
  339. $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'");
  340. if ( !$post_id )
  341. return false;
  342. return get_permalink($post_id);
  343. }
  344. add_action('template_redirect', 'redirect_canonical');
  345. ?>