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() { |
||
| 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() { |
||
| 152 | |||
| 153 | wp_clean_plugins_cache(); |
||
| 154 | ob_start(); |
||
| 155 | wp_update_plugins(); // Check for Plugin updates |
||
| 156 | ob_end_clean(); |
||
| 157 | |||
| 158 | $update_plugins = get_site_transient( 'update_plugins' ); |
||
| 159 | |||
| 160 | if ( isset( $update_plugins->response ) ) { |
||
| 161 | $plugin_updates_needed = array_keys( $update_plugins->response ); |
||
| 162 | } else { |
||
| 163 | $plugin_updates_needed = array(); |
||
| 164 | } |
||
| 165 | |||
| 166 | $update_attempted = false; |
||
| 167 | |||
| 168 | include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
||
| 169 | |||
| 170 | // unhook this functions that output things before we send our response header. |
||
| 171 | remove_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 ); |
||
| 172 | remove_action( 'upgrader_process_complete', 'wp_version_check' ); |
||
| 173 | remove_action( 'upgrader_process_complete', 'wp_update_themes' ); |
||
| 174 | |||
| 175 | $result = false; |
||
| 176 | |||
| 177 | foreach ( $this->plugins as $plugin ) { |
||
| 178 | |||
| 179 | if ( ! in_array( $plugin, $plugin_updates_needed ) ) { |
||
| 180 | $this->log[ $plugin ][] = __( 'No update needed', 'jetpack' ); |
||
| 181 | continue; |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Pre-upgrade action |
||
| 186 | * |
||
| 187 | * @since 3.9.3 |
||
| 188 | * |
||
| 189 | * @param array $plugin Plugin data |
||
| 190 | * @param array $plugin Array of plugin objects |
||
| 191 | * @param bool $updated_attempted false for the first update, true subsequently |
||
| 192 | */ |
||
| 193 | do_action( 'jetpack_pre_plugin_upgrade', $plugin, $this->plugins, $update_attempted ); |
||
| 194 | |||
| 195 | $update_attempted = true; |
||
| 196 | |||
| 197 | // Object created inside the for loop to clean the messages for each plugin |
||
| 198 | $skin = new Automatic_Upgrader_Skin(); |
||
| 199 | // The Automatic_Upgrader_Skin skin shouldn't output anything. |
||
| 200 | $upgrader = new Plugin_Upgrader( $skin ); |
||
| 201 | $upgrader->init(); |
||
| 202 | // This avoids the plugin to be deactivated. |
||
| 203 | // Using bulk upgrade puts the site into maintenance mode during the upgrades |
||
| 204 | $result = $upgrader->bulk_upgrade( array( $plugin ) ); |
||
| 205 | |||
| 206 | $this->log[ $plugin ] = $upgrader->skin->get_upgrade_messages(); |
||
| 207 | } |
||
| 208 | |||
| 209 | View Code Duplication | if ( ! $this->bulk && ! $result && $update_attempted ) { |
|
| 210 | return new WP_Error( 'update_fail', __( 'There was an error updating your plugin', 'jetpack' ), 400 ); |
||
| 211 | } |
||
| 212 | |||
| 213 | return $this->default_action(); |
||
| 214 | } |
||
| 215 | |||
| 216 | function update_translations() { |
||
| 269 | |||
| 270 | protected function get_translation( $translation ) { |
||
| 273 | } |
||
| 274 |