1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
require_once dirname( __FILE__ ) . '/class.jetpack-sync-queue.php'; |
4
|
|
|
require_once dirname( __FILE__ ) . '/class.jetpack-sync-defaults.php'; |
5
|
|
|
require_once dirname( __FILE__ ) . '/class.jetpack-sync-json-deflate-codec.php'; |
6
|
|
|
require_once dirname( __FILE__ ) . '/class.jetpack-sync-modules.php'; |
7
|
|
|
require_once dirname( __FILE__ ) . '/class.jetpack-sync-settings.php'; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* This class grabs pending actions from the queue and sends them |
11
|
|
|
*/ |
12
|
|
|
class Jetpack_Sync_Sender { |
13
|
|
|
|
14
|
|
|
const SYNC_THROTTLE_OPTION_NAME = 'jetpack_sync_min_wait'; |
15
|
|
|
const LAST_SYNC_TIME_OPTION_NAME = 'jetpack_last_sync_time'; |
16
|
|
|
|
17
|
|
|
private $dequeue_max_bytes; |
18
|
|
|
private $upload_max_bytes; |
19
|
|
|
private $upload_max_rows; |
20
|
|
|
private $sync_wait_time; |
21
|
|
|
private $sync_queue; |
22
|
|
|
private $full_sync_client; |
|
|
|
|
23
|
|
|
private $codec; |
24
|
|
|
|
25
|
|
|
// singleton functions |
26
|
|
|
private static $instance; |
27
|
|
|
|
28
|
|
|
public static function getInstance() { |
29
|
|
|
if ( null === self::$instance ) { |
30
|
|
|
self::$instance = new self(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return self::$instance; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
// this is necessary because you can't use "new" when you declare instance properties >:( |
37
|
|
|
protected function __construct() { |
38
|
|
|
$this->set_defaults(); |
39
|
|
|
$this->init(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
private function init() { |
43
|
|
|
|
44
|
|
|
foreach( Jetpack_Sync_Modules::get_modules() as $module ) { |
45
|
|
|
$module->init_before_send(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Sync all pending actions with server |
50
|
|
|
*/ |
51
|
|
|
add_action( 'jetpack_sync_actions', array( $this, 'do_sync' ) ); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function do_sync() { |
55
|
|
|
// don't sync if importing |
56
|
|
|
if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) { |
57
|
|
|
$this->schedule_sync( "+1 minute" ); |
58
|
|
|
|
59
|
|
|
return false; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// don't sync if we are throttled |
63
|
|
|
$sync_wait = $this->get_sync_wait_time(); |
64
|
|
|
$last_sync = $this->get_last_sync_time(); |
65
|
|
|
|
66
|
|
|
if ( $last_sync && $sync_wait && $last_sync + $sync_wait > microtime( true ) ) { |
67
|
|
|
return false; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->set_last_sync_time(); |
71
|
|
|
|
72
|
|
|
do_action( 'jetpack_sync_before_send' ); |
73
|
|
|
|
74
|
|
|
if ( $this->sync_queue->size() === 0 ) { |
75
|
|
|
return false; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// now that we're sure we are about to sync, try to |
79
|
|
|
// ignore user abort so we can avoid getting into a |
80
|
|
|
// bad state |
81
|
|
|
if ( function_exists( 'ignore_user_abort' ) ) { |
82
|
|
|
ignore_user_abort( true ); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$buffer = $this->sync_queue->checkout_with_memory_limit( $this->dequeue_max_bytes, $this->upload_max_rows ); |
86
|
|
|
|
87
|
|
|
if ( ! $buffer ) { |
88
|
|
|
// buffer has no items |
89
|
|
|
return false; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if ( is_wp_error( $buffer ) ) { |
93
|
|
|
// another buffer is currently sending |
94
|
|
|
return false; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$upload_size = 0; |
98
|
|
|
$items_to_send = array(); |
99
|
|
|
$items = $buffer->get_items(); |
100
|
|
|
|
101
|
|
|
// we estimate the total encoded size as we go by encoding each item individually |
102
|
|
|
// this is expensive, but the only way to really know :/ |
103
|
|
|
foreach ( $items as $key => $item ) { |
104
|
|
|
/** |
105
|
|
|
* Modify the data within an action before it is serialized and sent to the server |
106
|
|
|
* For example, during full sync this expands Post ID's into full Post objects, |
107
|
|
|
* so that we don't have to serialize the whole object into the queue. |
108
|
|
|
* |
109
|
|
|
* @since 4.2.0 |
110
|
|
|
* |
111
|
|
|
* @param array The action parameters |
112
|
|
|
*/ |
113
|
|
|
$item[1] = apply_filters( "jetpack_sync_before_send_" . $item[0], $item[1], $item[2] ); |
114
|
|
|
|
115
|
|
|
$encoded_item = $this->codec->encode( $item ); |
116
|
|
|
|
117
|
|
|
$upload_size += strlen( $encoded_item ); |
118
|
|
|
|
119
|
|
|
if ( $upload_size > $this->upload_max_bytes && count( $items_to_send ) > 0 ) { |
120
|
|
|
break; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$items_to_send[ $key ] = $encoded_item; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Fires when data is ready to send to the server. |
128
|
|
|
* Return false or WP_Error to abort the sync (e.g. if there's an error) |
129
|
|
|
* The items will be automatically re-sent later |
130
|
|
|
* |
131
|
|
|
* @since 4.2.0 |
132
|
|
|
* |
133
|
|
|
* @param array $data The action buffer |
134
|
|
|
*/ |
135
|
|
|
$processed_item_ids = apply_filters( 'jetpack_sync_send_data', $items_to_send, $this->codec->name(), microtime( true ) ); |
136
|
|
|
|
137
|
|
|
if ( ! $processed_item_ids || is_wp_error( $processed_item_ids ) ) { |
138
|
|
|
$processed_item_ids = $this->sync_queue->checkin( $buffer ); |
139
|
|
|
|
140
|
|
|
if ( is_wp_error( $processed_item_ids ) ) { |
141
|
|
|
error_log( "Error checking in buffer: " . $processed_item_ids->get_error_message() ); |
142
|
|
|
$this->sync_queue->force_checkin(); |
143
|
|
|
} |
144
|
|
|
// try again in 1 minute |
145
|
|
|
$this->schedule_sync( "+1 minute" ); |
146
|
|
|
} else { |
147
|
|
|
$processed_items = array_intersect_key( $items, array_flip( $processed_item_ids ) ); |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Allows us to keep track of all the actions that have been sent. |
151
|
|
|
* Allows us to calculate the progress of specific actions. |
152
|
|
|
* |
153
|
|
|
* @since 4.2.0 |
154
|
|
|
* |
155
|
|
|
* @param array $processed_actions The actions that we send successfully. |
156
|
|
|
*/ |
157
|
|
|
do_action( 'jetpack_sync_processed_actions', $processed_items ); |
158
|
|
|
|
159
|
|
|
$this->sync_queue->close( $buffer, $processed_item_ids ); |
160
|
|
|
// check if there are any more events in the buffer |
161
|
|
|
// if so, schedule a cron job to happen soon |
162
|
|
|
if ( $this->sync_queue->has_any_items() ) { |
163
|
|
|
$this->schedule_sync( "+1 minute" ); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
private function schedule_sync( $when ) { |
169
|
|
|
wp_schedule_single_event( strtotime( $when ), 'jetpack_sync_actions' ); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
function get_sync_queue() { |
173
|
|
|
return $this->sync_queue; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
function get_codec() { |
177
|
|
|
return $this->codec; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
function send_checksum() { |
181
|
|
|
require_once 'class.jetpack-sync-wp-replicastore.php'; |
182
|
|
|
$store = new Jetpack_Sync_WP_Replicastore(); |
183
|
|
|
do_action( 'jetpack_sync_checksum', $store->checksum_all() ); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
function reset_sync_queue() { |
187
|
|
|
Jetpack_Sync_Modules::get_module( 'full-sync' )->clear_status(); |
188
|
|
|
$this->sync_queue->reset(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
function set_dequeue_max_bytes( $size ) { |
192
|
|
|
$this->dequeue_max_bytes = $size; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
// in bytes |
196
|
|
|
function set_upload_max_bytes( $max_bytes ) { |
197
|
|
|
$this->upload_max_bytes = $max_bytes; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
// in rows |
201
|
|
|
function set_upload_max_rows( $max_rows ) { |
202
|
|
|
$this->upload_max_rows = $max_rows; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
// in seconds |
206
|
|
|
function set_sync_wait_time( $seconds ) { |
207
|
|
|
$this->sync_wait_time = $seconds; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
function get_sync_wait_time() { |
211
|
|
|
return $this->sync_wait_time; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
private function get_last_sync_time() { |
215
|
|
|
return (double) get_option( self::LAST_SYNC_TIME_OPTION_NAME ); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
private function set_last_sync_time() { |
219
|
|
|
return update_option( self::LAST_SYNC_TIME_OPTION_NAME, microtime( true ), true ); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
function set_defaults() { |
223
|
|
|
$this->sync_queue = new Jetpack_Sync_Queue( 'sync' ); |
224
|
|
|
$this->codec = new Jetpack_Sync_JSON_Deflate_Codec(); |
225
|
|
|
|
226
|
|
|
// saved settings |
227
|
|
|
$settings = Jetpack_Sync_Settings::get_settings(); |
228
|
|
|
$this->set_dequeue_max_bytes( $settings['dequeue_max_bytes'] ); |
229
|
|
|
$this->set_upload_max_bytes( $settings['upload_max_bytes'] ); |
230
|
|
|
$this->set_upload_max_rows( $settings['upload_max_rows'] ); |
231
|
|
|
$this->set_sync_wait_time( $settings['sync_wait_time'] ); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
function reset_data() { |
235
|
|
|
$this->reset_sync_queue(); |
236
|
|
|
|
237
|
|
|
foreach( Jetpack_Sync_Modules::get_modules() as $module ) { |
238
|
|
|
$module->reset_data(); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
delete_option( self::SYNC_THROTTLE_OPTION_NAME ); |
242
|
|
|
delete_option( self::LAST_SYNC_TIME_OPTION_NAME ); |
243
|
|
|
|
244
|
|
|
Jetpack_Sync_Settings::reset_data(); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
function uninstall() { |
248
|
|
|
// Lets delete all the other fun stuff like transient and option and the sync queue |
249
|
|
|
$this->reset_data(); |
250
|
|
|
|
251
|
|
|
// delete the full sync status |
252
|
|
|
delete_option( 'jetpack_full_sync_status' ); |
253
|
|
|
|
254
|
|
|
// clear the sync cron. |
255
|
|
|
wp_clear_scheduled_hook( 'jetpack_sync_actions' ); |
256
|
|
|
|
257
|
|
|
// clear the checksum cron |
258
|
|
|
wp_clear_scheduled_hook( 'jetpack_send_db_checksum' ); |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.