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-array-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 NEXT_SYNC_TIME_OPTION_NAME = 'jetpack_next_sync_time'; |
15
|
|
|
const WPCOM_ERROR_SYNC_DELAY = 60; |
16
|
|
|
const QUEUE_LOCKED_SYNC_DELAY = 10; |
17
|
|
|
|
18
|
|
|
private $dequeue_max_bytes; |
19
|
|
|
private $upload_max_bytes; |
20
|
|
|
private $upload_max_rows; |
21
|
|
|
private $max_dequeue_time; |
22
|
|
|
private $sync_wait_time; |
23
|
|
|
private $sync_wait_threshold; |
24
|
|
|
private $sync_queue; |
25
|
|
|
private $full_sync_queue; |
26
|
|
|
private $codec; |
27
|
|
|
|
28
|
|
|
// singleton functions |
29
|
|
|
private static $instance; |
30
|
|
|
|
31
|
|
|
public static function get_instance() { |
32
|
|
|
if ( null === self::$instance ) { |
33
|
|
|
self::$instance = new self(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return self::$instance; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
// this is necessary because you can't use "new" when you declare instance properties >:( |
40
|
|
|
protected function __construct() { |
41
|
|
|
$this->set_defaults(); |
42
|
|
|
$this->init(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
private function init() { |
46
|
|
|
foreach ( Jetpack_Sync_Modules::get_modules() as $module ) { |
47
|
|
|
$module->init_before_send(); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function get_next_sync_time( $queue_name ) { |
52
|
|
|
return (double) get_option( self::NEXT_SYNC_TIME_OPTION_NAME . '_' . $queue_name, 0 ); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function set_next_sync_time( $time, $queue_name ) { |
56
|
|
|
return update_option( self::NEXT_SYNC_TIME_OPTION_NAME . '_' . $queue_name, $time, true ); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function do_full_sync() { |
60
|
|
|
return $this->do_sync_and_set_delays( $this->full_sync_queue ); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function do_sync() { |
64
|
|
|
return $this->do_sync_and_set_delays( $this->sync_queue ); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function do_sync_and_set_delays( $queue ) { |
68
|
|
|
// don't sync if importing |
69
|
|
|
if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) { |
70
|
|
|
return false; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// don't sync if we are throttled |
74
|
|
|
if ( $this->get_next_sync_time( $queue->id ) > microtime( true ) ) { |
75
|
|
|
return false; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$start_time = microtime( true ); |
79
|
|
|
|
80
|
|
|
$sync_result = $this->do_sync_for_queue( $queue ); |
81
|
|
|
|
82
|
|
|
$exceeded_sync_wait_threshold = ( microtime( true ) - $start_time ) > (double) $this->get_sync_wait_threshold(); |
83
|
|
|
|
84
|
|
|
if ( is_wp_error( $sync_result ) ) { |
85
|
|
|
if ( 'unclosed_buffer' === $sync_result->get_error_code() ) { |
86
|
|
|
$this->set_next_sync_time( time() + self::QUEUE_LOCKED_SYNC_DELAY, $queue->id ); |
87
|
|
|
} else { |
88
|
|
|
$this->set_next_sync_time( time() + self::WPCOM_ERROR_SYNC_DELAY, $queue->id ); |
89
|
|
|
} |
90
|
|
|
$sync_result = false; |
91
|
|
|
} elseif ( $exceeded_sync_wait_threshold ) { |
92
|
|
|
// if we actually sent data and it took a while, wait before sending again |
93
|
|
|
$this->set_next_sync_time( time() + $this->get_sync_wait_time(), $queue->id ); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $sync_result; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function get_items_to_send( $buffer, $encode = true ) { |
100
|
|
|
// track how long we've been processing so we can avoid request timeouts |
101
|
|
|
$start_time = microtime( true ); |
102
|
|
|
|
103
|
|
|
$upload_size = 0; |
104
|
|
|
$items_to_send = array(); |
105
|
|
|
$items = $buffer->get_items(); |
106
|
|
|
|
107
|
|
|
// set up current screen to avoid errors rendering content |
108
|
|
|
require_once( ABSPATH . 'wp-admin/includes/class-wp-screen.php' ); |
109
|
|
|
require_once( ABSPATH . 'wp-admin/includes/screen.php' ); |
110
|
|
|
set_current_screen( 'sync' ); |
111
|
|
|
|
112
|
|
|
$skipped_items_ids = array(); |
113
|
|
|
|
114
|
|
|
// we estimate the total encoded size as we go by encoding each item individually |
115
|
|
|
// this is expensive, but the only way to really know :/ |
116
|
|
|
foreach ( $items as $key => $item ) { |
117
|
|
|
// Suspending cache addition help prevent overloading in memory cache of large sites. |
118
|
|
|
wp_suspend_cache_addition( true ); |
119
|
|
|
/** |
120
|
|
|
* Modify the data within an action before it is serialized and sent to the server |
121
|
|
|
* For example, during full sync this expands Post ID's into full Post objects, |
122
|
|
|
* so that we don't have to serialize the whole object into the queue. |
123
|
|
|
* |
124
|
|
|
* @since 4.2.0 |
125
|
|
|
* |
126
|
|
|
* @param array The action parameters |
127
|
|
|
* @param int The ID of the user who triggered the action |
128
|
|
|
*/ |
129
|
|
|
$item[1] = apply_filters( 'jetpack_sync_before_send_' . $item[0], $item[1], $item[2] ); |
130
|
|
|
wp_suspend_cache_addition( false ); |
131
|
|
|
if ( $item[1] === false ) { |
132
|
|
|
$skipped_items_ids[] = $key; |
133
|
|
|
continue; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$encoded_item = $encode ? $this->codec->encode( $item ) : $item; |
137
|
|
|
|
138
|
|
|
$upload_size += strlen( $encoded_item ); |
139
|
|
|
|
140
|
|
|
if ( $upload_size > $this->upload_max_bytes && count( $items_to_send ) > 0 ) { |
141
|
|
|
break; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$items_to_send[ $key ] = $encoded_item; |
145
|
|
|
|
146
|
|
|
if ( microtime(true) - $start_time > $this->max_dequeue_time ) { |
147
|
|
|
break; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return array( $items_to_send, $skipped_items_ids, $items ); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function do_sync_for_queue( $queue ) { |
155
|
|
|
|
156
|
|
|
do_action( 'jetpack_sync_before_send_queue_' . $queue->id ); |
157
|
|
|
|
158
|
|
|
if ( $queue->size() === 0 ) { |
159
|
|
|
return false; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
// now that we're sure we are about to sync, try to |
163
|
|
|
// ignore user abort so we can avoid getting into a |
164
|
|
|
// bad state |
165
|
|
|
if ( function_exists( 'ignore_user_abort' ) ) { |
166
|
|
|
ignore_user_abort( true ); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
$buffer = $queue->checkout_with_memory_limit( $this->dequeue_max_bytes, $this->upload_max_rows ); |
170
|
|
|
|
171
|
|
|
if ( ! $buffer ) { |
172
|
|
|
// buffer has no items |
173
|
|
|
return false; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
if ( is_wp_error( $buffer ) ) { |
177
|
|
|
// another buffer is currently sending |
178
|
|
|
return false; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
list( $items_to_send, $skipped_items_ids, $items ) = $this->get_items_to_send( $buffer ); |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Fires when data is ready to send to the server. |
185
|
|
|
* Return false or WP_Error to abort the sync (e.g. if there's an error) |
186
|
|
|
* The items will be automatically re-sent later |
187
|
|
|
* |
188
|
|
|
* @since 4.2.0 |
189
|
|
|
* |
190
|
|
|
* @param array $data The action buffer |
191
|
|
|
* @param string $codec The codec name used to encode the data |
192
|
|
|
* @param double $time The current time |
193
|
|
|
* @param string $queue The queue used to send ('sync' or 'full_sync') |
194
|
|
|
*/ |
195
|
|
|
$processed_item_ids = apply_filters( 'jetpack_sync_send_data', $items_to_send, $this->codec->name(), microtime( true ), $queue->id ); |
196
|
|
|
|
197
|
|
|
if ( ! $processed_item_ids || is_wp_error( $processed_item_ids ) ) { |
198
|
|
|
$checked_in_item_ids = $queue->checkin( $buffer ); |
199
|
|
|
|
200
|
|
|
if ( is_wp_error( $checked_in_item_ids ) ) { |
201
|
|
|
error_log( 'Error checking in buffer: ' . $checked_in_item_ids->get_error_message() ); |
202
|
|
|
$queue->force_checkin(); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
if ( is_wp_error( $processed_item_ids ) ) { |
206
|
|
|
return $processed_item_ids; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
// returning a WP_Error is a sign to the caller that we should wait a while |
210
|
|
|
// before syncing again |
211
|
|
|
return new WP_Error( 'server_error' ); |
212
|
|
|
|
213
|
|
|
} else { |
214
|
|
|
|
215
|
|
|
// detect if the last item ID was an error |
216
|
|
|
$had_wp_error = is_wp_error( end( $processed_item_ids ) ); |
217
|
|
|
|
218
|
|
|
if ( $had_wp_error ) { |
219
|
|
|
$wp_error = array_pop( $processed_item_ids ); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
// also checkin any items that were skipped |
223
|
|
|
if ( count( $skipped_items_ids ) > 0 ) { |
224
|
|
|
$processed_item_ids = array_merge( $processed_item_ids, $skipped_items_ids ); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
$processed_items = array_intersect_key( $items, array_flip( $processed_item_ids ) ); |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Allows us to keep track of all the actions that have been sent. |
231
|
|
|
* Allows us to calculate the progress of specific actions. |
232
|
|
|
* |
233
|
|
|
* @since 4.2.0 |
234
|
|
|
* |
235
|
|
|
* @param array $processed_actions The actions that we send successfully. |
236
|
|
|
*/ |
237
|
|
|
do_action( 'jetpack_sync_processed_actions', $processed_items ); |
238
|
|
|
|
239
|
|
|
$queue->close( $buffer, $processed_item_ids ); |
240
|
|
|
|
241
|
|
|
// returning a WP_Error is a sign to the caller that we should wait a while |
242
|
|
|
// before syncing again |
243
|
|
|
if ( $had_wp_error ) { |
244
|
|
|
return $wp_error; |
|
|
|
|
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
return true; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
function get_sync_queue() { |
252
|
|
|
return $this->sync_queue; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
function get_full_sync_queue() { |
256
|
|
|
return $this->full_sync_queue; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
function get_codec() { |
260
|
|
|
return $this->codec; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
function send_checksum() { |
264
|
|
|
require_once 'class.jetpack-sync-wp-replicastore.php'; |
265
|
|
|
$store = new Jetpack_Sync_WP_Replicastore(); |
266
|
|
|
do_action( 'jetpack_sync_checksum', $store->checksum_all() ); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
function reset_sync_queue() { |
270
|
|
|
$this->sync_queue->reset(); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
function set_dequeue_max_bytes( $size ) { |
274
|
|
|
$this->dequeue_max_bytes = $size; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
// in bytes |
278
|
|
|
function set_upload_max_bytes( $max_bytes ) { |
279
|
|
|
$this->upload_max_bytes = $max_bytes; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
// in rows |
283
|
|
|
function set_upload_max_rows( $max_rows ) { |
284
|
|
|
$this->upload_max_rows = $max_rows; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
// in seconds |
288
|
|
|
function set_sync_wait_time( $seconds ) { |
289
|
|
|
$this->sync_wait_time = $seconds; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
function get_sync_wait_time() { |
293
|
|
|
return $this->sync_wait_time; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
// in seconds |
297
|
|
|
function set_sync_wait_threshold( $seconds ) { |
298
|
|
|
$this->sync_wait_threshold = $seconds; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
function get_sync_wait_threshold() { |
302
|
|
|
return $this->sync_wait_threshold; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
// in seconds |
306
|
|
|
function set_max_dequeue_time( $seconds ) { |
307
|
|
|
$this->max_dequeue_time = $seconds; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
function set_defaults() { |
311
|
|
|
$this->sync_queue = new Jetpack_Sync_Queue( 'sync' ); |
312
|
|
|
$this->full_sync_queue = new Jetpack_Sync_Queue( 'full_sync' ); |
313
|
|
|
$this->codec = new Jetpack_Sync_JSON_Deflate_Array_Codec(); |
314
|
|
|
|
315
|
|
|
// saved settings |
316
|
|
|
Jetpack_Sync_Settings::set_importing( null ); |
317
|
|
|
$settings = Jetpack_Sync_Settings::get_settings(); |
318
|
|
|
$this->set_dequeue_max_bytes( $settings['dequeue_max_bytes'] ); |
319
|
|
|
$this->set_upload_max_bytes( $settings['upload_max_bytes'] ); |
320
|
|
|
$this->set_upload_max_rows( $settings['upload_max_rows'] ); |
321
|
|
|
$this->set_sync_wait_time( $settings['sync_wait_time'] ); |
322
|
|
|
$this->set_sync_wait_threshold( $settings['sync_wait_threshold'] ); |
323
|
|
|
$this->set_max_dequeue_time( Jetpack_Sync_Defaults::get_max_sync_execution_time() ); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
function reset_data() { |
327
|
|
|
$this->reset_sync_queue(); |
328
|
|
|
|
329
|
|
|
foreach ( Jetpack_Sync_Modules::get_modules() as $module ) { |
330
|
|
|
$module->reset_data(); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
foreach ( array( 'sync', 'full_sync' ) as $queue_name ) { |
334
|
|
|
delete_option( self::NEXT_SYNC_TIME_OPTION_NAME . '_' . $queue_name ); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
Jetpack_Sync_Settings::reset_data(); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
function uninstall() { |
341
|
|
|
// Lets delete all the other fun stuff like transient and option and the sync queue |
342
|
|
|
$this->reset_data(); |
343
|
|
|
|
344
|
|
|
// delete the full sync status |
345
|
|
|
delete_option( 'jetpack_full_sync_status' ); |
346
|
|
|
|
347
|
|
|
// clear the sync cron. |
348
|
|
|
wp_clear_scheduled_hook( 'jetpack_sync_cron' ); |
349
|
|
|
} |
350
|
|
|
} |
351
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: