Completed
Push — update/sync-status-sync-disabl... ( 12abe9 )
by
unknown
16:51 queued 07:03
created

Jetpack_JSON_API_Sync_Status_Endpoint   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 10
c 0
b 0
f 0
wmc 2
lcom 0
cbo 5

1 Method

Rating   Name   Duplication   Size   Complexity  
B result() 0 27 2
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-settings.php';
56
		if ( ! empty( Jetpack_Sync_Settings::get_setting( 'disable' ) ) ) {
57
			return new WP_Error( 'sync_disabled', 'Sync has been disabled', 403 );
58
		}
59
60
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-modules.php';
61
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php';
62
63
		$sync_module = Jetpack_Sync_Modules::get_module( 'full-sync' );
64
		$sender      = Jetpack_Sync_Sender::get_instance();
65
		$queue       = $sender->get_sync_queue();
66
		$full_queue  = $sender->get_full_sync_queue();
67
68
		return array_merge(
69
			$sync_module->get_status(),
70
			array(
71
				'is_scheduled'          => Jetpack_Sync_Actions::is_scheduled_full_sync(),
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 );
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
		return array(
223
			'objects' => $codec->encode( $sync_module->get_objects_by_id( $object_type, $object_ids ) )
224
		);
225
	}
226
}
227
228
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...
229
	protected function result() {
230
		$args = $this->input();
231
		$queue_name = $this->validate_queue( $args['queue'] );
232
233
		if ( is_wp_error( $queue_name ) ){
234
			return $queue_name;
235
		}
236
237
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php';
238
239
		$sender = Jetpack_Sync_Sender::get_instance();
240
		$response = $sender->do_sync_for_queue( new Jetpack_Sync_Queue( $args['queue'] ) );
241
242
		return array(
243
			'response' => $response
244
		);
245
	}
246
}
247
248
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...
249
	protected function result() {
250
		$args = $this->input();
251
		$queue_name = $this->validate_queue( $args['queue'] );
252
253
		if ( is_wp_error( $queue_name ) ){
254
			return $queue_name;
255
		}
256
257
		if ( $args[ 'number_of_items' ] < 1 || $args[ 'number_of_items' ] > 100  ) {
258
			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 );
259
		}
260
261
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-queue.php';
262
		$queue = new Jetpack_Sync_Queue( $queue_name );
263
264
		if ( 0 === $queue->size() ) {
265
			return new WP_Error( 'queue_size', 'The queue is empty and there is nothing to send', 400 );
266
		}
267
268
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php';
269
		$sender = Jetpack_Sync_Sender::get_instance();
270
		
271
		// let's delete the checkin state
272
		if ( $args['force'] ) {
273
			$queue->unlock();
274
		}
275
276
		$buffer = $this->get_buffer( $queue, $args[ 'number_of_items' ] );
277
		
278
		// Check that the $buffer is not checkout out already
279
		if ( is_wp_error( $buffer ) ) {
280
			return new WP_Error( 'buffer_open', "We couldn't get the buffer it is currently checked out", 400 );
281
		}
282
		
283
		if ( ! is_object( $buffer ) ) {
284
			return new WP_Error( 'buffer_non-object', 'Buffer is not an object', 400 );
285
		}
286
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
289
		return array(
290
			'buffer_id'      => $buffer->id,
291
			'items'          => $items_to_send,
292
			'skipped_items'  => $skipped_items_ids,
293
			'codec'          => $args['encode'] ? $sender->get_codec()->name() : null,
294
			'sent_timestamp' => time(),
295
		);
296
	}
297
298
	protected function get_buffer( $queue, $number_of_items ) {
299
		$start = time();
300
		$max_duration = 5; // this will try to get the buffer
301
302
		$buffer = $queue->checkout( $number_of_items );
303
		$duration = time() - $start;
304
305
		while( is_wp_error( $buffer ) && $duration < $max_duration ) {
306
			sleep( 2 );
307
			$duration = time() - $start;
308
			$buffer = $queue->checkout( $number_of_items );
309
		}
310
311
		if ( $buffer === false ) {
312
			return new WP_Error( 'queue_size', 'The queue is empty and there is nothing to send', 400 );
313
		}
314
315
		return $buffer;
316
	}
317
}
318
319
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...
320
	protected function result() {
321
		$request_body = $this->input();
322
		$queue_name = $this->validate_queue( $request_body['queue'] );
323
324
		if ( is_wp_error( $queue_name ) ) {
325
			return $queue_name;
326
		}
327
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-queue.php';
328
329
		if ( ! isset( $request_body['buffer_id'] ) ) {
330
			return new WP_Error( 'missing_buffer_id', 'Please provide a buffer id', 400 );
331
		}
332
333
		if ( ! isset( $request_body['item_ids'] ) || ! is_array( $request_body['item_ids'] ) ) {
334
			return new WP_Error( 'missing_item_ids', 'Please provide a list of item ids in the item_ids argument', 400 );
335
		}
336
337
		//Limit to A-Z,a-z,0-9,_,-
338
		$request_body ['buffer_id'] = preg_replace( '/[^A-Za-z0-9]/', '', $request_body['buffer_id'] );
339
		$request_body['item_ids'] = array_filter( array_map( array( 'Jetpack_JSON_API_Sync_Close_Endpoint', 'sanitize_item_ids' ), $request_body['item_ids'] ) );
340
341
		$buffer = new Jetpack_Sync_Queue_Buffer( $request_body['buffer_id'], $request_body['item_ids'] );
342
		$queue = new Jetpack_Sync_Queue( $queue_name );
343
344
		$response = $queue->close( $buffer, $request_body['item_ids'] );
345
346
		if ( is_wp_error( $response ) ) {
347
			return $response;
348
		}
349
350
		return array(
351
			'success' => $response
352
		);
353
	}
354
355
	protected static function sanitize_item_ids( $item ) {
356
		// lets not delete any options that don't start with jpsq_sync-
357
		if ( substr( $item, 0, 5 ) !== 'jpsq_' ) {
358
			return null;
359
		}
360
		//Limit to A-Z,a-z,0-9,_,-,.
361
		return preg_replace( '/[^A-Za-z0-9-_.]/', '', $item );
362
	}
363
}
364
365
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...
366
	protected function result() {
367
		$args = $this->input();
368
369
		if ( ! isset( $args['queue'] ) ) {
370
			return new WP_Error( 'invalid_queue', 'Queue name is required', 400 );
371
		}
372
373 View Code Duplication
		if ( ! in_array( $args['queue'], array( 'sync', 'full_sync' ) ) ) {
374
			return new WP_Error( 'invalid_queue', 'Queue name should be sync or full_sync', 400 );
375
		}
376
377
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-queue.php';
378
		$queue = new Jetpack_Sync_Queue( $args['queue'] );
379
380
		// False means that there was no lock to delete.
381
		$response = $queue->unlock();
382
		return array(
383
			'success' => $response
384
		);
385
	}
386
}