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