Complex classes like GravityView_Settings 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 GravityView_Settings, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class GravityView_Settings extends GFAddOn { |
||
12 | |||
13 | /** |
||
14 | * @var string Version number of the Add-On |
||
15 | */ |
||
16 | protected $_version = GravityView_Plugin::version; |
||
17 | /** |
||
18 | * @var string Gravity Forms minimum version requirement |
||
19 | */ |
||
20 | protected $_min_gravityforms_version = GV_MIN_GF_VERSION; |
||
21 | |||
22 | /** |
||
23 | * @var string Title of the plugin to be used on the settings page, form settings and plugins page. Example: 'Gravity Forms MailChimp Add-On' |
||
24 | */ |
||
25 | protected $_title = 'GravityView'; |
||
26 | |||
27 | /** |
||
28 | * @var string Short version of the plugin title to be used on menus and other places where a less verbose string is useful. Example: 'MailChimp' |
||
29 | */ |
||
30 | protected $_short_title = 'GravityView'; |
||
31 | |||
32 | /** |
||
33 | * @var string URL-friendly identifier used for form settings, add-on settings, text domain localization... |
||
34 | */ |
||
35 | protected $_slug = 'gravityview'; |
||
36 | |||
37 | /** |
||
38 | * @var string|array A string or an array of capabilities or roles that can uninstall the plugin |
||
39 | */ |
||
40 | protected $_capabilities_uninstall = 'gravityview_uninstall'; |
||
41 | |||
42 | /** |
||
43 | * @var string|array A string or an array of capabilities or roles that have access to the settings page |
||
44 | */ |
||
45 | protected $_capabilities_app_settings = 'gravityview_view_settings'; |
||
46 | |||
47 | /** |
||
48 | * @var string|array A string or an array of capabilities or roles that have access to the settings page |
||
49 | */ |
||
50 | protected $_capabilities_app_menu = 'gravityview_view_settings'; |
||
51 | |||
52 | /** |
||
53 | * @var string The hook suffix for the app menu |
||
54 | */ |
||
55 | public $app_hook_suffix = 'gravityview'; |
||
56 | |||
57 | /** |
||
58 | * @var GV_License_Handler Process license validation |
||
59 | */ |
||
60 | private $License_Handler; |
||
61 | |||
62 | /** |
||
63 | * @var GravityView_Settings |
||
64 | */ |
||
65 | private static $instance; |
||
66 | |||
67 | /** |
||
68 | * We're not able to set the __construct() method to private because we're extending the GFAddon class, so |
||
69 | * we fake it. When called using `new GravityView_Settings`, it will return get_instance() instead. We pass |
||
70 | * 'get_instance' as a test string. |
||
71 | * |
||
72 | * @see get_instance() |
||
73 | * |
||
74 | * @param string $prevent_multiple_instances |
||
75 | */ |
||
76 | public function __construct( $prevent_multiple_instances = '' ) { |
||
84 | |||
85 | /** |
||
86 | * @return GravityView_Settings |
||
87 | */ |
||
88 | public static function get_instance() { |
||
96 | |||
97 | /** |
||
98 | * Prevent uninstall tab from being shown by returning false for the uninstall capability check. Otherwise: |
||
99 | * @inheritDoc |
||
100 | * |
||
101 | * @hack |
||
102 | * |
||
103 | * @param array|string $caps |
||
104 | * |
||
105 | * @return bool |
||
106 | */ |
||
107 | public function current_user_can_any( $caps ) { |
||
123 | |||
124 | /** |
||
125 | * Run actions when initializing admin |
||
126 | * |
||
127 | * Triggers the license key notice |
||
128 | * |
||
129 | * @return void |
||
130 | */ |
||
131 | function init_admin() { |
||
144 | |||
145 | /** |
||
146 | * Change the settings page header title to "GravityView" |
||
147 | * |
||
148 | * @param $setting_tabs |
||
149 | * |
||
150 | * @return array |
||
151 | */ |
||
152 | public function modify_app_settings_menu_title( $setting_tabs ) { |
||
158 | |||
159 | /** |
||
160 | * Load license handler in admin-ajax.php |
||
161 | */ |
||
162 | public function init_ajax() { |
||
165 | |||
166 | /** |
||
167 | * Make sure the license handler is available |
||
168 | */ |
||
169 | private function _load_license_handler() { |
||
179 | |||
180 | /** |
||
181 | * Display a notice if the plugin is inactive. |
||
182 | * @return void |
||
183 | */ |
||
184 | function license_key_notice() { |
||
185 | |||
186 | // Only show on GravityView pages |
||
187 | if( ! gravityview_is_admin_page() ) { |
||
188 | return; |
||
189 | } |
||
190 | |||
191 | $license_status = self::getSetting('license_key_status'); |
||
192 | $license_id = self::getSetting('license_key'); |
||
193 | $license_id = empty( $license_id ) ? 'license' : $license_id; |
||
194 | |||
195 | $message = esc_html__('Your GravityView license %s. This means you’re missing out on updates and support! %sActivate your license%s or %sget a license here%s.', 'gravityview'); |
||
196 | |||
197 | /** |
||
198 | * I wanted to remove the period from after the buttons in the string, |
||
199 | * but didn't want to mess up the translation strings for the translators. |
||
200 | */ |
||
201 | $message = mb_substr( $message, 0, mb_strlen( $message ) - 1 ); |
||
202 | $title = __('Inactive License', 'gravityview'); |
||
203 | $status = ''; |
||
204 | $update_below = false; |
||
205 | $primary_button_link = admin_url( 'edit.php?post_type=gravityview&page=gravityview_settings' ); |
||
206 | switch ( $license_status ) { |
||
207 | case 'invalid': |
||
208 | $title = __('Invalid License', 'gravityview'); |
||
209 | $status = __('is invalid', 'gravityview'); |
||
210 | break; |
||
211 | case 'deactivated': |
||
212 | $status = __('is inactive', 'gravityview'); |
||
213 | $update_below = __('Activate your license key below.', 'gravityview'); |
||
214 | break; |
||
215 | /** @noinspection PhpMissingBreakStatementInspection */ |
||
216 | case '': |
||
217 | $license_status = 'site_inactive'; |
||
218 | // break intentionally left blank |
||
219 | case 'site_inactive': |
||
220 | $status = __('has not been activated', 'gravityview'); |
||
221 | $update_below = __('Activate your license key below.', 'gravityview'); |
||
222 | break; |
||
223 | } |
||
224 | $url = 'https://gravityview.co/pricing/?utm_source=admin_notice&utm_medium=admin&utm_content='.$license_status.'&utm_campaign=Admin%20Notice'; |
||
225 | |||
226 | // Show a different notice on settings page for inactive licenses (hide the buttons) |
||
227 | if( $update_below && gravityview_is_admin_page( '', 'settings' ) ) { |
||
228 | $message = sprintf( $message, $status, '<div class="hidden">', '', '', '</div><a href="#" onclick="jQuery(\'#license_key\').focus(); return false;">' . $update_below . '</a>' ); |
||
229 | } else { |
||
230 | $message = sprintf( $message, $status, "\n\n" . '<a href="' . $primary_button_link . '" class="button button-primary">', '</a>', '<a href="' . esc_url( $url ) . '" class="button button-secondary">', '</a>' ); |
||
231 | } |
||
232 | |||
233 | if( !empty( $status ) ) { |
||
234 | GravityView_Admin_Notices::add_notice( array( |
||
235 | 'message' => $message, |
||
236 | 'class' => 'updated', |
||
237 | 'title' => $title, |
||
238 | 'cap' => 'gravityview_edit_settings', |
||
239 | 'dismiss' => sha1( $license_status.'_'.$license_id ), |
||
240 | )); |
||
241 | } |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * Register styles in the app admin page |
||
246 | * @return array |
||
247 | */ |
||
248 | public function styles() { |
||
268 | |||
269 | /** |
||
270 | * Add global Settings page for Multisite |
||
271 | * @since 1.7.6 |
||
272 | * @return void |
||
273 | */ |
||
274 | public function add_network_menu() { |
||
279 | |||
280 | /** |
||
281 | * Add Settings link to GravityView menu |
||
282 | * @return void |
||
283 | */ |
||
284 | public function create_app_menu() { |
||
305 | |||
306 | /** |
||
307 | * The Settings title |
||
308 | * @return string |
||
309 | */ |
||
310 | public function app_settings_title() { |
||
313 | |||
314 | /** |
||
315 | * Prevent displaying of any icon |
||
316 | * @return string |
||
317 | */ |
||
318 | public function app_settings_icon() { |
||
321 | |||
322 | /** |
||
323 | * Make protected public |
||
324 | * @inheritDoc |
||
325 | * @access public |
||
326 | */ |
||
327 | public function get_app_setting( $setting_name ) { |
||
342 | |||
343 | /** |
||
344 | * Returns the currently saved plugin settings |
||
345 | * |
||
346 | * Different from GFAddon in two ways: |
||
347 | * 1. Makes protected method public |
||
348 | * 2. Use default settings if the original settings don't exist |
||
349 | * |
||
350 | * @access public |
||
351 | * |
||
352 | * @return array |
||
353 | */ |
||
354 | public function get_app_settings() { |
||
357 | |||
358 | |||
359 | /** |
||
360 | * Updates app settings with the provided settings |
||
361 | * |
||
362 | * Same as the GVAddon, except it returns the value from update_option() |
||
363 | * |
||
364 | * @param array $settings - App settings to be saved |
||
365 | * |
||
366 | * @return boolean False if value was not updated and true if value was updated. |
||
367 | */ |
||
368 | public function update_app_settings( $settings ) { |
||
371 | |||
372 | /** |
||
373 | * Make protected public |
||
374 | * @inheritDoc |
||
375 | * @access public |
||
376 | */ |
||
377 | public function set_field_error( $field, $error_message = '' ) { |
||
380 | |||
381 | /** |
||
382 | * Register the settings field for the EDD License field type |
||
383 | * @param array $field |
||
384 | * @param bool $echo Whether to echo the |
||
385 | * |
||
386 | * @return string |
||
387 | */ |
||
388 | protected function settings_edd_license( $field, $echo = true ) { |
||
402 | |||
403 | /** |
||
404 | * Allow public access to the GV_License_Handler class |
||
405 | * @since 1.7.4 |
||
406 | * |
||
407 | * @return GV_License_Handler |
||
408 | */ |
||
409 | public function get_license_handler() { |
||
412 | |||
413 | /*** |
||
414 | * Renders the save button for settings pages |
||
415 | * |
||
416 | * @param array $field - Field array containing the configuration options of this field |
||
417 | * @param bool $echo = true - true to echo the output to the screen, false to simply return the contents as a string |
||
418 | * |
||
419 | * @return string The HTML |
||
420 | */ |
||
421 | public function settings_submit( $field, $echo = true ) { |
||
452 | |||
453 | /** |
||
454 | * Allow customizing the Save field parameters |
||
455 | * |
||
456 | * @param array $field |
||
457 | * @param bool $echo |
||
458 | * |
||
459 | * @return string |
||
460 | */ |
||
461 | public function settings_save( $field, $echo = true ) { |
||
477 | |||
478 | /** |
||
479 | * The same as the parent, except added support for field descriptions |
||
480 | * @inheritDoc |
||
481 | * @param $field array |
||
482 | */ |
||
483 | public function single_setting_label( $field ) { |
||
493 | |||
494 | /** |
||
495 | * Get the default settings for the plugin |
||
496 | * |
||
497 | * Merges previous settings created when using the Redux Framework |
||
498 | * |
||
499 | * @return array Settings with defaults set |
||
500 | */ |
||
501 | private function get_default_settings() { |
||
502 | |||
503 | $defaults = array( |
||
504 | // Set the default license in wp-config.php |
||
505 | 'license_key' => defined( 'GRAVITYVIEW_LICENSE_KEY' ) ? GRAVITYVIEW_LICENSE_KEY : '', |
||
506 | 'license_key_response' => '', |
||
507 | 'license_key_status' => '', |
||
508 | 'support-email' => get_bloginfo( 'admin_email' ), |
||
509 | 'no-conflict-mode' => '0', |
||
510 | 'support_port' => '1', |
||
511 | 'delete-on-uninstall' => '0', |
||
512 | ); |
||
513 | |||
514 | return $defaults; |
||
515 | } |
||
516 | |||
517 | /** |
||
518 | * Check for the `gravityview_edit_settings` capability before saving plugin settings. |
||
519 | * Gravity Forms says you're able to edit if you're able to view settings. GravityView allows two different permissions. |
||
520 | * |
||
521 | * @since 1.15 |
||
522 | * @return void |
||
523 | */ |
||
524 | public function maybe_save_app_settings() { |
||
536 | |||
537 | /** |
||
538 | * When the settings are saved, make sure the license key matches the previously activated key |
||
539 | * |
||
540 | * @return array settings from parent::get_posted_settings(), with `license_key_response` and `license_key_status` potentially unset |
||
541 | */ |
||
542 | public function get_posted_settings() { |
||
560 | |||
561 | /** |
||
562 | * Gets the required indicator |
||
563 | * Gets the markup of the required indicator symbol to highlight fields that are required |
||
564 | * |
||
565 | * @param $field - The field meta. |
||
566 | * |
||
567 | * @return string - Returns markup of the required indicator symbol |
||
568 | */ |
||
569 | public function get_required_indicator( $field ) { |
||
572 | |||
573 | /** |
||
574 | * Specify the settings fields to be rendered on the plugin settings page |
||
575 | * @return array |
||
576 | */ |
||
577 | public function app_settings_fields() { |
||
746 | |||
747 | /** |
||
748 | * Get the setting for GravityView by name |
||
749 | * |
||
750 | * @param string $key Option key to fetch |
||
751 | * |
||
752 | * @return mixed |
||
753 | */ |
||
754 | static public function getSetting( $key ) { |
||
757 | |||
758 | } |
||
759 | |||
760 | GravityView_Settings::get_instance(); |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.