Completed
Push — add/sync-rest-2 ( 6a7bf2...29cf06 )
by
unknown
24:44 queued 15:06
created

Jetpack_Sync_Full::start()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
/**
4
 * This class does a full resync of the database by 
5
 * enqueuing an outbound action for every single object
6
 * that we care about.
7
 * 
8
 * This class contains a few non-obvious optimisations that should be explained:
9
 * - we fire an action called jp_full_sync_start so that WPCOM can erase the contents of the cached database
10
 * - for each object type, we obtain a full list of object IDs to sync via a single API call (hoping that since they're ints, they can all fit in RAM)
11
 * - we load the full objects for those IDs in chunks of Jetpack_Sync_Full::$array_chunk_size (to reduce the number of MySQL calls)
12
 * - we fire a trigger for the entire array which the Jetpack_Sync_Client then serializes and queues.
13
 */
14
15
class Jetpack_Sync_Full {
16
	static $array_chunk_size = 5;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $array_chunk_size.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
17
18
	function start() {
19
		// TODO
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
20
		do_action( 'jp_full_sync_start' );
21
		$this->enqueue_all_constants();
22
		$this->enqueue_all_functions();
23
		$this->enqueue_all_posts();
24
		$this->enqueue_all_comments();
25
	}
26
27
	private function enqueue_all_constants() {
28
		$client = Jetpack_Sync_Client::getInstance();
29
		$client->force_sync_constants();
30
	}
31
32
	private function enqueue_all_functions() {
33
		$client = Jetpack_Sync_Client::getInstance();
34
		$client->force_sync_callables();
35
	}
36
37 View Code Duplication
	private function enqueue_all_posts() {
38
		global $wpdb;
39
40
		// I hope this is never bigger than RAM...
41
		$post_ids = $wpdb->get_col( "SELECT id FROM $wpdb->posts");
42
43
		// Request posts in groups of N for efficiency
44
		$chunked_post_ids = array_chunk( $post_ids, self::$array_chunk_size );
45
46
		// Send each chunk as an array of objects
47
		foreach ( $chunked_post_ids as $chunk ) {
48
			$posts = get_posts( array( 'post__in' => $chunk, 'post_status' => 'any' ) );
49
			do_action( 'jp_full_sync_posts', $posts );
50
		}
51
	}
52
53 View Code Duplication
	private function enqueue_all_comments() {
54
		global $wpdb;
55
56
		$comment_ids = $wpdb->get_col( "SELECT comment_id FROM $wpdb->comments");
57
		$chunked_comment_ids = array_chunk( $comment_ids, self::$array_chunk_size );
58
59
		foreach ( $chunked_comment_ids as $chunk ) {
60
			$comments = get_comments( array( 'comment__in' => $chunk ) );
61
			do_action( 'jp_full_sync_comments', $comments );
62
		}
63
	}
64
	
65
}