1 | <?php |
||
17 | class Jetpack_Sync_Module_Full_Sync extends Jetpack_Sync_Module { |
||
18 | const STATUS_OPTION = 'jetpack_full_sync_status'; |
||
19 | const FULL_SYNC_TIMEOUT = 3600; |
||
20 | |||
21 | public function name() { |
||
22 | return 'full-sync'; |
||
23 | } |
||
24 | |||
25 | function init_listeners( $callable ) { |
||
26 | // synthetic actions for full sync |
||
27 | add_action( 'jetpack_full_sync_start', $callable ); |
||
28 | add_action( 'jetpack_full_sync_end', $callable ); |
||
29 | } |
||
30 | |||
31 | function init_before_send() { |
||
35 | |||
36 | function start( $modules = null ) { |
||
37 | if ( ! $this->should_start_full_sync() ) { |
||
38 | return false; |
||
39 | } |
||
40 | |||
41 | // ensure listener is loaded so we can guarantee full sync actions are enqueued |
||
42 | require_once dirname( __FILE__ ) . '/class.jetpack-sync-listener.php'; |
||
43 | Jetpack_Sync_Listener::get_instance(); |
||
44 | |||
45 | /** |
||
46 | * Fires when a full sync begins. This action is serialized |
||
47 | * and sent to the server so that it knows a full sync is coming. |
||
48 | * |
||
49 | * @since 4.2.0 |
||
50 | */ |
||
51 | do_action( 'jetpack_full_sync_start' ); |
||
52 | $this->set_status_queuing_started(); |
||
53 | |||
54 | foreach ( Jetpack_Sync_Modules::get_modules() as $module ) { |
||
55 | $module_name = $module->name(); |
||
56 | if ( is_array( $modules ) && ! in_array( $module_name, $modules ) ) { |
||
57 | continue; |
||
58 | } |
||
59 | |||
60 | $items_enqueued = $module->enqueue_full_sync_actions(); |
||
61 | if ( 0 !== $items_enqueued ) { |
||
62 | $status = $this->get_status(); |
||
63 | |||
64 | if ( ! isset( $status['queue'][ $module_name ] ) ) { |
||
65 | $status['queue'][ $module_name ] = 0; |
||
66 | } |
||
67 | |||
68 | $status['queue'][ $module_name ] += $items_enqueued; |
||
69 | $this->update_status( $status ); |
||
70 | } |
||
71 | } |
||
72 | |||
73 | $this->set_status_queuing_finished(); |
||
74 | |||
75 | $store = new Jetpack_Sync_WP_Replicastore(); |
||
76 | |||
77 | /** |
||
78 | * Fires when a full sync ends. This action is serialized |
||
79 | * and sent to the server with checksums so that we can confirm the |
||
80 | * sync was successful. |
||
81 | * |
||
82 | * @since 4.2.0 |
||
83 | */ |
||
84 | do_action( 'jetpack_full_sync_end', $store->checksum_all() ); |
||
85 | |||
86 | return true; |
||
87 | } |
||
88 | |||
89 | private function should_start_full_sync() { |
||
90 | $status = $this->get_status(); |
||
91 | |||
92 | // We should try sync if we haven't started it yet or if we have finished it. |
||
93 | if ( is_null( $status['started'] ) || is_integer( $status['finished'] ) ) { |
||
94 | return true; |
||
95 | } |
||
96 | |||
97 | // allow enqueuing if last full sync was started more than FULL_SYNC_TIMEOUT seconds ago |
||
98 | if ( intval( $status['started'] ) + self::FULL_SYNC_TIMEOUT < time() ) { |
||
99 | return true; |
||
100 | } |
||
101 | |||
102 | return false; |
||
103 | } |
||
104 | |||
105 | function update_sent_progress_action( $actions ) { |
||
106 | // quick way to map to first items with an array of arrays |
||
107 | $actions_with_counts = array_count_values( array_map( 'reset', $actions ) ); |
||
108 | |||
109 | $status = $this->get_status(); |
||
110 | if ( is_null( $status['started'] ) || $status['finished'] ) { |
||
111 | return; |
||
112 | } |
||
113 | |||
114 | if ( isset( $actions_with_counts['jetpack_full_sync_start'] ) ) { |
||
115 | $status['sent_started'] = time(); |
||
116 | } |
||
117 | |||
118 | foreach ( Jetpack_Sync_Modules::get_modules() as $module ) { |
||
119 | $module_name = $module->name(); |
||
120 | $module_actions = $module->get_full_sync_actions(); |
||
121 | foreach ( $module_actions as $module_action ) { |
||
122 | if ( isset( $actions_with_counts[ $module_action ] ) ) { |
||
123 | if ( ! isset( $status['sent'][ $module_name ] ) ) { |
||
124 | $status['sent'][ $module_name ] = 0; |
||
125 | } |
||
126 | $status['sent'][ $module_name ] += $actions_with_counts[ $module_action ]; |
||
127 | } |
||
128 | } |
||
129 | } |
||
130 | |||
131 | if ( isset( $actions_with_counts['jetpack_full_sync_end'] ) ) { |
||
132 | $status['finished'] = time(); |
||
133 | } |
||
134 | |||
135 | $this->update_status( $status ); |
||
136 | } |
||
137 | |||
138 | private function set_status_queuing_started() { |
||
139 | $status = $this->initial_status; |
||
140 | $status['started'] = time(); |
||
141 | $this->update_status( $status ); |
||
142 | } |
||
143 | |||
144 | private function set_status_queuing_finished() { |
||
147 | |||
148 | private $initial_status = array( |
||
149 | 'started' => null, |
||
150 | 'queue_finished' => null, |
||
151 | 'sent_started' => null, |
||
152 | 'finished' => null, |
||
153 | 'sent' => array(), |
||
154 | 'queue' => array(), |
||
155 | ); |
||
156 | |||
157 | public function get_status() { |
||
160 | |||
161 | public function update_status( $status ) { |
||
167 | |||
168 | public function clear_status() { |
||
171 | } |
||
172 |