|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* The Jetpack Connection package Urls class file. |
|
4
|
|
|
* |
|
5
|
|
|
* @package automattic/jetpack-connection |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack\Connection; |
|
9
|
|
|
|
|
10
|
|
|
use Automattic\Jetpack\Constants; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Provides Url methods for the Connection package. |
|
14
|
|
|
*/ |
|
15
|
|
|
class Urls { |
|
16
|
|
|
|
|
17
|
|
|
const HTTPS_CHECK_OPTION_PREFIX = 'jetpack_sync_https_history_'; |
|
18
|
|
|
const HTTPS_CHECK_HISTORY = 5; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Return URL from option or PHP constant. |
|
22
|
|
|
* |
|
23
|
|
|
* @param string $option_name (e.g. 'home'). |
|
24
|
|
|
* |
|
25
|
|
|
* @return mixed|null URL. |
|
26
|
|
|
*/ |
|
27
|
|
|
public static function get_raw_url( $option_name ) { |
|
28
|
|
|
$value = null; |
|
|
|
|
|
|
29
|
|
|
$constant = ( 'home' === $option_name ) |
|
30
|
|
|
? 'WP_HOME' |
|
31
|
|
|
: 'WP_SITEURL'; |
|
32
|
|
|
|
|
33
|
|
|
// Since we disregard the constant for multisites in ms-default-filters.php, |
|
34
|
|
|
// let's also use the db value if this is a multisite. |
|
35
|
|
|
if ( ! is_multisite() && Constants::is_defined( $constant ) ) { |
|
36
|
|
|
$value = Constants::get_constant( $constant ); |
|
37
|
|
|
} else { |
|
38
|
|
|
// Let's get the option from the database so that we can bypass filters. This will help |
|
39
|
|
|
// ensure that we get more uniform values. |
|
40
|
|
|
$value = \Jetpack_Options::get_raw_option( $option_name ); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return $value; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Normalize domains by removing www unless declared in the site's option. |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $option Option value from the site. |
|
50
|
|
|
* @param callable $url_function Function retrieving the URL to normalize. |
|
51
|
|
|
* @return mixed|string URL. |
|
52
|
|
|
*/ |
|
53
|
|
|
public static function normalize_www_in_url( $option, $url_function ) { |
|
54
|
|
|
$url = wp_parse_url( call_user_func( $url_function ) ); |
|
55
|
|
|
$option_url = wp_parse_url( get_option( $option ) ); |
|
56
|
|
|
|
|
57
|
|
|
if ( ! $option_url || ! $url ) { |
|
|
|
|
|
|
58
|
|
|
return $url; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
View Code Duplication |
if ( "www.{$option_url[ 'host' ]}" === $url['host'] ) { |
|
62
|
|
|
// remove www if not present in option URL. |
|
63
|
|
|
$url['host'] = $option_url['host']; |
|
64
|
|
|
} |
|
65
|
|
View Code Duplication |
if ( "www.{$url[ 'host' ]}" === $option_url['host'] ) { |
|
66
|
|
|
// add www if present in option URL. |
|
67
|
|
|
$url['host'] = $option_url['host']; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$normalized_url = "{$url['scheme']}://{$url['host']}"; |
|
71
|
|
|
if ( isset( $url['path'] ) ) { |
|
72
|
|
|
$normalized_url .= "{$url['path']}"; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
if ( isset( $url['query'] ) ) { |
|
76
|
|
|
$normalized_url .= "?{$url['query']}"; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $normalized_url; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Return URL with a normalized protocol. |
|
84
|
|
|
* |
|
85
|
|
|
* @param callable $callable Function to retrieve URL option. |
|
86
|
|
|
* @param string $new_value URL Protocol to set URLs to. |
|
87
|
|
|
* @return string Normalized URL. |
|
88
|
|
|
*/ |
|
89
|
|
|
public static function get_protocol_normalized_url( $callable, $new_value ) { |
|
90
|
|
|
$option_key = self::HTTPS_CHECK_OPTION_PREFIX . $callable; |
|
91
|
|
|
|
|
92
|
|
|
$parsed_url = wp_parse_url( $new_value ); |
|
93
|
|
|
if ( ! $parsed_url ) { |
|
|
|
|
|
|
94
|
|
|
return $new_value; |
|
95
|
|
|
} |
|
96
|
|
|
if ( array_key_exists( 'scheme', $parsed_url ) ) { |
|
97
|
|
|
$scheme = $parsed_url['scheme']; |
|
98
|
|
|
} else { |
|
99
|
|
|
$scheme = ''; |
|
100
|
|
|
} |
|
101
|
|
|
$scheme_history = get_option( $option_key, array() ); |
|
102
|
|
|
$scheme_history[] = $scheme; |
|
103
|
|
|
|
|
104
|
|
|
// Limit length to self::HTTPS_CHECK_HISTORY. |
|
105
|
|
|
$scheme_history = array_slice( $scheme_history, ( self::HTTPS_CHECK_HISTORY * -1 ) ); |
|
106
|
|
|
|
|
107
|
|
|
update_option( $option_key, $scheme_history ); |
|
108
|
|
|
|
|
109
|
|
|
$forced_scheme = in_array( 'https', $scheme_history, true ) ? 'https' : 'http'; |
|
110
|
|
|
|
|
111
|
|
|
return set_url_scheme( $new_value, $forced_scheme ); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Helper function that is used when getting home or siteurl values. Decides |
|
116
|
|
|
* whether to get the raw or filtered value. |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $url_type URL to get, home or siteurl. |
|
119
|
|
|
* @return string |
|
120
|
|
|
*/ |
|
121
|
|
|
public static function get_raw_or_filtered_url( $url_type ) { |
|
122
|
|
|
$url_function = ( 'home' === $url_type ) |
|
123
|
|
|
? 'home_url' |
|
124
|
|
|
: 'site_url'; |
|
125
|
|
|
|
|
126
|
|
|
if ( |
|
127
|
|
|
! Constants::is_defined( 'JETPACK_SYNC_USE_RAW_URL' ) || |
|
128
|
|
|
Constants::get_constant( 'JETPACK_SYNC_USE_RAW_URL' ) |
|
129
|
|
|
) { |
|
130
|
|
|
$scheme = is_ssl() ? 'https' : 'http'; |
|
131
|
|
|
$url = self::get_raw_url( $url_type ); |
|
132
|
|
|
$url = set_url_scheme( $url, $scheme ); |
|
133
|
|
|
} else { |
|
134
|
|
|
$url = self::normalize_www_in_url( $url_type, $url_function ); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
return self::get_protocol_normalized_url( $url_function, $url ); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Return the escaped home_url. |
|
142
|
|
|
* |
|
143
|
|
|
* @return string |
|
144
|
|
|
*/ |
|
145
|
|
|
public static function home_url() { |
|
146
|
|
|
$url = self::get_raw_or_filtered_url( 'home' ); |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Allows overriding of the home_url value that is synced back to WordPress.com. |
|
150
|
|
|
* |
|
151
|
|
|
* @since 5.2.0 |
|
152
|
|
|
* |
|
153
|
|
|
* @param string $home_url |
|
154
|
|
|
*/ |
|
155
|
|
|
return esc_url_raw( apply_filters( 'jetpack_sync_home_url', $url ) ); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Return the escaped siteurl. |
|
160
|
|
|
* |
|
161
|
|
|
* @return string |
|
162
|
|
|
*/ |
|
163
|
|
|
public static function site_url() { |
|
164
|
|
|
$url = self::get_raw_or_filtered_url( 'siteurl' ); |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Allows overriding of the site_url value that is synced back to WordPress.com. |
|
168
|
|
|
* |
|
169
|
|
|
* @since 5.2.0 |
|
170
|
|
|
* |
|
171
|
|
|
* @param string $site_url |
|
172
|
|
|
*/ |
|
173
|
|
|
return esc_url_raw( apply_filters( 'jetpack_sync_site_url', $url ) ); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Return main site URL with a normalized protocol. |
|
178
|
|
|
* |
|
179
|
|
|
* @return string |
|
180
|
|
|
*/ |
|
181
|
|
|
public static function main_network_site_url() { |
|
182
|
|
|
return self::get_protocol_normalized_url( 'main_network_site_url', network_site_url() ); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
} |
|
186
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.