Completed
Push — enhance/strings-and-content ( 1c42ec...fbd7de )
by
unknown
29:41 queued 18:53
created

Jetpack_Sync_Actions::do_cron_sync()   C

Complexity

Conditions 7
Paths 9

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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