PluginProbe
Redirection / 5.2
Redirection v5.2
5.10.0 5.9.0 5.8.1 5.8.0 3.7.2 3.7.3 4.0 4.0.1 4.1 4.1.1 4.2 4.2.1 4.2.2 4.2.3 4.3 4.3.1 4.3.2 4.3.3 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6.2 4.7.1 All 130 releases
redirection / models / canonical.php

canonical.php in Redirection 5.2, at models/canonical.php

195 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Canonical redirects.
5 */
6 class Redirection_Canonical {
7 /**
8 * Aliased domains. These are domains that should be redirected to the WP domain.
9 *
10 * @var string[]
11 */
12 private $aliases = [];
13
14 /**
15 * Force HTTPS.
16 *
17 * @var boolean
18 */
19 private $force_https = false;
20
21 /**
22 * Preferred domain. WWW or no WWW.
23 *
24 * @var string
25 */
26 private $preferred_domain = '';
27
28 /**
29 * Current WP domain.
30 *
31 * @var string
32 */
33 private $actual_domain = '';
34
35 /**
36 * Constructor
37 *
38 * @param boolean $force_https `true` to force https, `false` otherwise.
39 * @param string $preferred_domain `www`, `nowww`, or empty string.
40 * @param string[] $aliases Array of domain aliases.
41 * @param string $configured_domain Current domain.
42 */
43 public function __construct( $force_https, $preferred_domain, $aliases, $configured_domain ) {
44 $this->force_https = $force_https;
45 $this->aliases = $aliases;
46 $this->preferred_domain = $preferred_domain;
47 $this->actual_domain = $configured_domain;
48 }
49
50 /**
51 * Get the canonical redirect.
52 *
53 * @param string $server Current server URL.
54 * @param string $request Current request.
55 * @return string|false
56 */
57 public function get_redirect( $server, $request ) {
58 $aliases = array_merge(
59 $this->get_preferred_aliases( $server ),
60 $this->aliases
61 );
62
63 if ( $this->force_https && ! is_ssl() ) {
64 $aliases[] = $server;
65 }
66
67 $aliases = array_unique( $aliases );
68 if ( count( $aliases ) > 0 ) {
69 foreach ( $aliases as $alias ) {
70 if ( $server === $alias ) {
71 // Redirect this to the WP url
72 $target = $this->get_canonical_target( get_bloginfo( 'url' ) );
73 if ( ! $target ) {
74 return false;
75 }
76
77 $target = esc_url_raw( $target ) . $request;
78
79 return apply_filters( 'redirect_canonical_target', $target );
80 }
81 }
82 }
83
84 return false;
85 }
86
87 /**
88 * Get the preferred alias
89 *
90 * @param string $server Current server.
91 * @return string[]
92 */
93 private function get_preferred_aliases( $server ) {
94 if ( $this->need_force_www( $server ) || $this->need_remove_www( $server ) ) {
95 return [ $server ];
96 }
97
98 return [];
99 }
100
101 /**
102 * A final check to prevent obvious site errors.
103 *
104 * @param string $server Current server.
105 * @return boolean
106 */
107 private function is_configured_domain( $server ) {
108 return $server === $this->actual_domain;
109 }
110
111 /**
112 * Get the canonical target
113 *
114 * @param string $server Current server.
115 * @return string|false
116 */
117 private function get_canonical_target( $server ) {
118 $canonical = rtrim( red_parse_domain_only( $server ), '/' );
119
120 if ( $this->need_force_www( $server ) ) {
121 $canonical = 'www.' . ltrim( $canonical, 'www.' );
122 } elseif ( $this->need_remove_www( $server ) ) {
123 $canonical = ltrim( $canonical, 'www.' );
124 }
125
126 $canonical = ( is_ssl() ? 'https://' : 'http://' ) . $canonical;
127
128 if ( $this->force_https ) {
129 $canonical = str_replace( 'http://', 'https://', $canonical );
130 }
131
132 if ( $this->is_configured_domain( $canonical ) ) {
133 return $canonical;
134 }
135
136 return false;
137 }
138
139 /**
140 * Do we need to force WWW?
141 *
142 * @param string $server Current server.
143 * @return boolean
144 */
145 private function need_force_www( $server ) {
146 $has_www = substr( $server, 0, 4 ) === 'www.';
147
148 return $this->preferred_domain === 'www' && ! $has_www;
149 }
150
151 /**
152 * Do we need to remove WWW?
153 *
154 * @param string $server Current server.
155 * @return boolean
156 */
157 private function need_remove_www( $server ) {
158 $has_www = substr( $server, 0, 4 ) === 'www.';
159
160 return $this->preferred_domain === 'nowww' && $has_www;
161 }
162
163 /**
164 * Return the full URL relocated to another domain. Certain URLs are protected from this.
165 *
166 * @param string $relocate Target domain.
167 * @param string $domain Current domain.
168 * @param string $request Current request.
169 * @return string|false
170 */
171 public function relocate_request( $relocate, $domain, $request ) {
172 $relocate = rtrim( $relocate, '/' );
173
174 $protected = apply_filters( 'redirect_relocate_protected', [
175 '/wp-admin',
176 '/wp-login.php',
177 '/wp-json/',
178 ] );
179
180 $not_protected = array_filter( $protected, function( $base ) use ( $request ) {
181 if ( substr( $request, 0, strlen( $base ) ) === $base ) {
182 return true;
183 }
184
185 return false;
186 } );
187
188 if ( $domain !== red_parse_domain_only( $relocate ) && count( $not_protected ) === 0 ) {
189 return apply_filters( 'redirect_relocate_target', $relocate . $request );
190 }
191
192 return false;
193 }
194 }
195