Completed
Push — fix/google-analytics-tag ( 9d0c0c...d2a887 )
by
unknown
136:04 queued 127:46
created

Status   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 280
Duplicated Lines 11.79 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 32
lcom 0
cbo 1
dl 33
loc 280
rs 9.84
c 0
b 0
f 0

8 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 38 6
A get_site_suffix() 0 15 3
C is_staging_site() 8 71 11

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
use WPCOM_Masterbar;
11
12
/**
13
 * Class Automattic\Jetpack\Status
14
 *
15
 * Used to retrieve information about the current status of Jetpack and the site overall.
16
 */
17
class Status {
18
	/**
19
	 * Is Jetpack in development (offline) mode?
20
	 *
21
	 * @deprecated 8.8.0 Use Status->is_offline_mode().
22
	 *
23
	 * @return bool Whether Jetpack's offline mode is active.
24
	 */
25
	public function is_development_mode() {
26
		_deprecated_function( __FUNCTION__, 'Jetpack 8.8.0', 'Automattic\Jetpack\Status->is_offline_mode' );
27
		return $this->is_offline_mode();
28
	}
29
30
	/**
31
	 * Is Jetpack in offline mode?
32
	 *
33
	 * This was formerly called "Development Mode", but sites "in development" aren't always offline/localhost.
34
	 *
35
	 * @since 8.8.0
36
	 *
37
	 * @return bool Whether Jetpack's offline mode is active.
38
	 */
39
	public function is_offline_mode() {
40
		$offline_mode = false;
41
42
		if ( defined( '\\JETPACK_DEV_DEBUG' ) ) {
43
			$offline_mode = constant( '\\JETPACK_DEV_DEBUG' );
44
		} elseif ( defined( '\\WP_LOCAL_DEV' ) ) {
45
			$offline_mode = constant( '\\WP_LOCAL_DEV' );
46
		} elseif ( $this->is_local_site() ) {
47
			$offline_mode = true;
48
		}
49
50
		/**
51
		 * Filters Jetpack's offline mode.
52
		 *
53
		 * @see https://jetpack.com/support/development-mode/
54
		 * @todo Update documentation ^^.
55
		 *
56
		 * @since 2.2.1
57
		 * @deprecated 8.8.0
58
		 *
59
		 * @param bool $offline_mode Is Jetpack's offline mode active.
60
		 */
61
		$offline_mode = (bool) apply_filters_deprecated( 'jetpack_development_mode', array( $offline_mode ), '8.8.0', 'jetpack_offline_mode' );
62
63
		/**
64
		 * Filters Jetpack's offline mode.
65
		 *
66
		 * @see https://jetpack.com/support/development-mode/
67
		 * @todo Update documentation ^^.
68
		 *
69
		 * @since 8.8.0
70
		 *
71
		 * @param bool $offline_mode Is Jetpack's offline mode active.
72
		 */
73
		$offline_mode = (bool) apply_filters( 'jetpack_offline_mode', $offline_mode );
74
75
		return $offline_mode;
76
	}
77
78
	/**
79
	 * Is Jetpack in "No User test mode"?
80
	 *
81
	 * This will make Jetpack act as if there were no connected users, but only a site connection (aka blog token)
82
	 *
83
	 * @since 9.2.0
84
	 *
85
	 * @return bool Whether Jetpack's No User Testing Mode is active.
86
	 */
87
	public function is_no_user_testing_mode() {
88
		$test_mode = false;
89
		if ( defined( 'JETPACK_NO_USER_TEST_MODE' ) ) {
90
			$test_mode = JETPACK_NO_USER_TEST_MODE;
91
		}
92
93
		/**
94
		 * Filters Jetpack's No User testing mode.
95
		 *
96
		 * @since 9.2.0
97
		 *
98
		 * @param bool $test_mode Is Jetpack's No User testing mode active.
99
		 */
100
		$test_mode = (bool) apply_filters( 'jetpack_no_user_testing_mode', $test_mode );
101
102
		return $test_mode;
103
104
	}
105
106
	/**
107
	 * Whether this is a system with a multiple networks.
108
	 * Implemented since there is no core is_multi_network function.
109
	 * Right now there is no way to tell which network is the dominant network on the system.
110
	 *
111
	 * @return boolean
112
	 */
113 View Code Duplication
	public function is_multi_network() {
114
		global $wpdb;
115
116
		// If we don't have a multi site setup no need to do any more.
117
		if ( ! is_multisite() ) {
118
			return false;
119
		}
120
121
		$num_sites = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->site}" );
122
		if ( $num_sites > 1 ) {
123
			return true;
124
		}
125
126
		return false;
127
	}
128
129
	/**
130
	 * Whether the current site is single user site.
131
	 *
132
	 * @return bool
133
	 */
134 View Code Duplication
	public function is_single_user_site() {
135
		global $wpdb;
136
137
		$some_users = get_transient( 'jetpack_is_single_user' );
138
		if ( false === $some_users ) {
139
			$some_users = $wpdb->get_var( "SELECT COUNT(*) FROM (SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '{$wpdb->prefix}capabilities' LIMIT 2) AS someusers" );
140
			set_transient( 'jetpack_is_single_user', (int) $some_users, 12 * HOUR_IN_SECONDS );
141
		}
142
		return 1 === (int) $some_users;
143
	}
144
145
	/**
146
	 * If the site is a local site.
147
	 *
148
	 * @since 8.8.0
149
	 *
150
	 * @return bool
151
	 */
152
	public function is_local_site() {
153
		// Check for localhost and sites using an IP only first.
154
		$is_local = site_url() && false === strpos( site_url(), '.' );
155
156
		// Use Core's environment check, if available. Added in 5.5.0 / 5.5.1 (for `local` return value).
157
		if ( 'local' === wp_get_environment_type() ) {
158
			$is_local = true;
159
		}
160
161
		// Then check for usual usual domains used by local dev tools.
162
		$known_local = array(
163
			'#\.local$#i',
164
			'#\.localhost$#i',
165
			'#\.test$#i',
166
			'#\.docksal$#i',      // Docksal.
167
			'#\.docksal\.site$#i', // Docksal.
168
			'#\.dev\.cc$#i',       // ServerPress.
169
			'#\.lndo\.site$#i',    // Lando.
170
		);
171
172
		if ( ! $is_local ) {
173
			foreach ( $known_local as $url ) {
174
				if ( preg_match( $url, site_url() ) ) {
175
					$is_local = true;
176
					break;
177
				}
178
			}
179
		}
180
181
		/**
182
		 * Filters is_local_site check.
183
		 *
184
		 * @since 8.8.0
185
		 *
186
		 * @param bool $is_local If the current site is a local site.
187
		 */
188
		return apply_filters( 'jetpack_is_local_site', $is_local );
189
	}
190
191
	/**
192
	 * If is a staging site.
193
	 *
194
	 * @todo Add IDC detection to a package.
195
	 *
196
	 * @return bool
197
	 */
198
	public function is_staging_site() {
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 = ! 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 View Code Duplication
		if ( isset( $known_staging['urls'] ) ) {
239
			foreach ( $known_staging['urls'] as $url ) {
240
				if ( preg_match( $url, wp_parse_url( site_url(), PHP_URL_HOST ) ) ) {
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
	/**
271
	 * Returns the site slug suffix to be used as part of Calypso URLs.
272
	 *
273
	 * Strips http:// or https:// from a url, replaces forward slash with ::.
274
	 *
275
	 * @since 9.2.0
276
	 *
277
	 * @param string $url Optional. URL to build the site suffix from. Default: Home URL.
278
	 *
279
	 * @return string
280
	 */
281
	public function get_site_suffix( $url = '' ) {
282
		// On WordPress.com, site suffixes are a bit different.
283
		if ( method_exists( 'WPCOM_Masterbar', 'get_calypso_site_slug' ) ) {
284
			return WPCOM_Masterbar::get_calypso_site_slug( get_current_blog_id() );
285
		}
286
287
		if ( empty( $url ) ) {
288
			$url = \home_url();
289
		}
290
291
		$url = preg_replace( '#^.*?://#', '', $url );
292
		$url = str_replace( '/', '::', $url );
293
294
		return $url;
295
	}
296
}
297