Completed
Push — update/try-react-0.15 ( ce045e...4c8711 )
by
unknown
26:02 queued 15:13
created

Jetpack_Sync_Module::get_object_by_id()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
	public function get_object_by_id( $object_type, $id ) {
12
		return false;
13
	}
14
15
	// override these to set up listeners and set/reset data/defaults
16
	public function init_listeners( $callable ) {
17
	}
18
19
	public function init_full_sync_listeners( $callable ) {
20
	}
21
22
	public function init_before_send() {
23
	}
24
25
	public function set_defaults() {
26
	}
27
28
	public function reset_data() {
29
	}
30
31
	public function enqueue_full_sync_actions( $config ) {
32
		// in subclasses, return the number of items enqueued
33
		return 0;
34
	}
35
36
	public function estimate_full_sync_actions( $config ) {
37
		// in subclasses, return the number of items yet to be enqueued
38
		return 0;
39
	}
40
41
	public function get_full_sync_actions() {
42
		return array();
43
	}
44
45
	protected function count_actions( $action_names, $actions_to_count ) {
46
		return count( array_intersect( $action_names, $actions_to_count ) );
47
	}
48
49
	protected function get_check_sum( $values ) {
50
		return crc32( json_encode( $values ) );
51
	}
52
53
	protected function still_valid_checksum( $sums_to_check, $name, $new_sum ) {
54
		if ( isset( $sums_to_check[ $name ] ) && $sums_to_check[ $name ] === $new_sum ) {
55
			return true;
56
		}
57
58
		return false;
59
	}
60
61
	protected function enqueue_all_ids_as_action( $action_name, $table_name, $id_field, $where_sql ) {
62
		global $wpdb;
63
64
		if ( ! $where_sql ) {
65
			$where_sql = '1 = 1';
66
		}
67
68
		$items_per_page = 1000;
69
		$page           = 1;
70
		$chunk_count    = 0;
71
		$previous_id    = 0;
72
		$listener       = Jetpack_Sync_Listener::get_instance();
73
		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}" ) ) {
74
			// Request posts in groups of N for efficiency
75
			$chunked_ids = array_chunk( $ids, self::ARRAY_CHUNK_SIZE );
76
77
			$listener->bulk_enqueue_full_sync_actions( $action_name, $chunked_ids );
78
79
			$chunk_count += count( $chunked_ids );
80
			$page += 1;
81
			$previous_id = end( $ids );
82
		}
83
84
		return $chunk_count;
85
	}
86
87
	protected function get_metadata( $ids, $meta_type ) {
88
		global $wpdb;
89
		$table = _get_meta_table( $meta_type );
90
		$id    = $meta_type . '_id';
91
		if ( ! $table ) {
92
			return array();
93
		}
94
95
		$private_meta_whitelist_sql = "'" . implode( "','", array_map( 'esc_sql', Jetpack_Sync_Defaults::$default_whitelist_meta_keys ) ) . "'";
0 ignored issues
show
Bug introduced by
The property default_whitelist_meta_keys cannot be accessed from this context as it is declared private in class Jetpack_Sync_Defaults.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
96
		$public_meta_blacklist_sql = "'" . implode( "','", array_map( 'esc_sql', Jetpack_Sync_Defaults::$default_blacklist_meta_keys ) ) . "'";
0 ignored issues
show
Bug introduced by
The property default_blacklist_meta_keys cannot be accessed from this context as it is declared private in class Jetpack_Sync_Defaults.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
97
98
		return array_map( 
99
			array( $this, 'unserialize_meta' ), 
100
			$wpdb->get_results( 
101
				"SELECT $id, meta_key, meta_value, meta_id FROM $table WHERE $id IN ( " . implode( ',', wp_parse_id_list( $ids ) ) . ' )'.
102
				" AND ( ( meta_key LIKE '\_%' AND meta_key IN ( $private_meta_whitelist_sql ) )".
103
				" OR ( meta_key NOT LIKE '\_%' AND meta_key NOT IN ( $public_meta_blacklist_sql ) ) )",
104
				OBJECT ) 
105
		);
106
	}
107
108
	protected function get_term_relationships( $ids ) {
109
		global $wpdb;
110
111
		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 );
112
	}
113
114
	public function unserialize_meta( $meta ) {
115
		$meta->meta_value = maybe_unserialize( $meta->meta_value );
116
		return $meta;
117
	}
118
119
	public function get_objects_by_id( $object_type, $ids ) {
120
		if ( empty( $ids ) || empty( $object_type ) ) {
121
			return array();
122
		}
123
124
		$objects = array();
125
		foreach( (array) $ids as $id ) {
126
			$object = $this->get_object_by_id( $object_type, $id );
127
128
			// Only add object if we have the object.
129
			if ( $object ) {
130
				$objects[ $id ] = $object;
131
			}
132
		}
133
134
		return $objects;
135
	}
136
}
137