Completed
Push — add/detect-hosting-provider ( 4b9a65 )
by
unknown
34:30 queued 23:07
created

Jetpack_Sync_Functions   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 160
Duplicated Lines 5 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 8
loc 160
rs 9.6
wmc 32
lcom 1
cbo 1

15 Methods

Rating   Name   Duplication   Size   Complexity  
A get_post_types() 0 5 1
A get_taxonomies() 0 5 1
A get_modules() 0 5 1
A get_post_type_features() 0 5 1
A get_hosting_provider() 0 8 4
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 3 1
A site_url() 0 3 1
A main_network_site_url() 0 3 1
D preserve_scheme() 8 47 10
A get_plugins() 0 8 2
A wp_version() 0 5 1

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
9
	public static function get_modules() {
10
		require_once( JETPACK__PLUGIN_DIR . 'class.jetpack-admin.php' );
11
12
		return Jetpack_Admin::init()->get_modules();
13
	}
14
15
	public static function get_taxonomies() {
16
		global $wp_taxonomies;
17
18
		return $wp_taxonomies;
19
	}
20
21
	public static function get_post_types() {
22
		global $wp_post_types;
23
24
		return $wp_post_types;
25
	}
26
27
	public static function get_post_type_features() {
28
		global $_wp_post_type_features;
29
30
		return $_wp_post_type_features;
31
	}
32
33
	public static function get_hosting_provider() {
34
		if ( defined( 'GD_SYSTEM_PLUGIN_DIR' ) || class_exists( '\\WPaaS\\Plugin' ) ) {
35
			return "gd-managed-wp";
36
		} elseif ( defined( 'MM_BASE_DIR' ) ) {
37
			return "bh";
38
		} 
39
		return "unknown";
40
	}
41
42
	public static function rest_api_allowed_post_types() {
43
		/** This filter is already documented in class.json-api-endpoints.php */
44
		return apply_filters( 'rest_api_allowed_post_types', array( 'post', 'page', 'revision' ) );
45
	}
46
47
	public static function rest_api_allowed_public_metadata() {
48
		/** This filter is documented in json-endpoints/class.wpcom-json-api-post-endpoint.php */
49
		return apply_filters( 'rest_api_allowed_public_metadata', array() );
50
	}
51
52
	/**
53
	 * Finds out if a site is using a version control system.
54
	 * @return bool
55
	 **/
56
	public static function is_version_controlled() {
57
58
		if ( ! class_exists( 'WP_Automatic_Updater' ) ) {
59
			require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );
60
		}
61
		$updater = new WP_Automatic_Updater();
62
63
		return (bool) strval( $updater->is_vcs_checkout( $context = ABSPATH ) );
64
	}
65
66
	/**
67
	 * Returns true if the site has file write access false otherwise.
68
	 * @return bool
69
	 **/
70
	public static function file_system_write_access() {
71
		if ( ! function_exists( 'get_filesystem_method' ) ) {
72
			require_once( ABSPATH . 'wp-admin/includes/file.php' );
73
		}
74
75
		require_once( ABSPATH . 'wp-admin/includes/template.php' );
76
77
		$filesystem_method = get_filesystem_method();
78
		if ( 'direct' === $filesystem_method  ) {
79
			return true;
80
		}
81
82
		ob_start();
83
		$filesystem_credentials_are_stored = request_filesystem_credentials( self_admin_url() );
84
		ob_end_clean();
85
		if ( $filesystem_credentials_are_stored ) {
86
			return true;
87
		}
88
89
		return false;
90
	}
91
92
	public static function home_url() {
93
		return self::preserve_scheme( 'home', 'home_url', true );
94
	}
95
96
	public static function site_url() {
97
		return self::preserve_scheme( 'siteurl', 'site_url', true );
98
	}
99
100
	public static function main_network_site_url() {
101
		return self::preserve_scheme( 'siteurl', 'network_site_url', false );
102
	}
103
104
	public static function preserve_scheme( $option, $url_function, $normalize_www = false ) {
105
		$previous_https_value = isset( $_SERVER['HTTPS'] ) ? $_SERVER['HTTPS'] : null;
106
		$_SERVER['HTTPS'] = 'off';
107
		$url = call_user_func( $url_function );
108
		$option_url = get_option( $option );
109
		if ( $previous_https_value ) {
110
			$_SERVER['HTTPS'] = $previous_https_value;	
111
		} else {
112
			unset( $_SERVER['HTTPS'] );
113
		}
114
115
		if ( $option_url === $url ) {
116
			return $url;
117
		}
118
119
		// turn them both into parsed format
120
		$option_url = parse_url( $option_url );
121
		$url        = parse_url( $url );
122
123
		if ( $normalize_www ) {
124 View Code Duplication
			if ( $url['host'] === "www.{$option_url[ 'host' ]}" ) {
125
				// remove www if not present in option URL
126
				$url['host'] = $option_url['host'];
127
			}
128 View Code Duplication
			if ( $option_url['host'] === "www.{$url[ 'host' ]}" ) {
129
				// add www if present in option URL
130
				$url['host'] = $option_url['host'];
131
			}
132
		}
133
134
		if ( $url['host'] === $option_url['host'] ) {
135
			$url['scheme'] = $option_url['scheme'];
136
			// return set_url_scheme( $current_url,  $option_url['scheme'] );
137
		}
138
139
		$normalized_url = "{$url['scheme']}://{$url['host']}";
140
141
		if ( isset( $url['path'] ) ) {
142
			$normalized_url .= "{$url['path']}";
143
		}
144
145
		if ( isset( $url['query'] ) ) {
146
			$normalized_url .= "?{$url['query']}";
147
		}
148
149
		return $normalized_url;
150
	}
151
152
	public static function get_plugins() {
153
		if ( ! function_exists( 'get_plugins' ) ) {
154
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
155
		}
156
157
		/** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */
158
		return apply_filters( 'all_plugins', get_plugins() );
159
	}
160
161
	public static function wp_version() {
162
		global $wp_version;
163
164
		return $wp_version;
165
	}
166
}
167