1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* A class that defines syncable actions for Jetpack. |
4
|
|
|
* |
5
|
|
|
* @package automattic/jetpack-sync |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack\Sync; |
9
|
|
|
|
10
|
|
|
use Automattic\Jetpack\Connection\Manager as Jetpack_Connection; |
11
|
|
|
use Automattic\Jetpack\Constants; |
12
|
|
|
use Automattic\Jetpack\Status; |
13
|
|
|
use Automattic\Jetpack\Sync\Health; |
14
|
|
|
use Automattic\Jetpack\Sync\Modules; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The role of this class is to hook the Sync subsystem into WordPress - when to listen for actions, |
18
|
|
|
* when to send, when to perform a full sync, etc. |
19
|
|
|
* |
20
|
|
|
* It also binds the action to send data to WPCOM to Jetpack's XMLRPC client object. |
21
|
|
|
*/ |
22
|
|
|
class Actions { |
23
|
|
|
/** |
24
|
|
|
* A variable to hold a sync sender object. |
25
|
|
|
* |
26
|
|
|
* @access public |
27
|
|
|
* @static |
28
|
|
|
* |
29
|
|
|
* @var Automattic\Jetpack\Sync\Sender |
30
|
|
|
*/ |
31
|
|
|
public static $sender = null; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* A variable to hold a sync listener object. |
35
|
|
|
* |
36
|
|
|
* @access public |
37
|
|
|
* @static |
38
|
|
|
* |
39
|
|
|
* @var Automattic\Jetpack\Sync\Listener |
40
|
|
|
*/ |
41
|
|
|
public static $listener = null; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Name of the sync cron schedule. |
45
|
|
|
* |
46
|
|
|
* @access public |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
const DEFAULT_SYNC_CRON_INTERVAL_NAME = 'jetpack_sync_interval'; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Interval between the last and the next sync cron action. |
54
|
|
|
* |
55
|
|
|
* @access public |
56
|
|
|
* |
57
|
|
|
* @var int |
58
|
|
|
*/ |
59
|
|
|
const DEFAULT_SYNC_CRON_INTERVAL_VALUE = 300; // 5 * MINUTE_IN_SECONDS; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Initialize Sync for cron jobs, set up listeners for WordPress Actions, |
63
|
|
|
* and set up a shut-down action for sending actions to WordPress.com |
64
|
|
|
* |
65
|
|
|
* @access public |
66
|
|
|
* @static |
67
|
|
|
*/ |
68
|
|
|
public static function init() { |
69
|
|
|
// Everything below this point should only happen if we're a valid sync site. |
70
|
|
|
if ( ! self::sync_allowed() ) { |
71
|
|
|
return; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ( self::sync_via_cron_allowed() ) { |
75
|
|
|
self::init_sync_cron_jobs(); |
76
|
|
|
} elseif ( wp_next_scheduled( 'jetpack_sync_cron' ) ) { |
77
|
|
|
self::clear_sync_cron_jobs(); |
78
|
|
|
} |
79
|
|
|
// When importing via cron, do not sync. |
80
|
|
|
add_action( 'wp_cron_importer_hook', array( __CLASS__, 'set_is_importing_true' ), 1 ); |
81
|
|
|
|
82
|
|
|
// Sync connected user role changes to WordPress.com. |
83
|
|
|
Users::init(); |
84
|
|
|
|
85
|
|
|
// Publicize filter to prevent publicizing blacklisted post types. |
86
|
|
|
add_filter( 'publicize_should_publicize_published_post', array( __CLASS__, 'prevent_publicize_blacklisted_posts' ), 10, 2 ); |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Fires on every request before default loading sync listener code. |
90
|
|
|
* Return false to not load sync listener code that monitors common |
91
|
|
|
* WP actions to be serialized. |
92
|
|
|
* |
93
|
|
|
* By default this returns true for cron jobs, non-GET-requests, or requests where the |
94
|
|
|
* user is logged-in. |
95
|
|
|
* |
96
|
|
|
* @since 4.2.0 |
97
|
|
|
* |
98
|
|
|
* @param bool should we load sync listener code for this request |
99
|
|
|
*/ |
100
|
|
|
if ( apply_filters( 'jetpack_sync_listener_should_load', true ) ) { |
101
|
|
|
self::initialize_listener(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
add_action( 'init', array( __CLASS__, 'add_sender_shutdown' ), 90 ); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Prepares sync to send actions on shutdown for the current request. |
109
|
|
|
* |
110
|
|
|
* @access public |
111
|
|
|
* @static |
112
|
|
|
*/ |
113
|
|
|
public static function add_sender_shutdown() { |
114
|
|
|
/** |
115
|
|
|
* Fires on every request before default loading sync sender code. |
116
|
|
|
* Return false to not load sync sender code that serializes pending |
117
|
|
|
* data and sends it to WPCOM for processing. |
118
|
|
|
* |
119
|
|
|
* By default this returns true for cron jobs, POST requests, admin requests, or requests |
120
|
|
|
* by users who can manage_options. |
121
|
|
|
* |
122
|
|
|
* @since 4.2.0 |
123
|
|
|
* |
124
|
|
|
* @param bool should we load sync sender code for this request |
125
|
|
|
*/ |
126
|
|
|
if ( apply_filters( |
127
|
|
|
'jetpack_sync_sender_should_load', |
128
|
|
|
self::should_initialize_sender() |
129
|
|
|
) ) { |
130
|
|
|
self::initialize_sender(); |
131
|
|
|
add_action( 'shutdown', array( self::$sender, 'do_sync' ) ); |
132
|
|
|
add_action( 'shutdown', array( self::$sender, 'do_full_sync' ), 9999 ); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Decides if the sender should run on shutdown for this request. |
138
|
|
|
* |
139
|
|
|
* @access public |
140
|
|
|
* @static |
141
|
|
|
* |
142
|
|
|
* @return bool |
143
|
|
|
*/ |
144
|
|
|
public static function should_initialize_sender() { |
145
|
|
|
if ( Constants::is_true( 'DOING_CRON' ) ) { |
146
|
|
|
return self::sync_via_cron_allowed(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
if ( isset( $_SERVER['REQUEST_METHOD'] ) && 'POST' === $_SERVER['REQUEST_METHOD'] ) { |
150
|
|
|
return true; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if ( current_user_can( 'manage_options' ) ) { |
154
|
|
|
return true; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if ( is_admin() ) { |
158
|
|
|
return true; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
if ( defined( 'PHPUNIT_JETPACK_TESTSUITE' ) ) { |
162
|
|
|
return true; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
if ( Constants::get_constant( 'WP_CLI' ) ) { |
166
|
|
|
return true; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return false; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Decides if sync should run at all during this request. |
174
|
|
|
* |
175
|
|
|
* @access public |
176
|
|
|
* @static |
177
|
|
|
* |
178
|
|
|
* @return bool |
179
|
|
|
*/ |
180
|
|
|
public static function sync_allowed() { |
181
|
|
|
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
182
|
|
|
return false; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
if ( defined( 'PHPUNIT_JETPACK_TESTSUITE' ) ) { |
186
|
|
|
return true; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
if ( ! Settings::is_sync_enabled() ) { |
190
|
|
|
return false; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
if ( ( new Status() )->is_development_mode() ) { |
194
|
|
|
return false; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
if ( ( new Status() )->is_staging_site() ) { |
198
|
|
|
return false; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
$connection = new Jetpack_Connection(); |
202
|
|
|
if ( ! $connection->is_active() ) { |
203
|
|
|
if ( ! doing_action( 'jetpack_user_authorized' ) ) { |
204
|
|
|
return false; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return true; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Determines if syncing during a cron job is allowed. |
213
|
|
|
* |
214
|
|
|
* @access public |
215
|
|
|
* @static |
216
|
|
|
* |
217
|
|
|
* @return bool|int |
218
|
|
|
*/ |
219
|
|
|
public static function sync_via_cron_allowed() { |
220
|
|
|
return ( Settings::get_setting( 'sync_via_cron' ) ); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Decides if the given post should be Publicized based on its type. |
225
|
|
|
* |
226
|
|
|
* @access public |
227
|
|
|
* @static |
228
|
|
|
* |
229
|
|
|
* @param bool $should_publicize Publicize status prior to this filter running. |
230
|
|
|
* @param \WP_Post $post The post to test for Publicizability. |
231
|
|
|
* @return bool |
232
|
|
|
*/ |
233
|
|
|
public static function prevent_publicize_blacklisted_posts( $should_publicize, $post ) { |
234
|
|
|
if ( in_array( $post->post_type, Settings::get_setting( 'post_types_blacklist' ), true ) ) { |
235
|
|
|
return false; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
return $should_publicize; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Set an importing flag to `true` in sync settings. |
243
|
|
|
* |
244
|
|
|
* @access public |
245
|
|
|
* @static |
246
|
|
|
*/ |
247
|
|
|
public static function set_is_importing_true() { |
248
|
|
|
Settings::set_importing( true ); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Sends data to WordPress.com via an XMLRPC request. |
253
|
|
|
* |
254
|
|
|
* @access public |
255
|
|
|
* @static |
256
|
|
|
* |
257
|
|
|
* @param object $data Data relating to a sync action. |
258
|
|
|
* @param string $codec_name The name of the codec that encodes the data. |
259
|
|
|
* @param float $sent_timestamp Current server time so we can compensate for clock differences. |
260
|
|
|
* @param string $queue_id The queue the action belongs to, sync or full_sync. |
261
|
|
|
* @param float $checkout_duration Time spent retrieving queue items from the DB. |
262
|
|
|
* @param float $preprocess_duration Time spent converting queue items into data to send. |
263
|
|
|
* @return Jetpack_Error|mixed|WP_Error The result of the sending request. |
264
|
|
|
*/ |
265
|
|
|
public static function send_data( $data, $codec_name, $sent_timestamp, $queue_id, $checkout_duration, $preprocess_duration ) { |
266
|
|
|
$query_args = array( |
267
|
|
|
'sync' => '1', // Add an extra parameter to the URL so we can tell it's a sync action. |
268
|
|
|
'codec' => $codec_name, |
269
|
|
|
'timestamp' => $sent_timestamp, |
270
|
|
|
'queue' => $queue_id, |
271
|
|
|
'home' => Functions::home_url(), // Send home url option to check for Identity Crisis server-side. |
272
|
|
|
'siteurl' => Functions::site_url(), // Send siteurl option to check for Identity Crisis server-side. |
273
|
|
|
'cd' => sprintf( '%.4f', $checkout_duration ), |
274
|
|
|
'pd' => sprintf( '%.4f', $preprocess_duration ), |
275
|
|
|
); |
276
|
|
|
|
277
|
|
|
// Has the site opted in to IDC mitigation? |
278
|
|
|
if ( \Jetpack::sync_idc_optin() ) { |
279
|
|
|
$query_args['idc'] = true; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
if ( \Jetpack_Options::get_option( 'migrate_for_idc', false ) ) { |
283
|
|
|
$query_args['migrate_for_idc'] = true; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
$query_args['timeout'] = Settings::is_doing_cron() ? 30 : 15; |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Filters query parameters appended to the Sync request URL sent to WordPress.com. |
290
|
|
|
* |
291
|
|
|
* @since 4.7.0 |
292
|
|
|
* |
293
|
|
|
* @param array $query_args associative array of query parameters. |
294
|
|
|
*/ |
295
|
|
|
$query_args = apply_filters( 'jetpack_sync_send_data_query_args', $query_args ); |
296
|
|
|
|
297
|
|
|
$connection = new Jetpack_Connection(); |
298
|
|
|
$url = add_query_arg( $query_args, $connection->xmlrpc_api_url() ); |
299
|
|
|
|
300
|
|
|
// If we're currently updating to Jetpack 7.7, the IXR client may be missing briefly |
301
|
|
|
// because since 7.7 it's being autoloaded with Composer. |
302
|
|
|
if ( ! class_exists( '\\Jetpack_IXR_Client' ) ) { |
303
|
|
|
return new \WP_Error( |
304
|
|
|
'ixr_client_missing', |
|
|
|
|
305
|
|
|
esc_html__( 'Sync has been aborted because the IXR client is missing.', 'jetpack' ) |
306
|
|
|
); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
$rpc = new \Jetpack_IXR_Client( |
310
|
|
|
array( |
311
|
|
|
'url' => $url, |
312
|
|
|
'user_id' => JETPACK_MASTER_USER, |
313
|
|
|
'timeout' => $query_args['timeout'], |
314
|
|
|
) |
315
|
|
|
); |
316
|
|
|
|
317
|
|
|
$result = $rpc->query( 'jetpack.syncActions', $data ); |
318
|
|
|
|
319
|
|
|
if ( ! $result ) { |
320
|
|
|
return $rpc->get_jetpack_error(); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
$response = $rpc->getResponse(); |
324
|
|
|
|
325
|
|
|
// Check if WordPress.com IDC mitigation blocked the sync request. |
326
|
|
|
if ( is_array( $response ) && isset( $response['error_code'] ) ) { |
327
|
|
|
$error_code = $response['error_code']; |
328
|
|
|
$allowed_idc_error_codes = array( |
329
|
|
|
'jetpack_url_mismatch', |
330
|
|
|
'jetpack_home_url_mismatch', |
331
|
|
|
'jetpack_site_url_mismatch', |
332
|
|
|
); |
333
|
|
|
|
334
|
|
|
if ( in_array( $error_code, $allowed_idc_error_codes, true ) ) { |
335
|
|
|
\Jetpack_Options::update_option( |
336
|
|
|
'sync_error_idc', |
337
|
|
|
\Jetpack::get_sync_error_idc_option( $response ) |
338
|
|
|
); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
return new \WP_Error( |
342
|
|
|
'sync_error_idc', |
|
|
|
|
343
|
|
|
esc_html__( 'Sync has been blocked from WordPress.com because it would cause an identity crisis', 'jetpack' ) |
344
|
|
|
); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
return $response; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Kicks off the initial sync. |
352
|
|
|
* |
353
|
|
|
* @access public |
354
|
|
|
* @static |
355
|
|
|
* |
356
|
|
|
* @return bool|null False if sync is not allowed. |
357
|
|
|
*/ |
358
|
|
|
public static function do_initial_sync() { |
359
|
|
|
// Lets not sync if we are not suppose to. |
360
|
|
|
if ( ! self::sync_allowed() ) { |
361
|
|
|
return false; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
// Don't start new sync if a full sync is in process. |
365
|
|
|
$full_sync_module = Modules::get_module( 'full-sync' ); |
366
|
|
|
if ( $full_sync_module && $full_sync_module->is_started() && ! $full_sync_module->is_finished() ) { |
367
|
|
|
return false; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
$initial_sync_config = array( |
371
|
|
|
'options' => true, |
372
|
|
|
'functions' => true, |
373
|
|
|
'constants' => true, |
374
|
|
|
'users' => array( get_current_user_id() ), |
375
|
|
|
); |
376
|
|
|
|
377
|
|
|
if ( is_multisite() ) { |
378
|
|
|
$initial_sync_config['network_options'] = true; |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
self::do_full_sync( $initial_sync_config ); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* Kicks off a full sync. |
386
|
|
|
* |
387
|
|
|
* @access public |
388
|
|
|
* @static |
389
|
|
|
* |
390
|
|
|
* @param array $modules The sync modules should be included in this full sync. All will be included if null. |
|
|
|
|
391
|
|
|
* @return bool True if full sync was successfully started. |
392
|
|
|
*/ |
393
|
|
|
public static function do_full_sync( $modules = null ) { |
394
|
|
|
if ( ! self::sync_allowed() ) { |
395
|
|
|
return false; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
$full_sync_module = Modules::get_module( 'full-sync' ); |
399
|
|
|
|
400
|
|
|
if ( ! $full_sync_module ) { |
401
|
|
|
return false; |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
self::initialize_listener(); |
405
|
|
|
|
406
|
|
|
$full_sync_module->start( $modules ); |
407
|
|
|
|
408
|
|
|
return true; |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* Adds a cron schedule for regular syncing via cron, unless the schedule already exists. |
413
|
|
|
* |
414
|
|
|
* @access public |
415
|
|
|
* @static |
416
|
|
|
* |
417
|
|
|
* @param array $schedules The list of WordPress cron schedules prior to this filter. |
418
|
|
|
* @return array A list of WordPress cron schedules with the Jetpack sync interval added. |
419
|
|
|
*/ |
420
|
|
|
public static function jetpack_cron_schedule( $schedules ) { |
421
|
|
|
if ( ! isset( $schedules[ self::DEFAULT_SYNC_CRON_INTERVAL_NAME ] ) ) { |
422
|
|
|
$minutes = intval( self::DEFAULT_SYNC_CRON_INTERVAL_VALUE / 60 ); |
423
|
|
|
$display = ( 1 === $minutes ) ? |
424
|
|
|
__( 'Every minute', 'jetpack' ) : |
425
|
|
|
/* translators: %d is an integer indicating the number of minutes. */ |
426
|
|
|
sprintf( __( 'Every %d minutes', 'jetpack' ), $minutes ); |
427
|
|
|
$schedules[ self::DEFAULT_SYNC_CRON_INTERVAL_NAME ] = array( |
428
|
|
|
'interval' => self::DEFAULT_SYNC_CRON_INTERVAL_VALUE, |
429
|
|
|
'display' => $display, |
430
|
|
|
); |
431
|
|
|
} |
432
|
|
|
return $schedules; |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* Starts an incremental sync via cron. |
437
|
|
|
* |
438
|
|
|
* @access public |
439
|
|
|
* @static |
440
|
|
|
*/ |
441
|
|
|
public static function do_cron_sync() { |
442
|
|
|
self::do_cron_sync_by_type( 'sync' ); |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
/** |
446
|
|
|
* Starts a full sync via cron. |
447
|
|
|
* |
448
|
|
|
* @access public |
449
|
|
|
* @static |
450
|
|
|
*/ |
451
|
|
|
public static function do_cron_full_sync() { |
452
|
|
|
self::do_cron_sync_by_type( 'full_sync' ); |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
/** |
456
|
|
|
* Try to send actions until we run out of things to send, |
457
|
|
|
* or have to wait more than 15s before sending again, |
458
|
|
|
* or we hit a lock or some other sending issue |
459
|
|
|
* |
460
|
|
|
* @access public |
461
|
|
|
* @static |
462
|
|
|
* |
463
|
|
|
* @param string $type Sync type. Can be `sync` or `full_sync`. |
464
|
|
|
*/ |
465
|
|
|
public static function do_cron_sync_by_type( $type ) { |
466
|
|
|
if ( ! self::sync_allowed() || ( 'sync' !== $type && 'full_sync' !== $type ) ) { |
467
|
|
|
return; |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
self::initialize_sender(); |
471
|
|
|
|
472
|
|
|
$time_limit = Settings::get_setting( 'cron_sync_time_limit' ); |
473
|
|
|
$start_time = time(); |
474
|
|
|
|
475
|
|
|
do { |
476
|
|
|
$next_sync_time = self::$sender->get_next_sync_time( $type ); |
477
|
|
|
|
478
|
|
|
if ( $next_sync_time ) { |
479
|
|
|
$delay = $next_sync_time - time() + 1; |
480
|
|
|
if ( $delay > 15 ) { |
481
|
|
|
break; |
482
|
|
|
} elseif ( $delay > 0 ) { |
483
|
|
|
sleep( $delay ); |
484
|
|
|
} |
485
|
|
|
} |
486
|
|
|
|
487
|
|
|
$result = 'full_sync' === $type ? self::$sender->do_full_sync() : self::$sender->do_sync(); |
488
|
|
|
} while ( $result && ! is_wp_error( $result ) && ( $start_time + $time_limit ) > time() ); |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
/** |
492
|
|
|
* Initialize the sync listener. |
493
|
|
|
* |
494
|
|
|
* @access public |
495
|
|
|
* @static |
496
|
|
|
*/ |
497
|
|
|
public static function initialize_listener() { |
498
|
|
|
self::$listener = Listener::get_instance(); |
|
|
|
|
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
/** |
502
|
|
|
* Initializes the sync sender. |
503
|
|
|
* |
504
|
|
|
* @access public |
505
|
|
|
* @static |
506
|
|
|
*/ |
507
|
|
|
public static function initialize_sender() { |
508
|
|
|
self::$sender = Sender::get_instance(); |
|
|
|
|
509
|
|
|
add_filter( 'jetpack_sync_send_data', array( __CLASS__, 'send_data' ), 10, 6 ); |
510
|
|
|
} |
511
|
|
|
|
512
|
|
|
/** |
513
|
|
|
* Initializes sync for WooCommerce. |
514
|
|
|
* |
515
|
|
|
* @access public |
516
|
|
|
* @static |
517
|
|
|
*/ |
518
|
|
|
public static function initialize_woocommerce() { |
519
|
|
|
if ( false === class_exists( 'WooCommerce' ) ) { |
520
|
|
|
return; |
521
|
|
|
} |
522
|
|
|
add_filter( 'jetpack_sync_modules', array( __CLASS__, 'add_woocommerce_sync_module' ) ); |
523
|
|
|
} |
524
|
|
|
|
525
|
|
|
/** |
526
|
|
|
* Adds Woo's sync modules to existing modules for sending. |
527
|
|
|
* |
528
|
|
|
* @access public |
529
|
|
|
* @static |
530
|
|
|
* |
531
|
|
|
* @param array $sync_modules The list of sync modules declared prior to this filter. |
532
|
|
|
* @return array A list of sync modules that now includes Woo's modules. |
533
|
|
|
*/ |
534
|
|
|
public static function add_woocommerce_sync_module( $sync_modules ) { |
535
|
|
|
$sync_modules[] = 'Automattic\\Jetpack\\Sync\\Modules\\WooCommerce'; |
536
|
|
|
return $sync_modules; |
537
|
|
|
} |
538
|
|
|
|
539
|
|
|
/** |
540
|
|
|
* Initializes sync for WP Super Cache. |
541
|
|
|
* |
542
|
|
|
* @access public |
543
|
|
|
* @static |
544
|
|
|
*/ |
545
|
|
|
public static function initialize_wp_super_cache() { |
546
|
|
|
if ( false === function_exists( 'wp_cache_is_enabled' ) ) { |
547
|
|
|
return; |
548
|
|
|
} |
549
|
|
|
add_filter( 'jetpack_sync_modules', array( __CLASS__, 'add_wp_super_cache_sync_module' ) ); |
550
|
|
|
} |
551
|
|
|
|
552
|
|
|
/** |
553
|
|
|
* Adds WP Super Cache's sync modules to existing modules for sending. |
554
|
|
|
* |
555
|
|
|
* @access public |
556
|
|
|
* @static |
557
|
|
|
* |
558
|
|
|
* @param array $sync_modules The list of sync modules declared prior to this filer. |
559
|
|
|
* @return array A list of sync modules that now includes WP Super Cache's modules. |
560
|
|
|
*/ |
561
|
|
|
public static function add_wp_super_cache_sync_module( $sync_modules ) { |
562
|
|
|
$sync_modules[] = 'Automattic\\Jetpack\\Sync\\Modules\\WP_Super_Cache'; |
563
|
|
|
return $sync_modules; |
564
|
|
|
} |
565
|
|
|
|
566
|
|
|
/** |
567
|
|
|
* Sanitizes the name of sync's cron schedule. |
568
|
|
|
* |
569
|
|
|
* @access public |
570
|
|
|
* @static |
571
|
|
|
* |
572
|
|
|
* @param string $schedule The name of a WordPress cron schedule. |
573
|
|
|
* @return string The sanitized name of sync's cron schedule. |
574
|
|
|
*/ |
575
|
|
|
public static function sanitize_filtered_sync_cron_schedule( $schedule ) { |
576
|
|
|
$schedule = sanitize_key( $schedule ); |
577
|
|
|
$schedules = wp_get_schedules(); |
578
|
|
|
|
579
|
|
|
// Make sure that the schedule has actually been registered using the `cron_intervals` filter. |
580
|
|
|
if ( isset( $schedules[ $schedule ] ) ) { |
581
|
|
|
return $schedule; |
582
|
|
|
} |
583
|
|
|
|
584
|
|
|
return self::DEFAULT_SYNC_CRON_INTERVAL_NAME; |
585
|
|
|
} |
586
|
|
|
|
587
|
|
|
/** |
588
|
|
|
* Allows offsetting of start times for sync cron jobs. |
589
|
|
|
* |
590
|
|
|
* @access public |
591
|
|
|
* @static |
592
|
|
|
* |
593
|
|
|
* @param string $schedule The name of a cron schedule. |
594
|
|
|
* @param string $hook The hook that this method is responding to. |
595
|
|
|
* @return int The offset for the sync cron schedule. |
596
|
|
|
*/ |
597
|
|
|
public static function get_start_time_offset( $schedule = '', $hook = '' ) { |
598
|
|
|
$start_time_offset = is_multisite() |
599
|
|
|
? wp_rand( 0, ( 2 * self::DEFAULT_SYNC_CRON_INTERVAL_VALUE ) ) |
600
|
|
|
: 0; |
601
|
|
|
|
602
|
|
|
/** |
603
|
|
|
* Allows overriding the offset that the sync cron jobs will first run. This can be useful when scheduling |
604
|
|
|
* cron jobs across multiple sites in a network. |
605
|
|
|
* |
606
|
|
|
* @since 4.5.0 |
607
|
|
|
* |
608
|
|
|
* @param int $start_time_offset |
609
|
|
|
* @param string $hook |
610
|
|
|
* @param string $schedule |
611
|
|
|
*/ |
612
|
|
|
return intval( |
613
|
|
|
apply_filters( |
614
|
|
|
'jetpack_sync_cron_start_time_offset', |
615
|
|
|
$start_time_offset, |
616
|
|
|
$hook, |
617
|
|
|
$schedule |
618
|
|
|
) |
619
|
|
|
); |
620
|
|
|
} |
621
|
|
|
|
622
|
|
|
/** |
623
|
|
|
* Decides if a sync cron should be scheduled. |
624
|
|
|
* |
625
|
|
|
* @access public |
626
|
|
|
* @static |
627
|
|
|
* |
628
|
|
|
* @param string $schedule The name of a cron schedule. |
629
|
|
|
* @param string $hook The hook that this method is responding to. |
630
|
|
|
*/ |
631
|
|
|
public static function maybe_schedule_sync_cron( $schedule, $hook ) { |
632
|
|
|
if ( ! $hook ) { |
633
|
|
|
return; |
634
|
|
|
} |
635
|
|
|
$schedule = self::sanitize_filtered_sync_cron_schedule( $schedule ); |
636
|
|
|
|
637
|
|
|
$start_time = time() + self::get_start_time_offset( $schedule, $hook ); |
638
|
|
|
if ( ! wp_next_scheduled( $hook ) ) { |
639
|
|
|
// Schedule a job to send pending queue items once a minute. |
640
|
|
|
wp_schedule_event( $start_time, $schedule, $hook ); |
641
|
|
|
} elseif ( wp_get_schedule( $hook ) !== $schedule ) { |
642
|
|
|
// If the schedule has changed, update the schedule. |
643
|
|
|
wp_clear_scheduled_hook( $hook ); |
644
|
|
|
wp_schedule_event( $start_time, $schedule, $hook ); |
645
|
|
|
} |
646
|
|
|
} |
647
|
|
|
|
648
|
|
|
/** |
649
|
|
|
* Clears Jetpack sync cron jobs. |
650
|
|
|
* |
651
|
|
|
* @access public |
652
|
|
|
* @static |
653
|
|
|
*/ |
654
|
|
|
public static function clear_sync_cron_jobs() { |
655
|
|
|
wp_clear_scheduled_hook( 'jetpack_sync_cron' ); |
656
|
|
|
wp_clear_scheduled_hook( 'jetpack_sync_full_cron' ); |
657
|
|
|
} |
658
|
|
|
|
659
|
|
|
/** |
660
|
|
|
* Initializes Jetpack sync cron jobs. |
661
|
|
|
* |
662
|
|
|
* @access public |
663
|
|
|
* @static |
664
|
|
|
*/ |
665
|
|
|
public static function init_sync_cron_jobs() { |
666
|
|
|
add_filter( 'cron_schedules', array( __CLASS__, 'jetpack_cron_schedule' ) ); // phpcs:ignore WordPress.WP.CronInterval.ChangeDetected |
667
|
|
|
|
668
|
|
|
add_action( 'jetpack_sync_cron', array( __CLASS__, 'do_cron_sync' ) ); |
669
|
|
|
add_action( 'jetpack_sync_full_cron', array( __CLASS__, 'do_cron_full_sync' ) ); |
670
|
|
|
|
671
|
|
|
/** |
672
|
|
|
* Allows overriding of the default incremental sync cron schedule which defaults to once every 5 minutes. |
673
|
|
|
* |
674
|
|
|
* @since 4.3.2 |
675
|
|
|
* |
676
|
|
|
* @param string self::DEFAULT_SYNC_CRON_INTERVAL_NAME |
677
|
|
|
*/ |
678
|
|
|
$incremental_sync_cron_schedule = apply_filters( 'jetpack_sync_incremental_sync_interval', self::DEFAULT_SYNC_CRON_INTERVAL_NAME ); |
679
|
|
|
self::maybe_schedule_sync_cron( $incremental_sync_cron_schedule, 'jetpack_sync_cron' ); |
680
|
|
|
|
681
|
|
|
/** |
682
|
|
|
* Allows overriding of the full sync cron schedule which defaults to once every 5 minutes. |
683
|
|
|
* |
684
|
|
|
* @since 4.3.2 |
685
|
|
|
* |
686
|
|
|
* @param string self::DEFAULT_SYNC_CRON_INTERVAL_NAME |
687
|
|
|
*/ |
688
|
|
|
$full_sync_cron_schedule = apply_filters( 'jetpack_sync_full_sync_interval', self::DEFAULT_SYNC_CRON_INTERVAL_NAME ); |
689
|
|
|
self::maybe_schedule_sync_cron( $full_sync_cron_schedule, 'jetpack_sync_full_cron' ); |
690
|
|
|
} |
691
|
|
|
|
692
|
|
|
/** |
693
|
|
|
* Perform maintenance when a plugin upgrade occurs. |
694
|
|
|
* |
695
|
|
|
* @access public |
696
|
|
|
* @static |
697
|
|
|
* |
698
|
|
|
* @param string $new_version New version of the plugin. |
|
|
|
|
699
|
|
|
* @param string $old_version Old version of the plugin. |
|
|
|
|
700
|
|
|
*/ |
701
|
|
|
public static function cleanup_on_upgrade( $new_version = null, $old_version = null ) { |
702
|
|
|
if ( wp_next_scheduled( 'jetpack_sync_send_db_checksum' ) ) { |
703
|
|
|
wp_clear_scheduled_hook( 'jetpack_sync_send_db_checksum' ); |
704
|
|
|
} |
705
|
|
|
|
706
|
|
|
$is_new_sync_upgrade = version_compare( $old_version, '4.2', '>=' ); |
707
|
|
|
if ( ! empty( $old_version ) && $is_new_sync_upgrade && version_compare( $old_version, '4.5', '<' ) ) { |
708
|
|
|
self::clear_sync_cron_jobs(); |
709
|
|
|
Settings::update_settings( |
710
|
|
|
array( |
711
|
|
|
'render_filtered_content' => Defaults::$default_render_filtered_content, |
712
|
|
|
) |
713
|
|
|
); |
714
|
|
|
} |
715
|
|
|
|
716
|
|
|
Health::on_jetpack_upgraded(); |
717
|
|
|
} |
718
|
|
|
|
719
|
|
|
/** |
720
|
|
|
* Get syncing status for the given fields. |
721
|
|
|
* |
722
|
|
|
* @access public |
723
|
|
|
* @static |
724
|
|
|
* |
725
|
|
|
* @param string|null $fields A comma-separated string of the fields to include in the array from the JSON response. |
726
|
|
|
* @return array An associative array with the status report. |
727
|
|
|
*/ |
728
|
|
|
public static function get_sync_status( $fields = null ) { |
729
|
|
|
self::initialize_sender(); |
730
|
|
|
|
731
|
|
|
$sync_module = Modules::get_module( 'full-sync' ); |
732
|
|
|
$queue = self::$sender->get_sync_queue(); |
733
|
|
|
|
734
|
|
|
// _get_cron_array can be false |
735
|
|
|
$cron_timestamps = ( _get_cron_array() ) ? array_keys( _get_cron_array() ) : array(); |
736
|
|
|
$next_cron = ( ! empty( $cron_timestamps ) ) ? $cron_timestamps[0] - time() : ''; |
737
|
|
|
|
738
|
|
|
$checksums = array(); |
739
|
|
|
|
740
|
|
|
if ( ! empty( $fields ) ) { |
741
|
|
|
$store = new Replicastore(); |
742
|
|
|
$fields_params = array_map( 'trim', explode( ',', $fields ) ); |
743
|
|
|
|
744
|
|
|
if ( in_array( 'posts_checksum', $fields_params, true ) ) { |
745
|
|
|
$checksums['posts_checksum'] = $store->posts_checksum(); |
746
|
|
|
} |
747
|
|
|
if ( in_array( 'comments_checksum', $fields_params, true ) ) { |
748
|
|
|
$checksums['comments_checksum'] = $store->comments_checksum(); |
749
|
|
|
} |
750
|
|
|
if ( in_array( 'post_meta_checksum', $fields_params, true ) ) { |
751
|
|
|
$checksums['post_meta_checksum'] = $store->post_meta_checksum(); |
752
|
|
|
} |
753
|
|
|
if ( in_array( 'comment_meta_checksum', $fields_params, true ) ) { |
754
|
|
|
$checksums['comment_meta_checksum'] = $store->comment_meta_checksum(); |
755
|
|
|
} |
756
|
|
|
} |
757
|
|
|
|
758
|
|
|
$full_sync_status = ( $sync_module ) ? $sync_module->get_status() : array(); |
759
|
|
|
|
760
|
|
|
$full_queue = self::$sender->get_full_sync_queue(); |
761
|
|
|
|
762
|
|
|
$result = array_merge( |
763
|
|
|
$full_sync_status, |
764
|
|
|
$checksums, |
765
|
|
|
array( |
766
|
|
|
'cron_size' => count( $cron_timestamps ), |
767
|
|
|
'next_cron' => $next_cron, |
768
|
|
|
'queue_size' => $queue->size(), |
769
|
|
|
'queue_lag' => $queue->lag(), |
770
|
|
|
'queue_next_sync' => ( self::$sender->get_next_sync_time( 'sync' ) - microtime( true ) ), |
771
|
|
|
'full_queue_next_sync' => ( self::$sender->get_next_sync_time( 'full_sync' ) - microtime( true ) ), |
772
|
|
|
) |
773
|
|
|
); |
774
|
|
|
|
775
|
|
|
// Verify $sync_module is not false |
776
|
|
|
if ( ( $sync_module ) && false === strpos( get_class( $sync_module ), 'Full_Sync_Immediately' ) ) { |
777
|
|
|
$result['full_queue_size'] = $full_queue->size(); |
778
|
|
|
$result['full_queue_lag'] = $full_queue->lag(); |
779
|
|
|
} |
780
|
|
|
return $result; |
781
|
|
|
} |
782
|
|
|
} |
783
|
|
|
|
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.