Completed
Push — update/wp-55-autoupdate ( 3add47...edccb1 )
by
unknown
07:48
created

Status   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 138
Duplicated Lines 18.12 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 25
loc 138
rs 10
c 0
b 0
f 0
wmc 19
lcom 0
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A is_multi_network() 15 15 3
A is_single_user_site() 10 10 2
A is_development_mode() 0 23 3
C is_staging_site() 0 61 11

How to fix   Duplicated Code   

Duplicated Code

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
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
	 * @return bool Whether Jetpack's development mode is active.
20
	 */
21
	public function is_development_mode() {
22
		$development_mode = false;
23
		$site_url         = site_url();
24
25
		if ( defined( '\\JETPACK_DEV_DEBUG' ) ) {
26
			$development_mode = constant( '\\JETPACK_DEV_DEBUG' );
27
		} elseif ( $site_url ) {
28
			$development_mode = false === strpos( $site_url, '.' );
29
		}
30
31
		/**
32
		 * Filters Jetpack's development mode.
33
		 *
34
		 * @see https://jetpack.com/support/development-mode/
35
		 *
36
		 * @since 2.2.1
37
		 *
38
		 * @param bool $development_mode Is Jetpack's development mode active.
39
		 */
40
		$development_mode = (bool) apply_filters( 'jetpack_development_mode', $development_mode );
41
42
		return $development_mode;
43
	}
44
45
	/**
46
	 * Whether this is a system with a multiple networks.
47
	 * Implemented since there is no core is_multi_network function.
48
	 * Right now there is no way to tell which network is the dominant network on the system.
49
	 *
50
	 * @return boolean
51
	 */
52 View Code Duplication
	public function is_multi_network() {
53
		global $wpdb;
54
55
		// If we don't have a multi site setup no need to do any more.
56
		if ( ! is_multisite() ) {
57
			return false;
58
		}
59
60
		$num_sites = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->site}" );
61
		if ( $num_sites > 1 ) {
62
			return true;
63
		}
64
65
		return false;
66
	}
67
68
	/**
69
	 * Whether the current site is single user site.
70
	 *
71
	 * @return bool
72
	 */
73 View Code Duplication
	public function is_single_user_site() {
74
		global $wpdb;
75
76
		$some_users = get_transient( 'jetpack_is_single_user' );
77
		if ( false === $some_users ) {
78
			$some_users = $wpdb->get_var( "SELECT COUNT(*) FROM (SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '{$wpdb->prefix}capabilities' LIMIT 2) AS someusers" );
79
			set_transient( 'jetpack_is_single_user', (int) $some_users, 12 * HOUR_IN_SECONDS );
80
		}
81
		return 1 === (int) $some_users;
82
	}
83
84
	/**
85
	 * If is a staging site.
86
	 *
87
	 * @todo Add IDC detection to a package.
88
	 *
89
	 * @return bool
90
	 */
91
	public function is_staging_site() {
92
		$is_staging = false;
93
94
		$known_staging = array(
95
			'urls'      => array(
96
				'#\.staging\.wpengine\.com$#i', // WP Engine.
97
				'#\.staging\.kinsta\.com$#i',   // Kinsta.com.
98
				'#\.stage\.site$#i',            // DreamPress.
99
				'#\.newspackstaging\.com$#i',   // Newspack.
100
			),
101
			'constants' => array(
102
				'IS_WPE_SNAPSHOT',      // WP Engine.
103
				'KINSTA_DEV_ENV',       // Kinsta.com.
104
				'WPSTAGECOACH_STAGING', // WP Stagecoach.
105
				'JETPACK_STAGING_MODE', // Generic.
106
			),
107
		);
108
		/**
109
		 * Filters the flags of known staging sites.
110
		 *
111
		 * @since 3.9.0
112
		 *
113
		 * @param array $known_staging {
114
		 *     An array of arrays that each are used to check if the current site is staging.
115
		 *     @type array $urls      URLs of staging sites in regex to check against site_url.
116
		 *     @type array $constants PHP constants of known staging/developement environments.
117
		 *  }
118
		 */
119
		$known_staging = apply_filters( 'jetpack_known_staging', $known_staging );
120
121
		if ( isset( $known_staging['urls'] ) ) {
122
			foreach ( $known_staging['urls'] as $url ) {
123
				if ( preg_match( $url, site_url() ) ) {
124
					$is_staging = true;
125
					break;
126
				}
127
			}
128
		}
129
130
		if ( isset( $known_staging['constants'] ) ) {
131
			foreach ( $known_staging['constants'] as $constant ) {
132
				if ( defined( $constant ) && constant( $constant ) ) {
133
					$is_staging = true;
134
				}
135
			}
136
		}
137
138
		// Last, let's check if sync is erroring due to an IDC. If so, set the site to staging mode.
139
		if ( ! $is_staging && method_exists( 'Jetpack', 'validate_sync_error_idc_option' ) && \Jetpack::validate_sync_error_idc_option() ) {
140
			$is_staging = true;
141
		}
142
143
		/**
144
		 * Filters is_staging_site check.
145
		 *
146
		 * @since 3.9.0
147
		 *
148
		 * @param bool $is_staging If the current site is a staging site.
149
		 */
150
		return apply_filters( 'jetpack_is_staging_site', $is_staging );
151
	}
152
}
153