Completed
Push — add/connect-splash-content ( ec1611...3bc2bd )
by
unknown
22:10 queued 11:17
created

Jetpack_Sync_Functions   C

Complexity

Total Complexity 73

Size/Duplication

Total Lines 346
Duplicated Lines 2.31 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 8
loc 346
rs 5.5447
c 0
b 0
f 0
wmc 73
lcom 1
cbo 4

25 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 14 3
A sanitize_post_type() 0 10 3
A expand_synced_post_type() 0 9 1
A get_post_type_features() 0 5 1
B get_hosting_provider() 0 18 9
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
B get_raw_or_filtered_url() 0 18 5
A home_url() 0 12 1
A site_url() 0 12 1
A main_network_site_url() 0 3 1
B get_protocol_normalized_url() 0 24 4
A get_raw_url() 0 18 4
C normalize_www_in_url() 8 28 7
A get_plugins() 0 8 2
B get_plugins_action_links() 0 14 5
A wp_version() 0 4 1
A site_icon_url() 0 7 3
A roles() 0 4 1

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
		$post_types_without_callbacks = array();
70
		foreach ( $wp_post_types as $post_type_name => $post_type ) {
71
			$sanitized_post_type = self::sanitize_post_type( $post_type );
72
			if ( ! empty( $sanitized_post_type ) ) {
73
				$post_types_without_callbacks[ $post_type_name ] = $sanitized_post_type;
74
			} else {
75
				error_log( 'Jetpack: Encountered a recusive post_type:' . $post_type_name );
76
			}
77
		}
78
		return $post_types_without_callbacks;
79
	}
80
81
	public static function sanitize_post_type( $post_type ) {
82
		// Lets clone the post type object instead of modifing the global one.
83
		$sanitized_post_type = array();
84
		foreach ( Jetpack_Sync_Defaults::$default_post_type_attributes as $attribute_key => $default_value ) {
0 ignored issues
show
Bug introduced by
The property default_post_type_attributes cannot be accessed from this context as it is declared private in class Jetpack_Sync_Defaults.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
85
			if ( isset( $post_type->{ $attribute_key } ) ) {
86
				$sanitized_post_type[ $attribute_key ] = $post_type->{ $attribute_key };
87
			}
88
		}
89
		return (object) $sanitized_post_type;
90
	}
91
92
	public static function expand_synced_post_type( $sanitized_post_type, $post_type ) {
93
		$post_type = sanitize_key( $post_type );
94
		$post_type_object = new WP_Post_Type( $post_type, $sanitized_post_type );
95
		$post_type_object->add_supports();
96
		$post_type_object->add_rewrite_rules();
97
		$post_type_object->add_hooks();
98
		$post_type_object->register_taxonomies();
99
		return (object) $post_type_object;
100
	}
101
102
	public static function get_post_type_features() {
103
		global $_wp_post_type_features;
104
105
		return $_wp_post_type_features;
106
	}
107
108
	public static function get_hosting_provider() {
109
		if ( defined( 'GD_SYSTEM_PLUGIN_DIR' ) || class_exists( '\\WPaaS\\Plugin' ) ) {
110
			return 'gd-managed-wp';
111
		}
112
		if ( defined( 'MM_BASE_DIR' ) ) {
113
			return 'bh';
114
		}
115
		if ( defined( 'IS_PRESSABLE' ) ) {
116
			return 'pressable';
117
		}
118
		if ( function_exists( 'is_wpe' ) || function_exists( 'is_wpe_snapshot' ) ) {
119
			return 'wpe';
120
		}
121
		if ( defined( 'VIP_GO_ENV' ) && false !== VIP_GO_ENV ) {
122
			return 'vip-go';
123
		}
124
		return 'unknown';
125
	}
126
127
	public static function rest_api_allowed_post_types() {
128
		/** This filter is already documented in class.json-api-endpoints.php */
129
		return apply_filters( 'rest_api_allowed_post_types', array( 'post', 'page', 'revision' ) );
130
	}
131
132
	public static function rest_api_allowed_public_metadata() {
133
		/** This filter is documented in json-endpoints/class.wpcom-json-api-post-endpoint.php */
134
		return apply_filters( 'rest_api_allowed_public_metadata', array() );
135
	}
136
137
	/**
138
	 * Finds out if a site is using a version control system.
139
	 * @return bool
140
	 **/
141
	public static function is_version_controlled() {
142
143
		if ( ! class_exists( 'WP_Automatic_Updater' ) ) {
144
			require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );
145
		}
146
		$updater = new WP_Automatic_Updater();
147
148
		return (bool) strval( $updater->is_vcs_checkout( $context = ABSPATH ) );
149
	}
150
151
	/**
152
	 * Returns true if the site has file write access false otherwise.
153
	 * @return bool
154
	 **/
155
	public static function file_system_write_access() {
156
		if ( ! function_exists( 'get_filesystem_method' ) ) {
157
			require_once( ABSPATH . 'wp-admin/includes/file.php' );
158
		}
159
160
		require_once( ABSPATH . 'wp-admin/includes/template.php' );
161
162
		$filesystem_method = get_filesystem_method();
163
		if ( 'direct' === $filesystem_method  ) {
164
			return true;
165
		}
166
167
		ob_start();
168
		$filesystem_credentials_are_stored = request_filesystem_credentials( self_admin_url() );
169
		ob_end_clean();
170
		if ( $filesystem_credentials_are_stored ) {
171
			return true;
172
		}
173
174
		return false;
175
	}
176
177
	/**
178
	 * Helper function that is used when getting home or siteurl values. Decides
179
	 * whether to get the raw or filtered value.
180
	 *
181
	 * @return string
182
	 */
183
	public static function get_raw_or_filtered_url( $url_type ) {
184
		$url_function = ( 'home' == $url_type )
185
			? 'home_url'
186
			: 'site_url';
187
188
		if (
189
			! Jetpack_Constants::is_defined( 'JETPACK_SYNC_USE_RAW_URL' ) ||
190
			Jetpack_Constants::get_constant( 'JETPACK_SYNC_USE_RAW_URL' )
191
		) {
192
			$scheme = is_ssl() ? 'https' : 'http';
193
			$url = self::get_raw_url( $url_type );
194
			$url = set_url_scheme( $url, $scheme );
195
		} else {
196
			$url = self::normalize_www_in_url( $url_type, $url_function );
197
		}
198
199
		return self::get_protocol_normalized_url( $url_function, $url );
200
	}
201
202
	public static function home_url() {
203
		$url = self::get_raw_or_filtered_url( 'home' );
204
205
		/**
206
		 * Allows overriding of the home_url value that is synced back to WordPress.com.
207
		 *
208
		 * @since 5.2
209
		 *
210
		 * @param string $home_url
211
		 */
212
		return esc_url_raw( apply_filters( 'jetpack_sync_home_url', $url ) );
213
	}
214
215
	public static function site_url() {
216
		$url = self::get_raw_or_filtered_url( 'siteurl' );
217
218
		/**
219
		 * Allows overriding of the site_url value that is synced back to WordPress.com.
220
		 *
221
		 * @since 5.2
222
		 *
223
		 * @param string $site_url
224
		 */
225
		return esc_url_raw( apply_filters( 'jetpack_sync_site_url', $url ) );
226
	}
227
228
	public static function main_network_site_url() {
229
		return self::get_protocol_normalized_url( 'main_network_site_url', network_site_url() );
230
	}
231
232
	public static function get_protocol_normalized_url( $callable, $new_value ) {
233
		$option_key = self::HTTPS_CHECK_OPTION_PREFIX . $callable;
234
235
		$parsed_url = wp_parse_url( $new_value );
236
		if ( ! $parsed_url ) {
237
			return $new_value;
238
		}
239
		if ( array_key_exists ( 'scheme' , $parsed_url ) ) {
240
			$scheme = $parsed_url['scheme'];
241
		} else {
242
			$scheme = '';
243
		}
244
		$scheme_history = get_option( $option_key, array() );
245
		$scheme_history[] = $scheme;
246
247
		// Limit length to self::HTTPS_CHECK_HISTORY
248
		$scheme_history = array_slice( $scheme_history, ( self::HTTPS_CHECK_HISTORY * -1 ) );
249
250
		update_option( $option_key, $scheme_history );
251
252
		$forced_scheme =  in_array( 'https', $scheme_history ) ? 'https' : 'http';
253
254
		return set_url_scheme( $new_value, $forced_scheme );
255
	}
256
257
	public static function get_raw_url( $option_name ) {
258
		$value = null;
0 ignored issues
show
Unused Code introduced by
$value is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
259
		$constant = ( 'home' == $option_name )
260
			? 'WP_HOME'
261
			: 'WP_SITEURL';
262
263
		// Since we disregard the constant for multisites in ms-default-filters.php,
264
		// let's also use the db value if this is a multisite.
265
		if ( ! is_multisite() && Jetpack_Constants::is_defined( $constant ) ) {
266
			$value = Jetpack_Constants::get_constant( $constant );
267
		} else {
268
			// Let's get the option from the database so that we can bypass filters. This will help
269
			// ensure that we get more uniform values.
270
			$value = Jetpack_Options::get_raw_option( $option_name );
271
		}
272
273
		return $value;
274
	}
275
276
	public static function normalize_www_in_url( $option, $url_function ) {
277
		$url        = wp_parse_url( call_user_func( $url_function ) );
278
		$option_url = wp_parse_url( get_option( $option ) );
279
280
		if ( ! $option_url || ! $url ) {
281
			return $url;
282
		}
283
284 View Code Duplication
		if ( $url[ 'host' ] === "www.{$option_url[ 'host' ]}" ) {
285
			// remove www if not present in option URL
286
			$url[ 'host' ] = $option_url[ 'host' ];
287
		}
288 View Code Duplication
		if ( $option_url[ 'host' ] === "www.{$url[ 'host' ]}" ) {
289
			// add www if present in option URL
290
			$url[ 'host' ] = $option_url[ 'host' ];
291
		}
292
293
		$normalized_url = "{$url['scheme']}://{$url['host']}";
294
		if ( isset( $url['path'] ) ) {
295
			$normalized_url .= "{$url['path']}";
296
		}
297
298
		if ( isset( $url['query'] ) ) {
299
			$normalized_url .= "?{$url['query']}";
300
		}
301
302
		return $normalized_url;
303
	}
304
305
	public static function get_plugins() {
306
		if ( ! function_exists( 'get_plugins' ) ) {
307
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
308
		}
309
310
		/** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */
311
		return apply_filters( 'all_plugins', get_plugins() );
312
	}
313
314
	/**
315
	 * Get custom action link tags that the plugin is using
316
	 * Ref: https://codex.wordpress.org/Plugin_API/Filter_Reference/plugin_action_links_(plugin_file_name)
317
	 * @return array of plugin action links (key: link name value: url)
318
	 */
319
	public static function get_plugins_action_links( $plugin_file_singular = null ) {
320
		// Some sites may have DOM disabled in PHP fail early
321
		if ( ! class_exists( 'DOMDocument' ) ) {
322
			return array();
323
		}
324
		$plugins_action_links = get_option( 'jetpack_plugin_api_action_links', array() );
325
		if ( ! empty( $plugins_action_links ) ) {
326
			if ( is_null( $plugin_file_singular ) ) {
327
				return $plugins_action_links;
328
			}
329
			return ( isset( $plugins_action_links[ $plugin_file_singular ] ) ? $plugins_action_links[ $plugin_file_singular ] : null );
330
		}
331
		return array();
332
	}
333
334
	public static function wp_version() {
335
		global $wp_version;
336
		return $wp_version;
337
	}
338
339
	public static function site_icon_url() {
340
		if ( ! function_exists( 'get_site_icon_url' ) || ! has_site_icon() ) {
341
			return get_option( 'jetpack_site_icon_url' );
342
		}
343
344
		return get_site_icon_url();
345
	}
346
347
	public static function roles() {
348
		$wp_roles = wp_roles();
349
		return $wp_roles->roles;
350
	}
351
352
}
353