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