Completed
Push — update/aag-views ( bf67f2...b39db8 )
by
unknown
24:45 queued 15:23
created

Jetpack_JSON_API_Sync_Histogram_Endpoint   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 36
rs 10
wmc 3
lcom 0
cbo 3

1 Method

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