Completed
Push — add/carousel-lightbox-single-i... ( 204ac6...43c884 )
by
unknown
09:26
created

Jetpack_Sync_Functions   B

Complexity

Total Complexity 38

Size/Duplication

Total Lines 186
Duplicated Lines 4.3 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 8
loc 186
rs 8.3999
c 0
b 0
f 0
wmc 38
lcom 1
cbo 1

17 Methods

Rating   Name   Duplication   Size   Complexity  
A get_modules() 0 5 1
A get_taxonomies() 0 5 1
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   

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
/*
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
20
		return $wp_taxonomies;
21
	}
22
23
	public static function get_post_types() {
24
		global $wp_post_types;
25
26
		return $wp_post_types;
27
	}
28
29
	public static function get_post_type_features() {
30
		global $_wp_post_type_features;
31
32
		return $_wp_post_type_features;
33
	}
34
35
	public static function get_hosting_provider() {
36
		if ( defined( 'GD_SYSTEM_PLUGIN_DIR' ) || class_exists( '\\WPaaS\\Plugin' ) ) {
37
			return 'gd-managed-wp';
38
		}
39
		if ( defined( 'MM_BASE_DIR' ) ) {
40
			return 'bh';
41
		} 
42
		if ( defined( 'IS_PRESSABLE' ) ) {
43
			return 'pressable';
44
		} 
45
		if ( function_exists( 'is_wpe' ) || function_exists( 'is_wpe_snapshot' ) ) {
46
			return 'wpe';
47
		}
48
		return 'unknown';
49
	}
50
51
	public static function rest_api_allowed_post_types() {
52
		/** This filter is already documented in class.json-api-endpoints.php */
53
		return apply_filters( 'rest_api_allowed_post_types', array( 'post', 'page', 'revision' ) );
54
	}
55
56
	public static function rest_api_allowed_public_metadata() {
57
		/** This filter is documented in json-endpoints/class.wpcom-json-api-post-endpoint.php */
58
		return apply_filters( 'rest_api_allowed_public_metadata', array() );
59
	}
60
61
	/**
62
	 * Finds out if a site is using a version control system.
63
	 * @return bool
64
	 **/
65
	public static function is_version_controlled() {
66
67
		if ( ! class_exists( 'WP_Automatic_Updater' ) ) {
68
			require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );
69
		}
70
		$updater = new WP_Automatic_Updater();
71
72
		return (bool) strval( $updater->is_vcs_checkout( $context = ABSPATH ) );
73
	}
74
75
	/**
76
	 * Returns true if the site has file write access false otherwise.
77
	 * @return bool
78
	 **/
79
	public static function file_system_write_access() {
80
		if ( ! function_exists( 'get_filesystem_method' ) ) {
81
			require_once( ABSPATH . 'wp-admin/includes/file.php' );
82
		}
83
84
		require_once( ABSPATH . 'wp-admin/includes/template.php' );
85
86
		$filesystem_method = get_filesystem_method();
87
		if ( 'direct' === $filesystem_method  ) {
88
			return true;
89
		}
90
91
		ob_start();
92
		$filesystem_credentials_are_stored = request_filesystem_credentials( self_admin_url() );
93
		ob_end_clean();
94
		if ( $filesystem_credentials_are_stored ) {
95
			return true;
96
		}
97
98
		return false;
99
	}
100
101
	public static function home_url() {
102
		return self::get_protocol_normalized_url(
103
			'home_url',
104
			self::normalize_www_in_url( 'home', 'home_url' )
105
		);
106
	}
107
108
	public static function site_url() {
109
		return self::get_protocol_normalized_url(
110
			'site_url',
111
			self::normalize_www_in_url( 'siteurl', 'site_url' )
112
		);
113
	}
114
115
	public static function main_network_site_url() {
116
		return self::get_protocol_normalized_url( 'main_network_site_url', network_site_url() );
117
	}
118
119
	public static function get_protocol_normalized_url( $callable, $new_value ) {
120
		$option_key = self::HTTPS_CHECK_OPTION_PREFIX . $callable;
121
122
		$parsed_url = wp_parse_url( $new_value );
123
		if ( ! $parsed_url ) {
124
			return $new_value;
125
		}
126
127
		$scheme = $parsed_url['scheme'];
128
		$scheme_history = get_option( $option_key, array() );
129
		$scheme_history[] = $scheme;
130
131
		// Limit length to self::HTTPS_CHECK_HISTORY
132
		$scheme_history = array_slice( $scheme_history, ( self::HTTPS_CHECK_HISTORY * -1 ) );
133
134
		update_option( $option_key, $scheme_history );
135
136
		$forced_scheme =  in_array( 'https', $scheme_history ) ? 'https' : 'http';
137
138
		return set_url_scheme( $new_value, $forced_scheme );
139
	}
140
141
	public static function normalize_www_in_url( $option, $url_function ) {
142
		$url        = wp_parse_url( call_user_func( $url_function ) );
143
		$option_url = wp_parse_url( get_option( $option ) );
144
145
		if ( ! $option_url || ! $url ) {
146
			return $url;
147
		}
148
149 View Code Duplication
		if ( $url[ 'host' ] === "www.{$option_url[ 'host' ]}" ) {
150
			// remove www if not present in option URL
151
			$url[ 'host' ] = $option_url[ 'host' ];
152
		}
153 View Code Duplication
		if ( $option_url[ 'host' ] === "www.{$url[ 'host' ]}" ) {
154
			// add www if present in option URL
155
			$url[ 'host' ] = $option_url[ 'host' ];
156
		}
157
158
		$normalized_url = "{$url['scheme']}://{$url['host']}";
159
		if ( isset( $url['path'] ) ) {
160
			$normalized_url .= "{$url['path']}";
161
		}
162
163
		if ( isset( $url['query'] ) ) {
164
			$normalized_url .= "?{$url['query']}";
165
		}
166
167
		return $normalized_url;
168
	}
169
170
	public static function get_plugins() {
171
		if ( ! function_exists( 'get_plugins' ) ) {
172
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
173
		}
174
175
		/** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */
176
		return apply_filters( 'all_plugins', get_plugins() );
177
	}
178
179
	public static function wp_version() {
180
		global $wp_version;
181
182
		return $wp_version;
183
	}
184
185
	public static function site_icon_url() {
186
		if ( ! function_exists( 'get_site_icon_url' ) || ! has_site_icon() ) {
187
			return get_option( 'jetpack_site_icon_url' );
188
		}
189
190
		return get_site_icon_url();
191
	}
192
}
193