Completed
Push — replace-intval ( 844d28 )
by Jeremy
36:38 queued 27:41
created

Status::is_single_user_site()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 10
loc 10
rs 9.9332
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
	 * Whether this is a system with a multiple networks.
78
	 * Implemented since there is no core is_multi_network function.
79
	 * Right now there is no way to tell which network is the dominant network on the system.
80
	 *
81
	 * @return boolean
82
	 */
83 View Code Duplication
	public function is_multi_network() {
84
		global $wpdb;
85
86
		// If we don't have a multi site setup no need to do any more.
87
		if ( ! is_multisite() ) {
88
			return false;
89
		}
90
91
		$num_sites = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->site}" );
92
		if ( $num_sites > 1 ) {
93
			return true;
94
		}
95
96
		return false;
97
	}
98
99
	/**
100
	 * Whether the current site is single user site.
101
	 *
102
	 * @return bool
103
	 */
104 View Code Duplication
	public function is_single_user_site() {
105
		global $wpdb;
106
107
		$some_users = get_transient( 'jetpack_is_single_user' );
108
		if ( false === $some_users ) {
109
			$some_users = $wpdb->get_var( "SELECT COUNT(*) FROM (SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '{$wpdb->prefix}capabilities' LIMIT 2) AS someusers" );
110
			set_transient( 'jetpack_is_single_user', (int) $some_users, 12 * HOUR_IN_SECONDS );
111
		}
112
		return 1 === (int) $some_users;
113
	}
114
115
	/**
116
	 * If the site is a local site.
117
	 *
118
	 * @since 8.8.0
119
	 *
120
	 * @return bool
121
	 */
122
	public function is_local_site() {
123
		// Check for localhost and sites using an IP only first.
124
		$is_local = site_url() && false === strpos( site_url(), '.' );
125
126
		// @todo Remove function_exists when WP 5.5 is the minimum version.
127
		// Use Core's environment check, if available.  Added in 5.5.0 / 5.5.1 (for `local` return value)
128
		if ( function_exists( 'wp_get_environment_type' ) && 'local' === wp_get_environment_type() ) {
129
			$is_local = true;
130
		}
131
132
		// Then check for usual usual domains used by local dev tools.
133
		$known_local = array(
134
			'#\.local$#i',
135
			'#\.localhost$#i',
136
			'#\.test$#i',
137
			'#\.docksal$#i',      // Docksal.
138
			'#\.docksal\.site$#i', // Docksal.
139
			'#\.dev\.cc$#i',       // ServerPress.
140
			'#\.lndo\.site$#i',    // Lando.
141
		);
142
143
		if ( ! $is_local ) {
144
			foreach ( $known_local as $url ) {
145
				if ( preg_match( $url, site_url() ) ) {
146
					$is_local = true;
147
					break;
148
				}
149
			}
150
		}
151
152
		/**
153
		 * Filters is_local_site check.
154
		 *
155
		 * @since 8.8.0
156
		 *
157
		 * @param bool $is_local If the current site is a local site.
158
		 */
159
		return apply_filters( 'jetpack_is_local_site', $is_local );
160
	}
161
162
	/**
163
	 * If is a staging site.
164
	 *
165
	 * @todo Add IDC detection to a package.
166
	 *
167
	 * @return bool
168
	 */
169
	public function is_staging_site() {
170
		// @todo Remove function_exists when WP 5.5 is the minimum version.
171
		// 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.
172
		$is_staging = function_exists( 'wp_get_environment_type' ) && ! in_array( wp_get_environment_type(), array( 'production', 'local' ), true );
173
174
		$known_staging = array(
175
			'urls'      => array(
176
				'#\.staging\.wpengine\.com$#i', // WP Engine.
177
				'#\.staging\.kinsta\.com$#i',   // Kinsta.com.
178
				'#\.kinsta\.cloud$#i',          // Kinsta.com.
179
				'#\.stage\.site$#i',            // DreamPress.
180
				'#\.newspackstaging\.com$#i',   // Newspack.
181
				'#\.pantheonsite\.io$#i',       // Pantheon.
182
				'#\.flywheelsites\.com$#i',     // Flywheel.
183
				'#\.flywheelstaging\.com$#i',   // Flywheel.
184
				'#\.cloudwaysapps\.com$#i',     // Cloudways.
185
				'#\.azurewebsites\.net$#i',     // Azure.
186
				'#\.wpserveur\.net$#i',         // WPServeur.
187
				'#\-liquidwebsites\.com$#i',    // Liquidweb.
188
			),
189
			'constants' => array(
190
				'IS_WPE_SNAPSHOT',      // WP Engine.
191
				'KINSTA_DEV_ENV',       // Kinsta.com.
192
				'WPSTAGECOACH_STAGING', // WP Stagecoach.
193
				'JETPACK_STAGING_MODE', // Generic.
194
				'WP_LOCAL_DEV',         // Generic.
195
			),
196
		);
197
		/**
198
		 * Filters the flags of known staging sites.
199
		 *
200
		 * @since 3.9.0
201
		 *
202
		 * @param array $known_staging {
203
		 *     An array of arrays that each are used to check if the current site is staging.
204
		 *     @type array $urls      URLs of staging sites in regex to check against site_url.
205
		 *     @type array $constants PHP constants of known staging/developement environments.
206
		 *  }
207
		 */
208
		$known_staging = apply_filters( 'jetpack_known_staging', $known_staging );
209
210
		if ( isset( $known_staging['urls'] ) ) {
211
			foreach ( $known_staging['urls'] as $url ) {
212
				if ( preg_match( $url, site_url() ) ) {
213
					$is_staging = true;
214
					break;
215
				}
216
			}
217
		}
218
219
		if ( isset( $known_staging['constants'] ) ) {
220
			foreach ( $known_staging['constants'] as $constant ) {
221
				if ( defined( $constant ) && constant( $constant ) ) {
222
					$is_staging = true;
223
				}
224
			}
225
		}
226
227
		// Last, let's check if sync is erroring due to an IDC. If so, set the site to staging mode.
228
		if ( ! $is_staging && method_exists( 'Jetpack', 'validate_sync_error_idc_option' ) && \Jetpack::validate_sync_error_idc_option() ) {
229
			$is_staging = true;
230
		}
231
232
		/**
233
		 * Filters is_staging_site check.
234
		 *
235
		 * @since 3.9.0
236
		 *
237
		 * @param bool $is_staging If the current site is a staging site.
238
		 */
239
		return apply_filters( 'jetpack_is_staging_site', $is_staging );
240
	}
241
}
242