|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class Jetpack_Full_Sync { |
|
4
|
|
|
|
|
5
|
|
|
// writes out a config item for each enabled module containing total items & input config |
|
6
|
|
|
static function start( $modules = null ) { |
|
7
|
|
|
// todo detect if config already set and/or sync already in progress |
|
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
$config = array(); |
|
10
|
|
|
|
|
11
|
|
|
if ( ! is_array( $modules ) ) { |
|
12
|
|
|
$modules = array(); |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
View Code Duplication |
if ( isset( $modules['users'] ) && 'initial' === $modules['users'] ) { |
|
16
|
|
|
$user_module = Jetpack_Sync_Modules::get_module( 'users' ); |
|
17
|
|
|
$modules['users'] = $user_module->get_initial_sync_user_config(); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
// by default, all modules are fully enabled |
|
21
|
|
|
if ( count( $modules ) === 0 ) { |
|
22
|
|
|
$default_module_config = true; |
|
23
|
|
|
} else { |
|
24
|
|
|
$default_module_config = false; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
// set default configuration, calculate totals, and save configuration if totals > 0 |
|
28
|
|
|
foreach ( Jetpack_Sync_Modules::get_modules() as $module ) { |
|
29
|
|
|
$module_name = $module->name(); |
|
30
|
|
|
if ( ! isset( $modules[ $module_name ] ) ) { |
|
31
|
|
|
$modules[ $module_name ] = $default_module_config; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
// check if this module is enabled |
|
35
|
|
|
if ( ! ( $module_config = $modules[ $module_name ] ) ) { |
|
36
|
|
|
continue; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$total_items = $module->estimate_full_sync_actions( $module_config ); |
|
40
|
|
|
|
|
41
|
|
|
if ( ! is_null( $total_items ) && $total_items > 0 ) { |
|
42
|
|
|
$config[ $module_name ] = array( |
|
43
|
|
|
'total' => $total_items, |
|
44
|
|
|
'config' => $module_config, |
|
45
|
|
|
); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
self::set_config( $config ); |
|
50
|
|
|
|
|
51
|
|
|
// now let's set an initial status |
|
52
|
|
|
$status = array(); |
|
53
|
|
|
foreach( $config as $module => $params ) { |
|
54
|
|
|
$status[ $module ] = array(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
self::set_status( $status ); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
static function do_sync() { |
|
61
|
|
|
$config = self::get_config(); |
|
62
|
|
|
$status = self::get_status(); |
|
63
|
|
|
|
|
64
|
|
|
error_log("Got config: ".print_r($config,1)); |
|
65
|
|
|
|
|
66
|
|
|
foreach ( Jetpack_Sync_Modules::get_modules() as $module ) { |
|
67
|
|
|
if ( isset( $status[ $module->name() ][ 'finished' ] ) ) { |
|
68
|
|
|
continue; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
// do work |
|
72
|
|
|
Jetpack_Sync_Settings::set_is_sending( true ); |
|
73
|
|
|
$processed_item_ids = apply_filters( 'jetpack_sync_send_data', $items_to_send, $this->codec->name(), microtime( true ), $queue->id ); |
|
|
|
|
|
|
74
|
|
|
Jetpack_Sync_Settings::set_is_sending( false ); |
|
75
|
|
|
} |
|
76
|
|
|
// get state |
|
77
|
|
|
|
|
78
|
|
|
// continue sync |
|
79
|
|
|
// save state |
|
80
|
|
|
// return result |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
private static function set_config( $config ) { |
|
84
|
|
|
self::write_option( 'jetpack_sync_full_config', $config ); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
private static function get_config() { |
|
88
|
|
|
return self::read_option( 'jetpack_sync_full_config' ); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
private static function set_status( $status ) { |
|
92
|
|
|
self::write_option( 'jetpack_sync_full_status', $status ); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
private static function get_status() { |
|
96
|
|
|
return self::read_option( 'jetpack_sync_full_status' ); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
private static function write_option( $name, $value ) { |
|
100
|
|
|
// we write our own option updating code to bypass filters/caching/etc on set_option/get_option |
|
101
|
|
|
global $wpdb; |
|
102
|
|
|
|
|
103
|
|
|
$serialized_value = maybe_serialize( $value ); |
|
104
|
|
|
|
|
105
|
|
|
// try updating, if no update then insert |
|
106
|
|
|
$updated_num = $wpdb->query( |
|
107
|
|
|
$wpdb->prepare( |
|
108
|
|
|
"UPDATE $wpdb->options SET option_value = %s WHERE option_name = %s", |
|
109
|
|
|
$serialized_value, |
|
110
|
|
|
$name |
|
111
|
|
|
) |
|
112
|
|
|
); |
|
113
|
|
|
|
|
114
|
|
|
if ( ! $updated_num ) { |
|
115
|
|
|
$updated_num = $wpdb->query( |
|
116
|
|
|
$wpdb->prepare( |
|
117
|
|
|
"INSERT INTO $wpdb->options ( option_name, option_value, autoload ) VALUES ( %s, %s, 'no' )", |
|
118
|
|
|
$name, |
|
119
|
|
|
$serialized_value |
|
120
|
|
|
) |
|
121
|
|
|
); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $updated_num; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
private static function read_option( $name ) { |
|
128
|
|
|
global $wpdb; |
|
129
|
|
|
|
|
130
|
|
|
$value = $wpdb->get_var( |
|
131
|
|
|
$wpdb->prepare( |
|
132
|
|
|
"SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", |
|
133
|
|
|
$name |
|
134
|
|
|
) |
|
135
|
|
|
); |
|
136
|
|
|
|
|
137
|
|
|
return maybe_unserialize( $value ); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
// private function set_checkout_id( $checkout_id ) { |
|
141
|
|
|
// global $wpdb; |
|
142
|
|
|
|
|
143
|
|
|
// $expires = time() + Jetpack_Sync_Defaults::$default_sync_queue_lock_timeout; |
|
144
|
|
|
// $updated_num = $wpdb->query( |
|
145
|
|
|
// $wpdb->prepare( |
|
146
|
|
|
// "UPDATE $wpdb->options SET option_value = %s WHERE option_name = %s", |
|
147
|
|
|
// "$checkout_id:$expires", |
|
148
|
|
|
// self::get_lock_option_name() |
|
149
|
|
|
// ) |
|
150
|
|
|
// ); |
|
151
|
|
|
|
|
152
|
|
|
// if ( ! $updated_num ) { |
|
153
|
|
|
// $updated_num = $wpdb->query( |
|
154
|
|
|
// $wpdb->prepare( |
|
155
|
|
|
// "INSERT INTO $wpdb->options ( option_name, option_value, autoload ) VALUES ( %s, %s, 'no' )", |
|
156
|
|
|
// self::get_lock_option_name(), |
|
157
|
|
|
// "$checkout_id:$expires" |
|
158
|
|
|
// ) |
|
159
|
|
|
// ); |
|
160
|
|
|
// } |
|
161
|
|
|
|
|
162
|
|
|
// return $updated_num; |
|
163
|
|
|
// } |
|
164
|
|
|
|
|
165
|
|
|
// private function delete_checkout_id() { |
|
166
|
|
|
// global $wpdb; |
|
167
|
|
|
// // rather than delete, which causes fragmentation, we update in place |
|
168
|
|
|
// return $wpdb->query( |
|
169
|
|
|
// $wpdb->prepare( |
|
170
|
|
|
// "UPDATE $wpdb->options SET option_value = %s WHERE option_name = %s", |
|
171
|
|
|
// "0:0", |
|
172
|
|
|
// self::get_lock_option_name() |
|
173
|
|
|
// ) |
|
174
|
|
|
// ); |
|
175
|
|
|
// } |
|
176
|
|
|
} |