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
|
|
|
|
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__, 'schedule_full_sync' ) ); |
21
|
|
|
|
22
|
|
|
// When imports are finished, schedule a full sync |
23
|
|
|
add_action( 'import_end', array( __CLASS__, 'schedule_full_sync' ) ); |
24
|
|
|
|
25
|
|
|
// When importing via cron, do not sync |
26
|
|
|
add_action( 'wp_cron_importer_hook', array( __CLASS__, 'set_is_importing_true' ), 1 ); |
27
|
|
|
|
28
|
|
|
// Sync connected user role changes to .com |
29
|
|
|
require_once dirname( __FILE__ ) . '/class.jetpack-sync-users.php'; |
30
|
|
|
|
31
|
|
|
// everything below this point should only happen if we're a valid sync site |
32
|
|
|
if ( ! self::sync_allowed() ) { |
33
|
|
|
return; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
// cron hooks |
37
|
|
|
add_action( 'jetpack_sync_send_db_checksum', array( __CLASS__, 'send_db_checksum' ) ); |
38
|
|
|
add_action( 'jetpack_sync_full', array( __CLASS__, 'do_full_sync' ), 10, 1 ); |
39
|
|
|
add_action( 'jetpack_sync_cron', array( __CLASS__, 'do_cron_sync' ) ); |
40
|
|
|
add_action( 'jetpack_sync_send_pending_data', array( __CLASS__, 'do_send_pending_data' ) ); |
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::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 schedule_initial_sync() { |
140
|
|
|
// we need this function call here because we have to run this function |
141
|
|
|
// reeeeally early in init, before WP_CRON_LOCK_TIMEOUT is defined. |
142
|
|
|
wp_functionality_constants(); |
143
|
|
|
self::schedule_full_sync( array( 'options' => true, 'network_options' => true, 'functions' => true, 'constants' => true, 'users' => true ) ); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
static function schedule_full_sync( $modules = null ) { |
147
|
|
|
if ( $modules ) { |
148
|
|
|
wp_schedule_single_event( time() + 1, 'jetpack_sync_full', array( $modules ) ); |
149
|
|
|
} else { |
150
|
|
|
wp_schedule_single_event( time() + 1, 'jetpack_sync_full' ); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
spawn_cron(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
static function is_scheduled_full_sync( $modules = null ) { |
157
|
|
|
if ( is_null( $modules ) ) { |
158
|
|
|
$crons = _get_cron_array(); |
159
|
|
|
|
160
|
|
|
foreach ( $crons as $timestamp => $cron ) { |
161
|
|
|
if ( ! empty( $cron['jetpack_sync_full'] ) ) { |
162
|
|
|
return true; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return false; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return wp_next_scheduled( 'jetpack_sync_full', array( $modules ) ); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
static function do_full_sync( $modules = null ) { |
173
|
|
|
if ( ! self::sync_allowed() ) { |
174
|
|
|
return; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
self::initialize_listener(); |
178
|
|
|
Jetpack_Sync_Modules::get_module( 'full-sync' )->start( $modules ); |
179
|
|
|
self::do_send_pending_data(); // try to send at least some of the data |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
static function minute_cron_schedule( $schedules ) { |
183
|
|
|
if( ! isset( $schedules["1min"] ) ) { |
184
|
|
|
$schedules["1min"] = array( |
185
|
|
|
'interval' => 60, |
186
|
|
|
'display' => __( 'Every minute' ) |
187
|
|
|
); |
188
|
|
|
} |
189
|
|
|
return $schedules; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
// try to send actions until we run out of things to send, |
193
|
|
|
// or have to wait more than 15s before sending again, |
194
|
|
|
// or we hit a lock or some other sending issue |
195
|
|
|
static function do_cron_sync() { |
196
|
|
|
if ( ! self::sync_allowed() ) { |
197
|
|
|
return; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
self::initialize_sender(); |
201
|
|
|
|
202
|
|
|
do { |
203
|
|
|
$next_sync_time = self::$sender->get_next_sync_time(); |
204
|
|
|
|
205
|
|
|
if ( $next_sync_time ) { |
206
|
|
|
$delay = $next_sync_time - time() + 1; |
207
|
|
|
if ( $delay > 15 ) { |
208
|
|
|
break; |
209
|
|
|
} elseif ( $delay > 0 ) { |
210
|
|
|
sleep( $delay ); |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
$result = self::$sender->do_sync(); |
215
|
|
|
} while ( $result ); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
static function do_send_pending_data() { |
219
|
|
|
self::initialize_sender(); |
220
|
|
|
self::$sender->do_sync(); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
static function send_db_checksum() { |
224
|
|
|
self::initialize_listener(); |
225
|
|
|
self::initialize_sender(); |
226
|
|
|
self::$sender->send_checksum(); |
227
|
|
|
self::$sender->do_sync(); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
static function initialize_listener() { |
231
|
|
|
require_once dirname( __FILE__ ) . '/class.jetpack-sync-listener.php'; |
232
|
|
|
self::$listener = Jetpack_Sync_Listener::get_instance(); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
static function initialize_sender() { |
236
|
|
|
require_once dirname( __FILE__ ) . '/class.jetpack-sync-sender.php'; |
237
|
|
|
self::$sender = Jetpack_Sync_Sender::get_instance(); |
238
|
|
|
|
239
|
|
|
// bind the sending process |
240
|
|
|
add_filter( 'jetpack_sync_send_data', array( __CLASS__, 'send_data' ), 10, 4 ); |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
// Allow other plugins to add filters before we initialize the actions. |
245
|
|
|
// Load the listeners if before modules get loaded so that we can capture version changes etc. |
246
|
|
|
add_action( 'plugins_loaded', array( 'Jetpack_Sync_Actions', 'init' ), 90 ); |
247
|
|
|
|
248
|
|
|
// We need to define this here so that it's hooked before `updating_jetpack_version` is called |
249
|
|
|
add_action( 'updating_jetpack_version', array( 'Jetpack_Sync_Actions', 'schedule_initial_sync' ), 10 ); |
250
|
|
|
|
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.