Completed
Push — suppress-initial-full-sync ( a42670...88154e )
by
unknown
53:48 queued 46:37
created

Jetpack_JSON_API_Sync_Endpoint::result()   C

Complexity

Conditions 13
Paths 24

Size

Total Lines 31

Duplication

Lines 3
Ratio 9.68 %

Importance

Changes 0
Metric Value
cc 13
nc 24
nop 0
dl 3
loc 31
rs 6.6166
c 0
b 0
f 0

How to fix   Complexity   

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
		return 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 ( 'posts' === $module_name && isset( $args[ $module_name ] ) && 'all' === $args[ $module_name ] ) {
26
				$posts = get_posts();
27
				error_log(print_r( $posts, true));
28
			} elseif ( isset( $args[ $module_name ] ) ) {
29
				$ids = explode( ',', $args[ $module_name ] );
30
				if ( count( $ids ) > 0 ) {
31
					$modules[ $module_name ] = $ids;
32
				}
33
			}
34
		}
35
36
		if ( empty( $modules ) ) {
37
			$modules = null;
38
		}
39
40
		return array( 'scheduled' => Jetpack_Sync_Actions::do_full_sync( $modules ) );
41
	}
42
43
	protected function validate_queue( $query ) {
44
		if ( ! isset( $query ) ) {
45
			return new WP_Error( 'invalid_queue', 'Queue name is required', 400 );
46
		}
47
48 View Code Duplication
		if ( ! in_array( $query, array( 'sync', 'full_sync' ) ) ) {
49
			return new WP_Error( 'invalid_queue', 'Queue name should be sync or full_sync', 400 );
50
		}
51
		return $query;
52
	}
53
}
54
55
// GET /sites/%s/sync/status
56
class Jetpack_JSON_API_Sync_Status_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
57
	protected function result() {
58
		return Jetpack_Sync_Actions::get_sync_status();
59
	}
60
}
61
62
// GET /sites/%s/data-check
63
class Jetpack_JSON_API_Sync_Check_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
64
	protected function result() {
65
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-wp-replicastore.php';
66
		$store = new Jetpack_Sync_WP_Replicastore();
67
		return $store->checksum_all();
68
	}
69
}
70
71
// GET /sites/%s/data-histogram
72
class Jetpack_JSON_API_Sync_Histogram_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
73
	protected function result() {
74
		$args = $this->query_args();
75
76
		if ( isset( $args['columns'] ) ) {
77
			$columns = array_map( 'trim', explode( ',', $args['columns'] ) );
78
		} else {
79
			$columns = null; // go with defaults
80
		}
81
82
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-wp-replicastore.php';
83
		$store = new Jetpack_Sync_WP_Replicastore();
84
85
		return $store->checksum_histogram( $args['object_type'], $args['buckets'], $args['start_id'], $args['end_id'], $columns, $args['strip_non_ascii'] );
86
	}
87
}
88
89
// POST /sites/%s/sync/settings
90
class Jetpack_JSON_API_Sync_Modify_Settings_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
91
	protected function result() {
92
		$args = $this->input();
93
94
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-settings.php';
95
96
		$sync_settings = Jetpack_Sync_Settings::get_settings();
97
98
		foreach ( $args as $key => $value ) {
99
			if ( $value !== false ) {
100
				if ( is_numeric( $value ) ) {
101
					$value = (int) $value;
102
				}
103
				
104
				// special case for sending empty arrays - a string with value 'empty'
105
				if ( $value === 'empty' ) {
106
					$value = array();
107
				}
108
109
				$sync_settings[ $key ] = $value;
110
			}
111
		}
112
113
		Jetpack_Sync_Settings::update_settings( $sync_settings );
114
115
		// re-fetch so we see what's really being stored
116
		return Jetpack_Sync_Settings::get_settings();
117
	}
118
}
119
120
// GET /sites/%s/sync/settings
121
class Jetpack_JSON_API_Sync_Get_Settings_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
122
	protected function result() {
123
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-settings.php';
124
125
		return Jetpack_Sync_Settings::get_settings();
126
	}
127
}
128
129
// GET /sites/%s/sync/object
130
class Jetpack_JSON_API_Sync_Object extends Jetpack_JSON_API_Sync_Endpoint {
131
	protected function result() {
132
		$args = $this->query_args();
133
134
		$module_name = $args['module_name'];
135
136
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-modules.php';
137
138
		if ( ! $sync_module = Jetpack_Sync_Modules::get_module( $module_name ) ) {
139
			return new WP_Error( 'invalid_module', 'You specified an invalid sync module' );
140
		}
141
142
		$object_type = $args['object_type'];
143
		$object_ids  = $args['object_ids'];
144
145
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php';
146
		$codec = Jetpack_Sync_Sender::get_instance()->get_codec();
147
148
		Jetpack_Sync_Settings::set_is_syncing( true );
149
		$objects = $codec->encode( $sync_module->get_objects_by_id( $object_type, $object_ids ) );
150
		Jetpack_Sync_Settings::set_is_syncing( false );
151
152
		return array(
153
			'objects' => $objects,
154
			'codec' => $codec->name(),
155
		);
156
	}
157
}
158
159
class Jetpack_JSON_API_Sync_Now_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
160
	protected function result() {
161
		$args = $this->input();
162
		$queue_name = $this->validate_queue( $args['queue'] );
163
164
		if ( is_wp_error( $queue_name ) ){
165
			return $queue_name;
166
		}
167
168
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php';
169
170
		$sender = Jetpack_Sync_Sender::get_instance();
171
		$response = $sender->do_sync_for_queue( new Jetpack_Sync_Queue( $args['queue'] ) );
172
173
		return array(
174
			'response' => $response
175
		);
176
	}
177
}
178
179
class Jetpack_JSON_API_Sync_Checkout_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
180
	protected function result() {
181
		$args = $this->input();
182
		$queue_name = $this->validate_queue( $args['queue'] );
183
184
		if ( is_wp_error( $queue_name ) ){
185
			return $queue_name;
186
		}
187
188
		if ( $args[ 'number_of_items' ] < 1 || $args[ 'number_of_items' ] > 100  ) {
189
			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 );
190
		}
191
192
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-queue.php';
193
		$queue = new Jetpack_Sync_Queue( $queue_name );
194
195
		if ( 0 === $queue->size() ) {
196
			return new WP_Error( 'queue_size', 'The queue is empty and there is nothing to send', 400 );
197
		}
198
199
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php';
200
		$sender = Jetpack_Sync_Sender::get_instance();
201
202
		// try to give ourselves as much time as possible
203
		set_time_limit( 0 );
204
205
		// let's delete the checkin state
206
		if ( $args['force'] ) {
207
			$queue->unlock();
208
		}
209
210
		$buffer = $this->get_buffer( $queue, $args[ 'number_of_items' ] );
211
		
212
		// Check that the $buffer is not checkout out already
213
		if ( is_wp_error( $buffer ) ) {
214
			return new WP_Error( 'buffer_open', "We couldn't get the buffer it is currently checked out", 400 );
215
		}
216
		
217
		if ( ! is_object( $buffer ) ) {
218
			return new WP_Error( 'buffer_non-object', 'Buffer is not an object', 400 );
219
		}
220
221
		Jetpack_Sync_Settings::set_is_syncing( true );
222
		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...
223
		Jetpack_Sync_Settings::set_is_syncing( false );
224
225
		return array(
226
			'buffer_id'      => $buffer->id,
227
			'items'          => $items_to_send,
228
			'skipped_items'  => $skipped_items_ids,
229
			'codec'          => $args['encode'] ? $sender->get_codec()->name() : null,
230
			'sent_timestamp' => time(),
231
		);
232
	}
233
234
	protected function get_buffer( $queue, $number_of_items ) {
235
		$start = time();
236
		$max_duration = 5; // this will try to get the buffer
237
238
		$buffer = $queue->checkout( $number_of_items );
239
		$duration = time() - $start;
240
241
		while( is_wp_error( $buffer ) && $duration < $max_duration ) {
242
			sleep( 2 );
243
			$duration = time() - $start;
244
			$buffer = $queue->checkout( $number_of_items );
245
		}
246
247
		if ( $buffer === false ) {
248
			return new WP_Error( 'queue_size', 'The queue is empty and there is nothing to send', 400 );
249
		}
250
251
		return $buffer;
252
	}
253
}
254
255
class Jetpack_JSON_API_Sync_Close_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
256
	protected function result() {
257
		$request_body = $this->input();
258
		$queue_name = $this->validate_queue( $request_body['queue'] );
259
260
		if ( is_wp_error( $queue_name ) ) {
261
			return $queue_name;
262
		}
263
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-queue.php';
264
265
		if ( ! isset( $request_body['buffer_id'] ) ) {
266
			return new WP_Error( 'missing_buffer_id', 'Please provide a buffer id', 400 );
267
		}
268
269
		if ( ! isset( $request_body['item_ids'] ) || ! is_array( $request_body['item_ids'] ) ) {
270
			return new WP_Error( 'missing_item_ids', 'Please provide a list of item ids in the item_ids argument', 400 );
271
		}
272
273
		//Limit to A-Z,a-z,0-9,_,-
274
		$request_body ['buffer_id'] = preg_replace( '/[^A-Za-z0-9]/', '', $request_body['buffer_id'] );
275
		$request_body['item_ids'] = array_filter( array_map( array( 'Jetpack_JSON_API_Sync_Close_Endpoint', 'sanitize_item_ids' ), $request_body['item_ids'] ) );
276
277
		$buffer = new Jetpack_Sync_Queue_Buffer( $request_body['buffer_id'], $request_body['item_ids'] );
278
		$queue = new Jetpack_Sync_Queue( $queue_name );
279
280
		$response = $queue->close( $buffer, $request_body['item_ids'] );
281
282
		if ( is_wp_error( $response ) ) {
283
			return $response;
284
		}
285
286
		return array(
287
			'success' => $response
288
		);
289
	}
290
291
	protected static function sanitize_item_ids( $item ) {
292
		// lets not delete any options that don't start with jpsq_sync-
293
		if ( substr( $item, 0, 5 ) !== 'jpsq_' ) {
294
			return null;
295
		}
296
		//Limit to A-Z,a-z,0-9,_,-,.
297
		return preg_replace( '/[^A-Za-z0-9-_.]/', '', $item );
298
	}
299
}
300
301
class Jetpack_JSON_API_Sync_Unlock_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
302
	protected function result() {
303
		$args = $this->input();
304
305
		if ( ! isset( $args['queue'] ) ) {
306
			return new WP_Error( 'invalid_queue', 'Queue name is required', 400 );
307
		}
308
309 View Code Duplication
		if ( ! in_array( $args['queue'], array( 'sync', 'full_sync' ) ) ) {
310
			return new WP_Error( 'invalid_queue', 'Queue name should be sync or full_sync', 400 );
311
		}
312
313
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-queue.php';
314
		$queue = new Jetpack_Sync_Queue( $args['queue'] );
315
316
		// False means that there was no lock to delete.
317
		$response = $queue->unlock();
318
		return array(
319
			'success' => $response
320
		);
321
	}
322
}
323