Completed
Push — add/changelog-432 ( 125982...8161c6 )
by
unknown
70:53 queued 60:29
created

Jetpack_JSON_API_Sync_Checkout_Endpoint::result()   C

Complexity

Conditions 9
Paths 11

Size

Total Lines 53
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 30
nc 11
nop 0
dl 0
loc 53
rs 6.8963
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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