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