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; |
|
|
|
|
11
|
|
|
static $listener = null; |
|
|
|
|
12
|
|
|
const DEFAULT_SYNC_CRON_INTERVAL_NAME = 'jetpack_sync_interval'; |
13
|
|
|
const DEFAULT_SYNC_CRON_INTERVAL_VALUE = 300; // 5 * MINUTE_IN_SECONDS; |
14
|
|
|
|
15
|
|
|
static function init() { |
16
|
|
|
|
17
|
|
|
// everything below this point should only happen if we're a valid sync site |
18
|
|
|
if ( ! self::sync_allowed() ) { |
19
|
|
|
return; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
if ( self::sync_via_cron_allowed() ) { |
23
|
|
|
self::init_sync_cron_jobs(); |
24
|
|
|
} else if ( wp_next_scheduled( 'jetpack_sync_cron' ) ) { |
25
|
|
|
wp_clear_scheduled_hook( 'jetpack_sync_cron' ); |
26
|
|
|
wp_clear_scheduled_hook( 'jetpack_sync_full_cron' ); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
// On jetpack authorization, schedule a full sync |
30
|
|
|
add_action( 'jetpack_client_authorized', array( __CLASS__, 'do_full_sync' ), 10, 0 ); |
31
|
|
|
|
32
|
|
|
// When importing via cron, do not sync |
33
|
|
|
add_action( 'wp_cron_importer_hook', array( __CLASS__, 'set_is_importing_true' ), 1 ); |
34
|
|
|
|
35
|
|
|
// Sync connected user role changes to .com |
36
|
|
|
require_once dirname( __FILE__ ) . '/class.jetpack-sync-users.php'; |
37
|
|
|
|
38
|
|
|
// publicize filter to prevent publicizing blacklisted post types |
39
|
|
|
add_filter( 'publicize_should_publicize_published_post', array( __CLASS__, 'prevent_publicize_blacklisted_posts' ), 10, 2 ); |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Fires on every request before default loading sync listener code. |
43
|
|
|
* Return false to not load sync listener code that monitors common |
44
|
|
|
* WP actions to be serialized. |
45
|
|
|
* |
46
|
|
|
* By default this returns true for cron jobs, non-GET-requests, or requests where the |
47
|
|
|
* user is logged-in. |
48
|
|
|
* |
49
|
|
|
* @since 4.2.0 |
50
|
|
|
* |
51
|
|
|
* @param bool should we load sync listener code for this request |
52
|
|
|
*/ |
53
|
|
|
if ( apply_filters( 'jetpack_sync_listener_should_load', true ) ) { |
54
|
|
|
self::initialize_listener(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
add_action( 'init', array( __CLASS__, 'add_sender_shutdown' ), 90 ); |
58
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
static function add_sender_shutdown() { |
62
|
|
|
/** |
63
|
|
|
* Fires on every request before default loading sync sender code. |
64
|
|
|
* Return false to not load sync sender code that serializes pending |
65
|
|
|
* data and sends it to WPCOM for processing. |
66
|
|
|
* |
67
|
|
|
* By default this returns true for cron jobs, POST requests, admin requests, or requests |
68
|
|
|
* by users who can manage_options. |
69
|
|
|
* |
70
|
|
|
* @since 4.2.0 |
71
|
|
|
* |
72
|
|
|
* @param bool should we load sync sender code for this request |
73
|
|
|
*/ |
74
|
|
|
if ( apply_filters( 'jetpack_sync_sender_should_load', |
75
|
|
|
( |
76
|
|
|
( isset( $_SERVER["REQUEST_METHOD"] ) && 'POST' === $_SERVER['REQUEST_METHOD'] ) |
77
|
|
|
|| |
78
|
|
|
current_user_can( 'manage_options' ) |
79
|
|
|
|| |
80
|
|
|
is_admin() |
81
|
|
|
|| |
82
|
|
|
defined( 'PHPUNIT_JETPACK_TESTSUITE' ) |
83
|
|
|
) |
84
|
|
|
) ) { |
85
|
|
|
self::initialize_sender(); |
86
|
|
|
add_action( 'shutdown', array( self::$sender, 'do_sync' ) ); |
87
|
|
|
add_action( 'shutdown', array( self::$sender, 'do_full_sync' ) ); |
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 sync_via_cron_allowed() { |
98
|
|
|
require_once dirname( __FILE__ ) . '/class.jetpack-sync-settings.php'; |
99
|
|
|
return ( Jetpack_Sync_Settings::get_setting( 'sync_via_cron' ) ); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
static function prevent_publicize_blacklisted_posts( $should_publicize, $post ) { |
103
|
|
|
require_once dirname( __FILE__ ) . '/class.jetpack-sync-settings.php'; |
104
|
|
|
if ( in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) ) ) { |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $should_publicize; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
static function set_is_importing_true() { |
112
|
|
|
require_once dirname( __FILE__ ) . '/class.jetpack-sync-settings.php'; |
113
|
|
|
Jetpack_Sync_Settings::set_importing( true ); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
static function send_data( $data, $codec_name, $sent_timestamp, $queue_id ) { |
117
|
|
|
Jetpack::load_xml_rpc_client(); |
118
|
|
|
|
119
|
|
|
$query_args = array( |
120
|
|
|
'sync' => '1', // add an extra parameter to the URL so we can tell it's a sync action |
121
|
|
|
'codec' => $codec_name, // send the name of the codec used to encode the data |
122
|
|
|
'timestamp' => $sent_timestamp, // send current server time so we can compensate for clock differences |
123
|
|
|
'queue' => $queue_id, // sync or full_sync |
124
|
|
|
'home' => get_home_url(), // Send home url option to check for Identity Crisis server-side |
125
|
|
|
'siteurl' => get_site_url(), // Send siteurl option to check for Identity Crisis server-side |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
// Has the site opted in to IDC mitigation? |
129
|
|
|
if ( Jetpack::sync_idc_optin() ) { |
130
|
|
|
$query_args['idc'] = true; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if ( Jetpack_Options::get_option( 'migrate_for_idc', false ) ) { |
134
|
|
|
$query_args['migrate_for_idc'] = true; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$query_args['timeout'] = Jetpack_Sync_Settings::is_doing_cron() ? 30 : 15; |
138
|
|
|
|
139
|
|
|
$url = add_query_arg( $query_args, Jetpack::xmlrpc_api_url() ); |
140
|
|
|
|
141
|
|
|
$rpc = new Jetpack_IXR_Client( array( |
142
|
|
|
'url' => $url, |
143
|
|
|
'user_id' => JETPACK_MASTER_USER, |
144
|
|
|
'timeout' => $query_args['timeout'], |
145
|
|
|
) ); |
146
|
|
|
|
147
|
|
|
$result = $rpc->query( 'jetpack.syncActions', $data ); |
148
|
|
|
|
149
|
|
|
if ( ! $result ) { |
150
|
|
|
return $rpc->get_jetpack_error(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$response = $rpc->getResponse(); |
154
|
|
|
|
155
|
|
|
// Check if WordPress.com IDC mitigation blocked the sync request |
156
|
|
|
if ( is_array( $response ) && isset( $response['error_code'] ) ) { |
157
|
|
|
$error_code = $response['error_code']; |
158
|
|
|
$allowed_idc_error_codes = array( |
159
|
|
|
'jetpack_url_mismatch', |
160
|
|
|
'jetpack_home_url_mismatch', |
161
|
|
|
'jetpack_site_url_mismatch' |
162
|
|
|
); |
163
|
|
|
|
164
|
|
|
if ( in_array( $error_code, $allowed_idc_error_codes ) ) { |
165
|
|
|
Jetpack_Options::update_option( |
166
|
|
|
'sync_error_idc', |
167
|
|
|
Jetpack::get_sync_error_idc_option( $response ) |
168
|
|
|
); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return new WP_Error( |
172
|
|
|
'sync_error_idc', |
173
|
|
|
esc_html__( 'Sync has been blocked from WordPress.com because it would cause an identity crisis', 'jetpack' ) |
174
|
|
|
); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return $response; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
static function do_initial_sync( $new_version = null, $old_version = null ) { |
181
|
|
|
$initial_sync_config = array( |
182
|
|
|
'options' => true, |
183
|
|
|
'network_options' => true, |
184
|
|
|
'functions' => true, |
185
|
|
|
'constants' => true, |
186
|
|
|
); |
187
|
|
|
|
188
|
|
|
if ( $old_version && ( version_compare( $old_version, '4.2', '<' ) ) ) { |
189
|
|
|
$initial_sync_config['users'] = 'initial'; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
self::do_full_sync( $initial_sync_config ); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
static function do_full_sync( $modules = null ) { |
196
|
|
|
if ( ! self::sync_allowed() ) { |
197
|
|
|
return false; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
self::initialize_listener(); |
201
|
|
|
Jetpack_Sync_Modules::get_module( 'full-sync' )->start( $modules ); |
202
|
|
|
|
203
|
|
|
return true; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
static function jetpack_cron_schedule( $schedules ) { |
207
|
|
|
if ( ! isset( $schedules[ self::DEFAULT_SYNC_CRON_INTERVAL_NAME ] ) ) { |
208
|
|
|
$schedules[ self::DEFAULT_SYNC_CRON_INTERVAL_NAME ] = array( |
209
|
|
|
'interval' => self::DEFAULT_SYNC_CRON_INTERVAL_VALUE, |
210
|
|
|
'display' => sprintf( |
211
|
|
|
esc_html__( 'Every %d minutes', 'jetpack' ), |
212
|
|
|
self::DEFAULT_SYNC_CRON_INTERVAL_VALUE / 60 |
213
|
|
|
) |
214
|
|
|
); |
215
|
|
|
} |
216
|
|
|
return $schedules; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
// try to send actions until we run out of things to send, |
220
|
|
|
// or have to wait more than 15s before sending again, |
221
|
|
|
// or we hit a lock or some other sending issue |
222
|
|
View Code Duplication |
static function do_cron_sync() { |
223
|
|
|
if ( ! self::sync_allowed() ) { |
224
|
|
|
return; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
self::initialize_sender(); |
228
|
|
|
|
229
|
|
|
do { |
230
|
|
|
$next_sync_time = self::$sender->get_next_sync_time( 'sync' ); |
231
|
|
|
|
232
|
|
|
if ( $next_sync_time ) { |
233
|
|
|
$delay = $next_sync_time - time() + 1; |
234
|
|
|
if ( $delay > 15 ) { |
235
|
|
|
break; |
236
|
|
|
} elseif ( $delay > 0 ) { |
237
|
|
|
sleep( $delay ); |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
$result = self::$sender->do_sync(); |
242
|
|
|
} while ( $result ); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
View Code Duplication |
static function do_cron_full_sync() { |
246
|
|
|
if ( ! self::sync_allowed() ) { |
247
|
|
|
return; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
self::initialize_sender(); |
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_NAME; |
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
|
|
|
// Add a custom "every minute" cron schedule |
311
|
|
|
add_filter( 'cron_schedules', array( __CLASS__, 'jetpack_cron_schedule' ) ); |
312
|
|
|
|
313
|
|
|
// cron hooks |
314
|
|
|
add_action( 'jetpack_sync_full', array( __CLASS__, 'do_full_sync' ), 10, 1 ); |
315
|
|
|
|
316
|
|
|
add_action( 'jetpack_sync_cron', array( __CLASS__, 'do_cron_sync' ) ); |
317
|
|
|
add_action( 'jetpack_sync_full_cron', array( __CLASS__, 'do_cron_full_sync' ) ); |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Allows overriding of the default incremental sync cron schedule which defaults to once every 5 minutes. |
321
|
|
|
* |
322
|
|
|
* @since 4.3.2 |
323
|
|
|
* |
324
|
|
|
* @param string self::DEFAULT_SYNC_CRON_INTERVAL_NAME |
325
|
|
|
*/ |
326
|
|
|
$incremental_sync_cron_schedule = apply_filters( 'jetpack_sync_incremental_sync_interval', self::DEFAULT_SYNC_CRON_INTERVAL_NAME ); |
327
|
|
|
self::maybe_schedule_sync_cron( $incremental_sync_cron_schedule, 'jetpack_sync_cron' ); |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Allows overriding of the full sync cron schedule which defaults to once every 5 minutes. |
331
|
|
|
* |
332
|
|
|
* @since 4.3.2 |
333
|
|
|
* |
334
|
|
|
* @param string self::DEFAULT_SYNC_CRON_INTERVAL_NAME |
335
|
|
|
*/ |
336
|
|
|
$full_sync_cron_schedule = apply_filters( 'jetpack_sync_full_sync_interval', self::DEFAULT_SYNC_CRON_INTERVAL_NAME ); |
337
|
|
|
self::maybe_schedule_sync_cron( $full_sync_cron_schedule, 'jetpack_sync_full_cron' ); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
static function cleanup_on_upgrade() { |
341
|
|
|
if ( wp_next_scheduled( 'jetpack_sync_send_db_checksum' ) ) { |
342
|
|
|
wp_clear_scheduled_hook( 'jetpack_sync_send_db_checksum' ); |
343
|
|
|
} |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
static function get_sync_status() { |
347
|
|
|
self::initialize_sender(); |
348
|
|
|
|
349
|
|
|
$sync_module = Jetpack_Sync_Modules::get_module( 'full-sync' ); |
350
|
|
|
$queue = self::$sender->get_sync_queue(); |
351
|
|
|
$full_queue = self::$sender->get_full_sync_queue(); |
352
|
|
|
$cron_timestamps = array_keys( _get_cron_array() ); |
353
|
|
|
$next_cron = $cron_timestamps[0] - time(); |
354
|
|
|
|
355
|
|
|
return array_merge( |
356
|
|
|
$sync_module->get_status(), |
357
|
|
|
array( |
358
|
|
|
'cron_size' => count( $cron_timestamps ), |
359
|
|
|
'next_cron' => $next_cron, |
360
|
|
|
'queue_size' => $queue->size(), |
361
|
|
|
'queue_lag' => $queue->lag(), |
362
|
|
|
'queue_next_sync' => ( self::$sender->get_next_sync_time( 'sync' ) - microtime( true ) ), |
363
|
|
|
'full_queue_size' => $full_queue->size(), |
364
|
|
|
'full_queue_lag' => $full_queue->lag(), |
365
|
|
|
'full_queue_next_sync' => ( self::$sender->get_next_sync_time( 'full_sync' ) - microtime( true ) ), |
366
|
|
|
) |
367
|
|
|
); |
368
|
|
|
} |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* If the site is using alternate cron, we need to init the listener and sender before cron |
373
|
|
|
* runs. So, we init at a priority of 9. |
374
|
|
|
* |
375
|
|
|
* If the site is using a regular cron job, we init at a priority of 90 which gives plugins a chance |
376
|
|
|
* to add filters before we initialize. |
377
|
|
|
*/ |
378
|
|
|
add_action( 'plugins_loaded', array( 'Jetpack_Sync_Actions', 'init' ), 90 ); |
379
|
|
|
|
380
|
|
|
// We need to define this here so that it's hooked before `updating_jetpack_version` is called |
381
|
|
|
add_action( 'updating_jetpack_version', array( 'Jetpack_Sync_Actions', 'do_initial_sync' ), 10, 2 ); |
382
|
|
|
add_action( 'updating_jetpack_version', array( 'Jetpack_Sync_Actions', 'cleanup_on_upgrade' ) ); |
383
|
|
|
|
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.