Completed
Push — fix/photon-filter-srcset ( 8990e0...8f0544 )
by
unknown
122:55 queued 111:44
created

Jetpack_Sync_Module::unserialize_meta()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
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_full_sync_listeners( $callable ) {
16
	}
17
18
	public function init_before_send() {
19
	}
20
21
	public function set_defaults() {
22
	}
23
24
	public function reset_data() {
25
	}
26
27
	public function enqueue_full_sync_actions( $config ) {
28
		// in subclasses, return the number of items enqueued
29
		return 0;
30
	}
31
32
	public function estimate_full_sync_actions( $config ) {
33
		// in subclasses, return the number of items yet to be enqueued
34
		return 0;
35
	}
36
37
	public function get_full_sync_actions() {
38
		return array();
39
	}
40
41
	protected function count_actions( $action_names, $actions_to_count ) {
42
		return count( array_intersect( $action_names, $actions_to_count ) );
43
	}
44
45
	protected function get_check_sum( $values ) {
46
		return crc32( json_encode( $values ) );
47
	}
48
49
	protected function still_valid_checksum( $sums_to_check, $name, $new_sum ) {
50
		if ( isset( $sums_to_check[ $name ] ) && $sums_to_check[ $name ] === $new_sum ) {
51
			return true;
52
		}
53
54
		return false;
55
	}
56
57
	protected function enqueue_all_ids_as_action( $action_name, $table_name, $id_field, $where_sql ) {
58
		global $wpdb;
59
60
		if ( ! $where_sql ) {
61
			$where_sql = '1 = 1';
62
		}
63
64
		$items_per_page = 1000;
65
		$page           = 1;
66
		$chunk_count    = 0;
67
		$previous_id    = 0;
68
		$listener       = Jetpack_Sync_Listener::get_instance();
69
		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}" ) ) {
70
			// Request posts in groups of N for efficiency
71
			$chunked_ids = array_chunk( $ids, self::ARRAY_CHUNK_SIZE );
72
73
			$listener->bulk_enqueue_full_sync_actions( $action_name, $chunked_ids );
74
75
			$chunk_count += count( $chunked_ids );
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 array_map( 
92
			array( $this, 'unserialize_meta' ), 
93
			$wpdb->get_results( "SELECT $id, meta_key, meta_value, meta_id FROM $table WHERE $id IN ( " . implode( ',', wp_parse_id_list( $ids ) ) . ' )', OBJECT ) 
94
		);
95
	}
96
97
	protected function get_term_relationships( $ids ) {
98
		global $wpdb;
99
100
		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 );
101
	}
102
103
	public function unserialize_meta( $meta ) {
104
		$meta->meta_value = maybe_unserialize( $meta->meta_value );
105
		return $meta;
106
	}
107
}
108