Completed
Push — branch-4.2 ( 820ca9...55d645 )
by Jeremy
14:43 queued 04:13
created

Jetpack_Sync_Client::full_sync_updates()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 11
rs 9.4285
1
<?php
2
require_once dirname( __FILE__ ) . '/class.jetpack-sync-deflate-codec.php';
3
require_once dirname( __FILE__ ) . '/class.jetpack-sync-queue.php';
4
require_once dirname( __FILE__ ) . '/class.jetpack-sync-functions.php';
5
require_once dirname( __FILE__ ) . '/class.jetpack-sync-full.php';
6
require_once dirname( __FILE__ ) . '/class.jetpack-sync-defaults.php';
7
8
class Jetpack_Sync_Client {
9
10
	const CONSTANTS_CHECKSUM_OPTION_NAME = 'jetpack_constants_sync_checksum';
11
	const CALLABLES_CHECKSUM_OPTION_NAME = 'jetpack_callables_sync_checksum';
12
	const SYNC_THROTTLE_OPTION_NAME = 'jetpack_sync_min_wait';
13
	const LAST_SYNC_TIME_OPTION_NAME = 'jetpack_last_sync_time';
14
	const CALLABLES_AWAIT_TRANSIENT_NAME = 'jetpack_sync_callables_await';
15
	const CONSTANTS_AWAIT_TRANSIENT_NAME = 'jetpack_sync_constants_await';
16
	const SETTINGS_OPTION_PREFIX = 'jetpack_sync_settings_';
17
	
18
	private static $valid_settings = array( 'dequeue_max_bytes' => true, 'upload_max_bytes' => true, 'upload_max_rows' => true, 'sync_wait_time' => true );
19
20
	private $dequeue_max_bytes;
21
	private $upload_max_bytes;
22
	private $upload_max_rows;
23
	private $sync_queue;
24
	private $full_sync_client;
25
	private $codec;
26
	private $options_whitelist;
27
	private $constants_whitelist;
28
	private $meta_types = array( 'post', 'comment' );
29
	private $callable_whitelist;
30
	private $network_options_whitelist;
31
	private $taxonomy_whitelist;
32
	private $is_multisite;
33
34
	// singleton functions
35
	private static $instance;
36
37
	public static function getInstance() {
38
		if ( null === self::$instance ) {
39
			self::$instance = new self();
40
		}
41
42
		return self::$instance;
43
	}
44
45
	// this is necessary because you can't use "new" when you declare instance properties >:(
46
	protected function __construct() {
47
		$this->set_defaults();
48
		$this->init();
49
	}
50
51
	private function init() {
52
53
		$handler = array( $this, 'action_handler' );
54
55
		/**
56
		 * Most of the following hooks are sent to the same $handler
57
		 * for immediate serialization and queuing be sent to the server.
58
		 * The only exceptions are actions which need additional processing.
59
		 */
60
61
		// constants
62
		add_action( 'jetpack_sync_constant', $handler, 10, 2 );
63
64
		// callables
65
		add_action( 'jetpack_sync_callable', $handler, 10, 2 );
66
67
		// posts
68
		add_action( 'wp_insert_post', $handler, 10, 3 );
69
		add_action( 'deleted_post', $handler, 10 );
70
		add_filter( 'jetpack_sync_before_send_wp_insert_post', array( $this, 'expand_wp_insert_post' ) );
71
72
		add_action( 'jetpack_publicize_post', $handler );
73
74
		// attachments
75
76
		add_action( 'edit_attachment', array( $this, 'send_attachment_info' ) );
77
		// Once we don't have to support 4.3 we can start using add_action( 'attachment_updated', $handler, 10, 3 ); instead
78
		add_action( 'add_attachment', array( $this, 'send_attachment_info' ) );
79
		add_action( 'jetpack_sync_save_add_attachment', $handler, 10, 2 );
80
81
		// comments
82
		add_action( 'wp_insert_comment', $handler, 10, 2 );
83
		add_action( 'deleted_comment', $handler, 10 );
84
		add_action( 'trashed_comment', $handler, 10 );
85
		add_action( 'spammed_comment', $handler, 10 );
86
		add_filter( 'jetpack_sync_before_send_wp_insert_comment', array( $this, 'expand_wp_insert_comment' ) );
87
88
		// even though it's messy, we implement these hooks because
89
		// the edit_comment hook doesn't include the data
90
		// so this saves us a DB read for every comment event
91
		foreach ( array( '', 'trackback', 'pingback' ) as $comment_type ) {
92
			foreach ( array( 'unapproved', 'approved' ) as $comment_status ) {
93
				$comment_action_name = "comment_{$comment_status}_{$comment_type}";
94
				add_action( $comment_action_name, $handler, 10, 2 );
95
				add_filter( "jetpack_sync_before_send_{$comment_action_name}", array( $this, 'expand_wp_comment_status_change' ) );
96
			}
97
		}
98
99
		// options
100
		add_action( 'added_option', $handler, 10, 2 );
101
		add_action( 'updated_option', $handler, 10, 3 );
102
		add_action( 'deleted_option', $handler, 10, 1 );
103
104
		// Sync Core Icon: Detect changes in Core's Site Icon and make it syncable.
105
		add_action( 'add_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) );
106
		add_action( 'update_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) );
107
		add_action( 'delete_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) );
108
109
		// wordpress version
110
		add_action( 'upgrader_process_complete', array( $this, 'send_wp_version' ), 10, 2 );
111
		add_action( 'jetpack_sync_wp_version', $handler );
112
113
		// themes
114
		add_action( 'switch_theme', array( $this, 'send_theme_info' ) );
115
		add_action( 'jetpack_sync_current_theme_support', $handler, 10 ); // custom hook, see meta-hooks below
116
117
		// post-meta, and in the future - other meta?
118
		foreach ( $this->meta_types as $meta_type ) {
119
			add_action( "added_{$meta_type}_meta", $handler, 10, 4 );
120
			add_action( "updated_{$meta_type}_meta", $handler, 10, 4 );
121
			add_action( "deleted_{$meta_type}_meta", $handler, 10, 4 );
122
		}
123
124
		// terms
125
		add_action( 'created_term', array( $this, 'save_term_handler' ), 10, 3 );
126
		add_action( 'edited_term', array( $this, 'save_term_handler' ), 10, 3 );
127
		add_action( 'jetpack_sync_save_term', $handler, 10, 4 );
128
		add_action( 'delete_term', $handler, 10, 4 );
129
		add_action( 'set_object_terms', $handler, 10, 6 );
130
		add_action( 'deleted_term_relationships', $handler, 10, 2 );
131
132
		// users
133
		add_action( 'user_register', array( $this, 'save_user_handler' ) );
134
		add_action( 'profile_update', array( $this, 'save_user_handler' ), 10, 2 );
135
		add_action( 'jetpack_sync_save_user', $handler, 10, 2 );
136
		add_action( 'deleted_user', $handler, 10, 2 );
137
138
		// user roles
139
		add_action( 'add_user_role', array( $this, 'save_user_role_handler' ), 10, 2 );
140
		add_action( 'set_user_role', array( $this, 'save_user_role_handler' ), 10, 3 );
141
		add_action( 'remove_user_role', array( $this, 'save_user_role_handler' ), 10, 2 );
142
143
		// user capabilities
144
		add_action( 'added_user_meta', array( $this, 'save_user_cap_handler' ), 10, 4 );
145
		add_action( 'updated_user_meta', array( $this, 'save_user_cap_handler' ), 10, 4 );
146
		add_action( 'deleted_user_meta', array( $this, 'save_user_cap_handler' ), 10, 4 );
147
148
		// updates
149
		add_action( 'set_site_transient_update_plugins', $handler, 10, 1 );
150
		add_action( 'set_site_transient_update_themes', $handler, 10, 1 );
151
		add_action( 'set_site_transient_update_core', $handler, 10, 1 );
152
		add_filter( 'jetpack_sync_before_enqueue_set_site_transient_update_plugins', array( $this, 'filter_update_keys' ), 10, 2 );
153
154
		// plugins
155
		add_action( 'upgrader_process_complete', $handler, 10, 2 );
156
		add_filter( 'jetpack_sync_before_send_upgrader_process_complete', array( $this, 'expand_upgrader_process_complete' ) );
157
		add_action( 'deleted_plugin', $handler, 10, 2 );
158
		add_action( 'activated_plugin', $handler, 10, 2 );
159
		add_action( 'deactivated_plugin', $handler, 10, 2 );
160
161
162
		// multi site network options
163
		if ( $this->is_multisite ) {
164
			add_action( 'add_site_option', $handler, 10, 2 );
165
			add_action( 'update_site_option', $handler, 10, 3 );
166
			add_action( 'delete_site_option', $handler, 10, 1 );
167
		}
168
169
		// synthetic actions for full sync
170
		add_action( 'jetpack_full_sync_start', $handler );
171
		add_action( 'jetpack_full_sync_end', $handler );
172
		add_action( 'jetpack_full_sync_options', $handler );
173
		add_action( 'jetpack_full_sync_posts', $handler ); // also sends post meta
174
		add_action( 'jetpack_full_sync_comments', $handler ); // also send comments meta
175
		add_action( 'jetpack_full_sync_constants', $handler );
176
		add_action( 'jetpack_full_sync_callables', $handler );
177
		add_action( 'jetpack_full_sync_updates', $handler );
178
179
		add_action( 'jetpack_full_sync_users', $handler );
180
		add_action( 'jetpack_full_sync_terms', $handler, 10, 2 );
181
		if ( is_multisite() ) {
182
			add_action( 'jetpack_full_sync_network_options', $handler );
183
		}
184
185
		// Module Activation
186
		add_action( 'jetpack_activate_module', $handler );
187
		add_action( 'jetpack_deactivate_module', $handler );
188
189
		/**
190
		 * Sync all pending actions with server
191
		 */
192
		add_action( 'jetpack_sync_actions', array( $this, 'do_sync' ) );
193
	}
194
195
	// removes unnecessary keys from synced updates data
196
	function filter_update_keys( $args ) {
197
		$updates = $args[0];
198
199
		if ( isset( $updates->no_update ) ) {
200
			unset( $updates->no_update );
201
		}
202
203
		return $args;
204
	}
205
206
	function set_options_whitelist( $options ) {
207
		$this->options_whitelist = $options;
208
	}
209
210
	function get_options_whitelist() {
211
		return $this->options_whitelist;
212
	}
213
214
	function set_constants_whitelist( $constants ) {
215
		$this->constants_whitelist = $constants;
216
	}
217
218
	function get_constants_whitelist() {
219
		return $this->constants_whitelist;
220
	}
221
222
	function set_callable_whitelist( $callables ) {
223
		$this->callable_whitelist = $callables;
224
	}
225
226
	function get_callable_whitelist() {
227
		return $this->callable_whitelist;
228
	}
229
230
	function set_network_options_whitelist( $options ) {
231
		$this->network_options_whitelist = $options;
232
	}
233
234
	function get_network_options_whitelist() {
235
		return $this->network_options_whitelist;
236
	}
237
238
	function set_dequeue_max_bytes( $size ) {
239
		$this->dequeue_max_bytes = $size;
240
	}
241
242
	// in bytes
243
	function set_upload_max_bytes( $max_bytes ) {
244
		$this->upload_max_bytes = $max_bytes;
245
	}
246
247
	// in rows
248
	function set_upload_max_rows( $max_rows ) {
249
		$this->upload_max_rows = $max_rows;
250
	}
251
252
	// in seconds
253
	function set_sync_wait_time( $seconds ) {
254
		update_option( self::SYNC_THROTTLE_OPTION_NAME, $seconds, true );
255
	}
256
257
	function get_sync_wait_time() {
258
		return get_option( self::SYNC_THROTTLE_OPTION_NAME );
259
	}
260
261
	private function get_last_sync_time() {
262
		return (double) get_option( self::LAST_SYNC_TIME_OPTION_NAME );
263
	}
264
265
	private function set_last_sync_time() {
266
		return update_option( self::LAST_SYNC_TIME_OPTION_NAME, microtime( true ), true );
267
	}
268
269
	function set_taxonomy_whitelist( $taxonomies ) {
270
		$this->taxonomy_whitelist = $taxonomies;
271
	}
272
273
	function is_whitelisted_option( $option ) {
274
		return in_array( $option, $this->options_whitelist );
275
	}
276
277
	function is_whitelisted_network_option( $option ) {
278
		return $this->is_multisite && in_array( $option, $this->network_options_whitelist );
279
	}
280
281
	function set_codec( iJetpack_Sync_Codec $codec ) {
282
		$this->codec = $codec;
283
	}
284
285
	function set_full_sync_client( $full_sync_client ) {
286
		if ( $this->full_sync_client ) {
287
			remove_action( 'jetpack_sync_full', array( $this->full_sync_client, 'start' ) );
288
		}
289
290
		$this->full_sync_client = $full_sync_client;
291
292
		/**
293
		 * Sync all objects in the database with the server
294
		 */
295
		add_action( 'jetpack_sync_full', array( $this->full_sync_client, 'start' ) );
296
	}
297
298
	function get_full_sync_client() {
299
		return $this->full_sync_client;
300
	}
301
302
	function action_handler() {
303
		
304
		$current_filter = current_filter();
305
		$args           = func_get_args();
306
		if ( in_array( $current_filter, array( 'deleted_option', 'added_option', 'updated_option' ) )
307
		     &&
308
		     ! $this->is_whitelisted_option( $args[0] )
309
		) {
310
			return;
311
		}
312
313
		if ( in_array( $current_filter, array( 'delete_site_option', 'add_site_option', 'update_site_option' ) )
314
		     &&
315
		     ! $this->is_whitelisted_network_option( $args[0] )
316
		) {
317
			return;
318
		}
319
		
320
		if ( $current_filter == 'upgrader_process_complete' ) {
321
			array_shift( $args );
322
		}
323
324
		// don't sync private meta
325
		if ( preg_match( '/^(added|updated|deleted)_.*_meta$/', $current_filter )
326
		     && $args[2][0] === '_'
327
		     && ! in_array( $args[2], Jetpack_Sync_Defaults::$default_whitelist_meta_keys )
0 ignored issues
show
Bug introduced by
The property default_whitelist_meta_keys cannot be accessed from this context as it is declared private in class 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...
328
		) {
329
			return;
330
		}
331
332
		/**
333
		 * Modify the data within an action before it is enqueued locally.
334
		 *
335
		 * @since 4.2.0
336
		 *
337
		 * @param array The action parameters
338
		 */
339
		$args = apply_filters( "jetpack_sync_before_enqueue_$current_filter", $args );
340
341
		// if we add any items to the queue, we should 
342
		// try to ensure that our script can't be killed before
343
		// they are sent
344
		if ( function_exists( 'ignore_user_abort' ) ) {
345
			ignore_user_abort( true );
346
		}
347
348
		$this->sync_queue->add( array(
349
			$current_filter,
350
			$args,
351
			get_current_user_id(),
352
			microtime( true )
353
		) );
354
	}
355
356
	function send_theme_info() {
357
		global $_wp_theme_features;
358
359
		$theme_support = array();
360
361
		foreach ( Jetpack_Sync_Defaults::$default_theme_support_whitelist as $theme_feature ) {
0 ignored issues
show
Bug introduced by
The property default_theme_support_whitelist cannot be accessed from this context as it is declared private in class 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...
362
			$has_support = current_theme_supports( $theme_feature );
363
			if ( $has_support ) {
364
				$theme_support[ $theme_feature ] = $_wp_theme_features[ $theme_feature ];
365
			}
366
367
		}
368
369
		/**
370
		 * Fires when the client needs to sync theme support info
371
		 * Only sends theme support attributes whitelisted in Jetpack_Sync_Defaults::$default_theme_support_whitelist
372
		 *
373
		 * @since 4.2.0
374
		 *
375
		 * @param object the theme support hash
376
		 */
377
		do_action( 'jetpack_sync_current_theme_support', $theme_support );
378
		return 1; // The number of actions enqueued
379
	}
380
381
	function send_wp_version( $update, $meta_data ) {
382
		if ( 'update' === $meta_data['action'] && 'core' === $meta_data['type'] ) {
383
			$this->force_sync_callables();
384
		}
385
	}
386
387
	function save_term_handler( $term_id, $tt_id, $taxonomy ) {
388
		if ( class_exists( 'WP_Term' ) ) {
389
			$term_object = WP_Term::get_instance( $term_id, $taxonomy );
390
		} else {
391
			$term_object = get_term_by( 'id', $term_id, $taxonomy );
392
		}
393
394
		/**
395
		 * Fires when the client needs to sync a new term
396
		 *
397
		 * @since 4.2.0
398
		 *
399
		 * @param object the Term object
400
		 */
401
		do_action( 'jetpack_sync_save_term', $term_object );
402
	}
403
404
	function send_attachment_info( $attachment_id ) {
405
		$attachment = get_post( $attachment_id );
406
407
		/**
408
		 * Fires when the client needs to sync an attachment for a post
409
		 *
410
		 * @since 4.2.0
411
		 *
412
		 * @param int The attachment ID
413
		 * @param object The attachment
414
		 */
415
		do_action( 'jetpack_sync_save_add_attachment', $attachment_id, $attachment );
416
	}
417
418
	function save_user_handler( $user_id, $old_user_data = null ) {
419
		$user = $this->sanitize_user( get_user_by( 'id', $user_id ) );
420
421
		// Older versions of WP don't pass the old_user_data in ->data
422
		if ( isset( $old_user_data->data ) ) {
423
			$old_user = $old_user_data->data;
424
		} else {
425
			$old_user = $old_user_data;
426
		}
427
428
		if ( $old_user !== null ) {
429
			unset( $old_user->user_pass );
430
			if ( serialize( $old_user ) === serialize( $user->data ) ) {
431
				return;
432
			}
433
		}
434
435
		/**
436
		 * Fires when the client needs to sync an updated user
437
		 *
438
		 * @since 4.2.0
439
		 *
440
		 * @param object The WP_User object
441
		 */
442
		do_action( 'jetpack_sync_save_user', $user );
443
	}
444
445
	function save_user_role_handler( $user_id, $role, $old_roles = null ) {
0 ignored issues
show
Unused Code introduced by
The parameter $role is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $old_roles is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
446
		$user = $this->sanitize_user( get_user_by( 'id', $user_id ) );
447
448
		/**
449
		 * Fires when the client needs to sync an updated user
450
		 *
451
		 * @since 4.2.0
452
		 *
453
		 * @param object The WP_User object
454
		 */
455
		do_action( 'jetpack_sync_save_user', $user );
456
	}
457
458
	function save_user_cap_handler( $meta_id, $user_id, $meta_key, $capabilities ) {
0 ignored issues
show
Unused Code introduced by
The parameter $capabilities is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
459
		$user = $this->sanitize_user( get_user_by( 'id', $user_id ) );
460
		if ( $meta_key === $user->cap_key ) {
461
			/**
462
			 * Fires when the client needs to sync an updated user
463
			 *
464
			 * @since 4.2.0
465
			 *
466
			 * @param object The WP_User object
467
			 */
468
			do_action( 'jetpack_sync_save_user', $user );
469
		}
470
	}
471
472
	public function sanitize_user( $user ) {
473
		unset( $user->data->user_pass );
474
475
		return $user;
476
	}
477
478
479
	function do_sync() {
480
		// don't sync if importing
481
		if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
482
			$this->schedule_sync( "+1 minute" );
483
484
			return false;
485
		}
486
487
		// don't sync if we are throttled
488
		$sync_wait = $this->get_sync_wait_time();
489
		$last_sync = $this->get_last_sync_time();
490
491
		if ( $last_sync && $sync_wait && $last_sync + $sync_wait > microtime( true ) ) {
492
			return false;
493
		}
494
495
		$this->set_last_sync_time();
496
		$this->maybe_sync_constants();
497
		$this->maybe_sync_callables();
498
499
		if ( $this->sync_queue->size() === 0 ) {
500
			return false;
501
		}
502
503
		// now that we're sure we are about to sync, try to
504
		// ignore user abort so we can avoid getting into a
505
		// bad state
506
		if ( function_exists( 'ignore_user_abort' ) ) {
507
			ignore_user_abort( true );
508
		}
509
510
		$buffer = $this->sync_queue->checkout_with_memory_limit( $this->dequeue_max_bytes, $this->upload_max_rows );
511
512
		if ( ! $buffer ) {
513
			// buffer has no items
514
			return false;
515
		}
516
517
		if ( is_wp_error( $buffer ) ) {
518
			// another buffer is currently sending
519
			return false;
520
		}
521
522
		$upload_size   = 0;
523
		$items_to_send = array();
524
		$actions_to_send = array();
525
		// we estimate the total encoded size as we go by encoding each item individually
526
		// this is expensive, but the only way to really know :/
527
		foreach ( $buffer->get_items() as $key => $item ) {
528
			/**
529
			 * Modify the data within an action before it is serialized and sent to the server
530
			 * For example, during full sync this expands Post ID's into full Post objects,
531
			 * so that we don't have to serialize the whole object into the queue.
532
			 *
533
			 * @since 4.2.0
534
			 *
535
			 * @param array The action parameters
536
			 */
537
			$item[1] = apply_filters( "jetpack_sync_before_send_" . $item[0], $item[1] );
538
539
			$encoded_item = $this->codec->encode( $item );
540
541
			$upload_size += strlen( $encoded_item );
542
543
			if ( $upload_size > $this->upload_max_bytes && count( $items_to_send ) > 0 ) {
544
				break;
545
			}
546
547
			$items_to_send[ $key ] = $encoded_item;
548
			$actions_to_send[ $key ] = $item[0];
549
		}
550
551
		/**
552
		 * Fires when data is ready to send to the server.
553
		 * Return false or WP_Error to abort the sync (e.g. if there's an error)
554
		 * The items will be automatically re-sent later
555
		 *
556
		 * @since 4.2.0
557
		 *
558
		 * @param array $data The action buffer
559
		 */
560
		$result = apply_filters( 'jetpack_sync_client_send_data', $items_to_send );
561
562
		if ( ! $result || is_wp_error( $result ) ) {
563
			// error_log("There was an error sending data:");
564
			// error_log(print_r($result, 1));
565
			$result = $this->sync_queue->checkin( $buffer );
566
567
			if ( is_wp_error( $result ) ) {
568
				error_log( "Error checking in buffer: " . $result->get_error_message() );
569
				$this->sync_queue->force_checkin();
570
			}
571
			// try again in 1 minute
572
			$this->schedule_sync( "+1 minute" );
573
		} else {
574
			$processed_actions = array();
575
			foreach( $result as $result_id ) {
576
				if ( isset( $actions_to_send[ $result_id ] ) ) {
577
					$processed_actions[] =  $actions_to_send[ $result_id ];
578
				}
579
			}
580
581
			/**
582
			 * Allows us to keep track of all the actions that have been sent.
583
			 * Allows us to calculate the progress of specific actions.
584
			 *
585
			 * @since 4.2.0
586
			 *
587
			 * @param array $processed_actions The actions that we send successfully.
588
			 */
589
			do_action( 'jetpack_sync_processed_actions', $processed_actions );
590
591
592
			$this->sync_queue->close( $buffer, $result );
593
			// check if there are any more events in the buffer
594
			// if so, schedule a cron job to happen soon
595
			if ( $this->sync_queue->has_any_items() ) {
596
				$this->schedule_sync( "+1 minute" );
597
			}
598
		}
599
	}
600
601
	function expand_wp_insert_post( $args ) {
602
		return array( $args[0], $this->filter_post_content_and_add_links( $args[1] ), $args[2] );
603
	}
604
605
	function expand_upgrader_process_complete( $args ) {
606
		list( $process ) = $args;
607
		if ( isset( $process['type'] ) && $process['type'] === 'plugin' ) {
608
			return array( $process, get_plugins() );
609
		}
610
		return $args;
611
	}
612
613
	// Expands wp_insert_post to include filtered content
614
	function filter_post_content_and_add_links( $post ) {
615
		if ( 0 < strlen( $post->post_password ) ) {
616
			$post->post_password = 'auto-' . wp_generate_password( 10, false );
617
		}
618
		/** This filter is already documented in core. wp-includes/post-template.php */
619
		$post->post_content_filtered   = apply_filters( 'the_content', $post->post_content );
620
		$post->post_excerpt_filtered   = apply_filters( 'the_content', $post->post_excerpt );
621
		$post->permalink               = get_permalink( $post->ID );
622
		$post->shortlink               = wp_get_shortlink( $post->ID );
623
		
624
		// legacy fields until we fully sync users
625
		$extra = array();
626
		$extra['author_email']            = get_the_author_meta( 'email', $post->post_author );
627
		$extra['author_display_name']     = get_the_author_meta( 'display_name', $post->post_author );
628
		$extra['dont_email_post_to_subs'] = get_post_meta( $post->ID, '_jetpack_dont_email_post_to_subs', true );
629
		$post->extra = $extra;
630
631
		return $post;
632
	}
633
634
	function expand_wp_comment_status_change( $args ) {
635
		return array( $args[0], $this->filter_comment_and_add_hc_meta( $args[1] ) );
636
	}
637
638
	function expand_wp_insert_comment( $args ) {
639
		return array( $args[0], $this->filter_comment_and_add_hc_meta( $args[1] ) );
640
	}
641
642
	function filter_comment_and_add_hc_meta( $comment ) {
643
		// add meta-property with Highlander Comment meta, which we 
644
		// we need to process synchronously on .com
645
		$hc_post_as = get_comment_meta( $comment->comment_ID, 'hc_post_as', true );
646
		if ( 'wordpress' === $hc_post_as ) {
647
			$meta = array();
648
			$meta['hc_post_as']         = $hc_post_as;
649
			$meta['hc_wpcom_id_sig']    = get_comment_meta( $comment->comment_ID, 'hc_wpcom_id_sig', true );
650
			$meta['hc_foreign_user_id'] = get_comment_meta( $comment->comment_ID, 'hc_foreign_user_id', true );
651
			$comment->meta = $meta;	
652
		}
653
654
		return $comment;
655
	}
656
657
	private function schedule_sync( $when ) {
658
		wp_schedule_single_event( strtotime( $when ), 'jetpack_sync_actions' );
659
	}
660
661
	function force_sync_constants() {
662
		delete_option( self::CONSTANTS_CHECKSUM_OPTION_NAME );
663
		delete_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME );
664
		$this->maybe_sync_constants();
665
666
	}
667
668
	function full_sync_constants() {
669
		/**
670
		 * Tells the client to sync all constants to the server
671
		 *
672
		 * @since 4.1
673
		 *
674
		 * @param boolean Whether to expand constants (should always be true)
675
		 */
676
		do_action( 'jetpack_full_sync_constants', true );
677
		return 1; // The number of actions enqueued
678
	}
679
680
	function force_sync_options() {
681
		/**
682
		 * Tells the client to sync all options to the server
683
		 *
684
		 * @since 4.2.0
685
		 *
686
		 * @param boolean Whether to expand options (should always be true)
687
		 */
688
		do_action( 'jetpack_full_sync_options', true );
689
		return 1; // The number of actions enqueued
690
	}
691
692
	function force_sync_network_options() {
693
		/**
694
		 * Tells the client to sync all network options to the server
695
		 *
696
		 * @since 4.2.0
697
		 *
698
		 * @param boolean Whether to expand options (should always be true)
699
		 */
700
		do_action( 'jetpack_full_sync_network_options', true );
701
		return 1; // The number of actions enqueued
702
	}
703
704
	public function full_sync_callables() {
705
		/**
706
		 * Tells the client to sync all callables to the server
707
		 *
708
		 * @since 4.1
709
		 *
710
		 * @param boolean Whether to expand callables (should always be true)
711
		 */
712
		do_action( 'jetpack_full_sync_callables', true );
713
		return 1; // The number of actions enqueued
714
	}
715
716
	public function full_sync_updates() {
717
		/**
718
		 * Tells the client to sync all updates to the server
719
		 *
720
		 * @since 4.1
721
		 *
722
		 * @param boolean Whether to expand callables (should always be true)
723
		 */
724
		do_action( 'jetpack_full_sync_updates', true );
725
		return 1; // The number of actions enqueued
726
	}
727
728 View Code Duplication
	private function maybe_sync_constants() {
729
		if ( get_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME ) ) {
730
			return;
731
		}
732
733
		$constants = $this->get_all_constants();
734
		if ( empty( $constants ) ) {
735
			return;
736
		}
737
738
		set_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME, microtime( true ), Jetpack_Sync_Defaults::$default_sync_constants_wait_time );
0 ignored issues
show
Bug introduced by
The property default_sync_constants_wait_time cannot be accessed from this context as it is declared private in class 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...
739
		$constants_checksums = get_option( self::CONSTANTS_CHECKSUM_OPTION_NAME, array() );
740
		// only send the constants that have changed
741
		foreach ( $constants as $name => $value ) {
742
			$checksum = $this->get_check_sum( $value );
743
744
			// explicitly not using Identical comparison as get_option returns a string
745
			if ( ! $this->still_valid_checksum( $constants_checksums, $name, $checksum ) ) {
746
				/**
747
				 * Tells the client to sync a constant to the server
748
				 *
749
				 * @since 4.2.0
750
				 *
751
				 * @param string The name of the constant
752
				 * @param mixed The value of the constant
753
				 */
754
				do_action( 'jetpack_sync_constant', $name, $value );
755
				$constants_checksums[ $name ] = $checksum;
756
			}
757
		}
758
759
		update_option( self::CONSTANTS_CHECKSUM_OPTION_NAME, $constants_checksums );
760
	}
761
	// public so that we don't have to store an option for each constant
762
	function get_all_constants() {
763
		return array_combine(
764
			$this->constants_whitelist,
765
			array_map( array( $this, 'get_constant' ), $this->constants_whitelist )
766
		);
767
	}
768
769
	private function get_constant( $constant ) {
770
		return ( defined( $constant ) ) ?
771
			constant( $constant ) 
772
			: null;
773
	}
774
775
	public function get_all_updates() {
776
		return array(
777
			'core' => get_site_transient( 'update_core' ),
778
			'plugins' => get_site_transient( 'update_plugins' ),
779
			'themes' => get_site_transient( 'update_themes' ),
780
		);
781
	}
782
783
	public function force_sync_callables() {
784
		delete_option( self::CALLABLES_CHECKSUM_OPTION_NAME );
785
		delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME );
786
		$this->maybe_sync_callables();
787
	}
788
789 View Code Duplication
	private function maybe_sync_callables() {
790
		if ( get_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME ) ) {
791
			return;
792
		}
793
794
		$callables = $this->get_all_callables();
795
		if ( empty( $callables ) ) {
796
			return;
797
		}
798
799
		set_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME, microtime( true ), Jetpack_Sync_Defaults::$default_sync_callables_wait_time );
0 ignored issues
show
Bug introduced by
The property default_sync_callables_wait_time cannot be accessed from this context as it is declared private in class 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...
800
801
		$callable_checksums = get_option( self::CALLABLES_CHECKSUM_OPTION_NAME , array() );
802
803
		// only send the callables that have changed
804
		foreach ( $callables as $name => $value ) {
805
			$checksum = $this->get_check_sum( $value );
806
			// explicitly not using Identical comparison as get_option returns a string
807
			if ( ! $this->still_valid_checksum( $callable_checksums, $name, $checksum ) ) {
808
				/**
809
				 * Tells the client to sync a callable (aka function) to the server
810
				 *
811
				 * @since 4.2.0
812
				 *
813
				 * @param string The name of the callable
814
				 * @param mixed The value of the callable
815
				 */
816
				do_action( 'jetpack_sync_callable', $name, $value );
817
				$callable_checksums[ $name ] = $checksum;
818
			}
819
		}
820
		update_option( self::CALLABLES_CHECKSUM_OPTION_NAME , $callable_checksums );
821
	}
822
823
	private function still_valid_checksum( $sums_to_check, $name, $new_sum ) {
824
		if ( isset( $sums_to_check[ $name ] ) && $sums_to_check[ $name ] === $new_sum ) {
825
			return true;
826
		}
827
		return false;
828
	}
829
830
	public function get_all_callables() {
831
		return array_combine(
832
			array_keys( $this->callable_whitelist ),
833
			array_map( array( $this, 'get_callable' ), array_values( $this->callable_whitelist ) )
834
		);
835
	}
836
837
	private function get_callable( $callable ) {
838
		return call_user_func( $callable );
839
	}
840
841
	// Is public so that we don't have to store so much data all the options twice.
842
	function get_all_options() {
843
		$options = array();
844
		foreach ( $this->options_whitelist as $option ) {
845
			$options[ $option ] = get_option( $option );
846
		}
847
848
		return $options;
849
	}
850
851
	function get_all_network_options() {
852
		$options = array();
853
		foreach ( $this->network_options_whitelist as $option ) {
854
			$options[ $option ] = get_site_option( $option );
855
		}
856
857
		return $options;
858
	}
859
860
	private function get_check_sum( $values ) {
861
		return crc32( json_encode( $values ) );
862
	}
863
864
	function jetpack_sync_core_icon() {
865
		if ( function_exists( 'get_site_icon_url' ) ) {
866
			$url = get_site_icon_url();
867
		} else {
868
			return;
869
		}
870
871
		require_once( JETPACK__PLUGIN_DIR . 'modules/site-icon/site-icon-functions.php' );
872
		// If there's a core icon, maybe update the option.  If not, fall back to Jetpack's.
873
		if ( ! empty( $url ) && $url !== jetpack_site_icon_url() ) {
874
			// This is the option that is synced with dotcom
875
			Jetpack_Options::update_option( 'site_icon_url', $url );
876
		} else if ( empty( $url ) ) {
877
			Jetpack_Options::delete_option( 'site_icon_url' );
878
		}
879
	}
880
881
	function get_sync_queue() {
882
		return $this->sync_queue;
883
	}
884
885
	function reset_sync_queue() {
886
		$this->sync_queue->reset();
887
	}
888
889
	function get_settings() {
890
		$settings = array();
891
		foreach( array_keys( self::$valid_settings ) as $setting ) {
892
			$default_name = "default_$setting"; // e.g. default_dequeue_max_bytes
893
			$settings[ $setting ] = (int) get_option( self::SETTINGS_OPTION_PREFIX.$setting, Jetpack_Sync_Defaults::$$default_name );
894
		}
895
		return $settings;
896
	}
897
898
	function update_settings( $new_settings ) {
899
		$validated_settings = array_intersect_key( $new_settings, self::$valid_settings );
900
		foreach( $validated_settings as $setting => $value ) {
901
			update_option( self::SETTINGS_OPTION_PREFIX.$setting, $value, true );
902
		}
903
	}
904
905
	function update_options_whitelist() {
906
		/** This filter is already documented in json-endpoints/jetpack/class.wpcom-json-api-get-option-endpoint.php */
907
		$this->options_whitelist = apply_filters( 'jetpack_options_whitelist', Jetpack_Sync_Defaults::$default_options_whitelist );
0 ignored issues
show
Bug introduced by
The property default_options_whitelist cannot be accessed from this context as it is declared private in class 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...
908
	}
909
910
	function set_defaults() {
911
		$this->sync_queue = new Jetpack_Sync_Queue( 'sync' );
912
913
		// saved settings
914
		$settings = $this->get_settings();
915
		$this->set_dequeue_max_bytes( $settings['dequeue_max_bytes'] );
916
		$this->set_upload_max_bytes( $settings['upload_max_bytes'] );
917
		$this->set_upload_max_rows( $settings['upload_max_rows'] );
918
		$this->set_sync_wait_time( $settings['sync_wait_time'] );
919
920
		$this->set_full_sync_client( Jetpack_Sync_Full::getInstance() );
921
		$this->codec                     = new Jetpack_Sync_Deflate_Codec();
922
		$this->constants_whitelist       = Jetpack_Sync_Defaults::$default_constants_whitelist;
0 ignored issues
show
Bug introduced by
The property default_constants_whitelist cannot be accessed from this context as it is declared private in class 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...
923
		$this->update_options_whitelist();
924
		$this->network_options_whitelist = Jetpack_Sync_Defaults::$default_network_options_whitelist;
0 ignored issues
show
Bug introduced by
The property default_network_options_whitelist cannot be accessed from this context as it is declared private in class 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...
925
		$this->taxonomy_whitelist        = Jetpack_Sync_Defaults::$default_taxonomy_whitelist;
0 ignored issues
show
Bug introduced by
The property default_taxonomy_whitelist cannot be accessed from this context as it is declared private in class 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...
926
		$this->is_multisite              = is_multisite();
927
928
		// theme mod varies from theme to theme.
929
		$this->options_whitelist[] = 'theme_mods_' . get_option( 'stylesheet' );
930
		if ( $this->is_multisite ) {
931
			$this->callable_whitelist = array_merge( Jetpack_Sync_Defaults::$default_callable_whitelist, Jetpack_Sync_Defaults::$default_multisite_callable_whitelist );
0 ignored issues
show
Bug introduced by
The property default_callable_whitelist cannot be accessed from this context as it is declared private in class 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...
Bug introduced by
The property default_multisite_callable_whitelist cannot be accessed from this context as it is declared private in class 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...
932
		} else {
933
			$this->callable_whitelist = Jetpack_Sync_Defaults::$default_callable_whitelist;
0 ignored issues
show
Bug introduced by
The property default_callable_whitelist cannot be accessed from this context as it is declared private in class 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...
934
		}
935
	}
936
937
	function reset_data() {
938
		$this->reset_sync_queue();
939
940
941
		delete_option( self::CONSTANTS_CHECKSUM_OPTION_NAME );
942
		delete_option( self::CALLABLES_CHECKSUM_OPTION_NAME );
943
944
		delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME );
945
		delete_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME );
946
947
		delete_option( self::SYNC_THROTTLE_OPTION_NAME );
948
		delete_option( self::LAST_SYNC_TIME_OPTION_NAME );
949
950
		$valid_settings  = self::$valid_settings;
951
		$settings_prefix =  self::SETTINGS_OPTION_PREFIX;
952
		foreach ( $valid_settings as $option => $value ) {
953
			delete_option( $settings_prefix . $option );
954
		}
955
	}
956
957
	function uninstall() {
958
		// Lets delete all the other fun stuff like transient and option and the sync queue
959
		$this->reset_data();
960
961
		// delete the full sync status
962
		delete_option( 'jetpack_full_sync_status' );
963
964
		// clear the sync cron.
965
		wp_clear_scheduled_hook( 'jetpack_sync_actions' );
966
	}
967
}
968