1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
require_once 'class.jetpack-sync-client.php'; |
5
|
|
|
require_once 'class.jetpack-sync-meta.php'; |
6
|
|
|
require_once 'class.jetpack-sync-options.php'; |
7
|
|
|
require_once 'class.jetpack-sync-network-options.php'; |
8
|
|
|
require_once 'class.jetpack-sync-functions.php'; |
9
|
|
|
require_once 'class.jetpack-sync-constants.php'; |
10
|
|
|
require_once 'class.jetpack-sync-users.php'; |
11
|
|
|
require_once 'class.jetpack-sync-updates.php'; |
12
|
|
|
require_once 'class.jetpack-sync-reindex.php'; |
13
|
|
|
require_once 'class.jetpack-sync-themes.php'; |
14
|
|
|
|
15
|
|
|
if ( is_multisite() ) { |
16
|
|
|
require_once 'class.jetpack-sync-network-options.php'; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
class Jetpack_Sync { |
20
|
|
|
static $do_shutdown = false; |
|
|
|
|
21
|
|
|
|
22
|
|
|
static $cron_name = 'jetpack_sync_next_beat'; |
|
|
|
|
23
|
|
|
|
24
|
|
|
static $actions = array(); |
|
|
|
|
25
|
|
|
static $client = null; |
|
|
|
|
26
|
|
|
|
27
|
|
|
static function init() { |
28
|
|
|
|
29
|
|
|
self::$client = new Jetpack_Sync_Client(); |
30
|
|
|
self::$client->init(); |
31
|
|
|
Jetpack_Sync_Meta::init(); |
32
|
|
|
Jetpack_Sync_Options::init(); |
33
|
|
|
Jetpack_Sync_Users::init(); |
34
|
|
|
Jetpack_Sync_Network_Options::init(); |
35
|
|
|
Jetpack_Sync_Updates::init(); |
36
|
|
|
Jetpack_Sync_Reindex::init(); |
37
|
|
|
|
38
|
|
|
// On jetpack version bump |
39
|
|
|
add_action( 'updating_jetpack_version', array( __CLASS__, 'schedule_full_sync' ) ); |
40
|
|
|
// On jetpack registration |
41
|
|
|
add_action( 'jetpack_site_registered', array( __CLASS__, 'schedule_full_sync' ) ); |
42
|
|
|
// Add on cron |
43
|
|
|
add_action( self::$cron_name, array( __CLASS__, 'cron_exec' ) ); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
static function schedule_sync() { |
47
|
|
|
if ( ! self::$do_shutdown ) { |
48
|
|
|
self::$do_shutdown = true; |
49
|
|
|
if ( function_exists( 'ignore_user_abort' ) ) { |
50
|
|
|
ignore_user_abort( true ); |
51
|
|
|
} |
52
|
|
|
add_action( 'shutdown', array( __CLASS__, 'sync_partial_on_shutdown' ), 9 ); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
static function cron_exec() { |
57
|
|
|
Jetpack::xmlrpc_async_call( 'jetpack.sync2', self::get_data_to_sync() ); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
static function schedule_next_cron() { |
61
|
|
|
if ( ! wp_next_scheduled( self::$cron_name ) ) { |
62
|
|
|
$next_minute = time() + 60; |
63
|
|
|
wp_schedule_single_event( $next_minute, self::$cron_name ); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
static function remove_cron() { |
68
|
|
|
$timestamp = wp_next_scheduled( self::$cron_name ); |
69
|
|
|
wp_unschedule_event( $timestamp, self::$cron_name ); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
static function slice_ids( $ids, $max, $option_name ) { |
73
|
|
|
$lock_name = $option_name . '_lock'; |
74
|
|
|
$is_locked = get_option( $lock_name ); |
75
|
|
|
$post_ids_que = get_option( $option_name ); |
76
|
|
|
|
77
|
|
|
if ( ! empty( $post_ids_que ) ) { |
78
|
|
|
$ids = array_unique( array_merge( $ids, $post_ids_que ) ); |
79
|
|
|
} |
80
|
|
|
$pid = getmypid(); |
81
|
|
|
if ( ! $is_locked || $pid === $is_locked ) { |
82
|
|
|
update_option( $lock_name, $pid ); |
83
|
|
|
|
84
|
|
|
if ( sizeof( $ids ) <= $max ) { |
85
|
|
|
delete_option( $option_name ); |
86
|
|
|
delete_option( $lock_name ); |
87
|
|
|
|
88
|
|
|
return $ids; |
89
|
|
|
} |
90
|
|
|
$to_save = array_splice( $ids, $max ); |
91
|
|
|
update_option( $option_name, $to_save ); |
92
|
|
|
delete_option( $lock_name ); |
93
|
|
|
|
94
|
|
|
Jetpack_Sync::schedule_next_cron(); |
95
|
|
|
} else { |
|
|
|
|
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $ids; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
static function schedule_full_sync() { |
103
|
|
|
add_action( 'shutdown', array( __CLASS__, 'sync_full_on_shutdown' ), 9 ); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
static function sync_partial_on_shutdown() { |
107
|
|
|
if ( ! self::should_sync() ) { |
108
|
|
|
return; |
109
|
|
|
} |
110
|
|
|
$to_sync = self::get_data_to_sync(); |
111
|
|
|
Jetpack::xmlrpc_async_call( 'jetpack.sync2', $to_sync ); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
static function sync_full_on_shutdown() { |
115
|
|
|
if ( ! self::should_sync() ) { |
116
|
|
|
return; |
117
|
|
|
} |
118
|
|
|
Jetpack::xmlrpc_async_call( 'jetpack.sync2', self::get_all_data_() ); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
static function should_sync() { |
122
|
|
|
if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) { |
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
if ( Jetpack::is_development_mode() || Jetpack::is_staging_site() ) { |
126
|
|
|
return false; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return true; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
static function get_actions_to_sync() { |
133
|
|
|
return self::$client->get_actions(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
static function get_data_to_sync() { |
138
|
|
|
$send['current_user_id'] = get_current_user_id(); |
|
|
|
|
139
|
|
|
$send['options'] = Jetpack_Sync_Options::get_to_sync(); |
140
|
|
|
$send['options_delete'] = Jetpack_Sync_Options::get_to_delete(); |
141
|
|
|
$send['constants'] = self::sync_if_has_changed( Jetpack_Sync_Constants::$check_sum_id, Jetpack_Sync_Constants::get_all() ); |
|
|
|
|
142
|
|
|
|
143
|
|
|
$send['actions'] = self::get_actions_to_sync(); |
144
|
|
|
|
145
|
|
|
$send['post_meta'] = Jetpack_Sync_Meta::meta_to_sync( 'post' ); |
146
|
|
|
$send['post_meta_delete'] = Jetpack_Sync_Meta::meta_to_delete( 'post' ); |
147
|
|
|
|
148
|
|
|
$send['comments'] = Jetpack_Sync_Comments::comments_to_sync(); |
149
|
|
|
$send['delete_comments'] = Jetpack_Sync_Comments::comments_to_delete(); |
150
|
|
|
$send['comment_meta'] = Jetpack_Sync_Meta::meta_to_sync( 'comment' ); |
151
|
|
|
$send['comment_meta_delete'] = Jetpack_Sync_Meta::meta_to_delete( 'comment' ); |
152
|
|
|
|
153
|
|
|
$send['updates'] = Jetpack_Sync_Updates::get_to_sync(); |
154
|
|
|
$send['themes'] = Jetpack_Sync_Themes::get_to_sync(); |
155
|
|
|
|
156
|
|
|
if ( false === ( $do_check = get_transient( 'jetpack_sync_functions' ) ) ) { |
157
|
|
|
$send['functions'] = self::sync_if_has_changed( Jetpack_Sync_Functions::$check_sum_id, Jetpack_Sync_Functions::get_all() ); |
|
|
|
|
158
|
|
|
set_transient( 'jetpack_sync_functions', true, MINUTE_IN_SECONDS ); |
159
|
|
|
} |
160
|
|
|
if ( is_multisite() ) { |
161
|
|
|
$send['network_options'] = Jetpack_Sync_Network_Options::get_to_sync(); |
162
|
|
|
$send['network_options_delete'] = Jetpack_Sync_Network_Options::get_to_delete(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return array_filter( $send ); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
|
169
|
|
|
static function array_diff_assoc_recursive( $array1, $array2 ) { |
170
|
|
|
$difference = array(); |
171
|
|
|
foreach ( $array1 as $key => $value ) { |
172
|
|
|
if ( is_array( $value ) ) { |
173
|
|
|
if ( ! isset( $array2[ $key ] ) || ! is_array( $array2[ $key ] ) ) { |
174
|
|
|
$difference[ $key ] = $value; |
175
|
|
|
} else { |
176
|
|
|
$new_diff = array_diff_assoc_recursive( $value, $array2[ $key ] ); |
177
|
|
|
if ( ! empty( $new_diff ) ) { |
178
|
|
|
$difference[ $key ] = $new_diff; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
} else if ( ! array_key_exists( $key, $array2 ) || $array2[ $key ] !== $value ) { |
182
|
|
|
$difference[ $key ] = $value; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
return $difference; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
static function get_all_data() { |
189
|
|
|
$send['options'] = Jetpack_Sync_Options::get_all(); |
|
|
|
|
190
|
|
|
$send['constants'] = Jetpack_Sync_Constants::get_all(); |
191
|
|
|
$send['functions'] = Jetpack_Sync_Functions::get_all(); |
192
|
|
|
$send['updates'] = Jetpack_Sync_Updates::get_all(); |
193
|
|
|
$send['themes'] = Jetpack_Sync_Themes::get_all(); |
194
|
|
|
if ( is_multisite() ) { |
195
|
|
|
$send['network_options'] = Jetpack_Sync_Network_Options::get_all(); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return $send; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
static function sync_if_has_changed( $check_sum_id, $values ) { |
202
|
|
|
$current_check_sum = self::get_check_sum( $values ); |
203
|
|
|
if ( Jetpack_Options::get_option( $check_sum_id ) !== $current_check_sum ) { |
204
|
|
|
Jetpack_Options::update_option( $check_sum_id, $current_check_sum ); |
205
|
|
|
|
206
|
|
|
return $values; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
return null; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
static function get_check_sum( $values ) { |
213
|
|
|
return crc32( json_encode( $values ) ); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
Jetpack_Sync::init(); |
219
|
|
|
|
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.