Completed
Push — add/loading-placeholder ( 986bfd...cdde5d )
by
unknown
248:20 queued 238:54
created

Jetpack_Sync_Functions::preserve_scheme()   D

Complexity

Conditions 10
Paths 164

Size

Total Lines 47
Code Lines 26

Duplication

Lines 8
Ratio 17.02 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 26
c 1
b 0
f 0
nc 164
nop 3
dl 8
loc 47
rs 4.9032

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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