1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This class does a full resync of the database by |
5
|
|
|
* enqueuing an outbound action for every single object |
6
|
|
|
* that we care about. |
7
|
|
|
* |
8
|
|
|
* This class contains a few non-obvious optimisations that should be explained: |
9
|
|
|
* - we fire an action called jp_full_sync_start so that WPCOM can erase the contents of the cached database |
10
|
|
|
* - for each object type, we obtain a full list of object IDs to sync via a single API call (hoping that since they're ints, they can all fit in RAM) |
11
|
|
|
* - we load the full objects for those IDs in chunks of Jetpack_Sync_Full::$array_chunk_size (to reduce the number of MySQL calls) |
12
|
|
|
* - we fire a trigger for the entire array which the Jetpack_Sync_Client then serializes and queues. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
class Jetpack_Sync_Full { |
16
|
|
|
static $array_chunk_size = 5; |
|
|
|
|
17
|
|
|
|
18
|
|
|
function start() { |
19
|
|
|
$this->client = Jetpack_Sync_Client::getInstance(); |
|
|
|
|
20
|
|
|
do_action( 'jp_full_sync_start' ); |
21
|
|
|
$this->enqueue_all_constants(); |
22
|
|
|
$this->enqueue_all_functions(); |
23
|
|
|
$this->enqueue_all_options(); |
24
|
|
|
$this->enqueue_all_posts(); |
25
|
|
|
$this->enqueue_all_comments(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
private function enqueue_all_constants() { |
29
|
|
|
$this->client->force_sync_constants(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
private function enqueue_all_functions() { |
33
|
|
|
$this->client->force_sync_callables(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
private function enqueue_all_options() { |
37
|
|
|
global $wpdb; |
38
|
|
|
|
39
|
|
|
// Unfortunately, since our options whitelist includes regexen, |
40
|
|
|
// we need to load all option ids/names and match them against the whitelist.. |
41
|
|
|
// This could be pretty awful if we have huge queues, but it's the only way to |
42
|
|
|
// be sure we're syncing everything. |
43
|
|
|
|
44
|
|
|
// As per posts and comments, we do this in ID batches and hope the IDs *AND* names don't exceed RAM |
45
|
|
|
|
46
|
|
|
$option_names = $wpdb->get_col( "SELECT option_name FROM $wpdb->options" ); |
47
|
|
|
|
48
|
|
|
// filter by client option whitelist |
49
|
|
|
$option_names = array_filter( $option_names, array( $this->client, 'is_whitelisted_option' ) ); |
50
|
|
|
|
51
|
|
|
foreach ( $option_names as $option_name ) { |
52
|
|
|
do_action( 'jp_full_sync_option', $option_name, get_option( $option_name ) ); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
View Code Duplication |
private function enqueue_all_posts() { |
57
|
|
|
global $wpdb; |
58
|
|
|
|
59
|
|
|
// I hope this is never bigger than RAM... |
60
|
|
|
$post_ids = $wpdb->get_col( "SELECT id FROM $wpdb->posts"); |
61
|
|
|
|
62
|
|
|
// Request posts in groups of N for efficiency |
63
|
|
|
$chunked_post_ids = array_chunk( $post_ids, self::$array_chunk_size ); |
64
|
|
|
|
65
|
|
|
// Send each chunk as an array of objects |
66
|
|
|
foreach ( $chunked_post_ids as $chunk ) { |
67
|
|
|
$posts = get_posts( array( 'post__in' => $chunk, 'post_status' => 'any' ) ); |
68
|
|
|
do_action( 'jp_full_sync_posts', $posts ); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
View Code Duplication |
private function enqueue_all_comments() { |
73
|
|
|
global $wpdb; |
74
|
|
|
|
75
|
|
|
$comment_ids = $wpdb->get_col( "SELECT comment_id FROM $wpdb->comments"); |
76
|
|
|
$chunked_comment_ids = array_chunk( $comment_ids, self::$array_chunk_size ); |
77
|
|
|
|
78
|
|
|
foreach ( $chunked_comment_ids as $chunk ) { |
79
|
|
|
$comments = get_comments( array( 'comment__in' => $chunk ) ); |
80
|
|
|
do_action( 'jp_full_sync_comments', $comments ); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
} |
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.