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