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