Completed
Push — update/non-admin-view ( b1ee9d...762240 )
by
unknown
10:19
created

Jetpack_JSON_API_Sync_Endpoint::result()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 5
eloc 14
c 2
b 1
f 0
nc 4
nop 0
dl 0
loc 26
rs 8.439
1
<?php
2
3
class Jetpack_JSON_API_Sync_Endpoint extends Jetpack_JSON_API_Endpoint {
4
	// POST /sites/%s/sync
5
	protected $needed_capabilities = 'manage_options';
6
7
	protected function result() {
8
		$args = $this->input();
9
10
		if ( isset( $args['clear'] ) && $args['clear'] ) {
11
			// clear sync queue
12
			require_once dirname(__FILE__).'/../../sync/class.jetpack-sync-sender.php';
13
14
			$sender = Jetpack_Sync_Sender::getInstance();
15
			$sync_queue = $sender->get_sync_queue();
16
			$sync_queue->reset();
17
		}
18
19
		if ( isset( $args['force'] ) && $args['force'] ) {
20
			// reset full sync lock
21
			require_once dirname(__FILE__).'/../../sync/class.jetpack-sync-modules.php';
22
23
			$sync_module = Jetpack_Sync_Modules::get_module( 'full-sync' );
24
			$sync_module->clear_status();
25
		}
26
27
		/** This action is documented in class.jetpack-sync-sender.php */
28
		Jetpack_Sync_Actions::schedule_full_sync();
29
		spawn_cron();
30
31
		return array( 'scheduled' => true );
32
	}
33
}
34
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
	// GET /sites/%s/sync/status
37
	protected $needed_capabilities = 'manage_options';
38
39
	protected function result() {
40
		require_once dirname(__FILE__).'/../../sync/class.jetpack-sync-modules.php';
41
42
		$sync_module = Jetpack_Sync_Modules::get_module( 'full-sync' );
43
		return array_merge(
44
			$sync_module->get_status(),
45
			array( 'is_scheduled' => (bool) wp_next_scheduled( 'jetpack_sync_full' ) )
46
		);
47
	}
48
}
49
50
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...
51
	// GET /sites/%s/cached-data-check
52
	protected $needed_capabilities = 'manage_options';
53
54
	protected function result() {
55
		require_once dirname(__FILE__).'/../../sync/class.jetpack-sync-sender.php';
56
57
		$sender = Jetpack_Sync_Sender::getInstance();
58
		$sync_queue = $sender->get_sync_queue();
59
60
		// lock sending from the queue while we compare checksums with the server
61
		$result = $sync_queue->lock( 30 ); // tries to acquire the lock for up to 30 seconds
62
63
		if ( !$result ) {
64
			$sync_queue->unlock();
65
			return new WP_Error( 'unknown_error', 'Unknown error trying to lock the sync queue' );
66
		}
67
68
		if ( is_wp_error( $result ) ) {
69
			$sync_queue->unlock();
70
			return $result;
71
		}
72
73
		require_once dirname(__FILE__).'/../../sync/class.jetpack-sync-wp-replicastore.php';
74
75
		$store = new Jetpack_Sync_WP_Replicastore();
76
77
		$result = $store->checksum_all();
78
79
		$sync_queue->unlock();
80
81
		return $result;
82
83
	}
84
}
85
86
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...
87
	// POST /sites/%s/sync/settings
88
	protected $needed_capabilities = 'manage_options';
89
90
	protected function result() {
91
		$args = $this->input();
92
93
		require_once dirname(__FILE__).'/../../sync/class.jetpack-sync-settings.php';
94
95
		$sync_settings = Jetpack_Sync_Settings::get_settings();
96
97
		foreach( $args as $key => $value ) {
98
			if ( $value !== false ) {
99
				if ( is_numeric( $value ) ) {
100
					$value = (int) $value;
101
				}
102
				$sync_settings[ $key ] = $value;
103
			}
104
		}
105
106
		Jetpack_Sync_Settings::update_settings( $sync_settings );
107
108
		// re-fetch so we see what's really being stored
109
		return Jetpack_Sync_Settings::get_settings();
110
	}
111
}
112
113
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...
114
	// GET /sites/%s/sync/settings
115
	protected $needed_capabilities = 'manage_options';
116
117
	protected function result() {
118
		require_once dirname(__FILE__).'/../../sync/class.jetpack-sync-settings.php';
119
		return Jetpack_Sync_Settings::get_settings();
120
	}
121
}
122