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