Completed
Push — add/sso-analytics ( 683a91...e67d7d )
by
unknown
162:06 queued 152:34
created

Jetpack_Sync_Client::action_handler()   C

Complexity

Conditions 12
Paths 6

Size

Total Lines 43
Code Lines 24

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 12
eloc 24
nc 6
nop 0
dl 0
loc 43
rs 5.1612

How to fix   Complexity   

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
		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
318
		/**
319
		 * Fires when the client needs to sync theme support info
320
		 * Only sends theme support attributes whitelisted in Jetpack_Sync_Defaults::$default_theme_support_whitelist
321
		 * 
322
		 * @since 4.1.0
323
		 *
324
		 * @param object the theme support hash
325
		 */
326
		do_action( 'jetpack_sync_current_theme_support', $theme_support );
327
	}
328
329
	function send_wp_version( $update, $meta_data ) {
330
		if ( 'update' === $meta_data['action'] && 'core' === $meta_data['type'] ) {
331
			global $wp_version;
332
333
			/**
334
			 * Fires when the client needs to sync WordPress version
335
			 * 
336
			 * @since 4.1.0
337
			 *
338
			 * @param string The WordPress version number
339
			 */
340
			do_action( 'jetpack_sync_wp_version', $wp_version );
341
		}
342
	}
343
344
	function save_term_handler( $term_id, $tt_id, $taxonomy ) {
345
		if ( class_exists( 'WP_Term' ) ) {
346
			$term_object = WP_Term::get_instance( $term_id, $taxonomy );
347
		} else {
348
			$term_object = get_term_by( 'id', $term_id, $taxonomy );
349
		}
350
351
		/**
352
		 * Fires when the client needs to sync a new term
353
		 * 
354
		 * @since 4.1.0
355
		 *
356
		 * @param object the Term object
357
		 */
358
		do_action( 'jetpack_sync_save_term', $term_object );
359
	}
360
361
	function send_attachment_info( $attachment_id ) {
362
		$attachment = get_post( $attachment_id );
363
364
		/**
365
		 * Fires when the client needs to sync an attachment for a post
366
		 * 
367
		 * @since 4.1.0
368
		 *
369
		 * @param int The attachment ID
370
		 * @param object The attachment
371
		 */
372
		do_action( 'jetpack_sync_save_add_attachment', $attachment_id, $attachment );
373
	}
374
375
	function save_user_handler( $user_id, $old_user_data = null ) {
376
		$user = $this->sanitize_user( get_user_by( 'id', $user_id ) );
377
378
		// Older versions of WP don't pass the old_user_data in ->data
379
		if ( isset( $old_user_data->data ) ) {
380
			$old_user = $old_user_data->data;
381
		} else {
382
			$old_user = $old_user_data;
383
		}
384
385
		if ( $old_user !== null ) {
386
			unset( $old_user->user_pass );
387
			if ( serialize( $old_user ) === serialize( $user->data ) ) {
388
				return;
389
			}
390
		}
391
392
		/**
393
		 * Fires when the client needs to sync an updated user
394
		 * 
395
		 * @since 4.1.0
396
		 *
397
		 * @param object The WP_User object
398
		 */
399
		do_action( 'jetpack_sync_save_user', $user );
400
	}
401
402
	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...
403
		$user = $this->sanitize_user( get_user_by( 'id', $user_id ) );
404
405
		/**
406
		 * Fires when the client needs to sync an updated user
407
		 * 
408
		 * @since 4.1.0
409
		 *
410
		 * @param object The WP_User object
411
		 */
412
		do_action( 'jetpack_sync_save_user', $user );
413
	}
414
415
	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...
416
		$user = $this->sanitize_user( get_user_by( 'id', $user_id ) );
417
		if ( $meta_key === $user->cap_key ) {
418
			/**
419
			 * Fires when the client needs to sync an updated user
420
			 * 
421
			 * @since 4.1.0
422
			 *
423
			 * @param object The WP_User object
424
			 */
425
			do_action( 'jetpack_sync_save_user', $user );
426
		}
427
	}
428
429
	public function sanitize_user( $user ) {
430
		unset( $user->data->user_pass );
431
432
		return $user;
433
	}
434
435
436
	function do_sync() {
437
		// don't sync if importing
438
		if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
439
			$this->schedule_sync( "+1 minute" );
440
441
			return false;
442
		}
443
444
		// don't sync if we are throttled
445
		$sync_wait = $this->get_min_sync_wait_time();
446
		$last_sync = $this->get_last_sync_time();
447
448
		if ( $last_sync && $sync_wait && $last_sync + $sync_wait > microtime( true ) ) {
449
			return false;
450
		}
451
452
		$this->set_last_sync_time();
453
		$this->maybe_sync_constants();
454
		$this->maybe_sync_callables();
455
456
		if ( $this->sync_queue->size() === 0 ) {
457
			return false;
458
		}
459
460
		$buffer = $this->sync_queue->checkout_with_memory_limit( $this->checkout_memory_size, $this->upload_max_rows );
461
462
		if ( ! $buffer ) {
463
			// buffer has no items
464
			return false;
465
		}
466
467
		if ( is_wp_error( $buffer ) ) {
468
			// another buffer is currently sending
469
			return false;
470
		}
471
472
		$upload_size   = 0;
473
		$items_to_send = array();
474
475
		// we estimate the total encoded size as we go by encoding each item individually
476
		// this is expensive, but the only way to really know :/
477
		foreach ( $buffer->get_items() as $key => $item ) {
478
479
			/**
480
			 * Modify the data within an action before it is serialized and sent to the server
481
			 * For example, during full sync this expands Post ID's into full Post objects,
482
			 * so that we don't have to serialize the whole object into the queue.
483
			 * 
484
			 * @since 4.1.0
485
			 *
486
			 * @param array The action parameters
487
			 */
488
			$item[1] = apply_filters( "jetpack_sync_before_send_" . $item[0], $item[1] );
489
490
			$encoded_item = $this->codec->encode( $item );
491
			$upload_size += strlen( $encoded_item );
492
493
			if ( $upload_size > $this->upload_max_bytes && count( $items_to_send ) > 0 ) {
494
				break;
495
			}
496
497
			$items_to_send[ $key ] = $encoded_item;
498
		}
499
500
		/**
501
		 * Fires when data is ready to send to the server.
502
		 * Return false or WP_Error to abort the sync (e.g. if there's an error)
503
		 * The items will be automatically re-sent later
504
		 *
505
		 * @since 4.1
506
		 *
507
		 * @param array $data The action buffer
508
		 */
509
		$result = apply_filters( 'jetpack_sync_client_send_data', $items_to_send );
510
511
		if ( ! $result || is_wp_error( $result ) ) {
512
			// error_log("There was an error sending data:");
513
			// error_log(print_r($result, 1));
514
			$result = $this->sync_queue->checkin( $buffer );
515
516
			if ( is_wp_error( $result ) ) {
517
				error_log( "Error checking in buffer: " . $result->get_error_message() );
518
				$this->sync_queue->force_checkin();
519
			}
520
			// try again in 1 minute
521
			$this->schedule_sync( "+1 minute" );
522
		} else {
523
524
			// scan the sent data to see if a full sync started or finished
525
			if ( $this->buffer_includes_action( $buffer, 'jetpack_full_sync_start' ) ) {
526
				$this->full_sync_client->set_status_sending_started();
527
			}
528
529
			if ( $this->buffer_includes_action( $buffer, 'jetpack_full_sync_end' ) ) {
530
				$this->full_sync_client->set_status_sending_finished();
531
			}
532
533
			$this->sync_queue->close( $buffer, $result );
534
			// check if there are any more events in the buffer
535
			// if so, schedule a cron job to happen soon
536
			if ( $this->sync_queue->has_any_items() ) {
537
				$this->schedule_sync( "+1 minute" );
538
			}
539
		}
540
	}
541
542
	private function buffer_includes_action( $buffer, $action_name ) {
543
		foreach ( $buffer->get_items() as $item ) {
544
			if ( $item[0] === $action_name ) {
545
				return true;
546
			}
547
		}
548
549
		return false;
550
	}
551
552
	function expand_wp_insert_post( $args ) {
553
		// list( $post_ID, $post, $update ) = $args;
554
		return array( $args[0], $this->filter_post_content_and_add_links( $args[1] ), $args[2] );
555
	}
556
557
	// Expands wp_insert_post to include filtered content
558
	function filter_post_content_and_add_links( $post ) {
559
		if ( 0 < strlen( $post->post_password ) ) {
560
			$post->post_password = 'auto-' . wp_generate_password( 10, false );
561
		}
562
		/** This filter is already documented in core. wp-includes/post-template.php */
563
		$post->post_content_filtered = apply_filters( 'the_content', $post->post_content );
564
		$post->permalink             = get_permalink( $post->ID );
565
		$post->shortlink             = wp_get_shortlink( $post->ID );
566
567
		return $post;
568
	}
569
570
	private function schedule_sync( $when ) {
571
		wp_schedule_single_event( strtotime( $when ), 'jetpack_sync_actions' );
572
	}
573
574
	function force_sync_constants() {
575
		delete_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME );
576
		$this->maybe_sync_constants();
577
	}
578
579
	function force_sync_options() {
580
		/**
581
		 * Tells the client to sync all options to the server
582
		 *
583
		 * @since 4.1
584
		 *
585
		 * @param boolean Whether to expand options (should always be true)
586
		 */
587
		do_action( 'jetpack_full_sync_options', true );
588
	}
589
590
	function force_sync_network_options() {
591
		/**
592
		 * Tells the client to sync all network options to the server
593
		 *
594
		 * @since 4.1
595
		 *
596
		 * @param boolean Whether to expand options (should always be true)
597
		 */
598
		do_action( 'jetpack_full_sync_network_options', true );
599
	}
600
601
	private function maybe_sync_constants() {
602
		if ( get_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME ) ) {
603
			return;
604
		}
605
		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...
606
607
		$constants = $this->get_all_constants();
608
		if ( empty( $constants ) ) {
609
			return;
610
		}
611
612
		/**
613
		 * Tells the client to sync all constants to the server
614
		 *
615
		 * @since 4.1
616
		 *
617
		 * @param array All the constants
618
		 */
619
		do_action( 'jetpack_sync_current_constants', $constants );
620
	}
621
622
	private function get_all_constants() {
623
		return array_combine(
624
			$this->constants_whitelist,
625
			array_map( array( $this, 'get_constant' ), $this->constants_whitelist )
626
		);
627
	}
628
629
	private function get_constant( $constant ) {
630
		if ( defined( $constant ) ) {
631
			return constant( $constant );
632
		}
633
634
		return null;
635
	}
636
637
	public function force_sync_callables() {
638
		foreach ( $this->callable_whitelist as $name => $config ) {
639
			delete_option( self::CALLABLES_CHECKSUM_OPTION_NAME . "_$name" );
640
		}
641
642
		delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME );
643
		$this->maybe_sync_callables();
644
	}
645
646
	private function maybe_sync_callables() {
647
		if ( get_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME ) ) {
648
			return;
649
		}
650
651
		$callables = $this->get_all_callables();
652
		if ( empty( $callables ) ) {
653
			return;
654
		}
655
656
		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...
657
		// only send the callables that have changed
658
		foreach ( $callables as $name => $value ) {
659
			$checksum = $this->get_check_sum( $value );
660
			// explicitly not using Identical comparison as get_option returns a string
661
			if ( $checksum != get_option( self::CALLABLES_CHECKSUM_OPTION_NAME . "_$name" ) ) {
662
				/**
663
				 * Tells the client to sync a callable (aka function) to the server
664
				 *
665
				 * @since 4.1
666
				 *
667
				 * @param string The name of the callable
668
				 * @param mixed The value of the callable
669
				 */
670
				do_action( 'jetpack_sync_current_callable', $name, $value );
671
				update_option( self::CALLABLES_CHECKSUM_OPTION_NAME . "_$name", $checksum );
672
			}
673
		}
674
	}
675
676
	private function get_all_callables() {
677
		return array_combine(
678
			array_keys( $this->callable_whitelist ),
679
			array_map( array( $this, 'get_callable' ), array_values( $this->callable_whitelist ) )
680
		);
681
	}
682
683
	private function get_callable( $callable ) {
684
		return call_user_func( $callable );
685
	}
686
687
	// Is public so that we don't have to store so much data all the options twice.
688
	function get_all_options() {
689
		$options = array();
690
		foreach ( $this->options_whitelist as $option ) {
691
			$options[ $option ] = get_option( $option );
692
		}
693
694
		return $options;
695
	}
696
697
	function get_all_network_options() {
698
		$options = array();
699
		foreach ( $this->network_options_whitelist as $option ) {
700
			$options[ $option ] = get_site_option( $option );
701
		}
702
703
		return $options;
704
	}
705
706
	private function get_check_sum( $values ) {
707
		return crc32( json_encode( $values ) );
708
	}
709
710 View Code Duplication
	function jetpack_sync_core_icon() {
711
		if ( function_exists( 'get_site_icon_url' ) ) {
712
			$url = get_site_icon_url();
713
		} else {
714
			return;
715
		}
716
717
		require_once( JETPACK__PLUGIN_DIR . 'modules/site-icon/site-icon-functions.php' );
718
		// If there's a core icon, maybe update the option.  If not, fall back to Jetpack's.
719
		if ( ! empty( $url ) && $url !== jetpack_site_icon_url() ) {
720
			// This is the option that is synced with dotcom
721
			Jetpack_Options::update_option( 'site_icon_url', $url );
722
		} else if ( empty( $url ) && did_action( 'delete_option_site_icon' ) ) {
723
			Jetpack_Options::delete_option( 'site_icon_url' );
724
		}
725
	}
726
727
	function get_sync_queue() {
728
		return $this->sync_queue;
729
	}
730
731
	function reset_sync_queue() {
732
		$this->sync_queue->reset();
733
	}
734
735
	function set_defaults() {
736
		$this->sync_queue = new Jetpack_Sync_Queue( 'sync' );
737
		$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...
738
		$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...
739
		$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...
740
741
		if ( $this->get_min_sync_wait_time() === false ) {
742
			$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...
743
		}
744
745
		$this->set_full_sync_client( Jetpack_Sync_Full::getInstance() );
746
		$this->codec                     = new Jetpack_Sync_Deflate_Codec();
747
		$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...
748
		$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...
749
		$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...
750
		$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...
751
		$this->is_multisite              = is_multisite();
752
753
		// theme mod varies from theme to theme.
754
		$this->options_whitelist[] = 'theme_mods_' . get_option( 'stylesheet' );
755
		if ( $this->is_multisite ) {
756
			$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...
757
		} else {
758
			$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...
759
		}
760
	}
761
762
	function reset_data() {
763
		delete_option( self::CONSTANTS_CHECKSUM_OPTION_NAME );
764
		$this->reset_sync_queue();
765
	}
766
}
767