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 | |||
| 9 | function name() { |
||
| 12 | |||
| 13 | public function init_listeners( $callable ) { |
||
| 48 | |||
| 49 | public function init_full_sync_listeners( $callable ) { |
||
| 52 | |||
| 53 | public function init_before_send() { |
||
| 57 | |||
| 58 | public function update_core_network_event( $wp_db_version, $old_wp_db_version ) { |
||
| 59 | global $wp_version; |
||
| 60 | /** |
||
| 61 | * Sync event for when core wp network updates to a new db version |
||
| 62 | * |
||
| 63 | * @since 5.0.0 |
||
| 64 | * |
||
| 65 | * @param int $wp_db_version the latest wp_db_version |
||
| 66 | * @param int $old_wp_db_version previous wp_db_version |
||
| 67 | * @param string $wp_version the latest wp_version |
||
| 68 | * |
||
| 69 | */ |
||
| 70 | do_action( 'jetpack_sync_core_update_network', $wp_db_version, $old_wp_db_version, $wp_version ); |
||
| 71 | return $wp_db_version; |
||
| 72 | } |
||
| 73 | |||
| 74 | public function update_core( $new_wp_version ) { |
||
| 114 | |||
| 115 | |||
| 116 | public function get_update_checksum( $update, $transient ) { |
||
| 117 | $updates = array(); |
||
| 118 | $no_updated = array(); |
||
| 119 | switch ( $transient ) { |
||
| 120 | case 'update_plugins': |
||
| 121 | if ( ! empty( $update->response ) && is_array( $update->response ) ) { |
||
| 122 | foreach ( $update->response as $plugin_slug => $response ) { |
||
| 123 | if ( ! empty( $plugin_slug ) && isset( $response->new_version ) ) { |
||
| 124 | $updates[] = array( $plugin_slug => $response->new_version ); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | } |
||
| 128 | if ( ! empty( $update->no_update ) ) { |
||
| 129 | $no_updated = array_keys( $update->no_update ); |
||
| 130 | } |
||
| 131 | |||
| 132 | if ( ! isset( $no_updated[ 'jetpack/jetpack.php' ] ) && isset( $updates[ 'jetpack/jetpack.php' ] ) ) { |
||
| 133 | return false; |
||
| 134 | } |
||
| 135 | |||
| 136 | break; |
||
| 137 | case 'update_themes': |
||
| 138 | if ( ! empty( $update->response ) && is_array( $update->response ) ) { |
||
| 139 | foreach ( $update->response as $theme_slug => $response ) { |
||
| 140 | if ( ! empty( $theme_slug ) && isset( $response['new_version'] ) ) { |
||
| 141 | $updates[] = array( $theme_slug => $response['new_version'] ); |
||
| 142 | } |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | if ( ! empty( $update->checked ) ) { |
||
| 147 | $no_updated = $update->checked; |
||
| 148 | } |
||
| 149 | |||
| 150 | break; |
||
| 151 | case 'update_core': |
||
| 152 | if ( ! empty( $update->updates ) && is_array( $update->response ) ) { |
||
| 153 | foreach ( $update->updates as $response ) { |
||
| 154 | if( ! empty( $response->response ) && $response->response === 'latest' ) { |
||
| 155 | continue; |
||
| 156 | } |
||
| 157 | if ( ! empty( $response->response ) && isset( $response->packages->full ) ) { |
||
| 158 | $updates[] = array( $response->response => $response->packages->full ); |
||
| 159 | } |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | if ( ! empty( $update->version_checked ) ) { |
||
| 164 | $no_updated = $update->version_checked; |
||
| 165 | } |
||
| 166 | |||
| 167 | if ( empty( $updates ) ) { |
||
| 168 | return false; |
||
| 169 | } |
||
| 170 | break; |
||
| 171 | |||
| 172 | } |
||
| 173 | if ( empty( $updates ) && empty( $no_updated ) ) { |
||
| 174 | return false; |
||
| 175 | } |
||
| 176 | return $this->get_check_sum( array( $no_updated, $updates ) ); |
||
| 177 | } |
||
| 178 | |||
| 179 | public function validate_update_change( $value, $expiration, $transient ) { |
||
| 180 | |||
| 181 | $new_checksum = $this->get_update_checksum( $value, $transient ); |
||
| 182 | if ( false === $new_checksum ) { |
||
| 183 | return; |
||
| 184 | } |
||
| 185 | |||
| 186 | $checksums = get_option( self::UPDATES_CHECKSUM_OPTION_NAME, array() ); |
||
| 187 | |||
| 188 | if ( isset( $checksums[ $transient ] ) && $checksums[ $transient ] === $new_checksum ) { |
||
| 189 | return; |
||
| 190 | } |
||
| 191 | |||
| 192 | $checksums[ $transient ] = $new_checksum; |
||
| 193 | |||
| 194 | update_option( self::UPDATES_CHECKSUM_OPTION_NAME, $checksums ); |
||
| 195 | do_action( "jetpack_{$transient}_change", $value ); |
||
| 196 | } |
||
| 197 | |||
| 198 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
||
| 199 | /** |
||
| 200 | * Tells the client to sync all updates to the server |
||
| 201 | * |
||
| 202 | * @since 4.2.0 |
||
| 203 | * |
||
| 204 | * @param boolean Whether to expand updates (should always be true) |
||
| 205 | */ |
||
| 206 | do_action( 'jetpack_full_sync_updates', true ); |
||
| 207 | |||
| 208 | // The number of actions enqueued, and next module state (true == done) |
||
| 209 | return array( 1, true ); |
||
| 210 | } |
||
| 211 | |||
| 212 | public function estimate_full_sync_actions( $config ) { |
||
| 213 | return 1; |
||
| 214 | } |
||
| 215 | |||
| 216 | function get_full_sync_actions() { |
||
| 217 | return array( 'jetpack_full_sync_updates' ); |
||
| 218 | } |
||
| 219 | |||
| 220 | public function get_all_updates() { |
||
| 221 | return array( |
||
| 222 | 'core' => get_site_transient( 'update_core' ), |
||
| 223 | 'plugins' => get_site_transient( 'update_plugins' ), |
||
| 224 | 'themes' => get_site_transient( 'update_themes' ), |
||
| 225 | ); |
||
| 226 | } |
||
| 227 | |||
| 228 | // removes unnecessary keys from synced updates data |
||
| 229 | function filter_update_keys( $args ) { |
||
| 230 | $updates = $args[0]; |
||
| 231 | |||
| 232 | if ( isset( $updates->no_update ) ) { |
||
| 233 | unset( $updates->no_update ); |
||
| 234 | } |
||
| 235 | |||
| 236 | return $args; |
||
| 237 | } |
||
| 238 | |||
| 239 | function filter_upgrader_process_complete( $args ) { |
||
| 240 | array_shift( $args ); |
||
| 241 | |||
| 242 | return $args; |
||
| 243 | } |
||
| 244 | |||
| 245 | public function expand_updates( $args ) { |
||
| 246 | if ( $args[0] ) { |
||
| 247 | return $this->get_all_updates(); |
||
| 248 | } |
||
| 249 | |||
| 250 | return $args; |
||
| 251 | } |
||
| 252 | |||
| 253 | public function expand_themes( $args ) { |
||
| 254 | if ( ! isset( $args[0], $args[0]->response ) ) { |
||
| 255 | return $args; |
||
| 256 | } |
||
| 257 | if ( ! is_array( $args[0]->response ) ) { |
||
| 258 | trigger_error( 'Warning: Not an Array as expected but -> ' . wp_json_encode( $args[0]->response ) . ' instead', E_USER_WARNING ); |
||
| 259 | return $args; |
||
| 260 | } |
||
| 261 | foreach ( $args[0]->response as $stylesheet => &$theme_data ) { |
||
| 262 | $theme = wp_get_theme( $stylesheet ); |
||
| 263 | $theme_data['name'] = $theme->name; |
||
| 264 | } |
||
| 265 | return $args; |
||
| 266 | } |
||
| 267 | |||
| 268 | public function reset_data() { |
||
| 271 | } |
||
| 272 |