Completed
Push — update/remove-disconnect-link ( 4b6a2c )
by
unknown
73:18 queued 63:47
created

Jetpack_Sync_Functions   B

Complexity

Total Complexity 49

Size/Duplication

Total Lines 229
Duplicated Lines 3.49 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 8
loc 229
rs 8.5454
c 0
b 0
f 0
wmc 49
lcom 1
cbo 1

19 Methods

Rating   Name   Duplication   Size   Complexity  
A get_modules() 0 5 1
A get_taxonomies() 0 13 3
A get_shortcodes() 0 4 1
C sanitize_taxonomy() 0 26 8
A get_post_types() 0 5 1
A get_post_type_features() 0 5 1
B get_hosting_provider() 0 15 7
A rest_api_allowed_post_types() 0 4 1
A rest_api_allowed_public_metadata() 0 4 1
A is_version_controlled() 0 9 2
A file_system_write_access() 0 21 4
A home_url() 0 6 1
A site_url() 0 6 1
A main_network_site_url() 0 3 1
A get_protocol_normalized_url() 0 21 3
C normalize_www_in_url() 8 28 7
A get_plugins() 0 8 2
A wp_version() 0 5 1
A site_icon_url() 0 7 3

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Jetpack_Sync_Functions often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Jetpack_Sync_Functions, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/*
4
 * Utility functions to generate data synced to wpcom
5
 */
6
7
class Jetpack_Sync_Functions {
8
	const HTTPS_CHECK_OPTION_PREFIX = 'jetpack_sync_https_history_';
9
	const HTTPS_CHECK_HISTORY = 5;
10
11
	public static function get_modules() {
12
		require_once( JETPACK__PLUGIN_DIR . 'class.jetpack-admin.php' );
13
14
		return Jetpack_Admin::init()->get_modules();
15
	}
16
17
	public static function get_taxonomies() {
18
		global $wp_taxonomies;
19
		$wp_taxonomies_without_callbacks = array();
20
		foreach ( $wp_taxonomies as $taxonomy_name => $taxonomy ) {
21
			$sanitized_taxonomy = self::sanitize_taxonomy( $taxonomy );
22
			if ( ! empty( $sanitized_taxonomy ) ) {
23
				$wp_taxonomies_without_callbacks[ $taxonomy_name ] = $sanitized_taxonomy;
24
	 		} else {
25
				error_log( 'Jetpack: Encountered a recusive taxonomy:' . $taxonomy_name );
26
			}
27
		}
28
		return $wp_taxonomies_without_callbacks;
29
	}
30
31
	public static function get_shortcodes() {
32
		global $shortcode_tags;
33
		return array_keys( $shortcode_tags );
34
	}
35
36
	/**
37
	 * Removes any callback data since we will not be able to process it on our side anyways.
38
	 */
39
	public static function sanitize_taxonomy( $taxonomy ) {
40
41
		// Lets clone the taxonomy object instead of modifing the global one.
42
		$cloned_taxonomy = json_decode( wp_json_encode( $taxonomy ) );
43
44
		// recursive taxonomies are no fun.
45
		if ( is_null( $cloned_taxonomy ) ) {
46
			return null;
47
		}
48
		// Remove any meta_box_cb if they are not the default wp ones.
49
		if ( isset( $cloned_taxonomy->meta_box_cb ) &&
50
		     ! in_array( $cloned_taxonomy->meta_box_cb, array( 'post_tags_meta_box', 'post_categories_meta_box' ) ) ) {
51
			$cloned_taxonomy->meta_box_cb = null;
52
		}
53
		// Remove update call back
54
		if ( isset( $cloned_taxonomy->update_count_callback ) &&
55
		     ! is_null( $cloned_taxonomy->update_count_callback ) ) {
56
			$cloned_taxonomy->update_count_callback = null;
57
		}
58
		// Remove rest_controller_class if it something other then the default.
59
		if ( isset( $cloned_taxonomy->rest_controller_class )  &&
60
		     'WP_REST_Terms_Controller' !== $cloned_taxonomy->rest_controller_class ) {
61
			$cloned_taxonomy->rest_controller_class = null;
62
		}
63
		return $cloned_taxonomy;
64
	}
65
66
	public static function get_post_types() {
67
		global $wp_post_types;
68
69
		return $wp_post_types;
70
	}
71
72
	public static function get_post_type_features() {
73
		global $_wp_post_type_features;
74
75
		return $_wp_post_type_features;
76
	}
77
78
	public static function get_hosting_provider() {
79
		if ( defined( 'GD_SYSTEM_PLUGIN_DIR' ) || class_exists( '\\WPaaS\\Plugin' ) ) {
80
			return 'gd-managed-wp';
81
		}
82
		if ( defined( 'MM_BASE_DIR' ) ) {
83
			return 'bh';
84
		} 
85
		if ( defined( 'IS_PRESSABLE' ) ) {
86
			return 'pressable';
87
		} 
88
		if ( function_exists( 'is_wpe' ) || function_exists( 'is_wpe_snapshot' ) ) {
89
			return 'wpe';
90
		}
91
		return 'unknown';
92
	}
93
94
	public static function rest_api_allowed_post_types() {
95
		/** This filter is already documented in class.json-api-endpoints.php */
96
		return apply_filters( 'rest_api_allowed_post_types', array( 'post', 'page', 'revision' ) );
97
	}
98
99
	public static function rest_api_allowed_public_metadata() {
100
		/** This filter is documented in json-endpoints/class.wpcom-json-api-post-endpoint.php */
101
		return apply_filters( 'rest_api_allowed_public_metadata', array() );
102
	}
103
104
	/**
105
	 * Finds out if a site is using a version control system.
106
	 * @return bool
107
	 **/
108
	public static function is_version_controlled() {
109
110
		if ( ! class_exists( 'WP_Automatic_Updater' ) ) {
111
			require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );
112
		}
113
		$updater = new WP_Automatic_Updater();
114
115
		return (bool) strval( $updater->is_vcs_checkout( $context = ABSPATH ) );
116
	}
117
118
	/**
119
	 * Returns true if the site has file write access false otherwise.
120
	 * @return bool
121
	 **/
122
	public static function file_system_write_access() {
123
		if ( ! function_exists( 'get_filesystem_method' ) ) {
124
			require_once( ABSPATH . 'wp-admin/includes/file.php' );
125
		}
126
127
		require_once( ABSPATH . 'wp-admin/includes/template.php' );
128
129
		$filesystem_method = get_filesystem_method();
130
		if ( 'direct' === $filesystem_method  ) {
131
			return true;
132
		}
133
134
		ob_start();
135
		$filesystem_credentials_are_stored = request_filesystem_credentials( self_admin_url() );
136
		ob_end_clean();
137
		if ( $filesystem_credentials_are_stored ) {
138
			return true;
139
		}
140
141
		return false;
142
	}
143
144
	public static function home_url() {
145
		return self::get_protocol_normalized_url(
146
			'home_url',
147
			self::normalize_www_in_url( 'home', 'home_url' )
148
		);
149
	}
150
151
	public static function site_url() {
152
		return self::get_protocol_normalized_url(
153
			'site_url',
154
			self::normalize_www_in_url( 'siteurl', 'site_url' )
155
		);
156
	}
157
158
	public static function main_network_site_url() {
159
		return self::get_protocol_normalized_url( 'main_network_site_url', network_site_url() );
160
	}
161
162
	public static function get_protocol_normalized_url( $callable, $new_value ) {
163
		$option_key = self::HTTPS_CHECK_OPTION_PREFIX . $callable;
164
165
		$parsed_url = wp_parse_url( $new_value );
166
		if ( ! $parsed_url ) {
167
			return $new_value;
168
		}
169
170
		$scheme = $parsed_url['scheme'];
171
		$scheme_history = get_option( $option_key, array() );
172
		$scheme_history[] = $scheme;
173
174
		// Limit length to self::HTTPS_CHECK_HISTORY
175
		$scheme_history = array_slice( $scheme_history, ( self::HTTPS_CHECK_HISTORY * -1 ) );
176
177
		update_option( $option_key, $scheme_history );
178
179
		$forced_scheme =  in_array( 'https', $scheme_history ) ? 'https' : 'http';
180
181
		return set_url_scheme( $new_value, $forced_scheme );
182
	}
183
184
	public static function normalize_www_in_url( $option, $url_function ) {
185
		$url        = wp_parse_url( call_user_func( $url_function ) );
186
		$option_url = wp_parse_url( get_option( $option ) );
187
188
		if ( ! $option_url || ! $url ) {
189
			return $url;
190
		}
191
192 View Code Duplication
		if ( $url[ 'host' ] === "www.{$option_url[ 'host' ]}" ) {
193
			// remove www if not present in option URL
194
			$url[ 'host' ] = $option_url[ 'host' ];
195
		}
196 View Code Duplication
		if ( $option_url[ 'host' ] === "www.{$url[ 'host' ]}" ) {
197
			// add www if present in option URL
198
			$url[ 'host' ] = $option_url[ 'host' ];
199
		}
200
201
		$normalized_url = "{$url['scheme']}://{$url['host']}";
202
		if ( isset( $url['path'] ) ) {
203
			$normalized_url .= "{$url['path']}";
204
		}
205
206
		if ( isset( $url['query'] ) ) {
207
			$normalized_url .= "?{$url['query']}";
208
		}
209
210
		return $normalized_url;
211
	}
212
213
	public static function get_plugins() {
214
		if ( ! function_exists( 'get_plugins' ) ) {
215
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
216
		}
217
218
		/** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */
219
		return apply_filters( 'all_plugins', get_plugins() );
220
	}
221
222
	public static function wp_version() {
223
		global $wp_version;
224
225
		return $wp_version;
226
	}
227
228
	public static function site_icon_url() {
229
		if ( ! function_exists( 'get_site_icon_url' ) || ! has_site_icon() ) {
230
			return get_option( 'jetpack_site_icon_url' );
231
		}
232
233
		return get_site_icon_url();
234
	}
235
}
236