|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
// POST /sites/%s/sync |
|
4
|
|
|
class Jetpack_JSON_API_Sync_Endpoint extends Jetpack_JSON_API_Endpoint { |
|
5
|
|
|
protected $needed_capabilities = 'manage_options'; |
|
6
|
|
|
|
|
7
|
|
|
protected function validate_call( $_blog_id, $capability, $check_manage_active = true ) { |
|
8
|
|
|
parent::validate_call( $_blog_id, $capability, false ); |
|
9
|
|
|
} |
|
10
|
|
|
|
|
11
|
|
|
protected function result() { |
|
12
|
|
|
$args = $this->input(); |
|
13
|
|
|
|
|
14
|
|
|
$modules = null; |
|
15
|
|
|
|
|
16
|
|
|
// convert list of modules in comma-delimited format into an array |
|
17
|
|
|
// of "$modulename => true" |
|
18
|
|
View Code Duplication |
if ( isset( $args['modules'] ) && ! empty( $args['modules'] ) ) { |
|
19
|
|
|
$modules = array_map( '__return_true', array_flip( array_map( 'trim', explode( ',', $args['modules'] ) ) ) ); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
foreach ( array( 'posts', 'comments', 'users' ) as $module_name ) { |
|
23
|
|
|
if ( 'users' === $module_name && isset( $args[ $module_name ] ) && 'initial' === $args[ $module_name ] ) { |
|
24
|
|
|
$modules[ 'users' ] = 'initial'; |
|
25
|
|
|
} elseif ( isset( $args[ $module_name ] ) ) { |
|
26
|
|
|
$ids = explode( ',', $args[ $module_name ] ); |
|
27
|
|
|
if ( count( $ids ) > 0 ) { |
|
28
|
|
|
$modules[ $module_name ] = $ids; |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
if ( empty( $modules ) ) { |
|
34
|
|
|
$modules = null; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
return array( 'started' => Jetpack_Sync_Actions::do_full_sync( $modules ) ); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
protected function validate_queue( $query ) { |
|
41
|
|
|
if ( ! isset( $query ) ) { |
|
42
|
|
|
return new WP_Error( 'invalid_queue', 'Queue name is required', 400 ); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
View Code Duplication |
if ( ! in_array( $query, array( 'sync', 'full_sync' ) ) ) { |
|
46
|
|
|
return new WP_Error( 'invalid_queue', 'Queue name should be sync or full_sync', 400 ); |
|
47
|
|
|
} |
|
48
|
|
|
return $query; |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
// GET /sites/%s/sync/status |
|
53
|
|
|
class Jetpack_JSON_API_Sync_Status_Endpoint extends Jetpack_JSON_API_Sync_Endpoint { |
|
|
|
|
|
|
54
|
|
|
protected function result() { |
|
55
|
|
|
require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-modules.php'; |
|
56
|
|
|
require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php'; |
|
57
|
|
|
|
|
58
|
|
|
$sync_module = Jetpack_Sync_Modules::get_module( 'full-sync' ); |
|
59
|
|
|
$sender = Jetpack_Sync_Sender::get_instance(); |
|
60
|
|
|
$queue = $sender->get_sync_queue(); |
|
61
|
|
|
$full_queue = $sender->get_full_sync_queue(); |
|
62
|
|
|
$cron_timestamps = array_keys( _get_cron_array() ); |
|
63
|
|
|
$cron_age = microtime( true ) - $cron_timestamps[0]; |
|
64
|
|
|
|
|
65
|
|
|
return array_merge( |
|
66
|
|
|
$sync_module->get_status(), |
|
67
|
|
|
array( |
|
68
|
|
|
'cron_size' => count( $cron_timestamps ), |
|
69
|
|
|
'oldest_cron' => $cron_age, |
|
70
|
|
|
'queue_size' => $queue->size(), |
|
71
|
|
|
'queue_lag' => $queue->lag(), |
|
72
|
|
|
'queue_next_sync' => ( $sender->get_next_sync_time( 'sync' ) - microtime( true ) ), |
|
73
|
|
|
'full_queue_size' => $full_queue->size(), |
|
74
|
|
|
'full_queue_lag' => $full_queue->lag(), |
|
75
|
|
|
'full_queue_next_sync' => ( $sender->get_next_sync_time( 'full_sync' ) - microtime( true ) ), |
|
76
|
|
|
) |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
// GET /sites/%s/data-check |
|
82
|
|
|
class Jetpack_JSON_API_Sync_Check_Endpoint extends Jetpack_JSON_API_Sync_Endpoint { |
|
|
|
|
|
|
83
|
|
|
protected function result() { |
|
84
|
|
|
require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php'; |
|
85
|
|
|
|
|
86
|
|
|
$sender = Jetpack_Sync_Sender::get_instance(); |
|
87
|
|
|
$sync_queue = $sender->get_sync_queue(); |
|
88
|
|
|
|
|
89
|
|
|
// lock sending from the queue while we compare checksums with the server |
|
90
|
|
|
$result = $sync_queue->lock( 30 ); // tries to acquire the lock for up to 30 seconds |
|
91
|
|
|
|
|
92
|
|
|
if ( ! $result ) { |
|
93
|
|
|
$sync_queue->unlock(); |
|
94
|
|
|
|
|
95
|
|
|
return new WP_Error( 'unknown_error', 'Unknown error trying to lock the sync queue' ); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
if ( is_wp_error( $result ) ) { |
|
99
|
|
|
$sync_queue->unlock(); |
|
100
|
|
|
|
|
101
|
|
|
return $result; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-wp-replicastore.php'; |
|
105
|
|
|
|
|
106
|
|
|
$store = new Jetpack_Sync_WP_Replicastore(); |
|
107
|
|
|
|
|
108
|
|
|
$result = $store->checksum_all(); |
|
109
|
|
|
|
|
110
|
|
|
$sync_queue->unlock(); |
|
111
|
|
|
|
|
112
|
|
|
return $result; |
|
113
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
// GET /sites/%s/data-histogram |
|
118
|
|
|
class Jetpack_JSON_API_Sync_Histogram_Endpoint extends Jetpack_JSON_API_Sync_Endpoint { |
|
|
|
|
|
|
119
|
|
|
protected function result() { |
|
120
|
|
|
require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php'; |
|
121
|
|
|
|
|
122
|
|
|
$sender = Jetpack_Sync_Sender::get_instance(); |
|
123
|
|
|
$sync_queue = $sender->get_sync_queue(); |
|
124
|
|
|
|
|
125
|
|
|
// lock sending from the queue while we compare checksums with the server |
|
126
|
|
|
$result = $sync_queue->lock( 30 ); // tries to acquire the lock for up to 30 seconds |
|
127
|
|
|
|
|
128
|
|
|
if ( ! $result ) { |
|
129
|
|
|
$sync_queue->unlock(); |
|
130
|
|
|
|
|
131
|
|
|
return new WP_Error( 'unknown_error', 'Unknown error trying to lock the sync queue' ); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
if ( is_wp_error( $result ) ) { |
|
135
|
|
|
$sync_queue->unlock(); |
|
136
|
|
|
|
|
137
|
|
|
return $result; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
$args = $this->query_args(); |
|
141
|
|
|
|
|
142
|
|
|
if ( isset( $args['columns'] ) ) { |
|
143
|
|
|
$columns = array_map( 'trim', explode( ',', $args['columns'] ) ); |
|
144
|
|
|
} else { |
|
145
|
|
|
$columns = null; // go with defaults |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-wp-replicastore.php'; |
|
149
|
|
|
|
|
150
|
|
|
$store = new Jetpack_Sync_WP_Replicastore(); |
|
151
|
|
|
|
|
152
|
|
|
$result = $store->checksum_histogram( $args['object_type'], $args['buckets'], $args['start_id'], $args['end_id'], $columns, $args['strip_non_ascii'] ); |
|
153
|
|
|
|
|
154
|
|
|
$sync_queue->unlock(); |
|
155
|
|
|
|
|
156
|
|
|
return $result; |
|
157
|
|
|
|
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
// POST /sites/%s/sync/settings |
|
162
|
|
|
class Jetpack_JSON_API_Sync_Modify_Settings_Endpoint extends Jetpack_JSON_API_Sync_Endpoint { |
|
|
|
|
|
|
163
|
|
|
protected function result() { |
|
164
|
|
|
$args = $this->input(); |
|
165
|
|
|
|
|
166
|
|
|
require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-settings.php'; |
|
167
|
|
|
|
|
168
|
|
|
$sync_settings = Jetpack_Sync_Settings::get_settings(); |
|
169
|
|
|
|
|
170
|
|
|
foreach ( $args as $key => $value ) { |
|
171
|
|
|
if ( $value !== false ) { |
|
172
|
|
|
if ( is_numeric( $value ) ) { |
|
173
|
|
|
$value = (int) $value; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
// special case for sending empty arrays - a string with value 'empty' |
|
177
|
|
|
if ( $value === 'empty' ) { |
|
178
|
|
|
$value = array(); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
$sync_settings[ $key ] = $value; |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
Jetpack_Sync_Settings::update_settings( $sync_settings ); |
|
186
|
|
|
|
|
187
|
|
|
// re-fetch so we see what's really being stored |
|
188
|
|
|
return Jetpack_Sync_Settings::get_settings(); |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
// GET /sites/%s/sync/settings |
|
193
|
|
|
class Jetpack_JSON_API_Sync_Get_Settings_Endpoint extends Jetpack_JSON_API_Sync_Endpoint { |
|
|
|
|
|
|
194
|
|
|
protected function result() { |
|
195
|
|
|
require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-settings.php'; |
|
196
|
|
|
|
|
197
|
|
|
return Jetpack_Sync_Settings::get_settings(); |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
// GET /sites/%s/sync/object |
|
202
|
|
|
class Jetpack_JSON_API_Sync_Object extends Jetpack_JSON_API_Sync_Endpoint { |
|
|
|
|
|
|
203
|
|
|
protected function result() { |
|
204
|
|
|
$args = $this->query_args(); |
|
205
|
|
|
|
|
206
|
|
|
$module_name = $args['module_name']; |
|
207
|
|
|
|
|
208
|
|
|
require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-modules.php'; |
|
209
|
|
|
|
|
210
|
|
|
if ( ! $sync_module = Jetpack_Sync_Modules::get_module( $module_name ) ) { |
|
211
|
|
|
return new WP_Error( 'invalid_module', 'You specified an invalid sync module' ); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
$object_type = $args['object_type']; |
|
215
|
|
|
$object_ids = $args['object_ids']; |
|
216
|
|
|
|
|
217
|
|
|
require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php'; |
|
218
|
|
|
$codec = Jetpack_Sync_Sender::get_instance()->get_codec(); |
|
219
|
|
|
|
|
220
|
|
|
Jetpack_Sync_Settings::set_is_syncing( true ); |
|
221
|
|
|
$objects = $codec->encode( $sync_module->get_objects_by_id( $object_type, $object_ids ) ); |
|
222
|
|
|
Jetpack_Sync_Settings::set_is_syncing( false ); |
|
223
|
|
|
|
|
224
|
|
|
return array( |
|
225
|
|
|
'objects' => $objects, |
|
226
|
|
|
); |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
class Jetpack_JSON_API_Sync_Now_Endpoint extends Jetpack_JSON_API_Sync_Endpoint { |
|
|
|
|
|
|
231
|
|
|
protected function result() { |
|
232
|
|
|
$args = $this->input(); |
|
233
|
|
|
$queue_name = $this->validate_queue( $args['queue'] ); |
|
234
|
|
|
|
|
235
|
|
|
if ( is_wp_error( $queue_name ) ){ |
|
236
|
|
|
return $queue_name; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php'; |
|
240
|
|
|
|
|
241
|
|
|
$sender = Jetpack_Sync_Sender::get_instance(); |
|
242
|
|
|
$response = $sender->do_sync_for_queue( new Jetpack_Sync_Queue( $args['queue'] ) ); |
|
243
|
|
|
|
|
244
|
|
|
return array( |
|
245
|
|
|
'response' => $response |
|
246
|
|
|
); |
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
class Jetpack_JSON_API_Sync_Checkout_Endpoint extends Jetpack_JSON_API_Sync_Endpoint { |
|
|
|
|
|
|
251
|
|
|
protected function result() { |
|
252
|
|
|
$args = $this->input(); |
|
253
|
|
|
$queue_name = $this->validate_queue( $args['queue'] ); |
|
254
|
|
|
|
|
255
|
|
|
if ( is_wp_error( $queue_name ) ){ |
|
256
|
|
|
return $queue_name; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
if ( $args[ 'number_of_items' ] < 1 || $args[ 'number_of_items' ] > 100 ) { |
|
260
|
|
|
return new WP_Error( 'invalid_number_of_items', 'Number of items needs to be an integer that is larger than 0 and less then 100', 400 ); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-queue.php'; |
|
264
|
|
|
$queue = new Jetpack_Sync_Queue( $queue_name ); |
|
265
|
|
|
|
|
266
|
|
|
if ( 0 === $queue->size() ) { |
|
267
|
|
|
return new WP_Error( 'queue_size', 'The queue is empty and there is nothing to send', 400 ); |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php'; |
|
271
|
|
|
$sender = Jetpack_Sync_Sender::get_instance(); |
|
272
|
|
|
|
|
273
|
|
|
// try to give ourselves as much time as possible |
|
274
|
|
|
set_time_limit( 0 ); |
|
275
|
|
|
|
|
276
|
|
|
// let's delete the checkin state |
|
277
|
|
|
if ( $args['force'] ) { |
|
278
|
|
|
$queue->unlock(); |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
$buffer = $this->get_buffer( $queue, $args[ 'number_of_items' ] ); |
|
282
|
|
|
|
|
283
|
|
|
// Check that the $buffer is not checkout out already |
|
284
|
|
|
if ( is_wp_error( $buffer ) ) { |
|
285
|
|
|
return new WP_Error( 'buffer_open', "We couldn't get the buffer it is currently checked out", 400 ); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
if ( ! is_object( $buffer ) ) { |
|
289
|
|
|
return new WP_Error( 'buffer_non-object', 'Buffer is not an object', 400 ); |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
Jetpack_Sync_Settings::set_is_syncing( true ); |
|
293
|
|
|
list( $items_to_send, $skipped_items_ids, $items ) = $sender->get_items_to_send( $buffer, $args['encode'] ); |
|
|
|
|
|
|
294
|
|
|
Jetpack_Sync_Settings::set_is_syncing( false ); |
|
295
|
|
|
|
|
296
|
|
|
return array( |
|
297
|
|
|
'buffer_id' => $buffer->id, |
|
298
|
|
|
'items' => $items_to_send, |
|
299
|
|
|
'skipped_items' => $skipped_items_ids, |
|
300
|
|
|
'codec' => $args['encode'] ? $sender->get_codec()->name() : null, |
|
301
|
|
|
'sent_timestamp' => time(), |
|
302
|
|
|
); |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
protected function get_buffer( $queue, $number_of_items ) { |
|
306
|
|
|
$start = time(); |
|
307
|
|
|
$max_duration = 5; // this will try to get the buffer |
|
308
|
|
|
|
|
309
|
|
|
$buffer = $queue->checkout( $number_of_items ); |
|
310
|
|
|
$duration = time() - $start; |
|
311
|
|
|
|
|
312
|
|
|
while( is_wp_error( $buffer ) && $duration < $max_duration ) { |
|
313
|
|
|
sleep( 2 ); |
|
314
|
|
|
$duration = time() - $start; |
|
315
|
|
|
$buffer = $queue->checkout( $number_of_items ); |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
if ( $buffer === false ) { |
|
319
|
|
|
return new WP_Error( 'queue_size', 'The queue is empty and there is nothing to send', 400 ); |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
return $buffer; |
|
323
|
|
|
} |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
class Jetpack_JSON_API_Sync_Close_Endpoint extends Jetpack_JSON_API_Sync_Endpoint { |
|
|
|
|
|
|
327
|
|
|
protected function result() { |
|
328
|
|
|
$request_body = $this->input(); |
|
329
|
|
|
$queue_name = $this->validate_queue( $request_body['queue'] ); |
|
330
|
|
|
|
|
331
|
|
|
if ( is_wp_error( $queue_name ) ) { |
|
332
|
|
|
return $queue_name; |
|
333
|
|
|
} |
|
334
|
|
|
require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-queue.php'; |
|
335
|
|
|
|
|
336
|
|
|
if ( ! isset( $request_body['buffer_id'] ) ) { |
|
337
|
|
|
return new WP_Error( 'missing_buffer_id', 'Please provide a buffer id', 400 ); |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
if ( ! isset( $request_body['item_ids'] ) || ! is_array( $request_body['item_ids'] ) ) { |
|
341
|
|
|
return new WP_Error( 'missing_item_ids', 'Please provide a list of item ids in the item_ids argument', 400 ); |
|
342
|
|
|
} |
|
343
|
|
|
|
|
344
|
|
|
//Limit to A-Z,a-z,0-9,_,- |
|
345
|
|
|
$request_body ['buffer_id'] = preg_replace( '/[^A-Za-z0-9]/', '', $request_body['buffer_id'] ); |
|
346
|
|
|
$request_body['item_ids'] = array_filter( array_map( array( 'Jetpack_JSON_API_Sync_Close_Endpoint', 'sanitize_item_ids' ), $request_body['item_ids'] ) ); |
|
347
|
|
|
|
|
348
|
|
|
$buffer = new Jetpack_Sync_Queue_Buffer( $request_body['buffer_id'], $request_body['item_ids'] ); |
|
349
|
|
|
$queue = new Jetpack_Sync_Queue( $queue_name ); |
|
350
|
|
|
|
|
351
|
|
|
$response = $queue->close( $buffer, $request_body['item_ids'] ); |
|
352
|
|
|
|
|
353
|
|
|
if ( is_wp_error( $response ) ) { |
|
354
|
|
|
return $response; |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
return array( |
|
358
|
|
|
'success' => $response |
|
359
|
|
|
); |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
protected static function sanitize_item_ids( $item ) { |
|
363
|
|
|
// lets not delete any options that don't start with jpsq_sync- |
|
364
|
|
|
if ( substr( $item, 0, 5 ) !== 'jpsq_' ) { |
|
365
|
|
|
return null; |
|
366
|
|
|
} |
|
367
|
|
|
//Limit to A-Z,a-z,0-9,_,-,. |
|
368
|
|
|
return preg_replace( '/[^A-Za-z0-9-_.]/', '', $item ); |
|
369
|
|
|
} |
|
370
|
|
|
} |
|
371
|
|
|
|
|
372
|
|
|
class Jetpack_JSON_API_Sync_Unlock_Endpoint extends Jetpack_JSON_API_Sync_Endpoint { |
|
|
|
|
|
|
373
|
|
|
protected function result() { |
|
374
|
|
|
$args = $this->input(); |
|
375
|
|
|
|
|
376
|
|
|
if ( ! isset( $args['queue'] ) ) { |
|
377
|
|
|
return new WP_Error( 'invalid_queue', 'Queue name is required', 400 ); |
|
378
|
|
|
} |
|
379
|
|
|
|
|
380
|
|
View Code Duplication |
if ( ! in_array( $args['queue'], array( 'sync', 'full_sync' ) ) ) { |
|
381
|
|
|
return new WP_Error( 'invalid_queue', 'Queue name should be sync or full_sync', 400 ); |
|
382
|
|
|
} |
|
383
|
|
|
|
|
384
|
|
|
require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-queue.php'; |
|
385
|
|
|
$queue = new Jetpack_Sync_Queue( $args['queue'] ); |
|
386
|
|
|
|
|
387
|
|
|
// False means that there was no lock to delete. |
|
388
|
|
|
$response = $queue->unlock(); |
|
389
|
|
|
return array( |
|
390
|
|
|
'success' => $response |
|
391
|
|
|
); |
|
392
|
|
|
} |
|
393
|
|
|
} |
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.