Completed
Push — remove/duplicate-gravatar-sett... ( 290ada...96cd33 )
by
unknown
09:45
created

Jetpack_JSON_API_Sync_Now_Endpoint   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 10
c 0
b 0
f 0
wmc 3
lcom 0
cbo 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A result() 0 22 3
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
41
// GET /sites/%s/sync/status
42
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...
43
	protected function result() {
44
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-modules.php';
45
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php';
46
47
		$sync_module = Jetpack_Sync_Modules::get_module( 'full-sync' );
48
		$sender      = Jetpack_Sync_Sender::get_instance();
49
		$queue       = $sender->get_sync_queue();
50
		$full_queue  = $sender->get_full_sync_queue();
51
52
		return array_merge(
53
			$sync_module->get_status(),
54
			array(
55
				'is_scheduled'          => Jetpack_Sync_Actions::is_scheduled_full_sync(),
56
				'queue_size'            => $queue->size(),
57
				'queue_lag'             => $queue->lag(),
58
				'queue_next_sync'       => ( $sender->get_next_sync_time( 'sync' ) - microtime( true ) ),
59
				'full_queue_size'       => $full_queue->size(),
60
				'full_queue_lag'        => $full_queue->lag(),
61
				'full_queue_next_sync'  => ( $sender->get_next_sync_time( 'full_sync' ) - microtime( true ) ),
62
			)
63
		);
64
	}
65
}
66
67
// GET /sites/%s/data-check
68
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...
69
	protected function result() {
70
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php';
71
72
		$sender     = Jetpack_Sync_Sender::get_instance();
73
		$sync_queue = $sender->get_sync_queue();
74
75
		// lock sending from the queue while we compare checksums with the server
76
		$result = $sync_queue->lock( 30 ); // tries to acquire the lock for up to 30 seconds
77
78
		if ( ! $result ) {
79
			$sync_queue->unlock();
80
81
			return new WP_Error( 'unknown_error', 'Unknown error trying to lock the sync queue' );
82
		}
83
84
		if ( is_wp_error( $result ) ) {
85
			$sync_queue->unlock();
86
87
			return $result;
88
		}
89
90
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-wp-replicastore.php';
91
92
		$store = new Jetpack_Sync_WP_Replicastore();
93
94
		$result = $store->checksum_all();
95
96
		$sync_queue->unlock();
97
98
		return $result;
99
100
	}
101
}
102
103
// GET /sites/%s/data-histogram
104
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...
105
	protected function result() {
106
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php';
107
108
		$sender     = Jetpack_Sync_Sender::get_instance();
109
		$sync_queue = $sender->get_sync_queue();
110
111
		// lock sending from the queue while we compare checksums with the server
112
		$result = $sync_queue->lock( 30 ); // tries to acquire the lock for up to 30 seconds
113
114
		if ( ! $result ) {
115
			$sync_queue->unlock();
116
117
			return new WP_Error( 'unknown_error', 'Unknown error trying to lock the sync queue' );
118
		}
119
120
		if ( is_wp_error( $result ) ) {
121
			$sync_queue->unlock();
122
123
			return $result;
124
		}
125
126
		$args = $this->query_args();
127
128
		if ( isset( $args['columns'] ) ) {
129
			$columns = array_map( 'trim', explode( ',', $args['columns'] ) );
130
		} else {
131
			$columns = null; // go with defaults
132
		}
133
134
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-wp-replicastore.php';
135
136
		$store = new Jetpack_Sync_WP_Replicastore();
137
138
		$result = $store->checksum_histogram( $args['object_type'], $args['buckets'], $args['start_id'], $args['end_id'], $columns );
139
140
		$sync_queue->unlock();
141
142
		return $result;
143
144
	}
145
}
146
147
// POST /sites/%s/sync/settings
148
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...
149
	protected function result() {
150
		$args = $this->input();
151
152
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-settings.php';
153
154
		$sync_settings = Jetpack_Sync_Settings::get_settings();
155
156
		foreach ( $args as $key => $value ) {
157
			if ( $value !== false ) {
158
				if ( is_numeric( $value ) ) {
159
					$value = (int) $value;
160
				}
161
				
162
				// special case for sending empty arrays - a string with value 'empty'
163
				if ( $value === 'empty' ) {
164
					$value = array();
165
				}
166
167
				$sync_settings[ $key ] = $value;
168
			}
169
		}
170
171
		Jetpack_Sync_Settings::update_settings( $sync_settings );
172
173
		// re-fetch so we see what's really being stored
174
		return Jetpack_Sync_Settings::get_settings();
175
	}
176
}
177
178
// GET /sites/%s/sync/settings
179
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...
180
	protected function result() {
181
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-settings.php';
182
183
		return Jetpack_Sync_Settings::get_settings();
184
	}
185
}
186
187
// GET /sites/%s/sync/object
188
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...
189
	protected function result() {
190
		$args = $this->query_args();
191
192
		$module_name = $args['module_name'];
193
194
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-modules.php';
195
196
		if ( ! $sync_module = Jetpack_Sync_Modules::get_module( $module_name ) ) {
197
			return new WP_Error( 'invalid_module', 'You specified an invalid sync module' );
198
		}
199
200
		$object_type = $args['object_type'];
201
		$object_ids  = $args['object_ids'];
202
203
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php';
204
		$codec = Jetpack_Sync_Sender::get_instance()->get_codec();
205
206
		return array(
207
			'objects' => $codec->encode( $sync_module->get_objects_by_id( $object_type, $object_ids ) )
208
		);
209
	}
210
}
211
212
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...
213
	protected function result() {
214
		$args = $this->input();
215
216
		if ( ! isset( $args['queue'] ) ) {
217
			return new WP_Error( 'invalid_queue', 'Queue name is required', 400 );
218
		}
219
220
		if ( ! in_array( $args['queue'], array( 'sync', 'full_sync' ) ) ) {
221
			return new WP_Error( 'invalid_queue', 'Queue name should be sync or full_sync', 400 );
222
		}
223
224
		$queue_name = $args['queue'];
225
226
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php';
227
228
		$sender = Jetpack_Sync_Sender::get_instance();
229
		$response = $sender->do_sync_for_queue( new Jetpack_Sync_Queue( $queue_name ) );
230
231
		return array(
232
			'response' => $response
233
		);
234
	}
235
}
236