Complex classes like FrmAddon 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 FrmAddon, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class FrmAddon { |
||
| 8 | public $store_url = 'https://formidablepro.com'; |
||
| 9 | public $download_id; |
||
| 10 | public $plugin_file; |
||
| 11 | public $plugin_folder; |
||
| 12 | public $plugin_name; |
||
| 13 | public $plugin_slug; |
||
| 14 | public $option_name; |
||
| 15 | public $version; |
||
| 16 | public $author = 'Strategy11'; |
||
| 17 | private $license; |
||
| 18 | |||
| 19 | public function __construct() { |
||
| 20 | |||
| 21 | if ( empty( $this->plugin_slug ) ) { |
||
| 22 | $this->plugin_slug = preg_replace( '/[^a-zA-Z0-9_\s]/', '', str_replace( ' ', '_', strtolower( $this->plugin_name ) ) ); |
||
| 23 | } |
||
| 24 | if ( empty( $this->option_name ) ) { |
||
| 25 | $this->option_name = 'edd_' . $this->plugin_slug . '_license_'; |
||
| 26 | } |
||
| 27 | |||
| 28 | $this->plugin_folder = plugin_basename( $this->plugin_file ); |
||
| 29 | $this->license = $this->get_license(); |
||
| 30 | |||
| 31 | add_filter( 'frm_installed_addons', array( &$this, 'insert_installed_addon' ) ); |
||
| 32 | $this->edd_plugin_updater(); |
||
| 33 | } |
||
| 34 | |||
| 35 | public static function load_hooks() { |
||
| 39 | |||
| 40 | public function insert_installed_addon( $plugins ) { |
||
| 44 | |||
| 45 | public static function get_addon( $plugin_slug ) { |
||
| 46 | $plugins = apply_filters( 'frm_installed_addons', array() ); |
||
| 47 | $plugin = false; |
||
| 48 | if ( isset( $plugins[ $plugin_slug ] ) ) { |
||
| 49 | $plugin = $plugins[ $plugin_slug ]; |
||
| 50 | } |
||
| 51 | return $plugin; |
||
| 52 | } |
||
| 53 | |||
| 54 | public function edd_plugin_updater() { |
||
| 55 | |||
| 56 | $license = $this->license; |
||
| 57 | |||
| 58 | if ( empty( $license ) ) { |
||
| 59 | add_action( 'after_plugin_row_' . plugin_basename( $this->plugin_file ), array( $this, 'show_license_message' ), 10, 2 ); |
||
| 60 | } else { |
||
| 61 | |||
| 62 | // setup the updater |
||
| 63 | $api_data = array( |
||
| 64 | 'version' => $this->version, |
||
| 65 | 'license' => $license, |
||
| 66 | 'author' => $this->author, |
||
| 67 | ); |
||
| 68 | if ( is_numeric( $this->download_id ) ) { |
||
| 69 | $api_data['item_id'] = $this->download_id; |
||
| 70 | } |
||
| 71 | |||
| 72 | $edd = new FrmEDD_SL_Plugin_Updater( $this->store_url, $this->plugin_file, $api_data ); |
||
| 73 | if ( $this->plugin_folder == 'formidable/formidable.php' ) { |
||
| 74 | remove_filter( 'plugins_api', array( $edd, 'plugins_api_filter' ), 10, 3 ); |
||
| 75 | } |
||
| 76 | |||
| 77 | add_filter( 'site_transient_update_plugins', array( &$this, 'clear_expired_download' ) ); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | public function get_license() { |
||
| 84 | |||
| 85 | public function set_license( $license ) { |
||
| 88 | |||
| 89 | public function clear_license() { |
||
| 93 | |||
| 94 | public function set_active( $is_active ) { |
||
| 97 | |||
| 98 | public function show_license_message( $file, $plugin ) { |
||
| 99 | $wp_list_table = _get_list_table( 'WP_Plugins_List_Table' ); |
||
| 100 | echo '<tr class="plugin-update-tr active"><td colspan="' . esc_attr( $wp_list_table->get_column_count() ) . '" class="plugin-update colspanchange"><div class="update-message">'; |
||
| 101 | echo sprintf( __( 'Your %1$s license key is missing. Please add it on the %2$slicenses page%3$s.', 'formidable' ), $this->plugin_name, '<a href="' . esc_url( admin_url('admin.php?page=formidable-settings&t=licenses_settings' ) ) . '">', '</a>' ); |
||
|
|
|||
| 102 | $id = sanitize_title( $plugin['Name'] ); |
||
| 103 | echo '<script type="text/javascript">var d = document.getElementById("' . esc_attr( $id ) . '");if ( d !== null ){ d.className = d.className + " update"; }</script>'; |
||
| 104 | echo '</div></td></tr>'; |
||
| 105 | } |
||
| 106 | |||
| 107 | public function clear_expired_download( $transient ) { |
||
| 108 | if ( ! is_object( $transient ) ) { |
||
| 109 | return $transient; |
||
| 110 | } |
||
| 111 | |||
| 112 | if ( $this->is_current_version( $transient ) ) { |
||
| 113 | //make sure it doesn't show there is an update if plugin is up-to-date |
||
| 114 | if ( isset( $transient->response[ $this->plugin_folder ] ) ) { |
||
| 115 | unset( $transient->response[ $this->plugin_folder ] ); |
||
| 116 | } |
||
| 117 | } else if ( isset( $transient->response ) && isset( $transient->response[ $this->plugin_folder ] ) ) { |
||
| 118 | $cache_key = 'edd_plugin_' . md5( sanitize_key( $this->license . $this->version ) . '_get_version' ); |
||
| 119 | $version_info = get_transient( $cache_key ); |
||
| 120 | if ( $version_info !== false && version_compare( $version_info->new_version, $this->version, '>' ) ) { |
||
| 121 | $transient->response[ $this->plugin_folder ] = $version_info; |
||
| 122 | } else { |
||
| 123 | delete_transient( $cache_key ); |
||
| 124 | if ( ! $this->has_been_cleared() ) { |
||
| 125 | // if the transient has expired, clear the update and trigger it again |
||
| 126 | $this->cleared_plugins(); |
||
| 127 | $this->manually_queue_update(); |
||
| 128 | } |
||
| 129 | |||
| 130 | unset( $transient->response[ $this->plugin_folder ] ); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | return $transient; |
||
| 135 | } |
||
| 136 | |||
| 137 | private function is_current_version( $transient ) { |
||
| 138 | if ( empty( $transient->checked ) || ! isset( $transient->checked[ $this->plugin_folder ] ) ) { |
||
| 139 | return false; |
||
| 140 | } |
||
| 141 | |||
| 142 | $response = ! isset( $transient->response ) || empty( $transient->response ); |
||
| 143 | if ( $response ) { |
||
| 144 | return true; |
||
| 145 | } |
||
| 146 | |||
| 147 | return isset( $transient->response ) && isset( $transient->response[ $this->plugin_folder ] ) && $transient->checked[ $this->plugin_folder ] == $transient->response[ $this->plugin_folder ]->new_version; |
||
| 148 | } |
||
| 149 | |||
| 150 | private function has_been_cleared() { |
||
| 151 | $last_cleared = get_option( 'frm_last_cleared' ); |
||
| 152 | return ( $last_cleared && $last_cleared > date( 'Y-m-d H:i:s', strtotime('-5 minutes') ) ); |
||
| 153 | } |
||
| 154 | |||
| 155 | private function cleared_plugins() { |
||
| 158 | |||
| 159 | public static function activate() { |
||
| 160 | FrmAppHelper::permission_check('frm_change_settings'); |
||
| 161 | check_ajax_referer( 'frm_ajax', 'nonce' ); |
||
| 162 | |||
| 163 | if ( ! isset( $_POST['license'] ) || empty( $_POST['license'] ) ) { |
||
| 164 | wp_die( __( 'Oops! You forgot to enter your license number.', 'formidable' ) ); |
||
| 165 | } |
||
| 166 | |||
| 167 | $license = stripslashes( sanitize_text_field( $_POST['license'] ) ); |
||
|
1 ignored issue
–
show
|
|||
| 168 | $plugin_slug = sanitize_text_field( $_POST['plugin'] ); |
||
| 169 | $this_plugin = self::get_addon( $plugin_slug ); |
||
| 170 | $this_plugin->set_license( $license ); |
||
| 171 | |||
| 172 | $response = array( 'success' => false, 'message' => '' ); |
||
| 173 | try { |
||
| 174 | $license_data = $this_plugin->send_mothership_request( 'activate_license', $license ); |
||
| 175 | |||
| 176 | // $license_data->license will be either "valid" or "invalid" |
||
| 177 | $is_valid = 'invalid'; |
||
| 178 | if ( is_array( $license_data ) ) { |
||
| 179 | if ( $license_data['license'] == 'valid' ) { |
||
| 180 | $is_valid = $license_data['license']; |
||
| 181 | $response['message'] = __( 'Your license has been activated. Enjoy!', 'formidable' ); |
||
| 182 | $response['success'] = true; |
||
| 183 | } else if ( $license_data['license'] == 'invalid' ) { |
||
| 184 | $response['message'] = __( 'That license key is invalid', 'formidable' ); |
||
| 185 | } |
||
| 186 | } else if ( $license_data == 'expired' ) { |
||
| 187 | $response['message'] = __( 'That license is expired', 'formidable' ); |
||
| 188 | } else if ( $license_data == 'no_activations_left' ) { |
||
| 189 | $response['message'] = __( 'That license has been used on too many sites', 'formidable' ); |
||
| 190 | } else if ( $license_data == 'invalid_item_id' ) { |
||
| 191 | $response['message'] = __( 'Oops! That is the wrong license key for this plugin.', 'formidable' ); |
||
| 192 | } else if ( $license_data == 'missing' ) { |
||
| 193 | $response['message'] = __( 'That license key is invalid', 'formidable' ); |
||
| 194 | } else { |
||
| 195 | $response['message'] = FrmAppHelper::kses( $license_data, array( 'a' ) ); |
||
| 196 | } |
||
| 197 | |||
| 198 | $this_plugin->set_active( $is_valid ); |
||
| 199 | } catch ( Exception $e ) { |
||
| 200 | $response['message'] = $e->getMessage(); |
||
| 201 | } |
||
| 202 | |||
| 203 | echo json_encode( $response ); |
||
| 204 | wp_die(); |
||
| 205 | } |
||
| 206 | |||
| 207 | public static function deactivate() { |
||
| 208 | FrmAppHelper::permission_check('frm_change_settings'); |
||
| 209 | check_ajax_referer( 'frm_ajax', 'nonce' ); |
||
| 210 | |||
| 211 | $plugin_slug = sanitize_text_field( $_POST['plugin'] ); |
||
| 212 | $this_plugin = self::get_addon( $plugin_slug ); |
||
| 213 | $license = $this_plugin->get_license(); |
||
| 214 | |||
| 215 | $response = array( 'success' => false, 'message' => '' ); |
||
| 216 | try { |
||
| 217 | // $license_data->license will be either "deactivated" or "failed" |
||
| 218 | $license_data = $this_plugin->send_mothership_request( 'deactivate_license', $license ); |
||
| 219 | if ( is_array( $license_data ) && $license_data['license'] == 'deactivated' ) { |
||
| 220 | $response['success'] = true; |
||
| 221 | $response['message'] = __( 'That license was removed successfully', 'formidable' ); |
||
| 222 | } else { |
||
| 223 | $response['message'] = __( 'There was an error deactivating your license.', 'formidable' ); |
||
| 224 | } |
||
| 225 | } catch ( Exception $e ) { |
||
| 226 | $response['message'] = $e->getMessage(); |
||
| 227 | } |
||
| 228 | |||
| 229 | $this_plugin->clear_license(); |
||
| 230 | |||
| 231 | echo json_encode( $response ); |
||
| 232 | wp_die(); |
||
| 233 | } |
||
| 234 | |||
| 235 | public function send_mothership_request( $action, $license ) { |
||
| 236 | $api_params = array( |
||
| 237 | 'edd_action' => $action, |
||
| 238 | 'license' => $license, |
||
| 239 | 'item_name' => urlencode( $this->plugin_name ), |
||
| 240 | 'url' => home_url(), |
||
| 241 | ); |
||
| 242 | if ( is_numeric( $this->download_id ) ) { |
||
| 243 | $api_params['item_id'] = absint( $this->download_id ); |
||
| 244 | } |
||
| 245 | |||
| 246 | $arg_array = array( |
||
| 247 | 'body' => $api_params, |
||
| 248 | 'timeout' => 25, |
||
| 249 | 'sslverify' => false, |
||
| 250 | 'user-agent' => $this->plugin_slug . '/' . $this->version . '; ' . get_bloginfo( 'url' ), |
||
| 251 | ); |
||
| 252 | |||
| 253 | $resp = wp_remote_post( $this->store_url, $arg_array ); |
||
| 254 | $body = wp_remote_retrieve_body( $resp ); |
||
| 255 | |||
| 256 | $message = __( 'Your License Key was invalid', 'formidable' ); |
||
| 257 | if ( is_wp_error( $resp ) ) { |
||
| 258 | $message = sprintf( __( 'You had an error communicating with Formidable Pro\'s API. %1$sClick here%2$s for more information.', 'formidable' ), '<a href="http://formidablepro.com/knowledgebase/why-cant-i-activate-formidable-pro/" target="_blank">', '</a>'); |
||
| 259 | $message .= ' ' . $resp->get_error_message(); |
||
| 260 | } else if ( $body == 'error' || is_wp_error( $body ) ) { |
||
| 261 | $message = __( 'You had an HTTP error connecting to Formidable Pro\'s API', 'formidable' ); |
||
| 262 | } else { |
||
| 263 | $json_res = json_decode( $body, true ); |
||
| 264 | if ( null !== $json_res ) { |
||
| 265 | if ( is_array( $json_res ) && isset( $json_res['error'] ) ) { |
||
| 266 | $message = $json_res['error']; |
||
| 267 | } else { |
||
| 268 | $message = $json_res; |
||
| 269 | } |
||
| 270 | } else if ( isset( $resp['response'] ) && isset( $resp['response']['code'] ) ) { |
||
| 271 | $message = sprintf( __( 'There was a %1$s error: %2$s', 'formidable' ), $resp['response']['code'], $resp['response']['message'] . ' ' . $resp['body'] ); |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | return $message; |
||
| 276 | } |
||
| 277 | |||
| 278 | public function manually_queue_update() { |
||
| 281 | } |
||
| 282 |