1
|
|
|
<?php |
2
|
|
|
require_once dirname( __FILE__ ) . '/class.jetpack-sync-deflate-codec.php'; |
3
|
|
|
require_once dirname( __FILE__ ) . '/class.jetpack-sync-queue.php'; |
4
|
|
|
require_once dirname( __FILE__ ) . '/class.jetpack-sync-functions.php'; |
5
|
|
|
require_once dirname( __FILE__ ) . '/class.jetpack-sync-full.php'; |
6
|
|
|
require_once dirname( __FILE__ ) . '/class.jetpack-sync-defaults.php'; |
7
|
|
|
|
8
|
|
|
class Jetpack_Sync_Client { |
9
|
|
|
|
10
|
|
|
const CONSTANTS_CHECKSUM_OPTION_NAME = 'jetpack_constants_sync_checksum'; |
11
|
|
|
const CALLABLES_CHECKSUM_OPTION_NAME = 'jetpack_callables_sync_checksum'; |
12
|
|
|
const SYNC_THROTTLE_OPTION_NAME = 'jetpack_sync_min_wait'; |
13
|
|
|
const LAST_SYNC_TIME_OPTION_NAME = 'jetpack_last_sync_time'; |
14
|
|
|
const CALLABLES_AWAIT_TRANSIENT_NAME = 'jetpack_sync_callables_await'; |
15
|
|
|
const CONSTANTS_AWAIT_TRANSIENT_NAME = 'jetpack_sync_constants_await'; |
16
|
|
|
|
17
|
|
|
private $checkout_memory_size; |
18
|
|
|
private $upload_max_bytes; |
19
|
|
|
private $upload_max_rows; |
20
|
|
|
private $sync_queue; |
21
|
|
|
private $full_sync_client; |
22
|
|
|
private $codec; |
23
|
|
|
private $options_whitelist; |
24
|
|
|
private $constants_whitelist; |
25
|
|
|
private $meta_types = array( 'post', 'comment' ); |
26
|
|
|
private $callable_whitelist; |
27
|
|
|
private $network_options_whitelist; |
28
|
|
|
private $taxonomy_whitelist; |
29
|
|
|
private $is_multisite; |
30
|
|
|
|
31
|
|
|
// singleton functions |
32
|
|
|
private static $instance; |
33
|
|
|
|
34
|
|
|
public static function getInstance() { |
35
|
|
|
if ( null === self::$instance ) { |
36
|
|
|
self::$instance = new self(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return self::$instance; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
// this is necessary because you can't use "new" when you declare instance properties >:( |
43
|
|
|
protected function __construct() { |
44
|
|
|
$this->set_defaults(); |
45
|
|
|
$this->init(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
private function init() { |
49
|
|
|
|
50
|
|
|
$handler = array( $this, 'action_handler' ); |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Most of the following hooks are sent to the same $handler |
54
|
|
|
* for immediate serialization and queuing be sent to the server. |
55
|
|
|
* The only exceptions are actions which need additional processing. |
56
|
|
|
*/ |
57
|
|
|
|
58
|
|
|
// constants |
59
|
|
|
add_action( 'jetpack_sync_constant', $handler, 10, 2 ); |
60
|
|
|
|
61
|
|
|
// callables |
62
|
|
|
add_action( 'jetpack_sync_callable', $handler, 10, 2 ); |
63
|
|
|
|
64
|
|
|
// posts |
65
|
|
|
add_action( 'wp_insert_post', $handler, 10, 3 ); |
66
|
|
|
add_action( 'deleted_post', $handler, 10 ); |
67
|
|
|
add_filter( 'jetpack_sync_before_send_wp_insert_post', array( $this, 'expand_wp_insert_post' ) ); |
68
|
|
|
|
69
|
|
|
add_action( 'jetpack_publicize_post', $handler ); |
70
|
|
|
|
71
|
|
|
// attachments |
72
|
|
|
|
73
|
|
|
add_action( 'edit_attachment', array( $this, 'send_attachment_info' ) ); |
74
|
|
|
// Once we don't have to support 4.3 we can start using add_action( 'attachment_updated', $handler, 10, 3 ); instead |
75
|
|
|
add_action( 'add_attachment', array( $this, 'send_attachment_info' ) ); |
76
|
|
|
add_action( 'jetpack_sync_save_add_attachment', $handler, 10, 2 ); |
77
|
|
|
|
78
|
|
|
// comments |
79
|
|
|
add_action( 'wp_insert_comment', $handler, 10, 2 ); |
80
|
|
|
add_action( 'deleted_comment', $handler, 10 ); |
81
|
|
|
add_action( 'trashed_comment', $handler, 10 ); |
82
|
|
|
add_action( 'spammed_comment', $handler, 10 ); |
83
|
|
|
add_filter( 'jetpack_sync_before_send_wp_insert_comment', array( $this, 'expand_wp_insert_comment' ) ); |
84
|
|
|
|
85
|
|
|
// even though it's messy, we implement these hooks because |
86
|
|
|
// the edit_comment hook doesn't include the data |
87
|
|
|
// so this saves us a DB read for every comment event |
88
|
|
|
foreach ( array( '', 'trackback', 'pingback' ) as $comment_type ) { |
89
|
|
|
foreach ( array( 'unapproved', 'approved' ) as $comment_status ) { |
90
|
|
|
$comment_action_name = "comment_{$comment_status}_{$comment_type}"; |
91
|
|
|
add_action( $comment_action_name, $handler, 10, 2 ); |
92
|
|
|
add_filter( "jetpack_sync_before_send_$comment_action_name", array( $this, 'expand_wp_comment_status_change' ) ); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// options |
97
|
|
|
add_action( 'added_option', $handler, 10, 2 ); |
98
|
|
|
add_action( 'updated_option', $handler, 10, 3 ); |
99
|
|
|
add_action( 'deleted_option', $handler, 10, 1 ); |
100
|
|
|
|
101
|
|
|
// Sync Core Icon: Detect changes in Core's Site Icon and make it syncable. |
102
|
|
|
add_action( 'add_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) ); |
103
|
|
|
add_action( 'update_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) ); |
104
|
|
|
add_action( 'delete_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) ); |
105
|
|
|
|
106
|
|
|
// wordpress version |
107
|
|
|
add_action( 'upgrader_process_complete', array( $this, 'send_wp_version' ), 10, 2 ); |
108
|
|
|
add_action( 'jetpack_sync_wp_version', $handler ); |
109
|
|
|
|
110
|
|
|
// themes |
111
|
|
|
add_action( 'switch_theme', array( $this, 'send_theme_info' ) ); |
112
|
|
|
add_action( 'jetpack_sync_current_theme_support', $handler, 10 ); // custom hook, see meta-hooks below |
113
|
|
|
|
114
|
|
|
// post-meta, and in the future - other meta? |
115
|
|
|
foreach ( $this->meta_types as $meta_type ) { |
116
|
|
|
add_action( "added_{$meta_type}_meta", $handler, 10, 4 ); |
117
|
|
|
add_action( "updated_{$meta_type}_meta", $handler, 10, 4 ); |
118
|
|
|
add_action( "deleted_{$meta_type}_meta", $handler, 10, 4 ); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
// terms |
122
|
|
|
add_action( 'created_term', array( $this, 'save_term_handler' ), 10, 3 ); |
123
|
|
|
add_action( 'edited_term', array( $this, 'save_term_handler' ), 10, 3 ); |
124
|
|
|
add_action( 'jetpack_sync_save_term', $handler, 10, 4 ); |
125
|
|
|
add_action( 'delete_term', $handler, 10, 4 ); |
126
|
|
|
add_action( 'set_object_terms', $handler, 10, 6 ); |
127
|
|
|
add_action( 'deleted_term_relationships', $handler, 10, 2 ); |
128
|
|
|
|
129
|
|
|
// users |
130
|
|
|
add_action( 'user_register', array( $this, 'save_user_handler' ) ); |
131
|
|
|
add_action( 'profile_update', array( $this, 'save_user_handler' ), 10, 2 ); |
132
|
|
|
add_action( 'jetpack_sync_save_user', $handler, 10, 2 ); |
133
|
|
|
add_action( 'deleted_user', $handler, 10, 2 ); |
134
|
|
|
|
135
|
|
|
// user roles |
136
|
|
|
add_action( 'add_user_role', array( $this, 'save_user_role_handler' ), 10, 2 ); |
137
|
|
|
add_action( 'set_user_role', array( $this, 'save_user_role_handler' ), 10, 3 ); |
138
|
|
|
add_action( 'remove_user_role', array( $this, 'save_user_role_handler' ), 10, 2 ); |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
// user capabilities |
142
|
|
|
add_action( 'added_user_meta', array( $this, 'save_user_cap_handler' ), 10, 4 ); |
143
|
|
|
add_action( 'updated_user_meta', array( $this, 'save_user_cap_handler' ), 10, 4 ); |
144
|
|
|
add_action( 'deleted_user_meta', array( $this, 'save_user_cap_handler' ), 10, 4 ); |
145
|
|
|
|
146
|
|
|
// themes |
147
|
|
|
add_action( 'set_site_transient_update_plugins', $handler, 10, 1 ); |
148
|
|
|
add_action( 'set_site_transient_update_themes', $handler, 10, 1 ); |
149
|
|
|
add_action( 'set_site_transient_update_core', $handler, 10, 1 ); |
150
|
|
|
|
151
|
|
|
// multi site network options |
152
|
|
|
if ( $this->is_multisite ) { |
153
|
|
|
add_action( 'add_site_option', $handler, 10, 2 ); |
154
|
|
|
add_action( 'update_site_option', $handler, 10, 3 ); |
155
|
|
|
add_action( 'delete_site_option', $handler, 10, 1 ); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
// synthetic actions for full sync |
159
|
|
|
add_action( 'jetpack_full_sync_start', $handler ); |
160
|
|
|
add_action( 'jetpack_full_sync_end', $handler ); |
161
|
|
|
add_action( 'jetpack_full_sync_options', $handler ); |
162
|
|
|
add_action( 'jetpack_full_sync_posts', $handler ); // also sends post meta |
163
|
|
|
add_action( 'jetpack_full_sync_comments', $handler ); // also send comments meta |
164
|
|
|
add_action( 'jetpack_full_sync_users', $handler ); |
165
|
|
|
add_action( 'jetpack_full_sync_terms', $handler, 10, 2 ); |
166
|
|
|
if ( is_multisite() ) { |
167
|
|
|
add_action( 'jetpack_full_sync_network_options', $handler ); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
|
171
|
|
|
// TODO: Callables, Constanst, Network Options, Users, Terms |
|
|
|
|
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Sync all pending actions with server |
175
|
|
|
*/ |
176
|
|
|
add_action( 'jetpack_sync_actions', array( $this, 'do_sync' ) ); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
// TODO: Refactor to use one set whitelist function, with one is_whitelisted. |
|
|
|
|
180
|
|
|
function set_options_whitelist( $options ) { |
181
|
|
|
$this->options_whitelist = $options; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
function get_options_whitelist() { |
185
|
|
|
return $this->options_whitelist; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
function set_constants_whitelist( $constants ) { |
189
|
|
|
$this->constants_whitelist = $constants; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
function get_callable_whitelist() { |
193
|
|
|
return $this->callable_whitelist; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
function set_callable_whitelist( $callables ) { |
197
|
|
|
$this->callable_whitelist = $callables; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
function set_network_options_whitelist( $options ) { |
201
|
|
|
$this->network_options_whitelist = $options; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
function set_send_buffer_memory_size( $size ) { |
205
|
|
|
$this->checkout_memory_size = $size; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
// in bytes |
209
|
|
|
function set_upload_max_bytes( $max_bytes ) { |
210
|
|
|
$this->upload_max_bytes = $max_bytes; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
// in rows |
214
|
|
|
function set_upload_max_rows( $max_rows ) { |
215
|
|
|
$this->upload_max_rows = $max_rows; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
// in seconds |
219
|
|
|
function set_min_sync_wait_time( $seconds ) { |
220
|
|
|
update_option( self::SYNC_THROTTLE_OPTION_NAME, $seconds, true ); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
function get_min_sync_wait_time() { |
224
|
|
|
return get_option( self::SYNC_THROTTLE_OPTION_NAME ); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
private function get_last_sync_time() { |
228
|
|
|
return (double) get_option( self::LAST_SYNC_TIME_OPTION_NAME ); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
private function set_last_sync_time() { |
232
|
|
|
return update_option( self::LAST_SYNC_TIME_OPTION_NAME, microtime( true ), true ); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
function set_taxonomy_whitelist( $taxonomies ) { |
236
|
|
|
$this->taxonomy_whitelist = $taxonomies; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
function is_whitelisted_option( $option ) { |
240
|
|
|
return in_array( $option, $this->options_whitelist ); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
function is_whitelisted_network_option( $option ) { |
244
|
|
|
return $this->is_multisite && in_array( $option, $this->network_options_whitelist ); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
function set_codec( iJetpack_Sync_Codec $codec ) { |
248
|
|
|
$this->codec = $codec; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
function set_full_sync_client( $full_sync_client ) { |
252
|
|
|
if ( $this->full_sync_client ) { |
253
|
|
|
remove_action( 'jetpack_sync_full', array( $this->full_sync_client, 'start' ) ); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
$this->full_sync_client = $full_sync_client; |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Sync all objects in the database with the server |
260
|
|
|
*/ |
261
|
|
|
add_action( 'jetpack_sync_full', array( $this->full_sync_client, 'start' ) ); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
function get_full_sync_client() { |
265
|
|
|
return $this->full_sync_client; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
function action_handler() { |
269
|
|
|
// TODO: it's really silly to have this function here - it should be |
|
|
|
|
270
|
|
|
// wherever we initialize the action listeners or we're just wasting cycles |
271
|
|
|
if ( Jetpack::is_development_mode() || Jetpack::is_staging_site() ) { |
272
|
|
|
return false; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
$current_filter = current_filter(); |
276
|
|
|
$args = func_get_args(); |
277
|
|
|
|
278
|
|
|
if ( $current_filter === 'wp_insert_post' && $args[1]->post_type === 'revision' ) { |
279
|
|
|
return; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
if ( in_array( $current_filter, array( 'deleted_option', 'added_option', 'updated_option' ) ) |
283
|
|
|
&& |
284
|
|
|
! $this->is_whitelisted_option( $args[0] ) |
285
|
|
|
) { |
286
|
|
|
return; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
if ( in_array( $current_filter, array( 'delete_site_option', 'add_site_option', 'update_site_option' ) ) |
290
|
|
|
&& |
291
|
|
|
! $this->is_whitelisted_network_option( $args[0] ) |
292
|
|
|
) { |
293
|
|
|
return; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
// don't sync private meta |
297
|
|
|
if ( preg_match( '/^(added|updated|deleted)_.*_meta$/', $current_filter ) |
298
|
|
|
&& $args[2][0] === '_' |
299
|
|
|
&& ! in_array( $args[2], Jetpack_Sync_Defaults::$default_whitelist_meta_keys ) |
|
|
|
|
300
|
|
|
) { |
301
|
|
|
return; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
$this->sync_queue->add( array( |
305
|
|
|
$current_filter, |
306
|
|
|
$args, |
307
|
|
|
get_current_user_id(), |
308
|
|
|
microtime( true ) |
309
|
|
|
) ); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
function send_theme_info() { |
313
|
|
|
global $_wp_theme_features; |
314
|
|
|
|
315
|
|
|
$theme_support = array(); |
316
|
|
|
|
317
|
|
|
foreach ( Jetpack_Sync_Defaults::$default_theme_support_whitelist as $theme_feature ) { |
|
|
|
|
318
|
|
|
$has_support = current_theme_supports( $theme_feature ); |
319
|
|
|
if ( $has_support ) { |
320
|
|
|
$theme_support[ $theme_feature ] = $_wp_theme_features[ $theme_feature ]; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Fires when the client needs to sync theme support info |
327
|
|
|
* Only sends theme support attributes whitelisted in Jetpack_Sync_Defaults::$default_theme_support_whitelist |
328
|
|
|
* |
329
|
|
|
* @since 4.1.0 |
330
|
|
|
* |
331
|
|
|
* @param object the theme support hash |
332
|
|
|
*/ |
333
|
|
|
do_action( 'jetpack_sync_current_theme_support', $theme_support ); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
function send_wp_version( $update, $meta_data ) { |
337
|
|
|
if ( 'update' === $meta_data['action'] && 'core' === $meta_data['type'] ) { |
338
|
|
|
global $wp_version; |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* Fires when the client needs to sync WordPress version |
342
|
|
|
* |
343
|
|
|
* @since 4.1.0 |
344
|
|
|
* |
345
|
|
|
* @param string The WordPress version number |
346
|
|
|
*/ |
347
|
|
|
do_action( 'jetpack_sync_wp_version', $wp_version ); |
348
|
|
|
} |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
function save_term_handler( $term_id, $tt_id, $taxonomy ) { |
352
|
|
|
if ( class_exists( 'WP_Term' ) ) { |
353
|
|
|
$term_object = WP_Term::get_instance( $term_id, $taxonomy ); |
354
|
|
|
} else { |
355
|
|
|
$term_object = get_term_by( 'id', $term_id, $taxonomy ); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* Fires when the client needs to sync a new term |
360
|
|
|
* |
361
|
|
|
* @since 4.1.0 |
362
|
|
|
* |
363
|
|
|
* @param object the Term object |
364
|
|
|
*/ |
365
|
|
|
do_action( 'jetpack_sync_save_term', $term_object ); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
function send_attachment_info( $attachment_id ) { |
369
|
|
|
$attachment = get_post( $attachment_id ); |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* Fires when the client needs to sync an attachment for a post |
373
|
|
|
* |
374
|
|
|
* @since 4.1.0 |
375
|
|
|
* |
376
|
|
|
* @param int The attachment ID |
377
|
|
|
* @param object The attachment |
378
|
|
|
*/ |
379
|
|
|
do_action( 'jetpack_sync_save_add_attachment', $attachment_id, $attachment ); |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
function save_user_handler( $user_id, $old_user_data = null ) { |
383
|
|
|
$user = $this->sanitize_user( get_user_by( 'id', $user_id ) ); |
384
|
|
|
|
385
|
|
|
// Older versions of WP don't pass the old_user_data in ->data |
386
|
|
|
if ( isset( $old_user_data->data ) ) { |
387
|
|
|
$old_user = $old_user_data->data; |
388
|
|
|
} else { |
389
|
|
|
$old_user = $old_user_data; |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
if ( $old_user !== null ) { |
393
|
|
|
unset( $old_user->user_pass ); |
394
|
|
|
if ( serialize( $old_user ) === serialize( $user->data ) ) { |
395
|
|
|
return; |
396
|
|
|
} |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* Fires when the client needs to sync an updated user |
401
|
|
|
* |
402
|
|
|
* @since 4.1.0 |
403
|
|
|
* |
404
|
|
|
* @param object The WP_User object |
405
|
|
|
*/ |
406
|
|
|
do_action( 'jetpack_sync_save_user', $user ); |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
function save_user_role_handler( $user_id, $role, $old_roles = null ) { |
|
|
|
|
410
|
|
|
$user = $this->sanitize_user( get_user_by( 'id', $user_id ) ); |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* Fires when the client needs to sync an updated user |
414
|
|
|
* |
415
|
|
|
* @since 4.1.0 |
416
|
|
|
* |
417
|
|
|
* @param object The WP_User object |
418
|
|
|
*/ |
419
|
|
|
do_action( 'jetpack_sync_save_user', $user ); |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
function save_user_cap_handler( $meta_id, $user_id, $meta_key, $capabilities ) { |
|
|
|
|
423
|
|
|
$user = $this->sanitize_user( get_user_by( 'id', $user_id ) ); |
424
|
|
|
if ( $meta_key === $user->cap_key ) { |
425
|
|
|
/** |
426
|
|
|
* Fires when the client needs to sync an updated user |
427
|
|
|
* |
428
|
|
|
* @since 4.1.0 |
429
|
|
|
* |
430
|
|
|
* @param object The WP_User object |
431
|
|
|
*/ |
432
|
|
|
do_action( 'jetpack_sync_save_user', $user ); |
433
|
|
|
} |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
public function sanitize_user( $user ) { |
437
|
|
|
unset( $user->data->user_pass ); |
438
|
|
|
|
439
|
|
|
return $user; |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
|
443
|
|
|
function do_sync() { |
444
|
|
|
// don't sync if importing |
445
|
|
|
if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) { |
446
|
|
|
$this->schedule_sync( "+1 minute" ); |
447
|
|
|
|
448
|
|
|
return false; |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
// don't sync if we are throttled |
452
|
|
|
$sync_wait = $this->get_min_sync_wait_time(); |
453
|
|
|
$last_sync = $this->get_last_sync_time(); |
454
|
|
|
|
455
|
|
|
if ( $last_sync && $sync_wait && $last_sync + $sync_wait > microtime( true ) ) { |
456
|
|
|
return false; |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
$this->set_last_sync_time(); |
460
|
|
|
$this->maybe_sync_constants(); |
461
|
|
|
$this->maybe_sync_callables(); |
462
|
|
|
|
463
|
|
|
if ( $this->sync_queue->size() === 0 ) { |
464
|
|
|
return false; |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
$buffer = $this->sync_queue->checkout_with_memory_limit( $this->checkout_memory_size, $this->upload_max_rows ); |
468
|
|
|
|
469
|
|
|
if ( ! $buffer ) { |
470
|
|
|
// buffer has no items |
471
|
|
|
return false; |
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
if ( is_wp_error( $buffer ) ) { |
475
|
|
|
// another buffer is currently sending |
476
|
|
|
return false; |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
$upload_size = 0; |
480
|
|
|
$items_to_send = array(); |
481
|
|
|
|
482
|
|
|
// we estimate the total encoded size as we go by encoding each item individually |
483
|
|
|
// this is expensive, but the only way to really know :/ |
484
|
|
|
foreach ( $buffer->get_items() as $key => $item ) { |
485
|
|
|
|
486
|
|
|
/** |
487
|
|
|
* Modify the data within an action before it is serialized and sent to the server |
488
|
|
|
* For example, during full sync this expands Post ID's into full Post objects, |
489
|
|
|
* so that we don't have to serialize the whole object into the queue. |
490
|
|
|
* |
491
|
|
|
* @since 4.1.0 |
492
|
|
|
* |
493
|
|
|
* @param array The action parameters |
494
|
|
|
*/ |
495
|
|
|
$item[1] = apply_filters( "jetpack_sync_before_send_" . $item[0], $item[1] ); |
496
|
|
|
|
497
|
|
|
$encoded_item = $this->codec->encode( $item ); |
498
|
|
|
$upload_size += strlen( $encoded_item ); |
499
|
|
|
|
500
|
|
|
if ( $upload_size > $this->upload_max_bytes && count( $items_to_send ) > 0 ) { |
501
|
|
|
break; |
502
|
|
|
} |
503
|
|
|
|
504
|
|
|
$items_to_send[ $key ] = $encoded_item; |
505
|
|
|
} |
506
|
|
|
|
507
|
|
|
/** |
508
|
|
|
* Fires when data is ready to send to the server. |
509
|
|
|
* Return false or WP_Error to abort the sync (e.g. if there's an error) |
510
|
|
|
* The items will be automatically re-sent later |
511
|
|
|
* |
512
|
|
|
* @since 4.1 |
513
|
|
|
* |
514
|
|
|
* @param array $data The action buffer |
515
|
|
|
*/ |
516
|
|
|
$result = apply_filters( 'jetpack_sync_client_send_data', $items_to_send ); |
517
|
|
|
|
518
|
|
|
if ( ! $result || is_wp_error( $result ) ) { |
519
|
|
|
// error_log("There was an error sending data:"); |
520
|
|
|
// error_log(print_r($result, 1)); |
521
|
|
|
$result = $this->sync_queue->checkin( $buffer ); |
522
|
|
|
|
523
|
|
|
if ( is_wp_error( $result ) ) { |
524
|
|
|
error_log( "Error checking in buffer: " . $result->get_error_message() ); |
525
|
|
|
$this->sync_queue->force_checkin(); |
526
|
|
|
} |
527
|
|
|
// try again in 1 minute |
528
|
|
|
$this->schedule_sync( "+1 minute" ); |
529
|
|
|
} else { |
530
|
|
|
|
531
|
|
|
// scan the sent data to see if a full sync started or finished |
532
|
|
|
if ( $this->buffer_includes_action( $buffer, 'jetpack_full_sync_start' ) ) { |
533
|
|
|
$this->full_sync_client->set_status_sending_started(); |
534
|
|
|
} |
535
|
|
|
|
536
|
|
|
if ( $this->buffer_includes_action( $buffer, 'jetpack_full_sync_end' ) ) { |
537
|
|
|
$this->full_sync_client->set_status_sending_finished(); |
538
|
|
|
} |
539
|
|
|
|
540
|
|
|
$this->sync_queue->close( $buffer, $result ); |
541
|
|
|
// check if there are any more events in the buffer |
542
|
|
|
// if so, schedule a cron job to happen soon |
543
|
|
|
if ( $this->sync_queue->has_any_items() ) { |
544
|
|
|
$this->schedule_sync( "+1 minute" ); |
545
|
|
|
} |
546
|
|
|
} |
547
|
|
|
} |
548
|
|
|
|
549
|
|
|
private function buffer_includes_action( $buffer, $action_name ) { |
550
|
|
|
foreach ( $buffer->get_items() as $item ) { |
551
|
|
|
if ( $item[0] === $action_name ) { |
552
|
|
|
return true; |
553
|
|
|
} |
554
|
|
|
} |
555
|
|
|
|
556
|
|
|
return false; |
557
|
|
|
} |
558
|
|
|
|
559
|
|
|
function expand_wp_insert_post( $args ) { |
560
|
|
|
return array( $args[0], $this->filter_post_content_and_add_links( $args[1] ), $args[2] ); |
561
|
|
|
} |
562
|
|
|
|
563
|
|
|
// Expands wp_insert_post to include filtered content |
564
|
|
|
function filter_post_content_and_add_links( $post ) { |
565
|
|
|
if ( 0 < strlen( $post->post_password ) ) { |
566
|
|
|
$post->post_password = 'auto-' . wp_generate_password( 10, false ); |
567
|
|
|
} |
568
|
|
|
/** This filter is already documented in core. wp-includes/post-template.php */ |
569
|
|
|
$post->post_content_filtered = apply_filters( 'the_content', $post->post_content ); |
570
|
|
|
$post->permalink = get_permalink( $post->ID ); |
571
|
|
|
$post->shortlink = wp_get_shortlink( $post->ID ); |
572
|
|
|
|
573
|
|
|
// legacy fields until we fully sync users |
574
|
|
|
$extra = array(); |
575
|
|
|
$extra['author_email'] = get_the_author_meta( 'email', $post->post_author ); |
576
|
|
|
$extra['author_display_name'] = get_the_author_meta( 'display_name', $post->post_author ); |
577
|
|
|
$extra['dont_email_post_to_subs'] = get_post_meta( $post->ID, '_jetpack_dont_email_post_to_subs', true ); |
578
|
|
|
$post->extra = $extra; |
579
|
|
|
|
580
|
|
|
return $post; |
581
|
|
|
} |
582
|
|
|
|
583
|
|
|
function expand_wp_comment_status_change( $args ) { |
584
|
|
|
return array( $args[0], $this->filter_comment_and_add_hc_meta( $args[1] ) ); |
585
|
|
|
} |
586
|
|
|
|
587
|
|
|
function expand_wp_insert_comment( $args ) { |
588
|
|
|
return array( $args[0], $this->filter_comment_and_add_hc_meta( $args[1] ) ); |
589
|
|
|
} |
590
|
|
|
|
591
|
|
|
function filter_comment_and_add_hc_meta( $comment ) { |
592
|
|
|
// add meta-property with Highlander Comment meta, which we |
593
|
|
|
// we need to process synchronously on .com |
594
|
|
|
$hc_post_as = get_comment_meta( $comment->comment_ID, 'hc_post_as', true ); |
595
|
|
|
if ( 'wordpress' === $hc_post_as ) { |
596
|
|
|
$meta = array(); |
597
|
|
|
$meta['hc_post_as'] = $hc_post_as; |
598
|
|
|
$meta['hc_wpcom_id_sig'] = get_comment_meta( $comment->comment_ID, 'hc_wpcom_id_sig', true ); |
599
|
|
|
$meta['hc_foreign_user_id'] = get_comment_meta( $comment->comment_ID, 'hc_foreign_user_id', true ); |
600
|
|
|
$comment->meta = $meta; |
601
|
|
|
} |
602
|
|
|
|
603
|
|
|
return $comment; |
604
|
|
|
} |
605
|
|
|
|
606
|
|
|
private function schedule_sync( $when ) { |
607
|
|
|
wp_schedule_single_event( strtotime( $when ), 'jetpack_sync_actions' ); |
608
|
|
|
} |
609
|
|
|
|
610
|
|
|
function force_sync_constants() { |
611
|
|
|
foreach ( $this->constants_whitelist as $name ) { |
612
|
|
|
delete_option( self::CONSTANTS_CHECKSUM_OPTION_NAME . "_$name" ); |
613
|
|
|
} |
614
|
|
|
|
615
|
|
|
delete_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME ); |
616
|
|
|
$this->maybe_sync_constants(); |
617
|
|
|
} |
618
|
|
|
|
619
|
|
|
function force_sync_options() { |
620
|
|
|
/** |
621
|
|
|
* Tells the client to sync all options to the server |
622
|
|
|
* |
623
|
|
|
* @since 4.1 |
624
|
|
|
* |
625
|
|
|
* @param boolean Whether to expand options (should always be true) |
626
|
|
|
*/ |
627
|
|
|
do_action( 'jetpack_full_sync_options', true ); |
628
|
|
|
} |
629
|
|
|
|
630
|
|
|
function force_sync_network_options() { |
631
|
|
|
/** |
632
|
|
|
* Tells the client to sync all network options to the server |
633
|
|
|
* |
634
|
|
|
* @since 4.1 |
635
|
|
|
* |
636
|
|
|
* @param boolean Whether to expand options (should always be true) |
637
|
|
|
*/ |
638
|
|
|
do_action( 'jetpack_full_sync_network_options', true ); |
639
|
|
|
} |
640
|
|
|
|
641
|
|
View Code Duplication |
private function maybe_sync_constants() { |
642
|
|
|
if ( get_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME ) ) { |
643
|
|
|
return; |
644
|
|
|
} |
645
|
|
|
|
646
|
|
|
$constants = $this->get_all_constants(); |
647
|
|
|
if ( empty( $constants ) ) { |
648
|
|
|
return; |
649
|
|
|
} |
650
|
|
|
|
651
|
|
|
set_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME, microtime( true ), Jetpack_Sync_Defaults::$default_sync_constants_wait_time ); |
|
|
|
|
652
|
|
|
|
653
|
|
|
// only send the constants that have changed |
654
|
|
|
foreach ( $constants as $name => $value ) { |
655
|
|
|
$checksum = $this->get_check_sum( $value ); |
656
|
|
|
|
657
|
|
|
// explicitly not using Identical comparison as get_option returns a string |
658
|
|
|
if ( $checksum != get_option( self::CONSTANTS_CHECKSUM_OPTION_NAME . "_$name" ) ) { |
659
|
|
|
/** |
660
|
|
|
* Tells the client to sync a constant to the server |
661
|
|
|
* |
662
|
|
|
* @since 4.1 |
663
|
|
|
* |
664
|
|
|
* @param string The name of the constant |
665
|
|
|
* @param mixed The value of the constant |
666
|
|
|
*/ |
667
|
|
|
do_action( 'jetpack_sync_constant', $name, $value ); |
668
|
|
|
update_option( self::CONSTANTS_CHECKSUM_OPTION_NAME . "_$name", $checksum ); |
669
|
|
|
} |
670
|
|
|
} |
671
|
|
|
} |
672
|
|
|
|
673
|
|
|
private function get_all_constants() { |
674
|
|
|
return array_combine( |
675
|
|
|
$this->constants_whitelist, |
676
|
|
|
array_map( array( $this, 'get_constant' ), $this->constants_whitelist ) |
677
|
|
|
); |
678
|
|
|
} |
679
|
|
|
|
680
|
|
|
private function get_constant( $constant ) { |
681
|
|
|
if ( defined( $constant ) ) { |
682
|
|
|
return constant( $constant ); |
683
|
|
|
} |
684
|
|
|
|
685
|
|
|
return null; |
686
|
|
|
} |
687
|
|
|
|
688
|
|
|
public function force_sync_callables() { |
689
|
|
|
foreach ( $this->callable_whitelist as $name => $config ) { |
690
|
|
|
delete_option( self::CALLABLES_CHECKSUM_OPTION_NAME . "_$name" ); |
691
|
|
|
} |
692
|
|
|
|
693
|
|
|
delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME ); |
694
|
|
|
$this->maybe_sync_callables(); |
695
|
|
|
} |
696
|
|
|
|
697
|
|
View Code Duplication |
private function maybe_sync_callables() { |
698
|
|
|
if ( get_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME ) ) { |
699
|
|
|
return; |
700
|
|
|
} |
701
|
|
|
|
702
|
|
|
$callables = $this->get_all_callables(); |
703
|
|
|
if ( empty( $callables ) ) { |
704
|
|
|
return; |
705
|
|
|
} |
706
|
|
|
|
707
|
|
|
set_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME, microtime( true ), Jetpack_Sync_Defaults::$default_sync_callables_wait_time ); |
|
|
|
|
708
|
|
|
|
709
|
|
|
// only send the callables that have changed |
710
|
|
|
foreach ( $callables as $name => $value ) { |
711
|
|
|
$checksum = $this->get_check_sum( $value ); |
712
|
|
|
// explicitly not using Identical comparison as get_option returns a string |
713
|
|
|
if ( $checksum != get_option( self::CALLABLES_CHECKSUM_OPTION_NAME . "_$name" ) ) { |
714
|
|
|
/** |
715
|
|
|
* Tells the client to sync a callable (aka function) to the server |
716
|
|
|
* |
717
|
|
|
* @since 4.1 |
718
|
|
|
* |
719
|
|
|
* @param string The name of the callable |
720
|
|
|
* @param mixed The value of the callable |
721
|
|
|
*/ |
722
|
|
|
do_action( 'jetpack_sync_callable', $name, $value ); |
723
|
|
|
update_option( self::CALLABLES_CHECKSUM_OPTION_NAME . "_$name", $checksum ); |
724
|
|
|
} |
725
|
|
|
} |
726
|
|
|
} |
727
|
|
|
|
728
|
|
|
private function get_all_callables() { |
729
|
|
|
return array_combine( |
730
|
|
|
array_keys( $this->callable_whitelist ), |
731
|
|
|
array_map( array( $this, 'get_callable' ), array_values( $this->callable_whitelist ) ) |
732
|
|
|
); |
733
|
|
|
} |
734
|
|
|
|
735
|
|
|
private function get_callable( $callable ) { |
736
|
|
|
return call_user_func( $callable ); |
737
|
|
|
} |
738
|
|
|
|
739
|
|
|
// Is public so that we don't have to store so much data all the options twice. |
740
|
|
|
function get_all_options() { |
741
|
|
|
$options = array(); |
742
|
|
|
foreach ( $this->options_whitelist as $option ) { |
743
|
|
|
$options[ $option ] = get_option( $option ); |
744
|
|
|
} |
745
|
|
|
|
746
|
|
|
return $options; |
747
|
|
|
} |
748
|
|
|
|
749
|
|
|
function get_all_network_options() { |
750
|
|
|
$options = array(); |
751
|
|
|
foreach ( $this->network_options_whitelist as $option ) { |
752
|
|
|
$options[ $option ] = get_site_option( $option ); |
753
|
|
|
} |
754
|
|
|
|
755
|
|
|
return $options; |
756
|
|
|
} |
757
|
|
|
|
758
|
|
|
private function get_check_sum( $values ) { |
759
|
|
|
return crc32( json_encode( $values ) ); |
760
|
|
|
} |
761
|
|
|
|
762
|
|
View Code Duplication |
function jetpack_sync_core_icon() { |
763
|
|
|
if ( function_exists( 'get_site_icon_url' ) ) { |
764
|
|
|
$url = get_site_icon_url(); |
765
|
|
|
} else { |
766
|
|
|
return; |
767
|
|
|
} |
768
|
|
|
|
769
|
|
|
require_once( JETPACK__PLUGIN_DIR . 'modules/site-icon/site-icon-functions.php' ); |
770
|
|
|
// If there's a core icon, maybe update the option. If not, fall back to Jetpack's. |
771
|
|
|
if ( ! empty( $url ) && $url !== jetpack_site_icon_url() ) { |
772
|
|
|
// This is the option that is synced with dotcom |
773
|
|
|
Jetpack_Options::update_option( 'site_icon_url', $url ); |
774
|
|
|
} else if ( empty( $url ) && did_action( 'delete_option_site_icon' ) ) { |
775
|
|
|
Jetpack_Options::delete_option( 'site_icon_url' ); |
776
|
|
|
} |
777
|
|
|
} |
778
|
|
|
|
779
|
|
|
function get_sync_queue() { |
780
|
|
|
return $this->sync_queue; |
781
|
|
|
} |
782
|
|
|
|
783
|
|
|
function reset_sync_queue() { |
784
|
|
|
$this->sync_queue->reset(); |
785
|
|
|
} |
786
|
|
|
|
787
|
|
|
function set_defaults() { |
788
|
|
|
$this->sync_queue = new Jetpack_Sync_Queue( 'sync' ); |
789
|
|
|
$this->set_send_buffer_memory_size( Jetpack_Sync_Defaults::$default_send_buffer_memory_size ); |
|
|
|
|
790
|
|
|
$this->set_upload_max_bytes( Jetpack_Sync_Defaults::$default_upload_max_bytes ); |
|
|
|
|
791
|
|
|
$this->set_upload_max_rows( Jetpack_Sync_Defaults::$default_upload_max_rows ); |
|
|
|
|
792
|
|
|
|
793
|
|
|
if ( $this->get_min_sync_wait_time() === false ) { |
794
|
|
|
$this->set_min_sync_wait_time( Jetpack_Sync_Defaults::$default_sync_wait_time ); |
|
|
|
|
795
|
|
|
} |
796
|
|
|
|
797
|
|
|
$this->set_full_sync_client( Jetpack_Sync_Full::getInstance() ); |
798
|
|
|
$this->codec = new Jetpack_Sync_Deflate_Codec(); |
799
|
|
|
$this->constants_whitelist = Jetpack_Sync_Defaults::$default_constants_whitelist; |
|
|
|
|
800
|
|
|
/** |
801
|
|
|
* This filter is already documented class.wpcom-json-api-get-option-endpoint.php |
802
|
|
|
*/ |
803
|
|
|
$this->options_whitelist = apply_filters( 'jetpack_options_whitelist', Jetpack_Sync_Defaults::$default_options_whitelist ); |
|
|
|
|
804
|
|
|
$this->network_options_whitelist = Jetpack_Sync_Defaults::$default_network_options_whitelist; |
|
|
|
|
805
|
|
|
$this->taxonomy_whitelist = Jetpack_Sync_Defaults::$default_taxonomy_whitelist; |
|
|
|
|
806
|
|
|
$this->is_multisite = is_multisite(); |
807
|
|
|
|
808
|
|
|
// theme mod varies from theme to theme. |
809
|
|
|
$this->options_whitelist[] = 'theme_mods_' . get_option( 'stylesheet' ); |
810
|
|
|
if ( $this->is_multisite ) { |
811
|
|
|
$this->callable_whitelist = array_merge( Jetpack_Sync_Defaults::$default_callable_whitelist, Jetpack_Sync_Defaults::$default_multisite_callable_whitelist ); |
|
|
|
|
812
|
|
|
} else { |
813
|
|
|
$this->callable_whitelist = Jetpack_Sync_Defaults::$default_callable_whitelist; |
|
|
|
|
814
|
|
|
} |
815
|
|
|
} |
816
|
|
|
|
817
|
|
|
function reset_data() { |
818
|
|
|
$this->reset_sync_queue(); |
819
|
|
|
} |
820
|
|
|
} |
821
|
|
|
|