Completed
Push — update/grey-out-elements-unava... ( 06b29b...1fb283 )
by
unknown
16:50 queued 06:18
created

Jetpack_Sync_Client::set_dequeue_max_bytes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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
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
		// themes
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
154
		// multi site network options
155
		if ( $this->is_multisite ) {
156
			add_action( 'add_site_option', $handler, 10, 2 );
157
			add_action( 'update_site_option', $handler, 10, 3 );
158
			add_action( 'delete_site_option', $handler, 10, 1 );
159
		}
160
161
		// synthetic actions for full sync
162
		add_action( 'jetpack_full_sync_start', $handler );
163
		add_action( 'jetpack_full_sync_end', $handler );
164
		add_action( 'jetpack_full_sync_options', $handler );
165
		add_action( 'jetpack_full_sync_posts', $handler ); // also sends post meta
166
		add_action( 'jetpack_full_sync_comments', $handler ); // also send comments meta
167
		add_action( 'jetpack_full_sync_users', $handler );
168
		add_action( 'jetpack_full_sync_terms', $handler, 10, 2 );
169
		if ( is_multisite() ) {
170
			add_action( 'jetpack_full_sync_network_options', $handler );
171
		}
172
173
174
		// TODO: Callables, Constanst, Network Options, Users, Terms
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
175
176
		/**
177
		 * Sync all pending actions with server
178
		 */
179
		add_action( 'jetpack_sync_actions', array( $this, 'do_sync' ) );
180
	}
181
182
	// TODO: Refactor to use one set whitelist function, with one is_whitelisted.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
183
	function set_options_whitelist( $options ) {
184
		$this->options_whitelist = $options;
185
	}
186
187
	function get_options_whitelist() {
188
		return $this->options_whitelist;
189
	}
190
191
	function set_constants_whitelist( $constants ) {
192
		$this->constants_whitelist = $constants;
193
	}
194
195
	function get_callable_whitelist() {
196
		return $this->callable_whitelist;
197
	}
198
199
	function set_callable_whitelist( $callables ) {
200
		$this->callable_whitelist = $callables;
201
	}
202
203
	function set_network_options_whitelist( $options ) {
204
		$this->network_options_whitelist = $options;
205
	}
206
207
	function set_dequeue_max_bytes( $size ) {
208
		$this->dequeue_max_bytes = $size;
209
	}
210
211
	// in bytes
212
	function set_upload_max_bytes( $max_bytes ) {
213
		$this->upload_max_bytes = $max_bytes;
214
	}
215
216
	// in rows
217
	function set_upload_max_rows( $max_rows ) {
218
		$this->upload_max_rows = $max_rows;
219
	}
220
221
	// in seconds
222
	function set_sync_wait_time( $seconds ) {
223
		update_option( self::SYNC_THROTTLE_OPTION_NAME, $seconds, true );
224
	}
225
226
	function get_sync_wait_time() {
227
		return get_option( self::SYNC_THROTTLE_OPTION_NAME );
228
	}
229
230
	private function get_last_sync_time() {
231
		return (double) get_option( self::LAST_SYNC_TIME_OPTION_NAME );
232
	}
233
234
	private function set_last_sync_time() {
235
		return update_option( self::LAST_SYNC_TIME_OPTION_NAME, microtime( true ), true );
236
	}
237
238
	function set_taxonomy_whitelist( $taxonomies ) {
239
		$this->taxonomy_whitelist = $taxonomies;
240
	}
241
242
	function is_whitelisted_option( $option ) {
243
		return in_array( $option, $this->options_whitelist );
244
	}
245
246
	function is_whitelisted_network_option( $option ) {
247
		return $this->is_multisite && in_array( $option, $this->network_options_whitelist );
248
	}
249
250
	function set_codec( iJetpack_Sync_Codec $codec ) {
251
		$this->codec = $codec;
252
	}
253
254
	function set_full_sync_client( $full_sync_client ) {
255
		if ( $this->full_sync_client ) {
256
			remove_action( 'jetpack_sync_full', array( $this->full_sync_client, 'start' ) );
257
		}
258
259
		$this->full_sync_client = $full_sync_client;
260
261
		/**
262
		 * Sync all objects in the database with the server
263
		 */
264
		add_action( 'jetpack_sync_full', array( $this->full_sync_client, 'start' ) );
265
	}
266
267
	function get_full_sync_client() {
268
		return $this->full_sync_client;
269
	}
270
271
	function action_handler() {
272
		// TODO: it's really silly to have this function here - it should be
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
273
		// wherever we initialize the action listeners or we're just wasting cycles
274
		if ( Jetpack::is_development_mode() || Jetpack::is_staging_site() ) {
275
			return false;
276
		}
277
278
		$current_filter = current_filter();
279
		$args           = func_get_args();
280
281
		if ( $current_filter === 'wp_insert_post' && $args[1]->post_type === 'revision' ) {
282
			return;
283
		}
284
285
		if ( in_array( $current_filter, array( 'deleted_option', 'added_option', 'updated_option' ) )
286
		     &&
287
		     ! $this->is_whitelisted_option( $args[0] )
288
		) {
289
			return;
290
		}
291
292
		if ( in_array( $current_filter, array( 'delete_site_option', 'add_site_option', 'update_site_option' ) )
293
		     &&
294
		     ! $this->is_whitelisted_network_option( $args[0] )
295
		) {
296
			return;
297
		}
298
299
		// don't sync private meta
300
		if ( preg_match( '/^(added|updated|deleted)_.*_meta$/', $current_filter )
301
		     && $args[2][0] === '_'
302
		     && ! 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...
303
		) {
304
			return;
305
		}
306
307
		// if we add any items to the queue, we should 
308
		// try to ensure that our script can't be killed before
309
		// they are sent
310
		if ( function_exists( 'ignore_user_abort' ) ) {
311
			ignore_user_abort( true );
312
		}
313
314
		$this->sync_queue->add( array(
315
			$current_filter,
316
			$args,
317
			get_current_user_id(),
318
			microtime( true )
319
		) );
320
	}
321
322
	function send_theme_info() {
323
		global $_wp_theme_features;
324
325
		$theme_support = array();
326
327
		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...
328
			$has_support = current_theme_supports( $theme_feature );
329
			if ( $has_support ) {
330
				$theme_support[ $theme_feature ] = $_wp_theme_features[ $theme_feature ];
331
			}
332
333
		}
334
335
		/**
336
		 * Fires when the client needs to sync theme support info
337
		 * Only sends theme support attributes whitelisted in Jetpack_Sync_Defaults::$default_theme_support_whitelist
338
		 *
339
		 * @since 4.1.0
340
		 *
341
		 * @param object the theme support hash
342
		 */
343
		do_action( 'jetpack_sync_current_theme_support', $theme_support );
344
	}
345
346
	function send_wp_version( $update, $meta_data ) {
347
		if ( 'update' === $meta_data['action'] && 'core' === $meta_data['type'] ) {
348
			global $wp_version;
349
350
			/**
351
			 * Fires when the client needs to sync WordPress version
352
			 *
353
			 * @since 4.1.0
354
			 *
355
			 * @param string The WordPress version number
356
			 */
357
			do_action( 'jetpack_sync_wp_version', $wp_version );
358
		}
359
	}
360
361
	function save_term_handler( $term_id, $tt_id, $taxonomy ) {
362
		if ( class_exists( 'WP_Term' ) ) {
363
			$term_object = WP_Term::get_instance( $term_id, $taxonomy );
364
		} else {
365
			$term_object = get_term_by( 'id', $term_id, $taxonomy );
366
		}
367
368
		/**
369
		 * Fires when the client needs to sync a new term
370
		 *
371
		 * @since 4.1.0
372
		 *
373
		 * @param object the Term object
374
		 */
375
		do_action( 'jetpack_sync_save_term', $term_object );
376
	}
377
378
	function send_attachment_info( $attachment_id ) {
379
		$attachment = get_post( $attachment_id );
380
381
		/**
382
		 * Fires when the client needs to sync an attachment for a post
383
		 *
384
		 * @since 4.1.0
385
		 *
386
		 * @param int The attachment ID
387
		 * @param object The attachment
388
		 */
389
		do_action( 'jetpack_sync_save_add_attachment', $attachment_id, $attachment );
390
	}
391
392
	function save_user_handler( $user_id, $old_user_data = null ) {
393
		$user = $this->sanitize_user( get_user_by( 'id', $user_id ) );
394
395
		// Older versions of WP don't pass the old_user_data in ->data
396
		if ( isset( $old_user_data->data ) ) {
397
			$old_user = $old_user_data->data;
398
		} else {
399
			$old_user = $old_user_data;
400
		}
401
402
		if ( $old_user !== null ) {
403
			unset( $old_user->user_pass );
404
			if ( serialize( $old_user ) === serialize( $user->data ) ) {
405
				return;
406
			}
407
		}
408
409
		/**
410
		 * Fires when the client needs to sync an updated user
411
		 *
412
		 * @since 4.1.0
413
		 *
414
		 * @param object The WP_User object
415
		 */
416
		do_action( 'jetpack_sync_save_user', $user );
417
	}
418
419
	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...
420
		$user = $this->sanitize_user( get_user_by( 'id', $user_id ) );
421
422
		/**
423
		 * Fires when the client needs to sync an updated user
424
		 *
425
		 * @since 4.1.0
426
		 *
427
		 * @param object The WP_User object
428
		 */
429
		do_action( 'jetpack_sync_save_user', $user );
430
	}
431
432
	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...
433
		$user = $this->sanitize_user( get_user_by( 'id', $user_id ) );
434
		if ( $meta_key === $user->cap_key ) {
435
			/**
436
			 * Fires when the client needs to sync an updated user
437
			 *
438
			 * @since 4.1.0
439
			 *
440
			 * @param object The WP_User object
441
			 */
442
			do_action( 'jetpack_sync_save_user', $user );
443
		}
444
	}
445
446
	public function sanitize_user( $user ) {
447
		unset( $user->data->user_pass );
448
449
		return $user;
450
	}
451
452
453
	function do_sync() {
454
		// don't sync if importing
455
		if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
456
			$this->schedule_sync( "+1 minute" );
457
458
			return false;
459
		}
460
461
		// don't sync if we are throttled
462
		$sync_wait = $this->get_sync_wait_time();
463
		$last_sync = $this->get_last_sync_time();
464
465
		if ( $last_sync && $sync_wait && $last_sync + $sync_wait > microtime( true ) ) {
466
			return false;
467
		}
468
469
		$this->set_last_sync_time();
470
		$this->maybe_sync_constants();
471
		$this->maybe_sync_callables();
472
473
		if ( $this->sync_queue->size() === 0 ) {
474
			return false;
475
		}
476
477
		// now that we're sure we are about to sync, try to
478
		// ignore user abort so we can avoid getting into a
479
		// bad state
480
		if ( function_exists( 'ignore_user_abort' ) ) {
481
			ignore_user_abort( true );
482
		}
483
484
		$buffer = $this->sync_queue->checkout_with_memory_limit( $this->dequeue_max_bytes, $this->upload_max_rows );
485
486
		if ( ! $buffer ) {
487
			// buffer has no items
488
			return false;
489
		}
490
491
		if ( is_wp_error( $buffer ) ) {
492
			// another buffer is currently sending
493
			return false;
494
		}
495
496
		$upload_size   = 0;
497
		$items_to_send = array();
498
499
		// we estimate the total encoded size as we go by encoding each item individually
500
		// this is expensive, but the only way to really know :/
501
		foreach ( $buffer->get_items() as $key => $item ) {
502
503
			/**
504
			 * Modify the data within an action before it is serialized and sent to the server
505
			 * For example, during full sync this expands Post ID's into full Post objects,
506
			 * so that we don't have to serialize the whole object into the queue.
507
			 *
508
			 * @since 4.1.0
509
			 *
510
			 * @param array The action parameters
511
			 */
512
			$item[1] = apply_filters( "jetpack_sync_before_send_" . $item[0], $item[1] );
513
514
			$encoded_item = $this->codec->encode( $item );
515
			$upload_size += strlen( $encoded_item );
516
517
			if ( $upload_size > $this->upload_max_bytes && count( $items_to_send ) > 0 ) {
518
				break;
519
			}
520
521
			$items_to_send[ $key ] = $encoded_item;
522
		}
523
524
		/**
525
		 * Fires when data is ready to send to the server.
526
		 * Return false or WP_Error to abort the sync (e.g. if there's an error)
527
		 * The items will be automatically re-sent later
528
		 *
529
		 * @since 4.1
530
		 *
531
		 * @param array $data The action buffer
532
		 */
533
		$result = apply_filters( 'jetpack_sync_client_send_data', $items_to_send );
534
535
		if ( ! $result || is_wp_error( $result ) ) {
536
			// error_log("There was an error sending data:");
537
			// error_log(print_r($result, 1));
538
			$result = $this->sync_queue->checkin( $buffer );
539
540
			if ( is_wp_error( $result ) ) {
541
				error_log( "Error checking in buffer: " . $result->get_error_message() );
542
				$this->sync_queue->force_checkin();
543
			}
544
			// try again in 1 minute
545
			$this->schedule_sync( "+1 minute" );
546
		} else {
547
548
			// scan the sent data to see if a full sync started or finished
549
			if ( $this->buffer_includes_action( $buffer, 'jetpack_full_sync_start' ) ) {
550
				$this->full_sync_client->set_status_sending_started();
551
			}
552
553
			if ( $this->buffer_includes_action( $buffer, 'jetpack_full_sync_end' ) ) {
554
				$this->full_sync_client->set_status_sending_finished();
555
			}
556
557
			$this->sync_queue->close( $buffer, $result );
558
			// check if there are any more events in the buffer
559
			// if so, schedule a cron job to happen soon
560
			if ( $this->sync_queue->has_any_items() ) {
561
				$this->schedule_sync( "+1 minute" );
562
			}
563
		}
564
	}
565
566
	private function buffer_includes_action( $buffer, $action_name ) {
567
		foreach ( $buffer->get_items() as $item ) {
568
			if ( $item[0] === $action_name ) {
569
				return true;
570
			}
571
		}
572
573
		return false;
574
	}
575
576
	function expand_wp_insert_post( $args ) {
577
		return array( $args[0], $this->filter_post_content_and_add_links( $args[1] ), $args[2] );
578
	}
579
580
	// Expands wp_insert_post to include filtered content
581
	function filter_post_content_and_add_links( $post ) {
582
		if ( 0 < strlen( $post->post_password ) ) {
583
			$post->post_password = 'auto-' . wp_generate_password( 10, false );
584
		}
585
		/** This filter is already documented in core. wp-includes/post-template.php */
586
		$post->post_content_filtered   = apply_filters( 'the_content', $post->post_content );
587
		$post->permalink               = get_permalink( $post->ID );
588
		$post->shortlink               = wp_get_shortlink( $post->ID );
589
		
590
		// legacy fields until we fully sync users
591
		$extra = array();
592
		$extra['author_email']            = get_the_author_meta( 'email', $post->post_author );
593
		$extra['author_display_name']     = get_the_author_meta( 'display_name', $post->post_author );
594
		$extra['dont_email_post_to_subs'] = get_post_meta( $post->ID, '_jetpack_dont_email_post_to_subs', true );
595
		$post->extra = $extra;
596
597
		return $post;
598
	}
599
600
	function expand_wp_comment_status_change( $args ) {
601
		return array( $args[0], $this->filter_comment_and_add_hc_meta( $args[1] ) );
602
	}
603
604
	function expand_wp_insert_comment( $args ) {
605
		return array( $args[0], $this->filter_comment_and_add_hc_meta( $args[1] ) );
606
	}
607
608
	function filter_comment_and_add_hc_meta( $comment ) {
609
		// add meta-property with Highlander Comment meta, which we 
610
		// we need to process synchronously on .com
611
		$hc_post_as = get_comment_meta( $comment->comment_ID, 'hc_post_as', true );
612
		if ( 'wordpress' === $hc_post_as ) {
613
			$meta = array();
614
			$meta['hc_post_as']         = $hc_post_as;
615
			$meta['hc_wpcom_id_sig']    = get_comment_meta( $comment->comment_ID, 'hc_wpcom_id_sig', true );
616
			$meta['hc_foreign_user_id'] = get_comment_meta( $comment->comment_ID, 'hc_foreign_user_id', true );
617
			$comment->meta = $meta;	
618
		}
619
620
		return $comment;
621
	}
622
623
	private function schedule_sync( $when ) {
624
		wp_schedule_single_event( strtotime( $when ), 'jetpack_sync_actions' );
625
	}
626
627
	function force_sync_constants() {
628
		foreach ( $this->constants_whitelist as $name ) {
629
			delete_option( self::CONSTANTS_CHECKSUM_OPTION_NAME . "_$name" );
630
		}
631
632
		delete_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME );
633
		$this->maybe_sync_constants();
634
	}
635
636
	function force_sync_options() {
637
		/**
638
		 * Tells the client to sync all options to the server
639
		 *
640
		 * @since 4.1
641
		 *
642
		 * @param boolean Whether to expand options (should always be true)
643
		 */
644
		do_action( 'jetpack_full_sync_options', true );
645
	}
646
647
	function force_sync_network_options() {
648
		/**
649
		 * Tells the client to sync all network options to the server
650
		 *
651
		 * @since 4.1
652
		 *
653
		 * @param boolean Whether to expand options (should always be true)
654
		 */
655
		do_action( 'jetpack_full_sync_network_options', true );
656
	}
657
658 View Code Duplication
	private function maybe_sync_constants() {
659
		if ( get_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME ) ) {
660
			return;
661
		}
662
663
		$constants = $this->get_all_constants();
664
		if ( empty( $constants ) ) {
665
			return;
666
		}
667
668
		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...
669
670
		// only send the constants that have changed
671
		foreach ( $constants as $name => $value ) {
672
			$checksum = $this->get_check_sum( $value );
673
674
			// explicitly not using Identical comparison as get_option returns a string
675
			if ( $checksum != get_option( self::CONSTANTS_CHECKSUM_OPTION_NAME . "_$name" ) ) {
676
				/**
677
				 * Tells the client to sync a constant to the server
678
				 *
679
				 * @since 4.1
680
				 *
681
				 * @param string The name of the constant
682
				 * @param mixed The value of the constant
683
				 */
684
				do_action( 'jetpack_sync_constant', $name, $value );
685
				update_option( self::CONSTANTS_CHECKSUM_OPTION_NAME . "_$name", $checksum );
686
			}
687
		}
688
	}
689
690
	private function get_all_constants() {
691
		return array_combine(
692
			$this->constants_whitelist,
693
			array_map( array( $this, 'get_constant' ), $this->constants_whitelist )
694
		);
695
	}
696
697
	private function get_constant( $constant ) {
698
		if ( defined( $constant ) ) {
699
			return constant( $constant );
700
		}
701
702
		return null;
703
	}
704
705
	public function force_sync_callables() {
706
		foreach ( $this->callable_whitelist as $name => $config ) {
707
			delete_option( self::CALLABLES_CHECKSUM_OPTION_NAME . "_$name" );
708
		}
709
710
		delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME );
711
		$this->maybe_sync_callables();
712
	}
713
714 View Code Duplication
	private function maybe_sync_callables() {
715
		if ( get_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME ) ) {
716
			return;
717
		}
718
719
		$callables = $this->get_all_callables();
720
		if ( empty( $callables ) ) {
721
			return;
722
		}
723
724
		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...
725
726
		// only send the callables that have changed
727
		foreach ( $callables as $name => $value ) {
728
			$checksum = $this->get_check_sum( $value );
729
			// explicitly not using Identical comparison as get_option returns a string
730
			if ( $checksum != get_option( self::CALLABLES_CHECKSUM_OPTION_NAME . "_$name" ) ) {
731
				/**
732
				 * Tells the client to sync a callable (aka function) to the server
733
				 *
734
				 * @since 4.1
735
				 *
736
				 * @param string The name of the callable
737
				 * @param mixed The value of the callable
738
				 */
739
				do_action( 'jetpack_sync_callable', $name, $value );
740
				update_option( self::CALLABLES_CHECKSUM_OPTION_NAME . "_$name", $checksum );
741
			}
742
		}
743
	}
744
745
	private function get_all_callables() {
746
		return array_combine(
747
			array_keys( $this->callable_whitelist ),
748
			array_map( array( $this, 'get_callable' ), array_values( $this->callable_whitelist ) )
749
		);
750
	}
751
752
	private function get_callable( $callable ) {
753
		return call_user_func( $callable );
754
	}
755
756
	// Is public so that we don't have to store so much data all the options twice.
757
	function get_all_options() {
758
		$options = array();
759
		foreach ( $this->options_whitelist as $option ) {
760
			$options[ $option ] = get_option( $option );
761
		}
762
763
		return $options;
764
	}
765
766
	function get_all_network_options() {
767
		$options = array();
768
		foreach ( $this->network_options_whitelist as $option ) {
769
			$options[ $option ] = get_site_option( $option );
770
		}
771
772
		return $options;
773
	}
774
775
	private function get_check_sum( $values ) {
776
		return crc32( json_encode( $values ) );
777
	}
778
779
	function jetpack_sync_core_icon() {
780
		if ( function_exists( 'get_site_icon_url' ) ) {
781
			$url = get_site_icon_url();
782
		} else {
783
			return;
784
		}
785
786
		require_once( JETPACK__PLUGIN_DIR . 'modules/site-icon/site-icon-functions.php' );
787
		// If there's a core icon, maybe update the option.  If not, fall back to Jetpack's.
788
		if ( ! empty( $url ) && $url !== jetpack_site_icon_url() ) {
789
			// This is the option that is synced with dotcom
790
			Jetpack_Options::update_option( 'site_icon_url', $url );
791
		} else if ( empty( $url ) ) {
792
			Jetpack_Options::delete_option( 'site_icon_url' );
793
		}
794
	}
795
796
	function get_sync_queue() {
797
		return $this->sync_queue;
798
	}
799
800
	function reset_sync_queue() {
801
		$this->sync_queue->reset();
802
	}
803
804
	function get_settings() {
805
		$settings = array();
806
		foreach( array_keys( self::$valid_settings ) as $setting ) {
807
			$default_name = "default_$setting"; // e.g. default_dequeue_max_bytes
808
			$settings[ $setting ] = (int) get_option( self::SETTINGS_OPTION_PREFIX.$setting, Jetpack_Sync_Defaults::$$default_name );
809
		}
810
		return $settings;
811
	}
812
813
	function update_settings( $new_settings ) {
814
		$validated_settings = array_intersect_key( $new_settings, self::$valid_settings );
815
		foreach( $validated_settings as $setting => $value ) {
816
			update_option( self::SETTINGS_OPTION_PREFIX.$setting, $value, true );
817
		}
818
	}
819
820
	function update_options_whitelist() {
821
		/** This filter is already documented in json-endpoints/jetpack/class.wpcom-json-api-get-option-endpoint.php */
822
		$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...
823
	}
824
825
	function set_defaults() {
826
		$this->sync_queue = new Jetpack_Sync_Queue( 'sync' );
827
828
		// saved settings
829
		$settings = $this->get_settings();
830
		$this->set_dequeue_max_bytes( $settings['dequeue_max_bytes'] );
831
		$this->set_upload_max_bytes( $settings['upload_max_bytes'] );
832
		$this->set_upload_max_rows( $settings['upload_max_rows'] );
833
		$this->set_sync_wait_time( $settings['sync_wait_time'] );
834
835
		$this->set_full_sync_client( Jetpack_Sync_Full::getInstance() );
836
		$this->codec                     = new Jetpack_Sync_Deflate_Codec();
837
		$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...
838
		$this->update_options_whitelist();
839
		$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...
840
		$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...
841
		$this->is_multisite              = is_multisite();
842
843
		// theme mod varies from theme to theme.
844
		$this->options_whitelist[] = 'theme_mods_' . get_option( 'stylesheet' );
845
		if ( $this->is_multisite ) {
846
			$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...
847
		} else {
848
			$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...
849
		}
850
	}
851
852
	function reset_data() {
853
		$this->reset_sync_queue();
854
	}
855
}
856