Completed
Push — add/e2e-connection-purchase-fl... ( d02cce...68beb5 )
by Yaroslav
14:07 queued 04:35
created

Heartbeat   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 236
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 236
rs 10
c 0
b 0
f 0
wmc 25
lcom 3
cbo 2

8 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 7 2
A __construct() 0 21 5
B cron_exec() 0 46 6
A generate_stats_array() 0 30 2
A jetpack_xmlrpc_methods() 0 4 1
A xmlrpc_data_response() 0 7 2
A deactivate() 0 10 2
A cli_callback() 0 31 5
1
<?php
2
/**
3
 * Jetpack Heartbeat package.
4
 *
5
 * @package  automattic/jetpack-heartbeat
6
 */
7
8
namespace Automattic\Jetpack;
9
10
use Automattic\Jetpack\A8c_Mc_Stats;
11
use WP_CLI;
12
use Jetpack_Options;
13
14
/**
15
 * Heartbeat sends a batch of stats to wp.com once a day
16
 */
17
class Heartbeat {
18
19
	/**
20
	 * Holds the singleton instance of this class
21
	 *
22
	 * @since 2.3.3
23
	 * @var Heartbeat
24
	 */
25
	private static $instance = false;
26
27
	/**
28
	 * Cronjob identifier
29
	 *
30
	 * @var string
31
	 */
32
	private $cron_name = 'jetpack_v2_heartbeat';
33
34
	/**
35
	 * Singleton
36
	 *
37
	 * @since 2.3.3
38
	 * @static
39
	 * @return Heartbeat
40
	 */
41
	public static function init() {
42
		if ( ! self::$instance ) {
43
			self::$instance = new Heartbeat();
44
		}
45
46
		return self::$instance;
47
	}
48
49
	/**
50
	 * Constructor for singleton
51
	 *
52
	 * @since 2.3.3
53
	 * @return 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...
54
	 */
55
	private function __construct() {
56
57
		// Schedule the task.
58
		add_action( $this->cron_name, array( $this, 'cron_exec' ) );
59
60
		if ( ! wp_next_scheduled( $this->cron_name ) ) {
61
			// Deal with the old pre-3.0 weekly one.
62
			$timestamp = wp_next_scheduled( 'jetpack_heartbeat' );
63
			if ( $timestamp ) {
64
				wp_unschedule_event( $timestamp, 'jetpack_heartbeat' );
65
			}
66
67
			wp_schedule_event( time(), 'daily', $this->cron_name );
68
		}
69
70
		add_filter( 'jetpack_xmlrpc_methods', array( __CLASS__, 'jetpack_xmlrpc_methods' ) );
71
72
		if ( defined( 'WP_CLI' ) && WP_CLI ) {
73
			WP_CLI::add_command( 'jetpack-heartbeat', array( $this, 'cli_callback' ) );
74
		}
75
	}
76
77
	/**
78
	 * Method that gets executed on the wp-cron call
79
	 *
80
	 * @since 2.3.3
81
	 * @global string $wp_version
82
	 */
83
	public function cron_exec() {
84
85
		$a8c_mc_stats = new A8c_Mc_Stats();
86
87
		/*
88
		 * This should run daily.  Figuring in for variances in
89
		 * WP_CRON, don't let it run more than every 23 hours at most.
90
		 *
91
		 * i.e. if it ran less than 23 hours ago, fail out.
92
		 */
93
		$last = (int) Jetpack_Options::get_option( 'last_heartbeat' );
94
		if ( $last && ( $last + DAY_IN_SECONDS - HOUR_IN_SECONDS > time() ) ) {
95
			return;
96
		}
97
98
		/*
99
		 * Check for an identity crisis
100
		 *
101
		 * If one exists:
102
		 * - Bump stat for ID crisis
103
		 * - Email site admin about potential ID crisis
104
		 */
105
106
		// Coming Soon!
107
108
		foreach ( self::generate_stats_array( 'v2-' ) as $key => $value ) {
109
			if ( is_array( $value ) ) {
110
				foreach ( $value as $v ) {
111
					$a8c_mc_stats->add( $key, (string) $v );
112
				}
113
			} else {
114
				$a8c_mc_stats->add( $key, (string) $value );
115
			}
116
		}
117
118
		Jetpack_Options::update_option( 'last_heartbeat', time() );
119
120
		$a8c_mc_stats->do_server_side_stats();
121
122
		/**
123
		 * Fires when we synchronize all registered options on heartbeat.
124
		 *
125
		 * @since 3.3.0
126
		 */
127
		do_action( 'jetpack_heartbeat' );
128
	}
129
130
	/**
131
	 * Generates heartbeat stats data.
132
	 *
133
	 * @param string $prefix Prefix to add before stats identifier.
134
	 *
135
	 * @return array The stats array.
136
	 */
137
	public static function generate_stats_array( $prefix = '' ) {
138
139
		/**
140
		 * This filter is used to build the array of stats that are bumped once a day by Jetpack Heartbeat.
141
		 *
142
		 * Filter the array and add key => value pairs where
143
		 * * key is the stat group name
144
		 * * value is the stat name.
145
		 *
146
		 * Example:
147
		 * add_filter( 'jetpack_heartbeat_stats_array', function( $stats ) {
148
		 *    $stats['is-https'] = is_ssl() ? 'https' : 'http';
149
		 * });
150
		 *
151
		 * This will bump the stats for the 'is-https/https' or 'is-https/http' stat.
152
		 *
153
		 * @param array  $stats The stats to be filtered.
154
		 * @param string $prefix The prefix that will automatically be added at the begining at each stat group name.
155
		 */
156
		$stats  = apply_filters( 'jetpack_heartbeat_stats_array', array(), $prefix );
0 ignored issues
show
Unused Code introduced by
The call to apply_filters() has too many arguments starting with $prefix.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
157
		$return = array();
158
159
		// Apply prefix to stats.
160
		foreach ( $stats as $stat => $value ) {
161
			$return[ "$prefix$stat" ] = $value;
162
		}
163
164
		return $return;
165
166
	}
167
168
	/**
169
	 * Registers jetpack.getHeartbeatData xmlrpc method
170
	 *
171
	 * @param array $methods The list of methods to be filtered.
172
	 * @return array $methods
173
	 */
174
	public static function jetpack_xmlrpc_methods( $methods ) {
175
		$methods['jetpack.getHeartbeatData'] = array( __CLASS__, 'xmlrpc_data_response' );
176
		return $methods;
177
	}
178
179
	/**
180
	 * Handles the response for the jetpack.getHeartbeatData xmlrpc method
181
	 *
182
	 * @param array $params The parameters received in the request.
183
	 * @return array $params all the stats that hearbeat handles.
184
	 */
185
	public static function xmlrpc_data_response( $params = array() ) {
186
		// The WordPress XML-RPC server sets a default param of array()
187
		// if no argument is passed on the request and the method handlers get this array in $params.
188
		// generate_stats_array() needs a string as first argument.
189
		$params = empty( $params ) ? '' : $params;
190
		return self::generate_stats_array( $params );
0 ignored issues
show
Bug introduced by
It seems like $params defined by empty($params) ? '' : $params on line 189 can also be of type array; however, Automattic\Jetpack\Heart...:generate_stats_array() does only seem to accept string, 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...
191
	}
192
193
	/**
194
	 * Clear scheduled events
195
	 *
196
	 * @return void
197
	 */
198
	public function deactivate() {
199
		// Deal with the old pre-3.0 weekly one.
200
		$timestamp = wp_next_scheduled( 'jetpack_heartbeat' );
201
		if ( $timestamp ) {
202
			wp_unschedule_event( $timestamp, 'jetpack_heartbeat' );
203
		}
204
205
		$timestamp = wp_next_scheduled( $this->cron_name );
206
		wp_unschedule_event( $timestamp, $this->cron_name );
207
	}
208
209
	/**
210
	 * Interact with the Heartbeat
211
	 *
212
	 * ## OPTIONS
213
	 *
214
	 * inspect (default): Gets the list of data that is going to be sent in the heartbeat and the date/time of the last heartbeat
215
	 *
216
	 * @param array $args Arguments passed via CLI.
217
	 *
218
	 * @return void
219
	 */
220
	public function cli_callback( $args ) {
221
222
		$allowed_args = array(
223
			'inspect',
224
		);
225
226
		if ( isset( $args[0] ) && ! in_array( $args[0], $allowed_args, true ) ) {
227
			/* translators: %s is a command like "prompt" */
228
			WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $args[0] ) );
229
		}
230
231
		$stats           = self::generate_stats_array();
232
		$formatted_stats = array();
233
234
		foreach ( $stats as $stat_name => $bin ) {
235
			$formatted_stats[] = array(
236
				'Stat name' => $stat_name,
237
				'Bin'       => $bin,
238
			);
239
		}
240
241
		WP_CLI\Utils\format_items( 'table', $formatted_stats, array( 'Stat name', 'Bin' ) );
242
243
		$last_heartbeat = Jetpack_Options::get_option( 'last_heartbeat' );
244
245
		if ( $last_heartbeat ) {
246
			$last_date = gmdate( 'Y-m-d H:i:s', $last_heartbeat );
247
			/* translators: %s is the full datetime of the last heart beat e.g. 2020-01-01 12:21:23 */
248
			WP_CLI::line( sprintf( __( 'Last heartbeat sent at: %s', 'jetpack' ), $last_date ) );
249
		}
250
	}
251
252
}
253