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 = '' ) { |
||
194 | |||
195 | // retrieve our license key from the DB |
||
196 | $license_key = empty( $license ) ? trim( $this->Addon->get_app_setting( 'license_key' ) ) : $license; |
||
197 | |||
198 | $settings = array( |
||
199 | 'version' => self::version, |
||
200 | 'license' => $license_key, |
||
201 | 'item_name' => self::name, |
||
202 | 'item_id' => self::item_id, |
||
203 | 'author' => self::author, |
||
204 | 'language' => get_locale(), |
||
205 | 'url' => home_url(), |
||
206 | ); |
||
207 | |||
208 | if( !empty( $action ) ) { |
||
209 | $settings['edd_action'] = esc_attr( $action ); |
||
210 | } |
||
211 | |||
212 | $settings = array_map( 'urlencode', $settings ); |
||
213 | |||
214 | return $settings; |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * Perform the call |
||
219 | * @return array|WP_Error |
||
220 | */ |
||
221 | private function _license_get_remote_response( $data, $license = '' ) { |
||
222 | |||
223 | $api_params = $this->_get_edd_settings( $data['edd_action'], $license ); |
||
224 | |||
225 | $url = add_query_arg( $api_params, self::url ); |
||
226 | |||
227 | $response = wp_remote_get( $url, array( |
||
228 | 'timeout' => 15, |
||
229 | 'sslverify' => false, |
||
230 | )); |
||
231 | |||
232 | if ( is_wp_error( $response ) ) { |
||
233 | return array(); |
||
234 | } |
||
235 | |||
236 | $license_data = json_decode( wp_remote_retrieve_body( $response ) ); |
||
237 | |||
238 | // Not JSON |
||
239 | if ( empty( $license_data ) ) { |
||
240 | |||
241 | delete_transient( self::status_transient_key ); |
||
242 | |||
243 | // Change status |
||
244 | return array(); |
||
245 | } |
||
246 | |||
247 | // Store the license key inside the data array |
||
248 | $license_data->license_key = $license; |
||
249 | |||
250 | return $license_data; |
||
251 | } |
||
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 ) { |
||
262 | |||
263 | if( empty( $license_data ) ) { |
||
264 | $message = ''; |
||
265 | } else { |
||
266 | |||
267 | if( ! empty( $license_data->error ) ) { |
||
268 | $class = 'error'; |
||
269 | $string_key = $license_data->error; |
||
270 | } else { |
||
271 | $class = $license_data->license; |
||
272 | $string_key = $license_data->license; |
||
273 | } |
||
274 | |||
275 | $message = sprintf( '<p><strong>%s: %s</strong></p>', $this->strings('status'), $this->strings( $string_key, $license_data ) ); |
||
276 | |||
277 | $message = $this->generate_license_box( $message, $class ); |
||
278 | } |
||
279 | |||
280 | return $message; |
||
281 | } |
||
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() ) { |
||
359 | |||
360 | /** |
||
361 | * Display possible upgrades for a license |
||
362 | * |
||
363 | * @since 1.17 |
||
364 | * |
||
365 | * @param array $upgrades Array of upgrade paths, returned from the GV website |
||
366 | * |
||
367 | * @return string HTML list of upgrades available for the current license |
||
368 | */ |
||
369 | function get_upgrade_html( $upgrades ) { |
||
412 | |||
413 | /** |
||
414 | * Perform the call to EDD based on the AJAX call or passed data |
||
415 | * |
||
416 | * @since 1.7.4 |
||
417 | * |
||
418 | * @param array $array { |
||
419 | * @type string $license The license key |
||
420 | * @type string $edd_action The EDD action to perform, like `check_license` |
||
421 | * @type string $field_id The ID of the field to check |
||
422 | * @type boolean $update Whether to update plugin settings. Prevent updating the data by setting an `update` key to false |
||
423 | * @type string $format If `object`, return the object of the license data. Else, return the JSON-encoded object |
||
424 | * } |
||
425 | * |
||
426 | * @return mixed|string|void |
||
427 | */ |
||
428 | public function license_call( $array = array() ) { |
||
487 | |||
488 | /** |
||
489 | * Update the license after fetching it |
||
490 | * @param object $license_data |
||
491 | * @return void |
||
492 | */ |
||
493 | private function license_call_update_settings( $license_data, $data ) { |
||
504 | |||
505 | /** |
||
506 | * URL to direct license renewal, or if license key is not set, then just the account page |
||
507 | * @since 1.13.1 |
||
508 | * @param object|null $license_data Object with license data |
||
509 | * @return string Renewal or account URL |
||
510 | */ |
||
511 | private function get_license_renewal_url( $license_data ) { |
||
516 | |||
517 | /** |
||
518 | * Override the text used in the GravityView EDD license Javascript |
||
519 | * |
||
520 | * @param array|null $status Status to get. If empty, get all strings. |
||
521 | * @param object|null $license_data Object with license data |
||
522 | * @return array Modified array of content |
||
523 | */ |
||
524 | public function strings( $status = NULL, $license_data = null ) { |
||
558 | |||
559 | } |