Completed
Push — branch-4.2 ( d97905...1b63bc )
by Jeremy
291:55 queued 236:53
created

Jetpack_JSON_API_Sync_Endpoint::result()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 9
Bugs 5 Features 3
Metric Value
cc 4
eloc 9
c 9
b 5
f 3
nc 4
nop 0
dl 0
loc 17
rs 9.2
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['modules'] ) && !empty( $args['modules'] ) ) {
13
			$modules = array_map('trim', explode( ',', $args['modules'] ) );
14
		}
15
16
		if ( empty( $modules ) ) {
17
			$modules = null;
18
		}
19
20
		Jetpack_Sync_Actions::schedule_full_sync( $modules );
21
22
		return array( 'scheduled' => true );
23
	}
24
}
25
26
// GET /sites/%s/sync/status
27
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...
28
	protected $needed_capabilities = 'manage_options';
29
30
	protected function result() {
31
		require_once dirname(__FILE__) . '/../../sync/class.jetpack-sync-modules.php';
32
		$sync_module = Jetpack_Sync_Modules::get_module( 'full-sync' );
33
34
		require_once dirname(__FILE__) . '/../../sync/class.jetpack-sync-sender.php';
35
		$sender = Jetpack_Sync_Sender::get_instance()
36
		$queue = $sender->get_sync_queue();
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_VARIABLE
Loading history...
37
		$full_queue = $sender->get_full_sync_queue();
38
39
		return array_merge(
40
			$sync_module->get_status(),
41
			array( 
42
				'is_scheduled' => (bool) wp_next_scheduled( 'jetpack_sync_full' ), 
43
				'queue_size' => $queue->size(),
44
				'queue_lag' => $queue->lag(),
45
				'full_queue_size' => $full_queue->size(),
46
				'full_queue_lag' => $full_queue->lag()
47
			)
48
		);
49
	}
50
}
51
52
// GET /sites/%s/data-check
53
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...
54
	protected $needed_capabilities = 'manage_options';
55
56
	protected function result() {
57
		require_once dirname(__FILE__) . '/../../sync/class.jetpack-sync-sender.php';
58
59
		$sender = Jetpack_Sync_Sender::get_instance();
60
		$sync_queue = $sender->get_sync_queue();
61
62
		// lock sending from the queue while we compare checksums with the server
63
		$result = $sync_queue->lock( 30 ); // tries to acquire the lock for up to 30 seconds
64
65
		if ( !$result ) {
66
			$sync_queue->unlock();
67
			return new WP_Error( 'unknown_error', 'Unknown error trying to lock the sync queue' );
68
		}
69
70
		if ( is_wp_error( $result ) ) {
71
			$sync_queue->unlock();
72
			return $result;
73
		}
74
75
		require_once dirname(__FILE__) . '/../../sync/class.jetpack-sync-wp-replicastore.php';
76
77
		$store = new Jetpack_Sync_WP_Replicastore();
78
79
		$result = $store->checksum_all();
80
81
		$sync_queue->unlock();
82
83
		return $result;
84
85
	}
86
}
87
88
// GET /sites/%s/data-histogram
89
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...
90
	protected $needed_capabilities = 'manage_options';
91
92
	protected function result() {
93
		require_once dirname(__FILE__) . '/../../sync/class.jetpack-sync-sender.php';
94
95
		$sender = Jetpack_Sync_Sender::get_instance();
96
		$sync_queue = $sender->get_sync_queue();
97
98
		// lock sending from the queue while we compare checksums with the server
99
		$result = $sync_queue->lock( 30 ); // tries to acquire the lock for up to 30 seconds
100
101
		if ( !$result ) {
102
			$sync_queue->unlock();
103
			return new WP_Error( 'unknown_error', 'Unknown error trying to lock the sync queue' );
104
		}
105
106
		if ( is_wp_error( $result ) ) {
107
			$sync_queue->unlock();
108
			return $result;
109
		}
110
111
		$args = $this->query_args();
112
113
		if ( isset( $args['columns'] ) ) {
114
			$columns = array_map('trim', explode( ',', $args['columns'] ) );
115
		} else {
116
			$columns = null; // go with defaults
117
		}
118
119
		require_once dirname(__FILE__) . '/../../sync/class.jetpack-sync-wp-replicastore.php';
120
121
		$store = new Jetpack_Sync_WP_Replicastore();
122
123
		$result = $store->checksum_histogram( $args['object_type'], $args['buckets'], $args['start_id'], $args['end_id'], $columns );
124
125
		$sync_queue->unlock();
126
127
		return $result;
128
129
	}
130
}
131
132
// POST /sites/%s/sync/settings
133
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...
134
	protected $needed_capabilities = 'manage_options';
135
136
	protected function result() {
137
		$args = $this->input();
138
139
		require_once dirname(__FILE__) . '/../../sync/class.jetpack-sync-settings.php';
140
141
		$sync_settings = Jetpack_Sync_Settings::get_settings();
142
143
		foreach( $args as $key => $value ) {
144
			if ( $value !== false ) {
145
				if ( is_numeric( $value ) ) {
146
					$value = (int) $value;
147
				}
148
				$sync_settings[ $key ] = $value;
149
			}
150
		}
151
152
		Jetpack_Sync_Settings::update_settings( $sync_settings );
153
154
		// re-fetch so we see what's really being stored
155
		return Jetpack_Sync_Settings::get_settings();
156
	}
157
}
158
159
// GET /sites/%s/sync/settings
160
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...
161
	protected $needed_capabilities = 'manage_options';
162
163
	protected function result() {
164
		require_once dirname(__FILE__) . '/../../sync/class.jetpack-sync-settings.php';
165
		return Jetpack_Sync_Settings::get_settings();
166
	}
167
}
168