1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Basic methods implemented by Jetpack Sync extensions |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
abstract class Jetpack_Sync_Module { |
8
|
|
|
const ARRAY_CHUNK_SIZE = 10; |
9
|
|
|
|
10
|
|
|
abstract public function name(); |
11
|
|
|
|
12
|
|
|
// override these to set up listeners and set/reset data/defaults |
13
|
|
|
public function init_listeners( $callable ) {} |
14
|
|
|
public function init_before_send() {} |
15
|
|
|
public function set_defaults() {} |
16
|
|
|
public function reset_data() {} |
17
|
|
|
public function enqueue_full_sync_actions() { |
18
|
|
|
// in subclasses, return the number of items enqueued |
19
|
|
|
return 0; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function get_full_sync_actions() { |
23
|
|
|
return array(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
protected function count_actions( $action_names, $actions_to_count ) { |
27
|
|
|
return count( array_intersect( $action_names, $actions_to_count ) ); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
protected function get_check_sum( $values ) { |
31
|
|
|
return crc32( json_encode( $values ) ); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
protected function still_valid_checksum( $sums_to_check, $name, $new_sum ) { |
35
|
|
|
if ( isset( $sums_to_check[ $name ] ) && $sums_to_check[ $name ] === $new_sum ) { |
36
|
|
|
return true; |
37
|
|
|
} |
38
|
|
|
return false; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function enqueue_all_ids_as_action( $action_name, $table_name, $id_field, $where_sql ) { |
42
|
|
|
global $wpdb; |
43
|
|
|
|
44
|
|
|
if ( ! $where_sql ) { |
45
|
|
|
$where_sql = "1 = 1"; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$items_per_page = 500; |
49
|
|
|
$page = 1; |
50
|
|
|
$offset = ( $page * $items_per_page ) - $items_per_page; |
51
|
|
|
$chunk_count = 0; |
52
|
|
|
while( $ids = $wpdb->get_col( "SELECT {$id_field} FROM {$table_name} WHERE {$where_sql} ORDER BY {$id_field} asc LIMIT {$offset}, {$items_per_page}" ) ) { |
53
|
|
|
// Request posts in groups of N for efficiency |
54
|
|
|
$chunked_ids = array_chunk( $ids, self::ARRAY_CHUNK_SIZE ); |
55
|
|
|
|
56
|
|
|
// Send each chunk as an array of objects |
57
|
|
|
foreach ( $chunked_ids as $chunk ) { |
58
|
|
|
/** |
59
|
|
|
* Fires with a chunk of object IDs during full sync. |
60
|
|
|
* These are expanded to full objects before upload |
61
|
|
|
* |
62
|
|
|
* @since 4.2.0 |
63
|
|
|
*/ |
64
|
|
|
do_action( $action_name, $chunk ); |
65
|
|
|
$chunk_count++; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$page += 1; |
69
|
|
|
$offset = ( $page * $items_per_page ) - $items_per_page; |
70
|
|
|
} |
71
|
|
|
return $chunk_count; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
protected function get_metadata( $ids, $meta_type ) { |
75
|
|
|
global $wpdb; |
76
|
|
|
$table = _get_meta_table( $meta_type ); |
77
|
|
|
$id = $meta_type . '_id'; |
78
|
|
|
if ( ! $table ) { |
79
|
|
|
return array(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $wpdb->get_results( "SELECT $id, meta_key, meta_value, meta_id FROM $table WHERE $id IN ( " . implode( ',', wp_parse_id_list( $ids ) ) . " )", OBJECT ); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
protected function get_term_relationships( $ids ) { |
86
|
|
|
global $wpdb; |
87
|
|
|
|
88
|
|
|
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 ); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|