Completed
Push — add/clear-transient-endpoint ( aeaaf7 )
by
unknown
91:23 queued 80:48
created

Jetpack_JSON_API_Sync_Clear_Transient_Endpoint   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A result() 0 11 2
1
<?php
2
3
use Automattic\Jetpack\Sync\Actions;
4
use Automattic\Jetpack\Sync\Health;
5
use Automattic\Jetpack\Sync\Modules;
6
use Automattic\Jetpack\Sync\Queue;
7
use Automattic\Jetpack\Sync\Queue_Buffer;
8
use Automattic\Jetpack\Sync\Replicastore;
9
use Automattic\Jetpack\Sync\Sender;
10
use Automattic\Jetpack\Sync\Settings;
11
12
// POST /sites/%s/sync
13
class Jetpack_JSON_API_Sync_Endpoint extends Jetpack_JSON_API_Endpoint {
14
15
	/**
16
	 * This endpoint allows authentication both via a blog and a user token.
17
	 * If a user token is used, that user should have `manage_options` capability.
18
	 *
19
	 * @var array|string
20
	 */
21
	protected $needed_capabilities = 'manage_options';
22
23
	protected function validate_call( $_blog_id, $capability, $check_manage_active = true ) {
24
		return parent::validate_call( $_blog_id, $capability, false );
25
	}
26
27
	protected function result() {
28
		$args = $this->input();
29
		$modules = null;
30
31
		// convert list of modules in comma-delimited format into an array
32
		// of "$modulename => true"
33 View Code Duplication
		if ( isset( $args['modules'] ) && ! empty( $args['modules'] ) ) {
34
			$modules = array_map( '__return_true', array_flip( array_map( 'trim', explode( ',', $args['modules'] ) ) ) );
35
		}
36
37 View Code Duplication
		foreach ( array( 'posts', 'comments', 'users' ) as $module_name ) {
38
			if ( 'users' === $module_name && isset( $args[ $module_name ] ) && 'initial' === $args[ $module_name ] ) {
39
				$modules[ 'users' ] = 'initial';
40
			} elseif ( isset( $args[ $module_name ] ) ) {
41
				$ids = explode( ',', $args[ $module_name ] );
42
				if ( count( $ids ) > 0 ) {
43
					$modules[ $module_name ] = $ids;
44
				}
45
			}
46
		}
47
48
		if ( empty( $modules ) ) {
49
			$modules = null;
50
		}
51
		return array( 'scheduled' => Actions::do_full_sync( $modules ) );
52
	}
53
54
	protected function validate_queue( $query ) {
55
		if ( ! isset( $query ) ) {
56
			return new WP_Error( 'invalid_queue', 'Queue name is required', 400 );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'invalid_queue'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
57
		}
58
59 View Code Duplication
		if ( ! in_array( $query, array( 'sync', 'full_sync', 'immediate' ) ) ) {
60
			return new WP_Error( 'invalid_queue', 'Queue name should be sync, full_sync or immediate', 400 );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'invalid_queue'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
61
		}
62
		return $query;
63
	}
64
}
65
66
// GET /sites/%s/sync/status
67
class Jetpack_JSON_API_Sync_Status_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
68
	protected function result() {
69
		$args   = $this->query_args();
70
		$fields = isset( $args['fields'] ) ? $args['fields'] : array();
71
		return Actions::get_sync_status( $fields );
72
	}
73
}
74
75
// GET /sites/%s/data-check
76
class Jetpack_JSON_API_Sync_Check_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
77
	protected function result() {
78
		Actions::mark_sync_read_only();
79
		$store = new Replicastore();
80
		return $store->checksum_all();
81
	}
82
}
83
84
// GET /sites/%s/data-histogram
85
class Jetpack_JSON_API_Sync_Histogram_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
86
	protected function result() {
87
		$args = $this->query_args();
88
89
		if ( isset( $args['columns'] ) ) {
90
			$columns = array_map( 'trim', explode( ',', $args['columns'] ) );
91
		} else {
92
			$columns = null; // go with defaults
93
		}
94
95
		$store = new Replicastore();
96
97
		if ( ! isset( $args['strip_non_ascii'] ) ) {
98
			$args['strip_non_ascii'] = true;
99
		}
100
101
		/**
102
		 * Hack: nullify the values of `start_id` and `end_id` if we're only requesting ranges.
103
		 *
104
		 * The endpoint doesn't support nullable values :(
105
		 */
106
		if ( true === $args['only_range_edges'] ) {
107
			if ( 0 === $args['start_id'] ) {
108
				$args['start_id'] = null;
109
			}
110
111
			if ( 0 === $args['end_id'] ) {
112
				$args['end_id'] = null;
113
			}
114
		}
115
116
		$histogram = $store->checksum_histogram( $args['object_type'], $args['buckets'], $args['start_id'], $args['end_id'], $columns, $args['strip_non_ascii'], $args['shared_salt'], $args['only_range_edges'], $args['detailed_drilldown'] );
0 ignored issues
show
Bug introduced by
It seems like $columns defined by array_map('trim', explode(',', $args['columns'])) on line 90 can also be of type array; however, Automattic\Jetpack\Sync\...e::checksum_histogram() does only seem to accept null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
117
118
		// Hack to disable Sync during this call, so we can resolve faster.
119
		Actions::mark_sync_read_only();
120
121
		return array( 'histogram' => $histogram, 'type' => $store->get_checksum_type() );
122
	}
123
}
124
125
// phpcs:disable Generic.Files.OneObjectStructurePerFile.MultipleFound
126
/**
127
 * POST /sites/%s/sync/health
128
 */
129
class Jetpack_JSON_API_Sync_Modify_Health_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
130
131
	/**
132
	 * Callback for sync/health endpoint.
133
	 *
134
	 * @return array|WP_Error result of request.
135
	 */
136
	protected function result() {
137
		$args = $this->input();
138
139
		switch ( $args['status'] ) {
140
			case Health::STATUS_IN_SYNC:
141
			case Health::STATUS_OUT_OF_SYNC:
142
				Health::update_status( $args['status'] );
143
				break;
144
			default:
145
				return new WP_Error( 'invalid_status', 'Invalid Sync Status Provided.' );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'invalid_status'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
146
		}
147
148
		// re-fetch so we see what's really being stored.
149
		return array(
150
			'success' => Health::get_status(),
151
		);
152
	}
153
}
154
// phpcs:enable
155
156
// phpcs:disable Generic.Files.OneObjectStructurePerFile.MultipleFound
157
/**
158
 * POST /sites/%s/clear/transient
159
 */
160
class Jetpack_JSON_API_Sync_Clear_Transient_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
161
	/**
162
	 * Callback for clear/transient endpoint.
163
	 *
164
	 * @return array|WP_Error result of request.
165
	 */
166
	protected function result() {
167
		$args = $this->input();
168
169
		if ( $args['name'] ) {
170
			return array(
171
				'success' => delete_transient( $args['name'] ),
172
			);
173
		} else {
174
			return new WP_Error( 'missing_transient', 'No transient provided.' );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'missing_transient'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
175
		}
176
	}
177
}
178
// phpcs:enable
179
180
// POST /sites/%s/sync/settings
181
class Jetpack_JSON_API_Sync_Modify_Settings_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
182
	protected function result() {
183
		$args = $this->input();
184
185
		$sync_settings = Settings::get_settings();
186
187
		foreach ( $args as $key => $value ) {
188
			if ( $value !== false ) {
189
				if ( is_numeric( $value ) ) {
190
					$value = (int) $value;
191
				}
192
193
				// special case for sending empty arrays - a string with value 'empty'
194
				if ( $value === 'empty' ) {
195
					$value = array();
196
				}
197
198
				$sync_settings[ $key ] = $value;
199
			}
200
		}
201
202
		Settings::update_settings( $sync_settings );
203
204
		// re-fetch so we see what's really being stored
205
		return Settings::get_settings();
206
	}
207
}
208
209
// GET /sites/%s/sync/settings
210
class Jetpack_JSON_API_Sync_Get_Settings_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
211
	protected function result() {
212
213
		return Settings::get_settings();
214
	}
215
}
216
217
// GET /sites/%s/sync/object
218
class Jetpack_JSON_API_Sync_Object extends Jetpack_JSON_API_Sync_Endpoint {
219
	protected function result() {
220
		$args = $this->query_args();
221
222
		$module_name = $args['module_name'];
223
224
		if ( ! $sync_module = Modules::get_module( $module_name ) ) {
225
			return new WP_Error( 'invalid_module', 'You specified an invalid sync module' );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'invalid_module'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
226
		}
227
228
		$object_type = $args['object_type'];
229
		$object_ids  = $args['object_ids'];
230
231
		$codec = Sender::get_instance()->get_codec();
232
233
		Actions::mark_sync_read_only();
234
		Settings::set_is_syncing( true );
235
		$objects = $codec->encode( $sync_module->get_objects_by_id( $object_type, $object_ids ) );
236
		Settings::set_is_syncing( false );
237
238
		return array(
239
			'objects' => $objects,
240
			'codec' => $codec->name(),
241
		);
242
	}
243
}
244
245
class Jetpack_JSON_API_Sync_Now_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
246
	protected function result() {
247
		$args = $this->input();
248
		$queue_name = $this->validate_queue( $args['queue'] );
249
250
		if ( is_wp_error( $queue_name ) ){
251
			return $queue_name;
252
		}
253
254
		$sender = Sender::get_instance();
255
		$response = $sender->do_sync_for_queue( new Queue( $args['queue'] ) );
0 ignored issues
show
Documentation introduced by
new \Automattic\Jetpack\...c\Queue($args['queue']) is of type object<Automattic\Jetpack\Sync\Queue>, but the function expects a object<Automattic\Jetpac...tic\Jetpack\Sync\Queue>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
256
257
		return array(
258
			'response' => $response
259
		);
260
	}
261
}
262
263
class Jetpack_JSON_API_Sync_Checkout_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
264
	protected function result() {
265
		$args       = $this->input();
266
		$queue_name = $this->validate_queue( $args['queue'] );
267
268
		if ( is_wp_error( $queue_name ) ) {
269
			return $queue_name;
270
		}
271
272
		if ( $args['number_of_items'] < 1 || $args['number_of_items'] > 100 ) {
273
			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 );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'invalid_number_of_items'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
274
		}
275
276
		$number_of_items = absint( $args['number_of_items'] );
277
278
		if ( 'immediate' === $queue_name ) {
279
			return $this->immediate_full_sync_pull( $number_of_items );
280
		}
281
282
		return $this->queue_pull( $queue_name, $number_of_items, $args );
283
	}
284
285
	function queue_pull( $queue_name, $number_of_items, $args ){
286
		$queue = new Queue( $queue_name );
287
288
		if ( 0 === $queue->size() ) {
289
			return new WP_Error( 'queue_size', 'The queue is empty and there is nothing to send', 400 );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'queue_size'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
290
		}
291
292
		$sender = Sender::get_instance();
293
294
		// try to give ourselves as much time as possible.
295
		set_time_limit( 0 );
296
297
		if ( $args['pop'] ) {
298
			$buffer = new Queue_Buffer( 'pop', $queue->pop( $number_of_items ) );
0 ignored issues
show
Bug introduced by
It seems like $queue->pop($number_of_items) targeting Automattic\Jetpack\Sync\Queue::pop() can also be of type null or object; however, Automattic\Jetpack\Sync\...e_Buffer::__construct() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
299
		} else {
300
			// let's delete the checkin state.
301
			if ( $args['force'] ) {
302
				$queue->unlock();
303
			}
304
			$buffer = $this->get_buffer( $queue, $number_of_items );
305
		}
306
		// Check that the $buffer is not checkout out already.
307
		if ( is_wp_error( $buffer ) ) {
308
			return new WP_Error( 'buffer_open', "We couldn't get the buffer it is currently checked out", 400 );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'buffer_open'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
309
		}
310
311
		if ( ! is_object( $buffer ) ) {
312
			return new WP_Error( 'buffer_non-object', 'Buffer is not an object', 400 );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'buffer_non-object'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
313
		}
314
315
		Settings::set_is_syncing( true );
316
		list( $items_to_send, $skipped_items_ids ) = $sender->get_items_to_send( $buffer, $args['encode'] );
317
		Settings::set_is_syncing( false );
318
319
		return array(
320
			'buffer_id'      => $buffer->id,
321
			'items'          => $items_to_send,
322
			'skipped_items'  => $skipped_items_ids,
323
			'codec'          => $args['encode'] ? $sender->get_codec()->name() : null,
324
			'sent_timestamp' => time(),
325
		);
326
	}
327
328
	public $items = [];
329
330
	public function jetpack_sync_send_data_listener() {
331
		foreach ( func_get_args()[0] as $key => $item ) {
332
			$this->items[ $key ] = $item;
333
		}
334
	}
335
336
	/**
337
	 * Check out a buffer of full sync actions.
338
	 *
339
	 * @param null $number_of_items Number of Actions to check-out.
340
	 *
341
	 * @return array Sync Actions to be returned to requestor
342
	 */
343
	public function immediate_full_sync_pull( $number_of_items = null ) {
344
		// try to give ourselves as much time as possible.
345
		set_time_limit( 0 );
346
347
		$original_send_data_cb = array( 'Automattic\Jetpack\Sync\Actions', 'send_data' );
348
		$temp_send_data_cb     = array( $this, 'jetpack_sync_send_data_listener' );
349
350
		Sender::get_instance()->set_enqueue_wait_time( 0 );
351
		remove_filter( 'jetpack_sync_send_data', $original_send_data_cb );
352
		add_filter( 'jetpack_sync_send_data', $temp_send_data_cb, 10, 6 );
353
		Sender::get_instance()->do_full_sync();
354
		remove_filter( 'jetpack_sync_send_data', $temp_send_data_cb );
355
		add_filter( 'jetpack_sync_send_data', $original_send_data_cb, 10, 6 );
356
357
		return array(
358
			'items'          => $this->items,
359
			'codec'          => Sender::get_instance()->get_codec()->name(),
360
			'sent_timestamp' => time(),
361
			'status'         => Actions::get_sync_status(),
362
		);
363
	}
364
365
	protected function get_buffer( $queue, $number_of_items ) {
366
		$start = time();
367
		$max_duration = 5; // this will try to get the buffer
368
369
		$buffer = $queue->checkout( $number_of_items );
370
		$duration = time() - $start;
371
372
		while( is_wp_error( $buffer ) && $duration < $max_duration ) {
373
			sleep( 2 );
374
			$duration = time() - $start;
375
			$buffer = $queue->checkout( $number_of_items );
376
		}
377
378
		if ( $buffer === false ) {
379
			return new WP_Error( 'queue_size', 'The queue is empty and there is nothing to send', 400 );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'queue_size'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
380
		}
381
382
		return $buffer;
383
	}
384
}
385
386
class Jetpack_JSON_API_Sync_Close_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
387
	protected function result() {
388
389
		$request_body = $this->input();
390
		$queue_name = $this->validate_queue( $request_body['queue'] );
391
392
		if ( is_wp_error( $queue_name ) ) {
393
			return $queue_name;
394
		}
395
396
		if ( ! isset( $request_body['buffer_id'] ) ) {
397
			return new WP_Error( 'missing_buffer_id', 'Please provide a buffer id', 400 );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'missing_buffer_id'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
398
		}
399
400
		if ( ! isset( $request_body['item_ids'] ) || ! is_array( $request_body['item_ids'] ) ) {
401
			return new WP_Error( 'missing_item_ids', 'Please provide a list of item ids in the item_ids argument', 400 );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'missing_item_ids'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
402
		}
403
404
		//Limit to A-Z,a-z,0-9,_,-
405
		$request_body ['buffer_id'] = preg_replace( '/[^A-Za-z0-9]/', '', $request_body['buffer_id'] );
406
		$request_body['item_ids'] = array_filter( array_map( array( 'Jetpack_JSON_API_Sync_Close_Endpoint', 'sanitize_item_ids' ), $request_body['item_ids'] ) );
407
408
		$queue = new Queue( $queue_name );
409
410
		$items = $queue->peek_by_id( $request_body['item_ids'] );
411
412
		// Update Full Sync Status if queue is "full_sync".
413
		if ( 'full_sync' === $queue_name ) {
414
			$full_sync_module = Modules::get_module( 'full-sync' );
415
416
			$full_sync_module->update_sent_progress_action( $items );
417
		}
418
419
		$buffer = new Queue_Buffer( $request_body['buffer_id'], $request_body['item_ids'] );
420
		$response = $queue->close( $buffer, $request_body['item_ids'] );
0 ignored issues
show
Documentation introduced by
$buffer is of type object<Automattic\Jetpack\Sync\Queue_Buffer>, but the function expects a object<Automattic\Jetpac...pack\Sync\Queue_Buffer>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
421
422
		// Perform another checkout?
423
		if ( isset( $request_body['continue'] ) && $request_body['continue'] ) {
424
			if ( in_array( $queue_name, array( 'full_sync', 'immediate' ), true ) ) {
425
				// Send Full Sync Actions.
426
				Sender::get_instance()->do_full_sync();
427
			} else {
428
				// Send Incremental Sync Actions.
429
				if ( $queue->has_any_items() ) {
430
					Sender::get_instance()->do_sync();
431
				}
432
			}
433
		}
434
435
		if ( is_wp_error( $response ) ) {
436
			return $response;
437
		}
438
439
		return array(
440
			'success' => $response,
441
			'status' => Actions::get_sync_status(),
442
		);
443
	}
444
445
	protected static function sanitize_item_ids( $item ) {
446
		// lets not delete any options that don't start with jpsq_sync-
447
		if ( ! is_string( $item ) || substr( $item, 0, 5 ) !== 'jpsq_' ) {
448
			return null;
449
		}
450
		//Limit to A-Z,a-z,0-9,_,-,.
451
		return preg_replace( '/[^A-Za-z0-9-_.]/', '', $item );
452
	}
453
}
454
455
class Jetpack_JSON_API_Sync_Unlock_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
456
	protected function result() {
457
		$args = $this->input();
458
459
		if ( ! isset( $args['queue'] ) ) {
460
			return new WP_Error( 'invalid_queue', 'Queue name is required', 400 );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'invalid_queue'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
461
		}
462
463 View Code Duplication
		if ( ! in_array( $args['queue'], array( 'sync', 'full_sync' ) ) ) {
464
			return new WP_Error( 'invalid_queue', 'Queue name should be sync or full_sync', 400 );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'invalid_queue'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
465
		}
466
467
		$queue = new Queue( $args['queue'] );
468
469
		// False means that there was no lock to delete.
470
		$response = $queue->unlock();
471
		return array(
472
			'success' => $response
473
		);
474
	}
475
}
476
477
class Jetpack_JSON_API_Sync_Object_Id_Range extends Jetpack_JSON_API_Sync_Endpoint {
478
	protected function result() {
479
		$args = $this->query_args();
480
481
		$module_name = $args['sync_module'];
482
		$batch_size  = $args['batch_size'];
483
484
		if ( ! $this->is_valid_sync_module( $module_name ) ) {
485
			return new WP_Error( 'invalid_module', 'This sync module cannot be used to calculate a range.', 400 );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'invalid_module'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
486
		}
487
488
		$module = Modules::get_module( $module_name );
489
490
		return array(
491
			'ranges' => $module->get_min_max_object_ids_for_batches( $batch_size ),
492
		);
493
	}
494
495
	protected function is_valid_sync_module( $module_name ) {
496
		return in_array(
497
			$module_name,
498
			array(
499
				'comments',
500
				'posts',
501
				'terms',
502
				'term_relationships',
503
				'users',
504
			),
505
			true
506
		);
507
	}
508
}
509