Completed
Push — add/videopress-direct-integrat... ( 13dbe5...4094a6 )
by
unknown
255:15 queued 248:21
created

Jetpack_Sync_Actions::do_cron_full_sync()   C

Complexity

Conditions 7
Paths 9

Size

Total Lines 27
Code Lines 16

Duplication

Lines 27
Ratio 100 %

Importance

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