Completed
Push — update/use-identity-crisis-pac... ( 417540...ed45be )
by
unknown
09:27
created

Functions::main_network_site_url()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Utility functions for consistent url based options.
4
 *
5
 * @package automattic/identity-crisis
6
 */
7
8
namespace Automattic\Jetpack\Identity_Crisis;
9
10
use Automattic\Jetpack\Constants;
11
12
/**
13
 * Utility functions for consistent url based options.
14
 */
15
class Functions {
16
17
	const HTTPS_CHECK_OPTION_PREFIX = 'jetpack_sync_https_history_';
18
19
	const HTTPS_CHECK_HISTORY = 5;
20
21
	/**
22
	 * Return URL from option or PHP constant.
23
	 *
24
	 * @param string $option_name (e.g. 'home').
25
	 *
26
	 * @return mixed|null URL.
27
	 */
28
	public static function get_raw_url( $option_name ) {
29
		$value    = null;
0 ignored issues
show
Unused Code introduced by
$value is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
30
		$constant = ( 'home' === $option_name )
31
			? 'WP_HOME'
32
			: 'WP_SITEURL';
33
34
		// Since we disregard the constant for multisites in ms-default-filters.php,
35
		// let's also use the db value if this is a multisite.
36
		if ( ! is_multisite() && Constants::is_defined( $constant ) ) {
37
			$value = Constants::get_constant( $constant );
38
		} else {
39
			// Let's get the option from the database so that we can bypass filters. This will help
40
			// ensure that we get more uniform values.
41
			$value = \Jetpack_Options::get_raw_option( $option_name );
42
		}
43
44
		return $value;
45
	}
46
47
	/**
48
	 * Normalize domains by removing www unless declared in the site's option.
49
	 *
50
	 * @param string   $option Option value from the site.
51
	 * @param callable $url_function Function retrieving the URL to normalize.
52
	 * @return mixed|string URL.
53
	 */
54
	public static function normalize_www_in_url( $option, $url_function ) {
55
		$url        = wp_parse_url( call_user_func( $url_function ) );
56
		$option_url = wp_parse_url( get_option( $option ) );
57
58
		if ( ! $option_url || ! $url ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $url of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Bug Best Practice introduced by
The expression $option_url of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
59
			return $url;
60
		}
61
62 View Code Duplication
		if ( "www.{$option_url[ 'host' ]}" === $url['host'] ) {
63
			// remove www if not present in option URL.
64
			$url['host'] = $option_url['host'];
65
		}
66 View Code Duplication
		if ( "www.{$url[ 'host' ]}" === $option_url['host'] ) {
67
			// add www if present in option URL.
68
			$url['host'] = $option_url['host'];
69
		}
70
71
		$normalized_url = "{$url['scheme']}://{$url['host']}";
72
		if ( isset( $url['path'] ) ) {
73
			$normalized_url .= "{$url['path']}";
74
		}
75
76
		if ( isset( $url['query'] ) ) {
77
			$normalized_url .= "?{$url['query']}";
78
		}
79
80
		return $normalized_url;
81
	}
82
83
	/**
84
	 * Return URL with a normalized protocol.
85
	 *
86
	 * @param callable $callable Function to retrieve URL option.
87
	 * @param string   $new_value URL Protocol to set URLs to.
88
	 * @return string Normalized URL.
89
	 */
90
	public static function get_protocol_normalized_url( $callable, $new_value ) {
91
		$option_key = self::HTTPS_CHECK_OPTION_PREFIX . $callable;
92
93
		$parsed_url = wp_parse_url( $new_value );
94
		if ( ! $parsed_url ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $parsed_url of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
95
			return $new_value;
96
		}
97
		if ( array_key_exists( 'scheme', $parsed_url ) ) {
98
			$scheme = $parsed_url['scheme'];
99
		} else {
100
			$scheme = '';
101
		}
102
		$scheme_history   = get_option( $option_key, array() );
103
		$scheme_history[] = $scheme;
104
105
		// Limit length to self::HTTPS_CHECK_HISTORY.
106
		$scheme_history = array_slice( $scheme_history, ( self::HTTPS_CHECK_HISTORY * -1 ) );
107
108
		update_option( $option_key, $scheme_history );
109
110
		$forced_scheme = in_array( 'https', $scheme_history, true ) ? 'https' : 'http';
111
112
		return set_url_scheme( $new_value, $forced_scheme );
113
	}
114
115
	/**
116
	 * Helper function that is used when getting home or siteurl values. Decides
117
	 * whether to get the raw or filtered value.
118
	 *
119
	 * @param string $url_type URL to get, home or siteurl.
120
	 * @return string
121
	 */
122
	public static function get_raw_or_filtered_url( $url_type ) {
123
		$url_function = ( 'home' === $url_type )
124
			? 'home_url'
125
			: 'site_url';
126
127
		if (
128
			! Constants::is_defined( 'JETPACK_SYNC_USE_RAW_URL' ) ||
129
			Constants::get_constant( 'JETPACK_SYNC_USE_RAW_URL' )
130
		) {
131
			$scheme = is_ssl() ? 'https' : 'http';
132
			$url    = self::get_raw_url( $url_type );
133
			$url    = set_url_scheme( $url, $scheme );
134
		} else {
135
			$url = self::normalize_www_in_url( $url_type, $url_function );
136
		}
137
138
		return self::get_protocol_normalized_url( $url_function, $url );
139
	}
140
141
	/**
142
	 * Return the escaped home_url.
143
	 *
144
	 * @return string
145
	 */
146
	public static function home_url() {
147
		$url = self::get_raw_or_filtered_url( 'home' );
148
149
		/**
150
		 * Allows overriding of the home_url value that is synced back to WordPress.com.
151
		 *
152
		 * @since 5.2.0
153
		 *
154
		 * @param string $home_url
155
		 */
156
		return esc_url_raw( apply_filters( 'jetpack_sync_home_url', $url ) );
157
	}
158
159
	/**
160
	 * Return the escaped siteurl.
161
	 *
162
	 * @return string
163
	 */
164
	public static function site_url() {
165
		$url = self::get_raw_or_filtered_url( 'siteurl' );
166
167
		/**
168
		 * Allows overriding of the site_url value that is synced back to WordPress.com.
169
		 *
170
		 * @since 5.2.0
171
		 *
172
		 * @param string $site_url
173
		 */
174
		return esc_url_raw( apply_filters( 'jetpack_sync_site_url', $url ) );
175
	}
176
177
	/**
178
	 * Return main site URL with a normalized protocol.
179
	 *
180
	 * @return string
181
	 */
182
	public static function main_network_site_url() {
183
		return self::get_protocol_normalized_url( 'main_network_site_url', network_site_url() );
184
	}
185
186
}
187