Completed
Push — add/prevent-sync-filter ( fb9133...f78925 )
by
unknown
08:44
created

Jetpack_Sync_Client::uninstall()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 10
rs 9.4285
1
<?php
2
require_once dirname( __FILE__ ) . '/class.jetpack-sync-json-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
87
		add_filter( 'jetpack_sync_before_send_wp_insert_comment', array( $this, 'expand_wp_insert_comment' ) );
88
89
		// even though it's messy, we implement these hooks because
90
		// the edit_comment hook doesn't include the data
91
		// so this saves us a DB read for every comment event
92
		foreach ( array( '', 'trackback', 'pingback' ) as $comment_type ) {
93
			foreach ( array( 'unapproved', 'approved' ) as $comment_status ) {
94
				$comment_action_name = "comment_{$comment_status}_{$comment_type}";
95
				add_action( $comment_action_name, $handler, 10, 2 );
96
				add_filter( 'jetpack_sync_before_send_' . $comment_action_name, array( $this, 'expand_wp_insert_comment' ) );
97
			}
98
		}
99
100
		// options
101
		add_action( 'added_option', $handler, 10, 2 );
102
		add_action( 'updated_option', $handler, 10, 3 );
103
		add_action( 'deleted_option', $handler, 10, 1 );
104
105
		// Sync Core Icon: Detect changes in Core's Site Icon and make it syncable.
106
		add_action( 'add_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) );
107
		add_action( 'update_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) );
108
		add_action( 'delete_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) );
109
110
		// wordpress version
111
		add_action( 'upgrader_process_complete', array( $this, 'send_wp_version' ), 10, 2 );
112
		add_action( 'jetpack_sync_wp_version', $handler );
113
114
		// themes
115
		add_action( 'switch_theme', array( $this, 'send_theme_info' ) );
116
		add_action( 'jetpack_sync_current_theme_support', $handler, 10 ); // custom hook, see meta-hooks below
117
118
		// post-meta, and in the future - other meta?
119
		foreach ( $this->meta_types as $meta_type ) {
120
			add_action( "added_{$meta_type}_meta", $handler, 10, 4 );
121
			add_action( "updated_{$meta_type}_meta", $handler, 10, 4 );
122
			add_action( "deleted_{$meta_type}_meta", $handler, 10, 4 );
123
		}
124
125
		// terms
126
		add_action( 'created_term', array( $this, 'save_term_handler' ), 10, 3 );
127
		add_action( 'edited_term', array( $this, 'save_term_handler' ), 10, 3 );
128
		add_action( 'jetpack_sync_save_term', $handler, 10, 4 );
129
		add_action( 'delete_term', $handler, 10, 4 );
130
		add_action( 'set_object_terms', $handler, 10, 6 );
131
		add_action( 'deleted_term_relationships', $handler, 10, 2 );
132
133
		// users
134
		add_action( 'user_register', array( $this, 'save_user_handler' ) );
135
		add_action( 'profile_update', array( $this, 'save_user_handler' ), 10, 2 );
136
		add_action( 'jetpack_sync_save_user', $handler, 10, 2 );
137
		add_action( 'deleted_user', $handler, 10, 2 );
138
139
		// user roles
140
		add_action( 'add_user_role', array( $this, 'save_user_role_handler' ), 10, 2 );
141
		add_action( 'set_user_role', array( $this, 'save_user_role_handler' ), 10, 3 );
142
		add_action( 'remove_user_role', array( $this, 'save_user_role_handler' ), 10, 2 );
143
144
145
		// user capabilities
146
		add_action( 'added_user_meta', array( $this, 'save_user_cap_handler' ), 10, 4 );
147
		add_action( 'updated_user_meta', array( $this, 'save_user_cap_handler' ), 10, 4 );
148
		add_action( 'deleted_user_meta', array( $this, 'save_user_cap_handler' ), 10, 4 );
149
150
		// themes
151
		add_action( 'set_site_transient_update_plugins', $handler, 10, 1 );
152
		add_action( 'set_site_transient_update_themes', $handler, 10, 1 );
153
		add_action( 'set_site_transient_update_core', $handler, 10, 1 );
154
155
		add_filter( 'jetpack_sync_before_enqueue_set_site_transient_update_plugins', array( $this, 'filter_update_keys' ), 10, 2 );
156
157
		// multi site network options
158
		if ( $this->is_multisite ) {
159
			add_action( 'add_site_option', $handler, 10, 2 );
160
			add_action( 'update_site_option', $handler, 10, 3 );
161
			add_action( 'delete_site_option', $handler, 10, 1 );
162
		}
163
164
		// synthetic actions for full sync
165
		add_action( 'jetpack_full_sync_start', $handler );
166
		add_action( 'jetpack_full_sync_end', $handler );
167
		add_action( 'jetpack_full_sync_options', $handler );
168
		add_action( 'jetpack_full_sync_posts', $handler ); // also sends post meta
169
		add_action( 'jetpack_full_sync_comments', $handler ); // also send comments meta
170
		add_action( 'jetpack_full_sync_constants', $handler );
171
		add_action( 'jetpack_full_sync_callables', $handler );
172
		add_action( 'jetpack_full_sync_updates', $handler );
173
174
		add_action( 'jetpack_full_sync_users', $handler );
175
		add_action( 'jetpack_full_sync_terms', $handler, 10, 2 );
176
		if ( is_multisite() ) {
177
			add_action( 'jetpack_full_sync_network_options', $handler );
178
		}
179
180
		// Module Activation
181
		add_action( 'jetpack_activate_module', $handler );
182
		add_action( 'jetpack_deactivate_module', $handler );
183
184
		/**
185
		 * Sync all pending actions with server
186
		 */
187
		add_action( 'jetpack_sync_actions', array( $this, 'do_sync' ) );
188
	}
189
190
	// removes unnecessary keys from synced updates data
191
	function filter_update_keys( $args ) {
192
		$updates = $args[0];
193
194
		if ( isset( $updates->no_update ) ) {
195
			unset( $updates->no_update );
196
		}
197
198
		return $args;
199
	}
200
201
	function set_options_whitelist( $options ) {
202
		$this->options_whitelist = $options;
203
	}
204
205
	function get_options_whitelist() {
206
		return $this->options_whitelist;
207
	}
208
209
	function set_constants_whitelist( $constants ) {
210
		$this->constants_whitelist = $constants;
211
	}
212
213
	function get_constants_whitelist() {
214
		return $this->constants_whitelist;
215
	}
216
217
	function set_callable_whitelist( $callables ) {
218
		$this->callable_whitelist = $callables;
219
	}
220
221
	function get_callable_whitelist() {
222
		return $this->callable_whitelist;
223
	}
224
225
	function set_network_options_whitelist( $options ) {
226
		$this->network_options_whitelist = $options;
227
	}
228
229
	function get_network_options_whitelist() {
230
		return $this->network_options_whitelist;
231
	}
232
233
	function set_dequeue_max_bytes( $size ) {
234
		$this->dequeue_max_bytes = $size;
235
	}
236
237
	// in bytes
238
	function set_upload_max_bytes( $max_bytes ) {
239
		$this->upload_max_bytes = $max_bytes;
240
	}
241
242
	// in rows
243
	function set_upload_max_rows( $max_rows ) {
244
		$this->upload_max_rows = $max_rows;
245
	}
246
247
	// in seconds
248
	function set_sync_wait_time( $seconds ) {
249
		update_option( self::SYNC_THROTTLE_OPTION_NAME, $seconds, true );
250
	}
251
252
	function get_sync_wait_time() {
253
		return get_option( self::SYNC_THROTTLE_OPTION_NAME );
254
	}
255
256
	private function get_last_sync_time() {
257
		return (double) get_option( self::LAST_SYNC_TIME_OPTION_NAME );
258
	}
259
260
	private function set_last_sync_time() {
261
		return update_option( self::LAST_SYNC_TIME_OPTION_NAME, microtime( true ), true );
262
	}
263
264
	function set_taxonomy_whitelist( $taxonomies ) {
265
		$this->taxonomy_whitelist = $taxonomies;
266
	}
267
268
	function is_whitelisted_option( $option ) {
269
		return in_array( $option, $this->options_whitelist );
270
	}
271
272
	function is_whitelisted_network_option( $option ) {
273
		return $this->is_multisite && in_array( $option, $this->network_options_whitelist );
274
	}
275
276
	function set_codec( iJetpack_Sync_Codec $codec ) {
277
		$this->codec = $codec;
278
	}
279
280
	function set_full_sync_client( $full_sync_client ) {
281
		if ( $this->full_sync_client ) {
282
			remove_action( 'jetpack_sync_full', array( $this->full_sync_client, 'start' ) );
283
		}
284
285
		$this->full_sync_client = $full_sync_client;
286
287
		/**
288
		 * Sync all objects in the database with the server
289
		 */
290
		add_action( 'jetpack_sync_full', array( $this->full_sync_client, 'start' ) );
291
	}
292
293
	function get_full_sync_client() {
294
		return $this->full_sync_client;
295
	}
296
297
	function action_handler() {
298
		
299
		$current_filter = current_filter();
300
		$args           = func_get_args();
301
302
		if ( in_array( $current_filter, array( 'deleted_option', 'added_option', 'updated_option' ) )
303
		     &&
304
		     ! $this->is_whitelisted_option( $args[0] )
305
		) {
306
			return;
307
		}
308
309
		if ( in_array( $current_filter, array( 'delete_site_option', 'add_site_option', 'update_site_option' ) )
310
		     &&
311
		     ! $this->is_whitelisted_network_option( $args[0] )
312
		) {
313
			return;
314
		}
315
316
		// don't sync private meta
317
		if ( preg_match( '/^(added|updated|deleted)_.*_meta$/', $current_filter )
318
		     && $args[2][0] === '_'
319
		     && ! 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...
320
		) {
321
			return;
322
		}
323
324
		/**
325
		 * Modify the data within an action before it is enqueued locally.
326
		 *
327
		 * @since 4.2.0
328
		 *
329
		 * @param array The action parameters
330
		 */
331
		$args = apply_filters( "jetpack_sync_before_enqueue_$current_filter", $args );
332
333
		// if we add any items to the queue, we should 
334
		// try to ensure that our script can't be killed before
335
		// they are sent
336
		if ( function_exists( 'ignore_user_abort' ) ) {
337
			ignore_user_abort( true );
338
		}
339
340
		$this->sync_queue->add( array(
341
			$current_filter,
342
			$args,
343
			get_current_user_id(),
344
			microtime( true )
345
		) );
346
	}
347
348
	function send_theme_info() {
349
		global $_wp_theme_features;
350
351
		$theme_support = array();
352
353
		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...
354
			$has_support = current_theme_supports( $theme_feature );
355
			if ( $has_support ) {
356
				$theme_support[ $theme_feature ] = $_wp_theme_features[ $theme_feature ];
357
			}
358
359
		}
360
361
		/**
362
		 * Fires when the client needs to sync theme support info
363
		 * Only sends theme support attributes whitelisted in Jetpack_Sync_Defaults::$default_theme_support_whitelist
364
		 *
365
		 * @since 4.2.0
366
		 *
367
		 * @param object the theme support hash
368
		 */
369
		do_action( 'jetpack_sync_current_theme_support', $theme_support );
370
		return 1; // The number of actions enqueued
371
	}
372
373
	function send_wp_version( $update, $meta_data ) {
374
		if ( 'update' === $meta_data['action'] && 'core' === $meta_data['type'] ) {
375
			$this->force_sync_callables();
376
		}
377
	}
378
379
	function save_term_handler( $term_id, $tt_id, $taxonomy ) {
380
		if ( class_exists( 'WP_Term' ) ) {
381
			$term_object = WP_Term::get_instance( $term_id, $taxonomy );
382
		} else {
383
			$term_object = get_term_by( 'id', $term_id, $taxonomy );
384
		}
385
386
		/**
387
		 * Fires when the client needs to sync a new term
388
		 *
389
		 * @since 4.2.0
390
		 *
391
		 * @param object the Term object
392
		 */
393
		do_action( 'jetpack_sync_save_term', $term_object );
394
	}
395
396
	function send_attachment_info( $attachment_id ) {
397
		$attachment = get_post( $attachment_id );
398
399
		/**
400
		 * Fires when the client needs to sync an attachment for a post
401
		 *
402
		 * @since 4.2.0
403
		 *
404
		 * @param int The attachment ID
405
		 * @param object The attachment
406
		 */
407
		do_action( 'jetpack_sync_save_add_attachment', $attachment_id, $attachment );
408
	}
409
410
	function save_user_handler( $user_id, $old_user_data = null ) {
411
		$user = $this->sanitize_user( get_user_by( 'id', $user_id ) );
412
413
		// Older versions of WP don't pass the old_user_data in ->data
414
		if ( isset( $old_user_data->data ) ) {
415
			$old_user = $old_user_data->data;
416
		} else {
417
			$old_user = $old_user_data;
418
		}
419
420
		if ( $old_user !== null ) {
421
			unset( $old_user->user_pass );
422
			if ( serialize( $old_user ) === serialize( $user->data ) ) {
423
				return;
424
			}
425
		}
426
427
		/**
428
		 * Fires when the client needs to sync an updated user
429
		 *
430
		 * @since 4.2.0
431
		 *
432
		 * @param object The WP_User object
433
		 */
434
		do_action( 'jetpack_sync_save_user', $user );
435
	}
436
437
	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...
438
		$user = $this->sanitize_user( get_user_by( 'id', $user_id ) );
439
440
		/**
441
		 * Fires when the client needs to sync an updated user
442
		 *
443
		 * @since 4.2.0
444
		 *
445
		 * @param object The WP_User object
446
		 */
447
		do_action( 'jetpack_sync_save_user', $user );
448
	}
449
450
	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...
451
		$user = $this->sanitize_user( get_user_by( 'id', $user_id ) );
452
		if ( $meta_key === $user->cap_key ) {
453
			/**
454
			 * Fires when the client needs to sync an updated user
455
			 *
456
			 * @since 4.2.0
457
			 *
458
			 * @param object The WP_User object
459
			 */
460
			do_action( 'jetpack_sync_save_user', $user );
461
		}
462
	}
463
464
	public function sanitize_user( $user ) {
465
		unset( $user->data->user_pass );
466
467
		return $user;
468
	}
469
470
471
	function do_sync() {
472
		// don't sync if importing
473
		if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
474
			$this->schedule_sync( "+1 minute" );
475
476
			return false;
477
		}
478
479
		// don't sync if we are throttled
480
		$sync_wait = $this->get_sync_wait_time();
481
		$last_sync = $this->get_last_sync_time();
482
483
		if ( $last_sync && $sync_wait && $last_sync + $sync_wait > microtime( true ) ) {
484
			return false;
485
		}
486
487
		$this->set_last_sync_time();
488
		$this->maybe_sync_constants();
489
		$this->maybe_sync_callables();
490
491
		if ( $this->sync_queue->size() === 0 ) {
492
			return false;
493
		}
494
495
		// now that we're sure we are about to sync, try to
496
		// ignore user abort so we can avoid getting into a
497
		// bad state
498
		if ( function_exists( 'ignore_user_abort' ) ) {
499
			ignore_user_abort( true );
500
		}
501
502
		$buffer = $this->sync_queue->checkout_with_memory_limit( $this->dequeue_max_bytes, $this->upload_max_rows );
503
504
		if ( ! $buffer ) {
505
			// buffer has no items
506
			return false;
507
		}
508
509
		if ( is_wp_error( $buffer ) ) {
510
			// another buffer is currently sending
511
			return false;
512
		}
513
514
		$upload_size   = 0;
515
		$items_to_send = array();
516
		$actions_to_send = array();
517
		// we estimate the total encoded size as we go by encoding each item individually
518
		// this is expensive, but the only way to really know :/
519
		foreach ( $buffer->get_items() as $key => $item ) {
520
			/**
521
			 * Modify the data within an action before it is serialized and sent to the server
522
			 * For example, during full sync this expands Post ID's into full Post objects,
523
			 * so that we don't have to serialize the whole object into the queue.
524
			 *
525
			 * @since 4.2.0
526
			 *
527
			 * @param array The action parameters
528
			 */
529
			$item[1] = apply_filters( "jetpack_sync_before_send_" . $item[0], $item[1] );
530
531
			$encoded_item = $this->codec->encode( $item );
532
533
			$upload_size += strlen( $encoded_item );
534
535
			if ( $upload_size > $this->upload_max_bytes && count( $items_to_send ) > 0 ) {
536
				break;
537
			}
538
539
			$items_to_send[ $key ] = $encoded_item;
540
			$actions_to_send[ $key ] = $item[0];
541
		}
542
543
		/**
544
		 * Fires when data is ready to send to the server.
545
		 * Return false or WP_Error to abort the sync (e.g. if there's an error)
546
		 * The items will be automatically re-sent later
547
		 *
548
		 * @since 4.2.0
549
		 *
550
		 * @param array $data The action buffer
551
		 */
552
		$result = apply_filters( 'jetpack_sync_client_send_data', $items_to_send, $this->codec->name() );
553
554
		if ( ! $result || is_wp_error( $result ) ) {
555
			$result = $this->sync_queue->checkin( $buffer );
556
557
			if ( is_wp_error( $result ) ) {
558
				error_log( "Error checking in buffer: " . $result->get_error_message() );
559
				$this->sync_queue->force_checkin();
560
			}
561
			// try again in 1 minute
562
			$this->schedule_sync( "+1 minute" );
563
		} else {
564
			$processed_actions = array();
565
			foreach( $result as $result_id ) {
566
				if ( isset( $actions_to_send[ $result_id ] ) ) {
567
					$processed_actions[] =  $actions_to_send[ $result_id ];
568
				}
569
			}
570
571
			/**
572
			 * Allows us to keep track of all the actions that have been sent.
573
			 * Allows us to calculate the progress of specific actions.
574
			 *
575
			 * @since 4.2.0
576
			 *
577
			 * @param array $processed_actions The actions that we send successfully.
578
			 */
579
			do_action( 'jetpack_sync_processed_actions', $processed_actions );
580
581
582
			$this->sync_queue->close( $buffer, $result );
583
			// check if there are any more events in the buffer
584
			// if so, schedule a cron job to happen soon
585
			if ( $this->sync_queue->has_any_items() ) {
586
				$this->schedule_sync( "+1 minute" );
587
			}
588
		}
589
	}
590
591
	function expand_wp_insert_post( $args ) {
592
		return array( $args[0], $this->filter_post_content_and_add_links( $args[1] ), $args[2] );
593
	}
594
595
	// Expands wp_insert_post to include filtered content
596
	function filter_post_content_and_add_links( $post ) {
597
598
		/**
599
		 * Filters whether to prevent sending post data to .com
600
		 *
601
		 * Passing true to the filter will prevent the post data from being sent
602
		 * to the WordPress.com.
603
		 * Instead we pass data that will still enable us to do a checksum against the
604
		 * Jetpacks data but will prevent us from displaying the data on in the API as well as
605
		 * other services.
606
		 * @since 4.2.0
607
		 *
608
		 * @param boolean false prevent post data from bing sycned to WordPress.com
609
		 * @param mixed $post WP_POST object
610
		 */
611
		if ( apply_filters( 'jetpack_sync_prevent_sending_post_data', false, $post ) ) {
612
			// We only send the bare necessery object to be able to create a checksum.
613
			$blocked_post = new stdClass();
614
			$blocked_post->ID = $post->ID;
615
			$blocked_post->post_modified = $post->post_modified;
616
			$blocked_post->post_modified_gmt = $post->post_modified_gmt;
617
			$blocked_post->post_status = 'jetpack_sync_blocked';
618
			return $blocked_post;
619
		}
620
621
		if ( 0 < strlen( $post->post_password ) ) {
622
			$post->post_password = 'auto-' . wp_generate_password( 10, false );
623
		}
624
		/** This filter is already documented in core. wp-includes/post-template.php */
625
		$post->post_content_filtered   = apply_filters( 'the_content', $post->post_content );
626
		$post->post_excerpt_filtered   = apply_filters( 'the_content', $post->post_excerpt );
627
		$post->permalink               = get_permalink( $post->ID );
628
		$post->shortlink               = wp_get_shortlink( $post->ID );
629
		$post->dont_email_post_to_subs = get_post_meta( $post->ID, '_jetpack_dont_email_post_to_subs', true );
630
631
		return $post;
632
	}
633
634
635
	function expand_wp_comment_status_change( $args ) {
636
		return array( $args[0], $this->filter_comment_and_add_hc_meta( $args[1] ) );
637
	}
638
639
	function expand_wp_insert_comment( $args ) {
640
		return array( $args[0], $this->filter_comment_and_add_hc_meta( $args[1] ) );
641
	}
642
643
	function filter_comment_and_add_hc_meta( $comment ) {
644
		/**
645
		 * Filters whether to prevent sending comment data to .com
646
		 *
647
		 * Passing true to the filter will prevent the comment data from being sent
648
		 * to the WordPress.com.
649
		 * Instead we pass data that will still enable us to do a checksum against the
650
		 * Jetpacks data but will prevent us from displaying the data on in the API as well as
651
		 * other services.
652
		 * @since 4.2.0
653
		 *
654
		 * @param boolean false prevent post data from bing sycned to WordPress.com
655
		 * @param mixed $comment WP_COMMENT object
656
		 */
657
		if ( apply_filters( 'jetpack_sync_prevent_sending_comment_data', false, $comment ) ) {
658
			$blocked_comment = new stdClass();
659
			$blocked_comment->comment_ID = $comment->comment_ID;
660
			$blocked_comment->comment_date = $comment->comment_date;
661
			$blocked_comment->comment_date_gmt = $comment->comment_date_gmt;
662
			$blocked_comment->comment_approved = 'jetpack_sync_blocked';
663
			return $blocked_comment;
664
		}
665
		
666
		// add meta-property with Highlander Comment meta, which we 
667
		// we need to process synchronously on .com
668
		$hc_post_as = get_comment_meta( $comment->comment_ID, 'hc_post_as', true );
669
		if ( 'wordpress' === $hc_post_as ) {
670
			$meta = array();
671
			$meta['hc_post_as']         = $hc_post_as;
672
			$meta['hc_wpcom_id_sig']    = get_comment_meta( $comment->comment_ID, 'hc_wpcom_id_sig', true );
673
			$meta['hc_foreign_user_id'] = get_comment_meta( $comment->comment_ID, 'hc_foreign_user_id', true );
674
			$comment->meta = $meta;	
675
		}
676
677
		return $comment;
678
	}
679
680
	private function schedule_sync( $when ) {
681
		wp_schedule_single_event( strtotime( $when ), 'jetpack_sync_actions' );
682
	}
683
684
	function force_sync_constants() {
685
		delete_option( self::CONSTANTS_CHECKSUM_OPTION_NAME );
686
		delete_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME );
687
		$this->maybe_sync_constants();
688
689
	}
690
691
	function full_sync_constants() {
692
		/**
693
		 * Tells the client to sync all constants to the server
694
		 *
695
		 * @since 4.1
696
		 *
697
		 * @param boolean Whether to expand constants (should always be true)
698
		 */
699
		do_action( 'jetpack_full_sync_constants', true );
700
		return 1; // The number of actions enqueued
701
	}
702
703
	function force_sync_options() {
704
		/**
705
		 * Tells the client to sync all options to the server
706
		 *
707
		 * @since 4.2.0
708
		 *
709
		 * @param boolean Whether to expand options (should always be true)
710
		 */
711
		do_action( 'jetpack_full_sync_options', true );
712
		return 1; // The number of actions enqueued
713
	}
714
715
	function force_sync_network_options() {
716
		/**
717
		 * Tells the client to sync all network options to the server
718
		 *
719
		 * @since 4.2.0
720
		 *
721
		 * @param boolean Whether to expand options (should always be true)
722
		 */
723
		do_action( 'jetpack_full_sync_network_options', true );
724
		return 1; // The number of actions enqueued
725
	}
726
727
	public function full_sync_callables() {
728
		/**
729
		 * Tells the client to sync all callables to the server
730
		 *
731
		 * @since 4.1
732
		 *
733
		 * @param boolean Whether to expand callables (should always be true)
734
		 */
735
		do_action( 'jetpack_full_sync_callables', true );
736
		return 1; // The number of actions enqueued
737
	}
738
739
	public function full_sync_updates() {
740
		/**
741
		 * Tells the client to sync all updates to the server
742
		 *
743
		 * @since 4.1
744
		 *
745
		 * @param boolean Whether to expand callables (should always be true)
746
		 */
747
		do_action( 'jetpack_full_sync_updates', true );
748
		return 1; // The number of actions enqueued
749
	}
750
751 View Code Duplication
	private function maybe_sync_constants() {
752
		if ( get_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME ) ) {
753
			return;
754
		}
755
756
		$constants = $this->get_all_constants();
757
		if ( empty( $constants ) ) {
758
			return;
759
		}
760
761
		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...
762
		$constants_checksums = get_option( self::CONSTANTS_CHECKSUM_OPTION_NAME, array() );
763
		// only send the constants that have changed
764
		foreach ( $constants as $name => $value ) {
765
			$checksum = $this->get_check_sum( $value );
766
767
			// explicitly not using Identical comparison as get_option returns a string
768
			if ( ! $this->still_valid_checksum( $constants_checksums, $name, $checksum ) ) {
769
				/**
770
				 * Tells the client to sync a constant to the server
771
				 *
772
				 * @since 4.2.0
773
				 *
774
				 * @param string The name of the constant
775
				 * @param mixed The value of the constant
776
				 */
777
				do_action( 'jetpack_sync_constant', $name, $value );
778
				$constants_checksums[ $name ] = $checksum;
779
			}
780
		}
781
782
		update_option( self::CONSTANTS_CHECKSUM_OPTION_NAME, $constants_checksums );
783
	}
784
	// public so that we don't have to store an option for each constant
785
	function get_all_constants() {
786
		return array_combine(
787
			$this->constants_whitelist,
788
			array_map( array( $this, 'get_constant' ), $this->constants_whitelist )
789
		);
790
	}
791
792
	private function get_constant( $constant ) {
793
		return ( defined( $constant ) ) ?
794
			constant( $constant ) 
795
			: null;
796
	}
797
798
	public function get_all_updates() {
799
		return array(
800
			'core' => get_site_transient( 'update_core' ),
801
			'plugins' => get_site_transient( 'update_plugins' ),
802
			'themes' => get_site_transient( 'update_themes' ),
803
		);
804
	}
805
806
	public function force_sync_callables() {
807
		delete_option( self::CALLABLES_CHECKSUM_OPTION_NAME );
808
		delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME );
809
		$this->maybe_sync_callables();
810
	}
811
812 View Code Duplication
	private function maybe_sync_callables() {
813
		if ( get_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME ) ) {
814
			return;
815
		}
816
817
		$callables = $this->get_all_callables();
818
		if ( empty( $callables ) ) {
819
			return;
820
		}
821
822
		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...
823
824
		$callable_checksums = get_option( self::CALLABLES_CHECKSUM_OPTION_NAME , array() );
825
826
		// only send the callables that have changed
827
		foreach ( $callables as $name => $value ) {
828
			$checksum = $this->get_check_sum( $value );
829
			// explicitly not using Identical comparison as get_option returns a string
830
			if ( ! $this->still_valid_checksum( $callable_checksums, $name, $checksum ) ) {
831
				/**
832
				 * Tells the client to sync a callable (aka function) to the server
833
				 *
834
				 * @since 4.2.0
835
				 *
836
				 * @param string The name of the callable
837
				 * @param mixed The value of the callable
838
				 */
839
				do_action( 'jetpack_sync_callable', $name, $value );
840
				$callable_checksums[ $name ] = $checksum;
841
			}
842
		}
843
		update_option( self::CALLABLES_CHECKSUM_OPTION_NAME , $callable_checksums );
844
	}
845
846
	private function still_valid_checksum( $sums_to_check, $name, $new_sum ) {
847
		if ( isset( $sums_to_check[ $name ] ) && $sums_to_check[ $name ] === $new_sum ) {
848
			return true;
849
		}
850
		return false;
851
	}
852
853
	public function get_all_callables() {
854
		return array_combine(
855
			array_keys( $this->callable_whitelist ),
856
			array_map( array( $this, 'get_callable' ), array_values( $this->callable_whitelist ) )
857
		);
858
	}
859
860
	private function get_callable( $callable ) {
861
		return call_user_func( $callable );
862
	}
863
864
	// Is public so that we don't have to store so much data all the options twice.
865
	function get_all_options() {
866
		$options = array();
867
		foreach ( $this->options_whitelist as $option ) {
868
			$options[ $option ] = get_option( $option );
869
		}
870
871
		return $options;
872
	}
873
874
	function get_all_network_options() {
875
		$options = array();
876
		foreach ( $this->network_options_whitelist as $option ) {
877
			$options[ $option ] = get_site_option( $option );
878
		}
879
880
		return $options;
881
	}
882
883
	private function get_check_sum( $values ) {
884
		return crc32( json_encode( $values ) );
885
	}
886
887
	function jetpack_sync_core_icon() {
888
		if ( function_exists( 'get_site_icon_url' ) ) {
889
			$url = get_site_icon_url();
890
		} else {
891
			return;
892
		}
893
894
		require_once( JETPACK__PLUGIN_DIR . 'modules/site-icon/site-icon-functions.php' );
895
		// If there's a core icon, maybe update the option.  If not, fall back to Jetpack's.
896
		if ( ! empty( $url ) && $url !== jetpack_site_icon_url() ) {
897
			// This is the option that is synced with dotcom
898
			Jetpack_Options::update_option( 'site_icon_url', $url );
899
		} else if ( empty( $url ) ) {
900
			Jetpack_Options::delete_option( 'site_icon_url' );
901
		}
902
	}
903
904
	function get_sync_queue() {
905
		return $this->sync_queue;
906
	}
907
908
	function reset_sync_queue() {
909
		$this->sync_queue->reset();
910
	}
911
912
	function get_settings() {
913
		$settings = array();
914
		foreach( array_keys( self::$valid_settings ) as $setting ) {
915
			$default_name = "default_$setting"; // e.g. default_dequeue_max_bytes
916
			$settings[ $setting ] = (int) get_option( self::SETTINGS_OPTION_PREFIX.$setting, Jetpack_Sync_Defaults::$$default_name );
917
		}
918
		return $settings;
919
	}
920
921
	function update_settings( $new_settings ) {
922
		$validated_settings = array_intersect_key( $new_settings, self::$valid_settings );
923
		foreach( $validated_settings as $setting => $value ) {
924
			update_option( self::SETTINGS_OPTION_PREFIX.$setting, $value, true );
925
		}
926
	}
927
928
	function update_options_whitelist() {
929
		/** This filter is already documented in json-endpoints/jetpack/class.wpcom-json-api-get-option-endpoint.php */
930
		$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...
931
	}
932
933
	function set_defaults() {
934
		$this->sync_queue = new Jetpack_Sync_Queue( 'sync' );
935
936
		// saved settings
937
		$settings = $this->get_settings();
938
		$this->set_dequeue_max_bytes( $settings['dequeue_max_bytes'] );
939
		$this->set_upload_max_bytes( $settings['upload_max_bytes'] );
940
		$this->set_upload_max_rows( $settings['upload_max_rows'] );
941
		$this->set_sync_wait_time( $settings['sync_wait_time'] );
942
943
		$this->set_full_sync_client( Jetpack_Sync_Full::getInstance() );
944
		$this->codec                     = new Jetpack_Sync_JSON_Deflate_Codec();
945
		$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...
946
		$this->update_options_whitelist();
947
		$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...
948
		$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...
949
		$this->is_multisite              = is_multisite();
950
951
		// theme mod varies from theme to theme.
952
		$this->options_whitelist[] = 'theme_mods_' . get_option( 'stylesheet' );
953
		if ( $this->is_multisite ) {
954
			$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...
955
		} else {
956
			$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...
957
		}
958
	}
959
960
	function reset_data() {
961
		$this->reset_sync_queue();
962
963
964
		delete_option( self::CONSTANTS_CHECKSUM_OPTION_NAME );
965
		delete_option( self::CALLABLES_CHECKSUM_OPTION_NAME );
966
967
		delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME );
968
		delete_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME );
969
970
		delete_option( self::SYNC_THROTTLE_OPTION_NAME );
971
		delete_option( self::LAST_SYNC_TIME_OPTION_NAME );
972
973
		$valid_settings  = self::$valid_settings;
974
		$settings_prefix =  self::SETTINGS_OPTION_PREFIX;
975
		foreach ( $valid_settings as $option => $value ) {
976
			delete_option( $settings_prefix . $option );
977
		}
978
	}
979
980
	function uninstall() {
981
		// Lets delete all the other fun stuff like transient and option and the sync queue
982
		$this->reset_data();
983
984
		// delete the full sync status
985
		delete_option( 'jetpack_full_sync_status' );
986
987
		// clear the sync cron.
988
		wp_clear_scheduled_hook( 'jetpack_sync_actions' );
989
	}
990
}
991