Completed
Push — update/send-urls-via-sync ( 27ae40...07e859 )
by
unknown
68:56 queued 59:25
created

Jetpack_JSON_API_Sync_Unlock_Endpoint   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 22
Duplicated Lines 13.64 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 3
loc 22
rs 10
c 0
b 0
f 0
wmc 3
lcom 0
cbo 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A result() 3 20 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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