Completed
Push — phpcs/milestone-widget ( bc2c59...b22254 )
by Jeremy
62:54 queued 53:39
created

Status   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 255
Duplicated Lines 9.8 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 25
loc 255
rs 9.92
c 0
b 0
f 0
wmc 31
lcom 0
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A is_development_mode() 0 4 1
A is_offline_mode() 0 38 4
A is_no_user_testing_mode() 0 18 2
A is_multi_network() 15 15 3
A is_single_user_site() 10 10 2
B is_local_site() 0 39 7
C is_staging_site() 0 72 12

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * A status class for Jetpack.
4
 *
5
 * @package automattic/jetpack-status
6
 */
7
8
namespace Automattic\Jetpack;
9
10
/**
11
 * Class Automattic\Jetpack\Status
12
 *
13
 * Used to retrieve information about the current status of Jetpack and the site overall.
14
 */
15
class Status {
16
	/**
17
	 * Is Jetpack in development (offline) mode?
18
	 *
19
	 * @deprecated 8.8.0 Use Status->is_offline_mode().
20
	 *
21
	 * @return bool Whether Jetpack's offline mode is active.
22
	 */
23
	public function is_development_mode() {
24
		_deprecated_function( __FUNCTION__, 'Jetpack 8.8.0', 'Automattic\Jetpack\Status->is_offline_mode' );
25
		return $this->is_offline_mode();
26
	}
27
28
	/**
29
	 * Is Jetpack in offline mode?
30
	 *
31
	 * This was formerly called "Development Mode", but sites "in development" aren't always offline/localhost.
32
	 *
33
	 * @since 8.8.0
34
	 *
35
	 * @return bool Whether Jetpack's offline mode is active.
36
	 */
37
	public function is_offline_mode() {
38
		$offline_mode = false;
39
40
		if ( defined( '\\JETPACK_DEV_DEBUG' ) ) {
41
			$offline_mode = constant( '\\JETPACK_DEV_DEBUG' );
42
		} elseif ( defined( '\\WP_LOCAL_DEV' ) ) {
43
			$offline_mode = constant( '\\WP_LOCAL_DEV' );
44
		} elseif ( $this->is_local_site() ) {
45
			$offline_mode = true;
46
		}
47
48
		/**
49
		 * Filters Jetpack's offline mode.
50
		 *
51
		 * @see https://jetpack.com/support/development-mode/
52
		 * @todo Update documentation ^^.
53
		 *
54
		 * @since 2.2.1
55
		 * @deprecated 8.8.0
56
		 *
57
		 * @param bool $offline_mode Is Jetpack's offline mode active.
58
		 */
59
		$offline_mode = (bool) apply_filters_deprecated( 'jetpack_development_mode', array( $offline_mode ), '8.8.0', 'jetpack_offline_mode' );
60
61
		/**
62
		 * Filters Jetpack's offline mode.
63
		 *
64
		 * @see https://jetpack.com/support/development-mode/
65
		 * @todo Update documentation ^^.
66
		 *
67
		 * @since 8.8.0
68
		 *
69
		 * @param bool $offline_mode Is Jetpack's offline mode active.
70
		 */
71
		$offline_mode = (bool) apply_filters( 'jetpack_offline_mode', $offline_mode );
72
73
		return $offline_mode;
74
	}
75
76
	/**
77
	 * Is Jetpack in "No User test mode"?
78
	 *
79
	 * This will make Jetpack act as if there were no connected users, but only a site connection (aka blog token)
80
	 *
81
	 * @since 9.2.0
82
	 *
83
	 * @return bool Whether Jetpack's No User Testing Mode is active.
84
	 */
85
	public function is_no_user_testing_mode() {
86
		$test_mode = false;
87
		if ( defined( 'JETPACK_NO_USER_TEST_MODE' ) ) {
88
			$test_mode = JETPACK_NO_USER_TEST_MODE;
89
		}
90
91
		/**
92
		 * Filters Jetpack's No User testing mode.
93
		 *
94
		 * @since 9.2.0
95
		 *
96
		 * @param bool $test_mode Is Jetpack's No User testing mode active.
97
		 */
98
		$test_mode = (bool) apply_filters( 'jetpack_no_user_testing_mode', $test_mode );
99
100
		return $test_mode;
101
102
	}
103
104
	/**
105
	 * Whether this is a system with a multiple networks.
106
	 * Implemented since there is no core is_multi_network function.
107
	 * Right now there is no way to tell which network is the dominant network on the system.
108
	 *
109
	 * @return boolean
110
	 */
111 View Code Duplication
	public function is_multi_network() {
112
		global $wpdb;
113
114
		// If we don't have a multi site setup no need to do any more.
115
		if ( ! is_multisite() ) {
116
			return false;
117
		}
118
119
		$num_sites = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->site}" );
120
		if ( $num_sites > 1 ) {
121
			return true;
122
		}
123
124
		return false;
125
	}
126
127
	/**
128
	 * Whether the current site is single user site.
129
	 *
130
	 * @return bool
131
	 */
132 View Code Duplication
	public function is_single_user_site() {
133
		global $wpdb;
134
135
		$some_users = get_transient( 'jetpack_is_single_user' );
136
		if ( false === $some_users ) {
137
			$some_users = $wpdb->get_var( "SELECT COUNT(*) FROM (SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '{$wpdb->prefix}capabilities' LIMIT 2) AS someusers" );
138
			set_transient( 'jetpack_is_single_user', (int) $some_users, 12 * HOUR_IN_SECONDS );
139
		}
140
		return 1 === (int) $some_users;
141
	}
142
143
	/**
144
	 * If the site is a local site.
145
	 *
146
	 * @since 8.8.0
147
	 *
148
	 * @return bool
149
	 */
150
	public function is_local_site() {
151
		// Check for localhost and sites using an IP only first.
152
		$is_local = site_url() && false === strpos( site_url(), '.' );
153
154
		// @todo Remove function_exists when WP 5.5 is the minimum version.
155
		// Use Core's environment check, if available.  Added in 5.5.0 / 5.5.1 (for `local` return value)
156
		if ( function_exists( 'wp_get_environment_type' ) && 'local' === wp_get_environment_type() ) {
157
			$is_local = true;
158
		}
159
160
		// Then check for usual usual domains used by local dev tools.
161
		$known_local = array(
162
			'#\.local$#i',
163
			'#\.localhost$#i',
164
			'#\.test$#i',
165
			'#\.docksal$#i',      // Docksal.
166
			'#\.docksal\.site$#i', // Docksal.
167
			'#\.dev\.cc$#i',       // ServerPress.
168
			'#\.lndo\.site$#i',    // Lando.
169
		);
170
171
		if ( ! $is_local ) {
172
			foreach ( $known_local as $url ) {
173
				if ( preg_match( $url, site_url() ) ) {
174
					$is_local = true;
175
					break;
176
				}
177
			}
178
		}
179
180
		/**
181
		 * Filters is_local_site check.
182
		 *
183
		 * @since 8.8.0
184
		 *
185
		 * @param bool $is_local If the current site is a local site.
186
		 */
187
		return apply_filters( 'jetpack_is_local_site', $is_local );
188
	}
189
190
	/**
191
	 * If is a staging site.
192
	 *
193
	 * @todo Add IDC detection to a package.
194
	 *
195
	 * @return bool
196
	 */
197
	public function is_staging_site() {
198
		// @todo Remove function_exists when WP 5.5 is the minimum version.
199
		// Core's wp_get_environment_type allows for a few specific options. We should default to bowing out gracefully for anything other than production or local.
200
		$is_staging = function_exists( 'wp_get_environment_type' ) && ! in_array( wp_get_environment_type(), array( 'production', 'local' ), true );
201
202
		$known_staging = array(
203
			'urls'      => array(
204
				'#\.staging\.wpengine\.com$#i', // WP Engine.
205
				'#\.staging\.kinsta\.com$#i',   // Kinsta.com.
206
				'#\.kinsta\.cloud$#i',          // Kinsta.com.
207
				'#\.stage\.site$#i',            // DreamPress.
208
				'#\.newspackstaging\.com$#i',   // Newspack.
209
				'#\.pantheonsite\.io$#i',       // Pantheon.
210
				'#\.flywheelsites\.com$#i',     // Flywheel.
211
				'#\.flywheelstaging\.com$#i',   // Flywheel.
212
				'#\.cloudwaysapps\.com$#i',     // Cloudways.
213
				'#\.azurewebsites\.net$#i',     // Azure.
214
				'#\.wpserveur\.net$#i',         // WPServeur.
215
				'#\-liquidwebsites\.com$#i',    // Liquidweb.
216
			),
217
			'constants' => array(
218
				'IS_WPE_SNAPSHOT',      // WP Engine.
219
				'KINSTA_DEV_ENV',       // Kinsta.com.
220
				'WPSTAGECOACH_STAGING', // WP Stagecoach.
221
				'JETPACK_STAGING_MODE', // Generic.
222
				'WP_LOCAL_DEV',         // Generic.
223
			),
224
		);
225
		/**
226
		 * Filters the flags of known staging sites.
227
		 *
228
		 * @since 3.9.0
229
		 *
230
		 * @param array $known_staging {
231
		 *     An array of arrays that each are used to check if the current site is staging.
232
		 *     @type array $urls      URLs of staging sites in regex to check against site_url.
233
		 *     @type array $constants PHP constants of known staging/developement environments.
234
		 *  }
235
		 */
236
		$known_staging = apply_filters( 'jetpack_known_staging', $known_staging );
237
238
		if ( isset( $known_staging['urls'] ) ) {
239
			foreach ( $known_staging['urls'] as $url ) {
240
				if ( preg_match( $url, site_url() ) ) {
241
					$is_staging = true;
242
					break;
243
				}
244
			}
245
		}
246
247
		if ( isset( $known_staging['constants'] ) ) {
248
			foreach ( $known_staging['constants'] as $constant ) {
249
				if ( defined( $constant ) && constant( $constant ) ) {
250
					$is_staging = true;
251
				}
252
			}
253
		}
254
255
		// Last, let's check if sync is erroring due to an IDC. If so, set the site to staging mode.
256
		if ( ! $is_staging && method_exists( 'Jetpack', 'validate_sync_error_idc_option' ) && \Jetpack::validate_sync_error_idc_option() ) {
257
			$is_staging = true;
258
		}
259
260
		/**
261
		 * Filters is_staging_site check.
262
		 *
263
		 * @since 3.9.0
264
		 *
265
		 * @param bool $is_staging If the current site is a staging site.
266
		 */
267
		return apply_filters( 'jetpack_is_staging_site', $is_staging );
268
	}
269
}
270