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 ) { |
||
| 43 | if( empty( self::$instance ) ) { |
||
| 44 | self::$instance = new self( $GFAddOn ); |
||
| 45 | } |
||
| 46 | return self::$instance; |
||
| 47 | } |
||
| 48 | |||
| 49 | private function __construct( GravityView_Settings $GFAddOn ) { |
||
| 50 | |||
| 51 | $this->Addon = $GFAddOn; |
||
| 52 | |||
| 53 | $this->setup_edd(); |
||
| 54 | |||
| 55 | $this->add_hooks(); |
||
| 56 | } |
||
| 57 | |||
| 58 | private function add_hooks() { |
||
| 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() { |
||
| 93 | |||
| 94 | function settings_edd_license_activation( $field, $echo ) { |
||
| 95 | |||
| 96 | $script_debug = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; |
||
| 97 | |||
| 98 | wp_enqueue_script( 'gv-admin-edd-license', GRAVITYVIEW_URL . 'assets/js/admin-edd-license' . $script_debug . '.js', array( 'jquery' ) ); |
||
| 99 | |||
| 100 | $status = trim( $this->Addon->get_app_setting( 'license_key_status' ) ); |
||
| 101 | $key = trim( $this->Addon->get_app_setting( 'license_key' ) ); |
||
| 102 | |||
| 103 | if( !empty( $key ) ) { |
||
| 104 | $response = $this->Addon->get_app_setting( 'license_key_response' ); |
||
| 105 | $response = is_array( $response ) ? (object) $response : json_decode( $response ); |
||
| 106 | } else { |
||
| 107 | $response = array(); |
||
| 108 | } |
||
| 109 | |||
| 110 | wp_localize_script( 'gv-admin-edd-license', 'GVGlobals', array( |
||
| 111 | 'license_box' => $this->get_license_message( $response ) |
||
| 112 | )); |
||
| 113 | |||
| 114 | |||
| 115 | $fields = array( |
||
| 116 | array( |
||
| 117 | 'name' => 'edd-activate', |
||
| 118 | 'value' => __('Activate License', 'gravityview'), |
||
| 119 | 'data-pending_text' => __('Verifying license…', 'gravityview'), |
||
| 120 | 'data-edd_action' => 'activate_license', |
||
| 121 | 'class' => 'button-primary', |
||
| 122 | ), |
||
| 123 | array( |
||
| 124 | 'name' => 'edd-deactivate', |
||
| 125 | 'value' => __('Deactivate License', 'gravityview'), |
||
| 126 | 'data-pending_text' => __('Deactivating license…', 'gravityview'), |
||
| 127 | 'data-edd_action' => 'deactivate_license', |
||
| 128 | 'class' => ( empty( $status ) ? 'button-primary hide' : 'button-primary' ), |
||
| 129 | ), |
||
| 130 | array( |
||
| 131 | 'name' => 'edd-check', |
||
| 132 | 'value' => __('Check License', 'gravityview'), |
||
| 133 | 'data-pending_text' => __('Verifying license…', 'gravityview'), |
||
| 134 | 'title' => 'Check the license before saving it', |
||
| 135 | 'data-edd_action' => 'check_license', |
||
| 136 | 'class' => 'button-secondary', |
||
| 137 | ), |
||
| 138 | ); |
||
| 139 | |||
| 140 | |||
| 141 | $class = 'button gv-edd-action'; |
||
| 142 | |||
| 143 | $class .= ( !empty( $key ) && $status !== 'valid' ) ? '' : ' hide'; |
||
| 144 | |||
| 145 | $disabled_attribute = GVCommon::has_cap( 'gravityview_edit_settings' ) ? false : 'disabled'; |
||
| 146 | |||
| 147 | $submit = '<div class="gv-edd-button-wrapper">'; |
||
| 148 | foreach ( $fields as $field ) { |
||
| 149 | $field['type'] = 'button'; |
||
| 150 | $field['class'] = isset( $field['class'] ) ? $field['class'] . ' '. $class : $class; |
||
| 151 | $field['style'] = 'margin-left: 10px;'; |
||
| 152 | if( $disabled_attribute ) { |
||
| 153 | $field['disabled'] = $disabled_attribute; |
||
| 154 | } |
||
| 155 | $submit .= $this->Addon->settings_submit( $field, $echo ); |
||
| 156 | } |
||
| 157 | $submit .= '</div>'; |
||
| 158 | |||
| 159 | return $submit; |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Include the EDD plugin updater class, if not exists |
||
| 164 | * @since 1.7.4 |
||
| 165 | * @return void |
||
| 166 | */ |
||
| 167 | private function setup_edd() { |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Generate the array of settings passed to the EDD license call |
||
| 184 | * |
||
| 185 | * @since 1.7.4 |
||
| 186 | * |
||
| 187 | * @param string $action The action to send to edd, such as `check_license` |
||
| 188 | * @param string $license The license key to have passed to EDD |
||
| 189 | * |
||
| 190 | * @return array |
||
| 191 | */ |
||
| 192 | function _get_edd_settings( $action = '', $license = '' ) { |
||
| 193 | |||
| 194 | // retrieve our license key from the DB |
||
| 195 | $license_key = empty( $license ) ? trim( $this->Addon->get_app_setting( 'license_key' ) ) : $license; |
||
| 196 | |||
| 197 | $settings = array( |
||
| 198 | 'version' => self::version, |
||
| 199 | 'license' => $license_key, |
||
| 200 | 'item_name' => self::name, |
||
| 201 | 'item_id' => self::item_id, |
||
| 202 | 'author' => self::author, |
||
| 203 | 'language' => get_locale(), |
||
| 204 | 'url' => home_url(), |
||
| 205 | ); |
||
| 206 | |||
| 207 | if( !empty( $action ) ) { |
||
| 208 | $settings['edd_action'] = esc_attr( $action ); |
||
| 209 | } |
||
| 210 | |||
| 211 | $settings = array_map( 'urlencode', $settings ); |
||
| 212 | |||
| 213 | return $settings; |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Perform the call |
||
| 218 | * @return array|WP_Error |
||
| 219 | */ |
||
| 220 | private function _license_get_remote_response( $data, $license = '' ) { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Generate the status message displayed in the license field |
||
| 254 | * |
||
| 255 | * @since 1.7.4 |
||
| 256 | * @param $license_data |
||
| 257 | * |
||
| 258 | * @return string |
||
| 259 | */ |
||
| 260 | function get_license_message( $license_data ) { |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Generate the status message box HTML based on the current status |
||
| 284 | * |
||
| 285 | * @since 1.7.4 |
||
| 286 | * @param $message |
||
| 287 | * @param string $class |
||
| 288 | * |
||
| 289 | * @return string |
||
| 290 | */ |
||
| 291 | private function generate_license_box( $message, $class = '' ) { |
||
| 292 | |||
| 293 | $template = '<div id="gv-edd-status" aria-live="polite" aria-busy="false" class="gv-edd-message inline %s">%s</div>'; |
||
| 294 | |||
| 295 | $output = sprintf( $template, esc_attr( $class ), $message ); |
||
| 296 | |||
| 297 | return $output; |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Allow pure HTML in settings fields |
||
| 302 | * |
||
| 303 | * @since 1.17 |
||
| 304 | * |
||
| 305 | * @param array $response License response |
||
| 306 | * |
||
| 307 | * @return string `html` key of the $field |
||
| 308 | */ |
||
| 309 | public function license_details( $response = array() ) { |
||
| 310 | |||
| 311 | $response = (array) $response; |
||
| 312 | |||
| 313 | $return = ''; |
||
| 314 | $return .= '<span class="gv-license-details" aria-live="polite" aria-busy="false">'; |
||
| 315 | $return .= '<h3>' . esc_html__( 'License Details:', 'gravityview' ) . '</h3>'; |
||
| 316 | |||
| 317 | if( in_array( rgar( $response, 'license' ), array( 'invalid', 'deactivated' ) ) ) { |
||
| 318 | $return .= $this->strings( $response['license'], $response ); |
||
| 319 | } elseif( ! empty( $response['license_name'] ) ) { |
||
| 320 | |||
| 321 | $response_keys = array( |
||
| 322 | 'license_name' => '', |
||
| 323 | 'license_limit' => '', |
||
| 324 | 'customer_name' => '', |
||
| 325 | 'customer_email' => '', |
||
| 326 | 'site_count' => '', |
||
| 327 | 'expires' => '', |
||
| 328 | 'upgrades' => '' |
||
| 329 | ); |
||
| 330 | |||
| 331 | // Make sure all the keys are set |
||
| 332 | $response = wp_parse_args( $response, $response_keys ); |
||
| 333 | |||
| 334 | $login_link = sprintf( '<a href="%s" class="howto" rel="external">%s</a>', esc_url( sprintf( 'https://gravityview.co/wp-login.php?username=%s', $response['customer_email'] ) ), esc_html__( 'Access your GravityView account', 'gravityview' ) ); |
||
| 335 | $local_text = ( ! empty( $response['is_local'] ) ? '<span class="howto">' . __( 'This development site does not count toward license activation limits', 'gravityview' ) . '</span>' : '' ); |
||
| 336 | $details = array( |
||
| 337 | 'license' => sprintf( esc_html__( 'License level: %s', 'gravityview' ), esc_html( $response['license_name'] ), esc_html( $response['license_limit'] ) ), |
||
| 338 | 'licensed_to' => sprintf( esc_html_x( 'Licensed to: %1$s (%2$s)', '1: Customer name; 2: Customer email', 'gravityview' ), esc_html__( $response['customer_name'] ), esc_html__( $response['customer_email'] ) ) . $login_link, |
||
| 339 | 'activations' => sprintf( esc_html__( 'Activations: %d of %s sites', 'gravityview' ), intval( $response['site_count'] ), esc_html( $response['license_limit'] ) ) . $local_text, |
||
| 340 | 'expires' => sprintf( esc_html__( 'Renew on: %s', 'gravityview' ), date_i18n( get_option( 'date_format' ), strtotime( $response['expires'] ) - DAY_IN_SECONDS ) ), |
||
| 341 | 'upgrade' => $this->get_upgrade_html( $response['upgrades'] ), |
||
| 342 | ); |
||
| 343 | |||
| 344 | if ( ! empty( $response['error'] ) && 'expired' === $response['error'] ) { |
||
| 345 | unset( $details['upgrade'] ); |
||
| 346 | $details['expires'] = '<div class="error inline"><p>' . $this->strings( 'expired', $response ) . '</p></div>'; |
||
| 347 | } |
||
| 348 | |||
| 349 | $return .= '<ul><li>' . implode( '</li><li>', array_filter( $details ) ) . '</li></ul>'; |
||
| 350 | } |
||
| 351 | |||
| 352 | $return .= '</span>'; |
||
| 353 | |||
| 354 | return $return; |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Display possible upgrades for a license |
||
| 359 | * |
||
| 360 | * @since 1.17 |
||
| 361 | * |
||
| 362 | * @param array $upgrades Array of upgrade paths, returned from the GV website |
||
| 363 | * |
||
| 364 | * @return string HTML list of upgrades available for the current license |
||
| 365 | */ |
||
| 366 | function get_upgrade_html( $upgrades ) { |
||
| 367 | |||
| 368 | $output = ''; |
||
| 369 | |||
| 370 | if( ! empty( $upgrades ) ) { |
||
| 371 | |||
| 372 | $locale_parts = explode( '_', get_locale() ); |
||
| 373 | |||
| 374 | $is_english = ( 'en' === $locale_parts[0] ); |
||
| 375 | |||
| 376 | $output .= '<h4>' . esc_html__( 'Upgrades available:', 'gravityview' ) . '</h4>'; |
||
| 377 | |||
| 378 | $output .= '<ul class="ul-disc">'; |
||
| 379 | |||
| 380 | foreach ( $upgrades as $upgrade_id => $upgrade ) { |
||
| 381 | |||
| 382 | $upgrade = (object) $upgrade; |
||
| 383 | |||
| 384 | $anchor_text = sprintf( esc_html_x( 'Upgrade to %1$s for %2$s', '1: GravityView upgrade name, 2: Cost of upgrade', 'gravityview' ), esc_attr( $upgrade->name ), esc_attr( $upgrade->price ) ); |
||
| 385 | |||
| 386 | if( $is_english && isset( $upgrade->description ) ) { |
||
| 387 | $message = esc_html( $upgrade->description ); |
||
| 388 | } else { |
||
| 389 | switch( $upgrade->price_id ) { |
||
| 390 | // Interstellar |
||
| 391 | case 1: |
||
| 392 | default: |
||
| 393 | $message = esc_html__( 'Get access to Extensions', 'gravityview' ); |
||
| 394 | break; |
||
| 395 | // Galactic |
||
| 396 | case 2: |
||
| 397 | $message = esc_html__( 'Get access to Entry Importer and other Premium plugins', 'gravityview' ); |
||
| 398 | break; |
||
| 399 | } |
||
| 400 | } |
||
| 401 | |||
| 402 | $output .= sprintf( '<li><a href="%s">%s</a><span class="howto">%s</span></li>', esc_url( add_query_arg( array( 'utm_source' => 'settings', 'utm_medium' => 'admin', 'utm_content' => 'license-details', 'utm_campaign' => 'Upgrades' ), $upgrade->url ) ), $anchor_text, $message ); |
||
| 403 | } |
||
| 404 | $output .= '</ul>'; |
||
| 405 | } |
||
| 406 | |||
| 407 | return $output; |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Perform the call to EDD based on the AJAX call or passed data |
||
| 412 | * |
||
| 413 | * @since 1.7.4 |
||
| 414 | * |
||
| 415 | * @param array $array { |
||
| 416 | * @type string $license The license key |
||
| 417 | * @type string $edd_action The EDD action to perform, like `check_license` |
||
| 418 | * @type string $field_id The ID of the field to check |
||
| 419 | * @type boolean $update Whether to update plugin settings. Prevent updating the data by setting an `update` key to false |
||
| 420 | * @type string $format If `object`, return the object of the license data. Else, return the JSON-encoded object |
||
| 421 | * } |
||
| 422 | * |
||
| 423 | * @return mixed|string|void |
||
| 424 | */ |
||
| 425 | public function license_call( $array = array() ) { |
||
| 426 | |||
| 427 | $is_ajax = ( defined('DOING_AJAX') && DOING_AJAX ); |
||
| 428 | $data = empty( $array ) ? $_POST['data'] : $array; |
||
| 429 | $has_cap = GVCommon::has_cap( 'gravityview_edit_settings' ); |
||
| 430 | |||
| 431 | if ( $is_ajax && empty( $data['license'] ) ) { |
||
| 432 | die( - 1 ); |
||
| 433 | } |
||
| 434 | |||
| 435 | // If the user isn't allowed to edit settings, show an error message |
||
| 436 | if( ! $has_cap ) { |
||
| 437 | $license_data = new stdClass(); |
||
| 438 | $license_data->error = 'capability'; |
||
| 439 | $license_data->message = $this->get_license_message( $license_data ); |
||
| 440 | $json = json_encode( $license_data ); |
||
| 441 | } else { |
||
| 442 | |||
| 443 | $license = esc_attr( rgget( 'license', $data ) ); |
||
| 444 | $license_data = $this->_license_get_remote_response( $data, $license ); |
||
| 445 | |||
| 446 | // Empty is returned when there's an error. |
||
| 447 | if ( empty( $license_data ) ) { |
||
| 448 | if ( $is_ajax ) { |
||
| 449 | exit( json_encode( array() ) ); |
||
| 450 | } else { // Non-ajax call |
||
| 451 | return json_encode( array() ); |
||
| 452 | } |
||
| 453 | } |
||
| 454 | |||
| 455 | $license_data->details = $this->license_details( $license_data ); |
||
| 456 | $license_data->message = $this->get_license_message( $license_data ); |
||
| 457 | |||
| 458 | $json = json_encode( $license_data ); |
||
| 459 | |||
| 460 | $update_license = ( ! isset( $data['update'] ) || ! empty( $data['update'] ) ); |
||
| 461 | |||
| 462 | $is_check_action_button = ( 'check_license' === $data['edd_action'] && defined( 'DOING_AJAX' ) && DOING_AJAX ); |
||
| 463 | |||
| 464 | // Failed is the response from trying to de-activate a license and it didn't work. |
||
| 465 | // This likely happened because people entered in a different key and clicked "Deactivate", |
||
| 466 | // meaning to deactivate the original key. We don't want to save this response, since it is |
||
| 467 | // most likely a mistake. |
||
| 468 | if ( $license_data->license !== 'failed' && ! $is_check_action_button && $update_license ) { |
||
| 469 | |||
| 470 | if ( ! empty( $data['field_id'] ) ) { |
||
| 471 | set_transient( self::status_transient_key, $license_data, DAY_IN_SECONDS ); |
||
| 472 | } |
||
| 473 | |||
| 474 | $this->license_call_update_settings( $license_data, $data ); |
||
| 475 | } |
||
| 476 | } // End $has_cap |
||
| 477 | |||
| 478 | if ( $is_ajax ) { |
||
| 479 | exit( $json ); |
||
| 480 | } else { // Non-ajax call |
||
| 481 | return ( rgget('format', $data ) === 'object' ) ? $license_data : $json; |
||
| 482 | } |
||
| 483 | } |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Update the license after fetching it |
||
| 487 | * @param object $license_data |
||
| 488 | * @return void |
||
| 489 | */ |
||
| 490 | private function license_call_update_settings( $license_data, $data ) { |
||
| 491 | |||
| 492 | // Update option with passed data license |
||
| 493 | $settings = $this->Addon->get_app_settings(); |
||
| 494 | |||
| 495 | $settings['license_key'] = $license_data->license_key = trim( $data['license'] ); |
||
| 496 | $settings['license_key_status'] = $license_data->license; |
||
| 497 | $settings['license_key_response'] = (array)$license_data; |
||
| 498 | |||
| 499 | $this->Addon->update_app_settings( $settings ); |
||
| 500 | } |
||
| 501 | |||
| 502 | /** |
||
| 503 | * URL to direct license renewal, or if license key is not set, then just the account page |
||
| 504 | * @since 1.13.1 |
||
| 505 | * @param object|null $license_data Object with license data |
||
| 506 | * @return string Renewal or account URL |
||
| 507 | */ |
||
| 508 | private function get_license_renewal_url( $license_data ) { |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Override the text used in the GravityView EDD license Javascript |
||
| 516 | * |
||
| 517 | * @param array|null $status Status to get. If empty, get all strings. |
||
| 518 | * @param object|null $license_data Object with license data |
||
| 519 | * @return array Modified array of content |
||
| 520 | */ |
||
| 521 | public function strings( $status = NULL, $license_data = null ) { |
||
| 555 | |||
| 556 | } |