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 INITIAL_SYNC_MULTISITE_INTERVAL = 10; |
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
|
|
|
// publicize filter to prevent publicizing blacklisted post types |
38
|
|
|
add_filter( 'publicize_should_publicize_published_post', array( __CLASS__, 'prevent_publicize_blacklisted_posts' ), 10, 2 ); |
39
|
|
|
|
40
|
|
|
// cron hooks |
41
|
|
|
add_action( 'jetpack_sync_full', array( __CLASS__, 'do_full_sync' ), 10, 1 ); |
42
|
|
|
add_action( 'jetpack_sync_cron', array( __CLASS__, 'do_cron_sync' ) ); |
43
|
|
|
|
44
|
|
|
if ( ! wp_next_scheduled( 'jetpack_sync_cron' ) ) { |
45
|
|
|
// Schedule a job to send pending queue items once a minute |
46
|
|
|
wp_schedule_event( time(), '1min', 'jetpack_sync_cron' ); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Fires on every request before default loading sync listener code. |
51
|
|
|
* Return false to not load sync listener code that monitors common |
52
|
|
|
* WP actions to be serialized. |
53
|
|
|
* |
54
|
|
|
* By default this returns true for non-GET-requests, or requests where the |
55
|
|
|
* user is logged-in. |
56
|
|
|
* |
57
|
|
|
* @since 4.2.0 |
58
|
|
|
* |
59
|
|
|
* @param bool should we load sync listener code for this request |
60
|
|
|
*/ |
61
|
|
|
if ( apply_filters( 'jetpack_sync_listener_should_load', |
62
|
|
|
( |
63
|
|
|
'GET' !== $_SERVER['REQUEST_METHOD'] |
64
|
|
|
|| |
65
|
|
|
is_user_logged_in() |
66
|
|
|
|| |
67
|
|
|
defined( 'PHPUNIT_JETPACK_TESTSUITE' ) |
68
|
|
|
) |
69
|
|
|
) ) { |
70
|
|
|
self::initialize_listener(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Fires on every request before default loading sync sender code. |
75
|
|
|
* Return false to not load sync sender code that serializes pending |
76
|
|
|
* data and sends it to WPCOM for processing. |
77
|
|
|
* |
78
|
|
|
* By default this returns true for POST requests, admin requests, or requests |
79
|
|
|
* by users who can manage_options. |
80
|
|
|
* |
81
|
|
|
* @since 4.2.0 |
82
|
|
|
* |
83
|
|
|
* @param bool should we load sync sender code for this request |
84
|
|
|
*/ |
85
|
|
|
if ( apply_filters( 'jetpack_sync_sender_should_load', |
86
|
|
|
( |
87
|
|
|
'POST' === $_SERVER['REQUEST_METHOD'] |
88
|
|
|
|| |
89
|
|
|
current_user_can( 'manage_options' ) |
90
|
|
|
|| |
91
|
|
|
is_admin() |
92
|
|
|
|| |
93
|
|
|
defined( 'PHPUNIT_JETPACK_TESTSUITE' ) |
94
|
|
|
) |
95
|
|
|
) ) { |
96
|
|
|
self::initialize_sender(); |
97
|
|
|
add_action( 'shutdown', array( self::$sender, 'do_sync' ) ); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
static function sync_allowed() { |
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
|
|
|
if ( in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) ) ) { |
109
|
|
|
return false; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $should_publicize; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
static function set_is_importing_true() { |
116
|
|
|
Jetpack_Sync_Settings::set_importing( true ); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
static function send_data( $data, $codec_name, $sent_timestamp, $queue_id ) { |
120
|
|
|
Jetpack::load_xml_rpc_client(); |
121
|
|
|
|
122
|
|
|
$url = add_query_arg( array( |
123
|
|
|
'sync' => '1', // add an extra parameter to the URL so we can tell it's a sync action |
124
|
|
|
'codec' => $codec_name, // send the name of the codec used to encode the data |
125
|
|
|
'timestamp' => $sent_timestamp, // send current server time so we can compensate for clock differences |
126
|
|
|
'queue' => $queue_id, // sync or full_sync |
127
|
|
|
), Jetpack::xmlrpc_api_url() ); |
128
|
|
|
|
129
|
|
|
$rpc = new Jetpack_IXR_Client( array( |
130
|
|
|
'url' => $url, |
131
|
|
|
'user_id' => JETPACK_MASTER_USER, |
132
|
|
|
'timeout' => 30, |
133
|
|
|
) ); |
134
|
|
|
|
135
|
|
|
$result = $rpc->query( 'jetpack.syncActions', $data ); |
136
|
|
|
|
137
|
|
|
if ( ! $result ) { |
138
|
|
|
return $rpc->get_jetpack_error(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $rpc->getResponse(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
static function schedule_initial_sync() { |
145
|
|
|
// we need this function call here because we have to run this function |
146
|
|
|
// reeeeally early in init, before WP_CRON_LOCK_TIMEOUT is defined. |
147
|
|
|
wp_functionality_constants(); |
148
|
|
|
|
149
|
|
|
if ( is_multisite() ) { |
150
|
|
|
// stagger initial syncs for multisite blogs so they don't all pile on top of each other |
151
|
|
|
$time_offset = ( rand() / getrandmax() ) * self::INITIAL_SYNC_MULTISITE_INTERVAL * get_blog_count(); |
152
|
|
|
} else { |
153
|
|
|
$time_offset = 1; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
self::schedule_full_sync( |
157
|
|
|
array( |
158
|
|
|
'options' => true, |
159
|
|
|
'network_options' => true, |
160
|
|
|
'functions' => true, |
161
|
|
|
'constants' => true, |
162
|
|
|
'users' => 'initial' |
163
|
|
|
), |
164
|
|
|
$time_offset |
165
|
|
|
); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
static function schedule_full_sync( $modules = null, $time_offset = 1 ) { |
169
|
|
|
if ( ! self::sync_allowed() ) { |
170
|
|
|
return false; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
if ( self::is_scheduled_full_sync() ) { |
174
|
|
|
self::unschedule_all_full_syncs(); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
if ( $modules ) { |
178
|
|
|
wp_schedule_single_event( time() + $time_offset, 'jetpack_sync_full', array( $modules ) ); |
179
|
|
|
} else { |
180
|
|
|
wp_schedule_single_event( time() + $time_offset, 'jetpack_sync_full' ); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
if ( $time_offset === 1 ) { |
184
|
|
|
spawn_cron(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return true; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
static function unschedule_all_full_syncs() { |
191
|
|
|
foreach ( _get_cron_array() as $timestamp => $cron ) { |
192
|
|
|
if ( ! empty( $cron['jetpack_sync_full'] ) ) { |
193
|
|
|
foreach( $cron['jetpack_sync_full'] as $key => $config ) { |
194
|
|
|
wp_unschedule_event( $timestamp, 'jetpack_sync_full', $config['args'] ); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
static function is_scheduled_full_sync( $modules = null ) { |
201
|
|
|
if ( is_null( $modules ) ) { |
202
|
|
|
$crons = _get_cron_array(); |
203
|
|
|
|
204
|
|
|
foreach ( $crons as $timestamp => $cron ) { |
205
|
|
|
if ( ! empty( $cron['jetpack_sync_full'] ) ) { |
206
|
|
|
return true; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
return false; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
return wp_next_scheduled( 'jetpack_sync_full', array( $modules ) ); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
static function do_full_sync( $modules = null ) { |
216
|
|
|
if ( ! self::sync_allowed() ) { |
217
|
|
|
return; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
self::initialize_listener(); |
221
|
|
|
Jetpack_Sync_Modules::get_module( 'full-sync' )->start( $modules ); |
222
|
|
|
self::do_cron_sync(); // immediately run a cron sync, which sends pending data |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
static function minute_cron_schedule( $schedules ) { |
226
|
|
|
if( ! isset( $schedules["1min"] ) ) { |
227
|
|
|
$schedules["1min"] = array( |
228
|
|
|
'interval' => 60, |
229
|
|
|
'display' => __( 'Every minute' ) |
230
|
|
|
); |
231
|
|
|
} |
232
|
|
|
return $schedules; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
// try to send actions until we run out of things to send, |
236
|
|
|
// or have to wait more than 15s before sending again, |
237
|
|
|
// or we hit a lock or some other sending issue |
238
|
|
|
static function do_cron_sync() { |
239
|
|
|
if ( ! self::sync_allowed() ) { |
240
|
|
|
return; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
self::initialize_sender(); |
244
|
|
|
|
245
|
|
|
// remove shutdown hook - no need to sync twice |
246
|
|
|
if ( has_action( 'shutdown', array( self::$sender, 'do_sync' ) ) ) { |
247
|
|
|
remove_action( 'shutdown', array( self::$sender, 'do_sync' ) ); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
do { |
251
|
|
|
$next_sync_time = self::$sender->get_next_sync_time(); |
252
|
|
|
|
253
|
|
|
if ( $next_sync_time ) { |
254
|
|
|
$delay = $next_sync_time - time() + 1; |
255
|
|
|
if ( $delay > 15 ) { |
256
|
|
|
break; |
257
|
|
|
} elseif ( $delay > 0 ) { |
258
|
|
|
sleep( $delay ); |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
$result = self::$sender->do_sync(); |
263
|
|
|
} while ( $result ); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
static function initialize_listener() { |
267
|
|
|
require_once dirname( __FILE__ ) . '/class.jetpack-sync-listener.php'; |
268
|
|
|
self::$listener = Jetpack_Sync_Listener::get_instance(); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
static function initialize_sender() { |
272
|
|
|
require_once dirname( __FILE__ ) . '/class.jetpack-sync-sender.php'; |
273
|
|
|
self::$sender = Jetpack_Sync_Sender::get_instance(); |
274
|
|
|
|
275
|
|
|
// bind the sending process |
276
|
|
|
add_filter( 'jetpack_sync_send_data', array( __CLASS__, 'send_data' ), 10, 4 ); |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
// Allow other plugins to add filters before we initialize the actions. |
281
|
|
|
// Load the listeners if before modules get loaded so that we can capture version changes etc. |
282
|
|
|
add_action( 'init', array( 'Jetpack_Sync_Actions', 'init' ), 90 ); |
283
|
|
|
|
284
|
|
|
// We need to define this here so that it's hooked before `updating_jetpack_version` is called |
285
|
|
|
add_action( 'updating_jetpack_version', array( 'Jetpack_Sync_Actions', 'schedule_initial_sync' ), 10 ); |
286
|
|
|
|
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.