1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
require_once dirname(__FILE__).'/../../sync/class.jetpack-sync-wp-replicastore.php'; |
4
|
|
|
|
5
|
|
|
class Jetpack_JSON_API_Sync_Endpoint extends Jetpack_JSON_API_Endpoint { |
6
|
|
|
// POST /sites/%s/sync |
7
|
|
|
protected $needed_capabilities = 'manage_options'; |
8
|
|
|
|
9
|
|
|
protected function result() { |
10
|
|
|
Jetpack::init(); |
11
|
|
|
/** This action is documented in class.jetpack-sync-client.php */ |
12
|
|
|
Jetpack_Sync_Actions::schedule_full_sync(); |
13
|
|
|
|
14
|
|
|
return array( 'scheduled' => true ); |
15
|
|
|
} |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
class Jetpack_JSON_API_Sync_Check_Endpoint extends Jetpack_JSON_API_Endpoint { |
|
|
|
|
19
|
|
|
// POST /sites/%s/cached-data-check |
20
|
|
|
protected $needed_capabilities = 'manage_options'; |
21
|
|
|
|
22
|
|
|
protected function result() { |
23
|
|
|
|
24
|
|
|
Jetpack::init(); |
25
|
|
|
|
26
|
|
|
$client = Jetpack_Sync_Client::getInstance(); |
27
|
|
|
$sync_queue = $client->get_sync_queue(); |
28
|
|
|
|
29
|
|
|
// lock sending from the queue while we compare checksums with the server |
30
|
|
|
$result = $sync_queue->lock( 30 ); // tries to acquire the lock for up to 30 seconds |
31
|
|
|
|
32
|
|
|
if ( !$result ) { |
33
|
|
|
$sync_queue->unlock(); |
34
|
|
|
return new WP_Error( 'unknown_error', 'Unknown error trying to lock the sync queue' ); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if ( is_wp_error( $result ) ) { |
38
|
|
|
$sync_queue->unlock(); |
39
|
|
|
return $result; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$store = new Jetpack_Sync_WP_Replicastore(); |
43
|
|
|
|
44
|
|
|
$result = $store->checksum_all(); |
45
|
|
|
|
46
|
|
|
$sync_queue->unlock(); |
47
|
|
|
|
48
|
|
|
return $result; |
49
|
|
|
|
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
class Jetpack_JSON_API_Sync_Modify_Settings_Endpoint extends Jetpack_JSON_API_Endpoint { |
|
|
|
|
54
|
|
|
// POST /sites/%s/sync/settings |
55
|
|
|
protected $needed_capabilities = 'manage_options'; |
56
|
|
|
|
57
|
|
|
protected function result() { |
58
|
|
|
$args = $this->input(); |
59
|
|
|
|
60
|
|
|
$client = Jetpack_Sync_Client::getInstance(); |
61
|
|
|
$sync_settings = $client->get_settings(); |
62
|
|
|
|
63
|
|
|
foreach( $args as $key => $value ) { |
64
|
|
|
if ( $value !== false ) { |
65
|
|
|
if ( is_numeric( $value ) ) { |
66
|
|
|
$value = (int) $value; |
67
|
|
|
} |
68
|
|
|
$sync_settings[ $key ] = $value; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$client->update_settings( $sync_settings ); |
73
|
|
|
|
74
|
|
|
// re-fetch so we see what's really being stored |
75
|
|
|
return $client->get_settings(); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
class Jetpack_JSON_API_Sync_Get_Settings_Endpoint extends Jetpack_JSON_API_Endpoint { |
|
|
|
|
80
|
|
|
// GET /sites/%s/sync/settings |
81
|
|
|
protected $needed_capabilities = 'manage_options'; |
82
|
|
|
|
83
|
|
|
protected function result() { |
84
|
|
|
$client = Jetpack_Sync_Client::getInstance(); |
85
|
|
|
return $client->get_settings(); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
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.