Completed
Push — add/sync-rest-2 ( 59fd01...a434af )
by
unknown
66:44 queued 57:55
created

Jetpack_Sync_Full::start()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 0 Features 1
Metric Value
c 8
b 0
f 1
dl 0
loc 13
rs 9.4285
cc 1
eloc 11
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 jetpack_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
		$this->client = Jetpack_Sync_Client::getInstance();
0 ignored issues
show
Bug introduced by
The property client does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
20
		do_action( 'jetpack_full_sync_start' );
21
22
		$this->enqueue_wp_version();
23
		$this->enqueue_all_constants();
24
		$this->enqueue_all_functions();
25
		$this->enqueue_all_options();
26
		$this->enqueue_all_theme_info();
27
		$this->enqueue_all_posts();
28
		$this->enqueue_all_comments();
29
		$this->enqueue_all_updates();
30
	}
31
32
	private function enqueue_wp_version() {
33
		global $wp_version;
34
		do_action( 'jetpack_sync_wp_version', $wp_version );
35
	}
36
37
	private function enqueue_all_constants() {
38
		$this->client->force_sync_constants();
39
	}
40
41
	private function enqueue_all_functions() {
42
		$this->client->force_sync_callables();
43
	}
44
45
	private function enqueue_all_options() {
46
		global $wpdb;
47
48
		// Unfortunately, since our options whitelist includes regexes,
49
		// we need to load all option names and match them against the whitelist.
50
		// This could be pretty awful if we have huge queues, but it's the only way to 
51
		// be sure we're syncing everything that's whitelisted.
52
53
		// As per posts and comments, we do this in ID batches and hope the IDs *AND* names don't exceed RAM
54
55
		// In theory, MySQL has regex support. In practice, I wouldn't want to rely on it being compatible
56
		// with PHP's regexes.
57
58
		// Alternatively, rather than option regexes, we could use wildcards in the option and then just
59
		// use "LIKE" queries here, replacing * with %?
60
61
		$option_names = $wpdb->get_col( "SELECT option_name FROM $wpdb->options" );
62
63
		// filter by client option whitelist
64
		$option_names = array_filter( $option_names, array( $this->client, 'is_whitelisted_option' ) );
65
66
		foreach ( $option_names as $option_name ) {
67
			do_action( 'jetpack_full_sync_option', $option_name, get_option( $option_name ) );
68
		}
69
	}
70
71 View Code Duplication
	private function enqueue_all_posts() {
72
		global $wpdb;
73
74
		// I hope this is never bigger than RAM...
75
		$post_ids = $wpdb->get_col( "SELECT id FROM $wpdb->posts");
76
77
		// Request posts in groups of N for efficiency
78
		$chunked_post_ids = array_chunk( $post_ids, self::$array_chunk_size );
79
80
		// Send each chunk as an array of objects
81
		foreach ( $chunked_post_ids as $chunk ) {
82
			do_action( 'jetpack_full_sync_posts', $chunk );
83
		}
84
	}
85
86 View Code Duplication
	private function enqueue_all_comments() {
87
		global $wpdb;
88
89
		$comment_ids = $wpdb->get_col( "SELECT comment_id FROM $wpdb->comments");
90
		$chunked_comment_ids = array_chunk( $comment_ids, self::$array_chunk_size );
91
92
		foreach ( $chunked_comment_ids as $chunk ) {
93
			do_action( 'jetpack_full_sync_comments', $chunk);
94
		}
95
	}
96
97
	// 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...
98
	private function enqueue_all_theme_info() {
99
		$this->client->send_theme_info();
100
	}
101
102
	private function enqueue_all_updates() {
103
		// check for updates
104
		wp_update_plugins();
105
		wp_update_themes();
106
		_maybe_update_core();
107
	}
108
	
109
}