Completed
Push — branch-4.5 ( fc6aa7...14220c )
by
unknown
15:26 queued 07:39
created

Jetpack_Sync_Functions::sanitize_taxonomy()   C

Complexity

Conditions 8
Paths 9

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 14
nc 9
nop 1
dl 0
loc 26
rs 5.3846
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
	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
	/**
32
	 * Removes any callback data since we will not be able to process it on our side anyways.
33
	 */
34
	public static function sanitize_taxonomy( $taxonomy ) {
35
36
		// Lets clone the taxonomy object instead of modifing the global one.
37
		$cloned_taxonomy = json_decode( wp_json_encode( $taxonomy ) );
38
39
		// recursive taxonomies are no fun.
40
		if ( is_null( $cloned_taxonomy ) ) {
41
			return null;
42
		}
43
		// Remove any meta_box_cb if they are not the default wp ones.
44
		if ( isset( $cloned_taxonomy->meta_box_cb ) &&
45
		     ! in_array( $cloned_taxonomy->meta_box_cb, array( 'post_tags_meta_box', 'post_categories_meta_box' ) ) ) {
46
			$cloned_taxonomy->meta_box_cb = null;
47
		}
48
		// Remove update call back
49
		if ( isset( $cloned_taxonomy->update_count_callback ) &&
50
		     ! is_null( $cloned_taxonomy->update_count_callback ) ) {
51
			$cloned_taxonomy->update_count_callback = null;
52
		}
53
		// Remove rest_controller_class if it something other then the default.
54
		if ( isset( $cloned_taxonomy->rest_controller_class )  &&
55
		     'WP_REST_Terms_Controller' !== $cloned_taxonomy->rest_controller_class ) {
56
			$cloned_taxonomy->rest_controller_class = null;
57
		}
58
		return $cloned_taxonomy;
59
	}
60
61
	public static function get_post_types() {
62
		global $wp_post_types;
63
64
		return $wp_post_types;
65
	}
66
67
	public static function get_post_type_features() {
68
		global $_wp_post_type_features;
69
70
		return $_wp_post_type_features;
71
	}
72
73
	public static function get_hosting_provider() {
74
		if ( defined( 'GD_SYSTEM_PLUGIN_DIR' ) || class_exists( '\\WPaaS\\Plugin' ) ) {
75
			return 'gd-managed-wp';
76
		}
77
		if ( defined( 'MM_BASE_DIR' ) ) {
78
			return 'bh';
79
		} 
80
		if ( defined( 'IS_PRESSABLE' ) ) {
81
			return 'pressable';
82
		} 
83
		if ( function_exists( 'is_wpe' ) || function_exists( 'is_wpe_snapshot' ) ) {
84
			return 'wpe';
85
		}
86
		return 'unknown';
87
	}
88
89
	public static function rest_api_allowed_post_types() {
90
		/** This filter is already documented in class.json-api-endpoints.php */
91
		return apply_filters( 'rest_api_allowed_post_types', array( 'post', 'page', 'revision' ) );
92
	}
93
94
	public static function rest_api_allowed_public_metadata() {
95
		/** This filter is documented in json-endpoints/class.wpcom-json-api-post-endpoint.php */
96
		return apply_filters( 'rest_api_allowed_public_metadata', array() );
97
	}
98
99
	/**
100
	 * Finds out if a site is using a version control system.
101
	 * @return bool
102
	 **/
103
	public static function is_version_controlled() {
104
105
		if ( ! class_exists( 'WP_Automatic_Updater' ) ) {
106
			require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );
107
		}
108
		$updater = new WP_Automatic_Updater();
109
110
		return (bool) strval( $updater->is_vcs_checkout( $context = ABSPATH ) );
111
	}
112
113
	/**
114
	 * Returns true if the site has file write access false otherwise.
115
	 * @return bool
116
	 **/
117
	public static function file_system_write_access() {
118
		if ( ! function_exists( 'get_filesystem_method' ) ) {
119
			require_once( ABSPATH . 'wp-admin/includes/file.php' );
120
		}
121
122
		require_once( ABSPATH . 'wp-admin/includes/template.php' );
123
124
		$filesystem_method = get_filesystem_method();
125
		if ( 'direct' === $filesystem_method  ) {
126
			return true;
127
		}
128
129
		ob_start();
130
		$filesystem_credentials_are_stored = request_filesystem_credentials( self_admin_url() );
131
		ob_end_clean();
132
		if ( $filesystem_credentials_are_stored ) {
133
			return true;
134
		}
135
136
		return false;
137
	}
138
139
	public static function home_url() {
140
		return self::get_protocol_normalized_url(
141
			'home_url',
142
			self::normalize_www_in_url( 'home', 'home_url' )
143
		);
144
	}
145
146
	public static function site_url() {
147
		return self::get_protocol_normalized_url(
148
			'site_url',
149
			self::normalize_www_in_url( 'siteurl', 'site_url' )
150
		);
151
	}
152
153
	public static function main_network_site_url() {
154
		return self::get_protocol_normalized_url( 'main_network_site_url', network_site_url() );
155
	}
156
157
	public static function get_protocol_normalized_url( $callable, $new_value ) {
158
		$option_key = self::HTTPS_CHECK_OPTION_PREFIX . $callable;
159
160
		$parsed_url = wp_parse_url( $new_value );
161
		if ( ! $parsed_url ) {
162
			return $new_value;
163
		}
164
165
		$scheme = $parsed_url['scheme'];
166
		$scheme_history = get_option( $option_key, array() );
167
		$scheme_history[] = $scheme;
168
169
		// Limit length to self::HTTPS_CHECK_HISTORY
170
		$scheme_history = array_slice( $scheme_history, ( self::HTTPS_CHECK_HISTORY * -1 ) );
171
172
		update_option( $option_key, $scheme_history );
173
174
		$forced_scheme =  in_array( 'https', $scheme_history ) ? 'https' : 'http';
175
176
		return set_url_scheme( $new_value, $forced_scheme );
177
	}
178
179
	public static function normalize_www_in_url( $option, $url_function ) {
180
		$url        = wp_parse_url( call_user_func( $url_function ) );
181
		$option_url = wp_parse_url( get_option( $option ) );
182
183
		if ( ! $option_url || ! $url ) {
184
			return $url;
185
		}
186
187 View Code Duplication
		if ( $url[ 'host' ] === "www.{$option_url[ 'host' ]}" ) {
188
			// remove www if not present in option URL
189
			$url[ 'host' ] = $option_url[ 'host' ];
190
		}
191 View Code Duplication
		if ( $option_url[ 'host' ] === "www.{$url[ 'host' ]}" ) {
192
			// add www if present in option URL
193
			$url[ 'host' ] = $option_url[ 'host' ];
194
		}
195
196
		$normalized_url = "{$url['scheme']}://{$url['host']}";
197
		if ( isset( $url['path'] ) ) {
198
			$normalized_url .= "{$url['path']}";
199
		}
200
201
		if ( isset( $url['query'] ) ) {
202
			$normalized_url .= "?{$url['query']}";
203
		}
204
205
		return $normalized_url;
206
	}
207
208
	public static function get_plugins() {
209
		if ( ! function_exists( 'get_plugins' ) ) {
210
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
211
		}
212
213
		/** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */
214
		return apply_filters( 'all_plugins', get_plugins() );
215
	}
216
217
	public static function wp_version() {
218
		global $wp_version;
219
220
		return $wp_version;
221
	}
222
223
	public static function site_icon_url() {
224
		if ( ! function_exists( 'get_site_icon_url' ) || ! has_site_icon() ) {
225
			return get_option( 'jetpack_site_icon_url' );
226
		}
227
228
		return get_site_icon_url();
229
	}
230
}
231