Completed
Push — update/story-block-loading-med... ( 27619f...a94e37 )
by
unknown
08:38
created

Status::is_offline_mode()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 0
dl 0
loc 38
rs 9.312
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
		// Then check for usual usual domains used by local dev tools.
127
		$known_local = array(
128
			'#\.local$#i',
129
			'#\.localhost$#i',
130
			'#\.test$#i',
131
			'#\.docksal$#i',      // Docksal.
132
			'#\.docksal\.site$#i', // Docksal.
133
			'#\.dev\.cc$#i',       // ServerPress.
134
			'#\.lndo\.site$#i',    // Lando.
135
		);
136
137
		if ( ! $is_local ) {
138
			foreach ( $known_local as $url ) {
139
				if ( preg_match( $url, site_url() ) ) {
140
					$is_local = true;
141
					break;
142
				}
143
			}
144
		}
145
146
		/**
147
		 * Filters is_local_site check.
148
		 *
149
		 * @since 8.8.0
150
		 *
151
		 * @param bool $is_local If the current site is a local site.
152
		 */
153
		return apply_filters( 'jetpack_is_local_site', $is_local );
154
	}
155
156
	/**
157
	 * If is a staging site.
158
	 *
159
	 * @todo Add IDC detection to a package.
160
	 *
161
	 * @return bool
162
	 */
163
	public function is_staging_site() {
164
		// @todo Remove function_exists when WP 5.5 is the minimum version.
165
		// Core's wp_get_environment_type allows for a few specific options. We should default to bowing out gracefully for anything other than production.
166
		$is_staging = function_exists( 'wp_get_environment_type' ) && 'production' !== wp_get_environment_type();
167
168
		$known_staging = array(
169
			'urls'      => array(
170
				'#\.staging\.wpengine\.com$#i', // WP Engine.
171
				'#\.staging\.kinsta\.com$#i',   // Kinsta.com.
172
				'#\.kinsta\.cloud$#i',          // Kinsta.com.
173
				'#\.stage\.site$#i',            // DreamPress.
174
				'#\.newspackstaging\.com$#i',   // Newspack.
175
				'#\.pantheonsite\.io$#i',       // Pantheon.
176
				'#\.flywheelsites\.com$#i',     // Flywheel.
177
				'#\.flywheelstaging\.com$#i',   // Flywheel.
178
				'#\.cloudwaysapps\.com$#i',     // Cloudways.
179
				'#\.azurewebsites\.net$#i',     // Azure.
180
				'#\.wpserveur\.net$#i',         // WPServeur.
181
				'#\-liquidwebsites\.com$#i',    // Liquidweb.
182
			),
183
			'constants' => array(
184
				'IS_WPE_SNAPSHOT',      // WP Engine.
185
				'KINSTA_DEV_ENV',       // Kinsta.com.
186
				'WPSTAGECOACH_STAGING', // WP Stagecoach.
187
				'JETPACK_STAGING_MODE', // Generic.
188
				'WP_LOCAL_DEV',         // Generic.
189
			),
190
		);
191
		/**
192
		 * Filters the flags of known staging sites.
193
		 *
194
		 * @since 3.9.0
195
		 *
196
		 * @param array $known_staging {
197
		 *     An array of arrays that each are used to check if the current site is staging.
198
		 *     @type array $urls      URLs of staging sites in regex to check against site_url.
199
		 *     @type array $constants PHP constants of known staging/developement environments.
200
		 *  }
201
		 */
202
		$known_staging = apply_filters( 'jetpack_known_staging', $known_staging );
203
204
		if ( isset( $known_staging['urls'] ) ) {
205
			foreach ( $known_staging['urls'] as $url ) {
206
				if ( preg_match( $url, site_url() ) ) {
207
					$is_staging = true;
208
					break;
209
				}
210
			}
211
		}
212
213
		if ( isset( $known_staging['constants'] ) ) {
214
			foreach ( $known_staging['constants'] as $constant ) {
215
				if ( defined( $constant ) && constant( $constant ) ) {
216
					$is_staging = true;
217
				}
218
			}
219
		}
220
221
		// Last, let's check if sync is erroring due to an IDC. If so, set the site to staging mode.
222
		if ( ! $is_staging && method_exists( 'Jetpack', 'validate_sync_error_idc_option' ) && \Jetpack::validate_sync_error_idc_option() ) {
223
			$is_staging = true;
224
		}
225
226
		/**
227
		 * Filters is_staging_site check.
228
		 *
229
		 * @since 3.9.0
230
		 *
231
		 * @param bool $is_staging If the current site is a staging site.
232
		 */
233
		return apply_filters( 'jetpack_is_staging_site', $is_staging );
234
	}
235
}
236