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 ( isset( $args[ $module_name ] ) ) { |
24
|
|
|
$ids = explode( ',', $args[ $module_name ] ); |
25
|
|
|
if ( count( $ids ) > 0 ) { |
26
|
|
|
$modules[ $module_name ] = $ids; |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
if ( empty( $modules ) ) { |
32
|
|
|
$modules = null; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
Jetpack_Sync_Actions::schedule_full_sync( $modules ); |
36
|
|
|
|
37
|
|
|
return array( 'scheduled' => true ); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// GET /sites/%s/sync/status |
42
|
|
|
class Jetpack_JSON_API_Sync_Status_Endpoint extends Jetpack_JSON_API_Sync_Endpoint { |
|
|
|
|
43
|
|
|
protected function result() { |
44
|
|
|
require_once dirname( __FILE__ ) . '/../../sync/class.jetpack-sync-modules.php'; |
45
|
|
|
require_once dirname( __FILE__ ) . '/../../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 { |
|
|
|
|
67
|
|
|
protected function result() { |
68
|
|
|
require_once dirname( __FILE__ ) . '/../../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 dirname( __FILE__ ) . '/../../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 { |
|
|
|
|
103
|
|
|
protected function result() { |
104
|
|
|
require_once dirname( __FILE__ ) . '/../../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 dirname( __FILE__ ) . '/../../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 { |
|
|
|
|
147
|
|
|
protected function result() { |
148
|
|
|
$args = $this->input(); |
149
|
|
|
|
150
|
|
|
require_once dirname( __FILE__ ) . '/../../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
|
|
|
$sync_settings[ $key ] = $value; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
Jetpack_Sync_Settings::update_settings( $sync_settings ); |
164
|
|
|
|
165
|
|
|
// re-fetch so we see what's really being stored |
166
|
|
|
return Jetpack_Sync_Settings::get_settings(); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
// GET /sites/%s/sync/settings |
171
|
|
|
class Jetpack_JSON_API_Sync_Get_Settings_Endpoint extends Jetpack_JSON_API_Sync_Endpoint { |
|
|
|
|
172
|
|
|
protected function result() { |
173
|
|
|
require_once dirname( __FILE__ ) . '/../../sync/class.jetpack-sync-settings.php'; |
174
|
|
|
|
175
|
|
|
return Jetpack_Sync_Settings::get_settings(); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
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.