Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Jetpack_JSON_API_Plugins_Modify_Endpoint 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_JSON_API_Plugins_Modify_Endpoint, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 3 | class Jetpack_JSON_API_Plugins_Modify_Endpoint extends Jetpack_JSON_API_Plugins_Endpoint { |
||
| 4 | // POST /sites/%s/plugins/%s |
||
| 5 | // POST /sites/%s/plugins |
||
| 6 | protected $slug = null; |
||
| 7 | protected $needed_capabilities = 'activate_plugins'; |
||
| 8 | protected $action = 'default_action'; |
||
| 9 | protected $expected_actions = array( 'update', 'install', 'delete', 'update_translations' ); |
||
| 10 | |||
| 11 | public function callback( $path = '', $blog_id = 0, $object = null ) { |
||
| 31 | |||
| 32 | public function default_action() { |
||
| 33 | $args = $this->input(); |
||
| 34 | |||
| 35 | if ( isset( $args['autoupdate'] ) && is_bool( $args['autoupdate'] ) ) { |
||
| 36 | if ( $args['autoupdate'] ) { |
||
| 37 | $this->autoupdate_on(); |
||
| 38 | } else { |
||
| 39 | $this->autoupdate_off(); |
||
| 40 | } |
||
| 41 | } |
||
| 42 | |||
| 43 | if ( isset( $args['active'] ) && is_bool( $args['active'] ) ) { |
||
| 44 | if ( $args['active'] ) { |
||
| 45 | return $this->activate(); |
||
| 46 | } else { |
||
| 47 | return $this->deactivate(); |
||
| 48 | } |
||
| 49 | } |
||
| 50 | |||
| 51 | View Code Duplication | if ( isset( $args['autoupdate_translations'] ) && is_bool( $args['autoupdate_translations'] ) ) { |
|
| 52 | if ( $args['autoupdate_translations'] ) { |
||
| 53 | $this->autoupdate_translations_on(); |
||
| 54 | } else { |
||
| 55 | $this->autoupdate_translations_off(); |
||
| 56 | } |
||
| 57 | } |
||
| 58 | |||
| 59 | return true; |
||
| 60 | } |
||
| 61 | |||
| 62 | protected function autoupdate_on() { |
||
| 67 | |||
| 68 | protected function autoupdate_off() { |
||
| 73 | |||
| 74 | protected function autoupdate_translations_on() { |
||
| 79 | |||
| 80 | protected function autoupdate_translations_off() { |
||
| 85 | |||
| 86 | protected function activate() { |
||
| 125 | |||
| 126 | protected function deactivate() { |
||
| 150 | |||
| 151 | protected function update() { |
||
| 215 | |||
| 216 | function update_translations() { |
||
| 217 | include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
||
| 218 | |||
| 219 | // Clear the cache. |
||
| 220 | wp_clean_plugins_cache(); |
||
| 221 | ob_start(); |
||
| 222 | wp_update_plugins(); // Check for Plugin updates |
||
| 223 | ob_end_clean(); |
||
| 224 | |||
| 225 | $available_updates = get_site_transient( 'update_plugins' ); |
||
| 226 | if ( ! isset( $available_updates->translations ) || empty( $available_updates->translations ) ) { |
||
| 227 | return new WP_Error( 'nothing_to_translate' ); |
||
| 228 | } |
||
| 229 | |||
| 230 | $update_attempted = false; |
||
| 231 | $result = false; |
||
| 232 | foreach( $this->plugins as $plugin ) { |
||
| 233 | $this->slug = Jetpack_Autoupdate::get_plugin_slug( $plugin ); |
||
| 234 | $translation = array_filter( $available_updates->translations, array( $this, 'get_translation' ) ); |
||
| 235 | |||
| 236 | if ( empty( $translation ) ) { |
||
| 237 | $this->log[ $plugin ][] = __( 'No update needed', 'jetpack' ); |
||
| 238 | continue; |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Pre-upgrade action |
||
| 243 | * |
||
| 244 | * @since 4.4 |
||
| 245 | * |
||
| 246 | * @param array $plugin Plugin data |
||
| 247 | * @param array $plugin Array of plugin objects |
||
| 248 | * @param bool $updated_attempted false for the first update, true subsequently |
||
| 249 | */ |
||
| 250 | do_action( 'jetpack_pre_plugin_upgrade_translations', $plugin, $this->plugins, $update_attempted ); |
||
| 251 | |||
| 252 | $update_attempted = true; |
||
| 253 | |||
| 254 | $skin = new Automatic_Upgrader_Skin(); |
||
| 255 | $upgrader = new Language_Pack_Upgrader( $skin ); |
||
| 256 | $upgrader->init(); |
||
| 257 | |||
| 258 | $result = $upgrader->upgrade( (object) $translation[0] ); |
||
| 259 | |||
| 260 | $this->log[ $plugin ] = $upgrader->skin->get_upgrade_messages(); |
||
| 261 | } |
||
| 262 | |||
| 263 | if ( ! $this->bulk && ! $result ) { |
||
| 264 | return new WP_Error( 'update_fail', __( 'There was an error updating your plugin', 'jetpack' ), 400 ); |
||
| 265 | } |
||
| 266 | |||
| 267 | return true; |
||
| 268 | } |
||
| 269 | |||
| 270 | protected function get_translation( $translation ) { |
||
| 273 | } |
||
| 274 |