Completed
Push — try/statically-access-asset-to... ( e50fad...74c9e7 )
by
unknown
126:59 queued 118:11
created

Functions::get_plugins_action_links()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 1
dl 0
loc 14
rs 9.4888
c 0
b 0
f 0
1
<?php
2
3
namespace Automattic\Jetpack\Sync;
4
5
use Automattic\Jetpack\Constants;
6
7
/*
8
 * Utility functions to generate data synced to wpcom
9
 */
10
11
class Functions {
12
	const HTTPS_CHECK_OPTION_PREFIX = 'jetpack_sync_https_history_';
13
	const HTTPS_CHECK_HISTORY       = 5;
14
15
	public static function get_modules() {
16
		require_once JETPACK__PLUGIN_DIR . 'class.jetpack-admin.php';
17
18
		return \Jetpack_Admin::init()->get_modules();
19
	}
20
21
	public static function get_taxonomies() {
22
		global $wp_taxonomies;
23
		$wp_taxonomies_without_callbacks = array();
24
		foreach ( $wp_taxonomies as $taxonomy_name => $taxonomy ) {
25
			$sanitized_taxonomy = self::sanitize_taxonomy( $taxonomy );
26
			if ( ! empty( $sanitized_taxonomy ) ) {
27
				$wp_taxonomies_without_callbacks[ $taxonomy_name ] = $sanitized_taxonomy;
28
			} else {
29
				error_log( 'Jetpack: Encountered a recusive taxonomy:' . $taxonomy_name );
30
			}
31
		}
32
		return $wp_taxonomies_without_callbacks;
33
	}
34
35
	public static function get_shortcodes() {
36
		global $shortcode_tags;
37
		return array_keys( $shortcode_tags );
38
	}
39
40
	/**
41
	 * Removes any callback data since we will not be able to process it on our side anyways.
42
	 */
43
	public static function sanitize_taxonomy( $taxonomy ) {
44
45
		// Lets clone the taxonomy object instead of modifing the global one.
46
		$cloned_taxonomy = json_decode( wp_json_encode( $taxonomy ) );
47
48
		// recursive taxonomies are no fun.
49
		if ( is_null( $cloned_taxonomy ) ) {
50
			return null;
51
		}
52
		// Remove any meta_box_cb if they are not the default wp ones.
53
		if ( isset( $cloned_taxonomy->meta_box_cb ) &&
54
			 ! in_array( $cloned_taxonomy->meta_box_cb, array( 'post_tags_meta_box', 'post_categories_meta_box' ) ) ) {
55
			$cloned_taxonomy->meta_box_cb = null;
56
		}
57
		// Remove update call back
58
		if ( isset( $cloned_taxonomy->update_count_callback ) &&
59
			 ! is_null( $cloned_taxonomy->update_count_callback ) ) {
60
			$cloned_taxonomy->update_count_callback = null;
61
		}
62
		// Remove rest_controller_class if it something other then the default.
63
		if ( isset( $cloned_taxonomy->rest_controller_class ) &&
64
			 'WP_REST_Terms_Controller' !== $cloned_taxonomy->rest_controller_class ) {
65
			$cloned_taxonomy->rest_controller_class = null;
66
		}
67
		return $cloned_taxonomy;
68
	}
69
70
	public static function get_post_types() {
71
		global $wp_post_types;
72
73
		$post_types_without_callbacks = array();
74
		foreach ( $wp_post_types as $post_type_name => $post_type ) {
75
			$sanitized_post_type = self::sanitize_post_type( $post_type );
76
			if ( ! empty( $sanitized_post_type ) ) {
77
				$post_types_without_callbacks[ $post_type_name ] = $sanitized_post_type;
78
			} else {
79
				error_log( 'Jetpack: Encountered a recusive post_type:' . $post_type_name );
80
			}
81
		}
82
		return $post_types_without_callbacks;
83
	}
84
85
	public static function sanitize_post_type( $post_type ) {
86
		// Lets clone the post type object instead of modifing the global one.
87
		$sanitized_post_type = array();
88
		foreach ( \Jetpack_Sync_Defaults::$default_post_type_attributes as $attribute_key => $default_value ) {
0 ignored issues
show
Bug introduced by
The property default_post_type_attributes cannot be accessed from this context as it is declared private in class Jetpack_Sync_Defaults.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
89
			if ( isset( $post_type->{ $attribute_key } ) ) {
90
				$sanitized_post_type[ $attribute_key ] = $post_type->{ $attribute_key };
91
			}
92
		}
93
		return (object) $sanitized_post_type;
94
	}
95
96
	public static function expand_synced_post_type( $sanitized_post_type, $post_type ) {
97
		$post_type        = sanitize_key( $post_type );
98
		$post_type_object = new \WP_Post_Type( $post_type, $sanitized_post_type );
99
		$post_type_object->add_supports();
100
		$post_type_object->add_rewrite_rules();
101
		$post_type_object->add_hooks();
102
		$post_type_object->register_taxonomies();
103
		return (object) $post_type_object;
104
	}
105
106
	public static function get_post_type_features() {
107
		global $_wp_post_type_features;
108
109
		return $_wp_post_type_features;
110
	}
111
112
	public static function get_hosting_provider() {
113
		if ( defined( 'GD_SYSTEM_PLUGIN_DIR' ) || class_exists( '\\WPaaS\\Plugin' ) ) {
114
			return 'gd-managed-wp';
115
		}
116
		if ( defined( 'MM_BASE_DIR' ) ) {
117
			return 'bh';
118
		}
119
		if ( defined( 'IS_PRESSABLE' ) ) {
120
			return 'pressable';
121
		}
122
		if ( function_exists( 'is_wpe' ) || function_exists( 'is_wpe_snapshot' ) ) {
123
			return 'wpe';
124
		}
125
		if ( defined( 'VIP_GO_ENV' ) && false !== VIP_GO_ENV ) {
126
			return 'vip-go';
127
		}
128
		return 'unknown';
129
	}
130
131
	public static function rest_api_allowed_post_types() {
132
		/** This filter is already documented in class.json-api-endpoints.php */
133
		return apply_filters( 'rest_api_allowed_post_types', array( 'post', 'page', 'revision' ) );
134
	}
135
136
	public static function rest_api_allowed_public_metadata() {
137
		/** This filter is documented in json-endpoints/class.wpcom-json-api-post-endpoint.php */
138
		return apply_filters( 'rest_api_allowed_public_metadata', array() );
139
	}
140
141
	/**
142
	 * Finds out if a site is using a version control system.
143
	 *
144
	 * @return bool
145
	 **/
146
	public static function is_version_controlled() {
147
148
		if ( ! class_exists( 'WP_Automatic_Updater' ) ) {
149
			require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
150
		}
151
		$updater = new \WP_Automatic_Updater();
152
153
		return (bool) strval( $updater->is_vcs_checkout( $context = ABSPATH ) );
154
	}
155
156
	/**
157
	 * Returns true if the site has file write access false otherwise.
158
	 *
159
	 * @return bool
160
	 **/
161
	public static function file_system_write_access() {
162
		if ( ! function_exists( 'get_filesystem_method' ) ) {
163
			require_once ABSPATH . 'wp-admin/includes/file.php';
164
		}
165
166
		require_once ABSPATH . 'wp-admin/includes/template.php';
167
168
		$filesystem_method = get_filesystem_method();
169
		if ( 'direct' === $filesystem_method ) {
170
			return true;
171
		}
172
173
		ob_start();
174
175
		if ( ! function_exists( 'request_filesystem_credentials' ) ) {
176
			require_once ABSPATH . 'wp-admin/includes/file.php';
177
		}
178
179
		$filesystem_credentials_are_stored = request_filesystem_credentials( self_admin_url() );
180
		ob_end_clean();
181
		if ( $filesystem_credentials_are_stored ) {
182
			return true;
183
		}
184
185
		return false;
186
	}
187
188
	/**
189
	 * Helper function that is used when getting home or siteurl values. Decides
190
	 * whether to get the raw or filtered value.
191
	 *
192
	 * @return string
193
	 */
194
	public static function get_raw_or_filtered_url( $url_type ) {
195
		$url_function = ( 'home' == $url_type )
196
			? 'home_url'
197
			: 'site_url';
198
199
		if (
200
			! Constants::is_defined( 'JETPACK_SYNC_USE_RAW_URL' ) ||
201
			Constants::get_constant( 'JETPACK_SYNC_USE_RAW_URL' )
202
		) {
203
			$scheme = is_ssl() ? 'https' : 'http';
204
			$url    = self::get_raw_url( $url_type );
205
			$url    = set_url_scheme( $url, $scheme );
206
		} else {
207
			$url = self::normalize_www_in_url( $url_type, $url_function );
208
		}
209
210
		return self::get_protocol_normalized_url( $url_function, $url );
211
	}
212
213
	public static function home_url() {
214
		$url = self::get_raw_or_filtered_url( 'home' );
215
216
		/**
217
		 * Allows overriding of the home_url value that is synced back to WordPress.com.
218
		 *
219
		 * @since 5.2.0
220
		 *
221
		 * @param string $home_url
222
		 */
223
		return esc_url_raw( apply_filters( 'jetpack_sync_home_url', $url ) );
224
	}
225
226
	public static function site_url() {
227
		$url = self::get_raw_or_filtered_url( 'siteurl' );
228
229
		/**
230
		 * Allows overriding of the site_url value that is synced back to WordPress.com.
231
		 *
232
		 * @since 5.2.0
233
		 *
234
		 * @param string $site_url
235
		 */
236
		return esc_url_raw( apply_filters( 'jetpack_sync_site_url', $url ) );
237
	}
238
239
	public static function main_network_site_url() {
240
		return self::get_protocol_normalized_url( 'main_network_site_url', network_site_url() );
241
	}
242
243
	public static function get_protocol_normalized_url( $callable, $new_value ) {
244
		$option_key = self::HTTPS_CHECK_OPTION_PREFIX . $callable;
245
246
		$parsed_url = wp_parse_url( $new_value );
247
		if ( ! $parsed_url ) {
248
			return $new_value;
249
		}
250
		if ( array_key_exists( 'scheme', $parsed_url ) ) {
251
			$scheme = $parsed_url['scheme'];
252
		} else {
253
			$scheme = '';
254
		}
255
		$scheme_history   = get_option( $option_key, array() );
256
		$scheme_history[] = $scheme;
257
258
		// Limit length to self::HTTPS_CHECK_HISTORY
259
		$scheme_history = array_slice( $scheme_history, ( self::HTTPS_CHECK_HISTORY * -1 ) );
260
261
		update_option( $option_key, $scheme_history );
262
263
		$forced_scheme = in_array( 'https', $scheme_history ) ? 'https' : 'http';
264
265
		return set_url_scheme( $new_value, $forced_scheme );
266
	}
267
268
	public static function get_raw_url( $option_name ) {
269
		$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...
270
		$constant = ( 'home' == $option_name )
271
			? 'WP_HOME'
272
			: 'WP_SITEURL';
273
274
		// Since we disregard the constant for multisites in ms-default-filters.php,
275
		// let's also use the db value if this is a multisite.
276
		if ( ! is_multisite() && Constants::is_defined( $constant ) ) {
277
			$value = Constants::get_constant( $constant );
278
		} else {
279
			// Let's get the option from the database so that we can bypass filters. This will help
280
			// ensure that we get more uniform values.
281
			$value = \Jetpack_Options::get_raw_option( $option_name );
282
		}
283
284
		return $value;
285
	}
286
287
	public static function normalize_www_in_url( $option, $url_function ) {
288
		$url        = wp_parse_url( call_user_func( $url_function ) );
289
		$option_url = wp_parse_url( get_option( $option ) );
290
291
		if ( ! $option_url || ! $url ) {
292
			return $url;
293
		}
294
295 View Code Duplication
		if ( $url['host'] === "www.{$option_url[ 'host' ]}" ) {
296
			// remove www if not present in option URL
297
			$url['host'] = $option_url['host'];
298
		}
299 View Code Duplication
		if ( $option_url['host'] === "www.{$url[ 'host' ]}" ) {
300
			// add www if present in option URL
301
			$url['host'] = $option_url['host'];
302
		}
303
304
		$normalized_url = "{$url['scheme']}://{$url['host']}";
305
		if ( isset( $url['path'] ) ) {
306
			$normalized_url .= "{$url['path']}";
307
		}
308
309
		if ( isset( $url['query'] ) ) {
310
			$normalized_url .= "?{$url['query']}";
311
		}
312
313
		return $normalized_url;
314
	}
315
316
	public static function get_plugins() {
317
		if ( ! function_exists( 'get_plugins' ) ) {
318
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
319
		}
320
321
		/** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */
322
		return apply_filters( 'all_plugins', get_plugins() );
323
	}
324
325
	/**
326
	 * Get custom action link tags that the plugin is using
327
	 * Ref: https://codex.wordpress.org/Plugin_API/Filter_Reference/plugin_action_links_(plugin_file_name)
328
	 *
329
	 * @return array of plugin action links (key: link name value: url)
330
	 */
331
	public static function get_plugins_action_links( $plugin_file_singular = null ) {
332
		// Some sites may have DOM disabled in PHP fail early
333
		if ( ! class_exists( 'DOMDocument' ) ) {
334
			return array();
335
		}
336
		$plugins_action_links = get_option( 'jetpack_plugin_api_action_links', array() );
337
		if ( ! empty( $plugins_action_links ) ) {
338
			if ( is_null( $plugin_file_singular ) ) {
339
				return $plugins_action_links;
340
			}
341
			return ( isset( $plugins_action_links[ $plugin_file_singular ] ) ? $plugins_action_links[ $plugin_file_singular ] : null );
342
		}
343
		return array();
344
	}
345
346
	public static function wp_version() {
347
		global $wp_version;
348
		return $wp_version;
349
	}
350
351
	public static function site_icon_url( $size = 512 ) {
352
		$site_icon = get_site_icon_url( $size );
353
		return $site_icon ? $site_icon : get_option( 'jetpack_site_icon_url' );
354
	}
355
356
	public static function roles() {
357
		$wp_roles = wp_roles();
358
		return $wp_roles->roles;
359
	}
360
361
	/**
362
	 * Determine time zone from WordPress' options "timezone_string"
363
	 * and "gmt_offset".
364
	 *
365
	 * 1. Check if `timezone_string` is set and return it.
366
	 * 2. Check if `gmt_offset` is set, formats UTC-offset from it and return it.
367
	 * 3. Default to "UTC+0" if nothing is set.
368
	 *
369
	 * @return string
370
	 */
371
	public static function get_timezone() {
372
		$timezone_string = get_option( 'timezone_string' );
373
374
		if ( ! empty( $timezone_string ) ) {
375
			return str_replace( '_', ' ', $timezone_string );
376
		}
377
378
		$gmt_offset = get_option( 'gmt_offset', 0 );
379
380
		$formatted_gmt_offset = sprintf( '%+g', floatval( $gmt_offset ) );
381
382
		$formatted_gmt_offset = str_replace(
383
			array( '.25', '.5', '.75' ),
384
			array( ':15', ':30', ':45' ),
385
			(string) $formatted_gmt_offset
386
		);
387
388
		/* translators: %s is UTC offset, e.g. "+1" */
389
		return sprintf( __( 'UTC%s', 'jetpack' ), $formatted_gmt_offset );
390
	}
391
392
	/**
393
	 * Return list of paused themes.
394
	 *
395
	 * @todo Remove function_exists check when WP 5.2 is the minimum.
396
	 *
397
	 * @return array|bool Array of paused themes or false if unsupported.
398
	 */
399
	public static function get_paused_themes() {
400
		if ( function_exists( 'wp_paused_themes' ) ) {
401
			$paused_themes = wp_paused_themes();
402
			return $paused_themes->get_all();
403
		}
404
		return false;
405
	}
406
407
	/**
408
	 * Return list of paused plugins.
409
	 *
410
	 * @todo Remove function_exists check when WP 5.2 is the minimum.
411
	 *
412
	 * @return array|bool Array of paused plugins or false if unsupported.
413
	 */
414
	public static function get_paused_plugins() {
415
		if ( function_exists( 'wp_paused_plugins' ) ) {
416
			$paused_plugins = wp_paused_plugins();
417
			return $paused_plugins->get_all();
418
		}
419
		return false;
420
	}
421
}
422