|
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; |
|
|
|
|
|
|
17
|
|
|
static $status_transient_name = "jp_full_sync_progress"; |
|
|
|
|
|
|
18
|
|
|
static $transient_timeout = 3600; // an hour |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
function start() { |
|
21
|
|
|
$this->client = Jetpack_Sync_Client::getInstance(); |
|
|
|
|
|
|
22
|
|
|
do_action( 'jetpack_full_sync_start' ); |
|
23
|
|
|
|
|
24
|
|
|
$this->set_status_queuing_started(); |
|
25
|
|
|
|
|
26
|
|
|
$this->enqueue_wp_version(); |
|
27
|
|
|
$this->enqueue_all_constants(); |
|
28
|
|
|
$this->enqueue_all_functions(); |
|
29
|
|
|
$this->enqueue_all_options(); |
|
30
|
|
|
$this->enqueue_all_theme_info(); |
|
31
|
|
|
$this->enqueue_all_posts(); |
|
32
|
|
|
$this->enqueue_all_comments(); |
|
33
|
|
|
$this->enqueue_all_updates(); |
|
34
|
|
|
|
|
35
|
|
|
$this->set_status_queuing_finished(); |
|
36
|
|
|
|
|
37
|
|
|
do_action( 'jp_full_sync_end' ); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
private function enqueue_wp_version() { |
|
41
|
|
|
$this->set_status("wp_version", 0); |
|
42
|
|
|
global $wp_version; |
|
43
|
|
|
do_action( 'jetpack_sync_wp_version', $wp_version ); |
|
44
|
|
|
$this->set_status("wp_version", 100); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
private function enqueue_all_constants() { |
|
48
|
|
|
$this->set_status("constants", 0); |
|
49
|
|
|
$this->client->force_sync_constants(); |
|
50
|
|
|
$this->set_status("constants", 100); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
private function enqueue_all_functions() { |
|
54
|
|
|
$this->set_status("functions", 0); |
|
55
|
|
|
$this->client->force_sync_callables(); |
|
56
|
|
|
$this->set_status("functions", 100); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
private function enqueue_all_options() { |
|
60
|
|
|
$this->set_status("options", 0); |
|
61
|
|
|
global $wpdb; |
|
62
|
|
|
|
|
63
|
|
|
// Unfortunately, since our options whitelist includes regexes, |
|
64
|
|
|
// we need to load all option names and match them against the whitelist. |
|
65
|
|
|
// This could be pretty awful if we have huge queues, but it's the only way to |
|
66
|
|
|
// be sure we're syncing everything that's whitelisted. |
|
67
|
|
|
|
|
68
|
|
|
// As per posts and comments, we do this in ID batches and hope the IDs *AND* names don't exceed RAM |
|
69
|
|
|
|
|
70
|
|
|
// In theory, MySQL has regex support. In practice, I wouldn't want to rely on it being compatible |
|
71
|
|
|
// with PHP's regexes. |
|
72
|
|
|
|
|
73
|
|
|
// Alternatively, rather than option regexes, we could use wildcards in the option and then just |
|
74
|
|
|
// use "LIKE" queries here, replacing * with %? |
|
75
|
|
|
|
|
76
|
|
|
$option_names = $wpdb->get_col( "SELECT option_name FROM $wpdb->options" ); |
|
77
|
|
|
|
|
78
|
|
|
// filter by client option whitelist |
|
79
|
|
|
$option_names = array_filter( $option_names, array( $this->client, 'is_whitelisted_option' ) ); |
|
80
|
|
|
|
|
81
|
|
|
$counter = 0; |
|
82
|
|
|
$total = count( $option_names ); |
|
83
|
|
|
|
|
84
|
|
|
foreach ( $option_names as $option_name ) { |
|
85
|
|
|
$this->set_status( "options", ( $counter / $total ) * 100 ); |
|
86
|
|
|
do_action( 'jetpack_full_sync_option', $option_name, get_option( $option_name ) ); |
|
87
|
|
|
$counter += 1; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$this->set_status("options", 100); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
View Code Duplication |
private function enqueue_all_posts() { |
|
94
|
|
|
$this->set_status("posts", 0); |
|
95
|
|
|
global $wpdb; |
|
96
|
|
|
|
|
97
|
|
|
// I hope this is never bigger than RAM... |
|
98
|
|
|
$post_ids = $wpdb->get_col( "SELECT id FROM $wpdb->posts"); |
|
99
|
|
|
|
|
100
|
|
|
// Request posts in groups of N for efficiency |
|
101
|
|
|
$chunked_post_ids = array_chunk( $post_ids, self::$array_chunk_size ); |
|
102
|
|
|
|
|
103
|
|
|
$counter = 0; |
|
104
|
|
|
$total = count( $chunked_post_ids ); |
|
105
|
|
|
|
|
106
|
|
|
// Send each chunk as an array of objects |
|
107
|
|
|
foreach ( $chunked_post_ids as $chunk ) { |
|
108
|
|
|
$this->set_status( "posts", ( $counter / $total ) * 100 ); |
|
109
|
|
|
do_action( 'jetpack_full_sync_posts', $chunk ); |
|
110
|
|
|
$counter += 1; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$this->set_status("posts", 100); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
View Code Duplication |
private function enqueue_all_comments() { |
|
117
|
|
|
$this->set_status("comments", 0); |
|
118
|
|
|
|
|
119
|
|
|
global $wpdb; |
|
120
|
|
|
|
|
121
|
|
|
$comment_ids = $wpdb->get_col( "SELECT comment_id FROM $wpdb->comments"); |
|
122
|
|
|
$chunked_comment_ids = array_chunk( $comment_ids, self::$array_chunk_size ); |
|
123
|
|
|
|
|
124
|
|
|
$counter = 0; |
|
125
|
|
|
$total = count( $chunked_comment_ids ); |
|
126
|
|
|
|
|
127
|
|
|
foreach ( $chunked_comment_ids as $chunk ) { |
|
128
|
|
|
$this->set_status( "comments", ( $counter / $total ) * 100 ); |
|
129
|
|
|
do_action( 'jetpack_full_sync_comments', $chunk); |
|
130
|
|
|
$counter += 1; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
$this->set_status("comments", 100); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
// TODO: |
|
|
|
|
|
|
137
|
|
|
private function enqueue_all_theme_info() { |
|
138
|
|
|
$this->set_status("themes", 0); |
|
139
|
|
|
$this->client->send_theme_info(); |
|
140
|
|
|
$this->set_status("themes", 100); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
private function enqueue_all_updates() { |
|
144
|
|
|
$this->set_status("updates", 0); |
|
145
|
|
|
// check for updates |
|
146
|
|
|
wp_update_plugins(); |
|
147
|
|
|
wp_update_themes(); |
|
148
|
|
|
_maybe_update_core(); |
|
149
|
|
|
$this->set_status("updates", 100); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
private function set_status( $name, $percent, $count = 1, $total =1 ) { |
|
153
|
|
|
set_transient( self::$status_transient_name.'_'.$name, array( |
|
154
|
|
|
'phase' => 'preparing', |
|
155
|
|
|
'name' => $name, |
|
156
|
|
|
'progress' => $percent, |
|
157
|
|
|
'count' => $count, |
|
158
|
|
|
'total' => $total ), |
|
159
|
|
|
self::$transient_timeout |
|
160
|
|
|
); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
private function set_status_queuing_started() { |
|
164
|
|
|
set_transient( self::$status_transient_name, array( 'phase' => 'queuing started' ), self::$transient_timeout ); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
private function set_status_queuing_finished() { |
|
168
|
|
|
set_transient( self::$status_transient_name, array( 'phase' => 'queuing finished' ), self::$transient_timeout ); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
// these are called by the Sync Client when it sees that the full sync start/end actions have actually been transmitted |
|
172
|
|
|
public function set_status_sending_started() { |
|
173
|
|
|
set_transient( self::$status_transient_name, array( 'phase' => 'sending started' ), self::$transient_timeout ); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
public function set_status_sending_finished() { |
|
177
|
|
|
set_transient( self::$status_transient_name, array( 'phase' => 'sending finished' ), self::$transient_timeout ); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
public function get_status() { |
|
181
|
|
|
return get_transient( self::$status_transient_name ); |
|
182
|
|
|
} |
|
183
|
|
|
} |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.