Completed
Push — phpcs/3rd-party ( af361d...50f77e )
by
unknown
187:54 queued 179:53
created

Jetpack_Heartbeat::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
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
	 * Method that gets executed on the wp-cron call
52
	 *
53
	 * @deprecated since 8.9.0
54
	 * @see Automattic\Jetpack\Heartbeat::cron_exec()
55
	 *
56
	 * @since 2.3.3
57
	 * @global string $wp_version
58
	 */
59
	public function cron_exec() {
60
		_deprecated_function( __METHOD__, 'jetpack-8.9.0', 'Automattic\\Jetpack\\Heartbeat::cron_exec' );
61
		return self::$proxied_instance->cron_exec();
62
63
	}
64
65
	/**
66
	 * Generates heartbeat stats data.
67
	 *
68
	 * @param string $prefix Prefix to add before stats identifier.
69
	 *
70
	 * @return array The stats array.
71
	 */
72
	public static function generate_stats_array( $prefix = '' ) {
73
		$return = array();
74
75
		$return[ "{$prefix}version" ]        = JETPACK__VERSION;
76
		$return[ "{$prefix}wp-version" ]     = get_bloginfo( 'version' );
77
		$return[ "{$prefix}php-version" ]    = PHP_VERSION;
78
		$return[ "{$prefix}branch" ]         = (float) JETPACK__VERSION;
79
		$return[ "{$prefix}wp-branch" ]      = (float) get_bloginfo( 'version' );
80
		$return[ "{$prefix}php-branch" ]     = (float) PHP_VERSION;
81
		$return[ "{$prefix}public" ]         = Jetpack_Options::get_option( 'public' );
82
		$return[ "{$prefix}ssl" ]            = Jetpack::permit_ssl();
83
		$return[ "{$prefix}is-https" ]       = is_ssl() ? 'https' : 'http';
84
		$return[ "{$prefix}language" ]       = get_bloginfo( 'language' );
85
		$return[ "{$prefix}charset" ]        = get_bloginfo( 'charset' );
86
		$return[ "{$prefix}is-multisite" ]   = is_multisite() ? 'multisite' : 'singlesite';
87
		$return[ "{$prefix}identitycrisis" ] = Jetpack::check_identity_crisis() ? 'yes' : 'no';
88
		$return[ "{$prefix}plugins" ]        = implode( ',', Jetpack::get_active_plugins() );
89
		if ( function_exists( 'get_mu_plugins' ) ) {
90
			$return[ "{$prefix}mu-plugins" ] = implode( ',', array_keys( get_mu_plugins() ) );
91
		}
92
		$return[ "{$prefix}manage-enabled" ] = true;
93
94
		$xmlrpc_errors = Jetpack_Options::get_option( 'xmlrpc_errors', array() );
95
		if ( $xmlrpc_errors ) {
96
			$return[ "{$prefix}xmlrpc-errors" ] = implode( ',', array_keys( $xmlrpc_errors ) );
97
			Jetpack_Options::delete_option( 'xmlrpc_errors' );
98
		}
99
100
		// Missing the connection owner?
101
		$connection_manager                 = new Manager();
102
		$return[ "{$prefix}missing-owner" ] = $connection_manager->is_missing_connection_owner();
103
104
		// is-multi-network can have three values, `single-site`, `single-network`, and `multi-network`.
105
		$return[ "{$prefix}is-multi-network" ] = 'single-site';
106
		if ( is_multisite() ) {
107
			$return[ "{$prefix}is-multi-network" ] = Jetpack::is_multi_network() ? 'multi-network' : 'single-network';
108
		}
109
110
		if ( ! empty( $_SERVER['SERVER_ADDR'] ) || ! empty( $_SERVER['LOCAL_ADDR'] ) ) {
111
			$ip     = ! empty( $_SERVER['SERVER_ADDR'] ) ? $_SERVER['SERVER_ADDR'] : $_SERVER['LOCAL_ADDR'];
112
			$ip_arr = array_map( 'intval', explode( '.', $ip ) );
113
			if ( 4 === count( $ip_arr ) ) {
114
				$return[ "{$prefix}ip-2-octets" ] = implode( '.', array_slice( $ip_arr, 0, 2 ) );
115
			}
116
		}
117
118
		foreach ( Jetpack::get_available_modules() as $slug ) {
119
			$return[ "{$prefix}module-{$slug}" ] = Jetpack::is_module_active( $slug ) ? 'on' : 'off';
120
		}
121
122
		return $return;
123
	}
124
125
	/**
126
	 * Registers jetpack.getHeartbeatData xmlrpc method
127
	 *
128
	 * @deprecated since 8.9.0
129
	 * @see Automattic\Jetpack\Heartbeat::jetpack_xmlrpc_methods()
130
	 *
131
	 * @param array $methods The list of methods to be filtered.
132
	 * @return array $methods
133
	 */
134
	public static function jetpack_xmlrpc_methods( $methods ) {
135
		_deprecated_function( __METHOD__, 'jetpack-8.9.0', 'Automattic\\Jetpack\\Heartbeat::jetpack_xmlrpc_methods' );
136
		return Heartbeat::jetpack_xmlrpc_methods( $methods );
137
	}
138
139
	/**
140
	 * Handles the response for the jetpack.getHeartbeatData xmlrpc method
141
	 *
142
	 * @deprecated since 8.9.0
143
	 * @see Automattic\Jetpack\Heartbeat::xmlrpc_data_response()
144
	 *
145
	 * @param array $params The parameters received in the request.
146
	 * @return array $params all the stats that hearbeat handles.
147
	 */
148
	public static function xmlrpc_data_response( $params = array() ) {
149
		_deprecated_function( __METHOD__, 'jetpack-8.9.0', 'Automattic\\Jetpack\\Heartbeat::xmlrpc_data_response' );
150
		return Heartbeat::xmlrpc_data_response( $params );
151
	}
152
153
	/**
154
	 * Clear scheduled events
155
	 *
156
	 * @deprecated since 8.9.0
157
	 * @see Automattic\Jetpack\Heartbeat::deactivate()
158
	 *
159
	 * @return void
160
	 */
161
	public function deactivate() {
162
		// Cronjobs are now handled by the Heartbeat package and we don't want to deactivate it here.
163
		// 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.
164
		_deprecated_function( __METHOD__, 'jetpack-8.9.0', 'Automattic\\Jetpack\\Heartbeat::deactivate' );
165
	}
166
167
	/**
168
	 * Add Jetpack Stats array to Heartbeat if Jetpack is connected
169
	 *
170
	 * @since 8.9.0
171
	 *
172
	 * @param array $stats Jetpack Heartbeat stats.
173
	 * @return array $stats
174
	 */
175
	public function add_stats_to_heartbeat( $stats ) {
176
177
		if ( ! Jetpack::is_active() ) {
178
			return $stats;
179
		}
180
181
		$jetpack_stats = self::generate_stats_array();
182
183
		return array_merge( $stats, $jetpack_stats );
184
	}
185
186
}
187