1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
|
<?php /** * A status class for Jetpack. * * @package automattic/jetpack-status */
namespace Automattic\Jetpack;
use Automattic\Jetpack\Status\Cache; use Automattic\Jetpack\Status\Host; use WPCOM_Masterbar;
/** * Class Automattic\Jetpack\Status * * Used to retrieve information about the current status of Jetpack and the site overall. */ class Status { /** * Is Jetpack in offline mode? * * This was formerly called "Development Mode", but sites "in development" aren't always offline/localhost. * * @since 1.3.0 * * @return bool Whether Jetpack's offline mode is active. */ public function is_offline_mode() { $cached = Cache::get( 'is_offline_mode' ); if ( null !== $cached ) { return $cached; }
$offline_mode = false;
if ( defined( '\\JETPACK_DEV_DEBUG' ) ) { $offline_mode = constant( '\\JETPACK_DEV_DEBUG' ); } elseif ( defined( '\\WP_LOCAL_DEV' ) ) { $offline_mode = constant( '\\WP_LOCAL_DEV' ); } elseif ( $this->is_local_site() ) { $offline_mode = true; }
/** * Filters Jetpack's offline mode. * * @see https://jetpack.com/support/offline-mode/ * * @since 1.3.0 * * @param bool $offline_mode Is Jetpack's offline mode active. */ $offline_mode = (bool) apply_filters( 'jetpack_offline_mode', $offline_mode );
Cache::set( 'is_offline_mode', $offline_mode ); return $offline_mode; }
/** * Whether this is a system with a multiple networks. * Implemented since there is no core is_multi_network function. * Right now there is no way to tell which network is the dominant network on the system. * * @return boolean */ public function is_multi_network() { global $wpdb;
$cached = Cache::get( 'is_multi_network' ); if ( null !== $cached ) { return $cached; }
// If we don't have a multi site setup no need to do any more. if ( ! is_multisite() ) { Cache::set( 'is_multi_network', false ); return false; }
$num_sites = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->site}" ); if ( $num_sites > 1 ) { Cache::set( 'is_multi_network', true ); return true; }
Cache::set( 'is_multi_network', false ); return false; }
/** * Whether the current site is single user site. * * @return bool */ public function is_single_user_site() { global $wpdb;
$ret = Cache::get( 'is_single_user_site' ); if ( null === $ret ) { $some_users = get_transient( 'jetpack_is_single_user' ); if ( false === $some_users ) { $some_users = $wpdb->get_var( "SELECT COUNT(*) FROM (SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '{$wpdb->prefix}capabilities' LIMIT 2) AS someusers" ); set_transient( 'jetpack_is_single_user', (int) $some_users, 12 * HOUR_IN_SECONDS ); } $ret = 1 === (int) $some_users; Cache::set( 'is_single_user_site', $ret ); } return $ret; }
/** * If the site is a local site. * * @since 1.3.0 * * @return bool */ public function is_local_site() { $cached = Cache::get( 'is_local_site' ); if ( null !== $cached ) { return $cached; }
$site_url = site_url();
// Check for localhost and sites using an IP only first. // Note: str_contains() is not used here, as wp-includes/compat.php is not loaded in this file. $is_local = $site_url && false === strpos( $site_url, '.' );
// Use Core's environment check, if available. if ( 'local' === wp_get_environment_type() ) { $is_local = true; }
// Then check for usual usual domains used by local dev tools. $known_local = array( '#\.local$#i', '#\.localhost$#i', '#\.test$#i', '#\.docksal$#i', // Docksal. '#\.docksal\.site$#i', // Docksal. '#\.dev\.cc$#i', // ServerPress. '#\.lndo\.site$#i', // Lando. '#^https?://127\.0\.0\.1$#', );
if ( ! $is_local ) { foreach ( $known_local as $url ) { if ( preg_match( $url, $site_url ) ) { $is_local = true; break; } } }
/** * Filters is_local_site check. * * @since 1.3.0 * * @param bool $is_local If the current site is a local site. */ $is_local = apply_filters( 'jetpack_is_local_site', $is_local );
Cache::set( 'is_local_site', $is_local ); return $is_local; }
/** * If is a staging site. * * @deprecated since 3.3.0 * * @return bool */ public function is_staging_site() { _deprecated_function( __FUNCTION__, '3.3.0', 'in_safe_mode' ); $cached = Cache::get( 'is_staging_site' ); if ( null !== $cached ) { return $cached; }
/* * 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. */ $is_staging = ! in_array( wp_get_environment_type(), array( 'production', 'local' ), true );
$known_staging = array( 'urls' => array( '#\.staging\.wpengine\.com$#i', // WP Engine. This is their legacy staging URL structure. Their new platform does not have a common URL. https://github.com/Automattic/jetpack/issues/21504 '#\.staging\.kinsta\.com$#i', // Kinsta.com. '#\.kinsta\.cloud$#i', // Kinsta.com. '#\.stage\.site$#i', // DreamPress. '#\.newspackstaging\.com$#i', // Newspack. '#^(?!live-)([a-zA-Z0-9-]+)\.pantheonsite\.io$#i', // Pantheon. '#\.flywheelsites\.com$#i', // Flywheel. '#\.flywheelstaging\.com$#i', // Flywheel. '#\.cloudwaysapps\.com$#i', // Cloudways. '#\.azurewebsites\.net$#i', // Azure. '#\.wpserveur\.net$#i', // WPServeur. '#\-liquidwebsites\.com$#i', // Liquidweb. ), 'constants' => array( 'IS_WPE_SNAPSHOT', // WP Engine. This is used on their legacy staging environment. Their new platform does not have a constant. https://github.com/Automattic/jetpack/issues/21504 'KINSTA_DEV_ENV', // Kinsta.com. 'WPSTAGECOACH_STAGING', // WP Stagecoach. 'JETPACK_STAGING_MODE', // Generic. 'WP_LOCAL_DEV', // Generic. ), ); /** * Filters the flags of known staging sites. * * @since 1.1.1 * @since-jetpack 3.9.0 * * @param array $known_staging { * An array of arrays that each are used to check if the current site is staging. * @type array $urls URLs of staging sites in regex to check against site_url. * @type array $constants PHP constants of known staging/developement environments. * } */ $known_staging = apply_filters( 'jetpack_known_staging', $known_staging );
if ( isset( $known_staging['urls'] ) ) { $site_url = site_url(); foreach ( $known_staging['urls'] as $url ) { if ( preg_match( $url, wp_parse_url( $site_url, PHP_URL_HOST ) ) ) { $is_staging = true; break; } } }
if ( isset( $known_staging['constants'] ) ) { foreach ( $known_staging['constants'] as $constant ) { if ( defined( $constant ) && constant( $constant ) ) { $is_staging = true; } } }
// Last, let's check if sync is erroring due to an IDC. If so, set the site to staging mode. if ( ! $is_staging && method_exists( 'Automattic\\Jetpack\\Identity_Crisis', 'validate_sync_error_idc_option' ) && \Automattic\Jetpack\Identity_Crisis::validate_sync_error_idc_option() ) { $is_staging = true; }
/** * Filters is_staging_site check. * * @since 1.1.1 * @since-jetpack 3.9.0 * * @param bool $is_staging If the current site is a staging site. */ $is_staging = apply_filters( 'jetpack_is_staging_site', $is_staging );
Cache::set( 'is_staging_site', $is_staging ); return $is_staging; }
/** * If the site is in safe mode. * * @since 3.3.0 * * @return bool */ public function in_safe_mode() { $cached = Cache::get( 'in_safe_mode' ); if ( null !== $cached ) { return $cached; } $in_safe_mode = false; if ( method_exists( 'Automattic\\Jetpack\\Identity_Crisis', 'validate_sync_error_idc_option' ) && \Automattic\Jetpack\Identity_Crisis::validate_sync_error_idc_option() ) { $in_safe_mode = true; } /** * Filters in_safe_mode check. * * @since 3.3.0 * * @param bool $in_safe_mode If the current site is in safe mode. */ $in_safe_mode = apply_filters( 'jetpack_is_in_safe_mode', $in_safe_mode );
Cache::set( 'in_safe_mode', $in_safe_mode ); return $in_safe_mode; }
/** * If the site is a development/staging site. * This is a new version of is_staging_site added to separate safe mode from the legacy staging mode. * This method checks for core WP_ENVIRONMENT_TYPE setting * Using the jetpack_is_development_site filter. * * @since 3.3.0 * * @return bool */ public static function is_development_site() { $cached = Cache::get( 'is_development_site' ); if ( null !== $cached ) { return $cached; } $is_dev_site = ! in_array( wp_get_environment_type(), array( 'production', 'local' ), true ); /** * Filters is_development_site check. * * @since 3.3.0 * * @param bool $is_dev_site If the current site is a staging or dev site. */ $is_dev_site = apply_filters( 'jetpack_is_development_site', $is_dev_site );
Cache::set( 'is_development_site', $is_dev_site ); return $is_dev_site; }
/** * Whether the site is currently onboarding or not. * A site is considered as being onboarded if it currently has an onboarding token. * * @since-jetpack 5.8 * * @deprecated since 4.0.0 * * @access public * @static * * @return bool True if the site is currently onboarding, false otherwise */ public function is_onboarding() { return \Jetpack_Options::get_option( 'onboarding' ) !== false; }
/** * Whether the site is currently private or not. * On WordPress.com and WoA, sites can be marked as private * * @since 1.16.0 * * @return bool True if the site is private. */ public function is_private_site() { $ret = Cache::get( 'is_private_site' ); if ( null === $ret ) { $is_private_site = '-1' === get_option( 'blog_public' );
/** * Filters the is_private_site check. * * @since 1.16.1 * * @param bool $is_private_site True if the site is private. */ $is_private_site = apply_filters( 'jetpack_is_private_site', $is_private_site );
Cache::set( 'is_private_site', $is_private_site ); return $is_private_site; } return $ret; }
/** * Whether the site is currently unlaunched or not. * On WordPress.com and WoA, sites can be marked as "coming soon", aka unlaunched * * @since 1.16.0 * * @return bool True if the site is not launched. */ public function is_coming_soon() { $ret = Cache::get( 'is_coming_soon' ); if ( null === $ret ) { $is_coming_soon = (bool) ( function_exists( 'site_is_coming_soon' ) && \site_is_coming_soon() ) || get_option( 'wpcom_public_coming_soon' );
/** * Filters the is_coming_soon check. * * @since 1.16.1 * * @param bool $is_coming_soon True if the site is coming soon (i.e. unlaunched). */ $is_coming_soon = apply_filters( 'jetpack_is_coming_soon', $is_coming_soon );
Cache::set( 'is_coming_soon', $is_coming_soon ); return $is_coming_soon; } return $ret; }
/** * Returns the site slug suffix to be used as part of Calypso URLs. * * Strips http:// or https:// from a url, replaces forward slash with ::. * * @since 1.6.0 * * @param string $url Optional. URL to build the site suffix from. Default: Home URL. * * @return string */ public function get_site_suffix( $url = '' ) { // On WordPress.com, site suffixes are a bit different. if ( method_exists( 'WPCOM_Masterbar', 'get_calypso_site_slug' ) ) { return WPCOM_Masterbar::get_calypso_site_slug( get_current_blog_id() ); }
// Grab the 'site_url' option for WoA sites to avoid plugins to interfere with the site // identifier (e.g. i18n plugins may change the main url to '<DOMAIN>/<LOCALE>', but we // want to exclude the locale since it's not part of the site suffix). if ( ( new Host() )->is_woa_site() ) { $url = \site_url(); }
if ( empty( $url ) ) { // WordPress can be installed in subdirectories (e.g. make.wordpress.org/plugins) // where the 'site_url' option points to the root domain (e.g. make.wordpress.org) // which could collide with another site in the same domain but with WordPress // installed in a different subdirectory (e.g. make.wordpress.org/core). To avoid // such collision, we identify the site with the 'home_url' option. $url = \home_url(); }
$url = preg_replace( '#^.*?://#', '', $url ); $url = str_replace( '/', '::', $url );
return rtrim( $url, ':' ); } }
|