Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 17 | class Status { |
||
| 18 | /** |
||
| 19 | * Is Jetpack in development (offline) mode? |
||
| 20 | * |
||
| 21 | * @deprecated 8.8.0 Use Status->is_offline_mode(). |
||
| 22 | * |
||
| 23 | * @return bool Whether Jetpack's offline mode is active. |
||
| 24 | */ |
||
| 25 | public function is_development_mode() { |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Is Jetpack in offline mode? |
||
| 32 | * |
||
| 33 | * This was formerly called "Development Mode", but sites "in development" aren't always offline/localhost. |
||
| 34 | * |
||
| 35 | * @since 8.8.0 |
||
| 36 | * |
||
| 37 | * @return bool Whether Jetpack's offline mode is active. |
||
| 38 | */ |
||
| 39 | public function is_offline_mode() { |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Is Jetpack in "No User test mode"? |
||
| 80 | * |
||
| 81 | * This will make Jetpack act as if there were no connected users, but only a site connection (aka blog token) |
||
| 82 | * |
||
| 83 | * @since 9.2.0 |
||
| 84 | * |
||
| 85 | * @return bool Whether Jetpack's No User Testing Mode is active. |
||
| 86 | */ |
||
| 87 | public function is_no_user_testing_mode() { |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Whether this is a system with a multiple networks. |
||
| 108 | * Implemented since there is no core is_multi_network function. |
||
| 109 | * Right now there is no way to tell which network is the dominant network on the system. |
||
| 110 | * |
||
| 111 | * @return boolean |
||
| 112 | */ |
||
| 113 | View Code Duplication | public function is_multi_network() { |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Whether the current site is single user site. |
||
| 131 | * |
||
| 132 | * @return bool |
||
| 133 | */ |
||
| 134 | View Code Duplication | public function is_single_user_site() { |
|
| 144 | |||
| 145 | /** |
||
| 146 | * If the site is a local site. |
||
| 147 | * |
||
| 148 | * @since 8.8.0 |
||
| 149 | * |
||
| 150 | * @return bool |
||
| 151 | */ |
||
| 152 | public function is_local_site() { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * If is a staging site. |
||
| 193 | * |
||
| 194 | * @todo Add IDC detection to a package. |
||
| 195 | * |
||
| 196 | * @return bool |
||
| 197 | */ |
||
| 198 | public function is_staging_site() { |
||
| 199 | // 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. |
||
| 200 | $is_staging = ! in_array( \wp_get_environment_type(), array( 'production', 'local' ), true ); |
||
| 201 | |||
| 202 | $known_staging = array( |
||
| 203 | 'urls' => array( |
||
| 204 | '#\.staging\.wpengine\.com$#i', // WP Engine. |
||
| 205 | '#\.staging\.kinsta\.com$#i', // Kinsta.com. |
||
| 206 | '#\.kinsta\.cloud$#i', // Kinsta.com. |
||
| 207 | '#\.stage\.site$#i', // DreamPress. |
||
| 208 | '#\.newspackstaging\.com$#i', // Newspack. |
||
| 209 | '#\.pantheonsite\.io$#i', // Pantheon. |
||
| 210 | '#\.flywheelsites\.com$#i', // Flywheel. |
||
| 211 | '#\.flywheelstaging\.com$#i', // Flywheel. |
||
| 212 | '#\.cloudwaysapps\.com$#i', // Cloudways. |
||
| 213 | '#\.azurewebsites\.net$#i', // Azure. |
||
| 214 | '#\.wpserveur\.net$#i', // WPServeur. |
||
| 215 | '#\-liquidwebsites\.com$#i', // Liquidweb. |
||
| 216 | ), |
||
| 217 | 'constants' => array( |
||
| 218 | 'IS_WPE_SNAPSHOT', // WP Engine. |
||
| 219 | 'KINSTA_DEV_ENV', // Kinsta.com. |
||
| 220 | 'WPSTAGECOACH_STAGING', // WP Stagecoach. |
||
| 221 | 'JETPACK_STAGING_MODE', // Generic. |
||
| 222 | 'WP_LOCAL_DEV', // Generic. |
||
| 223 | ), |
||
| 224 | ); |
||
| 225 | /** |
||
| 226 | * Filters the flags of known staging sites. |
||
| 227 | * |
||
| 228 | * @since 3.9.0 |
||
| 229 | * |
||
| 230 | * @param array $known_staging { |
||
| 231 | * An array of arrays that each are used to check if the current site is staging. |
||
| 232 | * @type array $urls URLs of staging sites in regex to check against site_url. |
||
| 233 | * @type array $constants PHP constants of known staging/developement environments. |
||
| 234 | * } |
||
| 235 | */ |
||
| 236 | $known_staging = apply_filters( 'jetpack_known_staging', $known_staging ); |
||
| 237 | |||
| 238 | View Code Duplication | if ( isset( $known_staging['urls'] ) ) { |
|
| 239 | foreach ( $known_staging['urls'] as $url ) { |
||
| 240 | if ( preg_match( $url, wp_parse_url( site_url(), PHP_URL_HOST ) ) ) { |
||
| 241 | $is_staging = true; |
||
| 242 | break; |
||
| 243 | } |
||
| 244 | } |
||
| 245 | } |
||
| 246 | |||
| 247 | if ( isset( $known_staging['constants'] ) ) { |
||
| 248 | foreach ( $known_staging['constants'] as $constant ) { |
||
| 249 | if ( defined( $constant ) && constant( $constant ) ) { |
||
| 250 | $is_staging = true; |
||
| 251 | } |
||
| 252 | } |
||
| 253 | } |
||
| 254 | |||
| 255 | // Last, let's check if sync is erroring due to an IDC. If so, set the site to staging mode. |
||
| 256 | if ( ! $is_staging && method_exists( 'Jetpack', 'validate_sync_error_idc_option' ) && \Jetpack::validate_sync_error_idc_option() ) { |
||
| 257 | $is_staging = true; |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Filters is_staging_site check. |
||
| 262 | * |
||
| 263 | * @since 3.9.0 |
||
| 264 | * |
||
| 265 | * @param bool $is_staging If the current site is a staging site. |
||
| 266 | */ |
||
| 267 | return apply_filters( 'jetpack_is_staging_site', $is_staging ); |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Returns the site slug suffix to be used as part of Calypso URLs. |
||
| 272 | * |
||
| 273 | * Strips http:// or https:// from a url, replaces forward slash with ::. |
||
| 274 | * |
||
| 275 | * @since 9.2.0 |
||
| 276 | * |
||
| 277 | * @param string $url Optional. URL to build the site suffix from. Default: Home URL. |
||
| 278 | * |
||
| 279 | * @return string |
||
| 280 | */ |
||
| 281 | public function get_site_suffix( $url = '' ) { |
||
| 296 | } |
||
| 297 |