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
|
|
View Code Duplication |
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
|
|
|
$ids = explode( ',', $args[ $module_name ] ); |
21
|
|
|
if ( count( $ids ) > 0 ) { |
22
|
|
|
$modules[ $module_name ] = $ids; |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
if ( empty( $modules ) ) { |
28
|
|
|
$modules = null; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
Jetpack_Sync_Actions::schedule_full_sync( $modules ); |
32
|
|
|
|
33
|
|
|
return array( 'scheduled' => true ); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
// GET /sites/%s/sync/status |
38
|
|
|
class Jetpack_JSON_API_Sync_Status_Endpoint extends Jetpack_JSON_API_Endpoint { |
|
|
|
|
39
|
|
|
protected $needed_capabilities = 'manage_options'; |
40
|
|
|
|
41
|
|
|
protected function result() { |
42
|
|
|
require_once dirname(__FILE__) . '/../../sync/class.jetpack-sync-modules.php'; |
43
|
|
|
require_once dirname(__FILE__) . '/../../sync/class.jetpack-sync-sender.php'; |
44
|
|
|
|
45
|
|
|
$sync_module = Jetpack_Sync_Modules::get_module( 'full-sync' ); |
46
|
|
|
$sender = Jetpack_Sync_Sender::get_instance(); |
47
|
|
|
$queue = $sender->get_sync_queue(); |
48
|
|
|
$full_queue = $sender->get_full_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
|
|
|
'full_queue_size' => $full_queue->size(), |
57
|
|
|
'full_queue_lag' => $full_queue->lag() |
58
|
|
|
) |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// GET /sites/%s/data-check |
64
|
|
|
class Jetpack_JSON_API_Sync_Check_Endpoint extends Jetpack_JSON_API_Endpoint { |
|
|
|
|
65
|
|
|
protected $needed_capabilities = 'manage_options'; |
66
|
|
|
|
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
|
|
|
return new WP_Error( 'unknown_error', 'Unknown error trying to lock the sync queue' ); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ( is_wp_error( $result ) ) { |
82
|
|
|
$sync_queue->unlock(); |
83
|
|
|
return $result; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
require_once dirname(__FILE__) . '/../../sync/class.jetpack-sync-wp-replicastore.php'; |
87
|
|
|
|
88
|
|
|
$store = new Jetpack_Sync_WP_Replicastore(); |
89
|
|
|
|
90
|
|
|
$result = $store->checksum_all(); |
91
|
|
|
|
92
|
|
|
$sync_queue->unlock(); |
93
|
|
|
|
94
|
|
|
return $result; |
95
|
|
|
|
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// GET /sites/%s/data-histogram |
100
|
|
|
class Jetpack_JSON_API_Sync_Histogram_Endpoint extends Jetpack_JSON_API_Endpoint { |
|
|
|
|
101
|
|
|
protected $needed_capabilities = 'manage_options'; |
102
|
|
|
|
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
|
|
|
return new WP_Error( 'unknown_error', 'Unknown error trying to lock the sync queue' ); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if ( is_wp_error( $result ) ) { |
118
|
|
|
$sync_queue->unlock(); |
119
|
|
|
return $result; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$args = $this->query_args(); |
123
|
|
|
|
124
|
|
View Code Duplication |
if ( isset( $args['columns'] ) ) { |
125
|
|
|
$columns = array_map('trim', explode( ',', $args['columns'] ) ); |
126
|
|
|
} else { |
127
|
|
|
$columns = null; // go with defaults |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
require_once dirname(__FILE__) . '/../../sync/class.jetpack-sync-wp-replicastore.php'; |
131
|
|
|
|
132
|
|
|
$store = new Jetpack_Sync_WP_Replicastore(); |
133
|
|
|
|
134
|
|
|
$result = $store->checksum_histogram( $args['object_type'], $args['buckets'], $args['start_id'], $args['end_id'], $columns ); |
135
|
|
|
|
136
|
|
|
$sync_queue->unlock(); |
137
|
|
|
|
138
|
|
|
return $result; |
139
|
|
|
|
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// POST /sites/%s/sync/settings |
144
|
|
|
class Jetpack_JSON_API_Sync_Modify_Settings_Endpoint extends Jetpack_JSON_API_Endpoint { |
|
|
|
|
145
|
|
|
protected $needed_capabilities = 'manage_options'; |
146
|
|
|
|
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_Endpoint { |
|
|
|
|
172
|
|
|
protected $needed_capabilities = 'manage_options'; |
173
|
|
|
|
174
|
|
|
protected function result() { |
175
|
|
|
require_once dirname(__FILE__) . '/../../sync/class.jetpack-sync-settings.php'; |
176
|
|
|
return Jetpack_Sync_Settings::get_settings(); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
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.