Completed
Push — add/sync-id-ranges ( 11c3f9...86ab98 )
by
unknown
288:56 queued 279:27
created

Jetpack_JSON_API_Sync_Endpoint::result()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 6
eloc 12
c 3
b 0
f 0
nc 12
nop 0
dl 0
loc 25
rs 8.439
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 result() {
8
		$args = $this->input();
9
10
		$modules = null;
11
12
		// convert list of modules in comma-delimited format into an array
13
		// of "$modulename => true"
14
		if ( isset( $args['modules'] ) && !empty( $args['modules'] ) ) {
15
			$modules = array_map( '__return_true', array_flip( array_map('trim', explode( ',', $args['modules'] ) ) ) );
16
		}
17
18
		foreach ( array( 'posts', 'comments', 'users' ) as $module_name ) {
19
			if ( isset( $args[ $module_name ] ) ) {
20
				$modules[ $module_name ] = explode( ',', $args[ $module_name ] );
21
			}
22
		}
23
24
		if ( empty( $modules ) ) {
25
			$modules = null;
26
		}
27
28
		Jetpack_Sync_Actions::schedule_full_sync( $modules );
29
30
		return array( 'scheduled' => true );
31
	}
32
}
33
34
// GET /sites/%s/sync/status
35
class Jetpack_JSON_API_Sync_Status_Endpoint extends Jetpack_JSON_API_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...
36
	protected $needed_capabilities = 'manage_options';
37
38
	protected function result() {
39
		require_once dirname(__FILE__) . '/../../sync/class.jetpack-sync-modules.php';
40
		require_once dirname(__FILE__) . '/../../sync/class.jetpack-sync-sender.php';
41
42
		$sync_module = Jetpack_Sync_Modules::get_module( 'full-sync' );
43
		$sender      = Jetpack_Sync_Sender::get_instance();
44
		$queue       = $sender->get_sync_queue();
45
		$full_queue  = $sender->get_full_sync_queue();
46
47
		return array_merge(
48
			$sync_module->get_status(),
49
			array(
50
				'is_scheduled'    => (bool) wp_next_scheduled( 'jetpack_sync_full' ),
51
				'queue_size'      => $queue->size(),
52
				'queue_lag'       => $queue->lag(),
53
				'full_queue_size' => $full_queue->size(),
54
				'full_queue_lag'  => $full_queue->lag()
55
			)
56
		);
57
	}
58
}
59
60
// GET /sites/%s/data-check
61
class Jetpack_JSON_API_Sync_Check_Endpoint extends Jetpack_JSON_API_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...
62
	protected $needed_capabilities = 'manage_options';
63
64
	protected function result() {
65
		require_once dirname(__FILE__) . '/../../sync/class.jetpack-sync-sender.php';
66
67
		$sender = Jetpack_Sync_Sender::get_instance();
68
		$sync_queue = $sender->get_sync_queue();
69
70
		// lock sending from the queue while we compare checksums with the server
71
		$result = $sync_queue->lock( 30 ); // tries to acquire the lock for up to 30 seconds
72
73
		if ( !$result ) {
74
			$sync_queue->unlock();
75
			return new WP_Error( 'unknown_error', 'Unknown error trying to lock the sync queue' );
76
		}
77
78
		if ( is_wp_error( $result ) ) {
79
			$sync_queue->unlock();
80
			return $result;
81
		}
82
83
		require_once dirname(__FILE__) . '/../../sync/class.jetpack-sync-wp-replicastore.php';
84
85
		$store = new Jetpack_Sync_WP_Replicastore();
86
87
		$result = $store->checksum_all();
88
89
		$sync_queue->unlock();
90
91
		return $result;
92
93
	}
94
}
95
96
// GET /sites/%s/data-histogram
97
class Jetpack_JSON_API_Sync_Histogram_Endpoint extends Jetpack_JSON_API_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...
98
	protected $needed_capabilities = 'manage_options';
99
100
	protected function result() {
101
		require_once dirname(__FILE__) . '/../../sync/class.jetpack-sync-sender.php';
102
103
		$sender = Jetpack_Sync_Sender::get_instance();
104
		$sync_queue = $sender->get_sync_queue();
105
106
		// lock sending from the queue while we compare checksums with the server
107
		$result = $sync_queue->lock( 30 ); // tries to acquire the lock for up to 30 seconds
108
109
		if ( !$result ) {
110
			$sync_queue->unlock();
111
			return new WP_Error( 'unknown_error', 'Unknown error trying to lock the sync queue' );
112
		}
113
114
		if ( is_wp_error( $result ) ) {
115
			$sync_queue->unlock();
116
			return $result;
117
		}
118
119
		$args = $this->query_args();
120
121 View Code Duplication
		if ( isset( $args['columns'] ) ) {
122
			$columns = array_map('trim', explode( ',', $args['columns'] ) );
123
		} else {
124
			$columns = null; // go with defaults
125
		}
126
127
		require_once dirname(__FILE__) . '/../../sync/class.jetpack-sync-wp-replicastore.php';
128
129
		$store = new Jetpack_Sync_WP_Replicastore();
130
131
		$result = $store->checksum_histogram( $args['object_type'], $args['buckets'], $args['start_id'], $args['end_id'], $columns );
132
133
		$sync_queue->unlock();
134
135
		return $result;
136
137
	}
138
}
139
140
// POST /sites/%s/sync/settings
141
class Jetpack_JSON_API_Sync_Modify_Settings_Endpoint extends Jetpack_JSON_API_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...
142
	protected $needed_capabilities = 'manage_options';
143
144
	protected function result() {
145
		$args = $this->input();
146
147
		require_once dirname(__FILE__) . '/../../sync/class.jetpack-sync-settings.php';
148
149
		$sync_settings = Jetpack_Sync_Settings::get_settings();
150
151
		foreach( $args as $key => $value ) {
152
			if ( $value !== false ) {
153
				if ( is_numeric( $value ) ) {
154
					$value = (int) $value;
155
				}
156
				$sync_settings[ $key ] = $value;
157
			}
158
		}
159
160
		Jetpack_Sync_Settings::update_settings( $sync_settings );
161
162
		// re-fetch so we see what's really being stored
163
		return Jetpack_Sync_Settings::get_settings();
164
	}
165
}
166
167
// GET /sites/%s/sync/settings
168
class Jetpack_JSON_API_Sync_Get_Settings_Endpoint extends Jetpack_JSON_API_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...
169
	protected $needed_capabilities = 'manage_options';
170
171
	protected function result() {
172
		require_once dirname(__FILE__) . '/../../sync/class.jetpack-sync-settings.php';
173
		return Jetpack_Sync_Settings::get_settings();
174
	}
175
}
176