1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Full sync module. |
4
|
|
|
* |
5
|
|
|
* @package automattic/jetpack-sync |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack\Sync\Modules; |
9
|
|
|
|
10
|
|
|
use Automattic\Jetpack\Sync\Listener; |
11
|
|
|
use Automattic\Jetpack\Sync\Modules; |
12
|
|
|
use Automattic\Jetpack\Sync\Queue; |
13
|
|
|
use Automattic\Jetpack\Sync\Settings; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* This class does a full resync of the database by |
17
|
|
|
* enqueuing an outbound action for every single object |
18
|
|
|
* that we care about. |
19
|
|
|
* |
20
|
|
|
* This class, and its related class Jetpack_Sync_Module, contain a few non-obvious optimisations that should be explained: |
21
|
|
|
* - we fire an action called jetpack_full_sync_start so that WPCOM can erase the contents of the cached database |
22
|
|
|
* - for each object type, we page through the object IDs and enqueue them by firing some monitored actions |
23
|
|
|
* - we load the full objects for those IDs in chunks of Jetpack_Sync_Module::ARRAY_CHUNK_SIZE (to reduce the number of MySQL calls) |
24
|
|
|
* - we fire a trigger for the entire array which the Automattic\Jetpack\Sync\Listener then serializes and queues. |
25
|
|
|
*/ |
26
|
|
|
class Full_Sync extends Module { |
27
|
|
|
/** |
28
|
|
|
* Prefix of the full sync status option name. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
const STATUS_OPTION_PREFIX = 'jetpack_sync_full_'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Timeout between the previous and the next allowed full sync. |
36
|
|
|
* |
37
|
|
|
* @todo Remove this as it's no longer used since https://github.com/Automattic/jetpack/pull/4561 |
38
|
|
|
* |
39
|
|
|
* @var int |
40
|
|
|
*/ |
41
|
|
|
const FULL_SYNC_TIMEOUT = 3600; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Sync module name. |
45
|
|
|
* |
46
|
|
|
* @access public |
47
|
|
|
* |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
public function name() { |
51
|
|
|
return 'full-sync'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Initialize action listeners for full sync. |
56
|
|
|
* |
57
|
|
|
* @access public |
58
|
|
|
* |
59
|
|
|
* @param callable $callable Action handler callable. |
60
|
|
|
*/ |
61
|
|
|
public function init_full_sync_listeners( $callable ) { |
62
|
|
|
// Synthetic actions for full sync. |
63
|
|
|
add_action( 'jetpack_full_sync_start', $callable, 10, 3 ); |
64
|
|
|
add_action( 'jetpack_full_sync_end', $callable, 10, 2 ); |
65
|
|
|
add_action( 'jetpack_full_sync_cancelled', $callable ); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Initialize the module in the sender. |
70
|
|
|
* |
71
|
|
|
* @access public |
72
|
|
|
*/ |
73
|
|
|
public function init_before_send() { |
74
|
|
|
// This is triggered after actions have been processed on the server. |
75
|
|
|
add_action( 'jetpack_sync_processed_actions', array( $this, 'update_sent_progress_action' ) ); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Start a full sync. |
80
|
|
|
* |
81
|
|
|
* @access public |
82
|
|
|
* |
83
|
|
|
* @param array $module_configs Full sync configuration for all sync modules. |
|
|
|
|
84
|
|
|
* @return bool Always returns true at success. |
85
|
|
|
*/ |
86
|
|
|
public function start( $module_configs = null ) { |
87
|
|
|
$was_already_running = $this->is_started() && ! $this->is_finished(); |
88
|
|
|
|
89
|
|
|
// Remove all evidence of previous full sync items and status. |
90
|
|
|
$this->reset_data(); |
91
|
|
|
|
92
|
|
|
if ( $was_already_running ) { |
93
|
|
|
/** |
94
|
|
|
* Fires when a full sync is cancelled. |
95
|
|
|
* |
96
|
|
|
* @since 4.2.0 |
97
|
|
|
*/ |
98
|
|
|
do_action( 'jetpack_full_sync_cancelled' ); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$this->update_status_option( 'started', time() ); |
102
|
|
|
$this->update_status_option( 'params', $module_configs ); |
103
|
|
|
|
104
|
|
|
$enqueue_status = array(); |
105
|
|
|
$full_sync_config = array(); |
106
|
|
|
$include_empty = false; |
107
|
|
|
$empty = array(); |
108
|
|
|
|
109
|
|
|
// Default value is full sync. |
110
|
|
|
if ( ! is_array( $module_configs ) ) { |
111
|
|
|
$module_configs = array(); |
112
|
|
|
$include_empty = true; |
113
|
|
|
foreach ( Modules::get_modules() as $module ) { |
|
|
|
|
114
|
|
|
$module_configs[ $module->name() ] = true; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// Set default configuration, calculate totals, and save configuration if totals > 0. |
119
|
|
|
foreach ( Modules::get_modules() as $module ) { |
|
|
|
|
120
|
|
|
$module_name = $module->name(); |
121
|
|
|
$module_config = isset( $module_configs[ $module_name ] ) ? $module_configs[ $module_name ] : false; |
122
|
|
|
|
123
|
|
|
if ( ! $module_config ) { |
124
|
|
|
continue; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if ( 'users' === $module_name && 'initial' === $module_config ) { |
128
|
|
|
$module_config = $module->get_initial_sync_user_config(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$enqueue_status[ $module_name ] = false; |
132
|
|
|
|
133
|
|
|
$total_items = $module->estimate_full_sync_actions( $module_config ); |
134
|
|
|
|
135
|
|
|
// If there's information to process, configure this module. |
136
|
|
|
if ( ! is_null( $total_items ) && $total_items > 0 ) { |
137
|
|
|
$full_sync_config[ $module_name ] = $module_config; |
138
|
|
|
$enqueue_status[ $module_name ] = array( |
139
|
|
|
$total_items, // Total. |
140
|
|
|
0, // Queued. |
141
|
|
|
false, // Current state. |
142
|
|
|
); |
143
|
|
|
} elseif ( $include_empty && 0 === $total_items ) { |
144
|
|
|
$empty[ $module_name ] = true; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$this->set_config( $full_sync_config ); |
149
|
|
|
$this->set_enqueue_status( $enqueue_status ); |
150
|
|
|
|
151
|
|
|
$range = $this->get_content_range( $full_sync_config ); |
152
|
|
|
/** |
153
|
|
|
* Fires when a full sync begins. This action is serialized |
154
|
|
|
* and sent to the server so that it knows a full sync is coming. |
155
|
|
|
* |
156
|
|
|
* @since 4.2.0 |
157
|
|
|
* @since 7.3.0 Added $range arg. |
158
|
|
|
* @since 7.4.0 Added $empty arg. |
159
|
|
|
* |
160
|
|
|
* @param array $full_sync_config Sync configuration for all sync modules. |
161
|
|
|
* @param array $range Range of the sync items, containing min and max IDs for some item types. |
162
|
|
|
* @param array $empty The modules with no items to sync during a full sync. |
163
|
|
|
*/ |
164
|
|
|
do_action( 'jetpack_full_sync_start', $full_sync_config, $range, $empty ); |
165
|
|
|
|
166
|
|
|
$this->continue_enqueuing( $full_sync_config, $enqueue_status ); |
167
|
|
|
|
168
|
|
|
return true; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Enqueue the next items to sync. |
173
|
|
|
* |
174
|
|
|
* @access public |
175
|
|
|
* |
176
|
|
|
* @param array $configs Full sync configuration for all sync modules. |
|
|
|
|
177
|
|
|
* @param array $enqueue_status Current status of the queue, indexed by sync modules. |
|
|
|
|
178
|
|
|
*/ |
179
|
|
|
public function continue_enqueuing( $configs = null, $enqueue_status = null ) { |
180
|
|
|
if ( ! $this->is_started() || $this->get_status_option( 'queue_finished' ) ) { |
181
|
|
|
return; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
// If full sync queue is full, don't enqueue more items. |
185
|
|
|
$max_queue_size_full_sync = Settings::get_setting( 'max_queue_size_full_sync' ); |
186
|
|
|
$full_sync_queue = new Queue( 'full_sync' ); |
187
|
|
|
|
188
|
|
|
$available_queue_slots = $max_queue_size_full_sync - $full_sync_queue->size(); |
189
|
|
|
|
190
|
|
|
if ( $available_queue_slots <= 0 ) { |
191
|
|
|
return; |
192
|
|
|
} else { |
193
|
|
|
$remaining_items_to_enqueue = min( Settings::get_setting( 'max_enqueue_full_sync' ), $available_queue_slots ); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
if ( ! $configs ) { |
197
|
|
|
$configs = $this->get_config(); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
if ( ! $enqueue_status ) { |
201
|
|
|
$enqueue_status = $this->get_enqueue_status(); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
$modules = Modules::get_modules(); |
205
|
|
|
$modules_processed = 0; |
206
|
|
|
foreach ( $modules as $module ) { |
|
|
|
|
207
|
|
|
$module_name = $module->name(); |
208
|
|
|
|
209
|
|
|
// Skip module if not configured for this sync or module is done. |
210
|
|
|
if ( ! isset( $configs[ $module_name ] ) |
211
|
|
|
|| // No module config. |
212
|
|
|
! $configs[ $module_name ] |
213
|
|
|
|| // No enqueue status. |
214
|
|
|
! $enqueue_status[ $module_name ] |
215
|
|
|
|| // Finished enqueuing this module. |
216
|
|
|
true === $enqueue_status[ $module_name ][2] ) { |
217
|
|
|
$modules_processed ++; |
218
|
|
|
continue; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
list( $items_enqueued, $next_enqueue_state ) = $module->enqueue_full_sync_actions( $configs[ $module_name ], $remaining_items_to_enqueue, $enqueue_status[ $module_name ][2] ); |
222
|
|
|
|
223
|
|
|
$enqueue_status[ $module_name ][2] = $next_enqueue_state; |
224
|
|
|
|
225
|
|
|
// If items were processed, subtract them from the limit. |
226
|
|
|
if ( ! is_null( $items_enqueued ) && $items_enqueued > 0 ) { |
227
|
|
|
$enqueue_status[ $module_name ][1] += $items_enqueued; |
228
|
|
|
$remaining_items_to_enqueue -= $items_enqueued; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
if ( true === $next_enqueue_state ) { |
232
|
|
|
$modules_processed ++; |
233
|
|
|
} |
234
|
|
|
// Stop processing if we've reached our limit of items to enqueue. |
235
|
|
|
if ( 0 >= $remaining_items_to_enqueue ) { |
236
|
|
|
break; |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
$this->set_enqueue_status( $enqueue_status ); |
241
|
|
|
|
242
|
|
|
if ( count( $modules ) > $modules_processed ) { |
243
|
|
|
return; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
// Setting autoload to true means that it's faster to check whether we should continue enqueuing. |
247
|
|
|
$this->update_status_option( 'queue_finished', time(), true ); |
248
|
|
|
|
249
|
|
|
$range = $this->get_content_range( $configs ); |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Fires when a full sync ends. This action is serialized |
253
|
|
|
* and sent to the server. |
254
|
|
|
* |
255
|
|
|
* @since 4.2.0 |
256
|
|
|
* @since 7.3.0 Added $range arg. |
257
|
|
|
* |
258
|
|
|
* @param string $checksum Deprecated since 7.3.0 - @see https://github.com/Automattic/jetpack/pull/11945/ |
259
|
|
|
* @param array $range Range of the sync items, containing min and max IDs for some item types. |
260
|
|
|
*/ |
261
|
|
|
do_action( 'jetpack_full_sync_end', '', $range ); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Get the range (min ID, max ID and total items) of items to sync. |
266
|
|
|
* |
267
|
|
|
* @access public |
268
|
|
|
* |
269
|
|
|
* @param string $type Type of sync item to get the range for. |
270
|
|
|
* @return array Array of min ID, max ID and total items in the range. |
271
|
|
|
*/ |
272
|
|
|
public function get_range( $type ) { |
273
|
|
|
global $wpdb; |
274
|
|
|
if ( ! in_array( $type, array( 'comments', 'posts' ), true ) ) { |
275
|
|
|
return array(); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
switch ( $type ) { |
279
|
|
|
case 'posts': |
280
|
|
|
$table = $wpdb->posts; |
281
|
|
|
$id = 'ID'; |
282
|
|
|
$where_sql = Settings::get_blacklisted_post_types_sql(); |
283
|
|
|
|
284
|
|
|
break; |
285
|
|
|
case 'comments': |
286
|
|
|
$table = $wpdb->comments; |
287
|
|
|
$id = 'comment_ID'; |
288
|
|
|
$where_sql = Settings::get_comments_filter_sql(); |
289
|
|
|
break; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
// TODO: Call $wpdb->prepare on the following query. |
293
|
|
|
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
294
|
|
|
$results = $wpdb->get_results( "SELECT MAX({$id}) as max, MIN({$id}) as min, COUNT({$id}) as count FROM {$table} WHERE {$where_sql}" ); |
|
|
|
|
295
|
|
|
if ( isset( $results[0] ) ) { |
296
|
|
|
return $results[0]; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
return array(); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Get the range for content (posts and comments) to sync. |
304
|
|
|
* |
305
|
|
|
* @access private |
306
|
|
|
* |
307
|
|
|
* @param array $config Full sync configuration for this all sync modules. |
308
|
|
|
* @return array Array of range (min ID, max ID, total items) for all content types. |
309
|
|
|
*/ |
310
|
|
|
private function get_content_range( $config ) { |
311
|
|
|
$range = array(); |
312
|
|
|
// Only when we are sending the whole range do we want to send also the range. |
313
|
|
|
if ( true === isset( $config['posts'] ) && $config['posts'] ) { |
314
|
|
|
$range['posts'] = $this->get_range( 'posts' ); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
if ( true === isset( $config['comments'] ) && $config['comments'] ) { |
318
|
|
|
$range['comments'] = $this->get_range( 'comments' ); |
319
|
|
|
} |
320
|
|
|
return $range; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Update the progress after sync modules actions have been processed on the server. |
325
|
|
|
* |
326
|
|
|
* @access public |
327
|
|
|
* |
328
|
|
|
* @param array $actions Actions that have been processed on the server. |
329
|
|
|
*/ |
330
|
|
|
public function update_sent_progress_action( $actions ) { |
331
|
|
|
// Quick way to map to first items with an array of arrays. |
332
|
|
|
$actions_with_counts = array_count_values( array_filter( array_map( array( $this, 'get_action_name' ), $actions ) ) ); |
333
|
|
|
|
334
|
|
|
// Total item counts for each action. |
335
|
|
|
$actions_with_total_counts = $this->get_actions_totals( $actions ); |
336
|
|
|
|
337
|
|
|
if ( ! $this->is_started() || $this->is_finished() ) { |
338
|
|
|
return; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
if ( isset( $actions_with_counts['jetpack_full_sync_start'] ) ) { |
342
|
|
|
$this->update_status_option( 'send_started', time() ); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
foreach ( Modules::get_modules() as $module ) { |
|
|
|
|
346
|
|
|
$module_actions = $module->get_full_sync_actions(); |
347
|
|
|
$status_option_name = "{$module->name()}_sent"; |
348
|
|
|
$total_option_name = "{$status_option_name}_total"; |
349
|
|
|
$items_sent = $this->get_status_option( $status_option_name, 0 ); |
350
|
|
|
$items_sent_total = $this->get_status_option( $total_option_name, 0 ); |
351
|
|
|
|
352
|
|
|
foreach ( $module_actions as $module_action ) { |
353
|
|
|
if ( isset( $actions_with_counts[ $module_action ] ) ) { |
354
|
|
|
$items_sent += $actions_with_counts[ $module_action ]; |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
if ( ! empty( $actions_with_total_counts[ $module_action ] ) ) { |
358
|
|
|
$items_sent_total += $actions_with_total_counts[ $module_action ]; |
359
|
|
|
} |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
if ( $items_sent > 0 ) { |
363
|
|
|
$this->update_status_option( $status_option_name, $items_sent ); |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
if ( 0 !== $items_sent_total ) { |
367
|
|
|
$this->update_status_option( $total_option_name, $items_sent_total ); |
368
|
|
|
} |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
if ( isset( $actions_with_counts['jetpack_full_sync_end'] ) ) { |
372
|
|
|
$this->update_status_option( 'finished', time() ); |
373
|
|
|
} |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* Get the name of the action for an item in the sync queue. |
378
|
|
|
* |
379
|
|
|
* @access public |
380
|
|
|
* |
381
|
|
|
* @param array $queue_item Item of the sync queue. |
382
|
|
|
* @return string|boolean Name of the action, false if queue item is invalid. |
383
|
|
|
*/ |
384
|
|
|
public function get_action_name( $queue_item ) { |
385
|
|
|
if ( is_array( $queue_item ) && isset( $queue_item[0] ) ) { |
386
|
|
|
return $queue_item[0]; |
387
|
|
|
} |
388
|
|
|
return false; |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* Retrieve the total number of items we're syncing in a particular queue item (action). |
393
|
|
|
* `$queue_item[1]` is expected to contain chunks of items, and `$queue_item[1][0]` |
394
|
|
|
* represents the first (and only) chunk of items to sync in that action. |
395
|
|
|
* |
396
|
|
|
* @access public |
397
|
|
|
* |
398
|
|
|
* @param array $queue_item Item of the sync queue that corresponds to a particular action. |
399
|
|
|
* @return int Total number of items in the action. |
400
|
|
|
*/ |
401
|
|
|
public function get_action_totals( $queue_item ) { |
402
|
|
|
if ( is_array( $queue_item ) && isset( $queue_item[1][0] ) ) { |
403
|
|
|
if ( is_array( $queue_item[1][0] ) ) { |
404
|
|
|
// Let's count the items we sync in this action. |
405
|
|
|
return count( $queue_item[1][0] ); |
406
|
|
|
} |
407
|
|
|
// -1 indicates that this action syncs all items by design. |
408
|
|
|
return -1; |
409
|
|
|
} |
410
|
|
|
return 0; |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
/** |
414
|
|
|
* Retrieve the total number of items for a set of actions, grouped by action name. |
415
|
|
|
* |
416
|
|
|
* @access public |
417
|
|
|
* |
418
|
|
|
* @param array $actions An array of actions. |
419
|
|
|
* @return array An array, representing the total number of items, grouped per action. |
420
|
|
|
*/ |
421
|
|
|
public function get_actions_totals( $actions ) { |
422
|
|
|
$totals = array(); |
423
|
|
|
|
424
|
|
|
foreach ( $actions as $action ) { |
425
|
|
|
$name = $this->get_action_name( $action ); |
426
|
|
|
$action_totals = $this->get_action_totals( $action ); |
427
|
|
|
if ( ! isset( $totals[ $name ] ) ) { |
428
|
|
|
$totals[ $name ] = 0; |
429
|
|
|
} |
430
|
|
|
$totals[ $name ] += $action_totals; |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
return $totals; |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
/** |
437
|
|
|
* Whether full sync has started. |
438
|
|
|
* |
439
|
|
|
* @access public |
440
|
|
|
* |
441
|
|
|
* @return boolean |
442
|
|
|
*/ |
443
|
|
|
public function is_started() { |
444
|
|
|
return ! ! $this->get_status_option( 'started' ); |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
/** |
448
|
|
|
* Whether full sync has finished. |
449
|
|
|
* |
450
|
|
|
* @access public |
451
|
|
|
* |
452
|
|
|
* @return boolean |
453
|
|
|
*/ |
454
|
|
|
public function is_finished() { |
455
|
|
|
return ! ! $this->get_status_option( 'finished' ); |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
/** |
459
|
|
|
* Retrieve the status of the current full sync. |
460
|
|
|
* |
461
|
|
|
* @access public |
462
|
|
|
* |
463
|
|
|
* @return array Full sync status. |
464
|
|
|
*/ |
465
|
|
|
public function get_status() { |
466
|
|
|
$status = array( |
467
|
|
|
'started' => $this->get_status_option( 'started' ), |
468
|
|
|
'queue_finished' => $this->get_status_option( 'queue_finished' ), |
469
|
|
|
'send_started' => $this->get_status_option( 'send_started' ), |
470
|
|
|
'finished' => $this->get_status_option( 'finished' ), |
471
|
|
|
'sent' => array(), |
472
|
|
|
'sent_total' => array(), |
473
|
|
|
'queue' => array(), |
474
|
|
|
'config' => $this->get_status_option( 'params' ), |
475
|
|
|
'total' => array(), |
476
|
|
|
); |
477
|
|
|
|
478
|
|
|
$enqueue_status = $this->get_enqueue_status(); |
479
|
|
|
|
480
|
|
|
foreach ( Modules::get_modules() as $module ) { |
|
|
|
|
481
|
|
|
$name = $module->name(); |
482
|
|
|
|
483
|
|
|
if ( ! isset( $enqueue_status[ $name ] ) ) { |
484
|
|
|
continue; |
485
|
|
|
} |
486
|
|
|
|
487
|
|
|
list( $total, $queued ) = $enqueue_status[ $name ]; |
488
|
|
|
|
489
|
|
|
if ( $total ) { |
490
|
|
|
$status['total'][ $name ] = $total; |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
if ( $queued ) { |
494
|
|
|
$status['queue'][ $name ] = $queued; |
495
|
|
|
} |
496
|
|
|
|
497
|
|
|
$sent = $this->get_status_option( "{$name}_sent" ); |
498
|
|
|
if ( $sent ) { |
499
|
|
|
$status['sent'][ $name ] = $sent; |
500
|
|
|
} |
501
|
|
|
|
502
|
|
|
$sent_total = $this->get_status_option( "{$name}_sent_total" ); |
503
|
|
|
if ( $sent_total ) { |
504
|
|
|
$status['sent_total'][ $name ] = $sent_total; |
505
|
|
|
} |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
return $status; |
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
/** |
512
|
|
|
* Clear all the full sync status options. |
513
|
|
|
* |
514
|
|
|
* @access public |
515
|
|
|
*/ |
516
|
|
|
public function clear_status() { |
517
|
|
|
$prefix = self::STATUS_OPTION_PREFIX; |
518
|
|
|
\Jetpack_Options::delete_raw_option( "{$prefix}_started" ); |
519
|
|
|
\Jetpack_Options::delete_raw_option( "{$prefix}_params" ); |
520
|
|
|
\Jetpack_Options::delete_raw_option( "{$prefix}_queue_finished" ); |
521
|
|
|
\Jetpack_Options::delete_raw_option( "{$prefix}_send_started" ); |
522
|
|
|
\Jetpack_Options::delete_raw_option( "{$prefix}_finished" ); |
523
|
|
|
|
524
|
|
|
$this->delete_enqueue_status(); |
525
|
|
|
|
526
|
|
|
foreach ( Modules::get_modules() as $module ) { |
|
|
|
|
527
|
|
|
\Jetpack_Options::delete_raw_option( "{$prefix}_{$module->name()}_sent" ); |
528
|
|
|
\Jetpack_Options::delete_raw_option( "{$prefix}_{$module->name()}_sent_total" ); |
529
|
|
|
} |
530
|
|
|
} |
531
|
|
|
|
532
|
|
|
/** |
533
|
|
|
* Clear all the full sync data. |
534
|
|
|
* |
535
|
|
|
* @access public |
536
|
|
|
*/ |
537
|
|
|
public function reset_data() { |
538
|
|
|
$this->clear_status(); |
539
|
|
|
$this->delete_config(); |
540
|
|
|
|
541
|
|
|
$listener = Listener::get_instance(); |
542
|
|
|
$listener->get_full_sync_queue()->reset(); |
543
|
|
|
} |
544
|
|
|
|
545
|
|
|
/** |
546
|
|
|
* Get the value of a full sync status option. |
547
|
|
|
* |
548
|
|
|
* @access private |
549
|
|
|
* |
550
|
|
|
* @param string $name Name of the option. |
551
|
|
|
* @param mixed $default Default value of the option. |
552
|
|
|
* @return mixed Option value. |
553
|
|
|
*/ |
554
|
|
|
private function get_status_option( $name, $default = null ) { |
555
|
|
|
$value = \Jetpack_Options::get_raw_option( self::STATUS_OPTION_PREFIX . "_$name", $default ); |
556
|
|
|
|
557
|
|
|
return is_numeric( $value ) ? intval( $value ) : $value; |
558
|
|
|
} |
559
|
|
|
|
560
|
|
|
/** |
561
|
|
|
* Update the value of a full sync status option. |
562
|
|
|
* |
563
|
|
|
* @access private |
564
|
|
|
* |
565
|
|
|
* @param string $name Name of the option. |
566
|
|
|
* @param mixed $value Value of the option. |
567
|
|
|
* @param boolean $autoload Whether the option should be autoloaded at the beginning of the request. |
568
|
|
|
*/ |
569
|
|
|
private function update_status_option( $name, $value, $autoload = false ) { |
570
|
|
|
\Jetpack_Options::update_raw_option( self::STATUS_OPTION_PREFIX . "_$name", $value, $autoload ); |
571
|
|
|
} |
572
|
|
|
|
573
|
|
|
/** |
574
|
|
|
* Set the full sync enqueue status. |
575
|
|
|
* |
576
|
|
|
* @access private |
577
|
|
|
* |
578
|
|
|
* @param array $new_status The new full sync enqueue status. |
579
|
|
|
*/ |
580
|
|
|
private function set_enqueue_status( $new_status ) { |
581
|
|
|
\Jetpack_Options::update_raw_option( 'jetpack_sync_full_enqueue_status', $new_status ); |
582
|
|
|
} |
583
|
|
|
|
584
|
|
|
/** |
585
|
|
|
* Delete full sync enqueue status. |
586
|
|
|
* |
587
|
|
|
* @access private |
588
|
|
|
* |
589
|
|
|
* @return boolean Whether the status was deleted. |
590
|
|
|
*/ |
591
|
|
|
private function delete_enqueue_status() { |
592
|
|
|
return \Jetpack_Options::delete_raw_option( 'jetpack_sync_full_enqueue_status' ); |
593
|
|
|
} |
594
|
|
|
|
595
|
|
|
/** |
596
|
|
|
* Retrieve the current full sync enqueue status. |
597
|
|
|
* |
598
|
|
|
* @access private |
599
|
|
|
* |
600
|
|
|
* @return array Full sync enqueue status. |
601
|
|
|
*/ |
602
|
|
|
public function get_enqueue_status() { |
603
|
|
|
return \Jetpack_Options::get_raw_option( 'jetpack_sync_full_enqueue_status' ); |
604
|
|
|
} |
605
|
|
|
|
606
|
|
|
/** |
607
|
|
|
* Set the full sync enqueue configuration. |
608
|
|
|
* |
609
|
|
|
* @access private |
610
|
|
|
* |
611
|
|
|
* @param array $config The new full sync enqueue configuration. |
612
|
|
|
*/ |
613
|
|
|
private function set_config( $config ) { |
614
|
|
|
\Jetpack_Options::update_raw_option( 'jetpack_sync_full_config', $config ); |
615
|
|
|
} |
616
|
|
|
|
617
|
|
|
/** |
618
|
|
|
* Delete full sync configuration. |
619
|
|
|
* |
620
|
|
|
* @access private |
621
|
|
|
* |
622
|
|
|
* @return boolean Whether the configuration was deleted. |
623
|
|
|
*/ |
624
|
|
|
private function delete_config() { |
625
|
|
|
return \Jetpack_Options::delete_raw_option( 'jetpack_sync_full_config' ); |
626
|
|
|
} |
627
|
|
|
|
628
|
|
|
/** |
629
|
|
|
* Retrieve the current full sync enqueue config. |
630
|
|
|
* |
631
|
|
|
* @access private |
632
|
|
|
* |
633
|
|
|
* @return array Full sync enqueue config. |
634
|
|
|
*/ |
635
|
|
|
private function get_config() { |
636
|
|
|
return \Jetpack_Options::get_raw_option( 'jetpack_sync_full_config' ); |
637
|
|
|
} |
638
|
|
|
|
639
|
|
|
/** |
640
|
|
|
* Update an option manually to bypass filters and caching. |
641
|
|
|
* |
642
|
|
|
* @access private |
643
|
|
|
* |
644
|
|
|
* @param string $name Option name. |
645
|
|
|
* @param mixed $value Option value. |
646
|
|
|
* @return int The number of updated rows in the database. |
647
|
|
|
*/ |
648
|
|
|
private function write_option( $name, $value ) { |
649
|
|
|
// We write our own option updating code to bypass filters/caching/etc on set_option/get_option. |
650
|
|
|
global $wpdb; |
651
|
|
|
$serialized_value = maybe_serialize( $value ); |
652
|
|
|
|
653
|
|
|
/** |
654
|
|
|
* Try updating, if no update then insert |
655
|
|
|
* TODO: try to deal with the fact that unchanged values can return updated_num = 0 |
656
|
|
|
* below we used "insert ignore" to at least suppress the resulting error. |
657
|
|
|
*/ |
658
|
|
|
$updated_num = $wpdb->query( |
659
|
|
|
$wpdb->prepare( |
660
|
|
|
"UPDATE $wpdb->options SET option_value = %s WHERE option_name = %s", |
661
|
|
|
$serialized_value, |
662
|
|
|
$name |
663
|
|
|
) |
664
|
|
|
); |
665
|
|
|
|
666
|
|
|
if ( ! $updated_num ) { |
667
|
|
|
$updated_num = $wpdb->query( |
668
|
|
|
$wpdb->prepare( |
669
|
|
|
"INSERT IGNORE INTO $wpdb->options ( option_name, option_value, autoload ) VALUES ( %s, %s, 'no' )", |
670
|
|
|
$name, |
671
|
|
|
$serialized_value |
672
|
|
|
) |
673
|
|
|
); |
674
|
|
|
} |
675
|
|
|
return $updated_num; |
676
|
|
|
} |
677
|
|
|
|
678
|
|
|
/** |
679
|
|
|
* Update an option manually to bypass filters and caching. |
680
|
|
|
* |
681
|
|
|
* @access private |
682
|
|
|
* |
683
|
|
|
* @param string $name Option name. |
684
|
|
|
* @param mixed $default Default option value. |
685
|
|
|
* @return mixed Option value. |
686
|
|
|
*/ |
687
|
|
|
private function read_option( $name, $default = null ) { |
688
|
|
|
global $wpdb; |
689
|
|
|
$value = $wpdb->get_var( |
690
|
|
|
$wpdb->prepare( |
691
|
|
|
"SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", |
692
|
|
|
$name |
693
|
|
|
) |
694
|
|
|
); |
695
|
|
|
$value = maybe_unserialize( $value ); |
696
|
|
|
|
697
|
|
|
if ( null === $value && null !== $default ) { |
698
|
|
|
return $default; |
699
|
|
|
} |
700
|
|
|
|
701
|
|
|
return $value; |
702
|
|
|
} |
703
|
|
|
} |
704
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.