Completed
Push — add/sync-action ( cda3e4...59c977 )
by
unknown
24:57 queued 24:46
created

Jetpack_Sync_Client::init()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 125
Code Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 64
c 2
b 0
f 0
nc 24
nop 0
dl 0
loc 125
rs 8.1463

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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