Completed
Push — update/plugin-api-endpoint ( 022fb4 )
by
unknown
19:54 queued 11:24
created

Jetpack_Sync_Functions::get_plugin_org_info()   B

Complexity

Conditions 6
Paths 20

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
226
		$constant = ( 'home' == $option_name )
227
			? 'WP_HOME'
228
			: 'WP_SITEURL';
229
230
		// Since we disregard the constant for multisites in ms-default-filters.php,
231
		// let's also use the db value if this is a multisite.
232
		if ( ! is_multisite() && Jetpack_Constants::is_defined( $constant ) ) {
233
			$value = Jetpack_Constants::get_constant( $constant );
234
		} else {
235
			// Let's get the option from the database so that we can bypass filters. This will help
236
			// ensure that we get more uniform values.
237
			$value = Jetpack_Options::get_raw_option( $option_name );
238
		}
239
240
		return $value;
241
	}
242
243
	public static function normalize_www_in_url( $option, $url_function ) {
244
		$url        = wp_parse_url( call_user_func( $url_function ) );
245
		$option_url = wp_parse_url( get_option( $option ) );
246
247
		if ( ! $option_url || ! $url ) {
248
			return $url;
249
		}
250
251 View Code Duplication
		if ( $url[ 'host' ] === "www.{$option_url[ 'host' ]}" ) {
252
			// remove www if not present in option URL
253
			$url[ 'host' ] = $option_url[ 'host' ];
254
		}
255 View Code Duplication
		if ( $option_url[ 'host' ] === "www.{$url[ 'host' ]}" ) {
256
			// add www if present in option URL
257
			$url[ 'host' ] = $option_url[ 'host' ];
258
		}
259
260
		$normalized_url = "{$url['scheme']}://{$url['host']}";
261
		if ( isset( $url['path'] ) ) {
262
			$normalized_url .= "{$url['path']}";
263
		}
264
265
		if ( isset( $url['query'] ) ) {
266
			$normalized_url .= "?{$url['query']}";
267
		}
268
269
		return $normalized_url;
270
	}
271
272
	public static function get_plugins() {
273
		if ( ! function_exists( 'get_plugins' ) ) {
274
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
275
		}
276
277
		/** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */
278
		return apply_filters( 'all_plugins', get_plugins() );
279
	}
280
281
	/**
282
	 * Get custom action link tags that the plugin is using
283
	 * Ref: https://codex.wordpress.org/Plugin_API/Filter_Reference/plugin_action_links_(plugin_file_name)
284
	 * @return array of plugin action links (key: link name value: url)
285
	 */
286
	public static function get_plugins_action_links( $plugin_file_singular = null ) {
287
		// Some sites may have DOM disabled in PHP fail early
288
		if ( ! class_exists( 'DOMDocument' ) ) {
289
			return array();
290
		}
291
		$plugins_action_links = get_option( 'jetpack_plugin_api_action_links', array() );
292
		if ( ! empty( $plugins_action_links ) ) {
293
			if ( is_null( $plugin_file_singular ) ) {
294
				return $plugins_action_links;
295
			}
296
			return ( isset( $plugins_action_links[ $plugin_file_singular ] ) ? $plugins_action_links[ $plugin_file_singular ] : null );
297
		}
298
		return array();
299
	}
300
301
	public static function wp_version() {
302
		global $wp_version;
303
		return $wp_version;
304
	}
305
306
	public static function get_plugin_org_info() {
307
		$update_plugins = get_site_transient( 'update_plugins' );
308
		$org_plugin_data = array();
309
		if ( isset( $update_plugins->no_update ) ) {
310
			$org_plugin_data = (array) $update_plugins->no_update;
311
		}
312
		if ( isset( $update_plugins->response ) ) {
313
			$org_plugin_data = array_merge( $org_plugin_data, $update_plugins->response );
314
		}
315
		$valid_org_plugin_data = array();
316
		foreach ( $org_plugin_data as $plugin_file => $plugin_data ) {
317
			$id = ( isset( $plugin_data->id ) ? explode( '/', (string) $plugin_data->id ) : null );
318
			$icons = is_array( $plugin_data->icons ) ? $plugin_data->icons : null;
319
			$valid_org_plugin_data[ $plugin_file ] = array_filter( array(
320
				'icons' => $icons,
321
				'slug' => $plugin_data->slug
322
			) );
323
			$valid_org_plugin_data[ $plugin_file ]['is_org'] = ( 'w.org' === $id[0] );
324
		}
325
		return $valid_org_plugin_data;
326
	}
327
	public static function site_icon_url() {
328
		if ( ! function_exists( 'get_site_icon_url' ) || ! has_site_icon() ) {
329
			return get_option( 'jetpack_site_icon_url' );
330
		}
331
332
		return get_site_icon_url();
333
	}
334
335
	public static function roles() {
336
		$wp_roles = wp_roles();
337
		return $wp_roles->roles;
338
	}
339
340
}
341