|
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
|
|
|
|
|
13
|
|
|
static function init() { |
|
14
|
|
|
|
|
15
|
|
|
// Add a custom "every minute" cron schedule |
|
16
|
|
|
add_filter( 'cron_schedules', array( __CLASS__, 'minute_cron_schedule' ) ); |
|
17
|
|
|
|
|
18
|
|
|
// On jetpack authorization, schedule a full sync |
|
19
|
|
|
add_action( 'jetpack_client_authorized', array( __CLASS__, 'schedule_full_sync' ) ); |
|
20
|
|
|
|
|
21
|
|
|
// Sync connected user role changes to .com |
|
22
|
|
|
require_once dirname( __FILE__ ) . '/class.jetpack-sync-users.php'; |
|
23
|
|
|
|
|
24
|
|
|
// everything below this point should only happen if we're a valid sync site |
|
25
|
|
|
if ( ! self::sync_allowed() ) { |
|
26
|
|
|
return; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
// cron hooks |
|
30
|
|
|
add_action( 'jetpack_sync_send_db_checksum', array( __CLASS__, 'send_db_checksum' ) ); |
|
31
|
|
|
add_action( 'jetpack_sync_full', array( __CLASS__, 'do_full_sync' ), 10, 1 ); |
|
32
|
|
|
add_action( 'jetpack_sync_cron', array( __CLASS__, 'do_cron_sync' ) ); |
|
33
|
|
|
add_action( 'jetpack_sync_send_pending_data', array( __CLASS__, 'do_send_pending_data' ) ); |
|
34
|
|
|
|
|
35
|
|
|
if ( ! wp_next_scheduled( 'jetpack_sync_send_db_checksum' ) ) { |
|
36
|
|
|
// Schedule a job to send DB checksums once an hour |
|
37
|
|
|
wp_schedule_event( time(), 'hourly', 'jetpack_sync_send_db_checksum' ); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
if ( ! wp_next_scheduled( 'jetpack_sync_cron' ) ) { |
|
41
|
|
|
// Schedule a job to send pending queue items once a minute |
|
42
|
|
|
wp_schedule_event( time(), '1min', 'jetpack_sync_cron' ); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Fires on every request before default loading sync listener code. |
|
47
|
|
|
* Return false to not load sync listener code that monitors common |
|
48
|
|
|
* WP actions to be serialized. |
|
49
|
|
|
* |
|
50
|
|
|
* By default this returns true for non-GET-requests, or requests where the |
|
51
|
|
|
* user is logged-in. |
|
52
|
|
|
* |
|
53
|
|
|
* @since 4.2.0 |
|
54
|
|
|
* |
|
55
|
|
|
* @param bool should we load sync listener code for this request |
|
56
|
|
|
*/ |
|
57
|
|
|
if ( apply_filters( 'jetpack_sync_listener_should_load', |
|
58
|
|
|
( |
|
59
|
|
|
'GET' !== $_SERVER['REQUEST_METHOD'] |
|
60
|
|
|
|| |
|
61
|
|
|
is_user_logged_in() |
|
62
|
|
|
|| |
|
63
|
|
|
defined( 'PHPUNIT_JETPACK_TESTSUITE' ) |
|
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 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
|
|
|
'POST' === $_SERVER['REQUEST_METHOD'] |
|
84
|
|
|
|| |
|
85
|
|
|
current_user_can( 'manage_options' ) |
|
86
|
|
|
|| |
|
87
|
|
|
is_admin() |
|
88
|
|
|
|| |
|
89
|
|
|
defined( 'PHPUNIT_JETPACK_TESTSUITE' ) |
|
90
|
|
|
) |
|
91
|
|
|
) ) { |
|
92
|
|
|
self::initialize_sender(); |
|
93
|
|
|
add_action( 'shutdown', array( self::$sender, 'do_sync' ) ); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
static function sync_allowed() { |
|
99
|
|
|
return ( Jetpack::is_active() && ! ( Jetpack::is_development_mode() || Jetpack::is_staging_site() ) ) |
|
100
|
|
|
|| defined( 'PHPUNIT_JETPACK_TESTSUITE' ); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
static function send_data( $data, $codec_name, $sent_timestamp ) { |
|
104
|
|
|
Jetpack::load_xml_rpc_client(); |
|
105
|
|
|
|
|
106
|
|
|
$url = add_query_arg( array( |
|
107
|
|
|
'sync' => '1', // add an extra parameter to the URL so we can tell it's a sync action |
|
108
|
|
|
'codec' => $codec_name, // send the name of the codec used to encode the data |
|
109
|
|
|
'timestamp' => $sent_timestamp, // send current server time so we can compensate for clock differences |
|
110
|
|
|
), Jetpack::xmlrpc_api_url() ); |
|
111
|
|
|
|
|
112
|
|
|
$rpc = new Jetpack_IXR_Client( array( |
|
113
|
|
|
'url' => $url, |
|
114
|
|
|
'user_id' => JETPACK_MASTER_USER, |
|
115
|
|
|
'timeout' => 30, |
|
116
|
|
|
) ); |
|
117
|
|
|
|
|
118
|
|
|
$result = $rpc->query( 'jetpack.syncActions', $data ); |
|
119
|
|
|
|
|
120
|
|
|
if ( ! $result ) { |
|
121
|
|
|
return $rpc->get_jetpack_error(); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $rpc->getResponse(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
static function schedule_initial_sync() { |
|
128
|
|
|
// we need this function call here because we have to run this function |
|
129
|
|
|
// reeeeally early in init, before WP_CRON_LOCK_TIMEOUT is defined. |
|
130
|
|
|
wp_functionality_constants(); |
|
131
|
|
|
self::schedule_full_sync( array( 'options', 'network_options', 'functions', 'constants' ) ); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
static function schedule_full_sync( $modules = null ) { |
|
135
|
|
|
wp_schedule_single_event( time() + 1, 'jetpack_sync_full', array( $modules ) ); |
|
136
|
|
|
spawn_cron(); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
static function do_full_sync( $modules = null ) { |
|
140
|
|
|
if ( ! self::sync_allowed() ) { |
|
141
|
|
|
return; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
self::initialize_listener(); |
|
145
|
|
|
Jetpack_Sync_Modules::get_module( 'full-sync' )->start( $modules ); |
|
146
|
|
|
self::do_send_pending_data(); // try to send at least some of the data |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
static function minute_cron_schedule( $schedules ) { |
|
150
|
|
|
if( ! isset( $schedules["1min"] ) ) { |
|
151
|
|
|
$schedules["1min"] = array( |
|
152
|
|
|
'interval' => 60, |
|
153
|
|
|
'display' => __( 'Every minute' ) |
|
154
|
|
|
); |
|
155
|
|
|
} |
|
156
|
|
|
return $schedules; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
// try to send actions until we run out of things to send, |
|
160
|
|
|
// or have to wait more than 15s before sending again, |
|
161
|
|
|
// or we hit a lock or some other sending issue |
|
162
|
|
|
static function do_cron_sync() { |
|
163
|
|
|
if ( ! self::sync_allowed() ) { |
|
164
|
|
|
return; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
self::initialize_sender(); |
|
168
|
|
|
|
|
169
|
|
|
do { |
|
170
|
|
|
$next_sync_time = self::$sender->next_sync_time(); |
|
171
|
|
|
|
|
172
|
|
|
if ( $next_sync_time ) { |
|
173
|
|
|
$delay = $next_sync_time - time() + 1; |
|
174
|
|
|
if ( $delay > 15 ) { |
|
175
|
|
|
break; |
|
176
|
|
|
} elseif ( $delay > 0 ) { |
|
177
|
|
|
sleep( $delay ); |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
$result = self::$sender->do_sync(); |
|
182
|
|
|
} while ( $result ); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
static function do_send_pending_data() { |
|
186
|
|
|
self::initialize_sender(); |
|
187
|
|
|
self::$sender->do_sync(); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
static function send_db_checksum() { |
|
191
|
|
|
self::initialize_listener(); |
|
192
|
|
|
self::initialize_sender(); |
|
193
|
|
|
self::$sender->send_checksum(); |
|
194
|
|
|
self::$sender->do_sync(); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
static function initialize_listener() { |
|
198
|
|
|
require_once dirname( __FILE__ ) . '/class.jetpack-sync-listener.php'; |
|
199
|
|
|
self::$listener = Jetpack_Sync_Listener::get_instance(); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
static function initialize_sender() { |
|
203
|
|
|
require_once dirname( __FILE__ ) . '/class.jetpack-sync-sender.php'; |
|
204
|
|
|
self::$sender = Jetpack_Sync_Sender::get_instance(); |
|
205
|
|
|
|
|
206
|
|
|
// bind the sending process |
|
207
|
|
|
add_filter( 'jetpack_sync_send_data', array( __CLASS__, 'send_data' ), 10, 3 ); |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
// Allow other plugins to add filters before we initialize the actions. |
|
212
|
|
|
// Load the listeners if before modules get loaded so that we can capture version changes etc. |
|
213
|
|
|
add_action( 'plugins_loaded', array( 'Jetpack_Sync_Actions', 'init' ), 90 ); |
|
214
|
|
|
|
|
215
|
|
|
// We need to define this here so that it's hooked before `updating_jetpack_version` is called |
|
216
|
|
|
add_action( 'updating_jetpack_version', array( 'Jetpack_Sync_Actions', 'schedule_initial_sync' ), 10 ); |
|
217
|
|
|
|
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.