1
|
|
|
<? |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace Automattic\Jetpack\Sync\Modules; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Basic methods implemented by Jetpack Sync extensions |
7
|
|
|
*/ |
8
|
|
|
abstract class Module { |
9
|
|
|
const ARRAY_CHUNK_SIZE = 10; |
10
|
|
|
|
11
|
|
|
abstract public function name(); |
12
|
|
|
|
13
|
|
|
public function get_object_by_id( $object_type, $id ) { |
14
|
|
|
return false; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
// override these to set up listeners and set/reset data/defaults |
18
|
|
|
public function init_listeners( $callable ) { |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function init_full_sync_listeners( $callable ) { |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function init_before_send() { |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function set_defaults() { |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function reset_data() { |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
34
|
|
|
// in subclasses, return the number of actions enqueued, and next module state (true == done) |
35
|
|
|
return array( null, true ); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function estimate_full_sync_actions( $config ) { |
39
|
|
|
// in subclasses, return the number of items yet to be enqueued |
40
|
|
|
return null; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function get_full_sync_actions() { |
44
|
|
|
return array(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected function count_actions( $action_names, $actions_to_count ) { |
48
|
|
|
return count( array_intersect( $action_names, $actions_to_count ) ); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
protected function get_check_sum( $values ) { |
52
|
|
|
return crc32( wp_json_encode( jetpack_json_wrap( $values ) ) ); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
protected function still_valid_checksum( $sums_to_check, $name, $new_sum ) { |
56
|
|
|
if ( isset( $sums_to_check[ $name ] ) && $sums_to_check[ $name ] === $new_sum ) { |
57
|
|
|
return true; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return false; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
protected function enqueue_all_ids_as_action( $action_name, $table_name, $id_field, $where_sql, $max_items_to_enqueue, $state ) { |
64
|
|
|
global $wpdb; |
65
|
|
|
|
66
|
|
|
if ( ! $where_sql ) { |
67
|
|
|
$where_sql = '1 = 1'; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$items_per_page = 1000; |
71
|
|
|
$page = 1; |
72
|
|
|
$chunk_count = 0; |
73
|
|
|
$previous_interval_end = $state ? $state : '~0'; |
74
|
|
|
$listener = Listener::get_instance(); |
75
|
|
|
|
76
|
|
|
// count down from max_id to min_id so we get newest posts/comments/etc first |
77
|
|
|
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}" ) ) { |
78
|
|
|
// Request posts in groups of N for efficiency |
79
|
|
|
$chunked_ids = array_chunk( $ids, self::ARRAY_CHUNK_SIZE ); |
80
|
|
|
|
81
|
|
|
// if we hit our row limit, process and return |
82
|
|
|
if ( $chunk_count + count( $chunked_ids ) >= $max_items_to_enqueue ) { |
83
|
|
|
$remaining_items_count = $max_items_to_enqueue - $chunk_count; |
84
|
|
|
$remaining_items = array_slice( $chunked_ids, 0, $remaining_items_count ); |
85
|
|
|
$remaining_items_with_previous_interval_end = $this->get_chunks_with_preceding_end( $remaining_items, $previous_interval_end ); |
86
|
|
|
$listener->bulk_enqueue_full_sync_actions( $action_name, $remaining_items_with_previous_interval_end ); |
87
|
|
|
|
88
|
|
|
$last_chunk = end( $remaining_items ); |
89
|
|
|
return array( $remaining_items_count + $chunk_count, end( $last_chunk ) ); |
90
|
|
|
} |
91
|
|
|
$chunked_ids_with_previous_end = $this->get_chunks_with_preceding_end( $chunked_ids, $previous_interval_end ); |
92
|
|
|
|
93
|
|
|
$listener->bulk_enqueue_full_sync_actions( $action_name, $chunked_ids_with_previous_end ); |
94
|
|
|
|
95
|
|
|
$chunk_count += count( $chunked_ids ); |
96
|
|
|
$page += 1; |
97
|
|
|
// $ids are ordered in descending order |
98
|
|
|
$previous_interval_end = end( $ids ); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return array( $chunk_count, true ); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
private function get_chunks_with_preceding_end( $chunks, $previous_interval_end ) { |
105
|
|
|
foreach ( $chunks as $chunk ) { |
106
|
|
|
$chunks_with_ends[] = array( |
|
|
|
|
107
|
|
|
'ids' => $chunk, |
108
|
|
|
'previous_end' => $previous_interval_end, |
109
|
|
|
); |
110
|
|
|
// Chunks are ordered in descending order |
111
|
|
|
$previous_interval_end = end( $chunk ); |
112
|
|
|
} |
113
|
|
|
return $chunks_with_ends; |
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
protected function get_metadata( $ids, $meta_type, $meta_key_whitelist ) { |
117
|
|
|
global $wpdb; |
118
|
|
|
$table = _get_meta_table( $meta_type ); |
119
|
|
|
$id = $meta_type . '_id'; |
120
|
|
|
if ( ! $table ) { |
121
|
|
|
return array(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$private_meta_whitelist_sql = "'" . implode( "','", array_map( 'esc_sql', $meta_key_whitelist ) ) . "'"; |
125
|
|
|
|
126
|
|
|
return array_map( |
127
|
|
|
array( $this, 'unserialize_meta' ), |
128
|
|
|
$wpdb->get_results( |
129
|
|
|
"SELECT $id, meta_key, meta_value, meta_id FROM $table WHERE $id IN ( " . implode( ',', wp_parse_id_list( $ids ) ) . ' )' . |
130
|
|
|
" AND meta_key IN ( $private_meta_whitelist_sql ) ", |
131
|
|
|
OBJECT |
132
|
|
|
) |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function init_listeners_for_meta_type( $meta_type, $callable ) { |
137
|
|
|
add_action( "added_{$meta_type}_meta", $callable, 10, 4 ); |
138
|
|
|
add_action( "updated_{$meta_type}_meta", $callable, 10, 4 ); |
139
|
|
|
add_action( "deleted_{$meta_type}_meta", $callable, 10, 4 ); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function init_meta_whitelist_handler( $meta_type, $whitelist_handler ) { |
143
|
|
|
add_filter( "jetpack_sync_before_enqueue_added_{$meta_type}_meta", $whitelist_handler ); |
144
|
|
|
add_filter( "jetpack_sync_before_enqueue_updated_{$meta_type}_meta", $whitelist_handler ); |
145
|
|
|
add_filter( "jetpack_sync_before_enqueue_deleted_{$meta_type}_meta", $whitelist_handler ); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
protected function get_term_relationships( $ids ) { |
149
|
|
|
global $wpdb; |
150
|
|
|
|
151
|
|
|
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 ); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function unserialize_meta( $meta ) { |
155
|
|
|
$meta->meta_value = maybe_unserialize( $meta->meta_value ); |
156
|
|
|
return $meta; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function get_objects_by_id( $object_type, $ids ) { |
160
|
|
|
if ( empty( $ids ) || empty( $object_type ) ) { |
161
|
|
|
return array(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$objects = array(); |
165
|
|
|
foreach ( (array) $ids as $id ) { |
166
|
|
|
$object = $this->get_object_by_id( $object_type, $id ); |
167
|
|
|
|
168
|
|
|
// Only add object if we have the object. |
169
|
|
|
if ( $object ) { |
170
|
|
|
$objects[ $id ] = $object; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return $objects; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
Short opening tags are disabled in PHP’s default configuration. In such a case, all content of this file is output verbatim to the browser without being parsed, or executed.
As a precaution to avoid these problems better use the long opening tag
<?php
.