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-sender.php */ |
12
|
|
|
Jetpack_Sync_Actions::schedule_full_sync(); |
13
|
|
|
spawn_cron(); |
14
|
|
|
|
15
|
|
|
return array( 'scheduled' => true ); |
16
|
|
|
} |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
class Jetpack_JSON_API_Sync_Status_Endpoint extends Jetpack_JSON_API_Endpoint { |
|
|
|
|
20
|
|
|
// GET /sites/%s/sync/status |
21
|
|
|
protected $needed_capabilities = 'manage_options'; |
22
|
|
|
|
23
|
|
|
protected function result() { |
24
|
|
|
$sender = Jetpack_Sync_Sender::getInstance(); |
25
|
|
|
return array_merge( |
26
|
|
|
$sender->get_full_sync_client()->get_status(), |
27
|
|
|
array( 'is_scheduled' => (bool) wp_next_scheduled( 'jetpack_sync_full' ) ) |
28
|
|
|
); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
class Jetpack_JSON_API_Sync_Check_Endpoint extends Jetpack_JSON_API_Endpoint { |
|
|
|
|
33
|
|
|
// GET /sites/%s/cached-data-check |
34
|
|
|
protected $needed_capabilities = 'manage_options'; |
35
|
|
|
|
36
|
|
|
protected function result() { |
37
|
|
|
|
38
|
|
|
Jetpack::init(); |
39
|
|
|
|
40
|
|
|
$sender = Jetpack_Sync_Sender::getInstance(); |
41
|
|
|
$sync_queue = $sender->get_sync_queue(); |
42
|
|
|
|
43
|
|
|
// lock sending from the queue while we compare checksums with the server |
44
|
|
|
$result = $sync_queue->lock( 30 ); // tries to acquire the lock for up to 30 seconds |
45
|
|
|
|
46
|
|
|
if ( !$result ) { |
47
|
|
|
$sync_queue->unlock(); |
48
|
|
|
return new WP_Error( 'unknown_error', 'Unknown error trying to lock the sync queue' ); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if ( is_wp_error( $result ) ) { |
52
|
|
|
$sync_queue->unlock(); |
53
|
|
|
return $result; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$store = new Jetpack_Sync_WP_Replicastore(); |
57
|
|
|
|
58
|
|
|
$result = $store->checksum_all(); |
59
|
|
|
|
60
|
|
|
$sync_queue->unlock(); |
61
|
|
|
|
62
|
|
|
return $result; |
63
|
|
|
|
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
class Jetpack_JSON_API_Sync_Modify_Settings_Endpoint extends Jetpack_JSON_API_Endpoint { |
|
|
|
|
68
|
|
|
// POST /sites/%s/sync/settings |
69
|
|
|
protected $needed_capabilities = 'manage_options'; |
70
|
|
|
|
71
|
|
|
protected function result() { |
72
|
|
|
$args = $this->input(); |
73
|
|
|
|
74
|
|
|
$sender = Jetpack_Sync_Sender::getInstance(); |
75
|
|
|
$sync_settings = $sender->get_settings(); |
76
|
|
|
|
77
|
|
|
foreach( $args as $key => $value ) { |
78
|
|
|
if ( $value !== false ) { |
79
|
|
|
if ( is_numeric( $value ) ) { |
80
|
|
|
$value = (int) $value; |
81
|
|
|
} |
82
|
|
|
$sync_settings[ $key ] = $value; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$sender->update_settings( $sync_settings ); |
87
|
|
|
|
88
|
|
|
// re-fetch so we see what's really being stored |
89
|
|
|
return $sender->get_settings(); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
class Jetpack_JSON_API_Sync_Get_Settings_Endpoint extends Jetpack_JSON_API_Endpoint { |
|
|
|
|
94
|
|
|
// GET /sites/%s/sync/settings |
95
|
|
|
protected $needed_capabilities = 'manage_options'; |
96
|
|
|
|
97
|
|
|
protected function result() { |
98
|
|
|
$sender = Jetpack_Sync_Sender::getInstance(); |
99
|
|
|
return $sender->get_settings(); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
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.