1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Sync sender. |
4
|
|
|
* |
5
|
|
|
* @package automattic/jetpack-sync |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack\Sync; |
9
|
|
|
|
10
|
|
|
use Automattic\Jetpack\Constants; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* This class grabs pending actions from the queue and sends them |
14
|
|
|
*/ |
15
|
|
|
class Sender { |
16
|
|
|
/** |
17
|
|
|
* Name of the option that stores the time of the next sync. |
18
|
|
|
* |
19
|
|
|
* @access public |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
const NEXT_SYNC_TIME_OPTION_NAME = 'jetpack_next_sync_time'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Sync timeout after a WPCOM error. |
27
|
|
|
* |
28
|
|
|
* @access public |
29
|
|
|
* |
30
|
|
|
* @var int |
31
|
|
|
*/ |
32
|
|
|
const WPCOM_ERROR_SYNC_DELAY = 60; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Sync timeout after a queue has been locked. |
36
|
|
|
* |
37
|
|
|
* @access public |
38
|
|
|
* |
39
|
|
|
* @var int |
40
|
|
|
*/ |
41
|
|
|
const QUEUE_LOCKED_SYNC_DELAY = 10; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Maximum bytes to checkout without exceeding the memory limit. |
45
|
|
|
* |
46
|
|
|
* @access private |
47
|
|
|
* |
48
|
|
|
* @var int |
49
|
|
|
*/ |
50
|
|
|
private $dequeue_max_bytes; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Maximum bytes in a single encoded item. |
54
|
|
|
* |
55
|
|
|
* @access private |
56
|
|
|
* |
57
|
|
|
* @var int |
58
|
|
|
*/ |
59
|
|
|
private $upload_max_bytes; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Maximum number of sync items in a single action. |
63
|
|
|
* |
64
|
|
|
* @access private |
65
|
|
|
* |
66
|
|
|
* @var int |
67
|
|
|
*/ |
68
|
|
|
private $upload_max_rows; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Maximum time for perfirming a checkout of items from the queue (in seconds). |
72
|
|
|
* |
73
|
|
|
* @access private |
74
|
|
|
* |
75
|
|
|
* @var int |
76
|
|
|
*/ |
77
|
|
|
private $max_dequeue_time; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* How many seconds to wait after sending sync items after exceeding the sync wait threshold (in seconds). |
81
|
|
|
* |
82
|
|
|
* @access private |
83
|
|
|
* |
84
|
|
|
* @var int |
85
|
|
|
*/ |
86
|
|
|
private $sync_wait_time; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* How much maximum time to wait for the checkout to finish (in seconds). |
90
|
|
|
* |
91
|
|
|
* @access private |
92
|
|
|
* |
93
|
|
|
* @var int |
94
|
|
|
*/ |
95
|
|
|
private $sync_wait_threshold; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* How much maximum time to wait for the sync items to be queued for sending (in seconds). |
99
|
|
|
* |
100
|
|
|
* @access private |
101
|
|
|
* |
102
|
|
|
* @var int |
103
|
|
|
*/ |
104
|
|
|
private $enqueue_wait_time; |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Incremental sync queue object. |
108
|
|
|
* |
109
|
|
|
* @access private |
110
|
|
|
* |
111
|
|
|
* @var Automattic\Jetpack\Sync\Queue |
112
|
|
|
*/ |
113
|
|
|
private $sync_queue; |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Full sync queue object. |
117
|
|
|
* |
118
|
|
|
* @access private |
119
|
|
|
* |
120
|
|
|
* @var Automattic\Jetpack\Sync\Queue |
121
|
|
|
*/ |
122
|
|
|
private $full_sync_queue; |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Codec object for encoding and decoding sync items. |
126
|
|
|
* |
127
|
|
|
* @access private |
128
|
|
|
* |
129
|
|
|
* @var Automattic\Jetpack\Sync\Codec_Interface |
130
|
|
|
*/ |
131
|
|
|
private $codec; |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* The current user before we change or clear it. |
135
|
|
|
* |
136
|
|
|
* @access private |
137
|
|
|
* |
138
|
|
|
* @var \WP_User |
139
|
|
|
*/ |
140
|
|
|
private $old_user; |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Container for the singleton instance of this class. |
144
|
|
|
* |
145
|
|
|
* @access private |
146
|
|
|
* @static |
147
|
|
|
* |
148
|
|
|
* @var Automattic\Jetpack\Sync\Sender |
149
|
|
|
*/ |
150
|
|
|
private static $instance; |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Retrieve the singleton instance of this class. |
154
|
|
|
* |
155
|
|
|
* @access public |
156
|
|
|
* @static |
157
|
|
|
* |
158
|
|
|
* @return Automattic\Jetpack\Sync\Sender |
159
|
|
|
*/ |
160
|
|
|
public static function get_instance() { |
161
|
|
|
if ( null === self::$instance ) { |
162
|
|
|
self::$instance = new self(); |
|
|
|
|
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return self::$instance; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Constructor. |
170
|
|
|
* This is necessary because you can't use "new" when you declare instance properties >:( |
171
|
|
|
* |
172
|
|
|
* @access protected |
173
|
|
|
* @static |
174
|
|
|
*/ |
175
|
|
|
protected function __construct() { |
176
|
|
|
$this->set_defaults(); |
177
|
|
|
$this->init(); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Initialize the sender. |
182
|
|
|
* Prepares the current user and initializes all sync modules. |
183
|
|
|
* |
184
|
|
|
* @access private |
185
|
|
|
*/ |
186
|
|
|
private function init() { |
187
|
|
|
add_action( 'jetpack_sync_before_send_queue_sync', array( $this, 'maybe_set_user_from_token' ), 1 ); |
188
|
|
|
add_action( 'jetpack_sync_before_send_queue_sync', array( $this, 'maybe_clear_user_from_token' ), 20 ); |
189
|
|
|
add_filter( 'jetpack_xmlrpc_methods', array( $this, 'register_jetpack_xmlrpc_methods' ) ); |
190
|
|
|
foreach ( Modules::get_modules() as $module ) { |
|
|
|
|
191
|
|
|
$module->init_before_send(); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Detect if this is a XMLRPC request with a valid signature. |
197
|
|
|
* If so, changes the user to the new one. |
198
|
|
|
* |
199
|
|
|
* @access public |
200
|
|
|
*/ |
201
|
|
|
public function maybe_set_user_from_token() { |
202
|
|
|
$verified_user = \Jetpack::connection()->verify_xml_rpc_signature(); |
203
|
|
|
if ( Constants::is_true( 'XMLRPC_REQUEST' ) && |
204
|
|
|
! is_wp_error( $verified_user ) |
205
|
|
|
&& $verified_user |
206
|
|
|
) { |
207
|
|
|
$old_user = wp_get_current_user(); |
208
|
|
|
$this->old_user = isset( $old_user->ID ) ? $old_user->ID : 0; |
209
|
|
|
wp_set_current_user( $verified_user['user_id'] ); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* If we used to have a previous current user, revert back to it. |
215
|
|
|
* |
216
|
|
|
* @access public |
217
|
|
|
*/ |
218
|
|
|
public function maybe_clear_user_from_token() { |
219
|
|
|
if ( isset( $this->old_user ) ) { |
220
|
|
|
wp_set_current_user( $this->old_user ); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Retrieve the next sync time. |
226
|
|
|
* |
227
|
|
|
* @access public |
228
|
|
|
* |
229
|
|
|
* @param string $queue_name Name of the queue. |
230
|
|
|
* @return float Timestamp of the next sync. |
231
|
|
|
*/ |
232
|
|
|
public function get_next_sync_time( $queue_name ) { |
233
|
|
|
return (float) get_option( self::NEXT_SYNC_TIME_OPTION_NAME . '_' . $queue_name, 0 ); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Set the next sync time. |
238
|
|
|
* |
239
|
|
|
* @access public |
240
|
|
|
* |
241
|
|
|
* @param int $time Timestamp of the next sync. |
242
|
|
|
* @param string $queue_name Name of the queue. |
243
|
|
|
* @return boolean True if update was successful, false otherwise. |
244
|
|
|
*/ |
245
|
|
|
public function set_next_sync_time( $time, $queue_name ) { |
246
|
|
|
return update_option( self::NEXT_SYNC_TIME_OPTION_NAME . '_' . $queue_name, $time, true ); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Trigger a full sync. |
251
|
|
|
* |
252
|
|
|
* @access public |
253
|
|
|
* |
254
|
|
|
* @return boolean|\WP_Error True if this sync sending was successful, error object otherwise. |
255
|
|
|
*/ |
256
|
|
|
public function do_full_sync() { |
257
|
|
|
if ( ! Modules::get_module( 'full-sync' ) ) { |
258
|
|
|
return; |
259
|
|
|
} |
260
|
|
|
$this->continue_full_sync_enqueue(); |
261
|
|
|
return $this->do_sync_and_set_delays( $this->full_sync_queue ); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Enqueue the next sync items for sending. |
266
|
|
|
* Will not be done if the current request is a WP import one. |
267
|
|
|
* Will be delayed until the next sync time comes. |
268
|
|
|
* |
269
|
|
|
* @access private |
270
|
|
|
*/ |
271
|
|
|
private function continue_full_sync_enqueue() { |
272
|
|
|
if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) { |
273
|
|
|
return false; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
if ( $this->get_next_sync_time( 'full-sync-enqueue' ) > microtime( true ) ) { |
277
|
|
|
return false; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
Modules::get_module( 'full-sync' )->continue_enqueuing(); |
281
|
|
|
|
282
|
|
|
$this->set_next_sync_time( time() + $this->get_enqueue_wait_time(), 'full-sync-enqueue' ); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Trigger incremental sync. |
287
|
|
|
* |
288
|
|
|
* @access public |
289
|
|
|
* |
290
|
|
|
* @return boolean|\WP_Error True if this sync sending was successful, error object otherwise. |
291
|
|
|
*/ |
292
|
|
|
public function do_sync() { |
293
|
|
|
return $this->do_sync_and_set_delays( $this->sync_queue ); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Trigger sync for a certain sync queue. |
298
|
|
|
* Responsible for setting next sync time. |
299
|
|
|
* Will not be delayed if the current request is a WP import one. |
300
|
|
|
* Will be delayed until the next sync time comes. |
301
|
|
|
* |
302
|
|
|
* @access public |
303
|
|
|
* |
304
|
|
|
* @param Automattic\Jetpack\Sync\Queue $queue Queue object. |
305
|
|
|
* @return boolean|\WP_Error True if this sync sending was successful, error object otherwise. |
306
|
|
|
*/ |
307
|
|
|
public function do_sync_and_set_delays( $queue ) { |
308
|
|
|
// Don't sync if importing. |
309
|
|
|
if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) { |
310
|
|
|
return new \WP_Error( 'is_importing' ); |
|
|
|
|
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
// Don't sync if we are throttled. |
314
|
|
|
if ( $this->get_next_sync_time( $queue->id ) > microtime( true ) ) { |
315
|
|
|
return new \WP_Error( 'sync_throttled' ); |
|
|
|
|
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
$start_time = microtime( true ); |
319
|
|
|
|
320
|
|
|
Settings::set_is_syncing( true ); |
321
|
|
|
|
322
|
|
|
$sync_result = $this->do_sync_for_queue( $queue ); |
323
|
|
|
|
324
|
|
|
Settings::set_is_syncing( false ); |
325
|
|
|
|
326
|
|
|
$exceeded_sync_wait_threshold = ( microtime( true ) - $start_time ) > (float) $this->get_sync_wait_threshold(); |
327
|
|
|
|
328
|
|
|
if ( is_wp_error( $sync_result ) ) { |
329
|
|
|
if ( 'unclosed_buffer' === $sync_result->get_error_code() ) { |
|
|
|
|
330
|
|
|
$this->set_next_sync_time( time() + self::QUEUE_LOCKED_SYNC_DELAY, $queue->id ); |
331
|
|
|
} |
332
|
|
|
if ( 'wpcom_error' === $sync_result->get_error_code() ) { |
|
|
|
|
333
|
|
|
$this->set_next_sync_time( time() + self::WPCOM_ERROR_SYNC_DELAY, $queue->id ); |
334
|
|
|
} |
335
|
|
|
} elseif ( $exceeded_sync_wait_threshold ) { |
336
|
|
|
// If we actually sent data and it took a while, wait before sending again. |
337
|
|
|
$this->set_next_sync_time( time() + $this->get_sync_wait_time(), $queue->id ); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
return $sync_result; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Retrieve the next sync items to send. |
345
|
|
|
* |
346
|
|
|
* @access public |
347
|
|
|
* |
348
|
|
|
* @param Automattic\Jetpack\Sync\Queue_Buffer $buffer Queue buffer object. |
349
|
|
|
* @param boolean $encode Whether to encode the items. |
350
|
|
|
* @return array Sync items to send. |
351
|
|
|
*/ |
352
|
|
|
public function get_items_to_send( $buffer, $encode = true ) { |
353
|
|
|
// Track how long we've been processing so we can avoid request timeouts. |
354
|
|
|
$start_time = microtime( true ); |
355
|
|
|
$upload_size = 0; |
356
|
|
|
$items_to_send = array(); |
357
|
|
|
$items = $buffer->get_items(); |
358
|
|
|
// Set up current screen to avoid errors rendering content. |
359
|
|
|
require_once ABSPATH . 'wp-admin/includes/class-wp-screen.php'; |
360
|
|
|
require_once ABSPATH . 'wp-admin/includes/screen.php'; |
361
|
|
|
set_current_screen( 'sync' ); |
362
|
|
|
$skipped_items_ids = array(); |
363
|
|
|
/** |
364
|
|
|
* We estimate the total encoded size as we go by encoding each item individually. |
365
|
|
|
* This is expensive, but the only way to really know :/ |
366
|
|
|
*/ |
367
|
|
|
foreach ( $items as $key => $item ) { |
368
|
|
|
// Suspending cache addition help prevent overloading in memory cache of large sites. |
369
|
|
|
wp_suspend_cache_addition( true ); |
370
|
|
|
/** |
371
|
|
|
* Modify the data within an action before it is serialized and sent to the server |
372
|
|
|
* For example, during full sync this expands Post ID's into full Post objects, |
373
|
|
|
* so that we don't have to serialize the whole object into the queue. |
374
|
|
|
* |
375
|
|
|
* @since 4.2.0 |
376
|
|
|
* |
377
|
|
|
* @param array The action parameters |
378
|
|
|
* @param int The ID of the user who triggered the action |
379
|
|
|
*/ |
380
|
|
|
$item[1] = apply_filters( 'jetpack_sync_before_send_' . $item[0], $item[1], $item[2] ); |
381
|
|
|
wp_suspend_cache_addition( false ); |
382
|
|
|
if ( false === $item[1] ) { |
383
|
|
|
$skipped_items_ids[] = $key; |
384
|
|
|
continue; |
385
|
|
|
} |
386
|
|
|
$encoded_item = $encode ? $this->codec->encode( $item ) : $item; |
387
|
|
|
$upload_size += strlen( $encoded_item ); |
388
|
|
|
if ( $upload_size > $this->upload_max_bytes && count( $items_to_send ) > 0 ) { |
389
|
|
|
break; |
390
|
|
|
} |
391
|
|
|
$items_to_send[ $key ] = $encoded_item; |
392
|
|
|
if ( microtime( true ) - $start_time > $this->max_dequeue_time ) { |
393
|
|
|
break; |
394
|
|
|
} |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
return array( $items_to_send, $skipped_items_ids, $items, microtime( true ) - $start_time ); |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* If supported, flush all response data to the client and finish the request. |
402
|
|
|
* This allows for time consuming tasks to be performed without leaving the connection open. |
403
|
|
|
* |
404
|
|
|
* @access private |
405
|
|
|
*/ |
406
|
|
|
private function fastcgi_finish_request() { |
407
|
|
|
if ( function_exists( 'fastcgi_finish_request' ) && version_compare( phpversion(), '7.0.16', '>=' ) ) { |
408
|
|
|
fastcgi_finish_request(); |
409
|
|
|
} |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* Perform sync for a certain sync queue. |
414
|
|
|
* |
415
|
|
|
* @access public |
416
|
|
|
* |
417
|
|
|
* @param Automattic\Jetpack\Sync\Queue $queue Queue object. |
418
|
|
|
* @return boolean|\WP_Error True if this sync sending was successful, error object otherwise. |
419
|
|
|
*/ |
420
|
|
|
public function do_sync_for_queue( $queue ) { |
421
|
|
|
do_action( 'jetpack_sync_before_send_queue_' . $queue->id ); |
422
|
|
|
if ( $queue->size() === 0 ) { |
423
|
|
|
return new \WP_Error( 'empty_queue_' . $queue->id ); |
|
|
|
|
424
|
|
|
} |
425
|
|
|
/** |
426
|
|
|
* Now that we're sure we are about to sync, try to ignore user abort |
427
|
|
|
* so we can avoid getting into a bad state. |
428
|
|
|
*/ |
429
|
|
|
if ( function_exists( 'ignore_user_abort' ) ) { |
430
|
|
|
ignore_user_abort( true ); |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
/* Don't make the request block till we finish, if possible. */ |
434
|
|
|
if ( Constants::is_true( 'REST_REQUEST' ) || Constants::is_true( 'XMLRPC_REQUEST' ) ) { |
435
|
|
|
$this->fastcgi_finish_request(); |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
$checkout_start_time = microtime( true ); |
439
|
|
|
|
440
|
|
|
$buffer = $queue->checkout_with_memory_limit( $this->dequeue_max_bytes, $this->upload_max_rows ); |
441
|
|
|
|
442
|
|
|
if ( ! $buffer ) { |
443
|
|
|
// Buffer has no items. |
444
|
|
|
return new \WP_Error( 'empty_buffer' ); |
|
|
|
|
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
if ( is_wp_error( $buffer ) ) { |
448
|
|
|
return $buffer; |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
$checkout_duration = microtime( true ) - $checkout_start_time; |
452
|
|
|
|
453
|
|
|
list( $items_to_send, $skipped_items_ids, $items, $preprocess_duration ) = $this->get_items_to_send( $buffer, true ); |
454
|
|
|
if ( ! empty( $items_to_send ) ) { |
455
|
|
|
/** |
456
|
|
|
* Fires when data is ready to send to the server. |
457
|
|
|
* Return false or WP_Error to abort the sync (e.g. if there's an error) |
458
|
|
|
* The items will be automatically re-sent later |
459
|
|
|
* |
460
|
|
|
* @since 4.2.0 |
461
|
|
|
* |
462
|
|
|
* @param array $data The action buffer |
463
|
|
|
* @param string $codec The codec name used to encode the data |
464
|
|
|
* @param double $time The current time |
465
|
|
|
* @param string $queue The queue used to send ('sync' or 'full_sync') |
466
|
|
|
*/ |
467
|
|
|
Settings::set_is_sending( true ); |
468
|
|
|
$processed_item_ids = apply_filters( 'jetpack_sync_send_data', $items_to_send, $this->codec->name(), microtime( true ), $queue->id, $checkout_duration, $preprocess_duration ); |
469
|
|
|
Settings::set_is_sending( false ); |
470
|
|
|
} else { |
471
|
|
|
$processed_item_ids = $skipped_items_ids; |
472
|
|
|
$skipped_items_ids = array(); |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
if ( ! $processed_item_ids || is_wp_error( $processed_item_ids ) ) { |
476
|
|
|
$checked_in_item_ids = $queue->checkin( $buffer ); |
477
|
|
|
if ( is_wp_error( $checked_in_item_ids ) ) { |
478
|
|
|
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
479
|
|
|
error_log( 'Error checking in buffer: ' . $checked_in_item_ids->get_error_message() ); |
480
|
|
|
$queue->force_checkin(); |
481
|
|
|
} |
482
|
|
|
if ( is_wp_error( $processed_item_ids ) ) { |
483
|
|
|
return new \WP_Error( 'wpcom_error', $processed_item_ids->get_error_code() ); |
|
|
|
|
484
|
|
|
} |
485
|
|
|
// Returning a wpcom_error is a sign to the caller that we should wait a while before syncing again. |
486
|
|
|
return new \WP_Error( 'wpcom_error', 'jetpack_sync_send_data_false' ); |
|
|
|
|
487
|
|
|
} else { |
488
|
|
|
// Detect if the last item ID was an error. |
489
|
|
|
$had_wp_error = is_wp_error( end( $processed_item_ids ) ); |
490
|
|
|
if ( $had_wp_error ) { |
491
|
|
|
$wp_error = array_pop( $processed_item_ids ); |
492
|
|
|
} |
493
|
|
|
// Also checkin any items that were skipped. |
494
|
|
|
if ( count( $skipped_items_ids ) > 0 ) { |
495
|
|
|
$processed_item_ids = array_merge( $processed_item_ids, $skipped_items_ids ); |
496
|
|
|
} |
497
|
|
|
$processed_items = array_intersect_key( $items, array_flip( $processed_item_ids ) ); |
498
|
|
|
/** |
499
|
|
|
* Allows us to keep track of all the actions that have been sent. |
500
|
|
|
* Allows us to calculate the progress of specific actions. |
501
|
|
|
* |
502
|
|
|
* @since 4.2.0 |
503
|
|
|
* |
504
|
|
|
* @param array $processed_actions The actions that we send successfully. |
505
|
|
|
*/ |
506
|
|
|
do_action( 'jetpack_sync_processed_actions', $processed_items ); |
507
|
|
|
$queue->close( $buffer, $processed_item_ids ); |
508
|
|
|
// Returning a WP_Error is a sign to the caller that we should wait a while before syncing again. |
509
|
|
|
if ( $had_wp_error ) { |
510
|
|
|
return new \WP_Error( 'wpcom_error', $wp_error->get_error_code() ); |
|
|
|
|
511
|
|
|
} |
512
|
|
|
} |
513
|
|
|
return true; |
514
|
|
|
} |
515
|
|
|
|
516
|
|
|
/** |
517
|
|
|
* Returns any object that is able to be synced. |
518
|
|
|
* |
519
|
|
|
* @access public |
520
|
|
|
* |
521
|
|
|
* @param array $args the synchronized object parameters. |
522
|
|
|
* @return string Encoded sync object. |
523
|
|
|
*/ |
524
|
|
|
public function sync_object( $args ) { |
525
|
|
|
// For example: posts, post, 5. |
526
|
|
|
list( $module_name, $object_type, $id ) = $args; |
527
|
|
|
|
528
|
|
|
$sync_module = Modules::get_module( $module_name ); |
529
|
|
|
$codec = $this->get_codec(); |
530
|
|
|
|
531
|
|
|
return $codec->encode( $sync_module->get_object_by_id( $object_type, $id ) ); |
532
|
|
|
} |
533
|
|
|
|
534
|
|
|
/** |
535
|
|
|
* Register additional sync XML-RPC methods available to Jetpack for authenticated users. |
536
|
|
|
* |
537
|
|
|
* @access public |
538
|
|
|
* @since 7.8 |
539
|
|
|
* |
540
|
|
|
* @param array $jetpack_methods XML-RPC methods available to the Jetpack Server. |
541
|
|
|
* @return array Filtered XML-RPC methods. |
542
|
|
|
*/ |
543
|
|
|
public function register_jetpack_xmlrpc_methods( $jetpack_methods ) { |
544
|
|
|
$jetpack_methods['jetpack.syncObject'] = array( $this, 'sync_object' ); |
545
|
|
|
return $jetpack_methods; |
546
|
|
|
} |
547
|
|
|
|
548
|
|
|
/** |
549
|
|
|
* Get the incremental sync queue object. |
550
|
|
|
* |
551
|
|
|
* @access public |
552
|
|
|
* |
553
|
|
|
* @return Automattic\Jetpack\Sync\Queue Queue object. |
554
|
|
|
*/ |
555
|
|
|
public function get_sync_queue() { |
556
|
|
|
return $this->sync_queue; |
557
|
|
|
} |
558
|
|
|
|
559
|
|
|
/** |
560
|
|
|
* Get the full sync queue object. |
561
|
|
|
* |
562
|
|
|
* @access public |
563
|
|
|
* |
564
|
|
|
* @return Automattic\Jetpack\Sync\Queue Queue object. |
565
|
|
|
*/ |
566
|
|
|
public function get_full_sync_queue() { |
567
|
|
|
return $this->full_sync_queue; |
568
|
|
|
} |
569
|
|
|
|
570
|
|
|
/** |
571
|
|
|
* Get the codec object. |
572
|
|
|
* |
573
|
|
|
* @access public |
574
|
|
|
* |
575
|
|
|
* @return Automattic\Jetpack\Sync\Codec_Interface Codec object. |
576
|
|
|
*/ |
577
|
|
|
public function get_codec() { |
578
|
|
|
return $this->codec; |
579
|
|
|
} |
580
|
|
|
|
581
|
|
|
/** |
582
|
|
|
* Determine the codec object. |
583
|
|
|
* Use gzip deflate if supported. |
584
|
|
|
* |
585
|
|
|
* @access public |
586
|
|
|
*/ |
587
|
|
|
public function set_codec() { |
588
|
|
|
if ( function_exists( 'gzinflate' ) ) { |
589
|
|
|
$this->codec = new JSON_Deflate_Array_Codec(); |
|
|
|
|
590
|
|
|
} else { |
591
|
|
|
$this->codec = new Simple_Codec(); |
|
|
|
|
592
|
|
|
} |
593
|
|
|
} |
594
|
|
|
|
595
|
|
|
/** |
596
|
|
|
* Compute and send all the checksums. |
597
|
|
|
* |
598
|
|
|
* @access public |
599
|
|
|
*/ |
600
|
|
|
public function send_checksum() { |
601
|
|
|
$store = new Replicastore(); |
602
|
|
|
do_action( 'jetpack_sync_checksum', $store->checksum_all() ); |
603
|
|
|
} |
604
|
|
|
|
605
|
|
|
/** |
606
|
|
|
* Reset the incremental sync queue. |
607
|
|
|
* |
608
|
|
|
* @access public |
609
|
|
|
*/ |
610
|
|
|
public function reset_sync_queue() { |
611
|
|
|
$this->sync_queue->reset(); |
612
|
|
|
} |
613
|
|
|
|
614
|
|
|
/** |
615
|
|
|
* Reset the full sync queue. |
616
|
|
|
* |
617
|
|
|
* @access public |
618
|
|
|
*/ |
619
|
|
|
public function reset_full_sync_queue() { |
620
|
|
|
$this->full_sync_queue->reset(); |
621
|
|
|
} |
622
|
|
|
|
623
|
|
|
/** |
624
|
|
|
* Set the maximum bytes to checkout without exceeding the memory limit. |
625
|
|
|
* |
626
|
|
|
* @access public |
627
|
|
|
* |
628
|
|
|
* @param int $size Maximum bytes to checkout. |
629
|
|
|
*/ |
630
|
|
|
public function set_dequeue_max_bytes( $size ) { |
631
|
|
|
$this->dequeue_max_bytes = $size; |
632
|
|
|
} |
633
|
|
|
|
634
|
|
|
/** |
635
|
|
|
* Set the maximum bytes in a single encoded item. |
636
|
|
|
* |
637
|
|
|
* @access public |
638
|
|
|
* |
639
|
|
|
* @param int $max_bytes Maximum bytes in a single encoded item. |
640
|
|
|
*/ |
641
|
|
|
public function set_upload_max_bytes( $max_bytes ) { |
642
|
|
|
$this->upload_max_bytes = $max_bytes; |
643
|
|
|
} |
644
|
|
|
|
645
|
|
|
/** |
646
|
|
|
* Set the maximum number of sync items in a single action. |
647
|
|
|
* |
648
|
|
|
* @access public |
649
|
|
|
* |
650
|
|
|
* @param int $max_rows Maximum number of sync items. |
651
|
|
|
*/ |
652
|
|
|
public function set_upload_max_rows( $max_rows ) { |
653
|
|
|
$this->upload_max_rows = $max_rows; |
654
|
|
|
} |
655
|
|
|
|
656
|
|
|
/** |
657
|
|
|
* Set the sync wait time (in seconds). |
658
|
|
|
* |
659
|
|
|
* @access public |
660
|
|
|
* |
661
|
|
|
* @param int $seconds Sync wait time. |
662
|
|
|
*/ |
663
|
|
|
public function set_sync_wait_time( $seconds ) { |
664
|
|
|
$this->sync_wait_time = $seconds; |
665
|
|
|
} |
666
|
|
|
|
667
|
|
|
/** |
668
|
|
|
* Get current sync wait time (in seconds). |
669
|
|
|
* |
670
|
|
|
* @access public |
671
|
|
|
* |
672
|
|
|
* @return int Sync wait time. |
673
|
|
|
*/ |
674
|
|
|
public function get_sync_wait_time() { |
675
|
|
|
return $this->sync_wait_time; |
676
|
|
|
} |
677
|
|
|
|
678
|
|
|
/** |
679
|
|
|
* Set the enqueue wait time (in seconds). |
680
|
|
|
* |
681
|
|
|
* @access public |
682
|
|
|
* |
683
|
|
|
* @param int $seconds Enqueue wait time. |
684
|
|
|
*/ |
685
|
|
|
public function set_enqueue_wait_time( $seconds ) { |
686
|
|
|
$this->enqueue_wait_time = $seconds; |
687
|
|
|
} |
688
|
|
|
|
689
|
|
|
/** |
690
|
|
|
* Get current enqueue wait time (in seconds). |
691
|
|
|
* |
692
|
|
|
* @access public |
693
|
|
|
* |
694
|
|
|
* @return int Enqueue wait time. |
695
|
|
|
*/ |
696
|
|
|
public function get_enqueue_wait_time() { |
697
|
|
|
return $this->enqueue_wait_time; |
698
|
|
|
} |
699
|
|
|
|
700
|
|
|
/** |
701
|
|
|
* Set the sync wait threshold (in seconds). |
702
|
|
|
* |
703
|
|
|
* @access public |
704
|
|
|
* |
705
|
|
|
* @param int $seconds Sync wait threshold. |
706
|
|
|
*/ |
707
|
|
|
public function set_sync_wait_threshold( $seconds ) { |
708
|
|
|
$this->sync_wait_threshold = $seconds; |
709
|
|
|
} |
710
|
|
|
|
711
|
|
|
/** |
712
|
|
|
* Get current sync wait threshold (in seconds). |
713
|
|
|
* |
714
|
|
|
* @access public |
715
|
|
|
* |
716
|
|
|
* @return int Sync wait threshold. |
717
|
|
|
*/ |
718
|
|
|
public function get_sync_wait_threshold() { |
719
|
|
|
return $this->sync_wait_threshold; |
720
|
|
|
} |
721
|
|
|
|
722
|
|
|
/** |
723
|
|
|
* Set the maximum time for perfirming a checkout of items from the queue (in seconds). |
724
|
|
|
* |
725
|
|
|
* @access public |
726
|
|
|
* |
727
|
|
|
* @param int $seconds Maximum dequeue time. |
728
|
|
|
*/ |
729
|
|
|
public function set_max_dequeue_time( $seconds ) { |
730
|
|
|
$this->max_dequeue_time = $seconds; |
731
|
|
|
} |
732
|
|
|
|
733
|
|
|
/** |
734
|
|
|
* Initialize the sync queues, codec and set the default settings. |
735
|
|
|
* |
736
|
|
|
* @access public |
737
|
|
|
*/ |
738
|
|
|
public function set_defaults() { |
739
|
|
|
$this->sync_queue = new Queue( 'sync' ); |
|
|
|
|
740
|
|
|
$this->full_sync_queue = new Queue( 'full_sync' ); |
|
|
|
|
741
|
|
|
$this->set_codec(); |
742
|
|
|
|
743
|
|
|
// Saved settings. |
744
|
|
|
Settings::set_importing( null ); |
|
|
|
|
745
|
|
|
$settings = Settings::get_settings(); |
746
|
|
|
$this->set_dequeue_max_bytes( $settings['dequeue_max_bytes'] ); |
747
|
|
|
$this->set_upload_max_bytes( $settings['upload_max_bytes'] ); |
748
|
|
|
$this->set_upload_max_rows( $settings['upload_max_rows'] ); |
749
|
|
|
$this->set_sync_wait_time( $settings['sync_wait_time'] ); |
750
|
|
|
$this->set_enqueue_wait_time( $settings['enqueue_wait_time'] ); |
751
|
|
|
$this->set_sync_wait_threshold( $settings['sync_wait_threshold'] ); |
752
|
|
|
$this->set_max_dequeue_time( Defaults::get_max_sync_execution_time() ); |
753
|
|
|
} |
754
|
|
|
|
755
|
|
|
/** |
756
|
|
|
* Reset sync queues, modules and settings. |
757
|
|
|
* |
758
|
|
|
* @access public |
759
|
|
|
*/ |
760
|
|
|
public function reset_data() { |
761
|
|
|
$this->reset_sync_queue(); |
762
|
|
|
$this->reset_full_sync_queue(); |
763
|
|
|
|
764
|
|
|
foreach ( Modules::get_modules() as $module ) { |
|
|
|
|
765
|
|
|
$module->reset_data(); |
766
|
|
|
} |
767
|
|
|
|
768
|
|
|
foreach ( array( 'sync', 'full_sync', 'full-sync-enqueue' ) as $queue_name ) { |
769
|
|
|
delete_option( self::NEXT_SYNC_TIME_OPTION_NAME . '_' . $queue_name ); |
770
|
|
|
} |
771
|
|
|
|
772
|
|
|
Settings::reset_data(); |
773
|
|
|
} |
774
|
|
|
|
775
|
|
|
/** |
776
|
|
|
* Perform cleanup at the event of plugin uninstallation. |
777
|
|
|
* |
778
|
|
|
* @access public |
779
|
|
|
*/ |
780
|
|
|
public function uninstall() { |
781
|
|
|
// Lets delete all the other fun stuff like transient and option and the sync queue. |
782
|
|
|
$this->reset_data(); |
783
|
|
|
|
784
|
|
|
// Delete the full sync status. |
785
|
|
|
delete_option( 'jetpack_full_sync_status' ); |
786
|
|
|
|
787
|
|
|
// Clear the sync cron. |
788
|
|
|
wp_clear_scheduled_hook( 'jetpack_sync_cron' ); |
789
|
|
|
wp_clear_scheduled_hook( 'jetpack_sync_full_cron' ); |
790
|
|
|
} |
791
|
|
|
} |
792
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..