Completed
Push — fix/json-api-site-endpoint ( 62d6c2...e4ff06 )
by
unknown
10:56
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 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 3
c 1
b 1
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
		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}" ) ) {
69
			// Request posts in groups of N for efficiency
70
			$chunked_ids = array_chunk( $ids, self::ARRAY_CHUNK_SIZE );
71
72
			// Send each chunk as an array of objects
73
			foreach ( $chunked_ids as $chunk ) {
74
				/**
75
				 * Fires with a chunk of object IDs during full sync.
76
				 * These are expanded to full objects before upload
77
				 *
78
				 * @since 4.2.0
79
				 */
80
				do_action( $action_name, $chunk );
81
				$chunk_count ++;
82
			}
83
84
			$page += 1;
85
			$previous_id = end( $ids );
86
		}
87
88
		return $chunk_count;
89
	}
90
91
	protected function get_metadata( $ids, $meta_type ) {
92
		global $wpdb;
93
		$table = _get_meta_table( $meta_type );
94
		$id    = $meta_type . '_id';
95
		if ( ! $table ) {
96
			return array();
97
		}
98
99
		return array_map( 
100
			array( $this, 'unserialize_meta' ), 
101
			$wpdb->get_results( "SELECT $id, meta_key, meta_value, meta_id FROM $table WHERE $id IN ( " . implode( ',', wp_parse_id_list( $ids ) ) . ' )', OBJECT ) 
102
		);
103
	}
104
105
	protected function get_term_relationships( $ids ) {
106
		global $wpdb;
107
108
		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 );
109
	}
110
111
	public function unserialize_meta( $meta ) {
112
		$meta->meta_value = maybe_unserialize( $meta->meta_value );
113
		return $meta;
114
	}
115
}
116