Complex classes like GV_License_Handler 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 GV_License_Handler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 3 | class GV_License_Handler { |
||
|
|
|||
| 4 | |||
| 5 | /** |
||
| 6 | * @var GravityView_Settings |
||
| 7 | */ |
||
| 8 | private $Addon; |
||
| 9 | |||
| 10 | const name = 'GravityView'; |
||
| 11 | |||
| 12 | const author = 'Katz Web Services, Inc.'; |
||
| 13 | |||
| 14 | const url = 'https://gravityview.co'; |
||
| 15 | |||
| 16 | const version = GravityView_Plugin::version; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Post ID on gravityview.co |
||
| 20 | * @since 1.15 |
||
| 21 | */ |
||
| 22 | const item_id = 17; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Name of the transient used to store license status for GV |
||
| 26 | * @since 1.17 |
||
| 27 | */ |
||
| 28 | const status_transient_key = 'gravityview_edd-activate_valid'; |
||
| 29 | |||
| 30 | private $EDD_SL_Plugin_Updater; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var GV_License_Handler |
||
| 34 | */ |
||
| 35 | public static $instance; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @param GravityView_Settings $GFAddOn |
||
| 39 | * |
||
| 40 | * @return GV_License_Handler |
||
| 41 | */ |
||
| 42 | public static function get_instance( GravityView_Settings $GFAddOn ) { |
||
| 48 | |||
| 49 | private function __construct( GravityView_Settings $GFAddOn ) { |
||
| 57 | |||
| 58 | private function add_hooks() { |
||
| 59 | add_action( 'wp_ajax_gravityview_license', array( $this, 'license_call' ) ); |
||
| 60 | add_action( 'admin_init', array( $this, 'refresh_license_status' ) ); |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * When the status transient expires (or is deleted on activation), re-check the status |
||
| 65 | * |
||
| 66 | * @since 1.17 |
||
| 67 | * |
||
| 68 | * @return void |
||
| 69 | */ |
||
| 70 | public function refresh_license_status() { |
||
| 71 | |||
| 72 | // Only perform on GravityView pages |
||
| 73 | if( ! gravityview_is_admin_page() ) { |
||
| 74 | return; |
||
| 75 | } |
||
| 76 | |||
| 77 | // The transient is fresh; don't fetch. |
||
| 78 | if( $status = get_transient( self::status_transient_key ) ) { |
||
| 79 | return; |
||
| 80 | } |
||
| 81 | |||
| 82 | $data = array( |
||
| 83 | 'edd_action' => 'check_license', |
||
| 84 | 'license' => trim( $this->Addon->get_app_setting( 'license_key' ) ), |
||
| 85 | 'update' => true, |
||
| 86 | 'format' => 'object', |
||
| 87 | 'field_id' => 'refresh_license_status', // Required to set the `status_transient_key` transient |
||
| 88 | ); |
||
| 89 | |||
| 90 | $license_call = GravityView_Settings::get_instance()->get_license_handler()->license_call( $data ); |
||
| 91 | |||
| 92 | do_action( 'gravityview_log_debug', __METHOD__ . ': Refreshed the license.', $license_call ); |
||
| 93 | } |
||
| 94 | |||
| 95 | function settings_edd_license_activation( $field, $echo ) { |
||
| 96 | |||
| 97 | $script_debug = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; |
||
| 98 | |||
| 99 | wp_enqueue_script( 'gv-admin-edd-license', GRAVITYVIEW_URL . 'assets/js/admin-edd-license' . $script_debug . '.js', array( 'jquery' ) ); |
||
| 100 | |||
| 101 | $status = trim( $this->Addon->get_app_setting( 'license_key_status' ) ); |
||
| 102 | $key = trim( $this->Addon->get_app_setting( 'license_key' ) ); |
||
| 103 | |||
| 104 | if( !empty( $key ) ) { |
||
| 105 | $response = $this->Addon->get_app_setting( 'license_key_response' ); |
||
| 106 | $response = is_array( $response ) ? (object) $response : json_decode( $response ); |
||
| 107 | } else { |
||
| 108 | $response = array(); |
||
| 109 | } |
||
| 110 | |||
| 111 | wp_localize_script( 'gv-admin-edd-license', 'GVGlobals', array( |
||
| 112 | 'license_box' => $this->get_license_message( $response ) |
||
| 113 | )); |
||
| 114 | |||
| 115 | |||
| 116 | $fields = array( |
||
| 117 | array( |
||
| 118 | 'name' => 'edd-activate', |
||
| 119 | 'value' => __('Activate License', 'gravityview'), |
||
| 120 | 'data-pending_text' => __('Verifying license…', 'gravityview'), |
||
| 121 | 'data-edd_action' => 'activate_license', |
||
| 122 | 'class' => 'button-primary', |
||
| 123 | ), |
||
| 124 | array( |
||
| 125 | 'name' => 'edd-deactivate', |
||
| 126 | 'value' => __('Deactivate License', 'gravityview'), |
||
| 127 | 'data-pending_text' => __('Deactivating license…', 'gravityview'), |
||
| 128 | 'data-edd_action' => 'deactivate_license', |
||
| 129 | 'class' => ( empty( $status ) ? 'button-primary hide' : 'button-primary' ), |
||
| 130 | ), |
||
| 131 | array( |
||
| 132 | 'name' => 'edd-check', |
||
| 133 | 'value' => __('Check License', 'gravityview'), |
||
| 134 | 'data-pending_text' => __('Verifying license…', 'gravityview'), |
||
| 135 | 'title' => 'Check the license before saving it', |
||
| 136 | 'data-edd_action' => 'check_license', |
||
| 137 | 'class' => 'button-secondary', |
||
| 138 | ), |
||
| 139 | ); |
||
| 140 | |||
| 141 | |||
| 142 | $class = 'button gv-edd-action'; |
||
| 143 | |||
| 144 | $class .= ( !empty( $key ) && $status !== 'valid' ) ? '' : ' hide'; |
||
| 145 | |||
| 146 | $disabled_attribute = GVCommon::has_cap( 'gravityview_edit_settings' ) ? false : 'disabled'; |
||
| 147 | |||
| 148 | $submit = '<div class="gv-edd-button-wrapper">'; |
||
| 149 | foreach ( $fields as $field ) { |
||
| 150 | $field['type'] = 'button'; |
||
| 151 | $field['class'] = isset( $field['class'] ) ? $field['class'] . ' '. $class : $class; |
||
| 152 | $field['style'] = 'margin-left: 10px;'; |
||
| 153 | if( $disabled_attribute ) { |
||
| 154 | $field['disabled'] = $disabled_attribute; |
||
| 155 | } |
||
| 156 | $submit .= $this->Addon->settings_submit( $field, $echo ); |
||
| 157 | } |
||
| 158 | $submit .= '</div>'; |
||
| 159 | |||
| 160 | return $submit; |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Include the EDD plugin updater class, if not exists |
||
| 165 | * @since 1.7.4 |
||
| 166 | * @return void |
||
| 167 | */ |
||
| 168 | private function setup_edd() { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Generate the array of settings passed to the EDD license call |
||
| 185 | * |
||
| 186 | * @since 1.7.4 |
||
| 187 | * |
||
| 188 | * @param string $action The action to send to edd, such as `check_license` |
||
| 189 | * @param string $license The license key to have passed to EDD |
||
| 190 | * |
||
| 191 | * @return array |
||
| 192 | */ |
||
| 193 | function _get_edd_settings( $action = '', $license = '' ) { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Perform the call |
||
| 219 | * @return array|WP_Error |
||
| 220 | */ |
||
| 221 | private function _license_get_remote_response( $data, $license = '' ) { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Generate the status message displayed in the license field |
||
| 255 | * |
||
| 256 | * @since 1.7.4 |
||
| 257 | * @param $license_data |
||
| 258 | * |
||
| 259 | * @return string |
||
| 260 | */ |
||
| 261 | function get_license_message( $license_data ) { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Generate the status message box HTML based on the current status |
||
| 285 | * |
||
| 286 | * @since 1.7.4 |
||
| 287 | * @param $message |
||
| 288 | * @param string $class |
||
| 289 | * |
||
| 290 | * @return string |
||
| 291 | */ |
||
| 292 | private function generate_license_box( $message, $class = '' ) { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Allow pure HTML in settings fields |
||
| 303 | * |
||
| 304 | * @since 1.17 |
||
| 305 | * |
||
| 306 | * @param array $response License response |
||
| 307 | * |
||
| 308 | * @return string `html` key of the $field |
||
| 309 | */ |
||
| 310 | public function license_details( $response = array() ) { |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Display possible upgrades for a license |
||
| 360 | * |
||
| 361 | * @since 1.17 |
||
| 362 | * |
||
| 363 | * @param array $upgrades Array of upgrade paths, returned from the GV website |
||
| 364 | * |
||
| 365 | * @return string HTML list of upgrades available for the current license |
||
| 366 | */ |
||
| 367 | function get_upgrade_html( $upgrades ) { |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Perform the call to EDD based on the AJAX call or passed data |
||
| 413 | * |
||
| 414 | * @since 1.7.4 |
||
| 415 | * |
||
| 416 | * @param array $array { |
||
| 417 | * @type string $license The license key |
||
| 418 | * @type string $edd_action The EDD action to perform, like `check_license` |
||
| 419 | * @type string $field_id The ID of the field to check |
||
| 420 | * @type boolean $update Whether to update plugin settings. Prevent updating the data by setting an `update` key to false |
||
| 421 | * @type string $format If `object`, return the object of the license data. Else, return the JSON-encoded object |
||
| 422 | * } |
||
| 423 | * |
||
| 424 | * @return mixed|string|void |
||
| 425 | */ |
||
| 426 | public function license_call( $array = array() ) { |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Update the license after fetching it |
||
| 488 | * @param object $license_data |
||
| 489 | * @return void |
||
| 490 | */ |
||
| 491 | private function license_call_update_settings( $license_data, $data ) { |
||
| 502 | |||
| 503 | /** |
||
| 504 | * URL to direct license renewal, or if license key is not set, then just the account page |
||
| 505 | * @since 1.13.1 |
||
| 506 | * @param object|null $license_data Object with license data |
||
| 507 | * @return string Renewal or account URL |
||
| 508 | */ |
||
| 509 | private function get_license_renewal_url( $license_data ) { |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Override the text used in the GravityView EDD license Javascript |
||
| 517 | * |
||
| 518 | * @param array|null $status Status to get. If empty, get all strings. |
||
| 519 | * @param object|null $license_data Object with license data |
||
| 520 | * @return array Modified array of content |
||
| 521 | */ |
||
| 522 | public function strings( $status = NULL, $license_data = null ) { |
||
| 556 | |||
| 557 | } |