Completed
Push — add/search-p2-multi-site ( 72df15 )
by
unknown
29:19 queued 18:37
created

Functions::p2_workspace_hub_blog_id()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 0
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
1
<?php
2
/**
3
 * Utility functions to generate data synced to wpcom
4
 *
5
 * @package automattic/jetpack-sync
6
 */
7
8
namespace Automattic\Jetpack\Sync;
9
10
use Automattic\Jetpack\Constants;
11
12
/**
13
 * Utility functions to generate data synced to wpcom
14
 */
15
class Functions {
16
	const HTTPS_CHECK_OPTION_PREFIX = 'jetpack_sync_https_history_';
17
	const HTTPS_CHECK_HISTORY       = 5;
18
19
	/**
20
	 * Return array of Jetpack modules.
21
	 *
22
	 * @return array
23
	 */
24
	public static function get_modules() {
25
		if ( defined( 'JETPACK__PLUGIN_DIR' ) ) {
26
			require_once JETPACK__PLUGIN_DIR . 'class.jetpack-admin.php';
27
28
			return \Jetpack_Admin::init()->get_modules();
29
		}
30
31
		return array();
32
	}
33
34
	/**
35
	 * Return array of taxonomies registered on the site.
36
	 *
37
	 * @return array
38
	 */
39
	public static function get_taxonomies() {
40
		global $wp_taxonomies;
41
		$wp_taxonomies_without_callbacks = array();
42
		foreach ( $wp_taxonomies as $taxonomy_name => $taxonomy ) {
43
			$sanitized_taxonomy = self::sanitize_taxonomy( $taxonomy );
44
			if ( ! empty( $sanitized_taxonomy ) ) {
45
				$wp_taxonomies_without_callbacks[ $taxonomy_name ] = $sanitized_taxonomy;
46
			}
47
		}
48
		return $wp_taxonomies_without_callbacks;
49
	}
50
51
	/**
52
	 * Return array of registered shortcodes.
53
	 *
54
	 * @return array
55
	 */
56
	public static function get_shortcodes() {
57
		global $shortcode_tags;
58
		return array_keys( $shortcode_tags );
59
	}
60
61
	/**
62
	 * Removes any callback data since we will not be able to process it on our side anyways.
63
	 *
64
	 * @param \WP_Taxonomy $taxonomy \WP_Taxonomy item.
65
	 *
66
	 * @return mixed|null
67
	 */
68
	public static function sanitize_taxonomy( $taxonomy ) {
69
70
		// Lets clone the taxonomy object instead of modifing the global one.
71
		$cloned_taxonomy = json_decode( wp_json_encode( $taxonomy ) );
72
73
		// recursive taxonomies are no fun.
74
		if ( is_null( $cloned_taxonomy ) ) {
75
			return null;
76
		}
77
		// Remove any meta_box_cb if they are not the default wp ones.
78
		if ( isset( $cloned_taxonomy->meta_box_cb ) &&
79
			! in_array( $cloned_taxonomy->meta_box_cb, array( 'post_tags_meta_box', 'post_categories_meta_box' ), true ) ) {
80
			$cloned_taxonomy->meta_box_cb = null;
81
		}
82
		// Remove update call back.
83
		if ( isset( $cloned_taxonomy->update_count_callback ) &&
84
			! is_null( $cloned_taxonomy->update_count_callback ) ) {
85
			$cloned_taxonomy->update_count_callback = null;
86
		}
87
		// Remove rest_controller_class if it something other then the default.
88
		if ( isset( $cloned_taxonomy->rest_controller_class ) &&
89
			'WP_REST_Terms_Controller' !== $cloned_taxonomy->rest_controller_class ) {
90
			$cloned_taxonomy->rest_controller_class = null;
91
		}
92
		return $cloned_taxonomy;
93
	}
94
95
	/**
96
	 * Return array of registered post types.
97
	 *
98
	 * @return array
99
	 */
100
	public static function get_post_types() {
101
		global $wp_post_types;
102
103
		$post_types_without_callbacks = array();
104
		foreach ( $wp_post_types as $post_type_name => $post_type ) {
105
			$sanitized_post_type = self::sanitize_post_type( $post_type );
106
			if ( ! empty( $sanitized_post_type ) ) {
107
				$post_types_without_callbacks[ $post_type_name ] = $sanitized_post_type;
108
			}
109
		}
110
		return $post_types_without_callbacks;
111
	}
112
113
	/**
114
	 * Sanitizes by cloning post type object.
115
	 *
116
	 * @param object $post_type \WP_Post_Type.
117
	 *
118
	 * @return object
119
	 */
120
	public static function sanitize_post_type( $post_type ) {
121
		// Lets clone the post type object instead of modifing the global one.
122
		$sanitized_post_type = array();
123
		foreach ( Defaults::$default_post_type_attributes as $attribute_key => $default_value ) {
124
			if ( isset( $post_type->{ $attribute_key } ) ) {
125
				$sanitized_post_type[ $attribute_key ] = $post_type->{ $attribute_key };
126
			}
127
		}
128
		return (object) $sanitized_post_type;
129
	}
130
131
	/**
132
	 * Return information about a synced post type.
133
	 *
134
	 * @param array  $sanitized_post_type Array of args used in constructing \WP_Post_Type.
135
	 * @param string $post_type Post type name.
136
	 *
137
	 * @return object \WP_Post_Type
138
	 */
139
	public static function expand_synced_post_type( $sanitized_post_type, $post_type ) {
140
		$post_type        = sanitize_key( $post_type );
141
		$post_type_object = new \WP_Post_Type( $post_type, $sanitized_post_type );
142
		$post_type_object->add_supports();
143
		$post_type_object->add_rewrite_rules();
144
		$post_type_object->add_hooks();
145
		$post_type_object->register_taxonomies();
146
		return (object) $post_type_object;
147
	}
148
149
	/**
150
	 * Returns site's post_type_features.
151
	 *
152
	 * @return array
153
	 */
154
	public static function get_post_type_features() {
155
		global $_wp_post_type_features;
156
157
		return $_wp_post_type_features;
158
	}
159
160
	/**
161
	 * Return hosting provider.
162
	 *
163
	 * Uses a set of known constants, classes, or functions to help determine the hosting platform.
164
	 *
165
	 * @return string Hosting provider.
166
	 */
167
	public static function get_hosting_provider() {
168
		$hosting_provider_detection_methods = array(
169
			'get_hosting_provider_by_known_constant',
170
			'get_hosting_provider_by_known_class',
171
			'get_hosting_provider_by_known_function',
172
		);
173
174
		$functions = new Functions();
175
		foreach ( $hosting_provider_detection_methods as $method ) {
176
			$hosting_provider = call_user_func( array( $functions, $method ) );
177
			if ( false !== $hosting_provider ) {
178
				return $hosting_provider;
179
			}
180
		}
181
182
		return 'unknown';
183
	}
184
185
	/**
186
	 * Return a hosting provider using a set of known constants.
187
	 *
188
	 * @return mixed A host identifier string or false.
189
	 */
190
	public function get_hosting_provider_by_known_constant() {
191
		$hosting_provider_constants = array(
192
			'GD_SYSTEM_PLUGIN_DIR' => 'gd-managed-wp',
193
			'MM_BASE_DIR'          => 'bh',
194
			'PAGELYBIN'            => 'pagely',
195
			'KINSTAMU_VERSION'     => 'kinsta',
196
			'FLYWHEEL_CONFIG_DIR'  => 'flywheel',
197
			'IS_PRESSABLE'         => 'pressable',
198
			'VIP_GO_ENV'           => 'vip-go',
199
		);
200
201
		foreach ( $hosting_provider_constants as $constant => $constant_value ) {
202
			if ( Constants::is_defined( $constant ) ) {
203
				if ( 'VIP_GO_ENV' === $constant && false === Constants::get_constant( 'VIP_GO_ENV' ) ) {
204
					continue;
205
				}
206
				return $constant_value;
207
			}
208
		}
209
210
		return false;
211
	}
212
213
	/**
214
	 * Return a hosting provider using a set of known classes.
215
	 *
216
	 * @return mixed A host identifier string or false.
217
	 */
218
	public function get_hosting_provider_by_known_class() {
219
		$hosting_provider = false;
220
221
		switch ( true ) {
222
			case ( class_exists( '\\WPaaS\\Plugin' ) ):
223
				$hosting_provider = 'gd-managed-wp';
224
				break;
225
		}
226
227
		return $hosting_provider;
228
	}
229
230
	/**
231
	 * Return a hosting provider using a set of known functions.
232
	 *
233
	 * @return mixed A host identifier string or false.
234
	 */
235
	public function get_hosting_provider_by_known_function() {
236
		$hosting_provider = false;
237
238
		switch ( true ) {
239
			case ( function_exists( 'is_wpe' ) || function_exists( 'is_wpe_snapshot' ) ):
240
				$hosting_provider = 'wpe';
241
				break;
242
		}
243
244
		return $hosting_provider;
245
	}
246
247
	/**
248
	 * Return array of allowed REST API post types.
249
	 *
250
	 * @return array Array of allowed post types.
251
	 */
252
	public static function rest_api_allowed_post_types() {
253
		/** This filter is already documented in class.json-api-endpoints.php */
254
		return apply_filters( 'rest_api_allowed_post_types', array( 'post', 'page', 'revision' ) );
255
	}
256
257
	/**
258
	 * Return array of allowed REST API public metadata.
259
	 *
260
	 * @return array Array of allowed metadata.
261
	 */
262
	public static function rest_api_allowed_public_metadata() {
263
		/**
264
		 * Filters the meta keys accessible by the REST API.
265
		 *
266
		 * @see https://developer.wordpress.com/2013/04/26/custom-post-type-and-metadata-support-in-the-rest-api/
267
		 *
268
		 * @module json-api
269
		 *
270
		 * @since 2.2.3
271
		 *
272
		 * @param array $whitelisted_meta Array of metadata that is accessible by the REST API.
273
		 */
274
		return apply_filters( 'rest_api_allowed_public_metadata', array() );
275
	}
276
277
	/**
278
	 * Finds out if a site is using a version control system.
279
	 *
280
	 * @return bool
281
	 **/
282
	public static function is_version_controlled() {
283
284
		if ( ! class_exists( 'WP_Automatic_Updater' ) ) {
285
			require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
286
		}
287
		$updater = new \WP_Automatic_Updater();
288
289
		return (bool) (string) $updater->is_vcs_checkout( ABSPATH );
290
	}
291
292
	/**
293
	 * Returns true if the site has file write access false otherwise.
294
	 *
295
	 * @return bool
296
	 **/
297
	public static function file_system_write_access() {
298
		if ( ! function_exists( 'get_filesystem_method' ) ) {
299
			require_once ABSPATH . 'wp-admin/includes/file.php';
300
		}
301
302
		require_once ABSPATH . 'wp-admin/includes/template.php';
303
304
		$filesystem_method = get_filesystem_method();
305
		if ( 'direct' === $filesystem_method ) {
306
			return true;
307
		}
308
309
		ob_start();
310
311
		if ( ! function_exists( 'request_filesystem_credentials' ) ) {
312
			require_once ABSPATH . 'wp-admin/includes/file.php';
313
		}
314
315
		$filesystem_credentials_are_stored = request_filesystem_credentials( self_admin_url() );
316
		ob_end_clean();
317
		if ( $filesystem_credentials_are_stored ) {
318
			return true;
319
		}
320
321
		return false;
322
	}
323
324
	/**
325
	 * Helper function that is used when getting home or siteurl values. Decides
326
	 * whether to get the raw or filtered value.
327
	 *
328
	 * @param string $url_type URL to get, home or siteurl.
329
	 * @return string
330
	 */
331
	public static function get_raw_or_filtered_url( $url_type ) {
332
		$url_function = ( 'home' === $url_type )
333
			? 'home_url'
334
			: 'site_url';
335
336
		if (
337
			! Constants::is_defined( 'JETPACK_SYNC_USE_RAW_URL' ) ||
338
			Constants::get_constant( 'JETPACK_SYNC_USE_RAW_URL' )
339
		) {
340
			$scheme = is_ssl() ? 'https' : 'http';
341
			$url    = self::get_raw_url( $url_type );
342
			$url    = set_url_scheme( $url, $scheme );
343
		} else {
344
			$url = self::normalize_www_in_url( $url_type, $url_function );
345
		}
346
347
		return self::get_protocol_normalized_url( $url_function, $url );
348
	}
349
350
	/**
351
	 * Return the escaped home_url.
352
	 *
353
	 * @return string
354
	 */
355
	public static function home_url() {
356
		$url = self::get_raw_or_filtered_url( 'home' );
357
358
		/**
359
		 * Allows overriding of the home_url value that is synced back to WordPress.com.
360
		 *
361
		 * @since 5.2.0
362
		 *
363
		 * @param string $home_url
364
		 */
365
		return esc_url_raw( apply_filters( 'jetpack_sync_home_url', $url ) );
366
	}
367
368
	/**
369
	 * Return the escaped siteurl.
370
	 *
371
	 * @return string
372
	 */
373
	public static function site_url() {
374
		$url = self::get_raw_or_filtered_url( 'siteurl' );
375
376
		/**
377
		 * Allows overriding of the site_url value that is synced back to WordPress.com.
378
		 *
379
		 * @since 5.2.0
380
		 *
381
		 * @param string $site_url
382
		 */
383
		return esc_url_raw( apply_filters( 'jetpack_sync_site_url', $url ) );
384
	}
385
386
	/**
387
	 * Return main site URL with a normalized protocol.
388
	 *
389
	 * @return string
390
	 */
391
	public static function main_network_site_url() {
392
		return self::get_protocol_normalized_url( 'main_network_site_url', network_site_url() );
393
	}
394
395
	/**
396
	 * Return main site WordPress.com site ID.
397
	 *
398
	 * @return string
399
	 */
400
	public static function main_network_site_wpcom_id() {
401
		/**
402
		 * Return the current site WPCOM ID for single site installs
403
		 */
404
		if ( ! is_multisite() ) {
405
			return \Jetpack_Options::get_option( 'id' );
406
		}
407
408
		/**
409
		 * Return the main network site WPCOM ID for multi-site installs
410
		 */
411
		$current_network = get_network();
412
		switch_to_blog( $current_network->blog_id );
413
		$wpcom_blog_id = \Jetpack_Options::get_option( 'id' );
414
		restore_current_blog();
415
		return $wpcom_blog_id;
416
	}
417
418
	/**
419
	 * Return P2 Workspace Hub WordPress.com blog id.
420
	 *
421
	 * @return int
422
	 */
423
	public static function p2_workspace_hub_blog_id() {
424
		$p2_options = get_option( 'p2_options' );
425
426
		if ( ! is_array( $p2_options ) ) {
427
			// No support for sites that are not part of a Workspace.
428
			return 0;
429
		}
430
431
		$is_hub_option_key = 'is_p2_hub_site'; // keep in sync with WPForTeams\Constants\OPTION_KEY_IS_HUB.
432
		if ( array_key_exists( $is_hub_option_key, $p2_options ) ) {
433
			// Current site is a hub, return its WPCOM ID.
434
			return \Jetpack_Options::get_option( 'id' );
435
		}
436
437
		$has_hub_option_key = 'p2_hub_blog_id'; // in sync with WPForTeams\Constants\OPTION_KEY_PARENT_HUB_ID.
438
		if ( array_key_exists( $has_hub_option_key, $p2_options ) ) {
439
			// Current site is part of a Workspace, return its hub WPCOM ID.
440
			return $p2_options[ $has_hub_option_key ];
441
		}
442
443
		// Site doesnt have proper p2_options, it is neither a Workspace site or hub.
444
		return 0;
445
	}
446
447
	/**
448
	 * Return URL with a normalized protocol.
449
	 *
450
	 * @param callable $callable Function to retrieve URL option.
451
	 * @param string   $new_value URL Protocol to set URLs to.
452
	 * @return string Normalized URL.
453
	 */
454
	public static function get_protocol_normalized_url( $callable, $new_value ) {
455
		$option_key = self::HTTPS_CHECK_OPTION_PREFIX . $callable;
456
457
		$parsed_url = wp_parse_url( $new_value );
458
		if ( ! $parsed_url ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $parsed_url of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
459
			return $new_value;
460
		}
461
		if ( array_key_exists( 'scheme', $parsed_url ) ) {
462
			$scheme = $parsed_url['scheme'];
463
		} else {
464
			$scheme = '';
465
		}
466
		$scheme_history   = get_option( $option_key, array() );
467
		$scheme_history[] = $scheme;
468
469
		// Limit length to self::HTTPS_CHECK_HISTORY.
470
		$scheme_history = array_slice( $scheme_history, ( self::HTTPS_CHECK_HISTORY * -1 ) );
471
472
		update_option( $option_key, $scheme_history );
473
474
		$forced_scheme = in_array( 'https', $scheme_history, true ) ? 'https' : 'http';
475
476
		return set_url_scheme( $new_value, $forced_scheme );
477
	}
478
479
	/**
480
	 * Return URL from option or PHP constant.
481
	 *
482
	 * @param string $option_name (e.g. 'home').
483
	 *
484
	 * @return mixed|null URL.
485
	 */
486
	public static function get_raw_url( $option_name ) {
487
		$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...
488
		$constant = ( 'home' === $option_name )
489
			? 'WP_HOME'
490
			: 'WP_SITEURL';
491
492
		// Since we disregard the constant for multisites in ms-default-filters.php,
493
		// let's also use the db value if this is a multisite.
494
		if ( ! is_multisite() && Constants::is_defined( $constant ) ) {
495
			$value = Constants::get_constant( $constant );
496
		} else {
497
			// Let's get the option from the database so that we can bypass filters. This will help
498
			// ensure that we get more uniform values.
499
			$value = \Jetpack_Options::get_raw_option( $option_name );
500
		}
501
502
		return $value;
503
	}
504
505
	/**
506
	 * Normalize domains by removing www unless declared in the site's option.
507
	 *
508
	 * @param string   $option Option value from the site.
509
	 * @param callable $url_function Function retrieving the URL to normalize.
510
	 * @return mixed|string URL.
511
	 */
512
	public static function normalize_www_in_url( $option, $url_function ) {
513
		$url        = wp_parse_url( call_user_func( $url_function ) );
514
		$option_url = wp_parse_url( get_option( $option ) );
515
516
		if ( ! $option_url || ! $url ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $url of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Bug Best Practice introduced by
The expression $option_url of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
517
			return $url;
518
		}
519
520 View Code Duplication
		if ( "www.{$option_url[ 'host' ]}" === $url['host'] ) {
521
			// remove www if not present in option URL.
522
			$url['host'] = $option_url['host'];
523
		}
524 View Code Duplication
		if ( "www.{$url[ 'host' ]}" === $option_url['host'] ) {
525
			// add www if present in option URL.
526
			$url['host'] = $option_url['host'];
527
		}
528
529
		$normalized_url = "{$url['scheme']}://{$url['host']}";
530
		if ( isset( $url['path'] ) ) {
531
			$normalized_url .= "{$url['path']}";
532
		}
533
534
		if ( isset( $url['query'] ) ) {
535
			$normalized_url .= "?{$url['query']}";
536
		}
537
538
		return $normalized_url;
539
	}
540
541
	/**
542
	 * Return filtered value of get_plugins.
543
	 *
544
	 * @return mixed|void
545
	 */
546
	public static function get_plugins() {
547
		if ( ! function_exists( 'get_plugins' ) ) {
548
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
549
		}
550
551
		/** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */
552
		return apply_filters( 'all_plugins', get_plugins() );
553
	}
554
555
	/**
556
	 * Get custom action link tags that the plugin is using
557
	 * Ref: https://codex.wordpress.org/Plugin_API/Filter_Reference/plugin_action_links_(plugin_file_name)
558
	 *
559
	 * @param string $plugin_file_singular Particular plugin.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $plugin_file_singular not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
560
	 * @return array of plugin action links (key: link name value: url)
561
	 */
562
	public static function get_plugins_action_links( $plugin_file_singular = null ) {
563
		// Some sites may have DOM disabled in PHP fail early.
564
		if ( ! class_exists( 'DOMDocument' ) ) {
565
			return array();
566
		}
567
		$plugins_action_links = get_option( 'jetpack_plugin_api_action_links', array() );
568
		if ( ! empty( $plugins_action_links ) ) {
569
			if ( is_null( $plugin_file_singular ) ) {
570
				return $plugins_action_links;
571
			}
572
			return ( isset( $plugins_action_links[ $plugin_file_singular ] ) ? $plugins_action_links[ $plugin_file_singular ] : null );
573
		}
574
		return array();
575
	}
576
577
	/**
578
	 * Return the WP version as defined in the $wp_version global.
579
	 *
580
	 * @return string
581
	 */
582
	public static function wp_version() {
583
		global $wp_version;
584
		return $wp_version;
585
	}
586
587
	/**
588
	 * Return site icon url used on the site.
589
	 *
590
	 * @param int $size Size of requested icon in pixels.
591
	 * @return mixed|string|void
592
	 */
593
	public static function site_icon_url( $size = 512 ) {
594
		$site_icon = get_site_icon_url( $size );
595
		return $site_icon ? $site_icon : get_option( 'jetpack_site_icon_url' );
596
	}
597
598
	/**
599
	 * Return roles registered on the site.
600
	 *
601
	 * @return array
602
	 */
603
	public static function roles() {
604
		$wp_roles = wp_roles();
605
		return $wp_roles->roles;
606
	}
607
608
	/**
609
	 * Determine time zone from WordPress' options "timezone_string"
610
	 * and "gmt_offset".
611
	 *
612
	 * 1. Check if `timezone_string` is set and return it.
613
	 * 2. Check if `gmt_offset` is set, formats UTC-offset from it and return it.
614
	 * 3. Default to "UTC+0" if nothing is set.
615
	 *
616
	 * Note: This function is specifically not using wp_timezone() to keep consistency with
617
	 * the existing formatting of the timezone string.
618
	 *
619
	 * @return string
620
	 */
621
	public static function get_timezone() {
622
		$timezone_string = get_option( 'timezone_string' );
623
624
		if ( ! empty( $timezone_string ) ) {
625
			return str_replace( '_', ' ', $timezone_string );
626
		}
627
628
		$gmt_offset = get_option( 'gmt_offset', 0 );
629
630
		$formatted_gmt_offset = sprintf( '%+g', (float) $gmt_offset );
631
632
		$formatted_gmt_offset = str_replace(
633
			array( '.25', '.5', '.75' ),
634
			array( ':15', ':30', ':45' ),
635
			(string) $formatted_gmt_offset
636
		);
637
638
		/* translators: %s is UTC offset, e.g. "+1" */
639
		return sprintf( __( 'UTC%s', 'jetpack' ), $formatted_gmt_offset );
640
	}
641
642
	/**
643
	 * Return list of paused themes.
644
	 *
645
	 * @return array|bool Array of paused themes or false if unsupported.
646
	 */
647
	public static function get_paused_themes() {
648
		$paused_themes = wp_paused_themes();
649
		return $paused_themes->get_all();
650
	}
651
652
	/**
653
	 * Return list of paused plugins.
654
	 *
655
	 * @return array|bool Array of paused plugins or false if unsupported.
656
	 */
657
	public static function get_paused_plugins() {
658
		$paused_plugins = wp_paused_plugins();
659
		return $paused_plugins->get_all();
660
	}
661
662
	/**
663
	 * Return the theme's supported features.
664
	 * Used for syncing the supported feature that we care about.
665
	 *
666
	 * @return array List of features that the theme supports.
667
	 */
668
	public static function get_theme_support() {
669
		global $_wp_theme_features;
670
671
		$theme_support = array();
672
		foreach ( Defaults::$default_theme_support_whitelist as $theme_feature ) {
673
			$has_support = current_theme_supports( $theme_feature );
674
			if ( $has_support ) {
675
				$theme_support[ $theme_feature ] = $_wp_theme_features[ $theme_feature ];
676
			}
677
		}
678
679
		return $theme_support;
680
	}
681
682
	/**
683
	 * Wraps data in a way so that we can distinguish between objects and array and also prevent object recursion.
684
	 *
685
	 * @since 9.5.0
686
	 *
687
	 * @param array|obj $any        Source data to be cleaned up.
688
	 * @param array     $seen_nodes Built array of nodes.
689
	 *
690
	 * @return array
691
	 */
692
	public static function json_wrap( &$any, $seen_nodes = array() ) {
693
		if ( is_object( $any ) ) {
694
			$input        = get_object_vars( $any );
695
			$input['__o'] = 1;
696
		} else {
697
			$input = &$any;
698
		}
699
700
		if ( is_array( $input ) ) {
701
			$seen_nodes[] = &$any;
702
703
			$return = array();
704
705
			foreach ( $input as $k => &$v ) {
706
				if ( ( is_array( $v ) || is_object( $v ) ) ) {
707
					if ( in_array( $v, $seen_nodes, true ) ) {
708
						continue;
709
					}
710
					$return[ $k ] = self::json_wrap( $v, $seen_nodes );
0 ignored issues
show
Bug introduced by
It seems like $v can also be of type object; however, Automattic\Jetpack\Sync\Functions::json_wrap() does only seem to accept array|object<Automattic\Jetpack\Sync\obj>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
711
				} else {
712
					$return[ $k ] = $v;
713
				}
714
			}
715
716
			return $return;
717
		}
718
719
		return $any;
720
721
	}
722
}
723