Completed
Push — remove/local-site-data-rest-ap... ( f316a5...4e2164 )
by
unknown
16:57 queued 08:25
created

Jetpack_Sync_Functions::get_plugins_action_links()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 5
nop 1
dl 0
loc 14
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
	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
		if (
155
			! Jetpack_Constants::is_defined( 'JETPACK_SYNC_USE_RAW_URL' ) ||
156
			Jetpack_Constants::get_constant( 'JETPACK_SYNC_USE_RAW_URL' )
157
		) {
158
			$url = self::get_raw_url( $url_type );
159
		} else {
160
			$url_function = ( 'home' == $url_type )
161
				? 'home_url'
162
				: 'site_url';
163
			$url = self::normalize_www_in_url( $url_type, $url_function );
164
			$url = self::get_protocol_normalized_url( $url_function, $url );
165
		}
166
167
		return $url;
168
	}
169
170
	public static function home_url() {
171
		$url = self::get_raw_or_filtered_url( 'home' );
172
173
		/**
174
		 * Allows overriding of the home_url value that is synced back to WordPress.com.
175
		 *
176
		 * @since 5.2
177
		 *
178
		 * @param string $home_url
179
		 */
180
		return esc_url_raw( apply_filters( 'jetpack_sync_home_url', $url ) );
181
	}
182
183
	public static function site_url() {
184
		$url = self::get_raw_or_filtered_url( 'siteurl' );
185
186
		/**
187
		 * Allows overriding of the site_url value that is synced back to WordPress.com.
188
		 *
189
		 * @since 5.2
190
		 *
191
		 * @param string $site_url
192
		 */
193
		return esc_url_raw( apply_filters( 'jetpack_sync_site_url', $url ) );
194
	}
195
196
	public static function main_network_site_url() {
197
		return self::get_protocol_normalized_url( 'main_network_site_url', network_site_url() );
198
	}
199
200
	public static function get_protocol_normalized_url( $callable, $new_value ) {
201
		$option_key = self::HTTPS_CHECK_OPTION_PREFIX . $callable;
202
203
		$parsed_url = wp_parse_url( $new_value );
204
		if ( ! $parsed_url ) {
205
			return $new_value;
206
		}
207
208
		$scheme = $parsed_url['scheme'];
209
		$scheme_history = get_option( $option_key, array() );
210
		$scheme_history[] = $scheme;
211
212
		// Limit length to self::HTTPS_CHECK_HISTORY
213
		$scheme_history = array_slice( $scheme_history, ( self::HTTPS_CHECK_HISTORY * -1 ) );
214
215
		update_option( $option_key, $scheme_history );
216
217
		$forced_scheme =  in_array( 'https', $scheme_history ) ? 'https' : 'http';
218
219
		return set_url_scheme( $new_value, $forced_scheme );
220
	}
221
222
	public static function get_raw_url( $option_name ) {
223
		$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...
224
		$constant = ( 'home' == $option_name )
225
			? 'WP_HOME'
226
			: 'WP_SITEURL';
227
228
		if ( Jetpack_Constants::is_defined( $constant ) ) {
229
			$value = Jetpack_Constants::get_constant( $constant );
230
		} else {
231
			// Let's get the option from the database so that we can bypass filters. This will help
232
			// ensure that we get more uniform values.
233
			$value = Jetpack_Options::get_raw_option( $option_name );
234
		}
235
236
		return $value;
237
	}
238
239
	public static function normalize_www_in_url( $option, $url_function ) {
240
		$url        = wp_parse_url( call_user_func( $url_function ) );
241
		$option_url = wp_parse_url( get_option( $option ) );
242
243
		if ( ! $option_url || ! $url ) {
244
			return $url;
245
		}
246
247 View Code Duplication
		if ( $url[ 'host' ] === "www.{$option_url[ 'host' ]}" ) {
248
			// remove www if not present in option URL
249
			$url[ 'host' ] = $option_url[ 'host' ];
250
		}
251 View Code Duplication
		if ( $option_url[ 'host' ] === "www.{$url[ 'host' ]}" ) {
252
			// add www if present in option URL
253
			$url[ 'host' ] = $option_url[ 'host' ];
254
		}
255
256
		$normalized_url = "{$url['scheme']}://{$url['host']}";
257
		if ( isset( $url['path'] ) ) {
258
			$normalized_url .= "{$url['path']}";
259
		}
260
261
		if ( isset( $url['query'] ) ) {
262
			$normalized_url .= "?{$url['query']}";
263
		}
264
265
		return $normalized_url;
266
	}
267
268
	public static function get_plugins() {
269
		if ( ! function_exists( 'get_plugins' ) ) {
270
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
271
		}
272
273
		/** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */
274
		return apply_filters( 'all_plugins', get_plugins() );
275
	}
276
277
	/**
278
	 * Get custom action link tags that the plugin is using
279
	 * Ref: https://codex.wordpress.org/Plugin_API/Filter_Reference/plugin_action_links_(plugin_file_name)
280
	 * @return array of plugin action links (key: link name value: url)
281
	 */
282
	public static function get_plugins_action_links( $plugin_file_singular = null ) {
283
		// Some sites may have DOM disabled in PHP fail early
284
		if ( ! class_exists( 'DOMDocument' ) ) {
285
			return array();
286
		}
287
		$plugins_action_links = get_option( 'jetpack_plugin_api_action_links', array() );
288
		if ( ! empty( $plugins_action_links ) ) {
289
			if ( is_null( $plugin_file_singular ) ) {
290
				return $plugins_action_links;
291
			}
292
			return ( isset( $plugins_action_links[ $plugin_file_singular ] ) ? $plugins_action_links[ $plugin_file_singular ] : array() );
293
		}
294
		return array();
295
	}
296
297
	public static function wp_version() {
298
		global $wp_version;
299
		return $wp_version;
300
	}
301
302
	public static function site_icon_url() {
303
		if ( ! function_exists( 'get_site_icon_url' ) || ! has_site_icon() ) {
304
			return get_option( 'jetpack_site_icon_url' );
305
		}
306
307
		return get_site_icon_url();
308
	}
309
310
	public static function roles() {
311
		$wp_roles = wp_roles();
312
		return $wp_roles->roles;
313
	}
314
315
}
316