wp/wp-includes/canonical.php
author ymh <ymh.work@gmail.com>
Mon, 14 Oct 2019 18:28:13 +0200
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
permissions -rw-r--r--
upgrade wordpress to 5.2.3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * Canonical API to handle WordPress Redirecting
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * Based on "Permalink Redirect" from Scott Yang and "Enforce www. Preference"
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 * by Mark Jaquith
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
 * Redirects incoming links to the proper URL based on the site url.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
 * Search engines consider www.somedomain.com and somedomain.com to be two
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
 * different URLs when they both go to the same location. This SEO enhancement
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
 * prevents penalty for duplicate content by redirecting all incoming links to
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
 * one or the other.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
 *
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    20
 * Prevents redirection for feeds, trackbacks, searches, and
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
 * admin URLs. Does not redirect on non-pretty-permalink-supporting IIS 7+,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
 * page/post previews, WP admin, Trackbacks, robots.txt, searches, or on POST
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
 * requests.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
 * Will also attempt to find the correct link when a user enters a URL that does
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
 * not exist based on exact WordPress query. Will instead try to parse the URL
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
 * or query in an attempt to figure the correct page to go to.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
 * @since 2.3.0
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    30
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    31
 * @global WP_Rewrite $wp_rewrite
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    32
 * @global bool $is_IIS
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    33
 * @global WP_Query $wp_query
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    34
 * @global wpdb $wpdb WordPress database abstraction object.
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    35
 * @global WP $wp Current WordPress environment instance.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
 * @param string $requested_url Optional. The URL that was requested, used to
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    38
 *      figure if redirect is needed.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
 * @param bool $do_redirect Optional. Redirect to the new URL.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    40
 * @return string|void The string of the URL, if redirect needed.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
function redirect_canonical( $requested_url = null, $do_redirect = true ) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    43
	global $wp_rewrite, $is_IIS, $wp_query, $wpdb, $wp;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    45
	if ( isset( $_SERVER['REQUEST_METHOD'] ) && ! in_array( strtoupper( $_SERVER['REQUEST_METHOD'] ), array( 'GET', 'HEAD' ) ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
		return;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    47
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    48
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    49
	// If we're not in wp-admin and the post has been published and preview nonce
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    50
	// is non-existent or invalid then no need for preview in query
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    51
	if ( is_preview() && get_query_var( 'p' ) && 'publish' == get_post_status( get_query_var( 'p' ) ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    52
		if ( ! isset( $_GET['preview_id'] )
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    53
			|| ! isset( $_GET['preview_nonce'] )
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    54
			|| ! wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . (int) $_GET['preview_id'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    55
			$wp_query->is_preview = false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    56
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    57
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    58
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    59
	if ( is_trackback() || is_search() || is_admin() || is_preview() || is_robots() || ( $is_IIS && ! iis7_supports_permalinks() ) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    60
		return;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    61
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    63
	if ( ! $requested_url && isset( $_SERVER['HTTP_HOST'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
		// build the URL in the address bar
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
		$requested_url  = is_ssl() ? 'https://' : 'http://';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
		$requested_url .= $_SERVER['HTTP_HOST'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
		$requested_url .= $_SERVER['REQUEST_URI'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    70
	$original = @parse_url( $requested_url );
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    71
	if ( false === $original ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
		return;
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    73
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    75
	$redirect     = $original;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
	$redirect_url = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
	// Notice fixing
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    79
	if ( ! isset( $redirect['path'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
		$redirect['path'] = '';
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    81
	}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    82
	if ( ! isset( $redirect['query'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
		$redirect['query'] = '';
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    84
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    86
	// If the original URL ended with non-breaking spaces, they were almost
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    87
	// certainly inserted by accident. Let's remove them, so the reader doesn't
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    88
	// see a 404 error with no obvious cause.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    89
	$redirect['path'] = preg_replace( '|(%C2%A0)+$|i', '', $redirect['path'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    90
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    91
	// It's not a preview, so remove it from URL
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    92
	if ( get_query_var( 'preview' ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    93
		$redirect['query'] = remove_query_arg( 'preview', $redirect['query'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    94
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    95
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
	if ( is_feed() && ( $id = get_query_var( 'p' ) ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
		if ( $redirect_url = get_post_comments_feed_link( $id, get_query_var( 'feed' ) ) ) {
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    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 );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    99
			$redirect['path']  = parse_url( $redirect_url, PHP_URL_PATH );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   103
	if ( is_singular() && 1 > $wp_query->post_count && ( $id = get_query_var( 'p' ) ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   105
		$vars = $wpdb->get_results( $wpdb->prepare( "SELECT post_type, post_parent FROM $wpdb->posts WHERE ID = %d", $id ) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   107
		if ( isset( $vars[0] ) && $vars = $vars[0] ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   108
			if ( 'revision' == $vars->post_type && $vars->post_parent > 0 ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
				$id = $vars->post_parent;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   110
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   112
			if ( $redirect_url = get_permalink( $id ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
				$redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   114
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
	// These tests give us a WP-generated permalink
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
	if ( is_404() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
		// Redirect ?page_id, ?p=, ?attachment_id= to their respective url's
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   122
		$id = max( get_query_var( 'p' ), get_query_var( 'page_id' ), get_query_var( 'attachment_id' ) );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   123
		if ( $id && $redirect_post = get_post( $id ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   124
			$post_type_obj = get_post_type_object( $redirect_post->post_type );
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   125
			if ( $post_type_obj->public && 'auto-draft' != $redirect_post->post_status ) {
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   126
				$redirect_url      = get_permalink( $redirect_post );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
				$redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
		if ( get_query_var( 'day' ) && get_query_var( 'monthnum' ) && get_query_var( 'year' ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
			$year  = get_query_var( 'year' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
			$month = get_query_var( 'monthnum' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
			$day   = get_query_var( 'day' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
			$date  = sprintf( '%04d-%02d-%02d', $year, $month, $day );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
			if ( ! wp_checkdate( $month, $day, $year, $date ) ) {
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   137
				$redirect_url      = get_month_link( $year, $month );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
				$redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'year', 'monthnum', 'day' ), $redirect_url );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
		} elseif ( get_query_var( 'monthnum' ) && get_query_var( 'year' ) && 12 < get_query_var( 'monthnum' ) ) {
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   141
			$redirect_url      = get_year_link( get_query_var( 'year' ) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
			$redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'year', 'monthnum' ), $redirect_url );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
		if ( ! $redirect_url ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
			if ( $redirect_url = redirect_guess_404_permalink() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   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 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   151
		if ( get_query_var( 'page' ) && $wp_query->post &&
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   152
			false !== strpos( $wp_query->post->post_content, '<!--nextpage-->' ) ) {
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   153
			$redirect['path']  = rtrim( $redirect['path'], (int) get_query_var( 'page' ) . '/' );
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   154
			$redirect['query'] = remove_query_arg( 'page', $redirect['query'] );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   155
			$redirect_url      = get_permalink( $wp_query->post->ID );
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   156
		}
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   157
	} elseif ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
		// rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   159
		if ( is_attachment() &&
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   160
			! array_diff( array_keys( $wp->query_vars ), array( 'attachment', 'attachment_id' ) ) &&
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   161
			! $redirect_url ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   162
			if ( ! empty( $_GET['attachment_id'] ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   163
				$redirect_url = get_attachment_link( get_query_var( 'attachment_id' ) );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   164
				if ( $redirect_url ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   165
					$redirect['query'] = remove_query_arg( 'attachment_id', $redirect['query'] );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   166
				}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   167
			} else {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   168
				$redirect_url = get_attachment_link();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   169
			}
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   170
		} elseif ( is_single() && ! empty( $_GET['p'] ) && ! $redirect_url ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   171
			if ( $redirect_url = get_permalink( get_query_var( 'p' ) ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   172
				$redirect['query'] = remove_query_arg( array( 'p', 'post_type' ), $redirect['query'] );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   173
			}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   174
		} elseif ( is_single() && ! empty( $_GET['name'] ) && ! $redirect_url ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   175
			if ( $redirect_url = get_permalink( $wp_query->get_queried_object_id() ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   176
				$redirect['query'] = remove_query_arg( 'name', $redirect['query'] );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   177
			}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   178
		} elseif ( is_page() && ! empty( $_GET['page_id'] ) && ! $redirect_url ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   179
			if ( $redirect_url = get_permalink( get_query_var( 'page_id' ) ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   180
				$redirect['query'] = remove_query_arg( 'page_id', $redirect['query'] );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   181
			}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   182
		} elseif ( is_page() && ! is_feed() && 'page' == get_option( 'show_on_front' ) && get_queried_object_id() == get_option( 'page_on_front' ) && ! $redirect_url ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   183
			$redirect_url = home_url( '/' );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   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 ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   185
			if ( $redirect_url = get_permalink( get_option( 'page_for_posts' ) ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   186
				$redirect['query'] = remove_query_arg( 'page_id', $redirect['query'] );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   187
			}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   188
		} elseif ( ! empty( $_GET['m'] ) && ( is_year() || is_month() || is_day() ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   189
			$m = get_query_var( 'm' );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   190
			switch ( strlen( $m ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
				case 4: // Yearly
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   192
					$redirect_url = get_year_link( $m );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
					break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
				case 6: // Monthly
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   195
					$redirect_url = get_month_link( substr( $m, 0, 4 ), substr( $m, 4, 2 ) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
					break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
				case 8: // Daily
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   198
					$redirect_url = get_day_link( substr( $m, 0, 4 ), substr( $m, 4, 2 ), substr( $m, 6, 2 ) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
					break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
			}
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   201
			if ( $redirect_url ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   202
				$redirect['query'] = remove_query_arg( 'm', $redirect['query'] );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   203
			}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   204
			// now moving on to non ?m=X year/month/day links
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   205
		} elseif ( is_day() && get_query_var( 'year' ) && get_query_var( 'monthnum' ) && ! empty( $_GET['day'] ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   206
			if ( $redirect_url = get_day_link( get_query_var( 'year' ), get_query_var( 'monthnum' ), get_query_var( 'day' ) ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   207
				$redirect['query'] = remove_query_arg( array( 'year', 'monthnum', 'day' ), $redirect['query'] );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   208
			}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   209
		} elseif ( is_month() && get_query_var( 'year' ) && ! empty( $_GET['monthnum'] ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   210
			if ( $redirect_url = get_month_link( get_query_var( 'year' ), get_query_var( 'monthnum' ) ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   211
				$redirect['query'] = remove_query_arg( array( 'year', 'monthnum' ), $redirect['query'] );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   212
			}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   213
		} elseif ( is_year() && ! empty( $_GET['year'] ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   214
			if ( $redirect_url = get_year_link( get_query_var( 'year' ) ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   215
				$redirect['query'] = remove_query_arg( 'year', $redirect['query'] );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   216
			}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   217
		} elseif ( is_author() && ! empty( $_GET['author'] ) && preg_match( '|^[0-9]+$|', $_GET['author'] ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   218
			$author = get_userdata( get_query_var( 'author' ) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   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 ) ) ) {
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   220
				if ( $redirect_url = get_author_posts_url( $author->ID, $author->user_nicename ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   221
					$redirect['query'] = remove_query_arg( 'author', $redirect['query'] );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   222
				}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
		} elseif ( is_category() || is_tag() || is_tax() ) { // Terms (Tags/categories)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
			$term_count = 0;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   227
			foreach ( $wp_query->tax_query->queried_terms as $tax_query ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
				$term_count += count( $tax_query['terms'] );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   229
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
			$obj = $wp_query->get_queried_object();
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   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 ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   233
				if ( ! empty( $redirect['query'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
					// Strip taxonomy query vars off the url.
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   235
					$qv_remove = array( 'term', 'taxonomy' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
					if ( is_category() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
						$qv_remove[] = 'category_name';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
						$qv_remove[] = 'cat';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
					} elseif ( is_tag() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
						$qv_remove[] = 'tag';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
						$qv_remove[] = 'tag_id';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
					} else { // Custom taxonomies will have a custom query var, remove those too:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
						$tax_obj = get_taxonomy( $obj->taxonomy );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   244
						if ( false !== $tax_obj->query_var ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
							$qv_remove[] = $tax_obj->query_var;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   246
						}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
					}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   249
					$rewrite_vars = array_diff( array_keys( $wp_query->query ), array_keys( $_GET ) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   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
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   252
						$redirect['query'] = remove_query_arg( $qv_remove, $redirect['query'] ); //Remove all of the per-tax qv's
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
						// Create the destination url for this taxonomy
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   255
						$tax_url = parse_url( $tax_url );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   256
						if ( ! empty( $tax_url['query'] ) ) { // Taxonomy accessible via ?taxonomy=..&term=.. or any custom qv..
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   257
							parse_str( $tax_url['query'], $query_vars );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   258
							$redirect['query'] = add_query_arg( $query_vars, $redirect['query'] );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
						} else { // Taxonomy is accessible via a "pretty-URL"
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
							$redirect['path'] = $tax_url['path'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
						}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
					} else { // Some query vars are set via $_GET. Unset those from $_GET that exist via the rewrite
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
						foreach ( $qv_remove as $_qv ) {
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   264
							if ( isset( $rewrite_vars[ $_qv ] ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   265
								$redirect['query'] = remove_query_arg( $_qv, $redirect['query'] );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   266
							}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
						}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
					}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
			}
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   271
		} elseif ( is_single() && strpos( $wp_rewrite->permalink_structure, '%category%' ) !== false && $cat = get_query_var( 'category_name' ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
			$category = get_category_by_path( $cat );
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   273
			if ( ( ! $category || is_wp_error( $category ) ) || ! has_term( $category->term_id, 'category', $wp_query->get_queried_object_id() ) ) {
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   274
				$redirect_url = get_permalink( $wp_query->get_queried_object_id() );
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   275
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   278
			// Post Paging
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   279
		if ( is_singular() && get_query_var( 'page' ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   280
			if ( ! $redirect_url ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
				$redirect_url = get_permalink( get_queried_object_id() );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   282
			}
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   283
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   284
			$page = get_query_var( 'page' );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   285
			if ( $page > 1 ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   286
				if ( is_front_page() ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   287
					$redirect_url = trailingslashit( $redirect_url ) . user_trailingslashit( "$wp_rewrite->pagination_base/$page", 'paged' );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   288
				} else {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   289
					$redirect_url = trailingslashit( $redirect_url ) . user_trailingslashit( $page, 'single_paged' );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   290
				}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   291
			}
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   292
				$redirect['query'] = remove_query_arg( 'page', $redirect['query'] );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   295
			// paging and feeds
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   296
		if ( get_query_var( 'paged' ) || is_feed() || get_query_var( 'cpage' ) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   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'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   298
				// Strip off paging and feed
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   299
				$redirect['path'] = preg_replace( "#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", '/', $redirect['path'] ); // strip off any existing paging
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   300
				$redirect['path'] = preg_replace( '#/(comments/?)?(feed|rss2?|rdf|atom)(/+|$)#', '/', $redirect['path'] ); // strip off feed endings
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   301
				$redirect['path'] = preg_replace( "#/{$wp_rewrite->comments_pagination_base}-[0-9]+?(/+)?$#", '/', $redirect['path'] ); // strip off any existing comment paging
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
			$addl_path = '';
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   305
			if ( is_feed() && in_array( get_query_var( 'feed' ), $wp_rewrite->feeds ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   306
				$addl_path = ! empty( $addl_path ) ? trailingslashit( $addl_path ) : '';
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   307
				if ( ! is_singular() && get_query_var( 'withcomments' ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   308
					$addl_path .= 'comments/';
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   309
				}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   310
				if ( ( 'rss' == get_default_feed() && 'feed' == get_query_var( 'feed' ) ) || 'rss' == get_query_var( 'feed' ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   311
					$addl_path .= user_trailingslashit( 'feed/' . ( ( get_default_feed() == 'rss2' ) ? '' : 'rss2' ), 'feed' );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   312
				} else {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   313
					$addl_path .= user_trailingslashit( 'feed/' . ( ( get_default_feed() == get_query_var( 'feed' ) || 'feed' == get_query_var( 'feed' ) ) ? '' : get_query_var( 'feed' ) ), 'feed' );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   314
				}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   315
				$redirect['query'] = remove_query_arg( 'feed', $redirect['query'] );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   316
			} elseif ( is_feed() && 'old' == get_query_var( 'feed' ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   317
				$old_feed_files = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
					'wp-atom.php'         => 'atom',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
					'wp-commentsrss2.php' => 'comments_rss2',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   320
					'wp-feed.php'         => get_default_feed(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
					'wp-rdf.php'          => 'rdf',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
					'wp-rss.php'          => 'rss2',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
					'wp-rss2.php'         => 'rss2',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
				);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
				if ( isset( $old_feed_files[ basename( $redirect['path'] ) ] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
					$redirect_url = get_feed_link( $old_feed_files[ basename( $redirect['path'] ) ] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
					wp_redirect( $redirect_url, 301 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   328
					die();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   332
			if ( get_query_var( 'paged' ) > 0 ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   333
				$paged             = get_query_var( 'paged' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   334
				$redirect['query'] = remove_query_arg( 'paged', $redirect['query'] );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   335
				if ( ! is_feed() ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   336
					if ( $paged > 1 && ! is_single() ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   337
						$addl_path = ( ! empty( $addl_path ) ? trailingslashit( $addl_path ) : '' ) . user_trailingslashit( "$wp_rewrite->pagination_base/$paged", 'paged' );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   338
					} elseif ( ! is_single() ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   339
						$addl_path = ! empty( $addl_path ) ? trailingslashit( $addl_path ) : '';
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
					}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   341
				} elseif ( $paged > 1 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
					$redirect['query'] = add_query_arg( 'paged', $paged, $redirect['query'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   344
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   345
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   346
			if ( get_option( 'page_comments' ) && (
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   347
			( 'newest' == get_option( 'default_comments_page' ) && get_query_var( 'cpage' ) > 0 ) ||
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   348
			( 'newest' != get_option( 'default_comments_page' ) && get_query_var( 'cpage' ) > 1 )
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   349
			) ) {
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   350
				$addl_path         = ( ! empty( $addl_path ) ? trailingslashit( $addl_path ) : '' ) . user_trailingslashit( $wp_rewrite->comments_pagination_base . '-' . get_query_var( 'cpage' ), 'commentpaged' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   351
				$redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   354
			$redirect['path'] = user_trailingslashit( preg_replace( '|/' . preg_quote( $wp_rewrite->index, '|' ) . '/?$|', '/', $redirect['path'] ) ); // strip off trailing /index.php/
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   355
			if ( ! empty( $addl_path ) && $wp_rewrite->using_index_permalinks() && strpos( $redirect['path'], '/' . $wp_rewrite->index . '/' ) === false ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   356
				$redirect['path'] = trailingslashit( $redirect['path'] ) . $wp_rewrite->index . '/';
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   357
			}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   358
			if ( ! empty( $addl_path ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   359
				$redirect['path'] = trailingslashit( $redirect['path'] ) . $addl_path;
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   360
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   361
			$redirect_url = $redirect['scheme'] . '://' . $redirect['host'] . $redirect['path'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   362
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   363
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   364
		if ( 'wp-register.php' == basename( $redirect['path'] ) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   365
			if ( is_multisite() ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   366
				/** This filter is documented in wp-login.php */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   367
				$redirect_url = apply_filters( 'wp_signup_location', network_site_url( 'wp-signup.php' ) );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   368
			} else {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   369
				$redirect_url = wp_registration_url();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   370
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   371
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
			wp_redirect( $redirect_url, 301 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
			die();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   374
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   375
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   376
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   377
	// tack on any additional query vars
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   378
	$redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   379
	if ( $redirect_url && ! empty( $redirect['query'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   380
		parse_str( $redirect['query'], $_parsed_query );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   381
		$redirect = @parse_url( $redirect_url );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   382
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   383
		if ( ! empty( $_parsed_query['name'] ) && ! empty( $redirect['query'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
			parse_str( $redirect['query'], $_parsed_redirect_query );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   385
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   386
			if ( empty( $_parsed_redirect_query['name'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   387
				unset( $_parsed_query['name'] );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   388
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   389
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   390
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   391
		$_parsed_query = array_combine(
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   392
			rawurlencode_deep( array_keys( $_parsed_query ) ),
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   393
			rawurlencode_deep( array_values( $_parsed_query ) )
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   394
		);
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   395
		$redirect_url  = add_query_arg( $_parsed_query, $redirect_url );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   396
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   397
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   398
	if ( $redirect_url ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   399
		$redirect = @parse_url( $redirect_url );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   400
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   401
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   402
	// www.example.com vs example.com
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   403
	$user_home = @parse_url( home_url() );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   404
	if ( ! empty( $user_home['host'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   405
		$redirect['host'] = $user_home['host'];
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   406
	}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   407
	if ( empty( $user_home['path'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   408
		$user_home['path'] = '/';
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   409
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   410
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   411
	// Handle ports
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   412
	if ( ! empty( $user_home['port'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   413
		$redirect['port'] = $user_home['port'];
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   414
	} else {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   415
		unset( $redirect['port'] );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   416
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   417
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   418
	// trailing /index.php
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   419
	$redirect['path'] = preg_replace( '|/' . preg_quote( $wp_rewrite->index, '|' ) . '/*?$|', '/', $redirect['path'] );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   420
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   421
	$punctuation_pattern = implode(
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   422
		'|',
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   423
		array_map(
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   424
			'preg_quote',
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   425
			array(
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   426
				' ',
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   427
				'%20',  // space
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   428
				'!',
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   429
				'%21',  // exclamation mark
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   430
				'"',
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   431
				'%22',  // double quote
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   432
				"'",
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   433
				'%27',  // single quote
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   434
				'(',
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   435
				'%28',  // opening bracket
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   436
				')',
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   437
				'%29',  // closing bracket
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   438
				',',
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   439
				'%2C',  // comma
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   440
				'.',
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   441
				'%2E',  // period
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   442
				';',
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   443
				'%3B',  // semicolon
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   444
				'{',
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   445
				'%7B',  // opening curly bracket
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   446
				'}',
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   447
				'%7D',  // closing curly bracket
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   448
				'%E2%80%9C', // opening curly quote
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   449
				'%E2%80%9D', // closing curly quote
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   450
			)
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   451
		)
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   452
	);
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   453
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   454
	// Remove trailing spaces and end punctuation from the path.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   455
	$redirect['path'] = preg_replace( "#($punctuation_pattern)+$#", '', $redirect['path'] );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   456
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   457
	if ( ! empty( $redirect['query'] ) ) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   458
		// Remove trailing spaces and end punctuation from certain terminating query string args.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   459
		$redirect['query'] = preg_replace( "#((p|page_id|cat|tag)=[^&]*?)($punctuation_pattern)+$#", '$1', $redirect['query'] );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   460
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   461
		// Clean up empty query strings
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   462
		$redirect['query'] = trim( preg_replace( '#(^|&)(p|page_id|cat|tag)=?(&|$)#', '&', $redirect['query'] ), '&' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   463
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   464
		// Redirect obsolete feeds
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   465
		$redirect['query'] = preg_replace( '#(^|&)feed=rss(&|$)#', '$1feed=rss2$2', $redirect['query'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   466
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   467
		// Remove redundant leading ampersands
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   468
		$redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   469
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   470
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   471
	// strip /index.php/ when we're not using PATHINFO permalinks
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   472
	if ( ! $wp_rewrite->using_index_permalinks() ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   473
		$redirect['path'] = str_replace( '/' . $wp_rewrite->index . '/', '/', $redirect['path'] );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   474
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   475
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   476
	// trailing slashes
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   477
	if ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() && ! is_404() && ( ! is_front_page() || ( is_front_page() && ( get_query_var( 'paged' ) > 1 ) ) ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   478
		$user_ts_type = '';
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   479
		if ( get_query_var( 'paged' ) > 0 ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   480
			$user_ts_type = 'paged';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   481
		} else {
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   482
			foreach ( array( 'single', 'category', 'page', 'day', 'month', 'year', 'home' ) as $type ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   483
				$func = 'is_' . $type;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   484
				if ( call_user_func( $func ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   485
					$user_ts_type = $type;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   486
					break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   487
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   488
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   489
		}
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   490
		$redirect['path'] = user_trailingslashit( $redirect['path'], $user_ts_type );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   491
	} elseif ( is_front_page() ) {
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   492
		$redirect['path'] = trailingslashit( $redirect['path'] );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   493
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   494
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   495
	// Strip multiple slashes out of the URL
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   496
	if ( strpos( $redirect['path'], '//' ) > -1 ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   497
		$redirect['path'] = preg_replace( '|/+|', '/', $redirect['path'] );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   498
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   499
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   500
	// Always trailing slash the Front Page URL
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   501
	if ( trailingslashit( $redirect['path'] ) == trailingslashit( $user_home['path'] ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   502
		$redirect['path'] = trailingslashit( $redirect['path'] );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   503
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   504
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   505
	// Ignore differences in host capitalization, as this can lead to infinite redirects
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   506
	// Only redirect no-www <=> yes-www
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   507
	if ( strtolower( $original['host'] ) == strtolower( $redirect['host'] ) ||
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   508
		( strtolower( $original['host'] ) != 'www.' . strtolower( $redirect['host'] ) && 'www.' . strtolower( $original['host'] ) != strtolower( $redirect['host'] ) ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   509
		$redirect['host'] = $original['host'];
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   510
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   511
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   512
	$compare_original = array( $original['host'], $original['path'] );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   513
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   514
	if ( ! empty( $original['port'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   515
		$compare_original[] = $original['port'];
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   516
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   517
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   518
	if ( ! empty( $original['query'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   519
		$compare_original[] = $original['query'];
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   520
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   521
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   522
	$compare_redirect = array( $redirect['host'], $redirect['path'] );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   523
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   524
	if ( ! empty( $redirect['port'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   525
		$compare_redirect[] = $redirect['port'];
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   526
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   527
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   528
	if ( ! empty( $redirect['query'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   529
		$compare_redirect[] = $redirect['query'];
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   530
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   531
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   532
	if ( $compare_original !== $compare_redirect ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   533
		$redirect_url = $redirect['scheme'] . '://' . $redirect['host'];
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   534
		if ( ! empty( $redirect['port'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   535
			$redirect_url .= ':' . $redirect['port'];
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   536
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   537
		$redirect_url .= $redirect['path'];
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   538
		if ( ! empty( $redirect['query'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   539
			$redirect_url .= '?' . $redirect['query'];
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   540
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   541
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   542
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   543
	if ( ! $redirect_url || $redirect_url == $requested_url ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   544
		return;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   545
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   546
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   547
	// Hex encoded octets are case-insensitive.
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   548
	if ( false !== strpos( $requested_url, '%' ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   549
		if ( ! function_exists( 'lowercase_octets' ) ) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   550
			/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   551
			 * Converts the first hex-encoded octet match to lowercase.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   552
			 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   553
			 * @since 3.1.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   554
			 * @ignore
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   555
			 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   556
			 * @param array $matches Hex-encoded octet matches for the requested URL.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   557
			 * @return string Lowercased version of the first match.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   558
			 */
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   559
			function lowercase_octets( $matches ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   560
				return strtolower( $matches[0] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   561
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   562
		}
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   563
		$requested_url = preg_replace_callback( '|%[a-fA-F0-9][a-fA-F0-9]|', 'lowercase_octets', $requested_url );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   564
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   565
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   566
	/**
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   567
	 * Filters the canonical redirect URL.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   568
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   569
	 * Returning false to this filter will cancel the redirect.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   570
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   571
	 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   572
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   573
	 * @param string $redirect_url  The redirect URL.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   574
	 * @param string $requested_url The requested URL.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   575
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   576
	$redirect_url = apply_filters( 'redirect_canonical', $redirect_url, $requested_url );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   577
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   578
	// yes, again -- in case the filter aborted the request
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   579
	if ( ! $redirect_url || strip_fragment_from_url( $redirect_url ) == strip_fragment_from_url( $requested_url ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   580
		return;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   581
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   582
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   583
	if ( $do_redirect ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   584
		// protect against chained redirects
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   585
		if ( ! redirect_canonical( $redirect_url, false ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   586
			wp_redirect( $redirect_url, 301 );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   587
			exit();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   588
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   589
			// Debug
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   590
			// die("1: $redirect_url<br />2: " . redirect_canonical( $redirect_url, false ) );
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   591
			return;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   592
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   593
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   594
		return $redirect_url;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   595
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   596
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   597
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   598
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   599
 * Removes arguments from a query string if they are not present in a URL
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   600
 * DO NOT use this in plugin code.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   601
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   602
 * @since 3.4.0
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   603
 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   604
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   605
 * @param string $query_string
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   606
 * @param array $args_to_check
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   607
 * @param string $url
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   608
 * @return string The altered query string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   609
 */
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   610
function _remove_qs_args_if_not_in_url( $query_string, array $args_to_check, $url ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   611
	$parsed_url = @parse_url( $url );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   612
	if ( ! empty( $parsed_url['query'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   613
		parse_str( $parsed_url['query'], $parsed_query );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   614
		foreach ( $args_to_check as $qv ) {
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   615
			if ( ! isset( $parsed_query[ $qv ] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   616
				$query_string = remove_query_arg( $qv, $query_string );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   617
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   618
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   619
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   620
		$query_string = remove_query_arg( $args_to_check, $query_string );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   621
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   622
	return $query_string;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   623
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   624
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   625
/**
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   626
 * Strips the #fragment from a URL, if one is present.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   627
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   628
 * @since 4.4.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   629
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   630
 * @param string $url The URL to strip.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   631
 * @return string The altered URL.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   632
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   633
function strip_fragment_from_url( $url ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   634
	$parsed_url = @parse_url( $url );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   635
	if ( ! empty( $parsed_url['host'] ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   636
		// This mirrors code in redirect_canonical(). It does not handle every case.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   637
		$url = $parsed_url['scheme'] . '://' . $parsed_url['host'];
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   638
		if ( ! empty( $parsed_url['port'] ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   639
			$url .= ':' . $parsed_url['port'];
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   640
		}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   641
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   642
		if ( ! empty( $parsed_url['path'] ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   643
			$url .= $parsed_url['path'];
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   644
		}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   645
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   646
		if ( ! empty( $parsed_url['query'] ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   647
			$url .= '?' . $parsed_url['query'];
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   648
		}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   649
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   650
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   651
	return $url;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   652
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   653
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   654
/**
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   655
 * Attempts to guess the correct URL based on query vars
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   656
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   657
 * @since 2.3.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   658
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   659
 * @global wpdb $wpdb WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   660
 *
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   661
 * @return false|string The correct URL if one is found. False on failure.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   662
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   663
function redirect_guess_404_permalink() {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   664
	global $wpdb;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   665
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   666
	if ( get_query_var( 'name' ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   667
		$where = $wpdb->prepare( 'post_name LIKE %s', $wpdb->esc_like( get_query_var( 'name' ) ) . '%' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   668
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   669
		// if any of post_type, year, monthnum, or day are set, use them to refine the query
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   670
		if ( get_query_var( 'post_type' ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   671
			$where .= $wpdb->prepare( ' AND post_type = %s', get_query_var( 'post_type' ) );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   672
		} else {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   673
			$where .= " AND post_type IN ('" . implode( "', '", get_post_types( array( 'public' => true ) ) ) . "')";
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   674
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   675
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   676
		if ( get_query_var( 'year' ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   677
			$where .= $wpdb->prepare( ' AND YEAR(post_date) = %d', get_query_var( 'year' ) );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   678
		}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   679
		if ( get_query_var( 'monthnum' ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   680
			$where .= $wpdb->prepare( ' AND MONTH(post_date) = %d', get_query_var( 'monthnum' ) );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   681
		}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   682
		if ( get_query_var( 'day' ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   683
			$where .= $wpdb->prepare( ' AND DAYOFMONTH(post_date) = %d', get_query_var( 'day' ) );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   684
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   685
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   686
		$post_id = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'" );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   687
		if ( ! $post_id ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   688
			return false;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   689
		}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   690
		if ( get_query_var( 'feed' ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   691
			return get_post_comments_feed_link( $post_id, get_query_var( 'feed' ) );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   692
		} elseif ( get_query_var( 'page' ) && 1 < get_query_var( 'page' ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   693
			return trailingslashit( get_permalink( $post_id ) ) . user_trailingslashit( get_query_var( 'page' ), 'single_paged' );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   694
		} else {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   695
			return get_permalink( $post_id );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   696
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   697
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   698
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   699
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   700
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   701
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   702
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   703
 * Redirects a variety of shorthand URLs to the admin.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   704
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   705
 * If a user visits example.com/admin, they'll be redirected to /wp-admin.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   706
 * Visiting /login redirects to /wp-login.php, and so on.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   707
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   708
 * @since 3.4.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   709
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   710
 * @global WP_Rewrite $wp_rewrite
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   711
 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   712
function wp_redirect_admin_locations() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   713
	global $wp_rewrite;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   714
	if ( ! ( is_404() && $wp_rewrite->using_permalinks() ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   715
		return;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   716
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   717
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   718
	$admins = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   719
		home_url( 'wp-admin', 'relative' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   720
		home_url( 'dashboard', 'relative' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   721
		home_url( 'admin', 'relative' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   722
		site_url( 'dashboard', 'relative' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   723
		site_url( 'admin', 'relative' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   724
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   725
	if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $admins ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   726
		wp_redirect( admin_url() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   727
		exit;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   728
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   729
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   730
	$logins = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   731
		home_url( 'wp-login.php', 'relative' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   732
		home_url( 'login', 'relative' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   733
		site_url( 'login', 'relative' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   734
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   735
	if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $logins ) ) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   736
		wp_redirect( wp_login_url() );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   737
		exit;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   738
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   739
}