Complex classes like FrmSettings 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 FrmSettings, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 3 | class FrmSettings{ |
||
|
|
|||
| 4 | public $option_name = 'frm_options'; |
||
| 5 | public $menu; |
||
| 6 | public $mu_menu; |
||
| 7 | public $preview_page_id; |
||
| 8 | public $use_html; |
||
| 9 | public $jquery_css; |
||
| 10 | public $accordion_js; |
||
| 11 | |||
| 12 | public $success_msg; |
||
| 13 | public $blank_msg; |
||
| 14 | public $unique_msg; |
||
| 15 | public $invalid_msg; |
||
| 16 | public $failed_msg; |
||
| 17 | public $submit_value; |
||
| 18 | public $login_msg; |
||
| 19 | public $admin_permission; |
||
| 20 | |||
| 21 | public $email_to; |
||
| 22 | public $load_style; |
||
| 23 | public $custom_style; |
||
| 24 | |||
| 25 | public $pubkey; |
||
| 26 | public $privkey; |
||
| 27 | public $re_lang; |
||
| 28 | public $re_msg; |
||
| 29 | public $re_multi; |
||
| 30 | |||
| 31 | public function __construct() { |
||
| 32 | if ( ! defined('ABSPATH') ) { |
||
| 33 | die('You are not allowed to call this page directly.'); |
||
| 34 | } |
||
| 35 | |||
| 36 | $settings = get_transient($this->option_name); |
||
| 37 | |||
| 38 | if ( ! is_object($settings) ) { |
||
| 39 | $settings = $this->translate_settings($settings); |
||
| 40 | } |
||
| 41 | |||
| 42 | foreach ( $settings as $setting_name => $setting ) { |
||
| 43 | $this->{$setting_name} = $setting; |
||
| 44 | unset($setting_name, $setting); |
||
| 45 | } |
||
| 46 | |||
| 47 | $this->set_default_options(); |
||
| 48 | } |
||
| 49 | |||
| 50 | private function translate_settings( $settings ) { |
||
| 51 | if ( $settings ) { //workaround for W3 total cache conflict |
||
| 52 | return unserialize(serialize($settings)); |
||
| 53 | } |
||
| 54 | |||
| 55 | $settings = get_option($this->option_name); |
||
| 56 | if ( is_object($settings) ) { |
||
| 57 | set_transient($this->option_name, $settings); |
||
| 58 | return $settings; |
||
| 59 | } |
||
| 60 | |||
| 61 | // If unserializing didn't work |
||
| 62 | if ( $settings ) { //workaround for W3 total cache conflict |
||
| 63 | $settings = unserialize(serialize($settings)); |
||
| 64 | } else { |
||
| 65 | $settings = $this; |
||
| 66 | } |
||
| 67 | |||
| 68 | update_option($this->option_name, $settings); |
||
| 69 | set_transient($this->option_name, $settings); |
||
| 70 | |||
| 71 | return $settings; |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @return array |
||
| 76 | */ |
||
| 77 | public function default_options() { |
||
| 78 | return array( |
||
| 79 | 'menu' => apply_filters( 'frm_default_menu', __( 'Forms', 'formidable' ) ), |
||
| 80 | 'mu_menu' => 0, |
||
| 81 | 'preview_page_id' => 0, |
||
| 82 | 'use_html' => true, |
||
| 83 | 'jquery_css' => false, |
||
| 84 | 'accordion_js' => false, |
||
| 85 | |||
| 86 | 're_multi' => 0, |
||
| 87 | |||
| 88 | 'success_msg' => __( 'Your responses were successfully submitted. Thank you!', 'formidable' ), |
||
| 89 | 'blank_msg' => __( 'This field cannot be blank.', 'formidable' ), |
||
| 90 | 'unique_msg' => __( 'This value must be unique.', 'formidable' ), |
||
| 91 | 'invalid_msg' => __( 'There was a problem with your submission. Errors are marked below.', 'formidable' ), |
||
| 92 | 'failed_msg' => __( 'We\'re sorry. It looks like you\'ve already submitted that.', 'formidable' ), |
||
| 93 | 'submit_value' => __( 'Submit', 'formidable' ), |
||
| 94 | 'login_msg' => __( 'You do not have permission to view this form.', 'formidable' ), |
||
| 95 | 'admin_permission' => __( 'You do not have permission to do that', 'formidable' ), |
||
| 96 | |||
| 97 | 'email_to' => '[admin_email]', |
||
| 98 | ); |
||
| 99 | } |
||
| 100 | |||
| 101 | private function set_default_options() { |
||
| 102 | $this->fill_recaptcha_settings(); |
||
| 103 | |||
| 104 | if ( ! isset($this->load_style) ) { |
||
| 105 | if ( ! isset($this->custom_style) ) { |
||
| 106 | $this->custom_style = true; |
||
| 107 | } |
||
| 108 | |||
| 109 | $this->load_style = 'all'; |
||
| 110 | } |
||
| 111 | |||
| 112 | $this->fill_with_defaults(); |
||
| 113 | |||
| 114 | if ( is_multisite() && is_admin() ) { |
||
| 115 | $mu_menu = get_site_option('frm_admin_menu_name'); |
||
| 116 | if ( $mu_menu && ! empty($mu_menu) ) { |
||
| 117 | $this->menu = $mu_menu; |
||
| 118 | $this->mu_menu = 1; |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | $frm_roles = FrmAppHelper::frm_capabilities('pro'); |
||
| 123 | foreach ( $frm_roles as $frm_role => $frm_role_description ) { |
||
| 124 | if ( ! isset($this->$frm_role) ) { |
||
| 125 | $this->$frm_role = 'administrator'; |
||
| 126 | } |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | public function fill_with_defaults( $params = array() ) { |
||
| 131 | $settings = $this->default_options(); |
||
| 132 | |||
| 133 | foreach ( $settings as $setting => $default ) { |
||
| 134 | if ( isset( $params[ 'frm_' . $setting ] ) ) { |
||
| 135 | $this->{$setting} = $params[ 'frm_' . $setting ]; |
||
| 136 | } else if ( ! isset($this->{$setting}) ) { |
||
| 137 | $this->{$setting} = $default; |
||
| 138 | } |
||
| 139 | |||
| 140 | if ( $setting == 'menu' && empty( $this->{$setting} ) ) { |
||
| 141 | $this->{$setting} = $default; |
||
| 142 | } |
||
| 143 | |||
| 144 | unset($setting, $default); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | private function fill_recaptcha_settings() { |
||
| 149 | $privkey = ''; |
||
| 150 | $re_lang = 'en'; |
||
| 151 | |||
| 152 | if ( ! isset($this->pubkey) ) { |
||
| 153 | // get the options from the database |
||
| 154 | $recaptcha_opt = is_multisite() ? get_site_option('recaptcha') : get_option('recaptcha'); |
||
| 155 | $this->pubkey = isset($recaptcha_opt['pubkey']) ? $recaptcha_opt['pubkey'] : ''; |
||
| 156 | $privkey = isset($recaptcha_opt['privkey']) ? $recaptcha_opt['privkey'] : $privkey; |
||
| 157 | $re_lang = isset($recaptcha_opt['re_lang']) ? $recaptcha_opt['re_lang'] : $re_lang; |
||
| 158 | } |
||
| 159 | |||
| 160 | if ( ! isset($this->re_msg) || empty($this->re_msg) ) { |
||
| 161 | $this->re_msg = __( 'The reCAPTCHA was not entered correctly', 'formidable' ); |
||
| 162 | } |
||
| 163 | |||
| 164 | if ( ! isset($this->privkey) ) { |
||
| 165 | $this->privkey = $privkey; |
||
| 166 | } |
||
| 167 | |||
| 168 | if ( ! isset($this->re_lang) ) { |
||
| 169 | $this->re_lang = $re_lang; |
||
| 170 | } |
||
| 171 | } |
||
| 172 | |||
| 173 | public function validate( $params, $errors ) { |
||
| 174 | $errors = apply_filters( 'frm_validate_settings', $errors, $params ); |
||
| 175 | return $errors; |
||
| 176 | } |
||
| 177 | |||
| 178 | public function update( $params ) { |
||
| 179 | $this->fill_with_defaults($params); |
||
| 180 | $this->update_settings($params); |
||
| 181 | |||
| 182 | if ( $this->mu_menu ) { |
||
| 183 | update_site_option('frm_admin_menu_name', $this->menu); |
||
| 184 | } else if ( current_user_can('administrator') ) { |
||
| 185 | update_site_option('frm_admin_menu_name', false); |
||
| 186 | } |
||
| 187 | |||
| 188 | $this->update_roles($params); |
||
| 189 | |||
| 190 | do_action( 'frm_update_settings', $params ); |
||
| 191 | } |
||
| 192 | |||
| 193 | private function update_settings( $params ) { |
||
| 209 | |||
| 210 | private function update_roles( $params ) { |
||
| 211 | global $wp_roles; |
||
| 212 | |||
| 213 | $frm_roles = FrmAppHelper::frm_capabilities(); |
||
| 214 | $roles = get_editable_roles(); |
||
| 215 | foreach ( $frm_roles as $frm_role => $frm_role_description ) { |
||
| 216 | $this->$frm_role = (array) ( isset( $params[ $frm_role ] ) ? $params[ $frm_role ] : 'administrator' ); |
||
| 217 | |||
| 232 | |||
| 233 | public function store() { |
||
| 243 | } |
||
| 244 |