forked from humanmade/Mercator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redirect.php
163 lines (136 loc) · 4.07 KB
/
redirect.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
namespace Mercator\Redirect;
use Mercator\Mapping;
add_action( 'send_headers', __NAMESPACE__ . '\handle_redirect' );
function is_enabled( $mapping = null ) {
$mapping = $mapping ?: $GLOBALS['mercator_current_mapping'];
/**
* Determine whether a mapping should be used
*
* Typically, you'll want to only allow active mappings to be used. However,
* if you want to use more advanced logic, or allow non-active domains to
* be mapped too, simply filter here.
*
* @param boolean $is_active Should the mapping be treated as active?
* @param Mapping $mapping Mapping that we're inspecting
*/
return apply_filters( 'mercator.redirect.enabled', $mapping->is_active(), $mapping );
}
function redirect_admin() {
/**
* Whether to redirect mapped domains on visits to the WP admin.
* Recommended to leave this false as there's no SEO problem
* and avoids having a broken / unreachable admin if the
* custom domain DNS is incorrect or not fully propagated.
*
* @param bool Set to true to enable admin redirects
*/
return apply_filters( 'mercator.redirect.admin.enabled', false );
}
function use_legacy_redirect() {
/**
* If you still have blogs with the main domain as the subdomain
* or subsite path this will allow you to still redirect to the
* first active alias found.
*
* @param bool Set to true to enable legacy redirects
*/
return apply_filters( 'mercator.redirect.legacy.enabled', false );
}
/**
* Performs the redirect to the primary domain
*/
function handle_redirect() {
// Custom domain redirects need SUNRISE.
if ( ! defined( 'SUNRISE' ) || ! SUNRISE ) {
return;
}
// Should we redirect visits to the admin?
if ( ( is_admin() || $GLOBALS['pagenow'] === 'wp-login.php' ) && ! redirect_admin() ) {
return;
}
// Don't redirect REST API requests
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
return;
}
// Disable redirect on WP CLI.
if ( defined( 'WP_CLI' ) && WP_CLI ) {
return;
}
// Disable for XML RPC.
if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) {
return;
}
// Disable redirects for crons.
if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
return;
}
// Disable redirects for admin ajax.
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
return;
}
// Support for alias as primary domain
if ( use_legacy_redirect() ) {
legacy_redirect();
return;
}
// Get mapping if it exists, if we're already on the primary domain we'll exit here
$mapping = Mapping::get_by_domain( $_SERVER['HTTP_HOST'] );
if ( is_wp_error( $mapping ) || ! $mapping ) {
return;
}
if ( ! is_enabled( $mapping ) ) {
return;
}
// If current domain and domain mapping are the same, exit early.
$site = $mapping->get_site();
$domain = $site->domain;
if ( $domain === $_SERVER['HTTP_HOST'] ) {
return;
}
$path = $site->path;
// Use blogs table domain as the primary domain
redirect( $domain . $path );
}
/**
* Check if the main site domain contains the network hostname
* and use the first active alias if so
*/
function legacy_redirect() {
$site = get_site( get_current_blog_id() );
// Check the blog domain isn't a subdomain or subfolder
if ( false === strpos( $site->domain, get_current_site()->domain ) ) {
if ( $_SERVER['HTTP_HOST'] !== $site->domain ) {
redirect( $site->domain . $site->path );
}
return;
}
$mappings = Mapping::get_by_site( get_current_blog_id() );
if ( is_wp_error( $mappings ) || ! $mappings ) {
return;
}
foreach ( $mappings as $mapping ) {
if ( ! is_enabled( $mapping ) ) {
continue;
}
// Redirect to the first active alias if we're not there already
if ( $_SERVER['HTTP_HOST'] !== $mapping->get_domain() ) {
$domain = $mapping->get_domain();
$path = $mapping->get_site()->path;
redirect( $domain . $path );
} else {
break;
}
}
}
/**
* Helper function to redirect to url
*
* @param string $url
*/
function redirect( $url ) {
$status_code = (int) apply_filters( 'mercator.redirect.status.code', 301 );
$domain = untrailingslashit( set_url_scheme( "http://{$url}" ) );
wp_redirect( $domain . esc_url_raw( $_SERVER['REQUEST_URI'] ), $status_code );
exit;
}