1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* The Jetpack Connection package Utils class file. |
4
|
|
|
* |
5
|
|
|
* @package automattic/jetpack-connection |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack\Connection; |
9
|
|
|
|
10
|
|
|
use Automattic\Jetpack\Constants; |
11
|
|
|
use Automattic\Jetpack\Tracking; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Provides utility methods for the Connection package. |
15
|
|
|
*/ |
16
|
|
|
class Utils { |
17
|
|
|
|
18
|
|
|
const DEFAULT_JETPACK__API_VERSION = 1; |
19
|
|
|
const DEFAULT_JETPACK__API_BASE = 'https://jetpack.wordpress.com/jetpack.'; |
20
|
|
|
const DEFAULT_JETPACK__WPCOM_JSON_API_BASE = 'https://public-api.wordpress.com'; |
21
|
|
|
|
22
|
|
|
const HTTPS_CHECK_OPTION_PREFIX = 'jetpack_sync_https_history_'; |
23
|
|
|
const HTTPS_CHECK_HISTORY = 5; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* This method used to set the URL scheme to HTTP when HTTPS requests can't be made. |
27
|
|
|
* Now it returns the exact same URL you pass as an argument. |
28
|
|
|
* |
29
|
|
|
* @param string $url The url. |
30
|
|
|
* @return string The exact same url. |
31
|
|
|
* |
32
|
|
|
* @deprecated 9.1.0 Jetpack can't function properly on servers that don't support outbound HTTPS requests. |
33
|
|
|
*/ |
34
|
|
|
public static function fix_url_for_bad_hosts( $url ) { |
35
|
|
|
_deprecated_function( __METHOD__, 'jetpack-9.1.0' ); |
36
|
|
|
return $url; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Enters a user token into the user_tokens option |
41
|
|
|
* |
42
|
|
|
* @deprecated 9.5 Use Automattic\Jetpack\Connection\Tokens->update_user_token() instead. |
43
|
|
|
* |
44
|
|
|
* @param int $user_id The user id. |
45
|
|
|
* @param string $token The user token. |
46
|
|
|
* @param bool $is_master_user Whether the user is the master user. |
47
|
|
|
* @return bool |
48
|
|
|
*/ |
49
|
|
|
public static function update_user_token( $user_id, $token, $is_master_user ) { |
50
|
|
|
_deprecated_function( __METHOD__, 'jetpack-9.5', 'Automattic\\Jetpack\\Connection\\Tokens->update_user_token' ); |
51
|
|
|
return ( new Tokens() )->update_user_token( $user_id, $token, $is_master_user ); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Filters the value of the api constant. |
56
|
|
|
* |
57
|
|
|
* @param String $constant_value The constant value. |
58
|
|
|
* @param String $constant_name The constant name. |
59
|
|
|
* @return mixed | null |
60
|
|
|
*/ |
61
|
|
|
public static function jetpack_api_constant_filter( $constant_value, $constant_name ) { |
62
|
|
|
if ( ! is_null( $constant_value ) ) { |
63
|
|
|
// If the constant value was already set elsewhere, use that value. |
64
|
|
|
return $constant_value; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if ( defined( "self::DEFAULT_$constant_name" ) ) { |
68
|
|
|
return constant( "self::DEFAULT_$constant_name" ); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return null; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Add a filter to initialize default values of the constants. |
76
|
|
|
*/ |
77
|
|
|
public static function init_default_constants() { |
78
|
|
|
add_filter( |
79
|
|
|
'jetpack_constant_default_value', |
80
|
|
|
array( __CLASS__, 'jetpack_api_constant_filter' ), |
81
|
|
|
10, |
82
|
|
|
2 |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Filters the registration request body to include tracking properties. |
88
|
|
|
* |
89
|
|
|
* @param array $properties Already prepared tracking properties. |
90
|
|
|
* @return array amended properties. |
91
|
|
|
*/ |
92
|
|
View Code Duplication |
public static function filter_register_request_body( $properties ) { |
93
|
|
|
$tracking = new Tracking(); |
94
|
|
|
$tracks_identity = $tracking->tracks_get_identity( get_current_user_id() ); |
95
|
|
|
|
96
|
|
|
return array_merge( |
97
|
|
|
$properties, |
98
|
|
|
array( |
99
|
|
|
'_ui' => $tracks_identity['_ui'], |
100
|
|
|
'_ut' => $tracks_identity['_ut'], |
101
|
|
|
) |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Return URL from option or PHP constant. |
107
|
|
|
* |
108
|
|
|
* @param string $option_name (e.g. 'home'). |
109
|
|
|
* |
110
|
|
|
* @return mixed|null URL. |
111
|
|
|
*/ |
112
|
|
|
public static function get_raw_url( $option_name ) { |
113
|
|
|
$value = null; |
|
|
|
|
114
|
|
|
$constant = ( 'home' === $option_name ) |
115
|
|
|
? 'WP_HOME' |
116
|
|
|
: 'WP_SITEURL'; |
117
|
|
|
|
118
|
|
|
// Since we disregard the constant for multisites in ms-default-filters.php, |
119
|
|
|
// let's also use the db value if this is a multisite. |
120
|
|
|
if ( ! is_multisite() && Constants::is_defined( $constant ) ) { |
121
|
|
|
$value = Constants::get_constant( $constant ); |
122
|
|
|
} else { |
123
|
|
|
// Let's get the option from the database so that we can bypass filters. This will help |
124
|
|
|
// ensure that we get more uniform values. |
125
|
|
|
$value = \Jetpack_Options::get_raw_option( $option_name ); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $value; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Normalize domains by removing www unless declared in the site's option. |
133
|
|
|
* |
134
|
|
|
* @param string $option Option value from the site. |
135
|
|
|
* @param callable $url_function Function retrieving the URL to normalize. |
136
|
|
|
* @return mixed|string URL. |
137
|
|
|
*/ |
138
|
|
|
public static function normalize_www_in_url( $option, $url_function ) { |
139
|
|
|
$url = wp_parse_url( call_user_func( $url_function ) ); |
140
|
|
|
$option_url = wp_parse_url( get_option( $option ) ); |
141
|
|
|
|
142
|
|
|
if ( ! $option_url || ! $url ) { |
|
|
|
|
143
|
|
|
return $url; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
View Code Duplication |
if ( "www.{$option_url[ 'host' ]}" === $url['host'] ) { |
147
|
|
|
// remove www if not present in option URL. |
148
|
|
|
$url['host'] = $option_url['host']; |
149
|
|
|
} |
150
|
|
View Code Duplication |
if ( "www.{$url[ 'host' ]}" === $option_url['host'] ) { |
151
|
|
|
// add www if present in option URL. |
152
|
|
|
$url['host'] = $option_url['host']; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$normalized_url = "{$url['scheme']}://{$url['host']}"; |
156
|
|
|
if ( isset( $url['path'] ) ) { |
157
|
|
|
$normalized_url .= "{$url['path']}"; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
if ( isset( $url['query'] ) ) { |
161
|
|
|
$normalized_url .= "?{$url['query']}"; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $normalized_url; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Return URL with a normalized protocol. |
169
|
|
|
* |
170
|
|
|
* @param callable $callable Function to retrieve URL option. |
171
|
|
|
* @param string $new_value URL Protocol to set URLs to. |
172
|
|
|
* @return string Normalized URL. |
173
|
|
|
*/ |
174
|
|
|
public static function get_protocol_normalized_url( $callable, $new_value ) { |
175
|
|
|
$option_key = self::HTTPS_CHECK_OPTION_PREFIX . $callable; |
176
|
|
|
|
177
|
|
|
$parsed_url = wp_parse_url( $new_value ); |
178
|
|
|
if ( ! $parsed_url ) { |
|
|
|
|
179
|
|
|
return $new_value; |
180
|
|
|
} |
181
|
|
|
if ( array_key_exists( 'scheme', $parsed_url ) ) { |
182
|
|
|
$scheme = $parsed_url['scheme']; |
183
|
|
|
} else { |
184
|
|
|
$scheme = ''; |
185
|
|
|
} |
186
|
|
|
$scheme_history = get_option( $option_key, array() ); |
187
|
|
|
$scheme_history[] = $scheme; |
188
|
|
|
|
189
|
|
|
// Limit length to self::HTTPS_CHECK_HISTORY. |
190
|
|
|
$scheme_history = array_slice( $scheme_history, ( self::HTTPS_CHECK_HISTORY * -1 ) ); |
191
|
|
|
|
192
|
|
|
update_option( $option_key, $scheme_history ); |
193
|
|
|
|
194
|
|
|
$forced_scheme = in_array( 'https', $scheme_history, true ) ? 'https' : 'http'; |
195
|
|
|
|
196
|
|
|
return set_url_scheme( $new_value, $forced_scheme ); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Helper function that is used when getting home or siteurl values. Decides |
201
|
|
|
* whether to get the raw or filtered value. |
202
|
|
|
* |
203
|
|
|
* @param string $url_type URL to get, home or siteurl. |
204
|
|
|
* @return string |
205
|
|
|
*/ |
206
|
|
|
public static function get_raw_or_filtered_url( $url_type ) { |
207
|
|
|
$url_function = ( 'home' === $url_type ) |
208
|
|
|
? 'home_url' |
209
|
|
|
: 'site_url'; |
210
|
|
|
|
211
|
|
|
if ( |
212
|
|
|
! Constants::is_defined( 'JETPACK_SYNC_USE_RAW_URL' ) || |
213
|
|
|
Constants::get_constant( 'JETPACK_SYNC_USE_RAW_URL' ) |
214
|
|
|
) { |
215
|
|
|
$scheme = is_ssl() ? 'https' : 'http'; |
216
|
|
|
$url = self::get_raw_url( $url_type ); |
217
|
|
|
$url = set_url_scheme( $url, $scheme ); |
218
|
|
|
} else { |
219
|
|
|
$url = self::normalize_www_in_url( $url_type, $url_function ); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
return self::get_protocol_normalized_url( $url_function, $url ); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Return the escaped home_url. |
227
|
|
|
* |
228
|
|
|
* @return string |
229
|
|
|
*/ |
230
|
|
|
public static function home_url() { |
231
|
|
|
$url = self::get_raw_or_filtered_url( 'home' ); |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Allows overriding of the home_url value that is synced back to WordPress.com. |
235
|
|
|
* |
236
|
|
|
* @since 5.2.0 |
237
|
|
|
* |
238
|
|
|
* @param string $home_url |
239
|
|
|
*/ |
240
|
|
|
return esc_url_raw( apply_filters( 'jetpack_sync_home_url', $url ) ); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Return the escaped siteurl. |
245
|
|
|
* |
246
|
|
|
* @return string |
247
|
|
|
*/ |
248
|
|
|
public static function site_url() { |
249
|
|
|
$url = self::get_raw_or_filtered_url( 'siteurl' ); |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Allows overriding of the site_url value that is synced back to WordPress.com. |
253
|
|
|
* |
254
|
|
|
* @since 5.2.0 |
255
|
|
|
* |
256
|
|
|
* @param string $site_url |
257
|
|
|
*/ |
258
|
|
|
return esc_url_raw( apply_filters( 'jetpack_sync_site_url', $url ) ); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Return main site URL with a normalized protocol. |
263
|
|
|
* |
264
|
|
|
* @return string |
265
|
|
|
*/ |
266
|
|
|
public static function main_network_site_url() { |
267
|
|
|
return self::get_protocol_normalized_url( 'main_network_site_url', network_site_url() ); |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.