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