Completed
Push — fix/save-full-sync-status-duri... ( 2077fb...c9fff9 )
by
unknown
54:23 queued 45:09
created

Jetpack_Sync_Actions::maybe_schedule_sync_cron()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 4
nop 2
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
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
	const INITIAL_SYNC_MULTISITE_INTERVAL = 10;
13
	const DEFAULT_SYNC_CRON_INTERVAL = '1min';
14
15
	static function init() {
16
17
		// Add a custom "every minute" cron schedule
18
		add_filter( 'cron_schedules', array( __CLASS__, 'minute_cron_schedule' ) );
19
20
		// On jetpack authorization, schedule a full sync
21
		add_action( 'jetpack_client_authorized', array( __CLASS__, 'do_full_sync' ), 10, 0 );
22
23
		// When importing via cron, do not sync
24
		add_action( 'wp_cron_importer_hook', array( __CLASS__, 'set_is_importing_true' ), 1 );
25
26
		// Sync connected user role changes to .com
27
		require_once dirname( __FILE__ ) . '/class.jetpack-sync-users.php';
28
29
		// everything below this point should only happen if we're a valid sync site
30
		if ( ! self::sync_allowed() ) {
31
			return;
32
		}
33
34
		// publicize filter to prevent publicizing blacklisted post types
35
		add_filter( 'publicize_should_publicize_published_post', array( __CLASS__, 'prevent_publicize_blacklisted_posts' ), 10, 2 );
36
37
		// cron hooks
38
		add_action( 'jetpack_sync_full', array( __CLASS__, 'do_full_sync' ), 10, 1 );
39
		add_action( 'jetpack_sync_cron', array( __CLASS__, 'do_cron_sync' ) );
40
		add_action( 'jetpack_sync_full_cron', array( __CLASS__, 'do_cron_full_sync' ) );
41
42
		self::init_sync_cron_jobs();
43
44
		/**
45
		 * Fires on every request before default loading sync listener code.
46
		 * Return false to not load sync listener code that monitors common
47
		 * WP actions to be serialized.
48
		 *
49
		 * By default this returns true for cron jobs, non-GET-requests, or requests where the
50
		 * user is logged-in.
51
		 *
52
		 * @since 4.2.0
53
		 *
54
		 * @param bool should we load sync listener code for this request
55
		 */
56
		if ( apply_filters( 'jetpack_sync_listener_should_load',
57
			(
58
				( isset( $_SERVER["REQUEST_METHOD"] ) && 'GET' !== $_SERVER['REQUEST_METHOD'] )
59
				||
60
				is_user_logged_in()
61
				||
62
				defined( 'PHPUNIT_JETPACK_TESTSUITE' )
63
				||
64
				defined( 'DOING_CRON' ) && DOING_CRON
65
			)
66
		) ) {
67
			self::initialize_listener();
68
		}
69
70
		/**
71
		 * Fires on every request before default loading sync sender code.
72
		 * Return false to not load sync sender code that serializes pending
73
		 * data and sends it to WPCOM for processing.
74
		 *
75
		 * By default this returns true for cron jobs, POST requests, admin requests, or requests
76
		 * by users who can manage_options.
77
		 *
78
		 * @since 4.2.0
79
		 *
80
		 * @param bool should we load sync sender code for this request
81
		 */
82
		if ( apply_filters( 'jetpack_sync_sender_should_load',
83
			(
84
				( isset( $_SERVER["REQUEST_METHOD"] ) && 'POST' === $_SERVER['REQUEST_METHOD'] )
85
				||
86
				current_user_can( 'manage_options' )
87
				||
88
				is_admin()
89
				||
90
				defined( 'PHPUNIT_JETPACK_TESTSUITE' )
91
				||
92
				defined( 'DOING_CRON' ) && DOING_CRON
93
			)
94
		) ) {
95
			self::initialize_sender();
96
			add_action( 'shutdown', array( self::$sender, 'do_sync' ) );
97
			add_action( 'shutdown', array( self::$sender, 'do_full_sync' ) );
98
		}
99
100
	}
101
102
	static function sync_allowed() {
103
		require_once dirname( __FILE__ ) . '/class.jetpack-sync-settings.php';
104
		return ( ! Jetpack_Sync_Settings::get_setting( 'disable' ) && Jetpack::is_active() && ! ( Jetpack::is_development_mode() || Jetpack::is_staging_site() ) )
105
			   || defined( 'PHPUNIT_JETPACK_TESTSUITE' );
106
	}
107
108
	static function prevent_publicize_blacklisted_posts( $should_publicize, $post ) {
109
		require_once dirname( __FILE__ ) . '/class.jetpack-sync-settings.php';
110
		if ( in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) ) ) {
111
			return false;
112
		}
113
114
		return $should_publicize;
115
	}
116
117
	static function set_is_importing_true() {
118
		require_once dirname( __FILE__ ) . '/class.jetpack-sync-settings.php';
119
		Jetpack_Sync_Settings::set_importing( true );
120
	}
121
122
	static function send_data( $data, $codec_name, $sent_timestamp, $queue_id ) {
123
		Jetpack::load_xml_rpc_client();
124
125
		$query_args = array(
126
			'sync'      => '1',             // add an extra parameter to the URL so we can tell it's a sync action
127
			'codec'     => $codec_name,     // send the name of the codec used to encode the data
128
			'timestamp' => $sent_timestamp, // send current server time so we can compensate for clock differences
129
			'queue'     => $queue_id,       // sync or full_sync
130
		);
131
132
		if ( Jetpack::sync_idc_optin() ) {
133
			$query_args['home']    = get_home_url(); // Send home url option to check for Identity Crisis server-side
134
			$query_args['siteurl'] = get_site_url(); // Send home url option to check for Identity Crisis server-side
135
		}
136
137
		$url = add_query_arg( $query_args, Jetpack::xmlrpc_api_url() );
138
139
		$rpc = new Jetpack_IXR_Client( array(
140
			'url'     => $url,
141
			'user_id' => JETPACK_MASTER_USER,
142
			'timeout' => 30,
143
		) );
144
145
		$result = $rpc->query( 'jetpack.syncActions', $data );
146
147
		if ( ! $result ) {
148
			$error = $rpc->get_jetpack_error();
149
			if ( 'jetpack_url_mismatch' === $error->get_error_code() ) {
150
				Jetpack_Options::update_option( 'sync_error_idc', get_home_url() );
151
			}
152
			
153
			return $error;
154
		}
155
156
		return $rpc->getResponse();
157
	}
158
159
	static function do_initial_sync( $new_version = null, $old_version = null ) {
160
		$initial_sync_config = array(
161
			'options' => true,
162
			'network_options' => true,
163
			'functions' => true,
164
			'constants' => true,
165
		);
166
167
		if ( $old_version && ( version_compare( $old_version, '4.2', '<' ) ) ) {
168
			$initial_sync_config['users'] = 'initial';
169
		}
170
171
		self::do_full_sync( $initial_sync_config );
172
	}
173
174
	static function do_full_sync( $modules = null ) {
175
		if ( ! self::sync_allowed() ) {
176
			return false;
177
		}
178
179
		self::initialize_listener();
180
		Jetpack_Sync_Modules::get_module( 'full-sync' )->start( $modules );
181
		self::do_cron_full_sync(); // immediately run a cron full sync, which sends pending data
182
183
		return true;
184
	}
185
186
	static function minute_cron_schedule( $schedules ) {
187
		if( ! isset( $schedules["1min"] ) ) {
188
			$schedules["1min"] = array(
189
				'interval' => 60,
190
				'display' => __( 'Every minute' )
191
			);
192
		}
193
		return $schedules;
194
	}
195
196
	// try to send actions until we run out of things to send,
197
	// or have to wait more than 15s before sending again,
198
	// or we hit a lock or some other sending issue
199 View Code Duplication
	static function do_cron_sync() {
200
		if ( ! self::sync_allowed() ) {
201
			return;
202
		}
203
204
		self::initialize_sender();
205
206
		// remove shutdown hook - no need to sync twice
207
		if ( has_action( 'shutdown', array( self::$sender, 'do_sync' ) ) ) {
208
			remove_action( 'shutdown', array( self::$sender, 'do_sync' ) );
209
		}
210
211
		do {
212
			$next_sync_time = self::$sender->get_next_sync_time( 'sync' );
213
214
			if ( $next_sync_time ) {
215
				$delay = $next_sync_time - time() + 1;
216
				if ( $delay > 15 ) {
217
					break;
218
				} elseif ( $delay > 0 ) {
219
					sleep( $delay );
220
				}
221
			}
222
223
			$result = self::$sender->do_sync();
224
		} while ( $result );
225
	}
226
227 View Code Duplication
	static function do_cron_full_sync() {
228
		if ( ! self::sync_allowed() ) {
229
			return;
230
		}
231
232
		self::initialize_sender();
233
234
		// remove shutdown hook - no need to sync twice
235
		if ( has_action( 'shutdown', array( self::$sender, 'do_sync' ) ) ) {
236
			remove_action( 'shutdown', array( self::$sender, 'do_sync' ) );
237
		}
238
239
		do {
240
			$next_sync_time = self::$sender->get_next_sync_time( 'full_sync' );
241
242
			if ( $next_sync_time ) {
243
				$delay = $next_sync_time - time() + 1;
244
				if ( $delay > 15 ) {
245
					break;
246
				} elseif ( $delay > 0 ) {
247
					sleep( $delay );
248
				}
249
			}
250
251
			$result = self::$sender->do_full_sync();
252
		} while ( $result );
253
	}
254
255
	static function initialize_listener() {
256
		require_once dirname( __FILE__ ) . '/class.jetpack-sync-listener.php';
257
		self::$listener = Jetpack_Sync_Listener::get_instance();
258
	}
259
260
	static function initialize_sender() {
261
		require_once dirname( __FILE__ ) . '/class.jetpack-sync-sender.php';
262
		self::$sender = Jetpack_Sync_Sender::get_instance();
263
264
		// bind the sending process
265
		add_filter( 'jetpack_sync_send_data', array( __CLASS__, 'send_data' ), 10, 4 );
266
	}
267
268
	static function sanitize_filtered_sync_cron_schedule( $schedule ) {
269
		$schedule = sanitize_key( $schedule );
270
		$schedules = wp_get_schedules();
271
272
		// Make sure that the schedule has actually been registered using the `cron_intervals` filter.
273
		if ( isset( $schedules[ $schedule ] ) ) {
274
			return $schedule;
275
		}
276
277
		return self::DEFAULT_SYNC_CRON_INTERVAL;
278
	}
279
280
	static function maybe_schedule_sync_cron( $schedule, $hook ) {
281
		if ( ! $hook ) {
282
			return;
283
		}
284
		$schedule = self::sanitize_filtered_sync_cron_schedule( $schedule );
285
286
		if ( ! wp_next_scheduled( $hook ) ) {
287
			// Schedule a job to send pending queue items once a minute
288
			wp_schedule_event( time(), $schedule, $hook );
289
		} else if ( $schedule != wp_get_schedule( $hook ) ) {
290
			// If the schedule has changed, update the schedule
291
			wp_clear_scheduled_hook( $hook );
292
			wp_schedule_event( time(), $schedule, $hook );
293
		}
294
	}
295
296
	static function init_sync_cron_jobs() {
297
		/**
298
		 * Allows overriding of the default incremental sync cron schedule which defaults to once per minute.
299
		 *
300
		 * @since 4.3.2
301
		 *
302
		 * @param string self::DEFAULT_SYNC_CRON_INTERVAL
303
		 */
304
		$incremental_sync_cron_schedule = apply_filters( 'jetpack_sync_incremental_sync_interval', self::DEFAULT_SYNC_CRON_INTERVAL );
305
		self::maybe_schedule_sync_cron( $incremental_sync_cron_schedule, 'jetpack_sync_cron' );
306
307
		/**
308
		 * Allows overriding of the full sync cron schedule which defaults to once per minute.
309
		 *
310
		 * @since 4.3.2
311
		 *
312
		 * @param string self::DEFAULT_SYNC_CRON_INTERVAL
313
		 */
314
		$full_sync_cron_schedule = apply_filters( 'jetpack_sync_full_sync_interval', self::DEFAULT_SYNC_CRON_INTERVAL );
315
		self::maybe_schedule_sync_cron( $full_sync_cron_schedule, 'jetpack_sync_full_cron' );
316
	}
317
}
318
319
/**
320
 * If the site is using alternate cron, we need to init the listener and sender before cron
321
 * runs. So, we init at a priority of 9.
322
 * 
323
 * If the site is using a regular cron job, we init at a priority of 90 which gives plugins a chance
324
 * to add filters before we initialize.
325
 */
326
if ( defined( 'ALTERNATE_WP_CRON' ) && ALTERNATE_WP_CRON ) {
327
	add_action( 'init', array( 'Jetpack_Sync_Actions', 'init' ), 9 );
328
} else {
329
	add_action( 'init', array( 'Jetpack_Sync_Actions', 'init' ), 90 );
330
}
331
332
// We need to define this here so that it's hooked before `updating_jetpack_version` is called
333
add_action( 'updating_jetpack_version', array( 'Jetpack_Sync_Actions', 'do_initial_sync' ), 10, 2 );
334