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