Completed
Push — suppress-initial-full-sync ( a3a5d8...32db10 )
by
unknown
10:13
created

Jetpack_JSON_API_Sync_Endpoint::result()   D

Complexity

Conditions 18
Paths 36

Size

Total Lines 50

Duplication

Lines 3
Ratio 6 %

Importance

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