Complex classes like Jetpack_Sync_Module_Updates often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Jetpack_Sync_Module_Updates, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
3 | class Jetpack_Sync_Module_Updates extends Jetpack_Sync_Module { |
||
4 | |||
5 | const UPDATES_CHECKSUM_OPTION_NAME = 'jetpack_updates_sync_checksum'; |
||
6 | |||
7 | private $old_wp_version = null; |
||
8 | private $updates = array(); |
||
9 | |||
10 | public function set_defaults() { |
||
13 | |||
14 | function name() { |
||
17 | |||
18 | public function init_listeners( $callable ) { |
||
53 | |||
54 | public function init_full_sync_listeners( $callable ) { |
||
57 | |||
58 | public function init_before_send() { |
||
62 | |||
63 | public function update_core_network_event( $wp_db_version, $old_wp_db_version ) { |
||
78 | |||
79 | public function update_core( $new_wp_version ) { |
||
122 | |||
123 | public function get_update_checksum( $update, $transient ) { |
||
124 | $updates = array(); |
||
125 | $no_updated = array(); |
||
126 | switch ( $transient ) { |
||
127 | case 'update_plugins': |
||
128 | if ( ! empty( $update->response ) && is_array( $update->response ) ) { |
||
129 | foreach ( $update->response as $plugin_slug => $response ) { |
||
130 | if ( ! empty( $plugin_slug ) && isset( $response->new_version ) ) { |
||
131 | $updates[] = array( $plugin_slug => $response->new_version ); |
||
132 | } |
||
133 | } |
||
134 | } |
||
135 | if ( ! empty( $update->no_update ) ) { |
||
136 | $no_updated = array_keys( $update->no_update ); |
||
137 | } |
||
138 | |||
139 | if ( ! isset( $no_updated[ 'jetpack/jetpack.php' ] ) && isset( $updates[ 'jetpack/jetpack.php' ] ) ) { |
||
140 | return false; |
||
141 | } |
||
142 | |||
143 | break; |
||
144 | case 'update_themes': |
||
145 | if ( ! empty( $update->response ) && is_array( $update->response ) ) { |
||
146 | foreach ( $update->response as $theme_slug => $response ) { |
||
147 | if ( ! empty( $theme_slug ) && isset( $response['new_version'] ) ) { |
||
148 | $updates[] = array( $theme_slug => $response['new_version'] ); |
||
149 | } |
||
150 | } |
||
151 | } |
||
152 | |||
153 | if ( ! empty( $update->checked ) ) { |
||
154 | $no_updated = $update->checked; |
||
155 | } |
||
156 | |||
157 | break; |
||
158 | case 'update_core': |
||
159 | if ( ! empty( $update->updates ) && is_array( $update->updates ) ) { |
||
160 | foreach ( $update->updates as $response ) { |
||
161 | if( ! empty( $response->response ) && $response->response === 'latest' ) { |
||
162 | continue; |
||
163 | } |
||
164 | if ( ! empty( $response->response ) && isset( $response->packages->full ) ) { |
||
165 | $updates[] = array( $response->response => $response->packages->full ); |
||
166 | } |
||
167 | } |
||
168 | } |
||
169 | |||
170 | if ( ! empty( $update->version_checked ) ) { |
||
171 | $no_updated = $update->version_checked; |
||
172 | } |
||
173 | |||
174 | if ( empty( $updates ) ) { |
||
175 | return false; |
||
176 | } |
||
177 | break; |
||
178 | |||
179 | } |
||
180 | if ( empty( $updates ) && empty( $no_updated ) ) { |
||
181 | return false; |
||
182 | } |
||
183 | return $this->get_check_sum( array( $no_updated, $updates ) ); |
||
184 | } |
||
185 | |||
186 | public function validate_update_change( $value, $expiration, $transient ) { |
||
187 | $new_checksum = $this->get_update_checksum( $value, $transient ); |
||
188 | |||
189 | if ( false === $new_checksum ) { |
||
190 | return; |
||
191 | } |
||
192 | |||
193 | $checksums = get_option( self::UPDATES_CHECKSUM_OPTION_NAME, array() ); |
||
194 | |||
195 | if ( isset( $checksums[ $transient ] ) && $checksums[ $transient ] === $new_checksum ) { |
||
196 | return; |
||
197 | } |
||
198 | |||
199 | $checksums[ $transient ] = $new_checksum; |
||
200 | |||
201 | update_option( self::UPDATES_CHECKSUM_OPTION_NAME, $checksums ); |
||
202 | if ( 'update_core' === $transient ) { |
||
203 | /** |
||
204 | * jetpack_update_core_change |
||
205 | * |
||
206 | * @since 5.1.0 |
||
207 | * |
||
208 | * @param array containing info that tells us what needs updating |
||
209 | * |
||
210 | */ |
||
211 | do_action( 'jetpack_update_core_change', $value ); |
||
212 | return; |
||
213 | } |
||
214 | if ( empty( $this->updates ) ) { |
||
215 | // lets add the shutdown method once and only when the updates move from empty to filled with something |
||
216 | add_action( 'shutdown', array( $this, 'sync_last_event' ), 9 ); |
||
217 | } |
||
218 | if ( ! isset( $this->updates[ $transient ] ) ) { |
||
219 | $this->updates[ $transient ] = array(); |
||
220 | } |
||
221 | $this->updates[ $transient ][] = $value; |
||
222 | } |
||
223 | |||
224 | public function sync_last_event() { |
||
241 | |||
242 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
||
255 | |||
256 | public function estimate_full_sync_actions( $config ) { |
||
259 | |||
260 | function get_full_sync_actions() { |
||
263 | |||
264 | public function get_all_updates() { |
||
271 | |||
272 | // removes unnecessary keys from synced updates data |
||
273 | function filter_update_keys( $args ) { |
||
282 | |||
283 | function filter_upgrader_process_complete( $args ) { |
||
288 | |||
289 | public function expand_updates( $args ) { |
||
296 | |||
297 | public function expand_themes( $args ) { |
||
311 | |||
312 | public function reset_data() { |
||
315 | } |
||
316 |