Completed
Push — fix/full-sync-on-connect ( 1def8c...89b97c )
by
unknown
182:36 queued 173:48
created

Jetpack_Sync_Actions   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 13
Bugs 3 Features 0
Metric Value
c 13
b 3
f 0
dl 0
loc 141
rs 10
wmc 24
lcom 2
cbo 5

8 Methods

Rating   Name   Duplication   Size   Complexity  
C init() 0 77 16
A send_data() 0 23 2
A schedule_full_sync() 0 3 1
A do_full_sync() 0 5 1
A do_send_pending_data() 0 4 1
A send_db_checksum() 0 6 1
A initialize_listener() 0 4 1
A initialize_sender() 0 7 1
1
<?php
2
3
/**
4
 * The role of this class is to hook the Sync subsystem into WordPress - when to listen for actions,
5
 * when to send, when to perform a full sync, etc.
6
 *
7
 * It also binds the action to send data to WPCOM to Jetpack's XMLRPC client object.
8
 */
9
class Jetpack_Sync_Actions {
10
	static $sender = null;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $sender.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
11
	static $listener = null;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $listener.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
12
13
	static function init() {
14
		
15
		// On jetpack authorization, schedule a full sync
16
		add_action( 'jetpack_client_authorized', array( __CLASS__, 'schedule_full_sync' ) );
17
18
		// cron hooks
19
		add_action( 'jetpack_sync_send_db_checksum', array( __CLASS__, 'send_db_checksum' ) );
20
		add_action( 'jetpack_sync_full', array( __CLASS__, 'do_full_sync' ) );
21
		add_action( 'jetpack_sync_send_pending_data', array( __CLASS__, 'do_send_pending_data' ) );
22
23
		$prevent_sync_loading = 
24
				( !Jetpack::is_active() || Jetpack::is_development_mode() || Jetpack::is_staging_site() ) 
25
			&& 
26
				!defined( 'PHPUNIT_JETPACK_TESTSUITE' );
27
28
		if ( ! $prevent_sync_loading && ! wp_next_scheduled ( 'jetpack_sync_send_db_checksum' ) ) {
29
			// Schedule a job to send DB checksums once an hour
30
			wp_schedule_event( time(), 'hourly', 'jetpack_sync_send_db_checksum' );
31
		}
32
33
		/**
34
		 * Fires on every request before default loading sync listener code.
35
		 * Return false to not load sync listener code that monitors common
36
		 * WP actions to be serialized.
37
		 * 
38
		 * By default this returns true for non-GET-requests, or requests where the 
39
		 * user is logged-in.
40
		 *
41
		 * @since 4.2.0
42
		 *
43
		 * @param bool should we load sync listener code for this request
44
		 */
45
		if ( !$prevent_sync_loading && apply_filters( 'jetpack_sync_listener_should_load',
46
				(
47
					$_SERVER['REQUEST_METHOD'] !== 'GET'
48
				||
49
					is_user_logged_in()
50
				||
51
					defined( 'PHPUNIT_JETPACK_TESTSUITE' )
52
				||
53
					is_admin()
54
				)
55
			) ) {
56
			self::initialize_listener();
57
		}
58
59
		// Sync connected user role changes to .com
60
		require_once dirname( __FILE__ ) . '/class.jetpack-sync-users.php';
61
62
		/**
63
		 * Fires on every request before default loading sync sender code.
64
		 * Return false to not load sync sender code that serializes pending
65
		 * data and sends it to WPCOM for processing.
66
		 *
67
		 * By default this returns true for POST requests, admin requests, or requests
68
		 * by users who can manage_options.
69
		 *
70
		 * @since 4.2.0
71
		 *
72
		 * @param bool should we load sync sender code for this request
73
		 */
74
		if ( !$prevent_sync_loading && apply_filters( 'jetpack_sync_sender_should_load',
75
			(
76
				$_SERVER['REQUEST_METHOD'] === 'POST'
77
			||
78
				current_user_can( 'manage_options' )
79
			||
80
				is_admin()
81
			||
82
				defined( 'PHPUNIT_JETPACK_TESTSUITE' )
83
			)
84
			) ) {
85
			self::initialize_sender();
86
			add_action( 'shutdown', array( self::$sender, 'do_sync' ) );
87
		}
88
89
	}
90
91
	static function send_data( $data, $codec_name, $sent_timestamp ) {
92
		Jetpack::load_xml_rpc_client();
93
94
		$url = add_query_arg( array(
95
			'sync' => '1', // add an extra parameter to the URL so we can tell it's a sync action
96
			'codec' => $codec_name, // send the name of the codec used to encode the data
97
			'timestamp' => $sent_timestamp, // send current server time so we can compensate for clock differences
98
		), Jetpack::xmlrpc_api_url() );
99
100
		$rpc = new Jetpack_IXR_Client( array(
101
			'url'     => $url,
102
			'user_id' => JETPACK_MASTER_USER,
103
			'timeout' => 30
104
		) );
105
106
		$result = $rpc->query( 'jetpack.syncActions', $data );
107
108
		if ( ! $result ) {
109
			return $rpc->get_jetpack_error();
110
		}
111
112
		return $rpc->getResponse();
113
	}
114
115
	static function schedule_full_sync() {
116
		wp_schedule_single_event( time() + 1, 'jetpack_sync_full' );
117
	}
118
119
	static function do_full_sync() {
120
		self::initialize_listener();
121
		Jetpack_Sync_Modules::get_module( 'full-sync' )->start();
122
		self::do_send_pending_data(); // try to send at least some of the data
123
	}
124
125
	static function do_send_pending_data() {
126
		self::initialize_sender();
127
		self::$sender->do_sync();
128
	}
129
130
	static function send_db_checksum() {
131
		self::initialize_listener();
132
		self::initialize_sender();
133
		self::$sender->send_checksum();
134
		self::$sender->do_sync();
135
	}
136
137
	static function initialize_listener() {
138
		require_once dirname( __FILE__ ) . '/class.jetpack-sync-listener.php';
139
		self::$listener = Jetpack_Sync_Listener::getInstance();
140
	}
141
142
	static function initialize_sender() {
143
		require_once dirname( __FILE__ ) . '/class.jetpack-sync-sender.php';
144
		self::$sender = Jetpack_Sync_Sender::getInstance();
145
		
146
		// bind the sending process
147
		add_filter( 'jetpack_sync_send_data', array( __CLASS__, 'send_data' ), 10, 3 );
148
	}
149
}
150
151
// Allow other plugins to add filters before we initalize the actions.
152
add_action( 'init', array( 'Jetpack_Sync_Actions', 'init' ), 11, 0 );
153