Completed
Push — fix/refactor-full-sync-status-... ( d41df7 )
by
unknown
55:42 queued 45:27
created

Jetpack_Sync_Module::init_before_send()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 1
rs 10
1
<?php
2
3
/**
4
 * Basic methods implemented by Jetpack Sync extensions
5
 */
6
7
abstract class Jetpack_Sync_Module {
8
	const ARRAY_CHUNK_SIZE = 10;
9
10
	abstract public function name();
11
12
	// override these to set up listeners and set/reset data/defaults
13
	public function init_listeners( $callable ) {}
14
	public function init_before_send() {}
15
	public function set_defaults() {}
16
	public function reset_data() {}
17
	public function enqueue_full_sync_actions() {
18
		// in subclasses, return the number of items enqueued
19
		return 0;
20
	}
21
22
	final public function full_sync() {
23
		$items_enqueued = $this->enqueue_full_sync_actions();
24
		if ( $items_enqueued !== 0 ) {
25
			$this->update_queue_progress( $this->name(), $items_enqueued );	
26
		}
27
	}
28
29
	protected function get_check_sum( $values ) {
30
		return crc32( json_encode( $values ) );
31
	}
32
33
	protected function still_valid_checksum( $sums_to_check, $name, $new_sum ) {
34
		if ( isset( $sums_to_check[ $name ] ) && $sums_to_check[ $name ] === $new_sum ) {
35
			return true;
36
		}
37
		return false;
38
	}
39
40
	public function update_queue_progress( $module, $data ) {
41
		$full_sync_module = Jetpack_Sync_Modules::get_module( 'full-sync' );
42
		$status = $full_sync_module->get_status();
43
		if ( isset( $status['queue'][ $module ] ) )  {
44
			$status['queue'][ $module ] = $data + $status['queue'][ $module ];
45
		} else {
46
			$status['queue'][ $module ] = $data;
47
		}
48
49
		return $full_sync_module->update_status( $status );
50
	}
51
52
	protected function enqueue_all_ids_as_action( $action_name, $table_name, $id_field, $where_sql ) {
53
		global $wpdb;
54
55
		if ( ! $where_sql ) {
56
			$where_sql = "1 = 1";
57
		}
58
59
		$items_per_page = 500;
60
		$page = 1;
61
		$offset = ( $page * $items_per_page ) - $items_per_page;
62
		$chunk_count = 0;
63
		while( $ids = $wpdb->get_col( "SELECT {$id_field} FROM {$table_name} WHERE {$where_sql} ORDER BY {$id_field} asc LIMIT {$offset}, {$items_per_page}" ) ) {
64
			// Request posts in groups of N for efficiency
65
			$chunked_ids = array_chunk( $ids, self::ARRAY_CHUNK_SIZE );
66
67
			// Send each chunk as an array of objects
68
			foreach ( $chunked_ids as $chunk ) {
69
				/**
70
			 	 * Fires with a chunk of object IDs during full sync.
71
			 	 * These are expanded to full objects before upload
72
			 	 *
73
			 	 * @since 4.2.0
74
			 	 */
75
				do_action( $action_name, $chunk );
76
				$chunk_count++;
77
			}
78
79
			$page += 1;
80
			$offset = ( $page * $items_per_page ) - $items_per_page;
81
		}
82
		return $chunk_count;
83
	}
84
85
	protected function get_metadata( $ids, $meta_type ) {
86
		global $wpdb;
87
		$table = _get_meta_table( $meta_type );
88
		$id    = $meta_type . '_id';
89
		if ( ! $table ) {
90
			return array();
91
		}
92
93
		return $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
	protected function get_term_relationships( $ids ) {
97
		global $wpdb;
98
99
		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 );
100
	}
101
}
102