Completed
Push — rm/deprecated-code ( 60e936...87d3e1 )
by Jeremy
38:33 queued 26:57
created

Jetpack_Heartbeat::xmlrpc_data_response()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
use Automattic\Jetpack\Connection\Manager;
4
use Automattic\Jetpack\Heartbeat;
5
6
class Jetpack_Heartbeat {
7
8
	/**
9
	 * Holds the singleton instance of this class
10
	 *
11
	 * @since 2.3.3
12
	 * @var Jetpack_Heartbeat
13
	 */
14
	private static $instance = false;
15
16
	/**
17
	 * Holds the singleton instance of the proxied class
18
	 *
19
	 * @since 8.9.0
20
	 * @var Automattic\Jetpack\Heartbeat
21
	 */
22
	private static $proxied_instance = false;
23
24
	/**
25
	 * Singleton
26
	 *
27
	 * @since 2.3.3
28
	 * @static
29
	 * @return Jetpack_Heartbeat
30
	 */
31
	public static function init() {
32
		if ( ! self::$instance ) {
33
			self::$instance         = new Jetpack_Heartbeat();
34
			self::$proxied_instance = Heartbeat::init();
35
		}
36
37
		return self::$instance;
38
	}
39
40
	/**
41
	 * Constructor for singleton
42
	 *
43
	 * @since 2.3.3
44
	 * @return Jetpack_Heartbeat
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
45
	 */
46
	private function __construct() {
47
		add_filter( 'jetpack_heartbeat_stats_array', array( $this, 'add_stats_to_heartbeat' ) );
48
	}
49
50
	/**
51
	 * Generates heartbeat stats data.
52
	 *
53
	 * @param string $prefix Prefix to add before stats identifier.
54
	 *
55
	 * @return array The stats array.
56
	 */
57
	public static function generate_stats_array( $prefix = '' ) {
58
		$return = array();
59
60
		$return[ "{$prefix}version" ]        = JETPACK__VERSION;
61
		$return[ "{$prefix}wp-version" ]     = get_bloginfo( 'version' );
62
		$return[ "{$prefix}php-version" ]    = PHP_VERSION;
63
		$return[ "{$prefix}branch" ]         = (float) JETPACK__VERSION;
64
		$return[ "{$prefix}wp-branch" ]      = (float) get_bloginfo( 'version' );
65
		$return[ "{$prefix}php-branch" ]     = (float) PHP_VERSION;
66
		$return[ "{$prefix}public" ]         = Jetpack_Options::get_option( 'public' );
67
		$return[ "{$prefix}ssl" ]            = Jetpack::permit_ssl();
68
		$return[ "{$prefix}is-https" ]       = is_ssl() ? 'https' : 'http';
69
		$return[ "{$prefix}language" ]       = get_bloginfo( 'language' );
70
		$return[ "{$prefix}charset" ]        = get_bloginfo( 'charset' );
71
		$return[ "{$prefix}is-multisite" ]   = is_multisite() ? 'multisite' : 'singlesite';
72
		$return[ "{$prefix}identitycrisis" ] = Jetpack::check_identity_crisis() ? 'yes' : 'no';
73
		$return[ "{$prefix}plugins" ]        = implode( ',', Jetpack::get_active_plugins() );
74
		if ( function_exists( 'get_mu_plugins' ) ) {
75
			$return[ "{$prefix}mu-plugins" ] = implode( ',', array_keys( get_mu_plugins() ) );
76
		}
77
		$return[ "{$prefix}manage-enabled" ] = true;
78
79
		$xmlrpc_errors = Jetpack_Options::get_option( 'xmlrpc_errors', array() );
80
		if ( $xmlrpc_errors ) {
81
			$return[ "{$prefix}xmlrpc-errors" ] = implode( ',', array_keys( $xmlrpc_errors ) );
82
			Jetpack_Options::delete_option( 'xmlrpc_errors' );
83
		}
84
85
		// Missing the connection owner?
86
		$connection_manager                 = new Manager();
87
		$return[ "{$prefix}missing-owner" ] = $connection_manager->is_missing_connection_owner();
88
89
		// is-multi-network can have three values, `single-site`, `single-network`, and `multi-network`.
90
		$return[ "{$prefix}is-multi-network" ] = 'single-site';
91
		if ( is_multisite() ) {
92
			$return[ "{$prefix}is-multi-network" ] = Jetpack::is_multi_network() ? 'multi-network' : 'single-network';
93
		}
94
95
		if ( ! empty( $_SERVER['SERVER_ADDR'] ) || ! empty( $_SERVER['LOCAL_ADDR'] ) ) {
96
			$ip     = ! empty( $_SERVER['SERVER_ADDR'] ) ? $_SERVER['SERVER_ADDR'] : $_SERVER['LOCAL_ADDR'];
97
			$ip_arr = array_map( 'intval', explode( '.', $ip ) );
98
			if ( 4 === count( $ip_arr ) ) {
99
				$return[ "{$prefix}ip-2-octets" ] = implode( '.', array_slice( $ip_arr, 0, 2 ) );
100
			}
101
		}
102
103
		foreach ( Jetpack::get_available_modules() as $slug ) {
104
			$return[ "{$prefix}module-{$slug}" ] = Jetpack::is_module_active( $slug ) ? 'on' : 'off';
105
		}
106
107
		return $return;
108
	}
109
110
	/**
111
	 * Add Jetpack Stats array to Heartbeat if Jetpack is connected
112
	 *
113
	 * @since 8.9.0
114
	 *
115
	 * @param array $stats Jetpack Heartbeat stats.
116
	 * @return array $stats
117
	 */
118
	public function add_stats_to_heartbeat( $stats ) {
119
120
		if ( ! Jetpack::is_connection_ready() ) {
121
			return $stats;
122
		}
123
124
		$jetpack_stats = self::generate_stats_array();
125
126
		return array_merge( $stats, $jetpack_stats );
127
	}
128
129
}
130