Completed
Push — update/minimum-wp-version-55 ( 5d4f97...61c7af )
by Jeremy
69:48 queued 61:24
created

Status::is_no_user_testing_mode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
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
		// Use Core's environment check, if available. Added in 5.5.0 / 5.5.1 (for `local` return value).
155
		if ( 'local' === \wp_get_environment_type() ) {
156
			$is_local = true;
157
		}
158
159
		// Then check for usual usual domains used by local dev tools.
160
		$known_local = array(
161
			'#\.local$#i',
162
			'#\.localhost$#i',
163
			'#\.test$#i',
164
			'#\.docksal$#i',      // Docksal.
165
			'#\.docksal\.site$#i', // Docksal.
166
			'#\.dev\.cc$#i',       // ServerPress.
167
			'#\.lndo\.site$#i',    // Lando.
168
		);
169
170
		if ( ! $is_local ) {
171
			foreach ( $known_local as $url ) {
172
				if ( preg_match( $url, site_url() ) ) {
173
					$is_local = true;
174
					break;
175
				}
176
			}
177
		}
178
179
		/**
180
		 * Filters is_local_site check.
181
		 *
182
		 * @since 8.8.0
183
		 *
184
		 * @param bool $is_local If the current site is a local site.
185
		 */
186
		return apply_filters( 'jetpack_is_local_site', $is_local );
187
	}
188
189
	/**
190
	 * If is a staging site.
191
	 *
192
	 * @todo Add IDC detection to a package.
193
	 *
194
	 * @return bool
195
	 */
196
	public function is_staging_site() {
197
		// 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.
198
		$is_staging = ! in_array( \wp_get_environment_type(), array( 'production', 'local' ), true );
199
200
		$known_staging = array(
201
			'urls'      => array(
202
				'#\.staging\.wpengine\.com$#i', // WP Engine.
203
				'#\.staging\.kinsta\.com$#i',   // Kinsta.com.
204
				'#\.kinsta\.cloud$#i',          // Kinsta.com.
205
				'#\.stage\.site$#i',            // DreamPress.
206
				'#\.newspackstaging\.com$#i',   // Newspack.
207
				'#\.pantheonsite\.io$#i',       // Pantheon.
208
				'#\.flywheelsites\.com$#i',     // Flywheel.
209
				'#\.flywheelstaging\.com$#i',   // Flywheel.
210
				'#\.cloudwaysapps\.com$#i',     // Cloudways.
211
				'#\.azurewebsites\.net$#i',     // Azure.
212
				'#\.wpserveur\.net$#i',         // WPServeur.
213
				'#\-liquidwebsites\.com$#i',    // Liquidweb.
214
			),
215
			'constants' => array(
216
				'IS_WPE_SNAPSHOT',      // WP Engine.
217
				'KINSTA_DEV_ENV',       // Kinsta.com.
218
				'WPSTAGECOACH_STAGING', // WP Stagecoach.
219
				'JETPACK_STAGING_MODE', // Generic.
220
				'WP_LOCAL_DEV',         // Generic.
221
			),
222
		);
223
		/**
224
		 * Filters the flags of known staging sites.
225
		 *
226
		 * @since 3.9.0
227
		 *
228
		 * @param array $known_staging {
229
		 *     An array of arrays that each are used to check if the current site is staging.
230
		 *     @type array $urls      URLs of staging sites in regex to check against site_url.
231
		 *     @type array $constants PHP constants of known staging/developement environments.
232
		 *  }
233
		 */
234
		$known_staging = apply_filters( 'jetpack_known_staging', $known_staging );
235
236
		if ( isset( $known_staging['urls'] ) ) {
237
			foreach ( $known_staging['urls'] as $url ) {
238
				if ( preg_match( $url, site_url() ) ) {
239
					$is_staging = true;
240
					break;
241
				}
242
			}
243
		}
244
245
		if ( isset( $known_staging['constants'] ) ) {
246
			foreach ( $known_staging['constants'] as $constant ) {
247
				if ( defined( $constant ) && constant( $constant ) ) {
248
					$is_staging = true;
249
				}
250
			}
251
		}
252
253
		// Last, let's check if sync is erroring due to an IDC. If so, set the site to staging mode.
254
		if ( ! $is_staging && method_exists( 'Jetpack', 'validate_sync_error_idc_option' ) && \Jetpack::validate_sync_error_idc_option() ) {
255
			$is_staging = true;
256
		}
257
258
		/**
259
		 * Filters is_staging_site check.
260
		 *
261
		 * @since 3.9.0
262
		 *
263
		 * @param bool $is_staging If the current site is a staging site.
264
		 */
265
		return apply_filters( 'jetpack_is_staging_site', $is_staging );
266
	}
267
}
268