Completed
Push — branch-4.2 ( b502ce...f4f1fc )
by
unknown
57:56 queued 47:17
created

Jetpack_Sync_Actions::sync_allowed()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 4
eloc 3
c 3
b 0
f 1
nc 4
nop 0
dl 0
loc 4
rs 9.2
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
		// Add a custom "every minute" cron schedule
16
		add_filter( 'cron_schedules', array( __CLASS__, 'minute_cron_schedule' ) );
17
18
		// On jetpack authorization, schedule a full sync
19
		add_action( 'jetpack_client_authorized', array( __CLASS__, 'schedule_full_sync' ) );
20
		
21
		// When imports are finished, schedule a full sync
22
		add_action( 'import_end', array( __CLASS__, 'schedule_full_sync' ) );
23
24
		// Sync connected user role changes to .com
25
		require_once dirname( __FILE__ ) . '/class.jetpack-sync-users.php';
26
27
		// everything below this point should only happen if we're a valid sync site
28
		if ( ! self::sync_allowed() ) {
29
			return;
30
		}
31
32
		// cron hooks
33
		add_action( 'jetpack_sync_send_db_checksum', array( __CLASS__, 'send_db_checksum' ) );
34
		add_action( 'jetpack_sync_full', array( __CLASS__, 'do_full_sync' ), 10, 1 );
35
		add_action( 'jetpack_sync_cron', array( __CLASS__, 'do_cron_sync' ) );
36
		add_action( 'jetpack_sync_send_pending_data', array( __CLASS__, 'do_send_pending_data' ) );
37
38
		if ( ! wp_next_scheduled( 'jetpack_sync_send_db_checksum' ) ) {
39
			// Schedule a job to send DB checksums once an hour
40
			wp_schedule_event( time(), 'hourly', 'jetpack_sync_send_db_checksum' );
41
		}
42
43
		if ( ! wp_next_scheduled( 'jetpack_sync_cron' ) ) {
44
			// Schedule a job to send pending queue items once a minute
45
			wp_schedule_event( time(), '1min', 'jetpack_sync_cron' );
46
		}
47
48
		/**
49
		 * Fires on every request before default loading sync listener code.
50
		 * Return false to not load sync listener code that monitors common
51
		 * WP actions to be serialized.
52
		 *
53
		 * By default this returns true for non-GET-requests, or requests where the
54
		 * user is logged-in.
55
		 *
56
		 * @since 4.2.0
57
		 *
58
		 * @param bool should we load sync listener code for this request
59
		 */
60
		if ( apply_filters( 'jetpack_sync_listener_should_load',
61
			(
62
				'GET' !== $_SERVER['REQUEST_METHOD']
63
				||
64
				is_user_logged_in()
65
				||
66
				defined( 'PHPUNIT_JETPACK_TESTSUITE' )
67
			)
68
		) ) {
69
			self::initialize_listener();
70
		}
71
72
		/**
73
		 * Fires on every request before default loading sync sender code.
74
		 * Return false to not load sync sender code that serializes pending
75
		 * data and sends it to WPCOM for processing.
76
		 *
77
		 * By default this returns true for POST requests, admin requests, or requests
78
		 * by users who can manage_options.
79
		 *
80
		 * @since 4.2.0
81
		 *
82
		 * @param bool should we load sync sender code for this request
83
		 */
84
		if ( apply_filters( 'jetpack_sync_sender_should_load',
85
			(
86
				'POST' === $_SERVER['REQUEST_METHOD']
87
				||
88
				current_user_can( 'manage_options' )
89
				||
90
				is_admin()
91
				||
92
				defined( 'PHPUNIT_JETPACK_TESTSUITE' )
93
			)
94
		) ) {
95
			self::initialize_sender();
96
			add_action( 'shutdown', array( self::$sender, 'do_sync' ) );
97
		}
98
99
	}
100
101
	static function sync_allowed() {
102
		return ( Jetpack::is_active() && ! ( Jetpack::is_development_mode() || Jetpack::is_staging_site() ) )
103
			   || defined( 'PHPUNIT_JETPACK_TESTSUITE' );
104
	}
105
106
	static function send_data( $data, $codec_name, $sent_timestamp, $queue_id ) {
107
		Jetpack::load_xml_rpc_client();
108
109
		$url = add_query_arg( array(
110
			'sync'      => '1', // add an extra parameter to the URL so we can tell it's a sync action
111
			'codec'     => $codec_name, // send the name of the codec used to encode the data
112
			'timestamp' => $sent_timestamp, // send current server time so we can compensate for clock differences
113
			'queue'     => $queue_id, // sync or full_sync
114
		), Jetpack::xmlrpc_api_url() );
115
116
		$rpc = new Jetpack_IXR_Client( array(
117
			'url'     => $url,
118
			'user_id' => JETPACK_MASTER_USER,
119
			'timeout' => 30,
120
		) );
121
122
		$result = $rpc->query( 'jetpack.syncActions', $data );
123
124
		if ( ! $result ) {
125
			return $rpc->get_jetpack_error();
126
		}
127
128
		return $rpc->getResponse();
129
	}
130
131
	static function schedule_initial_sync() {
132
		// we need this function call here because we have to run this function 
133
		// reeeeally early in init, before WP_CRON_LOCK_TIMEOUT is defined.
134
		wp_functionality_constants();
135
		self::schedule_full_sync( array( 'options' => true, 'network_options' => true, 'functions' => true, 'constants' => true, 'users' => true ) );
136
	}
137
138
	static function schedule_full_sync( $modules = null ) {
139
		if ( $modules ) {
140
			wp_schedule_single_event( time() + 1, 'jetpack_sync_full', array( $modules ) );
141
		} else {
142
			wp_schedule_single_event( time() + 1, 'jetpack_sync_full' );
143
		}
144
145
		spawn_cron();
146
	}
147
148
	static function is_scheduled_full_sync( $modules = null ) {
149
		if ( is_null( $modules ) ) {
150
			$crons = _get_cron_array();
151
			if ( empty( $crons ) ) {
152
				return false;
153
			}
154
			$result = array();
155
			foreach ( $crons as $timestamp => $cron ) {
156
				if ( ! empty( $cron['jetpack_sync_full'] ) ) {
157
					$result[ $timestamp ] = $cron['jetpack_sync_full'];
158
				}
159
			}
160
			return $result;
161
		}
162
		return wp_next_scheduled( 'jetpack_sync_full', $modules );
163
	}
164
165
	static function do_full_sync( $modules = null ) {
166
		if ( ! self::sync_allowed() ) {
167
			return;
168
		}
169
170
		self::initialize_listener();
171
		Jetpack_Sync_Modules::get_module( 'full-sync' )->start( $modules );
172
		self::do_send_pending_data(); // try to send at least some of the data
173
	}
174
175
	static function minute_cron_schedule( $schedules ) {
176
		if( ! isset( $schedules["1min"] ) ) {
177
			$schedules["1min"] = array(
178
				'interval' => 60,
179
				'display' => __( 'Every minute' ) 
180
			);
181
		}
182
		return $schedules;
183
	}
184
185
	// try to send actions until we run out of things to send,
186
	// or have to wait more than 15s before sending again,
187
	// or we hit a lock or some other sending issue
188
	static function do_cron_sync() {
189
		if ( ! self::sync_allowed() ) {
190
			return;
191
		}
192
193
		self::initialize_sender();
194
		
195
		do {
196
			$next_sync_time = self::$sender->get_next_sync_time();
197
			
198
			if ( $next_sync_time ) {
199
				$delay = $next_sync_time - time() + 1;
200
				if ( $delay > 15 ) {
201
					break;
202
				} elseif ( $delay > 0 ) {
203
					sleep( $delay );
204
				}
205
			}
206
207
			$result = self::$sender->do_sync();
208
		} while ( $result );
209
	}
210
211
	static function do_send_pending_data() {
212
		self::initialize_sender();
213
		self::$sender->do_sync();
214
	}
215
216
	static function send_db_checksum() {
217
		self::initialize_listener();
218
		self::initialize_sender();
219
		self::$sender->send_checksum();
220
		self::$sender->do_sync();
221
	}
222
223
	static function initialize_listener() {
224
		require_once dirname( __FILE__ ) . '/class.jetpack-sync-listener.php';
225
		self::$listener = Jetpack_Sync_Listener::get_instance();
226
	}
227
228
	static function initialize_sender() {
229
		require_once dirname( __FILE__ ) . '/class.jetpack-sync-sender.php';
230
		self::$sender = Jetpack_Sync_Sender::get_instance();
231
232
		// bind the sending process
233
		add_filter( 'jetpack_sync_send_data', array( __CLASS__, 'send_data' ), 10, 4 );
234
	}
235
}
236
237
// Allow other plugins to add filters before we initialize the actions.
238
// Load the listeners if before modules get loaded so that we can capture version changes etc.
239
add_action( 'plugins_loaded', array( 'Jetpack_Sync_Actions', 'init' ), 90 );
240
241
// We need to define this here so that it's hooked before `updating_jetpack_version` is called
242
add_action( 'updating_jetpack_version', array( 'Jetpack_Sync_Actions', 'schedule_initial_sync' ), 10 );
243