Completed
Push — add/oldest-item-for-cron-to-sy... ( 70531e )
by
unknown
27:42 queued 16:07
created

Jetpack_JSON_API_Sync_Status_Endpoint   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 10
c 0
b 0
f 0
wmc 1
lcom 0
cbo 4

1 Method

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