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