Complex classes like EDD_SL_Plugin_Updater 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 EDD_SL_Plugin_Updater, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class EDD_SL_Plugin_Updater { |
||
| 16 | private $api_url = ''; |
||
| 17 | private $api_data = array(); |
||
| 18 | private $name = ''; |
||
| 19 | private $slug = ''; |
||
| 20 | private $version = ''; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Class constructor. |
||
| 24 | * |
||
| 25 | * @uses plugin_basename() |
||
| 26 | * @uses hook() |
||
| 27 | * |
||
| 28 | * @param string $_api_url The URL pointing to the custom API endpoint. |
||
| 29 | * @param string $_plugin_file Path to the plugin file. |
||
| 30 | * @param array $_api_data Optional data to send with API calls. |
||
| 31 | */ |
||
| 32 | function __construct( $_api_url, $_plugin_file, $_api_data = null ) { |
||
| 33 | $this->api_url = trailingslashit( $_api_url ); |
||
| 34 | $this->api_data = $_api_data; |
||
| 35 | $this->name = plugin_basename( $_plugin_file ); |
||
| 36 | $this->slug = basename( $_plugin_file, '.php' ); |
||
| 37 | $this->version = $_api_data['version']; |
||
| 38 | |||
| 39 | // Set up hooks. |
||
| 40 | $this->init(); |
||
| 41 | add_action( 'admin_init', array( $this, 'show_changelog' ) ); |
||
| 42 | |||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Set up WordPress filters to hook into WP's update process. |
||
| 47 | * |
||
| 48 | * @uses add_filter() |
||
| 49 | * |
||
| 50 | * @return void |
||
| 51 | */ |
||
| 52 | public function init() { |
||
| 53 | add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_update' ) ); |
||
| 54 | add_filter( 'plugins_api', array( $this, 'plugins_api_filter' ), 10, 3 ); |
||
| 55 | |||
| 56 | remove_action( 'after_plugin_row_' . $this->name, 'wp_plugin_update_row', 10, 2 ); |
||
| 57 | add_action( 'after_plugin_row_' . $this->name, array( $this, 'show_update_notification' ), 10, 2 ); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Check for Updates at the defined API endpoint and modify the update array. |
||
| 62 | * |
||
| 63 | * This function dives into the update API just when WordPress creates its update array, |
||
| 64 | * then adds a custom API call and injects the custom plugin data retrieved from the API. |
||
| 65 | * It is reassembled from parts of the native WordPress plugin update code. |
||
| 66 | * See wp-includes/update.php line 121 for the original wp_update_plugins() function. |
||
| 67 | * |
||
| 68 | * @uses api_request() |
||
| 69 | * |
||
| 70 | * @param array $_transient_data Update array build by WordPress. |
||
| 71 | * @return array Modified update array with custom plugin data. |
||
| 72 | */ |
||
| 73 | function check_update( $_transient_data ) { |
||
| 74 | |||
| 75 | global $pagenow; |
||
| 76 | |||
| 77 | if( ! is_object( $_transient_data ) ) { |
||
| 78 | $_transient_data = new stdClass; |
||
| 79 | } |
||
| 80 | |||
| 81 | if( 'plugins.php' == $pagenow && is_multisite() ) { |
||
| 82 | return $_transient_data; |
||
| 83 | } |
||
| 84 | |||
| 85 | if ( empty( $_transient_data->response ) || empty( $_transient_data->response[ $this->name ] ) ) { |
||
| 86 | |||
| 87 | $version_info = $this->api_request( 'plugin_latest_version', array( 'slug' => $this->slug ) ); |
||
| 88 | |||
| 89 | if ( false !== $version_info && is_object( $version_info ) && isset( $version_info->new_version ) ) { |
||
| 90 | |||
| 91 | if( version_compare( $this->version, $version_info->new_version, '<' ) ) { |
||
| 92 | |||
| 93 | $_transient_data->response[ $this->name ] = $version_info; |
||
| 94 | |||
| 95 | } |
||
| 96 | |||
| 97 | $_transient_data->last_checked = time(); |
||
| 98 | $_transient_data->checked[ $this->name ] = $this->version; |
||
| 99 | |||
| 100 | } |
||
| 101 | |||
| 102 | } |
||
| 103 | |||
| 104 | return $_transient_data; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * show update nofication row -- needed for multisite subsites, because WP won't tell you otherwise! |
||
| 109 | * |
||
| 110 | * @param string $file |
||
| 111 | * @param array $plugin |
||
| 112 | */ |
||
| 113 | public function show_update_notification( $file, $plugin ) { |
||
| 114 | |||
| 115 | if( ! current_user_can( 'update_plugins' ) ) { |
||
| 116 | return; |
||
| 117 | } |
||
| 118 | |||
| 119 | if( ! is_multisite() ) { |
||
| 120 | return; |
||
| 121 | } |
||
| 122 | |||
| 123 | if ( $this->name != $file ) { |
||
| 124 | return; |
||
| 125 | } |
||
| 126 | |||
| 127 | // Remove our filter on the site transient |
||
| 128 | remove_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_update' ), 10 ); |
||
| 129 | |||
| 130 | $update_cache = get_site_transient( 'update_plugins' ); |
||
| 131 | |||
| 132 | $update_cache = is_object( $update_cache ) ? $update_cache : new stdClass(); |
||
| 133 | |||
| 134 | if ( empty( $update_cache->response ) || empty( $update_cache->response[ $this->name ] ) ) { |
||
| 135 | |||
| 136 | $cache_key = md5( 'edd_plugin_' .sanitize_key( $this->name ) . '_version_info' ); |
||
| 137 | $version_info = get_transient( $cache_key ); |
||
| 138 | |||
| 139 | if( false === $version_info ) { |
||
| 140 | |||
| 141 | $version_info = $this->api_request( 'plugin_latest_version', array( 'slug' => $this->slug ) ); |
||
| 142 | |||
| 143 | set_transient( $cache_key, $version_info, 3600 ); |
||
| 144 | } |
||
| 145 | |||
| 146 | |||
| 147 | if( ! is_object( $version_info ) ) { |
||
| 148 | return; |
||
| 149 | } |
||
| 150 | |||
| 151 | if( version_compare( $this->version, $version_info->new_version, '<' ) ) { |
||
| 152 | |||
| 153 | $update_cache->response[ $this->name ] = $version_info; |
||
| 154 | |||
| 155 | } |
||
| 156 | |||
| 157 | $update_cache->last_checked = time(); |
||
| 158 | $update_cache->checked[ $this->name ] = $this->version; |
||
| 159 | |||
| 160 | set_site_transient( 'update_plugins', $update_cache ); |
||
| 161 | |||
| 162 | } else { |
||
| 163 | |||
| 164 | $version_info = $update_cache->response[ $this->name ]; |
||
| 165 | |||
| 166 | } |
||
| 167 | |||
| 168 | // Restore our filter |
||
| 169 | add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_update' ) ); |
||
| 170 | |||
| 171 | if ( ! empty( $update_cache->response[ $this->name ] ) && version_compare( $this->version, $version_info->new_version, '<' ) ) { |
||
| 172 | |||
| 173 | // build a plugin list row, with update notification |
||
| 174 | $wp_list_table = _get_list_table( 'WP_Plugins_List_Table' ); |
||
| 175 | echo '<tr class="plugin-update-tr"><td colspan="' . $wp_list_table->get_column_count() . '" class="plugin-update colspanchange"><div class="update-message">'; |
||
| 176 | |||
| 177 | $changelog_link = self_admin_url( 'index.php?edd_sl_action=view_plugin_changelog&plugin=' . $this->name . '&slug=' . $this->slug . '&TB_iframe=true&width=772&height=911' ); |
||
| 178 | |||
| 179 | if ( empty( $version_info->download_link ) ) { |
||
| 180 | printf( |
||
| 181 | __( 'There is a new version of %1$s available. <a target="_blank" class="thickbox" href="%2$s">View version %3$s details</a>.', 'gravityview' ), |
||
| 182 | esc_html( $version_info->name ), |
||
| 183 | esc_url( $changelog_link ), |
||
| 184 | esc_html( $version_info->new_version ) |
||
| 185 | ); |
||
| 186 | } else { |
||
| 187 | printf( |
||
| 188 | __( 'There is a new version of %1$s available. <a target="_blank" class="thickbox" href="%2$s">View version %3$s details</a> or <a href="%4$s">update now</a>.', 'gravityview' ), |
||
| 189 | esc_html( $version_info->name ), |
||
| 190 | esc_url( $changelog_link ), |
||
| 191 | esc_html( $version_info->new_version ), |
||
| 192 | esc_url( wp_nonce_url( self_admin_url( 'update.php?action=upgrade-plugin&plugin=' ) . $this->name, 'upgrade-plugin_' . $this->name ) ) |
||
| 193 | ); |
||
| 194 | } |
||
| 195 | |||
| 196 | echo '</div></td></tr>'; |
||
| 197 | } |
||
| 198 | } |
||
| 199 | |||
| 200 | |||
| 201 | /** |
||
| 202 | * Updates information on the "View version x.x details" page with custom data. |
||
| 203 | * |
||
| 204 | * @uses api_request() |
||
| 205 | * |
||
| 206 | * @param mixed $_data |
||
| 207 | * @param string $_action |
||
| 208 | * @param object $_args |
||
| 209 | * @return object $_data |
||
| 210 | */ |
||
| 211 | function plugins_api_filter( $_data, $_action = '', $_args = null ) { |
||
| 212 | |||
| 213 | |||
| 214 | if ( $_action != 'plugin_information' ) { |
||
| 215 | |||
| 216 | return $_data; |
||
| 217 | |||
| 218 | } |
||
| 219 | |||
| 220 | if ( ! isset( $_args->slug ) || ( $_args->slug != $this->slug ) ) { |
||
| 221 | |||
| 222 | return $_data; |
||
| 223 | |||
| 224 | } |
||
| 225 | |||
| 226 | $to_send = array( |
||
| 227 | 'slug' => $this->slug, |
||
| 228 | 'is_ssl' => is_ssl(), |
||
| 229 | 'fields' => array( |
||
| 230 | 'banners' => true, |
||
| 231 | 'reviews' => false |
||
| 232 | ) |
||
| 233 | ); |
||
| 234 | |||
| 235 | $api_response = $this->api_request( 'plugin_information', $to_send ); |
||
| 236 | |||
| 237 | if ( false !== $api_response ) { |
||
| 238 | $_data = $api_response; |
||
| 239 | } |
||
| 240 | |||
| 241 | return $_data; |
||
| 242 | } |
||
| 243 | |||
| 244 | |||
| 245 | /** |
||
| 246 | * Disable SSL verification in order to prevent download update failures |
||
| 247 | * |
||
| 248 | * @param array $args |
||
| 249 | * @param string $url |
||
| 250 | * @return object $array |
||
| 251 | */ |
||
| 252 | function http_request_args( $args, $url ) { |
||
| 253 | // If it is an https request and we are performing a package download, disable ssl verification |
||
| 254 | if ( strpos( $url, 'https://' ) !== false && strpos( $url, 'edd_action=package_download' ) ) { |
||
| 255 | $args['sslverify'] = false; |
||
| 256 | } |
||
| 257 | return $args; |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Calls the API and, if successfull, returns the object delivered by the API. |
||
| 262 | * |
||
| 263 | * @uses get_bloginfo() |
||
| 264 | * @uses wp_remote_post() |
||
| 265 | * @uses is_wp_error() |
||
| 266 | * |
||
| 267 | * @param string $_action The requested action. |
||
| 268 | * @param array $_data Parameters for the API action. |
||
| 269 | * @return false|object |
||
| 270 | */ |
||
| 271 | private function api_request( $_action, $_data ) { |
||
| 272 | |||
| 273 | global $wp_version; |
||
| 274 | |||
| 275 | $data = array_merge( $this->api_data, $_data ); |
||
| 276 | |||
| 277 | if ( $data['slug'] != $this->slug ) { |
||
| 278 | return; |
||
| 279 | } |
||
| 280 | |||
| 281 | if( $this->api_url == home_url() ) { |
||
| 282 | return false; // Don't allow a plugin to ping itself |
||
| 283 | } |
||
| 284 | |||
| 285 | $api_params = array( |
||
| 286 | 'edd_action' => 'get_version', |
||
| 287 | 'license' => ! empty( $data['license'] ) ? $data['license'] : '', |
||
| 288 | 'item_name' => isset( $data['item_name'] ) ? $data['item_name'] : false, |
||
| 289 | 'item_id' => isset( $data['item_id'] ) ? $data['item_id'] : false, |
||
| 290 | 'slug' => $data['slug'], |
||
| 291 | 'author' => $data['author'], |
||
| 292 | 'url' => home_url() |
||
| 293 | ); |
||
| 294 | |||
| 295 | $request = wp_remote_post( $this->api_url, array( 'timeout' => 15, 'sslverify' => false, 'body' => $api_params ) ); |
||
| 296 | |||
| 297 | if ( ! is_wp_error( $request ) ) { |
||
| 298 | $request = json_decode( wp_remote_retrieve_body( $request ) ); |
||
| 299 | } |
||
| 300 | |||
| 301 | if ( $request ) { |
||
| 302 | if( isset( $request->sections ) ) { |
||
| 303 | $request->sections = maybe_unserialize( $request->sections ); |
||
| 304 | } |
||
| 305 | |||
| 306 | if( isset( $request->banners ) ) { |
||
| 307 | $request->banners = (array)maybe_unserialize( $request->banners ); |
||
| 308 | } |
||
| 309 | } else { |
||
| 310 | $request = false; |
||
| 311 | } |
||
| 312 | |||
| 313 | return $request; |
||
| 314 | } |
||
| 315 | |||
| 316 | public function show_changelog() { |
||
| 317 | |||
| 318 | |||
| 319 | if( empty( $_REQUEST['edd_sl_action'] ) || 'view_plugin_changelog' != $_REQUEST['edd_sl_action'] ) { |
||
| 320 | return; |
||
| 321 | } |
||
| 322 | |||
| 323 | if( empty( $_REQUEST['plugin'] ) ) { |
||
| 324 | return; |
||
| 325 | } |
||
| 326 | |||
| 327 | if( empty( $_REQUEST['slug'] ) ) { |
||
| 328 | return; |
||
| 329 | } |
||
| 330 | |||
| 331 | if( ! current_user_can( 'update_plugins' ) ) { |
||
| 332 | wp_die( __( 'You do not have permission to install plugin updates', 'gravityview' ), __( 'Error', 'gravityview' ), array( 'response' => 403 ) ); |
||
| 333 | } |
||
| 334 | |||
| 335 | $response = $this->api_request( 'plugin_latest_version', array( 'slug' => $_REQUEST['slug'] ) ); |
||
| 336 | |||
| 337 | if( $response && isset( $response->sections['changelog'] ) ) { |
||
| 338 | echo '<div style="background:#fff;padding:10px;">' . $response->sections['changelog'] . '</div>'; |
||
| 339 | } |
||
| 340 | |||
| 341 | |||
| 342 | exit; |
||
| 343 | } |
||
| 344 | |||
| 345 | } |