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 |
|
|
|
|
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
|
|
|
$a8c_mc_stats->add( $key, $value ); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
Jetpack_Options::update_option( 'last_heartbeat', time() ); |
113
|
|
|
|
114
|
|
|
$a8c_mc_stats->do_server_side_stats(); |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Fires when we synchronize all registered options on heartbeat. |
118
|
|
|
* |
119
|
|
|
* @since 3.3.0 |
120
|
|
|
*/ |
121
|
|
|
do_action( 'jetpack_heartbeat' ); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Generates heartbeat stats data. |
126
|
|
|
* |
127
|
|
|
* @param string $prefix Prefix to add before stats identifier. |
128
|
|
|
* |
129
|
|
|
* @return array The stats array. |
130
|
|
|
*/ |
131
|
|
|
public static function generate_stats_array( $prefix = '' ) { |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* This filter is used to build the array of stats that are bumped once a day by Jetpack Heartbeat. |
135
|
|
|
* |
136
|
|
|
* Filter the array and add key => value pairs where |
137
|
|
|
* * key is the stat group name |
138
|
|
|
* * value is the stat name. |
139
|
|
|
* |
140
|
|
|
* Example: |
141
|
|
|
* add_filter( 'jetpack_heartbeat_stats_array', function( $stats ) { |
142
|
|
|
* $stats['is-https'] = is_ssl() ? 'https' : 'http'; |
143
|
|
|
* }); |
144
|
|
|
* |
145
|
|
|
* This will bump the stats for the 'is-https/https' or 'is-https/http' stat. |
146
|
|
|
* |
147
|
|
|
* @param array $stats The stats to be filtered. |
148
|
|
|
* @param string $prefix The prefix that will automatically be added at the begining at each stat group name. |
149
|
|
|
*/ |
150
|
|
|
$stats = apply_filters( 'jetpack_heartbeat_stats_array', array(), $prefix ); |
|
|
|
|
151
|
|
|
$return = array(); |
152
|
|
|
|
153
|
|
|
// Apply prefix to stats. |
154
|
|
|
foreach ( $stats as $stat => $value ) { |
155
|
|
|
$return[ "$prefix$stat" ] = $value; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $return; |
159
|
|
|
|
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Registers jetpack.getHeartbeatData xmlrpc method |
164
|
|
|
* |
165
|
|
|
* @param array $methods The list of methods to be filtered. |
166
|
|
|
* @return array $methods |
167
|
|
|
*/ |
168
|
|
|
public static function jetpack_xmlrpc_methods( $methods ) { |
169
|
|
|
$methods['jetpack.getHeartbeatData'] = array( __CLASS__, 'xmlrpc_data_response' ); |
170
|
|
|
return $methods; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Handles the response for the jetpack.getHeartbeatData xmlrpc method |
175
|
|
|
* |
176
|
|
|
* @param array $params The parameters received in the request. |
177
|
|
|
* @return array $params all the stats that hearbeat handles. |
178
|
|
|
*/ |
179
|
|
|
public static function xmlrpc_data_response( $params = array() ) { |
180
|
|
|
// The WordPress XML-RPC server sets a default param of array() |
181
|
|
|
// if no argument is passed on the request and the method handlers get this array in $params. |
182
|
|
|
// generate_stats_array() needs a string as first argument. |
183
|
|
|
$params = empty( $params ) ? '' : $params; |
184
|
|
|
return self::generate_stats_array( $params ); |
|
|
|
|
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Clear scheduled events |
189
|
|
|
* |
190
|
|
|
* @return void |
191
|
|
|
*/ |
192
|
|
|
public function deactivate() { |
193
|
|
|
// Deal with the old pre-3.0 weekly one. |
194
|
|
|
$timestamp = wp_next_scheduled( 'jetpack_heartbeat' ); |
195
|
|
|
if ( $timestamp ) { |
196
|
|
|
wp_unschedule_event( $timestamp, 'jetpack_heartbeat' ); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$timestamp = wp_next_scheduled( $this->cron_name ); |
200
|
|
|
wp_unschedule_event( $timestamp, $this->cron_name ); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Interact with the Heartbeat |
205
|
|
|
* |
206
|
|
|
* ## OPTIONS |
207
|
|
|
* |
208
|
|
|
* inspect (default): Gets the list of data that is going to be sent in the heartbeat and the date/time of the last heartbeat |
209
|
|
|
* |
210
|
|
|
* @param array $args Arguments passed via CLI. |
211
|
|
|
* |
212
|
|
|
* @return void |
213
|
|
|
*/ |
214
|
|
|
public function cli_callback( $args ) { |
215
|
|
|
|
216
|
|
|
$allowed_args = array( |
217
|
|
|
'inspect', |
218
|
|
|
); |
219
|
|
|
|
220
|
|
|
if ( isset( $args[0] ) && ! in_array( $args[0], $allowed_args, true ) ) { |
221
|
|
|
/* translators: %s is a command like "prompt" */ |
222
|
|
|
WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $args[0] ) ); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
$stats = self::generate_stats_array(); |
226
|
|
|
$formatted_stats = array(); |
227
|
|
|
|
228
|
|
|
foreach ( $stats as $stat_name => $bin ) { |
229
|
|
|
$formatted_stats[] = array( |
230
|
|
|
'Stat name' => $stat_name, |
231
|
|
|
'Bin' => $bin, |
232
|
|
|
); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
WP_CLI\Utils\format_items( 'table', $formatted_stats, array( 'Stat name', 'Bin' ) ); |
236
|
|
|
|
237
|
|
|
$last_heartbeat = Jetpack_Options::get_option( 'last_heartbeat' ); |
238
|
|
|
|
239
|
|
|
if ( $last_heartbeat ) { |
240
|
|
|
$last_date = gmdate( 'Y-m-d H:i:s', $last_heartbeat ); |
241
|
|
|
/* translators: %s is the full datetime of the last heart beat e.g. 2020-01-01 12:21:23 */ |
242
|
|
|
WP_CLI::line( sprintf( __( 'Last heartbeat sent at: %s', 'jetpack' ), $last_date ) ); |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
} |
247
|
|
|
|
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.