canonical.php
4.35 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/**
* Canonical redirects.
*/
class Redirection_Canonical {
/**
* Aliased domains. These are domains that should be redirected to the WP domain.
*
* @var string[]
*/
private $aliases = [];
/**
* Force HTTPS.
*
* @var boolean
*/
private $force_https = false;
/**
* Preferred domain. WWW or no WWW.
*
* @var string
*/
private $preferred_domain = '';
/**
* Current WP domain.
*
* @var string
*/
private $actual_domain = '';
/**
* Constructor
*
* @param boolean $force_https `true` to force https, `false` otherwise.
* @param string $preferred_domain `www`, `nowww`, or empty string.
* @param string[] $aliases Array of domain aliases.
* @param string $configured_domain Current domain.
*/
public function __construct( $force_https, $preferred_domain, $aliases, $configured_domain ) {
$this->force_https = $force_https;
$this->aliases = $aliases;
$this->preferred_domain = $preferred_domain;
$this->actual_domain = $configured_domain;
}
/**
* Get the canonical redirect.
*
* @param string $server Current server URL.
* @param string $request Current request.
* @return string|false
*/
public function get_redirect( $server, $request ) {
$aliases = array_merge(
$this->get_preferred_aliases( $server ),
$this->aliases
);
if ( $this->force_https && ! is_ssl() ) {
$aliases[] = $server;
}
$aliases = array_unique( $aliases );
if ( count( $aliases ) > 0 ) {
foreach ( $aliases as $alias ) {
if ( $server === $alias ) {
// Redirect this to the WP url
$target = $this->get_canonical_target( get_bloginfo( 'url' ) );
if ( ! $target ) {
return false;
}
$target = esc_url_raw( $target ) . $request;
return apply_filters( 'redirect_canonical_target', $target );
}
}
}
return false;
}
/**
* Get the preferred alias
*
* @param string $server Current server.
* @return string[]
*/
private function get_preferred_aliases( $server ) {
if ( $this->need_force_www( $server ) || $this->need_remove_www( $server ) ) {
return [ $server ];
}
return [];
}
/**
* A final check to prevent obvious site errors.
*
* @param string $server Current server.
* @return boolean
*/
private function is_configured_domain( $server ) {
return $server === $this->actual_domain;
}
/**
* Get the canonical target
*
* @param string $server Current server.
* @return string|false
*/
private function get_canonical_target( $server ) {
$canonical = rtrim( red_parse_domain_only( $server ), '/' );
if ( $this->need_force_www( $server ) ) {
$canonical = 'www.' . ltrim( $canonical, 'www.' );
} elseif ( $this->need_remove_www( $server ) ) {
$canonical = ltrim( $canonical, 'www.' );
}
$canonical = ( is_ssl() ? 'https://' : 'http://' ) . $canonical;
if ( $this->force_https ) {
$canonical = str_replace( 'http://', 'https://', $canonical );
}
if ( $this->is_configured_domain( $canonical ) ) {
return $canonical;
}
return false;
}
/**
* Do we need to force WWW?
*
* @param string $server Current server.
* @return boolean
*/
private function need_force_www( $server ) {
$has_www = substr( $server, 0, 4 ) === 'www.';
return $this->preferred_domain === 'www' && ! $has_www;
}
/**
* Do we need to remove WWW?
*
* @param string $server Current server.
* @return boolean
*/
private function need_remove_www( $server ) {
$has_www = substr( $server, 0, 4 ) === 'www.';
return $this->preferred_domain === 'nowww' && $has_www;
}
/**
* Return the full URL relocated to another domain. Certain URLs are protected from this.
*
* @param string $relocate Target domain.
* @param string $domain Current domain.
* @param string $request Current request.
* @return string|false
*/
public function relocate_request( $relocate, $domain, $request ) {
$relocate = rtrim( $relocate, '/' );
$protected = apply_filters( 'redirect_relocate_protected', [
'/wp-admin',
'/wp-login.php',
'/wp-json/',
] );
$not_protected = array_filter( $protected, function( $base ) use ( $request ) {
if ( substr( $request, 0, strlen( $base ) ) === $base ) {
return true;
}
return false;
} );
if ( $domain !== red_parse_domain_only( $relocate ) && count( $not_protected ) === 0 ) {
return apply_filters( 'redirect_relocate_target', $relocate . $request );
}
return false;
}
}