Completed
Push — branch-7.7 ( b57ae6...ab702e )
by Jeremy
40:58 queued 33:41
created

Actions::send_data()   C

Complexity

Conditions 9
Paths 40

Size

Total Lines 83

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 40
nop 6
dl 0
loc 83
rs 6.8153
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
14
/**
15
 * The role of this class is to hook the Sync subsystem into WordPress - when to listen for actions,
16
 * when to send, when to perform a full sync, etc.
17
 *
18
 * It also binds the action to send data to WPCOM to Jetpack's XMLRPC client object.
19
 */
20
class Actions {
21
	/**
22
	 * A variable to hold a sync sender object.
23
	 *
24
	 * @access public
25
	 * @static
26
	 *
27
	 * @var Automattic\Jetpack\Sync\Sender
28
	 */
29
	public static $sender = null;
30
31
	/**
32
	 * A variable to hold a sync listener object.
33
	 *
34
	 * @access public
35
	 * @static
36
	 *
37
	 * @var Automattic\Jetpack\Sync\Listener
38
	 */
39
	public static $listener = null;
40
41
	/**
42
	 * Name of the sync cron schedule.
43
	 *
44
	 * @access public
45
	 *
46
	 * @var string
47
	 */
48
	const DEFAULT_SYNC_CRON_INTERVAL_NAME = 'jetpack_sync_interval';
49
50
	/**
51
	 * Interval between the last and the next sync cron action.
52
	 *
53
	 * @access public
54
	 *
55
	 * @var int
56
	 */
57
	const DEFAULT_SYNC_CRON_INTERVAL_VALUE = 300; // 5 * MINUTE_IN_SECONDS;
58
59
	/**
60
	 * Initialize Sync for cron jobs, set up listeners for WordPress Actions,
61
	 * and set up a shut-down action for sending actions to WordPress.com
62
	 *
63
	 * @access public
64
	 * @static
65
	 */
66
	public static function init() {
67
		// Everything below this point should only happen if we're a valid sync site.
68
		if ( ! self::sync_allowed() ) {
69
			return;
70
		}
71
72
		if ( self::sync_via_cron_allowed() ) {
73
			self::init_sync_cron_jobs();
74
		} elseif ( wp_next_scheduled( 'jetpack_sync_cron' ) ) {
75
			self::clear_sync_cron_jobs();
76
		}
77
		// When importing via cron, do not sync.
78
		add_action( 'wp_cron_importer_hook', array( __CLASS__, 'set_is_importing_true' ), 1 );
79
80
		// Sync connected user role changes to WordPress.com.
81
		Users::init();
82
83
		// Publicize filter to prevent publicizing blacklisted post types.
84
		add_filter( 'publicize_should_publicize_published_post', array( __CLASS__, 'prevent_publicize_blacklisted_posts' ), 10, 2 );
85
86
		/**
87
		 * Fires on every request before default loading sync listener code.
88
		 * Return false to not load sync listener code that monitors common
89
		 * WP actions to be serialized.
90
		 *
91
		 * By default this returns true for cron jobs, non-GET-requests, or requests where the
92
		 * user is logged-in.
93
		 *
94
		 * @since 4.2.0
95
		 *
96
		 * @param bool should we load sync listener code for this request
97
		 */
98
		if ( apply_filters( 'jetpack_sync_listener_should_load', true ) ) {
99
			self::initialize_listener();
100
		}
101
102
		add_action( 'init', array( __CLASS__, 'add_sender_shutdown' ), 90 );
103
	}
104
105
	/**
106
	 * Prepares sync to send actions on shutdown for the current request.
107
	 *
108
	 * @access public
109
	 * @static
110
	 */
111
	public static function add_sender_shutdown() {
112
		/**
113
		 * Fires on every request before default loading sync sender code.
114
		 * Return false to not load sync sender code that serializes pending
115
		 * data and sends it to WPCOM for processing.
116
		 *
117
		 * By default this returns true for cron jobs, POST requests, admin requests, or requests
118
		 * by users who can manage_options.
119
		 *
120
		 * @since 4.2.0
121
		 *
122
		 * @param bool should we load sync sender code for this request
123
		 */
124
		if ( apply_filters(
125
			'jetpack_sync_sender_should_load',
126
			self::should_initialize_sender()
127
		) ) {
128
			self::initialize_sender();
129
			add_action( 'shutdown', array( self::$sender, 'do_sync' ) );
130
			add_action( 'shutdown', array( self::$sender, 'do_full_sync' ) );
131
		}
132
	}
133
134
	/**
135
	 * Decides if the sender should run on shutdown for this request.
136
	 *
137
	 * @access public
138
	 * @static
139
	 *
140
	 * @return bool
141
	 */
142
	public static function should_initialize_sender() {
143
		if ( Constants::is_true( 'DOING_CRON' ) ) {
144
			return self::sync_via_cron_allowed();
145
		}
146
147
		if ( isset( $_SERVER['REQUEST_METHOD'] ) && 'POST' === $_SERVER['REQUEST_METHOD'] ) {
148
			return true;
149
		}
150
151
		if ( current_user_can( 'manage_options' ) ) {
152
			return true;
153
		}
154
155
		if ( is_admin() ) {
156
			return true;
157
		}
158
159
		if ( defined( 'PHPUNIT_JETPACK_TESTSUITE' ) ) {
160
			return true;
161
		}
162
163
		if ( Constants::get_constant( 'WP_CLI' ) ) {
164
			return true;
165
		}
166
167
		return false;
168
	}
169
170
	/**
171
	 * Decides if sync should run at all during this request.
172
	 *
173
	 * @access public
174
	 * @static
175
	 *
176
	 * @return bool
177
	 */
178
	public static function sync_allowed() {
179
		if ( defined( 'PHPUNIT_JETPACK_TESTSUITE' ) ) {
180
			return true;
181
		}
182
183
		if ( ! Settings::is_sync_enabled() ) {
184
			return false;
185
		}
186
187
		$status = new Status();
188
		if ( $status->is_development_mode() ) {
189
			return false;
190
		}
191
192
		if ( \Jetpack::is_staging_site() ) {
193
			return false;
194
		}
195
196
		$connection = new Jetpack_Connection();
197
		if ( ! $connection->is_active() ) {
198
			if ( ! doing_action( 'jetpack_user_authorized' ) ) {
199
				return false;
200
			}
201
		}
202
203
		return true;
204
	}
205
206
	/**
207
	 * Determines if syncing during a cron job is allowed.
208
	 *
209
	 * @access public
210
	 * @static
211
	 *
212
	 * @return bool|int
213
	 */
214
	public static function sync_via_cron_allowed() {
215
		return ( Settings::get_setting( 'sync_via_cron' ) );
216
	}
217
218
	/**
219
	 * Decides if the given post should be Publicized based on its type.
220
	 *
221
	 * @access public
222
	 * @static
223
	 *
224
	 * @param bool     $should_publicize  Publicize status prior to this filter running.
225
	 * @param \WP_Post $post              The post to test for Publicizability.
226
	 * @return bool
227
	 */
228
	public static function prevent_publicize_blacklisted_posts( $should_publicize, $post ) {
229
		if ( in_array( $post->post_type, Settings::get_setting( 'post_types_blacklist' ), true ) ) {
230
			return false;
231
		}
232
233
		return $should_publicize;
234
	}
235
236
	/**
237
	 * Set an importing flag to `true` in sync settings.
238
	 *
239
	 * @access public
240
	 * @static
241
	 */
242
	public static function set_is_importing_true() {
243
		Settings::set_importing( true );
244
	}
245
246
	/**
247
	 * Sends data to WordPress.com via an XMLRPC request.
248
	 *
249
	 * @access public
250
	 * @static
251
	 *
252
	 * @param object $data                   Data relating to a sync action.
253
	 * @param string $codec_name             The name of the codec that encodes the data.
254
	 * @param float  $sent_timestamp         Current server time so we can compensate for clock differences.
255
	 * @param string $queue_id               The queue the action belongs to, sync or full_sync.
256
	 * @param float  $checkout_duration      Time spent retrieving queue items from the DB.
257
	 * @param float  $preprocess_duration    Time spent converting queue items into data to send.
258
	 * @return Jetpack_Error|mixed|WP_Error  The result of the sending request.
259
	 */
260
	public static function send_data( $data, $codec_name, $sent_timestamp, $queue_id, $checkout_duration, $preprocess_duration ) {
261
		$query_args = array(
262
			'sync'      => '1',             // Add an extra parameter to the URL so we can tell it's a sync action.
263
			'codec'     => $codec_name,
264
			'timestamp' => $sent_timestamp,
265
			'queue'     => $queue_id,
266
			'home'      => Functions::home_url(),  // Send home url option to check for Identity Crisis server-side.
267
			'siteurl'   => Functions::site_url(),  // Send siteurl option to check for Identity Crisis server-side.
268
			'cd'        => sprintf( '%.4f', $checkout_duration ),
269
			'pd'        => sprintf( '%.4f', $preprocess_duration ),
270
		);
271
272
		// Has the site opted in to IDC mitigation?
273
		if ( \Jetpack::sync_idc_optin() ) {
274
			$query_args['idc'] = true;
275
		}
276
277
		if ( \Jetpack_Options::get_option( 'migrate_for_idc', false ) ) {
278
			$query_args['migrate_for_idc'] = true;
279
		}
280
281
		$query_args['timeout'] = Settings::is_doing_cron() ? 30 : 15;
282
283
		/**
284
		 * Filters query parameters appended to the Sync request URL sent to WordPress.com.
285
		 *
286
		 * @since 4.7.0
287
		 *
288
		 * @param array $query_args associative array of query parameters.
289
		 */
290
		$query_args = apply_filters( 'jetpack_sync_send_data_query_args', $query_args );
291
292
		$url = add_query_arg( $query_args, \Jetpack::xmlrpc_api_url() );
293
294
		// If we're currently updating to Jetpack 7.7, the IXR client may be missing briefly
295
		// because since 7.7 it's being autoloaded with Composer.
296
		if ( ! class_exists( '\\Jetpack_IXR_Client' ) ) {
297
			return new \WP_Error(
298
				'ixr_client_missing',
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'ixr_client_missing'.

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...
299
				esc_html__( 'Sync has been aborted because the IXR client is missing.', 'jetpack' )
300
			);
301
		}
302
303
		$rpc = new \Jetpack_IXR_Client(
304
			array(
305
				'url'     => $url,
306
				'user_id' => JETPACK_MASTER_USER,
307
				'timeout' => $query_args['timeout'],
308
			)
309
		);
310
311
		$result = $rpc->query( 'jetpack.syncActions', $data );
312
313
		if ( ! $result ) {
314
			return $rpc->get_jetpack_error();
315
		}
316
317
		$response = $rpc->getResponse();
318
319
		// Check if WordPress.com IDC mitigation blocked the sync request.
320
		if ( is_array( $response ) && isset( $response['error_code'] ) ) {
321
			$error_code              = $response['error_code'];
322
			$allowed_idc_error_codes = array(
323
				'jetpack_url_mismatch',
324
				'jetpack_home_url_mismatch',
325
				'jetpack_site_url_mismatch',
326
			);
327
328
			if ( in_array( $error_code, $allowed_idc_error_codes, true ) ) {
329
				\Jetpack_Options::update_option(
330
					'sync_error_idc',
331
					\Jetpack::get_sync_error_idc_option( $response )
332
				);
333
			}
334
335
			return new \WP_Error(
336
				'sync_error_idc',
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'sync_error_idc'.

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...
337
				esc_html__( 'Sync has been blocked from WordPress.com because it would cause an identity crisis', 'jetpack' )
338
			);
339
		}
340
341
		return $response;
342
	}
343
344
	/**
345
	 * Kicks off the initial sync.
346
	 *
347
	 * @access public
348
	 * @static
349
	 *
350
	 * @return bool|null False if sync is not allowed.
351
	 */
352
	public static function do_initial_sync() {
353
		// Lets not sync if we are not suppose to.
354
		if ( ! self::sync_allowed() ) {
355
			return false;
356
		}
357
358
		$initial_sync_config = array(
359
			'options'   => true,
360
			'functions' => true,
361
			'constants' => true,
362
			'users'     => array( get_current_user_id() ),
363
		);
364
365
		if ( is_multisite() ) {
366
			$initial_sync_config['network_options'] = true;
367
		}
368
369
		self::do_full_sync( $initial_sync_config );
370
	}
371
372
	/**
373
	 * Kicks off a full sync.
374
	 *
375
	 * @access public
376
	 * @static
377
	 *
378
	 * @param array $modules  The sync modules should be included in this full sync. All will be included if null.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $modules not be array|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
379
	 * @return bool           True if full sync was successfully started.
380
	 */
381
	public static function do_full_sync( $modules = null ) {
382
		if ( ! self::sync_allowed() ) {
383
			return false;
384
		}
385
386
		$full_sync_module = Modules::get_module( 'full-sync' );
387
388
		if ( ! $full_sync_module ) {
389
			return false;
390
		}
391
392
		self::initialize_listener();
393
394
		$full_sync_module->start( $modules );
395
396
		return true;
397
	}
398
399
	/**
400
	 * Adds a cron schedule for regular syncing via cron, unless the schedule already exists.
401
	 *
402
	 * @access public
403
	 * @static
404
	 *
405
	 * @param array $schedules  The list of WordPress cron schedules prior to this filter.
406
	 * @return array            A list of WordPress cron schedules with the Jetpack sync interval added.
407
	 */
408
	public static function jetpack_cron_schedule( $schedules ) {
409
		if ( ! isset( $schedules[ self::DEFAULT_SYNC_CRON_INTERVAL_NAME ] ) ) {
410
			$minutes = intval( self::DEFAULT_SYNC_CRON_INTERVAL_VALUE / 60 );
411
			$display = ( 1 === $minutes ) ?
412
				__( 'Every minute', 'jetpack' ) :
413
				/* translators: %d is an integer indicating the number of minutes. */
414
				sprintf( __( 'Every %d minutes', 'jetpack' ), $minutes );
415
			$schedules[ self::DEFAULT_SYNC_CRON_INTERVAL_NAME ] = array(
416
				'interval' => self::DEFAULT_SYNC_CRON_INTERVAL_VALUE,
417
				'display'  => $display,
418
			);
419
		}
420
		return $schedules;
421
	}
422
423
	/**
424
	 * Starts an incremental sync via cron.
425
	 *
426
	 * @access public
427
	 * @static
428
	 */
429
	public static function do_cron_sync() {
430
		self::do_cron_sync_by_type( 'sync' );
431
	}
432
433
	/**
434
	 * Starts a full sync via cron.
435
	 *
436
	 * @access public
437
	 * @static
438
	 */
439
	public static function do_cron_full_sync() {
440
		self::do_cron_sync_by_type( 'full_sync' );
441
	}
442
443
	/**
444
	 * Try to send actions until we run out of things to send,
445
	 * or have to wait more than 15s before sending again,
446
	 * or we hit a lock or some other sending issue
447
	 *
448
	 * @access public
449
	 * @static
450
	 *
451
	 * @param string $type Sync type. Can be `sync` or `full_sync`.
452
	 */
453
	public static function do_cron_sync_by_type( $type ) {
454
		if ( ! self::sync_allowed() || ( 'sync' !== $type && 'full_sync' !== $type ) ) {
455
			return;
456
		}
457
458
		self::initialize_sender();
459
460
		$time_limit = Settings::get_setting( 'cron_sync_time_limit' );
461
		$start_time = time();
462
463
		do {
464
			$next_sync_time = self::$sender->get_next_sync_time( $type );
465
466
			if ( $next_sync_time ) {
467
				$delay = $next_sync_time - time() + 1;
468
				if ( $delay > 15 ) {
469
					break;
470
				} elseif ( $delay > 0 ) {
471
					sleep( $delay );
472
				}
473
			}
474
475
			$result = 'full_sync' === $type ? self::$sender->do_full_sync() : self::$sender->do_sync();
476
		} while ( $result && ! is_wp_error( $result ) && ( $start_time + $time_limit ) > time() );
477
	}
478
479
	/**
480
	 * Initialize the sync listener.
481
	 *
482
	 * @access public
483
	 * @static
484
	 */
485
	public static function initialize_listener() {
486
		self::$listener = Listener::get_instance();
487
	}
488
489
	/**
490
	 * Initializes the sync sender.
491
	 *
492
	 * @access public
493
	 * @static
494
	 */
495
	public static function initialize_sender() {
496
		self::$sender = Sender::get_instance();
497
		add_filter( 'jetpack_sync_send_data', array( __CLASS__, 'send_data' ), 10, 6 );
498
	}
499
500
	/**
501
	 * Initializes sync for WooCommerce.
502
	 *
503
	 * @access public
504
	 * @static
505
	 */
506
	public static function initialize_woocommerce() {
507
		if ( false === class_exists( 'WooCommerce' ) ) {
508
			return;
509
		}
510
		add_filter( 'jetpack_sync_modules', array( __CLASS__, 'add_woocommerce_sync_module' ) );
511
	}
512
513
	/**
514
	 * Adds Woo's sync modules to existing modules for sending.
515
	 *
516
	 * @access public
517
	 * @static
518
	 *
519
	 * @param array $sync_modules The list of sync modules declared prior to this filter.
520
	 * @return array A list of sync modules that now includes Woo's modules.
521
	 */
522
	public static function add_woocommerce_sync_module( $sync_modules ) {
523
		$sync_modules[] = 'Automattic\\Jetpack\\Sync\\Modules\\WooCommerce';
524
		return $sync_modules;
525
	}
526
527
	/**
528
	 * Initializes sync for WP Super Cache.
529
	 *
530
	 * @access public
531
	 * @static
532
	 */
533
	public static function initialize_wp_super_cache() {
534
		if ( false === function_exists( 'wp_cache_is_enabled' ) ) {
535
			return;
536
		}
537
		add_filter( 'jetpack_sync_modules', array( __CLASS__, 'add_wp_super_cache_sync_module' ) );
538
	}
539
540
	/**
541
	 * Adds WP Super Cache's sync modules to existing modules for sending.
542
	 *
543
	 * @access public
544
	 * @static
545
	 *
546
	 * @param array $sync_modules The list of sync modules declared prior to this filer.
547
	 * @return array A list of sync modules that now includes WP Super Cache's modules.
548
	 */
549
	public static function add_wp_super_cache_sync_module( $sync_modules ) {
550
		$sync_modules[] = 'Automattic\\Jetpack\\Sync\\Modules\\WP_Super_Cache';
551
		return $sync_modules;
552
	}
553
554
	/**
555
	 * Sanitizes the name of sync's cron schedule.
556
	 *
557
	 * @access public
558
	 * @static
559
	 *
560
	 * @param string $schedule The name of a WordPress cron schedule.
561
	 * @return string The sanitized name of sync's cron schedule.
562
	 */
563
	public static function sanitize_filtered_sync_cron_schedule( $schedule ) {
564
		$schedule  = sanitize_key( $schedule );
565
		$schedules = wp_get_schedules();
566
567
		// Make sure that the schedule has actually been registered using the `cron_intervals` filter.
568
		if ( isset( $schedules[ $schedule ] ) ) {
569
			return $schedule;
570
		}
571
572
		return self::DEFAULT_SYNC_CRON_INTERVAL_NAME;
573
	}
574
575
	/**
576
	 * Allows offsetting of start times for sync cron jobs.
577
	 *
578
	 * @access public
579
	 * @static
580
	 *
581
	 * @param string $schedule The name of a cron schedule.
582
	 * @param string $hook     The hook that this method is responding to.
583
	 * @return int The offset for the sync cron schedule.
584
	 */
585
	public static function get_start_time_offset( $schedule = '', $hook = '' ) {
586
		$start_time_offset = is_multisite()
587
			? wp_rand( 0, ( 2 * self::DEFAULT_SYNC_CRON_INTERVAL_VALUE ) )
588
			: 0;
589
590
		/**
591
		 * Allows overriding the offset that the sync cron jobs will first run. This can be useful when scheduling
592
		 * cron jobs across multiple sites in a network.
593
		 *
594
		 * @since 4.5.0
595
		 *
596
		 * @param int    $start_time_offset
597
		 * @param string $hook
598
		 * @param string $schedule
599
		 */
600
		return intval(
601
			apply_filters(
602
				'jetpack_sync_cron_start_time_offset',
603
				$start_time_offset,
604
				$hook,
605
				$schedule
606
			)
607
		);
608
	}
609
610
	/**
611
	 * Decides if a sync cron should be scheduled.
612
	 *
613
	 * @access public
614
	 * @static
615
	 *
616
	 * @param string $schedule The name of a cron schedule.
617
	 * @param string $hook     The hook that this method is responding to.
618
	 */
619
	public static function maybe_schedule_sync_cron( $schedule, $hook ) {
620
		if ( ! $hook ) {
621
			return;
622
		}
623
		$schedule = self::sanitize_filtered_sync_cron_schedule( $schedule );
624
625
		$start_time = time() + self::get_start_time_offset( $schedule, $hook );
626
		if ( ! wp_next_scheduled( $hook ) ) {
627
			// Schedule a job to send pending queue items once a minute.
628
			wp_schedule_event( $start_time, $schedule, $hook );
629
		} elseif ( wp_get_schedule( $hook ) !== $schedule ) {
630
			// If the schedule has changed, update the schedule.
631
			wp_clear_scheduled_hook( $hook );
632
			wp_schedule_event( $start_time, $schedule, $hook );
633
		}
634
	}
635
636
	/**
637
	 * Clears Jetpack sync cron jobs.
638
	 *
639
	 * @access public
640
	 * @static
641
	 */
642
	public static function clear_sync_cron_jobs() {
643
		wp_clear_scheduled_hook( 'jetpack_sync_cron' );
644
		wp_clear_scheduled_hook( 'jetpack_sync_full_cron' );
645
	}
646
647
	/**
648
	 * Initializes Jetpack sync cron jobs.
649
	 *
650
	 * @access public
651
	 * @static
652
	 */
653
	public static function init_sync_cron_jobs() {
654
		add_filter( 'cron_schedules', array( __CLASS__, 'jetpack_cron_schedule' ) );
655
656
		add_action( 'jetpack_sync_cron', array( __CLASS__, 'do_cron_sync' ) );
657
		add_action( 'jetpack_sync_full_cron', array( __CLASS__, 'do_cron_full_sync' ) );
658
659
		/**
660
		 * Allows overriding of the default incremental sync cron schedule which defaults to once every 5 minutes.
661
		 *
662
		 * @since 4.3.2
663
		 *
664
		 * @param string self::DEFAULT_SYNC_CRON_INTERVAL_NAME
665
		 */
666
		$incremental_sync_cron_schedule = apply_filters( 'jetpack_sync_incremental_sync_interval', self::DEFAULT_SYNC_CRON_INTERVAL_NAME );
667
		self::maybe_schedule_sync_cron( $incremental_sync_cron_schedule, 'jetpack_sync_cron' );
668
669
		/**
670
		 * Allows overriding of the full sync cron schedule which defaults to once every 5 minutes.
671
		 *
672
		 * @since 4.3.2
673
		 *
674
		 * @param string self::DEFAULT_SYNC_CRON_INTERVAL_NAME
675
		 */
676
		$full_sync_cron_schedule = apply_filters( 'jetpack_sync_full_sync_interval', self::DEFAULT_SYNC_CRON_INTERVAL_NAME );
677
		self::maybe_schedule_sync_cron( $full_sync_cron_schedule, 'jetpack_sync_full_cron' );
678
	}
679
680
	/**
681
	 * Perform maintenance when a plugin upgrade occurs.
682
	 *
683
	 * @access public
684
	 * @static
685
	 *
686
	 * @param string $new_version New version of the plugin.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $new_version not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
687
	 * @param string $old_version Old version of the plugin.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $old_version not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
688
	 */
689
	public static function cleanup_on_upgrade( $new_version = null, $old_version = null ) {
690
		if ( wp_next_scheduled( 'jetpack_sync_send_db_checksum' ) ) {
691
			wp_clear_scheduled_hook( 'jetpack_sync_send_db_checksum' );
692
		}
693
694
		$is_new_sync_upgrade = version_compare( $old_version, '4.2', '>=' );
695
		if ( ! empty( $old_version ) && $is_new_sync_upgrade && version_compare( $old_version, '4.5', '<' ) ) {
696
			self::clear_sync_cron_jobs();
697
			Settings::update_settings(
698
				array(
699
					'render_filtered_content' => Defaults::$default_render_filtered_content,
0 ignored issues
show
Bug introduced by
The property default_render_filtered_content cannot be accessed from this context as it is declared private in class Automattic\Jetpack\Sync\Defaults.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
700
				)
701
			);
702
		}
703
	}
704
705
	/**
706
	 * Get syncing status for the given fields.
707
	 *
708
	 * @access public
709
	 * @static
710
	 *
711
	 * @param string|null $fields A comma-separated string of the fields to include in the array from the JSON response.
712
	 * @return array An associative array with the status report.
713
	 */
714
	public static function get_sync_status( $fields = null ) {
715
		self::initialize_sender();
716
717
		$sync_module     = Modules::get_module( 'full-sync' );
718
		$queue           = self::$sender->get_sync_queue();
719
		$full_queue      = self::$sender->get_full_sync_queue();
720
		$cron_timestamps = array_keys( _get_cron_array() );
721
		$next_cron       = $cron_timestamps[0] - time();
722
723
		$checksums = array();
724
725
		if ( ! empty( $fields ) ) {
726
			$store         = new Replicastore();
727
			$fields_params = array_map( 'trim', explode( ',', $fields ) );
728
729
			if ( in_array( 'posts_checksum', $fields_params, true ) ) {
730
				$checksums['posts_checksum'] = $store->posts_checksum();
731
			}
732
			if ( in_array( 'comments_checksum', $fields_params, true ) ) {
733
				$checksums['comments_checksum'] = $store->comments_checksum();
734
			}
735
			if ( in_array( 'post_meta_checksum', $fields_params, true ) ) {
736
				$checksums['post_meta_checksum'] = $store->post_meta_checksum();
737
			}
738
			if ( in_array( 'comment_meta_checksum', $fields_params, true ) ) {
739
				$checksums['comment_meta_checksum'] = $store->comment_meta_checksum();
740
			}
741
		}
742
743
		$full_sync_status = ( $sync_module ) ? $sync_module->get_status() : array();
744
745
		return array_merge(
746
			$full_sync_status,
747
			$checksums,
748
			array(
749
				'cron_size'            => count( $cron_timestamps ),
750
				'next_cron'            => $next_cron,
751
				'queue_size'           => $queue->size(),
752
				'queue_lag'            => $queue->lag(),
753
				'queue_next_sync'      => ( self::$sender->get_next_sync_time( 'sync' ) - microtime( true ) ),
754
				'full_queue_size'      => $full_queue->size(),
755
				'full_queue_lag'       => $full_queue->lag(),
756
				'full_queue_next_sync' => ( self::$sender->get_next_sync_time( 'full_sync' ) - microtime( true ) ),
757
			)
758
		);
759
	}
760
}
761