← All changes
|
jetpack_vendor/automattic/jetpack-status/src/class-status.php
+122
-56
13.0.2
→
16.3-beta
View file →
| @@ -17,20 +17,8 @@ | ||
| 17 | 17 | * Used to retrieve information about the current status of Jetpack and the site overall. |
| 18 | 18 | */ |
| 19 | 19 | class Status { |
| 20 | 20 | /** |
| 21 | - * Is Jetpack in development (offline) mode? | |
| 22 | - * | |
| 23 | - * @deprecated 1.3.0 Use Status->is_offline_mode(). | |
| 24 | - * | |
| 25 | - * @return bool Whether Jetpack's offline mode is active. | |
| 26 | - */ | |
| 27 | - public function is_development_mode() { | |
| 28 | - _deprecated_function( __FUNCTION__, '1.3.0', 'Automattic\Jetpack\Status->is_offline_mode' ); | |
| 29 | - return $this->is_offline_mode(); | |
| 30 | - } | |
| 31 | - | |
| 32 | - /** | |
| 33 | 21 | * Is Jetpack in offline mode? |
| 34 | 22 | * |
| 35 | 23 | * This was formerly called "Development Mode", but sites "in development" aren't always offline/localhost. |
| 36 | 24 | * |
| @@ -56,25 +44,10 @@ | ||
| 56 | 44 | |
| 57 | 45 | /** |
| 58 | 46 | * Filters Jetpack's offline mode. |
| 59 | 47 | * |
| 60 | - * @see https://jetpack.com/support/development-mode/ | |
| 61 | - * @todo Update documentation ^^. | |
| 48 | + * @see https://jetpack.com/support/offline-mode/ | |
| 62 | 49 | * |
| 63 | - * @since 1.1.1 | |
| 64 | - * @since-jetpack 2.2.1 | |
| 65 | - * @deprecated 1.3.0 | |
| 66 | - * | |
| 67 | - * @param bool $offline_mode Is Jetpack's offline mode active. | |
| 68 | - */ | |
| 69 | - $offline_mode = (bool) apply_filters_deprecated( 'jetpack_development_mode', array( $offline_mode ), '1.3.0', 'jetpack_offline_mode' ); | |
| 70 | - | |
| 71 | - /** | |
| 72 | - * Filters Jetpack's offline mode. | |
| 73 | - * | |
| 74 | - * @see https://jetpack.com/support/development-mode/ | |
| 75 | - * @todo Update documentation ^^. | |
| 76 | - * | |
| 77 | 50 | * @since 1.3.0 |
| 78 | 51 | * |
| 79 | 52 | * @param bool $offline_mode Is Jetpack's offline mode active. |
| 80 | 53 | */ |
| @@ -79,28 +52,30 @@ | ||
| 79 | 52 | * @param bool $offline_mode Is Jetpack's offline mode active. |
| 80 | 53 | */ |
| 81 | 54 | $offline_mode = (bool) apply_filters( 'jetpack_offline_mode', $offline_mode ); |
| 82 | 55 | |
| 56 | + if ( ! $offline_mode ) { | |
| 57 | + // Sentinel default so an absent option isn't confused with a stored null/false. | |
| 58 | + $sentinel = new \stdClass(); | |
| 59 | + $option = get_option( 'jetpack_offline_mode', $sentinel ); | |
| 60 | + | |
| 61 | + if ( $sentinel === $option ) { | |
| 62 | + // Seed an autoloaded default to stop per-request reads, but only where it helps | |
| 63 | + // (no persistent object cache) and writes are safe (keep front-end reads read-only). | |
| 64 | + if ( ! wp_using_ext_object_cache() && ( is_admin() || wp_doing_cron() || ( defined( 'WP_CLI' ) && WP_CLI ) ) ) { | |
| 65 | + add_option( 'jetpack_offline_mode', false, '', true ); | |
| 66 | + } | |
| 67 | + } else { | |
| 68 | + // A default_option filter could return a non-scalar; only a scalar is a real value. | |
| 69 | + $offline_mode = is_scalar( $option ) ? (bool) $option : false; | |
| 70 | + } | |
| 71 | + } | |
| 72 | + | |
| 83 | 73 | Cache::set( 'is_offline_mode', $offline_mode ); |
| 84 | 74 | return $offline_mode; |
| 85 | 75 | } |
| 86 | 76 | |
| 87 | 77 | /** |
| 88 | - * Is Jetpack in "No User test mode"? | |
| 89 | - * | |
| 90 | - * This will make Jetpack act as if there were no connected users, but only a site connection (aka blog token) | |
| 91 | - * | |
| 92 | - * @since 1.6.0 | |
| 93 | - * @deprecated 1.7.5 Since this version, Jetpack connection is considered active after registration, making no_user_testing_mode obsolete. | |
| 94 | - * | |
| 95 | - * @return bool Whether Jetpack's No User Testing Mode is active. | |
| 96 | - */ | |
| 97 | - public function is_no_user_testing_mode() { | |
| 98 | - _deprecated_function( __METHOD__, '1.7.5' ); | |
| 99 | - return true; | |
| 100 | - } | |
| 101 | - | |
| 102 | - /** | |
| 103 | 78 | * Whether this is a system with a multiple networks. |
| 104 | 79 | * Implemented since there is no core is_multi_network function. |
| 105 | 80 | * Right now there is no way to tell which network is the dominant network on the system. |
| 106 | 81 | * |
| @@ -163,34 +138,64 @@ | ||
| 163 | 138 | if ( null !== $cached ) { |
| 164 | 139 | return $cached; |
| 165 | 140 | } |
| 166 | 141 | |
| 167 | - $site_url = site_url(); | |
| 142 | + $site_url = trim( (string) site_url() ); | |
| 168 | 143 | |
| 169 | - // Check for localhost and sites using an IP only first. | |
| 170 | - // Note: str_contains() is not used here, as wp-includes/compat.php is not loaded in this file. | |
| 171 | - $is_local = $site_url && false === strpos( $site_url, '.' ); | |
| 144 | + /* | |
| 145 | + * Every check below cares about the host, not the rest of the URL. Matching the host | |
| 146 | + * alone means a port, a subdirectory install, or a trailing slash can't defeat them, | |
| 147 | + * and a known local domain sitting in the path can't trigger them by accident. | |
| 148 | + */ | |
| 149 | + $host = wp_parse_url( $site_url, PHP_URL_HOST ); | |
| 150 | + if ( ( ! is_string( $host ) || '' === $host ) && ! preg_match( '#^[a-z][a-z0-9+.\-]*:#i', $site_url ) ) { | |
| 151 | + /* | |
| 152 | + * No scheme, so site_url() isn't absolute. Read the value as a bare host. | |
| 153 | + * A value that does carry a scheme but still won't parse is malformed, and | |
| 154 | + * guessing at it would read "https:/example.com" as the host "https". | |
| 155 | + */ | |
| 156 | + $host = wp_parse_url( '//' . ltrim( $site_url, '/' ), PHP_URL_HOST ); | |
| 157 | + } | |
| 158 | + if ( ! is_string( $host ) ) { | |
| 159 | + /* | |
| 160 | + * Nothing host-shaped to test. Treat the site as remote rather than matching the | |
| 161 | + * raw URL: a false "local" silently drops a live site into offline mode. | |
| 162 | + */ | |
| 163 | + $host = ''; | |
| 164 | + } | |
| 172 | 165 | |
| 166 | + /* | |
| 167 | + * Check for localhost and sites using an IP only first. No dot means it can't be a | |
| 168 | + * public domain. Dotless IPv6 literals land here too, routable ones included: | |
| 169 | + * WordPress.com won't register an IPv6 site URL, so offline mode is where they belong. | |
| 170 | + * An IPv4-mapped literal like [::ffff:127.0.0.1] keeps its dots and reads as remote. | |
| 171 | + */ | |
| 172 | + $is_local = '' !== $host && false === strpos( $host, '.' ); | |
| 173 | + | |
| 173 | 174 | // Use Core's environment check, if available. |
| 174 | 175 | if ( 'local' === wp_get_environment_type() ) { |
| 175 | 176 | $is_local = true; |
| 176 | 177 | } |
| 177 | 178 | |
| 178 | - // Then check for usual usual domains used by local dev tools. | |
| 179 | + // Then check for usual domains used by local dev tools. | |
| 179 | 180 | $known_local = array( |
| 180 | 181 | '#\.local$#i', |
| 181 | - '#\.localhost$#i', | |
| 182 | + '#(^|\.)localhost$#i', // localhost and any subdomain of it. | |
| 182 | 183 | '#\.test$#i', |
| 183 | - '#\.docksal$#i', // Docksal. | |
| 184 | + '#\.docksal$#i', // Docksal. | |
| 184 | 185 | '#\.docksal\.site$#i', // Docksal. |
| 185 | 186 | '#\.dev\.cc$#i', // ServerPress. |
| 186 | 187 | '#\.lndo\.site$#i', // Lando. |
| 187 | - '#^https?://127\.0\.0\.1$#', | |
| 188 | + '#\.ddev\.site$#i', // DDEV. | |
| 189 | + // The whole 127.0.0.0/8 range is loopback; each octet is capped at 255. | |
| 190 | + '#^127\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){2}$#', | |
| 191 | + '#^0\.0\.0\.0$#', // All-interfaces bind, common under Docker and `wp server`. | |
| 192 | + '#^playground\.wordpress\.net$#i', // WordPress Playground, which runs entirely in the browser. | |
| 188 | 193 | ); |
| 189 | 194 | |
| 190 | - if ( ! $is_local ) { | |
| 191 | - foreach ( $known_local as $url ) { | |
| 192 | - if ( preg_match( $url, $site_url ) ) { | |
| 195 | + if ( ! $is_local && '' !== $host ) { | |
| 196 | + foreach ( $known_local as $pattern ) { | |
| 197 | + if ( preg_match( $pattern, $host ) ) { | |
| 193 | 198 | $is_local = true; |
| 194 | 199 | break; |
| 195 | 200 | } |
| 196 | 201 | } |
| @@ -211,13 +216,14 @@ | ||
| 211 | 216 | |
| 212 | 217 | /** |
| 213 | 218 | * If is a staging site. |
| 214 | 219 | * |
| 215 | - * @todo Add IDC detection to a package. | |
| 220 | + * @deprecated since 3.3.0 | |
| 216 | 221 | * |
| 217 | 222 | * @return bool |
| 218 | 223 | */ |
| 219 | 224 | public function is_staging_site() { |
| 225 | + _deprecated_function( __FUNCTION__, '3.3.0', 'in_safe_mode' ); | |
| 220 | 226 | $cached = Cache::get( 'is_staging_site' ); |
| 221 | 227 | if ( null !== $cached ) { |
| 222 | 228 | return $cached; |
| 223 | 229 | } |
| @@ -302,13 +308,73 @@ | ||
| 302 | 308 | return $is_staging; |
| 303 | 309 | } |
| 304 | 310 | |
| 305 | 311 | /** |
| 312 | + * If the site is in safe mode. | |
| 313 | + * | |
| 314 | + * @since 3.3.0 | |
| 315 | + * | |
| 316 | + * @return bool | |
| 317 | + */ | |
| 318 | + public function in_safe_mode() { | |
| 319 | + $cached = Cache::get( 'in_safe_mode' ); | |
| 320 | + if ( null !== $cached ) { | |
| 321 | + return $cached; | |
| 322 | + } | |
| 323 | + $in_safe_mode = false; | |
| 324 | + if ( method_exists( 'Automattic\\Jetpack\\Identity_Crisis', 'validate_sync_error_idc_option' ) && \Automattic\Jetpack\Identity_Crisis::validate_sync_error_idc_option() ) { | |
| 325 | + $in_safe_mode = true; | |
| 326 | + } | |
| 327 | + /** | |
| 328 | + * Filters in_safe_mode check. | |
| 329 | + * | |
| 330 | + * @since 3.3.0 | |
| 331 | + * | |
| 332 | + * @param bool $in_safe_mode If the current site is in safe mode. | |
| 333 | + */ | |
| 334 | + $in_safe_mode = apply_filters( 'jetpack_is_in_safe_mode', $in_safe_mode ); | |
| 335 | + | |
| 336 | + Cache::set( 'in_safe_mode', $in_safe_mode ); | |
| 337 | + return $in_safe_mode; | |
| 338 | + } | |
| 339 | + | |
| 340 | + /** | |
| 341 | + * If the site is a development/staging site. | |
| 342 | + * This is a new version of is_staging_site added to separate safe mode from the legacy staging mode. | |
| 343 | + * This method checks for core WP_ENVIRONMENT_TYPE setting | |
| 344 | + * Using the jetpack_is_development_site filter. | |
| 345 | + * | |
| 346 | + * @since 3.3.0 | |
| 347 | + * | |
| 348 | + * @return bool | |
| 349 | + */ | |
| 350 | + public static function is_development_site() { | |
| 351 | + $cached = Cache::get( 'is_development_site' ); | |
| 352 | + if ( null !== $cached ) { | |
| 353 | + return $cached; | |
| 354 | + } | |
| 355 | + $is_dev_site = ! in_array( wp_get_environment_type(), array( 'production', 'local' ), true ); | |
| 356 | + /** | |
| 357 | + * Filters is_development_site check. | |
| 358 | + * | |
| 359 | + * @since 3.3.0 | |
| 360 | + * | |
| 361 | + * @param bool $is_dev_site If the current site is a staging or dev site. | |
| 362 | + */ | |
| 363 | + $is_dev_site = apply_filters( 'jetpack_is_development_site', $is_dev_site ); | |
| 364 | + | |
| 365 | + Cache::set( 'is_development_site', $is_dev_site ); | |
| 366 | + return $is_dev_site; | |
| 367 | + } | |
| 368 | + | |
| 369 | + /** | |
| 306 | 370 | * Whether the site is currently onboarding or not. |
| 307 | 371 | * A site is considered as being onboarded if it currently has an onboarding token. |
| 308 | 372 | * |
| 309 | 373 | * @since-jetpack 5.8 |
| 310 | 374 | * |
| 375 | + * @deprecated since 4.0.0 | |
| 376 | + * | |
| 311 | 377 | * @access public |
| 312 | 378 | * @static |
| 313 | 379 | * |
| 314 | 380 | * @return bool True if the site is currently onboarding, false otherwise |
| @@ -355,9 +421,9 @@ | ||
| 355 | 421 | */ |
| 356 | 422 | public function is_coming_soon() { |
| 357 | 423 | $ret = Cache::get( 'is_coming_soon' ); |
| 358 | 424 | if ( null === $ret ) { |
| 359 | - $is_coming_soon = (bool) ( function_exists( 'site_is_coming_soon' ) && \site_is_coming_soon() ) | |
| 425 | + $is_coming_soon = ( function_exists( 'site_is_coming_soon' ) && \site_is_coming_soon() ) | |
| 360 | 426 | || get_option( 'wpcom_public_coming_soon' ); |
| 361 | 427 | |
| 362 | 428 | /** |
| 363 | 429 | * Filters the is_coming_soon check. |