Completed
Push — update/remove-disconnect-link ( 4b6a2c )
by
unknown
73:18 queued 63:47
created

Jetpack_JSON_API_Sync_Status_Endpoint   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 5
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

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

1 Method

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