Completed
Push — update/preserve-schema ( 6cfee8 )
by
unknown
28:39 queued 14:40
created

Jetpack_Sync_Functions::raw__url()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 6
nop 1
dl 0
loc 16
rs 8.8571
c 0
b 0
f 0
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
		}
37
		if ( defined( 'MM_BASE_DIR' ) ) {
38
			return 'bh';
39
		} 
40
		if ( defined( 'IS_PRESSABLE' ) ) {
41
			return 'pressable';
42
		} 
43
		if ( function_exists( 'is_wpe' ) || function_exists( 'is_wpe_snapshot' ) ) {
44
			return 'wpe';
45
		}
46
		return 'unknown';
47
	}
48
49
	public static function rest_api_allowed_post_types() {
50
		/** This filter is already documented in class.json-api-endpoints.php */
51
		return apply_filters( 'rest_api_allowed_post_types', array( 'post', 'page', 'revision' ) );
52
	}
53
54
	public static function rest_api_allowed_public_metadata() {
55
		/** This filter is documented in json-endpoints/class.wpcom-json-api-post-endpoint.php */
56
		return apply_filters( 'rest_api_allowed_public_metadata', array() );
57
	}
58
59
	/**
60
	 * Finds out if a site is using a version control system.
61
	 * @return bool
62
	 **/
63
	public static function is_version_controlled() {
64
65
		if ( ! class_exists( 'WP_Automatic_Updater' ) ) {
66
			require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );
67
		}
68
		$updater = new WP_Automatic_Updater();
69
70
		return (bool) strval( $updater->is_vcs_checkout( $context = ABSPATH ) );
71
	}
72
73
	/**
74
	 * Returns true if the site has file write access false otherwise.
75
	 * @return bool
76
	 **/
77
	public static function file_system_write_access() {
78
		if ( ! function_exists( 'get_filesystem_method' ) ) {
79
			require_once( ABSPATH . 'wp-admin/includes/file.php' );
80
		}
81
82
		require_once( ABSPATH . 'wp-admin/includes/template.php' );
83
84
		$filesystem_method = get_filesystem_method();
85
		if ( 'direct' === $filesystem_method  ) {
86
			return true;
87
		}
88
89
		ob_start();
90
		$filesystem_credentials_are_stored = request_filesystem_credentials( self_admin_url() );
91
		ob_end_clean();
92
		if ( $filesystem_credentials_are_stored ) {
93
			return true;
94
		}
95
96
		return false;
97
	}
98
99
	public static function home_url() {
100
		return self::preserve_scheme( 'home', 'home_url', true );
101
	}
102
103
	public static function site_url() {
104
		return self::preserve_scheme( 'siteurl', 'site_url', true );
105
	}
106
107
	public static function main_network_site_url() {
108
		return self::preserve_scheme( 'siteurl', 'network_site_url', false );
109
	}
110
	private static function raw__url( $option ) {
111
		global $wpdb;
112
		switch( $option ) {
113
			case 'siteurl':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
114
				if( defined( 'WP_SITEURL' ) ) {
115
					return WP_SITEURL;
116
				}
117
			case 'home':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
118
				if ( defined( 'WP_HOME' ) ) {
119
					return WP_HOME;
120
				}
121
			default:
122
				return $wpdb->get_var( $wpdb->prepare(
123
				"SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
124
		}
125
	}
126
127
	public static function preserve_scheme( $option, $url_function, $normalize_www = false ) {
128
		$previous_https_value = isset( $_SERVER['HTTPS'] ) ? $_SERVER['HTTPS'] : null;
129
		$_SERVER['HTTPS'] = 'off';
130
		$url = call_user_func( $url_function );
131
		$option_url = self::raw__url( $option );
132
133
		if ( $previous_https_value ) {
134
			$_SERVER['HTTPS'] = $previous_https_value;	
135
		} else {
136
			unset( $_SERVER['HTTPS'] );
137
		}
138
139
		if ( $option_url === $url ) {
140
			return $url;
141
		}
142
143
		// turn them both into parsed format
144
		$option_url = parse_url( $option_url );
145
		$url        = parse_url( $url );
146
		
147
		if ( $normalize_www ) {
148 View Code Duplication
			if ( $url['host'] === "www.{$option_url[ 'host' ]}" ) {
149
				// remove www if not present in option URL
150
				$url['host'] = $option_url['host'];
151
			}
152 View Code Duplication
			if ( $option_url['host'] === "www.{$url[ 'host' ]}" ) {
153
				// add www if present in option URL
154
				$url['host'] = $option_url['host'];
155
			}
156
		}
157
158
		if ( $url['host'] === $option_url['host'] ) {
159
			$url['scheme'] = $option_url['scheme'];
160
			// return set_url_scheme( $current_url,  $option_url['scheme'] );
161
		}
162
163
		$normalized_url = "{$url['scheme']}://{$url['host']}";
164
165
		if ( isset( $url['path'] ) ) {
166
			$normalized_url .= "{$url['path']}";
167
		}
168
169
		if ( isset( $url['query'] ) ) {
170
			$normalized_url .= "?{$url['query']}";
171
		}
172
173
		return $normalized_url;
174
	}
175
176
	public static function get_plugins() {
177
		if ( ! function_exists( 'get_plugins' ) ) {
178
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
179
		}
180
181
		/** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */
182
		return apply_filters( 'all_plugins', get_plugins() );
183
	}
184
185
	public static function wp_version() {
186
		global $wp_version;
187
188
		return $wp_version;
189
	}
190
191
	public static function site_icon_url() {
192
		if ( ! function_exists( 'get_site_icon_url' ) || ! has_site_icon() ) {
193
			return get_option( 'jetpack_site_icon_url' );
194
		}
195
196
		return get_site_icon_url();
197
	}
198
}
199