Completed
Push — add/missing_connection_owner_h... ( 083f52 )
by
unknown
72:06 queued 63:43
created

class.jetpack-heartbeat.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
use Automattic\Jetpack\Heartbeat;
4
5
class Jetpack_Heartbeat {
6
7
	/**
8
	 * Holds the singleton instance of this class
9
	 *
10
	 * @since 2.3.3
11
	 * @var Jetpack_Heartbeat
12
	 */
13
	private static $instance = false;
14
15
	/**
16
	 * Holds the singleton instance of the proxied class
17
	 *
18
	 * @since 8.9.0
19
	 * @var Automattic\Jetpack\Heartbeat
20
	 */
21
	private static $proxied_instance = false;
22
23
	/**
24
	 * Singleton
25
	 *
26
	 * @since 2.3.3
27
	 * @static
28
	 * @return Jetpack_Heartbeat
29
	 */
30
	public static function init() {
31
		if ( ! self::$instance ) {
32
			self::$instance         = new Jetpack_Heartbeat();
33
			self::$proxied_instance = Heartbeat::init();
34
		}
35
36
		return self::$instance;
37
	}
38
39
	/**
40
	 * Constructor for singleton
41
	 *
42
	 * @since 2.3.3
43
	 * @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...
44
	 */
45
	private function __construct() {
46
		add_filter( 'jetpack_heartbeat_stats_array', array( $this, 'add_stats_to_heartbeat' ) );
47
	}
48
49
	/**
50
	 * Method that gets executed on the wp-cron call
51
	 *
52
	 * @deprecated since 8.9.0
53
	 * @see Automattic\Jetpack\Heartbeat::cron_exec()
54
	 *
55
	 * @since 2.3.3
56
	 * @global string $wp_version
57
	 */
58
	public function cron_exec() {
59
		_deprecated_function( __METHOD__, 'jetpack-8.9.0', 'Automattic\\Jetpack\\Heartbeat::cron_exec' );
60
		return self::$proxied_instance->cron_exec();
61
62
	}
63
64
	/**
65
	 * Generates heartbeat stats data.
66
	 *
67
	 * @param string $prefix Prefix to add before stats identifier.
68
	 *
69
	 * @return array The stats array.
70
	 */
71
	public static function generate_stats_array( $prefix = '' ) {
72
		$return = array();
73
74
		$return[ "{$prefix}version" ]        = JETPACK__VERSION;
75
		$return[ "{$prefix}wp-version" ]     = get_bloginfo( 'version' );
76
		$return[ "{$prefix}php-version" ]    = PHP_VERSION;
77
		$return[ "{$prefix}branch" ]         = (float) JETPACK__VERSION;
78
		$return[ "{$prefix}wp-branch" ]      = (float) get_bloginfo( 'version' );
79
		$return[ "{$prefix}php-branch" ]     = (float) PHP_VERSION;
80
		$return[ "{$prefix}public" ]         = Jetpack_Options::get_option( 'public' );
81
		$return[ "{$prefix}ssl" ]            = Jetpack::permit_ssl();
82
		$return[ "{$prefix}is-https" ]       = is_ssl() ? 'https' : 'http';
83
		$return[ "{$prefix}language" ]       = get_bloginfo( 'language' );
84
		$return[ "{$prefix}charset" ]        = get_bloginfo( 'charset' );
85
		$return[ "{$prefix}is-multisite" ]   = is_multisite() ? 'multisite' : 'singlesite';
86
		$return[ "{$prefix}identitycrisis" ] = Jetpack::check_identity_crisis() ? 'yes' : 'no';
87
		$return[ "{$prefix}plugins" ]        = implode( ',', Jetpack::get_active_plugins() );
88
		if ( function_exists( 'get_mu_plugins' ) ) {
89
			$return[ "{$prefix}mu-plugins" ] = implode( ',', array_keys( get_mu_plugins() ) );
90
		}
91
		$return[ "{$prefix}manage-enabled" ] = true;
92
93
		$xmlrpc_errors = Jetpack_Options::get_option( 'xmlrpc_errors', array() );
94
		if ( $xmlrpc_errors ) {
95
			$return[ "{$prefix}xmlrpc-errors" ] = implode( ',', array_keys( $xmlrpc_errors ) );
96
			Jetpack_Options::delete_option( 'xmlrpc_errors' );
97
		}
98
99
		// is-multi-network can have three values, `single-site`, `single-network`, and `multi-network`.
100
		$return[ "{$prefix}is-multi-network" ] = 'single-site';
101
		if ( is_multisite() ) {
102
			$return[ "{$prefix}is-multi-network" ] = Jetpack::is_multi_network() ? 'multi-network' : 'single-network';
103
		}
104
105
		if ( ! empty( $_SERVER['SERVER_ADDR'] ) || ! empty( $_SERVER['LOCAL_ADDR'] ) ) {
106
			$ip     = ! empty( $_SERVER['SERVER_ADDR'] ) ? $_SERVER['SERVER_ADDR'] : $_SERVER['LOCAL_ADDR'];
107
			$ip_arr = array_map( 'intval', explode( '.', $ip ) );
108
			if ( 4 === count( $ip_arr ) ) {
109
				$return[ "{$prefix}ip-2-octets" ] = implode( '.', array_slice( $ip_arr, 0, 2 ) );
110
			}
111
		}
112
113
		foreach ( Jetpack::get_available_modules() as $slug ) {
114
			$return[ "{$prefix}module-{$slug}" ] = Jetpack::is_module_active( $slug ) ? 'on' : 'off';
115
		}
116
117
		return $return;
118
	}
119
120
	/**
121
	 * Registers jetpack.getHeartbeatData xmlrpc method
122
	 *
123
	 * @deprecated since 8.9.0
124
	 * @see Automattic\Jetpack\Heartbeat::jetpack_xmlrpc_methods()
125
	 *
126
	 * @param array $methods The list of methods to be filtered.
127
	 * @return array $methods
128
	 */
129
	public static function jetpack_xmlrpc_methods( $methods ) {
130
		_deprecated_function( __METHOD__, 'jetpack-8.9.0', 'Automattic\\Jetpack\\Heartbeat::jetpack_xmlrpc_methods' );
131
		return Heartbeat::jetpack_xmlrpc_methods( $methods );
132
	}
133
134
	/**
135
	 * Handles the response for the jetpack.getHeartbeatData xmlrpc method
136
	 *
137
	 * @deprecated since 8.9.0
138
	 * @see Automattic\Jetpack\Heartbeat::xmlrpc_data_response()
139
	 *
140
	 * @param array $params The parameters received in the request.
141
	 * @return array $params all the stats that hearbeat handles.
142
	 */
143
	public static function xmlrpc_data_response( $params = array() ) {
144
		_deprecated_function( __METHOD__, 'jetpack-8.9.0', 'Automattic\\Jetpack\\Heartbeat::xmlrpc_data_response' );
145
		return Heartbeat::xmlrpc_data_response( $params );
146
	}
147
148
	/**
149
	 * Clear scheduled events
150
	 *
151
	 * @deprecated since 8.9.0
152
	 * @see Automattic\Jetpack\Heartbeat::deactivate()
153
	 *
154
	 * @return void
155
	 */
156
	public function deactivate() {
157
		// Cronjobs are now handled by the Heartbeat package and we don't want to deactivate it here.
158
		// We are adding jetpack stats to the heartbeat only if the connection is available. so we don't need to disable the cron when disconnecting.
159
		_deprecated_function( __METHOD__, 'jetpack-8.9.0', 'Automattic\\Jetpack\\Heartbeat::deactivate' );
160
	}
161
162
	/**
163
	 * Add Jetpack Stats array to Heartbeat if Jetpack is connected
164
	 *
165
	 * @since 8.9.0
166
	 *
167
	 * @param array $stats Jetpack Heartbeat stats.
168
	 * @return array $stats
169
	 */
170
	public function add_stats_to_heartbeat( $stats ) {
171
172
		if ( ! Jetpack::is_active() ) {
173
			return $stats;
174
		}
175
176
		$jetpack_stats = self::generate_stats_array();
177
178
		return array_merge( $stats, $jetpack_stats );
179
	}
180
181
}
182