wp/wp-includes/canonical.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
    17  * prevents penalty for duplicate content by redirecting all incoming links to
    17  * prevents penalty for duplicate content by redirecting all incoming links to
    18  * one or the other.
    18  * one or the other.
    19  *
    19  *
    20  * Prevents redirection for feeds, trackbacks, searches, and
    20  * Prevents redirection for feeds, trackbacks, searches, and
    21  * admin URLs. Does not redirect on non-pretty-permalink-supporting IIS 7+,
    21  * admin URLs. Does not redirect on non-pretty-permalink-supporting IIS 7+,
    22  * page/post previews, WP admin, Trackbacks, robots.txt, searches, or on POST
    22  * page/post previews, WP admin, Trackbacks, robots.txt, favicon.ico, searches,
    23  * requests.
    23  * or on POST requests.
    24  *
    24  *
    25  * Will also attempt to find the correct link when a user enters a URL that does
    25  * Will also attempt to find the correct link when a user enters a URL that does
    26  * not exist based on exact WordPress query. Will instead try to parse the URL
    26  * not exist based on exact WordPress query. Will instead try to parse the URL
    27  * or query in an attempt to figure the correct page to go to.
    27  * or query in an attempt to figure the correct page to go to.
    28  *
    28  *
    29  * @since 2.3.0
    29  * @since 2.3.0
    30  *
    30  *
    31  * @global WP_Rewrite $wp_rewrite
    31  * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
    32  * @global bool $is_IIS
    32  * @global bool       $is_IIS
    33  * @global WP_Query $wp_query
    33  * @global WP_Query   $wp_query   WordPress Query object.
    34  * @global wpdb $wpdb WordPress database abstraction object.
    34  * @global wpdb       $wpdb       WordPress database abstraction object.
    35  * @global WP $wp Current WordPress environment instance.
    35  * @global WP         $wp         Current WordPress environment instance.
    36  *
    36  *
    37  * @param string $requested_url Optional. The URL that was requested, used to
    37  * @param string $requested_url Optional. The URL that was requested, used to
    38  *      figure if redirect is needed.
    38  *                              figure if redirect is needed.
    39  * @param bool $do_redirect Optional. Redirect to the new URL.
    39  * @param bool   $do_redirect   Optional. Redirect to the new URL.
    40  * @return string|void The string of the URL, if redirect needed.
    40  * @return string|void The string of the URL, if redirect needed.
    41  */
    41  */
    42 function redirect_canonical( $requested_url = null, $do_redirect = true ) {
    42 function redirect_canonical( $requested_url = null, $do_redirect = true ) {
    43 	global $wp_rewrite, $is_IIS, $wp_query, $wpdb, $wp;
    43 	global $wp_rewrite, $is_IIS, $wp_query, $wpdb, $wp;
    44 
    44 
    45 	if ( isset( $_SERVER['REQUEST_METHOD'] ) && ! in_array( strtoupper( $_SERVER['REQUEST_METHOD'] ), array( 'GET', 'HEAD' ) ) ) {
    45 	if ( isset( $_SERVER['REQUEST_METHOD'] ) && ! in_array( strtoupper( $_SERVER['REQUEST_METHOD'] ), array( 'GET', 'HEAD' ), true ) ) {
    46 		return;
    46 		return;
    47 	}
    47 	}
    48 
    48 
    49 	// If we're not in wp-admin and the post has been published and preview nonce
    49 	// If we're not in wp-admin and the post has been published and preview nonce
    50 	// is non-existent or invalid then no need for preview in query
    50 	// is non-existent or invalid then no need for preview in query.
    51 	if ( is_preview() && get_query_var( 'p' ) && 'publish' == get_post_status( get_query_var( 'p' ) ) ) {
    51 	if ( is_preview() && get_query_var( 'p' ) && 'publish' === get_post_status( get_query_var( 'p' ) ) ) {
    52 		if ( ! isset( $_GET['preview_id'] )
    52 		if ( ! isset( $_GET['preview_id'] )
    53 			|| ! isset( $_GET['preview_nonce'] )
    53 			|| ! isset( $_GET['preview_nonce'] )
    54 			|| ! wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . (int) $_GET['preview_id'] ) ) {
    54 			|| ! wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . (int) $_GET['preview_id'] )
       
    55 		) {
    55 			$wp_query->is_preview = false;
    56 			$wp_query->is_preview = false;
    56 		}
    57 		}
    57 	}
    58 	}
    58 
    59 
    59 	if ( is_trackback() || is_search() || is_admin() || is_preview() || is_robots() || ( $is_IIS && ! iis7_supports_permalinks() ) ) {
    60 	if ( is_admin() || is_search() || is_preview() || is_trackback() || is_favicon()
       
    61 		|| ( $is_IIS && ! iis7_supports_permalinks() )
       
    62 	) {
    60 		return;
    63 		return;
    61 	}
    64 	}
    62 
    65 
    63 	if ( ! $requested_url && isset( $_SERVER['HTTP_HOST'] ) ) {
    66 	if ( ! $requested_url && isset( $_SERVER['HTTP_HOST'] ) ) {
    64 		// build the URL in the address bar
    67 		// Build the URL in the address bar.
    65 		$requested_url  = is_ssl() ? 'https://' : 'http://';
    68 		$requested_url  = is_ssl() ? 'https://' : 'http://';
    66 		$requested_url .= $_SERVER['HTTP_HOST'];
    69 		$requested_url .= $_SERVER['HTTP_HOST'];
    67 		$requested_url .= $_SERVER['REQUEST_URI'];
    70 		$requested_url .= $_SERVER['REQUEST_URI'];
    68 	}
    71 	}
    69 
    72 
    70 	$original = @parse_url( $requested_url );
    73 	$original = parse_url( $requested_url );
    71 	if ( false === $original ) {
    74 	if ( false === $original ) {
    72 		return;
    75 		return;
    73 	}
    76 	}
    74 
    77 
    75 	$redirect     = $original;
    78 	$redirect     = $original;
    76 	$redirect_url = false;
    79 	$redirect_url = false;
    77 
    80 
    78 	// Notice fixing
    81 	// Notice fixing.
    79 	if ( ! isset( $redirect['path'] ) ) {
    82 	if ( ! isset( $redirect['path'] ) ) {
    80 		$redirect['path'] = '';
    83 		$redirect['path'] = '';
    81 	}
    84 	}
    82 	if ( ! isset( $redirect['query'] ) ) {
    85 	if ( ! isset( $redirect['query'] ) ) {
    83 		$redirect['query'] = '';
    86 		$redirect['query'] = '';
    84 	}
    87 	}
    85 
    88 
    86 	// If the original URL ended with non-breaking spaces, they were almost
    89 	/*
    87 	// certainly inserted by accident. Let's remove them, so the reader doesn't
    90 	 * If the original URL ended with non-breaking spaces, they were almost
    88 	// see a 404 error with no obvious cause.
    91 	 * certainly inserted by accident. Let's remove them, so the reader doesn't
       
    92 	 * see a 404 error with no obvious cause.
       
    93 	 */
    89 	$redirect['path'] = preg_replace( '|(%C2%A0)+$|i', '', $redirect['path'] );
    94 	$redirect['path'] = preg_replace( '|(%C2%A0)+$|i', '', $redirect['path'] );
    90 
    95 
    91 	// It's not a preview, so remove it from URL
    96 	// It's not a preview, so remove it from URL.
    92 	if ( get_query_var( 'preview' ) ) {
    97 	if ( get_query_var( 'preview' ) ) {
    93 		$redirect['query'] = remove_query_arg( 'preview', $redirect['query'] );
    98 		$redirect['query'] = remove_query_arg( 'preview', $redirect['query'] );
    94 	}
    99 	}
    95 
   100 
    96 	if ( is_feed() && ( $id = get_query_var( 'p' ) ) ) {
   101 	$post_id = get_query_var( 'p' );
    97 		if ( $redirect_url = get_post_comments_feed_link( $id, get_query_var( 'feed' ) ) ) {
   102 
    98 			$redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type', 'feed' ), $redirect_url );
   103 	if ( is_feed() && $post_id ) {
    99 			$redirect['path']  = parse_url( $redirect_url, PHP_URL_PATH );
   104 		$redirect_url = get_post_comments_feed_link( $post_id, get_query_var( 'feed' ) );
   100 		}
   105 
   101 	}
   106 		if ( $redirect_url ) {
   102 
   107 			$redirect['query'] = _remove_qs_args_if_not_in_url(
   103 	if ( is_singular() && 1 > $wp_query->post_count && ( $id = get_query_var( 'p' ) ) ) {
   108 				$redirect['query'],
   104 
   109 				array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type', 'feed' ),
   105 		$vars = $wpdb->get_results( $wpdb->prepare( "SELECT post_type, post_parent FROM $wpdb->posts WHERE ID = %d", $id ) );
   110 				$redirect_url
   106 
   111 			);
   107 		if ( isset( $vars[0] ) && $vars = $vars[0] ) {
   112 
   108 			if ( 'revision' == $vars->post_type && $vars->post_parent > 0 ) {
   113 			$redirect['path'] = parse_url( $redirect_url, PHP_URL_PATH );
   109 				$id = $vars->post_parent;
   114 		}
   110 			}
   115 	}
   111 
   116 
   112 			if ( $redirect_url = get_permalink( $id ) ) {
   117 	if ( is_singular() && $wp_query->post_count < 1 && $post_id ) {
   113 				$redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url );
   118 
   114 			}
   119 		$vars = $wpdb->get_results( $wpdb->prepare( "SELECT post_type, post_parent FROM $wpdb->posts WHERE ID = %d", $post_id ) );
   115 		}
   120 
   116 	}
   121 		if ( ! empty( $vars[0] ) ) {
   117 
   122 			$vars = $vars[0];
   118 	// These tests give us a WP-generated permalink
   123 
       
   124 			if ( 'revision' === $vars->post_type && $vars->post_parent > 0 ) {
       
   125 				$post_id = $vars->post_parent;
       
   126 			}
       
   127 
       
   128 			$redirect_url = get_permalink( $post_id );
       
   129 
       
   130 			if ( $redirect_url ) {
       
   131 				$redirect['query'] = _remove_qs_args_if_not_in_url(
       
   132 					$redirect['query'],
       
   133 					array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ),
       
   134 					$redirect_url
       
   135 				);
       
   136 			}
       
   137 		}
       
   138 	}
       
   139 
       
   140 	// These tests give us a WP-generated permalink.
   119 	if ( is_404() ) {
   141 	if ( is_404() ) {
   120 
   142 
   121 		// Redirect ?page_id, ?p=, ?attachment_id= to their respective url's
   143 		// Redirect ?page_id, ?p=, ?attachment_id= to their respective URLs.
   122 		$id = max( get_query_var( 'p' ), get_query_var( 'page_id' ), get_query_var( 'attachment_id' ) );
   144 		$post_id = max( get_query_var( 'p' ), get_query_var( 'page_id' ), get_query_var( 'attachment_id' ) );
   123 		if ( $id && $redirect_post = get_post( $id ) ) {
   145 
       
   146 		$redirect_post = $post_id ? get_post( $post_id ) : false;
       
   147 
       
   148 		if ( $redirect_post ) {
   124 			$post_type_obj = get_post_type_object( $redirect_post->post_type );
   149 			$post_type_obj = get_post_type_object( $redirect_post->post_type );
   125 			if ( $post_type_obj->public && 'auto-draft' != $redirect_post->post_status ) {
   150 
   126 				$redirect_url      = get_permalink( $redirect_post );
   151 			if ( $post_type_obj->public && 'auto-draft' !== $redirect_post->post_status ) {
   127 				$redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url );
   152 				$redirect_url = get_permalink( $redirect_post );
   128 			}
   153 
   129 		}
   154 				$redirect['query'] = _remove_qs_args_if_not_in_url(
   130 
   155 					$redirect['query'],
   131 		if ( get_query_var( 'day' ) && get_query_var( 'monthnum' ) && get_query_var( 'year' ) ) {
   156 					array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ),
       
   157 					$redirect_url
       
   158 				);
       
   159 			}
       
   160 		}
       
   161 
       
   162 		$year  = get_query_var( 'year' );
       
   163 		$month = get_query_var( 'monthnum' );
       
   164 		$day   = get_query_var( 'day' );
       
   165 
       
   166 		if ( $year && $month && $day ) {
       
   167 			$date = sprintf( '%04d-%02d-%02d', $year, $month, $day );
       
   168 
       
   169 			if ( ! wp_checkdate( $month, $day, $year, $date ) ) {
       
   170 				$redirect_url = get_month_link( $year, $month );
       
   171 
       
   172 				$redirect['query'] = _remove_qs_args_if_not_in_url(
       
   173 					$redirect['query'],
       
   174 					array( 'year', 'monthnum', 'day' ),
       
   175 					$redirect_url
       
   176 				);
       
   177 			}
       
   178 		} elseif ( $year && $month && $month > 12 ) {
       
   179 			$redirect_url = get_year_link( $year );
       
   180 
       
   181 			$redirect['query'] = _remove_qs_args_if_not_in_url(
       
   182 				$redirect['query'],
       
   183 				array( 'year', 'monthnum' ),
       
   184 				$redirect_url
       
   185 			);
       
   186 		}
       
   187 
       
   188 		// Strip off non-existing <!--nextpage--> links from single posts or pages.
       
   189 		if ( get_query_var( 'page' ) ) {
       
   190 			$post_id = 0;
       
   191 
       
   192 			if ( $wp_query->queried_object instanceof WP_Post ) {
       
   193 				$post_id = $wp_query->queried_object->ID;
       
   194 			} elseif ( $wp_query->post ) {
       
   195 				$post_id = $wp_query->post->ID;
       
   196 			}
       
   197 
       
   198 			if ( $post_id ) {
       
   199 				$redirect_url = get_permalink( $post_id );
       
   200 
       
   201 				$redirect['path']  = rtrim( $redirect['path'], (int) get_query_var( 'page' ) . '/' );
       
   202 				$redirect['query'] = remove_query_arg( 'page', $redirect['query'] );
       
   203 			}
       
   204 		}
       
   205 
       
   206 		if ( ! $redirect_url ) {
       
   207 			$redirect_url = redirect_guess_404_permalink();
       
   208 
       
   209 			if ( $redirect_url ) {
       
   210 				$redirect['query'] = _remove_qs_args_if_not_in_url(
       
   211 					$redirect['query'],
       
   212 					array( 'page', 'feed', 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ),
       
   213 					$redirect_url
       
   214 				);
       
   215 			}
       
   216 		}
       
   217 	} elseif ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) {
       
   218 
       
   219 		// Rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101.
       
   220 		if ( is_attachment()
       
   221 			&& ! array_diff( array_keys( $wp->query_vars ), array( 'attachment', 'attachment_id' ) )
       
   222 			&& ! $redirect_url
       
   223 		) {
       
   224 			if ( ! empty( $_GET['attachment_id'] ) ) {
       
   225 				$redirect_url = get_attachment_link( get_query_var( 'attachment_id' ) );
       
   226 
       
   227 				if ( $redirect_url ) {
       
   228 					$redirect['query'] = remove_query_arg( 'attachment_id', $redirect['query'] );
       
   229 				}
       
   230 			} else {
       
   231 				$redirect_url = get_attachment_link();
       
   232 			}
       
   233 		} elseif ( is_single() && ! empty( $_GET['p'] ) && ! $redirect_url ) {
       
   234 			$redirect_url = get_permalink( get_query_var( 'p' ) );
       
   235 
       
   236 			if ( $redirect_url ) {
       
   237 				$redirect['query'] = remove_query_arg( array( 'p', 'post_type' ), $redirect['query'] );
       
   238 			}
       
   239 		} elseif ( is_single() && ! empty( $_GET['name'] ) && ! $redirect_url ) {
       
   240 			$redirect_url = get_permalink( $wp_query->get_queried_object_id() );
       
   241 
       
   242 			if ( $redirect_url ) {
       
   243 				$redirect['query'] = remove_query_arg( 'name', $redirect['query'] );
       
   244 			}
       
   245 		} elseif ( is_page() && ! empty( $_GET['page_id'] ) && ! $redirect_url ) {
       
   246 			$redirect_url = get_permalink( get_query_var( 'page_id' ) );
       
   247 
       
   248 			if ( $redirect_url ) {
       
   249 				$redirect['query'] = remove_query_arg( 'page_id', $redirect['query'] );
       
   250 			}
       
   251 		} elseif ( is_page() && ! is_feed() && ! $redirect_url
       
   252 			&& 'page' === get_option( 'show_on_front' ) && get_queried_object_id() === (int) get_option( 'page_on_front' )
       
   253 		) {
       
   254 			$redirect_url = home_url( '/' );
       
   255 		} elseif ( is_home() && ! empty( $_GET['page_id'] ) && ! $redirect_url
       
   256 			&& 'page' === get_option( 'show_on_front' ) && get_query_var( 'page_id' ) === (int) get_option( 'page_for_posts' )
       
   257 		) {
       
   258 			$redirect_url = get_permalink( get_option( 'page_for_posts' ) );
       
   259 
       
   260 			if ( $redirect_url ) {
       
   261 				$redirect['query'] = remove_query_arg( 'page_id', $redirect['query'] );
       
   262 			}
       
   263 		} elseif ( ! empty( $_GET['m'] ) && ( is_year() || is_month() || is_day() ) ) {
       
   264 			$m = get_query_var( 'm' );
       
   265 
       
   266 			switch ( strlen( $m ) ) {
       
   267 				case 4: // Yearly.
       
   268 					$redirect_url = get_year_link( $m );
       
   269 					break;
       
   270 				case 6: // Monthly.
       
   271 					$redirect_url = get_month_link( substr( $m, 0, 4 ), substr( $m, 4, 2 ) );
       
   272 					break;
       
   273 				case 8: // Daily.
       
   274 					$redirect_url = get_day_link( substr( $m, 0, 4 ), substr( $m, 4, 2 ), substr( $m, 6, 2 ) );
       
   275 					break;
       
   276 			}
       
   277 
       
   278 			if ( $redirect_url ) {
       
   279 				$redirect['query'] = remove_query_arg( 'm', $redirect['query'] );
       
   280 			}
       
   281 			// Now moving on to non ?m=X year/month/day links.
       
   282 		} elseif ( is_date() ) {
   132 			$year  = get_query_var( 'year' );
   283 			$year  = get_query_var( 'year' );
   133 			$month = get_query_var( 'monthnum' );
   284 			$month = get_query_var( 'monthnum' );
   134 			$day   = get_query_var( 'day' );
   285 			$day   = get_query_var( 'day' );
   135 			$date  = sprintf( '%04d-%02d-%02d', $year, $month, $day );
   286 
   136 			if ( ! wp_checkdate( $month, $day, $year, $date ) ) {
   287 			if ( is_day() && $year && $month && ! empty( $_GET['day'] ) ) {
   137 				$redirect_url      = get_month_link( $year, $month );
   288 				$redirect_url = get_day_link( $year, $month, $day );
   138 				$redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'year', 'monthnum', 'day' ), $redirect_url );
   289 
   139 			}
       
   140 		} elseif ( get_query_var( 'monthnum' ) && get_query_var( 'year' ) && 12 < get_query_var( 'monthnum' ) ) {
       
   141 			$redirect_url      = get_year_link( get_query_var( 'year' ) );
       
   142 			$redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'year', 'monthnum' ), $redirect_url );
       
   143 		}
       
   144 
       
   145 		if ( ! $redirect_url ) {
       
   146 			if ( $redirect_url = redirect_guess_404_permalink() ) {
       
   147 				$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 );
       
   148 			}
       
   149 		}
       
   150 
       
   151 		if ( get_query_var( 'page' ) && $wp_query->post &&
       
   152 			false !== strpos( $wp_query->post->post_content, '<!--nextpage-->' ) ) {
       
   153 			$redirect['path']  = rtrim( $redirect['path'], (int) get_query_var( 'page' ) . '/' );
       
   154 			$redirect['query'] = remove_query_arg( 'page', $redirect['query'] );
       
   155 			$redirect_url      = get_permalink( $wp_query->post->ID );
       
   156 		}
       
   157 	} elseif ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) {
       
   158 		// rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101
       
   159 		if ( is_attachment() &&
       
   160 			! array_diff( array_keys( $wp->query_vars ), array( 'attachment', 'attachment_id' ) ) &&
       
   161 			! $redirect_url ) {
       
   162 			if ( ! empty( $_GET['attachment_id'] ) ) {
       
   163 				$redirect_url = get_attachment_link( get_query_var( 'attachment_id' ) );
       
   164 				if ( $redirect_url ) {
   290 				if ( $redirect_url ) {
   165 					$redirect['query'] = remove_query_arg( 'attachment_id', $redirect['query'] );
   291 					$redirect['query'] = remove_query_arg( array( 'year', 'monthnum', 'day' ), $redirect['query'] );
   166 				}
   292 				}
   167 			} else {
   293 			} elseif ( is_month() && $year && ! empty( $_GET['monthnum'] ) ) {
   168 				$redirect_url = get_attachment_link();
   294 				$redirect_url = get_month_link( $year, $month );
   169 			}
   295 
   170 		} elseif ( is_single() && ! empty( $_GET['p'] ) && ! $redirect_url ) {
   296 				if ( $redirect_url ) {
   171 			if ( $redirect_url = get_permalink( get_query_var( 'p' ) ) ) {
   297 					$redirect['query'] = remove_query_arg( array( 'year', 'monthnum' ), $redirect['query'] );
   172 				$redirect['query'] = remove_query_arg( array( 'p', 'post_type' ), $redirect['query'] );
   298 				}
   173 			}
   299 			} elseif ( is_year() && ! empty( $_GET['year'] ) ) {
   174 		} elseif ( is_single() && ! empty( $_GET['name'] ) && ! $redirect_url ) {
   300 				$redirect_url = get_year_link( $year );
   175 			if ( $redirect_url = get_permalink( $wp_query->get_queried_object_id() ) ) {
   301 
   176 				$redirect['query'] = remove_query_arg( 'name', $redirect['query'] );
   302 				if ( $redirect_url ) {
   177 			}
   303 					$redirect['query'] = remove_query_arg( 'year', $redirect['query'] );
   178 		} elseif ( is_page() && ! empty( $_GET['page_id'] ) && ! $redirect_url ) {
   304 				}
   179 			if ( $redirect_url = get_permalink( get_query_var( 'page_id' ) ) ) {
       
   180 				$redirect['query'] = remove_query_arg( 'page_id', $redirect['query'] );
       
   181 			}
       
   182 		} elseif ( is_page() && ! is_feed() && 'page' == get_option( 'show_on_front' ) && get_queried_object_id() == get_option( 'page_on_front' ) && ! $redirect_url ) {
       
   183 			$redirect_url = home_url( '/' );
       
   184 		} 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 ) {
       
   185 			if ( $redirect_url = get_permalink( get_option( 'page_for_posts' ) ) ) {
       
   186 				$redirect['query'] = remove_query_arg( 'page_id', $redirect['query'] );
       
   187 			}
       
   188 		} elseif ( ! empty( $_GET['m'] ) && ( is_year() || is_month() || is_day() ) ) {
       
   189 			$m = get_query_var( 'm' );
       
   190 			switch ( strlen( $m ) ) {
       
   191 				case 4: // Yearly
       
   192 					$redirect_url = get_year_link( $m );
       
   193 					break;
       
   194 				case 6: // Monthly
       
   195 					$redirect_url = get_month_link( substr( $m, 0, 4 ), substr( $m, 4, 2 ) );
       
   196 					break;
       
   197 				case 8: // Daily
       
   198 					$redirect_url = get_day_link( substr( $m, 0, 4 ), substr( $m, 4, 2 ), substr( $m, 6, 2 ) );
       
   199 					break;
       
   200 			}
       
   201 			if ( $redirect_url ) {
       
   202 				$redirect['query'] = remove_query_arg( 'm', $redirect['query'] );
       
   203 			}
       
   204 			// now moving on to non ?m=X year/month/day links
       
   205 		} elseif ( is_day() && get_query_var( 'year' ) && get_query_var( 'monthnum' ) && ! empty( $_GET['day'] ) ) {
       
   206 			if ( $redirect_url = get_day_link( get_query_var( 'year' ), get_query_var( 'monthnum' ), get_query_var( 'day' ) ) ) {
       
   207 				$redirect['query'] = remove_query_arg( array( 'year', 'monthnum', 'day' ), $redirect['query'] );
       
   208 			}
       
   209 		} elseif ( is_month() && get_query_var( 'year' ) && ! empty( $_GET['monthnum'] ) ) {
       
   210 			if ( $redirect_url = get_month_link( get_query_var( 'year' ), get_query_var( 'monthnum' ) ) ) {
       
   211 				$redirect['query'] = remove_query_arg( array( 'year', 'monthnum' ), $redirect['query'] );
       
   212 			}
       
   213 		} elseif ( is_year() && ! empty( $_GET['year'] ) ) {
       
   214 			if ( $redirect_url = get_year_link( get_query_var( 'year' ) ) ) {
       
   215 				$redirect['query'] = remove_query_arg( 'year', $redirect['query'] );
       
   216 			}
   305 			}
   217 		} elseif ( is_author() && ! empty( $_GET['author'] ) && preg_match( '|^[0-9]+$|', $_GET['author'] ) ) {
   306 		} elseif ( is_author() && ! empty( $_GET['author'] ) && preg_match( '|^[0-9]+$|', $_GET['author'] ) ) {
   218 			$author = get_userdata( get_query_var( 'author' ) );
   307 			$author = get_userdata( get_query_var( 'author' ) );
   219 			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 ) ) ) {
   308 
   220 				if ( $redirect_url = get_author_posts_url( $author->ID, $author->user_nicename ) ) {
   309 			if ( false !== $author
       
   310 				&& $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 ) )
       
   311 			) {
       
   312 				$redirect_url = get_author_posts_url( $author->ID, $author->user_nicename );
       
   313 
       
   314 				if ( $redirect_url ) {
   221 					$redirect['query'] = remove_query_arg( 'author', $redirect['query'] );
   315 					$redirect['query'] = remove_query_arg( 'author', $redirect['query'] );
   222 				}
   316 				}
   223 			}
   317 			}
   224 		} elseif ( is_category() || is_tag() || is_tax() ) { // Terms (Tags/categories)
   318 		} elseif ( is_category() || is_tag() || is_tax() ) { // Terms (tags/categories).
   225 
       
   226 			$term_count = 0;
   319 			$term_count = 0;
       
   320 
   227 			foreach ( $wp_query->tax_query->queried_terms as $tax_query ) {
   321 			foreach ( $wp_query->tax_query->queried_terms as $tax_query ) {
   228 				$term_count += count( $tax_query['terms'] );
   322 				$term_count += count( $tax_query['terms'] );
   229 			}
   323 			}
   230 
   324 
   231 			$obj = $wp_query->get_queried_object();
   325 			$obj = $wp_query->get_queried_object();
   232 			if ( $term_count <= 1 && ! empty( $obj->term_id ) && ( $tax_url = get_term_link( (int) $obj->term_id, $obj->taxonomy ) ) && ! is_wp_error( $tax_url ) ) {
   326 
   233 				if ( ! empty( $redirect['query'] ) ) {
   327 			if ( $term_count <= 1 && ! empty( $obj->term_id ) ) {
   234 					// Strip taxonomy query vars off the url.
   328 				$tax_url = get_term_link( (int) $obj->term_id, $obj->taxonomy );
   235 					$qv_remove = array( 'term', 'taxonomy' );
   329 
   236 					if ( is_category() ) {
   330 				if ( $tax_url && ! is_wp_error( $tax_url ) ) {
   237 						$qv_remove[] = 'category_name';
   331 					if ( ! empty( $redirect['query'] ) ) {
   238 						$qv_remove[] = 'cat';
   332 						// Strip taxonomy query vars off the URL.
   239 					} elseif ( is_tag() ) {
   333 						$qv_remove = array( 'term', 'taxonomy' );
   240 						$qv_remove[] = 'tag';
   334 
   241 						$qv_remove[] = 'tag_id';
   335 						if ( is_category() ) {
   242 					} else { // Custom taxonomies will have a custom query var, remove those too:
   336 							$qv_remove[] = 'category_name';
   243 						$tax_obj = get_taxonomy( $obj->taxonomy );
   337 							$qv_remove[] = 'cat';
   244 						if ( false !== $tax_obj->query_var ) {
   338 						} elseif ( is_tag() ) {
   245 							$qv_remove[] = $tax_obj->query_var;
   339 							$qv_remove[] = 'tag';
       
   340 							$qv_remove[] = 'tag_id';
       
   341 						} else {
       
   342 							// Custom taxonomies will have a custom query var, remove those too.
       
   343 							$tax_obj = get_taxonomy( $obj->taxonomy );
       
   344 							if ( false !== $tax_obj->query_var ) {
       
   345 								$qv_remove[] = $tax_obj->query_var;
       
   346 							}
   246 						}
   347 						}
   247 					}
   348 
   248 
   349 						$rewrite_vars = array_diff( array_keys( $wp_query->query ), array_keys( $_GET ) );
   249 					$rewrite_vars = array_diff( array_keys( $wp_query->query ), array_keys( $_GET ) );
   350 
   250 
   351 						// Check to see if all the query vars are coming from the rewrite, none are set via $_GET.
   251 					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
   352 						if ( ! array_diff( $rewrite_vars, array_keys( $_GET ) ) ) {
   252 						$redirect['query'] = remove_query_arg( $qv_remove, $redirect['query'] ); //Remove all of the per-tax qv's
   353 							// Remove all of the per-tax query vars.
   253 
   354 							$redirect['query'] = remove_query_arg( $qv_remove, $redirect['query'] );
   254 						// Create the destination url for this taxonomy
   355 
   255 						$tax_url = parse_url( $tax_url );
   356 							// Create the destination URL for this taxonomy.
   256 						if ( ! empty( $tax_url['query'] ) ) { // Taxonomy accessible via ?taxonomy=..&term=.. or any custom qv..
   357 							$tax_url = parse_url( $tax_url );
   257 							parse_str( $tax_url['query'], $query_vars );
   358 
   258 							$redirect['query'] = add_query_arg( $query_vars, $redirect['query'] );
   359 							if ( ! empty( $tax_url['query'] ) ) {
   259 						} else { // Taxonomy is accessible via a "pretty-URL"
   360 								// Taxonomy accessible via ?taxonomy=...&term=... or any custom query var.
   260 							$redirect['path'] = $tax_url['path'];
   361 								parse_str( $tax_url['query'], $query_vars );
   261 						}
   362 								$redirect['query'] = add_query_arg( $query_vars, $redirect['query'] );
   262 					} else { // Some query vars are set via $_GET. Unset those from $_GET that exist via the rewrite
   363 							} else {
   263 						foreach ( $qv_remove as $_qv ) {
   364 								// Taxonomy is accessible via a "pretty URL".
   264 							if ( isset( $rewrite_vars[ $_qv ] ) ) {
   365 								$redirect['path'] = $tax_url['path'];
   265 								$redirect['query'] = remove_query_arg( $_qv, $redirect['query'] );
   366 							}
       
   367 						} else {
       
   368 							// Some query vars are set via $_GET. Unset those from $_GET that exist via the rewrite.
       
   369 							foreach ( $qv_remove as $_qv ) {
       
   370 								if ( isset( $rewrite_vars[ $_qv ] ) ) {
       
   371 									$redirect['query'] = remove_query_arg( $_qv, $redirect['query'] );
       
   372 								}
   266 							}
   373 							}
   267 						}
   374 						}
   268 					}
   375 					}
   269 				}
   376 				}
   270 			}
   377 			}
   271 		} elseif ( is_single() && strpos( $wp_rewrite->permalink_structure, '%category%' ) !== false && $cat = get_query_var( 'category_name' ) ) {
   378 		} elseif ( is_single() && strpos( $wp_rewrite->permalink_structure, '%category%' ) !== false ) {
   272 			$category = get_category_by_path( $cat );
   379 			$category_name = get_query_var( 'category_name' );
   273 			if ( ( ! $category || is_wp_error( $category ) ) || ! has_term( $category->term_id, 'category', $wp_query->get_queried_object_id() ) ) {
   380 
   274 				$redirect_url = get_permalink( $wp_query->get_queried_object_id() );
   381 			if ( $category_name ) {
   275 			}
   382 				$category = get_category_by_path( $category_name );
   276 		}
   383 
   277 
   384 				if ( ! $category || is_wp_error( $category )
   278 			// Post Paging
   385 					|| ! has_term( $category->term_id, 'category', $wp_query->get_queried_object_id() )
       
   386 				) {
       
   387 					$redirect_url = get_permalink( $wp_query->get_queried_object_id() );
       
   388 				}
       
   389 			}
       
   390 		}
       
   391 
       
   392 		// Post paging.
   279 		if ( is_singular() && get_query_var( 'page' ) ) {
   393 		if ( is_singular() && get_query_var( 'page' ) ) {
       
   394 			$page = get_query_var( 'page' );
       
   395 
   280 			if ( ! $redirect_url ) {
   396 			if ( ! $redirect_url ) {
   281 				$redirect_url = get_permalink( get_queried_object_id() );
   397 				$redirect_url = get_permalink( get_queried_object_id() );
   282 			}
   398 			}
   283 
   399 
   284 			$page = get_query_var( 'page' );
       
   285 			if ( $page > 1 ) {
   400 			if ( $page > 1 ) {
       
   401 				$redirect_url = trailingslashit( $redirect_url );
       
   402 
   286 				if ( is_front_page() ) {
   403 				if ( is_front_page() ) {
   287 					$redirect_url = trailingslashit( $redirect_url ) . user_trailingslashit( "$wp_rewrite->pagination_base/$page", 'paged' );
   404 					$redirect_url .= user_trailingslashit( "$wp_rewrite->pagination_base/$page", 'paged' );
   288 				} else {
   405 				} else {
   289 					$redirect_url = trailingslashit( $redirect_url ) . user_trailingslashit( $page, 'single_paged' );
   406 					$redirect_url .= user_trailingslashit( $page, 'single_paged' );
   290 				}
   407 				}
   291 			}
   408 			}
   292 				$redirect['query'] = remove_query_arg( 'page', $redirect['query'] );
   409 
   293 		}
   410 			$redirect['query'] = remove_query_arg( 'page', $redirect['query'] );
   294 
   411 		}
   295 			// paging and feeds
   412 
   296 		if ( get_query_var( 'paged' ) || is_feed() || get_query_var( 'cpage' ) ) {
   413 		if ( get_query_var( 'sitemap' ) ) {
   297 			while ( preg_match( "#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", $redirect['path'] ) || preg_match( '#/(comments/?)?(feed|rss|rdf|atom|rss2)(/+)?$#', $redirect['path'] ) || preg_match( "#/{$wp_rewrite->comments_pagination_base}-[0-9]+(/+)?$#", $redirect['path'] ) ) {
   414 			$redirect_url      = get_sitemap_url( get_query_var( 'sitemap' ), get_query_var( 'sitemap-subtype' ), get_query_var( 'paged' ) );
   298 				// Strip off paging and feed
   415 			$redirect['query'] = remove_query_arg( array( 'sitemap', 'sitemap-subtype', 'paged' ), $redirect['query'] );
   299 				$redirect['path'] = preg_replace( "#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", '/', $redirect['path'] ); // strip off any existing paging
   416 		} elseif ( get_query_var( 'paged' ) || is_feed() || get_query_var( 'cpage' ) ) {
   300 				$redirect['path'] = preg_replace( '#/(comments/?)?(feed|rss2?|rdf|atom)(/+|$)#', '/', $redirect['path'] ); // strip off feed endings
   417 			// Paging and feeds.
   301 				$redirect['path'] = preg_replace( "#/{$wp_rewrite->comments_pagination_base}-[0-9]+?(/+)?$#", '/', $redirect['path'] ); // strip off any existing comment paging
   418 			$paged = get_query_var( 'paged' );
   302 			}
   419 			$feed  = get_query_var( 'feed' );
   303 
   420 			$cpage = get_query_var( 'cpage' );
   304 			$addl_path = '';
   421 
   305 			if ( is_feed() && in_array( get_query_var( 'feed' ), $wp_rewrite->feeds ) ) {
   422 			while ( preg_match( "#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", $redirect['path'] )
       
   423 				|| preg_match( '#/(comments/?)?(feed|rss2?|rdf|atom)(/+)?$#', $redirect['path'] )
       
   424 				|| preg_match( "#/{$wp_rewrite->comments_pagination_base}-[0-9]+(/+)?$#", $redirect['path'] )
       
   425 			) {
       
   426 				// Strip off any existing paging.
       
   427 				$redirect['path'] = preg_replace( "#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", '/', $redirect['path'] );
       
   428 				// Strip off feed endings.
       
   429 				$redirect['path'] = preg_replace( '#/(comments/?)?(feed|rss2?|rdf|atom)(/+|$)#', '/', $redirect['path'] );
       
   430 				// Strip off any existing comment paging.
       
   431 				$redirect['path'] = preg_replace( "#/{$wp_rewrite->comments_pagination_base}-[0-9]+?(/+)?$#", '/', $redirect['path'] );
       
   432 			}
       
   433 
       
   434 			$addl_path    = '';
       
   435 			$default_feed = get_default_feed();
       
   436 
       
   437 			if ( is_feed() && in_array( $feed, $wp_rewrite->feeds, true ) ) {
   306 				$addl_path = ! empty( $addl_path ) ? trailingslashit( $addl_path ) : '';
   438 				$addl_path = ! empty( $addl_path ) ? trailingslashit( $addl_path ) : '';
       
   439 
   307 				if ( ! is_singular() && get_query_var( 'withcomments' ) ) {
   440 				if ( ! is_singular() && get_query_var( 'withcomments' ) ) {
   308 					$addl_path .= 'comments/';
   441 					$addl_path .= 'comments/';
   309 				}
   442 				}
   310 				if ( ( 'rss' == get_default_feed() && 'feed' == get_query_var( 'feed' ) ) || 'rss' == get_query_var( 'feed' ) ) {
   443 
   311 					$addl_path .= user_trailingslashit( 'feed/' . ( ( get_default_feed() == 'rss2' ) ? '' : 'rss2' ), 'feed' );
   444 				if ( ( 'rss' === $default_feed && 'feed' === $feed ) || 'rss' === $feed ) {
       
   445 					$format = ( 'rss2' === $default_feed ) ? '' : 'rss2';
   312 				} else {
   446 				} else {
   313 					$addl_path .= user_trailingslashit( 'feed/' . ( ( get_default_feed() == get_query_var( 'feed' ) || 'feed' == get_query_var( 'feed' ) ) ? '' : get_query_var( 'feed' ) ), 'feed' );
   447 					$format = ( $default_feed === $feed || 'feed' === $feed ) ? '' : $feed;
   314 				}
   448 				}
       
   449 
       
   450 				$addl_path .= user_trailingslashit( 'feed/' . $format, 'feed' );
       
   451 
   315 				$redirect['query'] = remove_query_arg( 'feed', $redirect['query'] );
   452 				$redirect['query'] = remove_query_arg( 'feed', $redirect['query'] );
   316 			} elseif ( is_feed() && 'old' == get_query_var( 'feed' ) ) {
   453 			} elseif ( is_feed() && 'old' === $feed ) {
   317 				$old_feed_files = array(
   454 				$old_feed_files = array(
   318 					'wp-atom.php'         => 'atom',
   455 					'wp-atom.php'         => 'atom',
   319 					'wp-commentsrss2.php' => 'comments_rss2',
   456 					'wp-commentsrss2.php' => 'comments_rss2',
   320 					'wp-feed.php'         => get_default_feed(),
   457 					'wp-feed.php'         => $default_feed,
   321 					'wp-rdf.php'          => 'rdf',
   458 					'wp-rdf.php'          => 'rdf',
   322 					'wp-rss.php'          => 'rss2',
   459 					'wp-rss.php'          => 'rss2',
   323 					'wp-rss2.php'         => 'rss2',
   460 					'wp-rss2.php'         => 'rss2',
   324 				);
   461 				);
       
   462 
   325 				if ( isset( $old_feed_files[ basename( $redirect['path'] ) ] ) ) {
   463 				if ( isset( $old_feed_files[ basename( $redirect['path'] ) ] ) ) {
   326 					$redirect_url = get_feed_link( $old_feed_files[ basename( $redirect['path'] ) ] );
   464 					$redirect_url = get_feed_link( $old_feed_files[ basename( $redirect['path'] ) ] );
       
   465 
   327 					wp_redirect( $redirect_url, 301 );
   466 					wp_redirect( $redirect_url, 301 );
   328 					die();
   467 					die();
   329 				}
   468 				}
   330 			}
   469 			}
   331 
   470 
   332 			if ( get_query_var( 'paged' ) > 0 ) {
   471 			if ( $paged > 0 ) {
   333 				$paged             = get_query_var( 'paged' );
       
   334 				$redirect['query'] = remove_query_arg( 'paged', $redirect['query'] );
   472 				$redirect['query'] = remove_query_arg( 'paged', $redirect['query'] );
       
   473 
   335 				if ( ! is_feed() ) {
   474 				if ( ! is_feed() ) {
   336 					if ( $paged > 1 && ! is_single() ) {
   475 					if ( ! is_single() ) {
   337 						$addl_path = ( ! empty( $addl_path ) ? trailingslashit( $addl_path ) : '' ) . user_trailingslashit( "$wp_rewrite->pagination_base/$paged", 'paged' );
       
   338 					} elseif ( ! is_single() ) {
       
   339 						$addl_path = ! empty( $addl_path ) ? trailingslashit( $addl_path ) : '';
   476 						$addl_path = ! empty( $addl_path ) ? trailingslashit( $addl_path ) : '';
       
   477 
       
   478 						if ( $paged > 1 ) {
       
   479 							$addl_path .= user_trailingslashit( "$wp_rewrite->pagination_base/$paged", 'paged' );
       
   480 						}
   340 					}
   481 					}
   341 				} elseif ( $paged > 1 ) {
   482 				} elseif ( $paged > 1 ) {
   342 					$redirect['query'] = add_query_arg( 'paged', $paged, $redirect['query'] );
   483 					$redirect['query'] = add_query_arg( 'paged', $paged, $redirect['query'] );
   343 				}
   484 				}
   344 			}
   485 			}
   345 
   486 
   346 			if ( get_option( 'page_comments' ) && (
   487 			$default_comments_page = get_option( 'default_comments_page' );
   347 			( 'newest' == get_option( 'default_comments_page' ) && get_query_var( 'cpage' ) > 0 ) ||
   488 
   348 			( 'newest' != get_option( 'default_comments_page' ) && get_query_var( 'cpage' ) > 1 )
   489 			if ( get_option( 'page_comments' )
   349 			) ) {
   490 				&& ( 'newest' === $default_comments_page && $cpage > 0
   350 				$addl_path         = ( ! empty( $addl_path ) ? trailingslashit( $addl_path ) : '' ) . user_trailingslashit( $wp_rewrite->comments_pagination_base . '-' . get_query_var( 'cpage' ), 'commentpaged' );
   491 					|| 'newest' !== $default_comments_page && $cpage > 1 )
       
   492 			) {
       
   493 				$addl_path  = ( ! empty( $addl_path ) ? trailingslashit( $addl_path ) : '' );
       
   494 				$addl_path .= user_trailingslashit( $wp_rewrite->comments_pagination_base . '-' . $cpage, 'commentpaged' );
       
   495 
   351 				$redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] );
   496 				$redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] );
   352 			}
   497 			}
   353 
   498 
   354 			$redirect['path'] = user_trailingslashit( preg_replace( '|/' . preg_quote( $wp_rewrite->index, '|' ) . '/?$|', '/', $redirect['path'] ) ); // strip off trailing /index.php/
   499 			// Strip off trailing /index.php/.
   355 			if ( ! empty( $addl_path ) && $wp_rewrite->using_index_permalinks() && strpos( $redirect['path'], '/' . $wp_rewrite->index . '/' ) === false ) {
   500 			$redirect['path'] = preg_replace( '|/' . preg_quote( $wp_rewrite->index, '|' ) . '/?$|', '/', $redirect['path'] );
       
   501 			$redirect['path'] = user_trailingslashit( $redirect['path'] );
       
   502 
       
   503 			if ( ! empty( $addl_path )
       
   504 				&& $wp_rewrite->using_index_permalinks()
       
   505 				&& strpos( $redirect['path'], '/' . $wp_rewrite->index . '/' ) === false
       
   506 			) {
   356 				$redirect['path'] = trailingslashit( $redirect['path'] ) . $wp_rewrite->index . '/';
   507 				$redirect['path'] = trailingslashit( $redirect['path'] ) . $wp_rewrite->index . '/';
   357 			}
   508 			}
       
   509 
   358 			if ( ! empty( $addl_path ) ) {
   510 			if ( ! empty( $addl_path ) ) {
   359 				$redirect['path'] = trailingslashit( $redirect['path'] ) . $addl_path;
   511 				$redirect['path'] = trailingslashit( $redirect['path'] ) . $addl_path;
   360 			}
   512 			}
       
   513 
   361 			$redirect_url = $redirect['scheme'] . '://' . $redirect['host'] . $redirect['path'];
   514 			$redirect_url = $redirect['scheme'] . '://' . $redirect['host'] . $redirect['path'];
   362 		}
   515 		}
   363 
   516 
   364 		if ( 'wp-register.php' == basename( $redirect['path'] ) ) {
   517 		if ( 'wp-register.php' === basename( $redirect['path'] ) ) {
   365 			if ( is_multisite() ) {
   518 			if ( is_multisite() ) {
   366 				/** This filter is documented in wp-login.php */
   519 				/** This filter is documented in wp-login.php */
   367 				$redirect_url = apply_filters( 'wp_signup_location', network_site_url( 'wp-signup.php' ) );
   520 				$redirect_url = apply_filters( 'wp_signup_location', network_site_url( 'wp-signup.php' ) );
   368 			} else {
   521 			} else {
   369 				$redirect_url = wp_registration_url();
   522 				$redirect_url = wp_registration_url();
   372 			wp_redirect( $redirect_url, 301 );
   525 			wp_redirect( $redirect_url, 301 );
   373 			die();
   526 			die();
   374 		}
   527 		}
   375 	}
   528 	}
   376 
   529 
   377 	// tack on any additional query vars
       
   378 	$redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] );
   530 	$redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] );
       
   531 
       
   532 	// Tack on any additional query vars.
   379 	if ( $redirect_url && ! empty( $redirect['query'] ) ) {
   533 	if ( $redirect_url && ! empty( $redirect['query'] ) ) {
   380 		parse_str( $redirect['query'], $_parsed_query );
   534 		parse_str( $redirect['query'], $_parsed_query );
   381 		$redirect = @parse_url( $redirect_url );
   535 		$redirect = parse_url( $redirect_url );
   382 
   536 
   383 		if ( ! empty( $_parsed_query['name'] ) && ! empty( $redirect['query'] ) ) {
   537 		if ( ! empty( $_parsed_query['name'] ) && ! empty( $redirect['query'] ) ) {
   384 			parse_str( $redirect['query'], $_parsed_redirect_query );
   538 			parse_str( $redirect['query'], $_parsed_redirect_query );
   385 
   539 
   386 			if ( empty( $_parsed_redirect_query['name'] ) ) {
   540 			if ( empty( $_parsed_redirect_query['name'] ) ) {
   390 
   544 
   391 		$_parsed_query = array_combine(
   545 		$_parsed_query = array_combine(
   392 			rawurlencode_deep( array_keys( $_parsed_query ) ),
   546 			rawurlencode_deep( array_keys( $_parsed_query ) ),
   393 			rawurlencode_deep( array_values( $_parsed_query ) )
   547 			rawurlencode_deep( array_values( $_parsed_query ) )
   394 		);
   548 		);
   395 		$redirect_url  = add_query_arg( $_parsed_query, $redirect_url );
   549 
       
   550 		$redirect_url = add_query_arg( $_parsed_query, $redirect_url );
   396 	}
   551 	}
   397 
   552 
   398 	if ( $redirect_url ) {
   553 	if ( $redirect_url ) {
   399 		$redirect = @parse_url( $redirect_url );
   554 		$redirect = parse_url( $redirect_url );
   400 	}
   555 	}
   401 
   556 
   402 	// www.example.com vs example.com
   557 	// www.example.com vs. example.com
   403 	$user_home = @parse_url( home_url() );
   558 	$user_home = parse_url( home_url() );
       
   559 
   404 	if ( ! empty( $user_home['host'] ) ) {
   560 	if ( ! empty( $user_home['host'] ) ) {
   405 		$redirect['host'] = $user_home['host'];
   561 		$redirect['host'] = $user_home['host'];
   406 	}
   562 	}
       
   563 
   407 	if ( empty( $user_home['path'] ) ) {
   564 	if ( empty( $user_home['path'] ) ) {
   408 		$user_home['path'] = '/';
   565 		$user_home['path'] = '/';
   409 	}
   566 	}
   410 
   567 
   411 	// Handle ports
   568 	// Handle ports.
   412 	if ( ! empty( $user_home['port'] ) ) {
   569 	if ( ! empty( $user_home['port'] ) ) {
   413 		$redirect['port'] = $user_home['port'];
   570 		$redirect['port'] = $user_home['port'];
   414 	} else {
   571 	} else {
   415 		unset( $redirect['port'] );
   572 		unset( $redirect['port'] );
   416 	}
   573 	}
   417 
   574 
   418 	// trailing /index.php
   575 	// Trailing /index.php.
   419 	$redirect['path'] = preg_replace( '|/' . preg_quote( $wp_rewrite->index, '|' ) . '/*?$|', '/', $redirect['path'] );
   576 	$redirect['path'] = preg_replace( '|/' . preg_quote( $wp_rewrite->index, '|' ) . '/*?$|', '/', $redirect['path'] );
   420 
   577 
   421 	$punctuation_pattern = implode(
   578 	$punctuation_pattern = implode(
   422 		'|',
   579 		'|',
   423 		array_map(
   580 		array_map(
   424 			'preg_quote',
   581 			'preg_quote',
   425 			array(
   582 			array(
   426 				' ',
   583 				' ',
   427 				'%20',  // space
   584 				'%20',  // Space.
   428 				'!',
   585 				'!',
   429 				'%21',  // exclamation mark
   586 				'%21',  // Exclamation mark.
   430 				'"',
   587 				'"',
   431 				'%22',  // double quote
   588 				'%22',  // Double quote.
   432 				"'",
   589 				"'",
   433 				'%27',  // single quote
   590 				'%27',  // Single quote.
   434 				'(',
   591 				'(',
   435 				'%28',  // opening bracket
   592 				'%28',  // Opening bracket.
   436 				')',
   593 				')',
   437 				'%29',  // closing bracket
   594 				'%29',  // Closing bracket.
   438 				',',
   595 				',',
   439 				'%2C',  // comma
   596 				'%2C',  // Comma.
   440 				'.',
   597 				'.',
   441 				'%2E',  // period
   598 				'%2E',  // Period.
   442 				';',
   599 				';',
   443 				'%3B',  // semicolon
   600 				'%3B',  // Semicolon.
   444 				'{',
   601 				'{',
   445 				'%7B',  // opening curly bracket
   602 				'%7B',  // Opening curly bracket.
   446 				'}',
   603 				'}',
   447 				'%7D',  // closing curly bracket
   604 				'%7D',  // Closing curly bracket.
   448 				'%E2%80%9C', // opening curly quote
   605 				'%E2%80%9C', // Opening curly quote.
   449 				'%E2%80%9D', // closing curly quote
   606 				'%E2%80%9D', // Closing curly quote.
   450 			)
   607 			)
   451 		)
   608 		)
   452 	);
   609 	);
   453 
   610 
   454 	// Remove trailing spaces and end punctuation from the path.
   611 	// Remove trailing spaces and end punctuation from the path.
   455 	$redirect['path'] = preg_replace( "#($punctuation_pattern)+$#", '', $redirect['path'] );
   612 	$redirect['path'] = preg_replace( "#($punctuation_pattern)+$#", '', $redirect['path'] );
   456 
   613 
   457 	if ( ! empty( $redirect['query'] ) ) {
   614 	if ( ! empty( $redirect['query'] ) ) {
   458 		// Remove trailing spaces and end punctuation from certain terminating query string args.
   615 		// Remove trailing spaces and end punctuation from certain terminating query string args.
   459 		$redirect['query'] = preg_replace( "#((p|page_id|cat|tag)=[^&]*?)($punctuation_pattern)+$#", '$1', $redirect['query'] );
   616 		$redirect['query'] = preg_replace( "#((^|&)(p|page_id|cat|tag)=[^&]*?)($punctuation_pattern)+$#", '$1', $redirect['query'] );
   460 
   617 
   461 		// Clean up empty query strings
   618 		// Clean up empty query strings.
   462 		$redirect['query'] = trim( preg_replace( '#(^|&)(p|page_id|cat|tag)=?(&|$)#', '&', $redirect['query'] ), '&' );
   619 		$redirect['query'] = trim( preg_replace( '#(^|&)(p|page_id|cat|tag)=?(&|$)#', '&', $redirect['query'] ), '&' );
   463 
   620 
   464 		// Redirect obsolete feeds
   621 		// Redirect obsolete feeds.
   465 		$redirect['query'] = preg_replace( '#(^|&)feed=rss(&|$)#', '$1feed=rss2$2', $redirect['query'] );
   622 		$redirect['query'] = preg_replace( '#(^|&)feed=rss(&|$)#', '$1feed=rss2$2', $redirect['query'] );
   466 
   623 
   467 		// Remove redundant leading ampersands
   624 		// Remove redundant leading ampersands.
   468 		$redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] );
   625 		$redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] );
   469 	}
   626 	}
   470 
   627 
   471 	// strip /index.php/ when we're not using PATHINFO permalinks
   628 	// Strip /index.php/ when we're not using PATHINFO permalinks.
   472 	if ( ! $wp_rewrite->using_index_permalinks() ) {
   629 	if ( ! $wp_rewrite->using_index_permalinks() ) {
   473 		$redirect['path'] = str_replace( '/' . $wp_rewrite->index . '/', '/', $redirect['path'] );
   630 		$redirect['path'] = str_replace( '/' . $wp_rewrite->index . '/', '/', $redirect['path'] );
   474 	}
   631 	}
   475 
   632 
   476 	// trailing slashes
   633 	// Trailing slashes.
   477 	if ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() && ! is_404() && ( ! is_front_page() || ( is_front_page() && ( get_query_var( 'paged' ) > 1 ) ) ) ) {
   634 	if ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks()
       
   635 		&& ! is_404() && ( ! is_front_page() || is_front_page() && get_query_var( 'paged' ) > 1 )
       
   636 	) {
   478 		$user_ts_type = '';
   637 		$user_ts_type = '';
       
   638 
   479 		if ( get_query_var( 'paged' ) > 0 ) {
   639 		if ( get_query_var( 'paged' ) > 0 ) {
   480 			$user_ts_type = 'paged';
   640 			$user_ts_type = 'paged';
   481 		} else {
   641 		} else {
   482 			foreach ( array( 'single', 'category', 'page', 'day', 'month', 'year', 'home' ) as $type ) {
   642 			foreach ( array( 'single', 'category', 'page', 'day', 'month', 'year', 'home' ) as $type ) {
   483 				$func = 'is_' . $type;
   643 				$func = 'is_' . $type;
   485 					$user_ts_type = $type;
   645 					$user_ts_type = $type;
   486 					break;
   646 					break;
   487 				}
   647 				}
   488 			}
   648 			}
   489 		}
   649 		}
       
   650 
   490 		$redirect['path'] = user_trailingslashit( $redirect['path'], $user_ts_type );
   651 		$redirect['path'] = user_trailingslashit( $redirect['path'], $user_ts_type );
   491 	} elseif ( is_front_page() ) {
   652 	} elseif ( is_front_page() ) {
   492 		$redirect['path'] = trailingslashit( $redirect['path'] );
   653 		$redirect['path'] = trailingslashit( $redirect['path'] );
   493 	}
   654 	}
   494 
   655 
   495 	// Strip multiple slashes out of the URL
   656 	// Remove trailing slash for robots.txt or sitemap requests.
       
   657 	if ( is_robots()
       
   658 		|| ! empty( get_query_var( 'sitemap' ) ) || ! empty( get_query_var( 'sitemap-stylesheet' ) )
       
   659 	) {
       
   660 		$redirect['path'] = untrailingslashit( $redirect['path'] );
       
   661 	}
       
   662 
       
   663 	// Strip multiple slashes out of the URL.
   496 	if ( strpos( $redirect['path'], '//' ) > -1 ) {
   664 	if ( strpos( $redirect['path'], '//' ) > -1 ) {
   497 		$redirect['path'] = preg_replace( '|/+|', '/', $redirect['path'] );
   665 		$redirect['path'] = preg_replace( '|/+|', '/', $redirect['path'] );
   498 	}
   666 	}
   499 
   667 
   500 	// Always trailing slash the Front Page URL
   668 	// Always trailing slash the Front Page URL.
   501 	if ( trailingslashit( $redirect['path'] ) == trailingslashit( $user_home['path'] ) ) {
   669 	if ( trailingslashit( $redirect['path'] ) === trailingslashit( $user_home['path'] ) ) {
   502 		$redirect['path'] = trailingslashit( $redirect['path'] );
   670 		$redirect['path'] = trailingslashit( $redirect['path'] );
   503 	}
   671 	}
   504 
   672 
   505 	// Ignore differences in host capitalization, as this can lead to infinite redirects
   673 	$original_host_low = strtolower( $original['host'] );
   506 	// Only redirect no-www <=> yes-www
   674 	$redirect_host_low = strtolower( $redirect['host'] );
   507 	if ( strtolower( $original['host'] ) == strtolower( $redirect['host'] ) ||
   675 
   508 		( strtolower( $original['host'] ) != 'www.' . strtolower( $redirect['host'] ) && 'www.' . strtolower( $original['host'] ) != strtolower( $redirect['host'] ) ) ) {
   676 	// Ignore differences in host capitalization, as this can lead to infinite redirects.
       
   677 	// Only redirect no-www <=> yes-www.
       
   678 	if ( $original_host_low === $redirect_host_low
       
   679 		|| ( 'www.' . $original_host_low !== $redirect_host_low
       
   680 			&& 'www.' . $redirect_host_low !== $original_host_low )
       
   681 	) {
   509 		$redirect['host'] = $original['host'];
   682 		$redirect['host'] = $original['host'];
   510 	}
   683 	}
   511 
   684 
   512 	$compare_original = array( $original['host'], $original['path'] );
   685 	$compare_original = array( $original['host'], $original['path'] );
   513 
   686 
   529 		$compare_redirect[] = $redirect['query'];
   702 		$compare_redirect[] = $redirect['query'];
   530 	}
   703 	}
   531 
   704 
   532 	if ( $compare_original !== $compare_redirect ) {
   705 	if ( $compare_original !== $compare_redirect ) {
   533 		$redirect_url = $redirect['scheme'] . '://' . $redirect['host'];
   706 		$redirect_url = $redirect['scheme'] . '://' . $redirect['host'];
       
   707 
   534 		if ( ! empty( $redirect['port'] ) ) {
   708 		if ( ! empty( $redirect['port'] ) ) {
   535 			$redirect_url .= ':' . $redirect['port'];
   709 			$redirect_url .= ':' . $redirect['port'];
   536 		}
   710 		}
       
   711 
   537 		$redirect_url .= $redirect['path'];
   712 		$redirect_url .= $redirect['path'];
       
   713 
   538 		if ( ! empty( $redirect['query'] ) ) {
   714 		if ( ! empty( $redirect['query'] ) ) {
   539 			$redirect_url .= '?' . $redirect['query'];
   715 			$redirect_url .= '?' . $redirect['query'];
   540 		}
   716 		}
   541 	}
   717 	}
   542 
   718 
   543 	if ( ! $redirect_url || $redirect_url == $requested_url ) {
   719 	if ( ! $redirect_url || $redirect_url === $requested_url ) {
   544 		return;
   720 		return;
   545 	}
   721 	}
   546 
   722 
   547 	// Hex encoded octets are case-insensitive.
   723 	// Hex encoded octets are case-insensitive.
   548 	if ( false !== strpos( $requested_url, '%' ) ) {
   724 	if ( false !== strpos( $requested_url, '%' ) ) {
   558 			 */
   734 			 */
   559 			function lowercase_octets( $matches ) {
   735 			function lowercase_octets( $matches ) {
   560 				return strtolower( $matches[0] );
   736 				return strtolower( $matches[0] );
   561 			}
   737 			}
   562 		}
   738 		}
       
   739 
   563 		$requested_url = preg_replace_callback( '|%[a-fA-F0-9][a-fA-F0-9]|', 'lowercase_octets', $requested_url );
   740 		$requested_url = preg_replace_callback( '|%[a-fA-F0-9][a-fA-F0-9]|', 'lowercase_octets', $requested_url );
   564 	}
   741 	}
   565 
   742 
   566 	/**
   743 	/**
   567 	 * Filters the canonical redirect URL.
   744 	 * Filters the canonical redirect URL.
   573 	 * @param string $redirect_url  The redirect URL.
   750 	 * @param string $redirect_url  The redirect URL.
   574 	 * @param string $requested_url The requested URL.
   751 	 * @param string $requested_url The requested URL.
   575 	 */
   752 	 */
   576 	$redirect_url = apply_filters( 'redirect_canonical', $redirect_url, $requested_url );
   753 	$redirect_url = apply_filters( 'redirect_canonical', $redirect_url, $requested_url );
   577 
   754 
   578 	// yes, again -- in case the filter aborted the request
   755 	// Yes, again -- in case the filter aborted the request.
   579 	if ( ! $redirect_url || strip_fragment_from_url( $redirect_url ) == strip_fragment_from_url( $requested_url ) ) {
   756 	if ( ! $redirect_url || strip_fragment_from_url( $redirect_url ) === strip_fragment_from_url( $requested_url ) ) {
   580 		return;
   757 		return;
   581 	}
   758 	}
   582 
   759 
   583 	if ( $do_redirect ) {
   760 	if ( $do_redirect ) {
   584 		// protect against chained redirects
   761 		// Protect against chained redirects.
   585 		if ( ! redirect_canonical( $redirect_url, false ) ) {
   762 		if ( ! redirect_canonical( $redirect_url, false ) ) {
   586 			wp_redirect( $redirect_url, 301 );
   763 			wp_redirect( $redirect_url, 301 );
   587 			exit();
   764 			exit;
   588 		} else {
   765 		} else {
   589 			// Debug
   766 			// Debug.
   590 			// die("1: $redirect_url<br />2: " . redirect_canonical( $redirect_url, false ) );
   767 			// die("1: $redirect_url<br />2: " . redirect_canonical( $redirect_url, false ) );
   591 			return;
   768 			return;
   592 		}
   769 		}
   593 	} else {
   770 	} else {
   594 		return $redirect_url;
   771 		return $redirect_url;
   601  *
   778  *
   602  * @since 3.4.0
   779  * @since 3.4.0
   603  * @access private
   780  * @access private
   604  *
   781  *
   605  * @param string $query_string
   782  * @param string $query_string
   606  * @param array $args_to_check
   783  * @param array  $args_to_check
   607  * @param string $url
   784  * @param string $url
   608  * @return string The altered query string
   785  * @return string The altered query string
   609  */
   786  */
   610 function _remove_qs_args_if_not_in_url( $query_string, array $args_to_check, $url ) {
   787 function _remove_qs_args_if_not_in_url( $query_string, array $args_to_check, $url ) {
   611 	$parsed_url = @parse_url( $url );
   788 	$parsed_url = parse_url( $url );
       
   789 
   612 	if ( ! empty( $parsed_url['query'] ) ) {
   790 	if ( ! empty( $parsed_url['query'] ) ) {
   613 		parse_str( $parsed_url['query'], $parsed_query );
   791 		parse_str( $parsed_url['query'], $parsed_query );
       
   792 
   614 		foreach ( $args_to_check as $qv ) {
   793 		foreach ( $args_to_check as $qv ) {
   615 			if ( ! isset( $parsed_query[ $qv ] ) ) {
   794 			if ( ! isset( $parsed_query[ $qv ] ) ) {
   616 				$query_string = remove_query_arg( $qv, $query_string );
   795 				$query_string = remove_query_arg( $qv, $query_string );
   617 			}
   796 			}
   618 		}
   797 		}
   619 	} else {
   798 	} else {
   620 		$query_string = remove_query_arg( $args_to_check, $query_string );
   799 		$query_string = remove_query_arg( $args_to_check, $query_string );
   621 	}
   800 	}
       
   801 
   622 	return $query_string;
   802 	return $query_string;
   623 }
   803 }
   624 
   804 
   625 /**
   805 /**
   626  * Strips the #fragment from a URL, if one is present.
   806  * Strips the #fragment from a URL, if one is present.
   629  *
   809  *
   630  * @param string $url The URL to strip.
   810  * @param string $url The URL to strip.
   631  * @return string The altered URL.
   811  * @return string The altered URL.
   632  */
   812  */
   633 function strip_fragment_from_url( $url ) {
   813 function strip_fragment_from_url( $url ) {
   634 	$parsed_url = @parse_url( $url );
   814 	$parsed_url = parse_url( $url );
       
   815 
   635 	if ( ! empty( $parsed_url['host'] ) ) {
   816 	if ( ! empty( $parsed_url['host'] ) ) {
   636 		// This mirrors code in redirect_canonical(). It does not handle every case.
   817 		// This mirrors code in redirect_canonical(). It does not handle every case.
   637 		$url = $parsed_url['scheme'] . '://' . $parsed_url['host'];
   818 		$url = $parsed_url['scheme'] . '://' . $parsed_url['host'];
   638 		if ( ! empty( $parsed_url['port'] ) ) {
   819 		if ( ! empty( $parsed_url['port'] ) ) {
   639 			$url .= ':' . $parsed_url['port'];
   820 			$url .= ':' . $parsed_url['port'];
   650 
   831 
   651 	return $url;
   832 	return $url;
   652 }
   833 }
   653 
   834 
   654 /**
   835 /**
   655  * Attempts to guess the correct URL based on query vars
   836  * Attempts to guess the correct URL for a 404 request based on query vars.
   656  *
   837  *
   657  * @since 2.3.0
   838  * @since 2.3.0
   658  *
   839  *
   659  * @global wpdb $wpdb WordPress database abstraction object.
   840  * @global wpdb $wpdb WordPress database abstraction object.
   660  *
   841  *
   661  * @return false|string The correct URL if one is found. False on failure.
   842  * @return string|false The correct URL if one is found. False on failure.
   662  */
   843  */
   663 function redirect_guess_404_permalink() {
   844 function redirect_guess_404_permalink() {
   664 	global $wpdb;
   845 	global $wpdb;
   665 
   846 
       
   847 	/**
       
   848 	 * Filters whether to attempt to guess a redirect URL for a 404 request.
       
   849 	 *
       
   850 	 * Returning a false value from the filter will disable the URL guessing
       
   851 	 * and return early without performing a redirect.
       
   852 	 *
       
   853 	 * @since 5.5.0
       
   854 	 *
       
   855 	 * @param bool $do_redirect_guess Whether to attempt to guess a redirect URL
       
   856 	 *                                for a 404 request. Default true.
       
   857 	 */
       
   858 	if ( false === apply_filters( 'do_redirect_guess_404_permalink', true ) ) {
       
   859 		return false;
       
   860 	}
       
   861 
       
   862 	/**
       
   863 	 * Short-circuits the redirect URL guessing for 404 requests.
       
   864 	 *
       
   865 	 * Returning a non-null value from the filter will effectively short-circuit
       
   866 	 * the URL guessing, returning the passed value instead.
       
   867 	 *
       
   868 	 * @since 5.5.0
       
   869 	 *
       
   870 	 * @param null|string|false $pre Whether to short-circuit guessing the redirect for a 404.
       
   871 	 *                               Default null to continue with the URL guessing.
       
   872 	 */
       
   873 	$pre = apply_filters( 'pre_redirect_guess_404_permalink', null );
       
   874 	if ( null !== $pre ) {
       
   875 		return $pre;
       
   876 	}
       
   877 
   666 	if ( get_query_var( 'name' ) ) {
   878 	if ( get_query_var( 'name' ) ) {
   667 		$where = $wpdb->prepare( 'post_name LIKE %s', $wpdb->esc_like( get_query_var( 'name' ) ) . '%' );
   879 		/**
   668 
   880 		 * Filters whether to perform a strict guess for a 404 redirect.
   669 		// if any of post_type, year, monthnum, or day are set, use them to refine the query
   881 		 *
       
   882 		 * Returning a truthy value from the filter will redirect only exact post_name matches.
       
   883 		 *
       
   884 		 * @since 5.5.0
       
   885 		 *
       
   886 		 * @param bool $strict_guess Whether to perform a strict guess. Default false (loose guess).
       
   887 		 */
       
   888 		$strict_guess = apply_filters( 'strict_redirect_guess_404_permalink', false );
       
   889 
       
   890 		if ( $strict_guess ) {
       
   891 			$where = $wpdb->prepare( 'post_name = %s', get_query_var( 'name' ) );
       
   892 		} else {
       
   893 			$where = $wpdb->prepare( 'post_name LIKE %s', $wpdb->esc_like( get_query_var( 'name' ) ) . '%' );
       
   894 		}
       
   895 
       
   896 		// If any of post_type, year, monthnum, or day are set, use them to refine the query.
   670 		if ( get_query_var( 'post_type' ) ) {
   897 		if ( get_query_var( 'post_type' ) ) {
   671 			$where .= $wpdb->prepare( ' AND post_type = %s', get_query_var( 'post_type' ) );
   898 			$where .= $wpdb->prepare( ' AND post_type = %s', get_query_var( 'post_type' ) );
   672 		} else {
   899 		} else {
   673 			$where .= " AND post_type IN ('" . implode( "', '", get_post_types( array( 'public' => true ) ) ) . "')";
   900 			$where .= " AND post_type IN ('" . implode( "', '", get_post_types( array( 'public' => true ) ) ) . "')";
   674 		}
   901 		}
   681 		}
   908 		}
   682 		if ( get_query_var( 'day' ) ) {
   909 		if ( get_query_var( 'day' ) ) {
   683 			$where .= $wpdb->prepare( ' AND DAYOFMONTH(post_date) = %d', get_query_var( 'day' ) );
   910 			$where .= $wpdb->prepare( ' AND DAYOFMONTH(post_date) = %d', get_query_var( 'day' ) );
   684 		}
   911 		}
   685 
   912 
       
   913 		// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
   686 		$post_id = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'" );
   914 		$post_id = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'" );
       
   915 
   687 		if ( ! $post_id ) {
   916 		if ( ! $post_id ) {
   688 			return false;
   917 			return false;
   689 		}
   918 		}
       
   919 
   690 		if ( get_query_var( 'feed' ) ) {
   920 		if ( get_query_var( 'feed' ) ) {
   691 			return get_post_comments_feed_link( $post_id, get_query_var( 'feed' ) );
   921 			return get_post_comments_feed_link( $post_id, get_query_var( 'feed' ) );
   692 		} elseif ( get_query_var( 'page' ) && 1 < get_query_var( 'page' ) ) {
   922 		} elseif ( get_query_var( 'page' ) > 1 ) {
   693 			return trailingslashit( get_permalink( $post_id ) ) . user_trailingslashit( get_query_var( 'page' ), 'single_paged' );
   923 			return trailingslashit( get_permalink( $post_id ) ) . user_trailingslashit( get_query_var( 'page' ), 'single_paged' );
   694 		} else {
   924 		} else {
   695 			return get_permalink( $post_id );
   925 			return get_permalink( $post_id );
   696 		}
   926 		}
   697 	}
   927 	}
   705  * If a user visits example.com/admin, they'll be redirected to /wp-admin.
   935  * If a user visits example.com/admin, they'll be redirected to /wp-admin.
   706  * Visiting /login redirects to /wp-login.php, and so on.
   936  * Visiting /login redirects to /wp-login.php, and so on.
   707  *
   937  *
   708  * @since 3.4.0
   938  * @since 3.4.0
   709  *
   939  *
   710  * @global WP_Rewrite $wp_rewrite
   940  * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
   711  */
   941  */
   712 function wp_redirect_admin_locations() {
   942 function wp_redirect_admin_locations() {
   713 	global $wp_rewrite;
   943 	global $wp_rewrite;
       
   944 
   714 	if ( ! ( is_404() && $wp_rewrite->using_permalinks() ) ) {
   945 	if ( ! ( is_404() && $wp_rewrite->using_permalinks() ) ) {
   715 		return;
   946 		return;
   716 	}
   947 	}
   717 
   948 
   718 	$admins = array(
   949 	$admins = array(
   720 		home_url( 'dashboard', 'relative' ),
   951 		home_url( 'dashboard', 'relative' ),
   721 		home_url( 'admin', 'relative' ),
   952 		home_url( 'admin', 'relative' ),
   722 		site_url( 'dashboard', 'relative' ),
   953 		site_url( 'dashboard', 'relative' ),
   723 		site_url( 'admin', 'relative' ),
   954 		site_url( 'admin', 'relative' ),
   724 	);
   955 	);
   725 	if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $admins ) ) {
   956 
       
   957 	if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $admins, true ) ) {
   726 		wp_redirect( admin_url() );
   958 		wp_redirect( admin_url() );
   727 		exit;
   959 		exit;
   728 	}
   960 	}
   729 
   961 
   730 	$logins = array(
   962 	$logins = array(
   731 		home_url( 'wp-login.php', 'relative' ),
   963 		home_url( 'wp-login.php', 'relative' ),
   732 		home_url( 'login', 'relative' ),
   964 		home_url( 'login', 'relative' ),
   733 		site_url( 'login', 'relative' ),
   965 		site_url( 'login', 'relative' ),
   734 	);
   966 	);
   735 	if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $logins ) ) {
   967 
       
   968 	if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $logins, true ) ) {
   736 		wp_redirect( wp_login_url() );
   969 		wp_redirect( wp_login_url() );
   737 		exit;
   970 		exit;
   738 	}
   971 	}
   739 }
   972 }