Completed
Push — fix/save-full-sync-status-duri... ( 6117f3...bc1926 )
by
unknown
09:30
created

Jetpack_JSON_API_Sync_Status_Endpoint::result()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 16
nc 1
nop 0
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
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
		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 {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

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.

Loading history...
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
63
		return array_merge(
64
			$sync_module->get_status(),
65
			array(
66
				'queue_size'            => $queue->size(),
67
				'queue_lag'             => $queue->lag(),
68
				'queue_next_sync'       => ( $sender->get_next_sync_time( 'sync' ) - microtime( true ) ),
69
				'full_queue_size'       => $full_queue->size(),
70
				'full_queue_lag'        => $full_queue->lag(),
71
				'full_queue_next_sync'  => ( $sender->get_next_sync_time( 'full_sync' ) - microtime( true ) ),
72
			)
73
		);
74
	}
75
}
76
77
// GET /sites/%s/data-check
78
class Jetpack_JSON_API_Sync_Check_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

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.

Loading history...
79
	protected function result() {
80
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php';
81
82
		$sender     = Jetpack_Sync_Sender::get_instance();
83
		$sync_queue = $sender->get_sync_queue();
84
85
		// lock sending from the queue while we compare checksums with the server
86
		$result = $sync_queue->lock( 30 ); // tries to acquire the lock for up to 30 seconds
87
88
		if ( ! $result ) {
89
			$sync_queue->unlock();
90
91
			return new WP_Error( 'unknown_error', 'Unknown error trying to lock the sync queue' );
92
		}
93
94
		if ( is_wp_error( $result ) ) {
95
			$sync_queue->unlock();
96
97
			return $result;
98
		}
99
100
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-wp-replicastore.php';
101
102
		$store = new Jetpack_Sync_WP_Replicastore();
103
104
		$result = $store->checksum_all();
105
106
		$sync_queue->unlock();
107
108
		return $result;
109
110
	}
111
}
112
113
// GET /sites/%s/data-histogram
114
class Jetpack_JSON_API_Sync_Histogram_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

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.

Loading history...
115
	protected function result() {
116
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php';
117
118
		$sender     = Jetpack_Sync_Sender::get_instance();
119
		$sync_queue = $sender->get_sync_queue();
120
121
		// lock sending from the queue while we compare checksums with the server
122
		$result = $sync_queue->lock( 30 ); // tries to acquire the lock for up to 30 seconds
123
124
		if ( ! $result ) {
125
			$sync_queue->unlock();
126
127
			return new WP_Error( 'unknown_error', 'Unknown error trying to lock the sync queue' );
128
		}
129
130
		if ( is_wp_error( $result ) ) {
131
			$sync_queue->unlock();
132
133
			return $result;
134
		}
135
136
		$args = $this->query_args();
137
138
		if ( isset( $args['columns'] ) ) {
139
			$columns = array_map( 'trim', explode( ',', $args['columns'] ) );
140
		} else {
141
			$columns = null; // go with defaults
142
		}
143
144
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-wp-replicastore.php';
145
146
		$store = new Jetpack_Sync_WP_Replicastore();
147
148
		$result = $store->checksum_histogram( $args['object_type'], $args['buckets'], $args['start_id'], $args['end_id'], $columns, $args['strip_non_ascii'] );
149
150
		$sync_queue->unlock();
151
152
		return $result;
153
154
	}
155
}
156
157
// POST /sites/%s/sync/settings
158
class Jetpack_JSON_API_Sync_Modify_Settings_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

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.

Loading history...
159
	protected function result() {
160
		$args = $this->input();
161
162
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-settings.php';
163
164
		$sync_settings = Jetpack_Sync_Settings::get_settings();
165
166
		foreach ( $args as $key => $value ) {
167
			if ( $value !== false ) {
168
				if ( is_numeric( $value ) ) {
169
					$value = (int) $value;
170
				}
171
				
172
				// special case for sending empty arrays - a string with value 'empty'
173
				if ( $value === 'empty' ) {
174
					$value = array();
175
				}
176
177
				$sync_settings[ $key ] = $value;
178
			}
179
		}
180
181
		Jetpack_Sync_Settings::update_settings( $sync_settings );
182
183
		// re-fetch so we see what's really being stored
184
		return Jetpack_Sync_Settings::get_settings();
185
	}
186
}
187
188
// GET /sites/%s/sync/settings
189
class Jetpack_JSON_API_Sync_Get_Settings_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

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.

Loading history...
190
	protected function result() {
191
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-settings.php';
192
193
		return Jetpack_Sync_Settings::get_settings();
194
	}
195
}
196
197
// GET /sites/%s/sync/object
198
class Jetpack_JSON_API_Sync_Object extends Jetpack_JSON_API_Sync_Endpoint {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

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.

Loading history...
199
	protected function result() {
200
		$args = $this->query_args();
201
202
		$module_name = $args['module_name'];
203
204
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-modules.php';
205
206
		if ( ! $sync_module = Jetpack_Sync_Modules::get_module( $module_name ) ) {
207
			return new WP_Error( 'invalid_module', 'You specified an invalid sync module' );
208
		}
209
210
		$object_type = $args['object_type'];
211
		$object_ids  = $args['object_ids'];
212
213
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php';
214
		$codec = Jetpack_Sync_Sender::get_instance()->get_codec();
215
216
		Jetpack_Sync_Settings::set_is_syncing( true );
217
		$objects = $codec->encode( $sync_module->get_objects_by_id( $object_type, $object_ids ) );
218
		Jetpack_Sync_Settings::set_is_syncing( false );
219
220
		return array(
221
			'objects' => $objects,
222
		);
223
	}
224
}
225
226
class Jetpack_JSON_API_Sync_Now_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

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.

Loading history...
227
	protected function result() {
228
		$args = $this->input();
229
		$queue_name = $this->validate_queue( $args['queue'] );
230
231
		if ( is_wp_error( $queue_name ) ){
232
			return $queue_name;
233
		}
234
235
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php';
236
237
		$sender = Jetpack_Sync_Sender::get_instance();
238
		$response = $sender->do_sync_for_queue( new Jetpack_Sync_Queue( $args['queue'] ) );
239
240
		return array(
241
			'response' => $response
242
		);
243
	}
244
}
245
246
class Jetpack_JSON_API_Sync_Checkout_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

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.

Loading history...
247
	protected function result() {
248
		$args = $this->input();
249
		$queue_name = $this->validate_queue( $args['queue'] );
250
251
		if ( is_wp_error( $queue_name ) ){
252
			return $queue_name;
253
		}
254
255
		if ( $args[ 'number_of_items' ] < 1 || $args[ 'number_of_items' ] > 100  ) {
256
			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 );
257
		}
258
259
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-queue.php';
260
		$queue = new Jetpack_Sync_Queue( $queue_name );
261
262
		if ( 0 === $queue->size() ) {
263
			return new WP_Error( 'queue_size', 'The queue is empty and there is nothing to send', 400 );
264
		}
265
266
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php';
267
		$sender = Jetpack_Sync_Sender::get_instance();
268
269
		// try to give ourselves as much time as possible
270
		set_time_limit( 0 );
271
272
		// let's delete the checkin state
273
		if ( $args['force'] ) {
274
			$queue->unlock();
275
		}
276
277
		$buffer = $this->get_buffer( $queue, $args[ 'number_of_items' ] );
278
		
279
		// Check that the $buffer is not checkout out already
280
		if ( is_wp_error( $buffer ) ) {
281
			return new WP_Error( 'buffer_open', "We couldn't get the buffer it is currently checked out", 400 );
282
		}
283
		
284
		if ( ! is_object( $buffer ) ) {
285
			return new WP_Error( 'buffer_non-object', 'Buffer is not an object', 400 );
286
		}
287
288
		Jetpack_Sync_Settings::set_is_syncing( true );
289
		list( $items_to_send, $skipped_items_ids, $items ) = $sender->get_items_to_send( $buffer, $args['encode'] );
0 ignored issues
show
Unused Code introduced by
The assignment to $items is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
290
		Jetpack_Sync_Settings::set_is_syncing( false );
291
292
		return array(
293
			'buffer_id'      => $buffer->id,
294
			'items'          => $items_to_send,
295
			'skipped_items'  => $skipped_items_ids,
296
			'codec'          => $args['encode'] ? $sender->get_codec()->name() : null,
297
			'sent_timestamp' => time(),
298
		);
299
	}
300
301
	protected function get_buffer( $queue, $number_of_items ) {
302
		$start = time();
303
		$max_duration = 5; // this will try to get the buffer
304
305
		$buffer = $queue->checkout( $number_of_items );
306
		$duration = time() - $start;
307
308
		while( is_wp_error( $buffer ) && $duration < $max_duration ) {
309
			sleep( 2 );
310
			$duration = time() - $start;
311
			$buffer = $queue->checkout( $number_of_items );
312
		}
313
314
		if ( $buffer === false ) {
315
			return new WP_Error( 'queue_size', 'The queue is empty and there is nothing to send', 400 );
316
		}
317
318
		return $buffer;
319
	}
320
}
321
322
class Jetpack_JSON_API_Sync_Close_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

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.

Loading history...
323
	protected function result() {
324
		$request_body = $this->input();
325
		$queue_name = $this->validate_queue( $request_body['queue'] );
326
327
		if ( is_wp_error( $queue_name ) ) {
328
			return $queue_name;
329
		}
330
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-queue.php';
331
332
		if ( ! isset( $request_body['buffer_id'] ) ) {
333
			return new WP_Error( 'missing_buffer_id', 'Please provide a buffer id', 400 );
334
		}
335
336
		if ( ! isset( $request_body['item_ids'] ) || ! is_array( $request_body['item_ids'] ) ) {
337
			return new WP_Error( 'missing_item_ids', 'Please provide a list of item ids in the item_ids argument', 400 );
338
		}
339
340
		//Limit to A-Z,a-z,0-9,_,-
341
		$request_body ['buffer_id'] = preg_replace( '/[^A-Za-z0-9]/', '', $request_body['buffer_id'] );
342
		$request_body['item_ids'] = array_filter( array_map( array( 'Jetpack_JSON_API_Sync_Close_Endpoint', 'sanitize_item_ids' ), $request_body['item_ids'] ) );
343
344
		$buffer = new Jetpack_Sync_Queue_Buffer( $request_body['buffer_id'], $request_body['item_ids'] );
345
		$queue = new Jetpack_Sync_Queue( $queue_name );
346
347
		$response = $queue->close( $buffer, $request_body['item_ids'] );
348
349
		if ( is_wp_error( $response ) ) {
350
			return $response;
351
		}
352
353
		return array(
354
			'success' => $response
355
		);
356
	}
357
358
	protected static function sanitize_item_ids( $item ) {
359
		// lets not delete any options that don't start with jpsq_sync-
360
		if ( substr( $item, 0, 5 ) !== 'jpsq_' ) {
361
			return null;
362
		}
363
		//Limit to A-Z,a-z,0-9,_,-,.
364
		return preg_replace( '/[^A-Za-z0-9-_.]/', '', $item );
365
	}
366
}
367
368
class Jetpack_JSON_API_Sync_Unlock_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

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.

Loading history...
369
	protected function result() {
370
		$args = $this->input();
371
372
		if ( ! isset( $args['queue'] ) ) {
373
			return new WP_Error( 'invalid_queue', 'Queue name is required', 400 );
374
		}
375
376 View Code Duplication
		if ( ! in_array( $args['queue'], array( 'sync', 'full_sync' ) ) ) {
377
			return new WP_Error( 'invalid_queue', 'Queue name should be sync or full_sync', 400 );
378
		}
379
380
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-queue.php';
381
		$queue = new Jetpack_Sync_Queue( $args['queue'] );
382
383
		// False means that there was no lock to delete.
384
		$response = $queue->unlock();
385
		return array(
386
			'success' => $response
387
		);
388
	}
389
}