Completed
Push — add/multisite-testing-to-travi... ( 14206e...6cfb7c )
by
unknown
08:41
created

Jetpack_Sync_Client::expand_wp_insert_comment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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