1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* A base abstraction of a 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\Replicastore; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Basic methods implemented by Jetpack Sync extensions. |
15
|
|
|
* |
16
|
|
|
* @abstract |
17
|
|
|
*/ |
18
|
|
|
abstract class Module { |
19
|
|
|
/** |
20
|
|
|
* Number of items per chunk when grouping objects for performance reasons. |
21
|
|
|
* |
22
|
|
|
* @access public |
23
|
|
|
* |
24
|
|
|
* @var int |
25
|
|
|
*/ |
26
|
|
|
const ARRAY_CHUNK_SIZE = 10; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Sync module name. |
30
|
|
|
* |
31
|
|
|
* @access public |
32
|
|
|
* |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
|
|
abstract public function name(); |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The id field in the database. |
39
|
|
|
* |
40
|
|
|
* @access public |
41
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public function id_field() { |
45
|
|
|
return 'ID'; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The table in the database. |
50
|
|
|
* |
51
|
|
|
* @access public |
52
|
|
|
* |
53
|
|
|
* @return string|bool |
54
|
|
|
*/ |
55
|
|
|
public function table_name() { |
56
|
|
|
return false; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Retrieve a sync object by its ID. |
63
|
|
|
* |
64
|
|
|
* @access public |
65
|
|
|
* |
66
|
|
|
* @param string $object_type Type of the sync object. |
67
|
|
|
* @param int $id ID of the sync object. |
68
|
|
|
* @return mixed Object, or false if the object is invalid. |
69
|
|
|
*/ |
70
|
|
|
public function get_object_by_id( $object_type, $id ) { |
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Initialize callables action listeners. |
76
|
|
|
* Override these to set up listeners and set/reset data/defaults. |
77
|
|
|
* |
78
|
|
|
* @access public |
79
|
|
|
* |
80
|
|
|
* @param callable $callable Action handler callable. |
81
|
|
|
*/ |
82
|
|
|
public function init_listeners( $callable ) { |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Initialize module action listeners for full sync. |
87
|
|
|
* |
88
|
|
|
* @access public |
89
|
|
|
* |
90
|
|
|
* @param callable $callable Action handler callable. |
91
|
|
|
*/ |
92
|
|
|
public function init_full_sync_listeners( $callable ) { |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Initialize the module in the sender. |
97
|
|
|
* |
98
|
|
|
* @access public |
99
|
|
|
*/ |
100
|
|
|
public function init_before_send() { |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Set module defaults. |
105
|
|
|
* |
106
|
|
|
* @access public |
107
|
|
|
*/ |
108
|
|
|
public function set_defaults() { |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Perform module cleanup. |
113
|
|
|
* Usually triggered when uninstalling the plugin. |
114
|
|
|
* |
115
|
|
|
* @access public |
116
|
|
|
*/ |
117
|
|
|
public function reset_data() { |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Enqueue the module actions for full sync. |
122
|
|
|
* |
123
|
|
|
* @access public |
124
|
|
|
* |
125
|
|
|
* @param array $config Full sync configuration for this sync module. |
126
|
|
|
* @param int $max_items_to_enqueue Maximum number of items to enqueue. |
127
|
|
|
* @param boolean $state True if full sync has finished enqueueing this module, false otherwise. |
128
|
|
|
* @return array Number of actions enqueued, and next module state. |
129
|
|
|
*/ |
130
|
|
|
public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
131
|
|
|
// In subclasses, return the number of actions enqueued, and next module state (true == done). |
132
|
|
|
return array( null, true ); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Retrieve an estimated number of actions that will be enqueued. |
137
|
|
|
* |
138
|
|
|
* @access public |
139
|
|
|
* |
140
|
|
|
* @param array $config Full sync configuration for this sync module. |
141
|
|
|
* @return array Number of items yet to be enqueued. |
142
|
|
|
*/ |
143
|
|
|
public function estimate_full_sync_actions( $config ) { |
144
|
|
|
// In subclasses, return the number of items yet to be enqueued. |
145
|
|
|
return null; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
// phpcs:enable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Retrieve the actions that will be sent for this module during a full sync. |
152
|
|
|
* |
153
|
|
|
* @access public |
154
|
|
|
* |
155
|
|
|
* @return array Full sync actions of this module. |
156
|
|
|
*/ |
157
|
|
|
public function get_full_sync_actions() { |
158
|
|
|
return array(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get the number of actions that we care about. |
163
|
|
|
* |
164
|
|
|
* @access protected |
165
|
|
|
* |
166
|
|
|
* @param array $action_names Action names we're interested in. |
167
|
|
|
* @param array $actions_to_count Unfiltered list of actions we want to count. |
168
|
|
|
* @return array Number of actions that we're interested in. |
169
|
|
|
*/ |
170
|
|
|
protected function count_actions( $action_names, $actions_to_count ) { |
171
|
|
|
return count( array_intersect( $action_names, $actions_to_count ) ); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Calculate the checksum of one or more values. |
176
|
|
|
* |
177
|
|
|
* @access protected |
178
|
|
|
* |
179
|
|
|
* @param mixed $values Values to calculate checksum for. |
180
|
|
|
* @return int The checksum. |
181
|
|
|
*/ |
182
|
|
|
protected function get_check_sum( $values ) { |
183
|
|
|
return crc32( wp_json_encode( jetpack_json_wrap( $values ) ) ); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Whether a particular checksum in a set of checksums is valid. |
188
|
|
|
* |
189
|
|
|
* @access protected |
190
|
|
|
* |
191
|
|
|
* @param array $sums_to_check Array of checksums. |
192
|
|
|
* @param string $name Name of the checksum. |
193
|
|
|
* @param int $new_sum Checksum to compare against. |
194
|
|
|
* @return boolean Whether the checksum is valid. |
195
|
|
|
*/ |
196
|
|
|
protected function still_valid_checksum( $sums_to_check, $name, $new_sum ) { |
197
|
|
|
if ( isset( $sums_to_check[ $name ] ) && $sums_to_check[ $name ] === $new_sum ) { |
198
|
|
|
return true; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return false; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Enqueue all items of a sync type as an action. |
206
|
|
|
* |
207
|
|
|
* @access protected |
208
|
|
|
* |
209
|
|
|
* @param string $action_name Name of the action. |
210
|
|
|
* @param string $table_name Name of the database table. |
211
|
|
|
* @param string $id_field Name of the ID field in the database. |
212
|
|
|
* @param string $where_sql The SQL WHERE clause to filter to the desired items. |
213
|
|
|
* @param int $max_items_to_enqueue Maximum number of items to enqueue in the same time. |
214
|
|
|
* @param boolean $state Whether enqueueing has finished. |
215
|
|
|
* @return array Array, containing the number of chunks and TRUE, indicating enqueueing has finished. |
216
|
|
|
*/ |
217
|
|
|
protected function enqueue_all_ids_as_action( $action_name, $table_name, $id_field, $where_sql, $max_items_to_enqueue, $state ) { |
218
|
|
|
global $wpdb; |
219
|
|
|
|
220
|
|
|
if ( ! $where_sql ) { |
221
|
|
|
$where_sql = '1 = 1'; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
$items_per_page = 1000; |
225
|
|
|
$page = 1; |
226
|
|
|
$chunk_count = 0; |
227
|
|
|
$previous_interval_end = $state ? $state : '~0'; |
228
|
|
|
$listener = Listener::get_instance(); |
229
|
|
|
|
230
|
|
|
// Count down from max_id to min_id so we get newest posts/comments/etc first. |
231
|
|
|
// phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition, WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
232
|
|
|
while ( $ids = $wpdb->get_col( "SELECT {$id_field} FROM {$table_name} WHERE {$where_sql} AND {$id_field} < {$previous_interval_end} ORDER BY {$id_field} DESC LIMIT {$items_per_page}" ) ) { |
233
|
|
|
// Request posts in groups of N for efficiency. |
234
|
|
|
$chunked_ids = array_chunk( $ids, self::ARRAY_CHUNK_SIZE ); |
235
|
|
|
|
236
|
|
|
// If we hit our row limit, process and return. |
237
|
|
|
if ( $chunk_count + count( $chunked_ids ) >= $max_items_to_enqueue ) { |
238
|
|
|
$remaining_items_count = $max_items_to_enqueue - $chunk_count; |
239
|
|
|
$remaining_items = array_slice( $chunked_ids, 0, $remaining_items_count ); |
240
|
|
|
$remaining_items_with_previous_interval_end = $this->get_chunks_with_preceding_end( $remaining_items, $previous_interval_end ); |
241
|
|
|
$listener->bulk_enqueue_full_sync_actions( $action_name, $remaining_items_with_previous_interval_end ); |
242
|
|
|
|
243
|
|
|
$last_chunk = end( $remaining_items ); |
244
|
|
|
return array( $remaining_items_count + $chunk_count, end( $last_chunk ) ); |
245
|
|
|
} |
246
|
|
|
$chunked_ids_with_previous_end = $this->get_chunks_with_preceding_end( $chunked_ids, $previous_interval_end ); |
247
|
|
|
|
248
|
|
|
$listener->bulk_enqueue_full_sync_actions( $action_name, $chunked_ids_with_previous_end ); |
249
|
|
|
|
250
|
|
|
$chunk_count += count( $chunked_ids ); |
251
|
|
|
$page++; |
252
|
|
|
// The $ids are ordered in descending order. |
253
|
|
|
$previous_interval_end = end( $ids ); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
if ( $wpdb->last_error ) { |
257
|
|
|
// return the values that were passed in so all these chunks get retried. |
258
|
|
|
return array( $max_items_to_enqueue, $state ); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
return array( $chunk_count, true ); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Retrieve chunk IDs with previous interval end. |
266
|
|
|
* |
267
|
|
|
* @access protected |
268
|
|
|
* |
269
|
|
|
* @param array $chunks All remaining items. |
270
|
|
|
* @param int $previous_interval_end The last item from the previous interval. |
271
|
|
|
* @return array Chunk IDs with the previous interval end. |
272
|
|
|
*/ |
273
|
|
|
protected function get_chunks_with_preceding_end( $chunks, $previous_interval_end ) { |
274
|
|
|
$chunks_with_ends = array(); |
275
|
|
|
foreach ( $chunks as $chunk ) { |
276
|
|
|
$chunks_with_ends[] = array( |
277
|
|
|
'ids' => $chunk, |
278
|
|
|
'previous_end' => $previous_interval_end, |
279
|
|
|
); |
280
|
|
|
// Chunks are ordered in descending order. |
281
|
|
|
$previous_interval_end = end( $chunk ); |
282
|
|
|
} |
283
|
|
|
return $chunks_with_ends; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Get metadata of a particular object type within the designated meta key whitelist. |
288
|
|
|
* |
289
|
|
|
* @access protected |
290
|
|
|
* |
291
|
|
|
* @todo Refactor to use $wpdb->prepare() on the SQL query. |
292
|
|
|
* |
293
|
|
|
* @param array $ids Object IDs. |
294
|
|
|
* @param string $meta_type Meta type. |
295
|
|
|
* @param array $meta_key_whitelist Meta key whitelist. |
296
|
|
|
* @return array Unserialized meta values. |
297
|
|
|
*/ |
298
|
|
|
protected function get_metadata( $ids, $meta_type, $meta_key_whitelist ) { |
299
|
|
|
global $wpdb; |
300
|
|
|
$table = _get_meta_table( $meta_type ); |
301
|
|
|
$id = $meta_type . '_id'; |
302
|
|
|
if ( ! $table ) { |
303
|
|
|
return array(); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
$private_meta_whitelist_sql = "'" . implode( "','", array_map( 'esc_sql', $meta_key_whitelist ) ) . "'"; |
307
|
|
|
|
308
|
|
|
return array_map( |
309
|
|
|
array( $this, 'unserialize_meta' ), |
310
|
|
|
$wpdb->get_results( |
311
|
|
|
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared |
312
|
|
|
"SELECT $id, meta_key, meta_value, meta_id FROM $table WHERE $id IN ( " . implode( ',', wp_parse_id_list( $ids ) ) . ' )' . |
313
|
|
|
" AND meta_key IN ( $private_meta_whitelist_sql ) ", |
314
|
|
|
// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared |
315
|
|
|
OBJECT |
316
|
|
|
) |
317
|
|
|
); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* In most cases the total of items that we enqueue will always stays the same. |
322
|
|
|
* Since the items are ordered using the primary key which auto increments. |
323
|
|
|
* |
324
|
|
|
* @param array $previous_enqueue_status Arr |
325
|
|
|
* |
326
|
|
|
* @return int Return the previous total |
327
|
|
|
*/ |
328
|
|
|
public function recalculate_total( $previous_enqueue_status ) { |
329
|
|
|
return $previous_enqueue_status[0]; // the previous total |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Initialize listeners for the particular meta type. |
334
|
|
|
* |
335
|
|
|
* @access public |
336
|
|
|
* |
337
|
|
|
* @param string $meta_type Meta type. |
338
|
|
|
* @param callable $callable Action handler callable. |
339
|
|
|
*/ |
340
|
|
|
public function init_listeners_for_meta_type( $meta_type, $callable ) { |
341
|
|
|
add_action( "added_{$meta_type}_meta", $callable, 10, 4 ); |
342
|
|
|
add_action( "updated_{$meta_type}_meta", $callable, 10, 4 ); |
343
|
|
|
add_action( "deleted_{$meta_type}_meta", $callable, 10, 4 ); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Initialize meta whitelist handler for the particular meta type. |
348
|
|
|
* |
349
|
|
|
* @access public |
350
|
|
|
* |
351
|
|
|
* @param string $meta_type Meta type. |
352
|
|
|
* @param callable $whitelist_handler Action handler callable. |
353
|
|
|
*/ |
354
|
|
|
public function init_meta_whitelist_handler( $meta_type, $whitelist_handler ) { |
355
|
|
|
add_filter( "jetpack_sync_before_enqueue_added_{$meta_type}_meta", $whitelist_handler ); |
356
|
|
|
add_filter( "jetpack_sync_before_enqueue_updated_{$meta_type}_meta", $whitelist_handler ); |
357
|
|
|
add_filter( "jetpack_sync_before_enqueue_deleted_{$meta_type}_meta", $whitelist_handler ); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Retrieve the term relationships for the specified object IDs. |
362
|
|
|
* |
363
|
|
|
* @access protected |
364
|
|
|
* |
365
|
|
|
* @todo This feels too specific to be in the abstract sync Module class. Move it? |
366
|
|
|
* |
367
|
|
|
* @param array $ids Object IDs. |
368
|
|
|
* @return array Term relationships - object ID and term taxonomy ID pairs. |
369
|
|
|
*/ |
370
|
|
|
protected function get_term_relationships( $ids ) { |
371
|
|
|
global $wpdb; |
372
|
|
|
|
373
|
|
|
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
374
|
|
|
return $wpdb->get_results( "SELECT object_id, term_taxonomy_id FROM $wpdb->term_relationships WHERE object_id IN ( " . implode( ',', wp_parse_id_list( $ids ) ) . ' )', OBJECT ); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* Unserialize the value of a meta object, if necessary. |
379
|
|
|
* |
380
|
|
|
* @access public |
381
|
|
|
* |
382
|
|
|
* @param object $meta Meta object. |
383
|
|
|
* @return object Meta object with possibly unserialized value. |
384
|
|
|
*/ |
385
|
|
|
public function unserialize_meta( $meta ) { |
386
|
|
|
$meta->meta_value = maybe_unserialize( $meta->meta_value ); |
387
|
|
|
return $meta; |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* Retrieve a set of objects by their IDs. |
392
|
|
|
* |
393
|
|
|
* @access public |
394
|
|
|
* |
395
|
|
|
* @param string $object_type Object type. |
396
|
|
|
* @param array $ids Object IDs. |
397
|
|
|
* @return array Array of objects. |
398
|
|
|
*/ |
399
|
|
|
public function get_objects_by_id( $object_type, $ids ) { |
400
|
|
|
if ( empty( $ids ) || empty( $object_type ) ) { |
401
|
|
|
return array(); |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
$objects = array(); |
405
|
|
|
foreach ( (array) $ids as $id ) { |
406
|
|
|
$object = $this->get_object_by_id( $object_type, $id ); |
407
|
|
|
|
408
|
|
|
// Only add object if we have the object. |
409
|
|
|
if ( $object ) { |
410
|
|
|
$objects[ $id ] = $object; |
411
|
|
|
} |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
return $objects; |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* Gets a list of minimum and maximum object ids for each batch based on the given batch size. |
419
|
|
|
* |
420
|
|
|
* @access public |
421
|
|
|
* |
422
|
|
|
* @param int $batch_size The batch size for objects. |
423
|
|
|
* @param string|bool $where_sql The sql where clause minus 'WHERE', or false if no where clause is needed. |
424
|
|
|
* |
425
|
|
|
* @return array|bool An array of min and max ids for each batch. FALSE if no table can be found. |
426
|
|
|
*/ |
427
|
|
|
public function get_min_max_object_ids_for_batches( $batch_size, $where_sql = false ) { |
428
|
|
|
global $wpdb; |
429
|
|
|
|
430
|
|
|
if ( ! $this->table_name() ) { |
431
|
|
|
return false; |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
$results = array(); |
435
|
|
|
$table = $wpdb->{$this->table_name()}; |
436
|
|
|
$current_max = 0; |
437
|
|
|
$current_min = 1; |
438
|
|
|
$id_field = $this->id_field(); |
439
|
|
|
$replicastore = new Replicastore(); |
440
|
|
|
|
441
|
|
|
$total = $replicastore->get_min_max_object_id( |
442
|
|
|
$id_field, |
443
|
|
|
$table, |
444
|
|
|
$where_sql, |
|
|
|
|
445
|
|
|
false |
|
|
|
|
446
|
|
|
); |
447
|
|
|
|
448
|
|
|
while ( $total->max > $current_max ) { |
449
|
|
|
$where = $where_sql ? |
450
|
|
|
$where_sql . " AND $id_field > $current_max" : |
451
|
|
|
"$id_field > $current_max"; |
452
|
|
|
$result = $replicastore->get_min_max_object_id( |
453
|
|
|
$id_field, |
454
|
|
|
$table, |
455
|
|
|
$where, |
456
|
|
|
$batch_size |
457
|
|
|
); |
458
|
|
|
if ( empty( $result->min ) && empty( $result->max ) ) { |
459
|
|
|
// Our query produced no min and max. We can assume the min from the previous query, |
460
|
|
|
// and the total max we found in the initial query. |
461
|
|
|
$current_max = (int) $total->max; |
462
|
|
|
$result = (object) array( |
463
|
|
|
'min' => $current_min, |
464
|
|
|
'max' => $current_max, |
465
|
|
|
); |
466
|
|
|
} else { |
467
|
|
|
$current_min = (int) $result->min; |
468
|
|
|
$current_max = (int) $result->max; |
469
|
|
|
} |
470
|
|
|
$results[] = $result; |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
return $results; |
474
|
|
|
} |
475
|
|
|
} |
476
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.