Completed
Push — update/gitignore ( 7f3e2e...30ec70 )
by
unknown
497:33 queued 488:24
created

Jetpack_Sync_Client::set_upload_max_rows()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
eloc 2
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
17
	private $checkout_memory_size;
18
	private $upload_max_bytes;
19
	private $upload_max_rows;
20
	private $sync_queue;
21
	private $full_sync_client;
22
	private $codec;
23
	private $options_whitelist;
24
	private $constants_whitelist;
25
	private $meta_types = array( 'post', 'comment' );
26
	private $callable_whitelist;
27
	private $network_options_whitelist;
28
	private $taxonomy_whitelist;
29
	private $is_multisite;
30
31
	// singleton functions
32
	private static $instance;
33
34
	public static function getInstance() {
35
		if ( null === self::$instance ) {
36
			self::$instance = new self();
37
		}
38
39
		return self::$instance;
40
	}
41
42
	// this is necessary because you can't use "new" when you declare instance properties >:(
43
	protected function __construct() {
44
		$this->set_defaults();
45
		$this->init();
46
	}
47
48
	private function init() {
49
50
		$handler = array( $this, 'action_handler' );
51
52
		/**
53
		 * Most of the following hooks are sent to the same $handler
54
		 * for immediate serialization and queuing be sent to the server.
55
		 * The only exceptions are actions which need additional processing.
56
		 */
57
58
		// constants
59
		add_action( 'jetpack_sync_current_constants', $handler, 10 );
60
61
		// callables
62
		add_action( 'jetpack_sync_current_callable', $handler, 10, 2 );
63
64
		// posts
65
		add_action( 'wp_insert_post', $handler, 10, 3 );
66
		add_action( 'deleted_post', $handler, 10 );
67
		add_filter( 'jetpack_sync_before_send_wp_insert_post', array( $this, 'expand_wp_insert_post' ) );
68
69
		add_action( 'jetpack_publicize_post', $handler );
70
71
		// attachments
72
73
		add_action( 'edit_attachment', array( $this, 'send_attachment_info' ) );
74
		// Once we don't have to support 4.3 we can start using add_action( 'attachment_updated', $handler, 10, 3 ); instead
75
		add_action( 'add_attachment', array( $this, 'send_attachment_info' ) );
76
		add_action( 'jetpack_sync_save_add_attachment', $handler, 10, 2 );
77
78
		// comments
79
		add_action( 'wp_insert_comment', $handler, 10, 2 );
80
		add_action( 'deleted_comment', $handler, 10 );
81
		add_action( 'trashed_comment', $handler, 10 );
82
		add_action( 'spammed_comment', $handler, 10 );
83
84
		// even though it's messy, we implement these hooks because 
85
		// the edit_comment hook doesn't include the data
86
		// so this saves us a DB read for every comment event
87
		foreach ( array( '', 'trackback', 'pingback' ) as $comment_type ) {
88
			foreach ( array( 'unapproved', 'approved' ) as $comment_status ) {
89
				add_action( "comment_{$comment_status}_{$comment_type}", $handler, 10, 2 );
90
			}
91
		}
92
93
		// options
94
		add_action( 'added_option', $handler, 10, 2 );
95
		add_action( 'updated_option', $handler, 10, 3 );
96
		add_action( 'deleted_option', $handler, 10, 1 );
97
98
		// Sync Core Icon: Detect changes in Core's Site Icon and make it syncable.
99
		add_action( 'add_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) );
100
		add_action( 'update_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) );
101
		add_action( 'delete_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) );
102
103
		// wordpress version
104
		add_action( 'upgrader_process_complete', array( $this, 'send_wp_version' ), 10, 2 );
105
		add_action( 'jetpack_sync_wp_version', $handler );
106
107
		// themes
108
		add_action( 'switch_theme', array( $this, 'send_theme_info' ) );
109
		add_action( 'jetpack_sync_current_theme_support', $handler, 10 ); // custom hook, see meta-hooks below
110
111
		// post-meta, and in the future - other meta?
112
		foreach ( $this->meta_types as $meta_type ) {
113
			add_action( "added_{$meta_type}_meta", $handler, 10, 4 );
114
			add_action( "updated_{$meta_type}_meta", $handler, 10, 4 );
115
			add_action( "deleted_{$meta_type}_meta", $handler, 10, 4 );
116
		}
117
118
		// terms
119
		add_action( 'created_term', array( $this, 'save_term_handler' ), 10, 3 );
120
		add_action( 'edited_term', array( $this, 'save_term_handler' ), 10, 3 );
121
		add_action( 'jetpack_sync_save_term', $handler, 10, 4 );
122
		add_action( 'delete_term', $handler, 10, 4 );
123
		add_action( 'set_object_terms', $handler, 10, 6 );
124
		add_action( 'deleted_term_relationships', $handler, 10, 2 );
125
126
		// users
127
		add_action( 'user_register', array( $this, 'save_user_handler' ) );
128
		add_action( 'profile_update', array( $this, 'save_user_handler' ), 10, 2 );
129
		add_action( 'jetpack_sync_save_user', $handler, 10, 2 );
130
		add_action( 'deleted_user', $handler, 10, 2 );
131
132
		// user roles
133
		add_action( 'add_user_role', array( $this, 'save_user_role_handler' ), 10, 2 );
134
		add_action( 'set_user_role', array( $this, 'save_user_role_handler' ), 10, 3 );
135
		add_action( 'remove_user_role', array( $this, 'save_user_role_handler' ), 10, 2 );
136
137
138
		// user capabilities
139
		add_action( 'added_user_meta', array( $this, 'save_user_cap_handler' ), 10, 4 );
140
		add_action( 'updated_user_meta', array( $this, 'save_user_cap_handler' ), 10, 4 );
141
		add_action( 'deleted_user_meta', array( $this, 'save_user_cap_handler' ), 10, 4 );
142
143
		// themes
144
		add_action( 'set_site_transient_update_plugins', $handler, 10, 1 );
145
		add_action( 'set_site_transient_update_themes', $handler, 10, 1 );
146
		add_action( 'set_site_transient_update_core', $handler, 10, 1 );
147
148
		// multi site network options
149
		if ( $this->is_multisite ) {
150
			add_action( 'add_site_option', $handler, 10, 2 );
151
			add_action( 'update_site_option', $handler, 10, 3 );
152
			add_action( 'delete_site_option', $handler, 10, 1 );
153
		}
154
155
		// synthetic actions for full sync
156
		add_action( 'jetpack_full_sync_start', $handler );
157
		add_action( 'jetpack_full_sync_end', $handler );
158
		add_action( 'jetpack_full_sync_options', $handler );
159
		add_action( 'jetpack_full_sync_posts', $handler ); // also sends post meta
160
		add_action( 'jetpack_full_sync_comments', $handler ); // also send comments meta
161
		add_action( 'jetpack_full_sync_users', $handler );
162
		add_action( 'jetpack_full_sync_terms', $handler, 10, 2 );
163
		if ( is_multisite() ) {
164
			add_action( 'jetpack_full_sync_network_options', $handler );
165
		}
166
167
168
		// 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...
169
170
		/**
171
		 * Sync all pending actions with server
172
		 */
173
		add_action( 'jetpack_sync_actions', array( $this, 'do_sync' ) );
174
	}
175
176
	// 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...
177
	function set_options_whitelist( $options ) {
178
		$this->options_whitelist = $options;
179
	}
180
181
	function set_constants_whitelist( $constants ) {
182
		$this->constants_whitelist = $constants;
183
	}
184
185
	function get_callable_whitelist() {
186
		return $this->callable_whitelist;
187
	}
188
189
	function set_callable_whitelist( $callables ) {
190
		$this->callable_whitelist = $callables;
191
	}
192
193
	function set_network_options_whitelist( $options ) {
194
		$this->network_options_whitelist = $options;
195
	}
196
197
	function set_send_buffer_memory_size( $size ) {
198
		$this->checkout_memory_size = $size;
199
	}
200
201
	// in bytes
202
	function set_upload_max_bytes( $max_bytes ) {
203
		$this->upload_max_bytes = $max_bytes;
204
	}
205
206
	// in rows
207
	function set_upload_max_rows( $max_rows ) {
208
		$this->upload_max_rows = $max_rows;
209
	}
210
211
	// in seconds
212
	function set_min_sync_wait_time( $seconds ) {
213
		update_option( self::SYNC_THROTTLE_OPTION_NAME, $seconds, true );
214
	}
215
216
	function get_min_sync_wait_time() {
217
		return get_option( self::SYNC_THROTTLE_OPTION_NAME );
218
	}
219
220
	private function get_last_sync_time() {
221
		return (double) get_option( self::LAST_SYNC_TIME_OPTION_NAME );
222
	}
223
224
	private function set_last_sync_time() {
225
		return update_option( self::LAST_SYNC_TIME_OPTION_NAME, microtime( true ), true );
226
	}
227
228
	function set_taxonomy_whitelist( $taxonomies ) {
229
		$this->taxonomy_whitelist = $taxonomies;
230
	}
231
232
	function is_whitelisted_option( $option ) {
233
		return in_array( $option, $this->options_whitelist );
234
	}
235
236
	function is_whitelisted_network_option( $option ) {
237
		return $this->is_multisite && in_array( $option, $this->network_options_whitelist );
238
	}
239
240
	function set_codec( iJetpack_Sync_Codec $codec ) {
241
		$this->codec = $codec;
242
	}
243
244
	function set_full_sync_client( $full_sync_client ) {
245
		if ( $this->full_sync_client ) {
246
			remove_action( 'jetpack_sync_full', array( $this->full_sync_client, 'start' ) );
247
		}
248
249
		$this->full_sync_client = $full_sync_client;
250
251
		/**
252
		 * Sync all objects in the database with the server
253
		 */
254
		add_action( 'jetpack_sync_full', array( $this->full_sync_client, 'start' ) );
255
	}
256
257
	function get_full_sync_client() {
258
		return $this->full_sync_client;
259
	}
260
261
	function action_handler() {
262
		// 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...
263
		// wherever we initialize the action listeners or we're just wasting cycles
264
		if ( Jetpack::is_development_mode() || Jetpack::is_staging_site() ) {
265
			return false;
266
		}
267
268
		$current_filter = current_filter();
269
		$args           = func_get_args();
270
271
		if ( $current_filter === 'wp_insert_post' && $args[1]->post_type === 'revision' ) {
272
			return;
273
		}
274
275
		if ( in_array( $current_filter, array( 'deleted_option', 'added_option', 'updated_option' ) )
276
		     &&
277
		     ! $this->is_whitelisted_option( $args[0] )
278
		) {
279
			return;
280
		}
281
282
		if ( in_array( $current_filter, array( 'delete_site_option', 'add_site_option', 'update_site_option' ) )
283
		     &&
284
		     ! $this->is_whitelisted_network_option( $args[0] )
285
		) {
286
			return;
287
		}
288
289
		// don't sync private meta
290
		if ( preg_match( '/^(added|updated|deleted)_.*_meta$/', $current_filter )
291
		     && $args[2][0] === '_'
292
		     && ! 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...
293
		) {
294
			return;
295
		}
296
297
		$this->sync_queue->add( array(
298
			$current_filter,
299
			$args,
300
			get_current_user_id(),
301
			microtime( true )
302
		) );
303
	}
304
305
	function send_theme_info() {
306
		global $_wp_theme_features;
307
308
		$theme_support = array();
309
310
		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...
311
			$has_support = current_theme_supports( $theme_feature );
312
			if ( $has_support ) {
313
				$theme_support[ $theme_feature ] = $_wp_theme_features[ $theme_feature ];
314
			}
315
316
		}
317
		do_action( 'jetpack_sync_current_theme_support', $theme_support );
318
	}
319
320
	function send_wp_version( $update, $meta_data ) {
321
		if ( 'update' === $meta_data['action'] && 'core' === $meta_data['type'] ) {
322
			global $wp_version;
323
			do_action( 'jetpack_sync_wp_version', $wp_version );
324
		}
325
	}
326
327
	function save_term_handler( $term_id, $tt_id, $taxonomy ) {
328
		if ( class_exists( 'WP_Term' ) ) {
329
			$term_object = WP_Term::get_instance( $term_id, $taxonomy );
330
		} else {
331
			$term_object = get_term_by( 'id', $term_id, $taxonomy );
332
		}
333
334
		do_action( 'jetpack_sync_save_term', $term_object );
335
	}
336
337
	function send_attachment_info( $attachment_id ) {
338
		$attachment = get_post( $attachment_id );
339
		do_action( 'jetpack_sync_save_add_attachment', $attachment_id, $attachment );
340
	}
341
342
	function save_user_handler( $user_id, $old_user_data = null ) {
343
		$user = $this->sanitize_user( get_user_by( 'id', $user_id ) );
344
345
		// Older versions of WP don't pass the old_user_data in ->data
346
		if ( isset( $old_user_data->data ) ) {
347
			$old_user = $old_user_data->data;
348
		} else {
349
			$old_user = $old_user_data;
350
		}
351
352
		if ( $old_user !== null ) {
353
			unset( $old_user->user_pass );
354
			if ( serialize( $old_user ) === serialize( $user->data ) ) {
355
				return;
356
			}
357
		}
358
		do_action( 'jetpack_sync_save_user', $user );
359
	}
360
361
	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...
362
		$user = $this->sanitize_user( get_user_by( 'id', $user_id ) );
363
		do_action( 'jetpack_sync_save_user', $user );
364
	}
365
366
	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...
367
		$user = $this->sanitize_user( get_user_by( 'id', $user_id ) );
368
		if ( $meta_key === $user->cap_key ) {
369
			do_action( 'jetpack_sync_save_user', $user );
370
		}
371
	}
372
373
	public function sanitize_user( $user ) {
374
		unset( $user->data->user_pass );
375
376
		return $user;
377
	}
378
379
380
	function do_sync() {
381
		// don't sync if importing
382
		if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
383
			$this->schedule_sync( "+1 minute" );
384
385
			return false;
386
		}
387
388
		// don't sync if we are throttled
389
		$sync_wait = $this->get_min_sync_wait_time();
390
		$last_sync = $this->get_last_sync_time();
391
392
		if ( $last_sync && $sync_wait && $last_sync + $sync_wait > microtime( true ) ) {
393
			return false;
394
		}
395
396
		$this->set_last_sync_time();
397
		$this->maybe_sync_constants();
398
		$this->maybe_sync_callables();
399
400
		if ( $this->sync_queue->size() === 0 ) {
401
			return false;
402
		}
403
404
		$buffer = $this->sync_queue->checkout_with_memory_limit( $this->checkout_memory_size, $this->upload_max_rows );
405
406
		if ( ! $buffer ) {
407
			// buffer has no items
408
			return false;
409
		}
410
411
		if ( is_wp_error( $buffer ) ) {
412
			// another buffer is currently sending
413
			return false;
414
		}
415
416
		$upload_size   = 0;
417
		$items_to_send = array();
418
419
		// we estimate the total encoded size as we go by encoding each item individually
420
		// this is expensive, but the only way to really know :/
421
		foreach ( $buffer->get_items() as $key => $item ) {
422
423
			// expand item data, e.g. IDs into posts (for full sync)
424
			$item[1] = apply_filters( "jetpack_sync_before_send_" . $item[0], $item[1] );
425
426
			$encoded_item = $this->codec->encode( $item );
427
			$upload_size += strlen( $encoded_item );
428
429
			if ( $upload_size > $this->upload_max_bytes && count( $items_to_send ) > 0 ) {
430
				break;
431
			}
432
433
			$items_to_send[ $key ] = $encoded_item;
434
		}
435
436
		/**
437
		 * Fires when data is ready to send to the server.
438
		 * Return false or WP_Error to abort the sync (e.g. if there's an error)
439
		 * The items will be automatically re-sent later
440
		 *
441
		 * @since 4.1
442
		 *
443
		 * @param array $data The action buffer
444
		 */
445
446
		$result = apply_filters( 'jetpack_sync_client_send_data', $items_to_send );
447
448
		if ( ! $result || is_wp_error( $result ) ) {
449
			// error_log("There was an error sending data:");
450
			// error_log(print_r($result, 1));
451
			$result = $this->sync_queue->checkin( $buffer );
452
453
			if ( is_wp_error( $result ) ) {
454
				error_log( "Error checking in buffer: " . $result->get_error_message() );
455
				$this->sync_queue->force_checkin();
456
			}
457
			// try again in 1 minute
458
			$this->schedule_sync( "+1 minute" );
459
		} else {
460
461
			// scan the sent data to see if a full sync started or finished
462
			if ( $this->buffer_includes_action( $buffer, 'jetpack_full_sync_start' ) ) {
463
				$this->full_sync_client->set_status_sending_started();
464
			}
465
466
			if ( $this->buffer_includes_action( $buffer, 'jetpack_full_sync_end' ) ) {
467
				$this->full_sync_client->set_status_sending_finished();
468
			}
469
470
			$this->sync_queue->close( $buffer, $result );
471
			// check if there are any more events in the buffer
472
			// if so, schedule a cron job to happen soon
473
			if ( $this->sync_queue->has_any_items() ) {
474
				$this->schedule_sync( "+1 minute" );
475
			}
476
		}
477
	}
478
479
	private function buffer_includes_action( $buffer, $action_name ) {
480
		foreach ( $buffer->get_items() as $item ) {
481
			if ( $item[0] === $action_name ) {
482
				return true;
483
			}
484
		}
485
486
		return false;
487
	}
488
489
	function expand_wp_insert_post( $args ) {
490
		// list( $post_ID, $post, $update ) = $args;
491
		return array( $args[0], $this->filter_post_content_and_add_links( $args[1] ), $args[2] );
492
	}
493
494
	// Expands wp_insert_post to include filteredpl
495
	function filter_post_content_and_add_links( $post ) {
496
		if ( 0 < strlen( $post->post_password ) ) {
497
			$post->post_password = 'auto-' . wp_generate_password( 10, false );
498
		}
499
		$post->post_content_filtered = apply_filters( 'the_content', $post->post_content );
500
		$post->permalink             = get_permalink( $post->ID );
501
		$post->shortlink             = wp_get_shortlink( $post->ID );
502
503
		return $post;
504
	}
505
506
	private function schedule_sync( $when ) {
507
		wp_schedule_single_event( strtotime( $when ), 'jetpack_sync_actions' );
508
	}
509
510
	function force_sync_constants() {
511
		delete_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME );
512
		$this->maybe_sync_constants();
513
	}
514
515
	function force_sync_options() {
516
		do_action( 'jetpack_full_sync_options', true );
517
	}
518
519
	function force_sync_network_options() {
520
		do_action( 'jetpack_full_sync_network_options', true );
521
	}
522
523
	private function maybe_sync_constants() {
524
		if ( get_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME ) ) {
525
			return;
526
		}
527
		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...
528
529
		$constants = $this->get_all_constants();
530
		if ( empty( $constants ) ) {
531
			return;
532
		}
533
		do_action( 'jetpack_sync_current_constants', $constants );
534
	}
535
536
	private function get_all_constants() {
537
		return array_combine(
538
			$this->constants_whitelist,
539
			array_map( array( $this, 'get_constant' ), $this->constants_whitelist )
540
		);
541
	}
542
543
	private function get_constant( $constant ) {
544
		if ( defined( $constant ) ) {
545
			return constant( $constant );
546
		}
547
548
		return null;
549
	}
550
551
	public function force_sync_callables() {
552
		foreach ( $this->callable_whitelist as $name => $config ) {
553
			delete_option( self::CALLABLES_CHECKSUM_OPTION_NAME . "_$name" );
554
		}
555
556
		delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME );
557
		$this->maybe_sync_callables();
558
	}
559
560
	private function maybe_sync_callables() {
561
		if ( get_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME ) ) {
562
			return;
563
		}
564
565
		$callables = $this->get_all_callables();
566
		if ( empty( $callables ) ) {
567
			return;
568
		}
569
570
		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...
571
		// only send the callables that have changed
572
		foreach ( $callables as $name => $value ) {
573
			$checksum = $this->get_check_sum( $value );
574
			// explicitly not using Identical comparison as get_option returns a string
575
			if ( $checksum != get_option( self::CALLABLES_CHECKSUM_OPTION_NAME . "_$name" ) ) {
576
				do_action( 'jetpack_sync_current_callable', $name, $value );
577
				update_option( self::CALLABLES_CHECKSUM_OPTION_NAME . "_$name", $checksum );
578
			}
579
		}
580
	}
581
582
	private function get_all_callables() {
583
		return array_combine(
584
			array_keys( $this->callable_whitelist ),
585
			array_map( array( $this, 'get_callable' ), array_values( $this->callable_whitelist ) )
586
		);
587
	}
588
589
	private function get_callable( $callable ) {
590
		return call_user_func( $callable );
591
	}
592
593
	// Is public so that we don't have to store so much data all the options twice.
594
	function get_all_options() {
595
		$options = array();
596
		foreach ( $this->options_whitelist as $option ) {
597
			$options[ $option ] = get_option( $option );
598
		}
599
600
		return $options;
601
	}
602
603
	function get_all_network_options() {
604
		$options = array();
605
		foreach ( $this->network_options_whitelist as $option ) {
606
			$options[ $option ] = get_site_option( $option );
607
		}
608
609
		return $options;
610
	}
611
612
	private function get_check_sum( $values ) {
613
		return crc32( json_encode( $values ) );
614
	}
615
616 View Code Duplication
	function jetpack_sync_core_icon() {
617
		if ( function_exists( 'get_site_icon_url' ) ) {
618
			$url = get_site_icon_url();
619
		} else {
620
			return;
621
		}
622
623
		require_once( JETPACK__PLUGIN_DIR . 'modules/site-icon/site-icon-functions.php' );
624
		// If there's a core icon, maybe update the option.  If not, fall back to Jetpack's.
625
		if ( ! empty( $url ) && $url !== jetpack_site_icon_url() ) {
626
			// This is the option that is synced with dotcom
627
			Jetpack_Options::update_option( 'site_icon_url', $url );
628
		} else if ( empty( $url ) && did_action( 'delete_option_site_icon' ) ) {
629
			Jetpack_Options::delete_option( 'site_icon_url' );
630
		}
631
	}
632
633
	function get_sync_queue() {
634
		return $this->sync_queue;
635
	}
636
637
	function reset_sync_queue() {
638
		$this->sync_queue->reset();
639
	}
640
641
	function set_defaults() {
642
		$this->sync_queue = new Jetpack_Sync_Queue( 'sync' );
643
		$this->set_send_buffer_memory_size( Jetpack_Sync_Defaults::$default_send_buffer_memory_size );
0 ignored issues
show
Bug introduced by
The property default_send_buffer_memory_size 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...
644
		$this->set_upload_max_bytes( Jetpack_Sync_Defaults::$default_upload_max_bytes );
0 ignored issues
show
Bug introduced by
The property default_upload_max_bytes 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...
645
		$this->set_upload_max_rows( Jetpack_Sync_Defaults::$default_upload_max_rows );
0 ignored issues
show
Bug introduced by
The property default_upload_max_rows 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...
646
647
		if ( $this->get_min_sync_wait_time() === false ) {
648
			$this->set_min_sync_wait_time( Jetpack_Sync_Defaults::$default_sync_wait_time );
0 ignored issues
show
Bug introduced by
The property default_sync_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...
649
		}
650
651
		$this->set_full_sync_client( Jetpack_Sync_Full::getInstance() );
652
		$this->codec                     = new Jetpack_Sync_Deflate_Codec();
653
		$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...
654
		$this->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...
655
		$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...
656
		$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...
657
		$this->is_multisite              = is_multisite();
658
659
		// theme mod varies from theme to theme.
660
		$this->options_whitelist[] = 'theme_mods_' . get_option( 'stylesheet' );
661
		if ( $this->is_multisite ) {
662
			$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...
663
		} else {
664
			$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...
665
		}
666
	}
667
668
	function reset_data() {
669
		delete_option( self::CONSTANTS_CHECKSUM_OPTION_NAME );
670
		$this->reset_sync_queue();
671
	}
672
}
673