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