Completed
Push — update/sync-status-endpoint-ad... ( 2994d8...5ec62d )
by
unknown
32:37 queued 21:41
created

Jetpack_Sync_Actions::schedule_full_sync()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 9
nop 2
dl 0
loc 21
rs 8.7624
c 0
b 0
f 0
1
<?php
2
require_once dirname( __FILE__ ) . '/class.jetpack-sync-settings.php';
3
4
/**
5
 * The role of this class is to hook the Sync subsystem into WordPress - when to listen for actions,
6
 * when to send, when to perform a full sync, etc.
7
 *
8
 * It also binds the action to send data to WPCOM to Jetpack's XMLRPC client object.
9
 */
10
class Jetpack_Sync_Actions {
11
	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...
12
	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...
13
	const INITIAL_SYNC_MULTISITE_INTERVAL = 10;
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__, 'schedule_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
		if ( ! wp_next_scheduled( 'jetpack_sync_cron' ) ) {
43
			// Schedule a job to send pending queue items once a minute
44
			wp_schedule_event( time(), '1min', 'jetpack_sync_cron' );
45
		}
46
47
		if ( ! wp_next_scheduled( 'jetpack_sync_full_cron' ) ) {
48
			// Schedule a job to send pending queue items once a minute
49
			wp_schedule_event( time(), '1min', 'jetpack_sync_full_cron' );
50
		}
51
52
		/**
53
		 * Fires on every request before default loading sync listener code.
54
		 * Return false to not load sync listener code that monitors common
55
		 * WP actions to be serialized.
56
		 *
57
		 * By default this returns true for cron jobs, non-GET-requests, or requests where the
58
		 * user is logged-in.
59
		 *
60
		 * @since 4.2.0
61
		 *
62
		 * @param bool should we load sync listener code for this request
63
		 */
64
		if ( apply_filters( 'jetpack_sync_listener_should_load',
65
			(
66
				( isset( $_SERVER["REQUEST_METHOD"] ) && 'GET' !== $_SERVER['REQUEST_METHOD'] )
67
				||
68
				is_user_logged_in()
69
				||
70
				defined( 'PHPUNIT_JETPACK_TESTSUITE' )
71
				||
72
				defined( 'DOING_CRON' ) && DOING_CRON
73
			)
74
		) ) {
75
			self::initialize_listener();
76
		}
77
78
		/**
79
		 * Fires on every request before default loading sync sender code.
80
		 * Return false to not load sync sender code that serializes pending
81
		 * data and sends it to WPCOM for processing.
82
		 *
83
		 * By default this returns true for cron jobs, POST requests, admin requests, or requests
84
		 * by users who can manage_options.
85
		 *
86
		 * @since 4.2.0
87
		 *
88
		 * @param bool should we load sync sender code for this request
89
		 */
90
		if ( apply_filters( 'jetpack_sync_sender_should_load',
91
			(
92
				( isset( $_SERVER["REQUEST_METHOD"] ) && 'POST' === $_SERVER['REQUEST_METHOD'] )
93
				||
94
				current_user_can( 'manage_options' )
95
				||
96
				is_admin()
97
				||
98
				defined( 'PHPUNIT_JETPACK_TESTSUITE' )
99
				||
100
				defined( 'DOING_CRON' ) && DOING_CRON
101
			)
102
		) ) {
103
			self::initialize_sender();
104
			add_action( 'shutdown', array( self::$sender, 'do_sync' ) );
105
		}
106
107
	}
108
109
	static function sync_allowed() {
110
		return ( ! Jetpack_Sync_Settings::get_setting( 'disable' ) && Jetpack::is_active() && ! ( Jetpack::is_development_mode() || Jetpack::is_staging_site() ) )
111
			   || defined( 'PHPUNIT_JETPACK_TESTSUITE' );
112
	}
113
114
	static function prevent_publicize_blacklisted_posts( $should_publicize, $post ) {
115
		if ( in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) ) ) {
116
			return false;
117
		}
118
119
		return $should_publicize;
120
	}
121
122
	static function set_is_importing_true() {
123
		Jetpack_Sync_Settings::set_importing( true );
124
	}
125
126
	static function send_data( $data, $codec_name, $sent_timestamp, $queue_id ) {
127
		Jetpack::load_xml_rpc_client();
128
129
		$url = add_query_arg( array(
130
			'sync'      => '1', // add an extra parameter to the URL so we can tell it's a sync action
131
			'codec'     => $codec_name, // send the name of the codec used to encode the data
132
			'timestamp' => $sent_timestamp, // send current server time so we can compensate for clock differences
133
			'queue'     => $queue_id, // sync or full_sync
134
		), Jetpack::xmlrpc_api_url() );
135
136
		$rpc = new Jetpack_IXR_Client( array(
137
			'url'     => $url,
138
			'user_id' => JETPACK_MASTER_USER,
139
			'timeout' => 30,
140
		) );
141
142
		$result = $rpc->query( 'jetpack.syncActions', $data );
143
144
		if ( ! $result ) {
145
			return $rpc->get_jetpack_error();
146
		}
147
148
		return $rpc->getResponse();
149
	}
150
151
	static function schedule_initial_sync( $new_version = null, $old_version = null ) {
152
		$initial_sync_config = array(
153
			'options' => true,
154
			'network_options' => true,
155
			'functions' => true,
156
			'constants' => true,
157
		);
158
159
		if ( $old_version && ( version_compare( $old_version, '4.2', '<' ) ) ) {
160
			$initial_sync_config['users'] = 'initial';
161
		}
162
163
		// we need this function call here because we have to run this function
164
		// reeeeally early in init, before WP_CRON_LOCK_TIMEOUT is defined.
165
		wp_functionality_constants();
166
167
		if ( is_multisite() ) {
168
			// stagger initial syncs for multisite blogs so they don't all pile on top of each other
169
			$time_offset = ( rand() / getrandmax() ) * self::INITIAL_SYNC_MULTISITE_INTERVAL * get_blog_count();
170
		} else {
171
			$time_offset = 1;
172
		}
173
174
		self::schedule_full_sync(
175
			$initial_sync_config,
176
			$time_offset
177
		);
178
	}
179
180
	static function schedule_full_sync( $modules = null, $time_offset = 1 ) {
181
		if ( ! self::sync_allowed() ) {
182
			return false;
183
		}
184
185
		if ( self::is_scheduled_full_sync() ) {
186
			self::unschedule_all_full_syncs();
187
		}
188
189
		if ( $modules ) {
190
			wp_schedule_single_event( time() + $time_offset, 'jetpack_sync_full', array( $modules ) );
191
		} else {
192
			wp_schedule_single_event( time() + $time_offset, 'jetpack_sync_full' );
193
		}
194
195
		if ( $time_offset === 1 ) {
196
			spawn_cron();
197
		}
198
199
		return true;
200
	}
201
202
	static function unschedule_all_full_syncs() {
203
		foreach ( _get_cron_array() as $timestamp => $cron ) {
204
			if ( ! empty( $cron['jetpack_sync_full'] ) ) {
205
				foreach( $cron['jetpack_sync_full'] as $key => $config ) {
206
					wp_unschedule_event( $timestamp, 'jetpack_sync_full', $config['args'] );
207
				}
208
			}
209
		}
210
	}
211
212
	static function is_scheduled_full_sync( $modules = null ) {
213
		if ( is_null( $modules ) ) {
214
			$crons = _get_cron_array();
215
216
			foreach ( $crons as $timestamp => $cron ) {
217
				if ( ! empty( $cron['jetpack_sync_full'] ) ) {
218
					return true;
219
				}
220
			}
221
			return false;
222
		}
223
224
		return !! wp_next_scheduled( 'jetpack_sync_full', array( $modules ) );
225
	}
226
227
	static function do_full_sync( $modules = null ) {
228
		if ( ! self::sync_allowed() ) {
229
			return;
230
		}
231
232
		self::initialize_listener();
233
		Jetpack_Sync_Modules::get_module( 'full-sync' )->start( $modules );
234
		self::do_cron_sync(); // immediately run a cron sync, which sends pending data
235
	}
236
237
	static function minute_cron_schedule( $schedules ) {
238
		if( ! isset( $schedules["1min"] ) ) {
239
			$schedules["1min"] = array(
240
				'interval' => 60,
241
				'display' => __( 'Every minute' )
242
			);
243
		}
244
		return $schedules;
245
	}
246
247
	// try to send actions until we run out of things to send,
248
	// or have to wait more than 15s before sending again,
249
	// or we hit a lock or some other sending issue
250 View Code Duplication
	static function do_cron_sync() {
251
		if ( ! self::sync_allowed() ) {
252
			return;
253
		}
254
255
		self::initialize_sender();
256
257
		// remove shutdown hook - no need to sync twice
258
		if ( has_action( 'shutdown', array( self::$sender, 'do_sync' ) ) ) {
259
			remove_action( 'shutdown', array( self::$sender, 'do_sync' ) );
260
		}
261
262
		do {
263
			$next_sync_time = self::$sender->get_next_sync_time( 'sync' );
264
265
			if ( $next_sync_time ) {
266
				$delay = $next_sync_time - time() + 1;
267
				if ( $delay > 15 ) {
268
					break;
269
				} elseif ( $delay > 0 ) {
270
					sleep( $delay );
271
				}
272
			}
273
274
			$result = self::$sender->do_sync();
275
		} while ( $result );
276
	}
277
278 View Code Duplication
	static function do_cron_full_sync() {
279
		if ( ! self::sync_allowed() ) {
280
			return;
281
		}
282
283
		self::initialize_sender();
284
285
		// remove shutdown hook - no need to sync twice
286
		if ( has_action( 'shutdown', array( self::$sender, 'do_sync' ) ) ) {
287
			remove_action( 'shutdown', array( self::$sender, 'do_sync' ) );
288
		}
289
290
		do {
291
			$next_sync_time = self::$sender->get_next_sync_time( 'full_sync' );
292
293
			if ( $next_sync_time ) {
294
				$delay = $next_sync_time - time() + 1;
295
				if ( $delay > 15 ) {
296
					break;
297
				} elseif ( $delay > 0 ) {
298
					sleep( $delay );
299
				}
300
			}
301
302
			$result = self::$sender->do_full_sync();
303
		} while ( $result );
304
	}
305
306
	static function initialize_listener() {
307
		require_once dirname( __FILE__ ) . '/class.jetpack-sync-listener.php';
308
		self::$listener = Jetpack_Sync_Listener::get_instance();
309
	}
310
311
	static function initialize_sender() {
312
		require_once dirname( __FILE__ ) . '/class.jetpack-sync-sender.php';
313
		self::$sender = Jetpack_Sync_Sender::get_instance();
314
315
		// bind the sending process
316
		add_filter( 'jetpack_sync_send_data', array( __CLASS__, 'send_data' ), 10, 4 );
317
	}
318
}
319
320
// Allow other plugins to add filters before we initialize the actions.
321
// Load the listeners if before modules get loaded so that we can capture version changes etc.
322
add_action( 'init', array( 'Jetpack_Sync_Actions', 'init' ), 90 );
323
324
// We need to define this here so that it's hooked before `updating_jetpack_version` is called
325
add_action( 'updating_jetpack_version', array( 'Jetpack_Sync_Actions', 'schedule_initial_sync' ), 10, 2 );
326