1 | <?php |
||
17 | abstract class Module { |
||
18 | /** |
||
19 | * Number of items per chunk when grouping objects for performance reasons. |
||
20 | * |
||
21 | * @access public |
||
22 | * |
||
23 | * @var int |
||
24 | */ |
||
25 | const ARRAY_CHUNK_SIZE = 10; |
||
26 | |||
27 | /** |
||
28 | * Sync module name. |
||
29 | * |
||
30 | * @access public |
||
31 | * |
||
32 | * @return string |
||
33 | */ |
||
34 | abstract public function name(); |
||
35 | |||
36 | // phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
37 | |||
38 | /** |
||
39 | * Retrieve a sync object by its ID. |
||
40 | * |
||
41 | * @access public |
||
42 | * |
||
43 | * @param string $object_type Type of the sync object. |
||
44 | * @param int $id ID of the sync object. |
||
45 | * @return mixed Object, or false if the object is invalid. |
||
46 | */ |
||
47 | public function get_object_by_id( $object_type, $id ) { |
||
50 | |||
51 | /** |
||
52 | * Initialize callables action listeners. |
||
53 | * Override these to set up listeners and set/reset data/defaults. |
||
54 | * |
||
55 | * @access public |
||
56 | * |
||
57 | * @param callable $callable Action handler callable. |
||
58 | */ |
||
59 | public function init_listeners( $callable ) { |
||
61 | |||
62 | /** |
||
63 | * Initialize module action listeners for full sync. |
||
64 | * |
||
65 | * @access public |
||
66 | * |
||
67 | * @param callable $callable Action handler callable. |
||
68 | */ |
||
69 | public function init_full_sync_listeners( $callable ) { |
||
71 | |||
72 | /** |
||
73 | * Initialize the module in the sender. |
||
74 | * |
||
75 | * @access public |
||
76 | */ |
||
77 | public function init_before_send() { |
||
79 | |||
80 | /** |
||
81 | * Set module defaults. |
||
82 | * |
||
83 | * @access public |
||
84 | */ |
||
85 | public function set_defaults() { |
||
87 | |||
88 | /** |
||
89 | * Perform module cleanup. |
||
90 | * Usually triggered when uninstalling the plugin. |
||
91 | * |
||
92 | * @access public |
||
93 | */ |
||
94 | public function reset_data() { |
||
96 | |||
97 | /** |
||
98 | * Enqueue the module actions for full sync. |
||
99 | * |
||
100 | * @access public |
||
101 | * |
||
102 | * @param array $config Full sync configuration for this sync module. |
||
103 | * @param int $max_items_to_enqueue Maximum number of items to enqueue. |
||
104 | * @param boolean $state True if full sync has finished enqueueing this module, false otherwise. |
||
105 | * @return array Number of actions enqueued, and next module state. |
||
106 | */ |
||
107 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
||
111 | |||
112 | /** |
||
113 | * Retrieve an estimated number of actions that will be enqueued. |
||
114 | * |
||
115 | * @access public |
||
116 | * |
||
117 | * @param array $config Full sync configuration for this sync module. |
||
118 | * @return array Number of items yet to be enqueued. |
||
119 | */ |
||
120 | public function estimate_full_sync_actions( $config ) { |
||
124 | |||
125 | // phpcs:enable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
126 | |||
127 | /** |
||
128 | * Retrieve the actions that will be sent for this module during a full sync. |
||
129 | * |
||
130 | * @access public |
||
131 | * |
||
132 | * @return array Full sync actions of this module. |
||
133 | */ |
||
134 | public function get_full_sync_actions() { |
||
137 | |||
138 | /** |
||
139 | * Get the number of actions that we care about. |
||
140 | * |
||
141 | * @access protected |
||
142 | * |
||
143 | * @param array $action_names Action names we're interested in. |
||
144 | * @param array $actions_to_count Unfiltered list of actions we want to count. |
||
145 | * @return array Number of actions that we're interested in. |
||
146 | */ |
||
147 | protected function count_actions( $action_names, $actions_to_count ) { |
||
150 | |||
151 | /** |
||
152 | * Calculate the checksum of one or more values. |
||
153 | * |
||
154 | * @access protected |
||
155 | * |
||
156 | * @param mixed $values Values to calculate checksum for. |
||
157 | * @return int The checksum. |
||
158 | */ |
||
159 | protected function get_check_sum( $values ) { |
||
162 | |||
163 | /** |
||
164 | * Whether a particular checksum in a set of checksums is valid. |
||
165 | * |
||
166 | * @access protected |
||
167 | * |
||
168 | * @param array $sums_to_check Array of checksums. |
||
169 | * @param string $name Name of the checksum. |
||
170 | * @param int $new_sum Checksum to compare against. |
||
171 | * @return boolean Whether the checksum is valid. |
||
172 | */ |
||
173 | protected function still_valid_checksum( $sums_to_check, $name, $new_sum ) { |
||
180 | |||
181 | /** |
||
182 | * Enqueue all items of a sync type as an action. |
||
183 | * |
||
184 | * @access protected |
||
185 | * |
||
186 | * @param string $action_name Name of the action. |
||
187 | * @param string $table_name Name of the database table. |
||
188 | * @param string $id_field Name of the ID field in the database. |
||
189 | * @param string $where_sql The SQL WHERE clause to filter to the desired items. |
||
190 | * @param int $max_items_to_enqueue Maximum number of items to enqueue in the same time. |
||
191 | * @param boolean $state Whether enqueueing has finished. |
||
192 | * @return array Array, containing the number of chunks and TRUE, indicating enqueueing has finished. |
||
193 | */ |
||
194 | protected function enqueue_all_ids_as_action( $action_name, $table_name, $id_field, $where_sql, $max_items_to_enqueue, $state ) { |
||
235 | |||
236 | /** |
||
237 | * Retrieve chunk IDs with previous interval end. |
||
238 | * |
||
239 | * @access private |
||
240 | * |
||
241 | * @param array $chunks All remaining items. |
||
242 | * @param int $previous_interval_end The last item from the previous interval. |
||
243 | * @return array Chunk IDs with the previous interval end. |
||
244 | */ |
||
245 | private function get_chunks_with_preceding_end( $chunks, $previous_interval_end ) { |
||
257 | |||
258 | /** |
||
259 | * Get metadata of a particular object type within the designated meta key whitelist. |
||
260 | * |
||
261 | * @access protected |
||
262 | * |
||
263 | * @todo Refactor to use $wpdb->prepare() on the SQL query. |
||
264 | * |
||
265 | * @param array $ids Object IDs. |
||
266 | * @param string $meta_type Meta type. |
||
267 | * @param array $meta_key_whitelist Meta key whitelist. |
||
268 | * @return array Unserialized meta values. |
||
269 | */ |
||
270 | protected function get_metadata( $ids, $meta_type, $meta_key_whitelist ) { |
||
291 | |||
292 | /** |
||
293 | * Initialize listeners for the particular meta type. |
||
294 | * |
||
295 | * @access public |
||
296 | * |
||
297 | * @param string $meta_type Meta type. |
||
298 | * @param callable $callable Action handler callable. |
||
299 | */ |
||
300 | public function init_listeners_for_meta_type( $meta_type, $callable ) { |
||
305 | |||
306 | /** |
||
307 | * Initialize meta whitelist handler for the particular meta type. |
||
308 | * |
||
309 | * @access public |
||
310 | * |
||
311 | * @param string $meta_type Meta type. |
||
312 | * @param callable $whitelist_handler Action handler callable. |
||
313 | */ |
||
314 | public function init_meta_whitelist_handler( $meta_type, $whitelist_handler ) { |
||
319 | |||
320 | /** |
||
321 | * Retrieve the term relationships for the specified object IDs. |
||
322 | * |
||
323 | * @access protected |
||
324 | * |
||
325 | * @todo This feels too specific to be in the abstract sync Module class. Move it? |
||
326 | * |
||
327 | * @param array $ids Object IDs. |
||
328 | * @return array Term relationships - object ID and term taxonomy ID pairs. |
||
329 | */ |
||
330 | protected function get_term_relationships( $ids ) { |
||
336 | |||
337 | /** |
||
338 | * Unserialize the value of a meta object, if necessary. |
||
339 | * |
||
340 | * @access public |
||
341 | * |
||
342 | * @param object $meta Meta object. |
||
343 | * @return object Meta object with possibly unserialized value. |
||
344 | */ |
||
345 | public function unserialize_meta( $meta ) { |
||
349 | |||
350 | /** |
||
351 | * Retrieve a set of objects by their IDs. |
||
352 | * |
||
353 | * @access public |
||
354 | * |
||
355 | * @param string $object_type Object type. |
||
356 | * @param array $ids Object IDs. |
||
357 | * @return array Array of objects. |
||
358 | */ |
||
359 | public function get_objects_by_id( $object_type, $ids ) { |
||
376 | } |
||
377 |