Completed
Push — add/sync-rest-2 ( e0eac0...35d25f )
by
unknown
323:03 queued 313:25
created

Jetpack_Sync_Full::enqueue_wp_version()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 3
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
		$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( 'jp_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( 'jp_full_sync_option', $option_name, get_option( $option_name ) );
68
		}
69
	}
70
71
	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
			$posts = get_posts( array( 'post__in' => $chunk, 'post_status' => 'any' ) );
83
			do_action( 'jp_full_sync_posts', $posts );
84
85
			// while we're here, sync post meta
86
			foreach( $posts as $post ) {
87
				$postmeta = $wpdb->get_results( 
88
					$wpdb->prepare( 
89
						"SELECT meta_id, meta_key, meta_value FROM $wpdb->postmeta WHERE post_id = %s", 
90
						$post->ID 
91
					),
92
					OBJECT
93
				);
94
				do_action( 'jp_full_sync_postmeta', $post->ID, $postmeta );
95
			}
96
		}
97
	}
98
99
	private function enqueue_all_comments() {
100
		global $wpdb;
101
102
		$comment_ids = $wpdb->get_col( "SELECT comment_id FROM $wpdb->comments");
103
		$chunked_comment_ids = array_chunk( $comment_ids, self::$array_chunk_size );
104
105
		foreach ( $chunked_comment_ids as $chunk ) {
106
			$comments = get_comments( array( 'comment__in' => $chunk ) );
107
			do_action( 'jp_full_sync_comments', $comments );
108
		}
109
	}
110
111
	// 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...
112
	private function enqueue_all_theme_info() {
113
		$this->client->send_theme_info();
114
	}
115
116
	private function enqueue_all_updates() {
117
		// check for updates
118
		wp_update_plugins();
119
		wp_update_themes();
120
		_maybe_update_core();
121
	}
122
	
123
}