Completed
Push — add/google-maps/api-key ( 5599f7...90239a )
by
unknown
26:57 queued 16:47
created

Jetpack_Sync_Module   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 94
rs 10
wmc 18
lcom 0
cbo 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A get_check_sum() 0 3 1
name() 0 1 ?
A enqueue_full_sync_actions() 0 4 1
A get_metadata() 0 10 2
A get_term_relationships() 0 5 1
A get_full_sync_actions() 0 3 1
A count_actions() 0 3 1
B enqueue_all_ids_as_action() 0 33 4
A init_listeners() 0 2 1
A init_before_send() 0 2 1
A set_defaults() 0 2 1
A reset_data() 0 2 1
A still_valid_checksum() 0 7 3
1
<?php
2
3
/**
4
 * Basic methods implemented by Jetpack Sync extensions
5
 */
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 ) {
38
		return crc32( json_encode( $values ) );
39
	}
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 ) {
84
		global $wpdb;
85
		$table = _get_meta_table( $meta_type );
86
		$id    = $meta_type . '_id';
87
		if ( ! $table ) {
88
			return array();
89
		}
90
91
		return $wpdb->get_results( "SELECT $id, meta_key, meta_value, meta_id FROM $table WHERE $id IN ( " . implode( ',', wp_parse_id_list( $ids ) ) . ' )', OBJECT );
92
	}
93
94
	protected function get_term_relationships( $ids ) {
95
		global $wpdb;
96
97
		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 );
98
	}
99
}
100