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