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 $use_html; |
||
| 8 | public $jquery_css; |
||
| 9 | public $accordion_js; |
||
| 10 | public $fade_form; |
||
| 11 | public $old_css; |
||
| 12 | public $admin_bar; |
||
| 13 | |||
| 14 | public $success_msg; |
||
| 15 | public $blank_msg; |
||
| 16 | public $unique_msg; |
||
| 17 | public $invalid_msg; |
||
| 18 | public $failed_msg; |
||
| 19 | public $submit_value; |
||
| 20 | public $login_msg; |
||
| 21 | public $admin_permission; |
||
| 22 | |||
| 23 | public $email_to; |
||
| 24 | public $load_style; |
||
| 25 | public $custom_style; |
||
| 26 | |||
| 27 | public $pubkey; |
||
| 28 | public $privkey; |
||
| 29 | public $re_lang; |
||
| 30 | public $re_type; |
||
| 31 | public $re_msg; |
||
| 32 | public $re_multi; |
||
| 33 | |||
| 34 | public $no_ips; |
||
| 35 | public $current_form = 0; |
||
| 36 | public $tracking; |
||
| 37 | |||
| 38 | public function __construct( $args = array() ) { |
||
| 39 | if ( ! defined( 'ABSPATH' ) ) { |
||
| 40 | die( 'You are not allowed to call this page directly.' ); |
||
| 41 | } |
||
| 42 | |||
| 43 | $settings = get_transient( $this->option_name ); |
||
| 44 | |||
| 45 | if ( ! is_object( $settings ) ) { |
||
| 46 | $settings = $this->translate_settings( $settings ); |
||
| 47 | } |
||
| 48 | |||
| 49 | foreach ( $settings as $setting_name => $setting ) { |
||
| 50 | $this->{$setting_name} = $setting; |
||
| 51 | unset( $setting_name, $setting ); |
||
| 52 | } |
||
| 53 | |||
| 54 | $this->set_default_options(); |
||
| 55 | |||
| 56 | $this->maybe_filter_for_form( $args ); |
||
| 57 | } |
||
| 58 | |||
| 59 | private function translate_settings( $settings ) { |
||
| 60 | if ( $settings ) { //workaround for W3 total cache conflict |
||
| 61 | return unserialize( serialize( $settings ) ); |
||
| 62 | } |
||
| 63 | |||
| 64 | $settings = get_option( $this->option_name ); |
||
| 65 | if ( is_object( $settings ) ) { |
||
| 66 | set_transient( $this->option_name, $settings ); |
||
| 67 | |||
| 68 | return $settings; |
||
| 69 | } |
||
| 70 | |||
| 71 | // If unserializing didn't work |
||
| 72 | if ( $settings ) { //workaround for W3 total cache conflict |
||
| 73 | $settings = unserialize( serialize( $settings ) ); |
||
| 74 | } else { |
||
| 75 | $settings = $this; |
||
| 76 | } |
||
| 77 | |||
| 78 | update_option( $this->option_name, $settings ); |
||
| 79 | set_transient( $this->option_name, $settings ); |
||
| 80 | |||
| 81 | return $settings; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @return array |
||
| 86 | */ |
||
| 87 | public function default_options() { |
||
| 88 | return array( |
||
| 89 | 'menu' => apply_filters( 'frm_default_menu', 'Formidable' ), |
||
| 90 | 'mu_menu' => 0, |
||
| 91 | 'use_html' => true, |
||
| 92 | 'jquery_css' => false, |
||
| 93 | 'accordion_js' => false, |
||
| 94 | 'fade_form' => false, |
||
| 95 | 'old_css' => false, |
||
| 96 | 'admin_bar' => false, |
||
| 97 | |||
| 98 | 're_multi' => 1, |
||
| 99 | |||
| 100 | 'success_msg' => __( 'Your responses were successfully submitted. Thank you!', 'formidable' ), |
||
| 101 | 'blank_msg' => __( 'This field cannot be blank.', 'formidable' ), |
||
| 102 | 'unique_msg' => __( 'This value must be unique.', 'formidable' ), |
||
| 103 | 'invalid_msg' => __( 'There was a problem with your submission. Errors are marked below.', 'formidable' ), |
||
| 104 | 'failed_msg' => __( 'We\'re sorry. It looks like you\'ve already submitted that.', 'formidable' ), |
||
| 105 | 'submit_value' => __( 'Submit', 'formidable' ), |
||
| 106 | 'login_msg' => __( 'You do not have permission to view this form.', 'formidable' ), |
||
| 107 | 'admin_permission' => __( 'You do not have permission to do that', 'formidable' ), |
||
| 108 | |||
| 109 | 'email_to' => '[admin_email]', |
||
| 110 | 'no_ips' => 0, |
||
| 111 | 'tracking' => FrmAppHelper::pro_is_installed(), |
||
| 112 | ); |
||
| 113 | } |
||
| 114 | |||
| 115 | private function set_default_options() { |
||
| 116 | $this->fill_recaptcha_settings(); |
||
| 117 | |||
| 118 | if ( ! isset( $this->load_style ) ) { |
||
| 119 | if ( ! isset( $this->custom_style ) ) { |
||
| 120 | $this->custom_style = true; |
||
| 121 | } |
||
| 122 | |||
| 123 | $this->load_style = 'all'; |
||
| 124 | } |
||
| 125 | |||
| 126 | $this->fill_with_defaults(); |
||
| 127 | |||
| 128 | if ( is_multisite() && is_admin() ) { |
||
| 129 | $mu_menu = get_site_option( 'frm_admin_menu_name' ); |
||
| 130 | if ( $mu_menu && ! empty( $mu_menu ) ) { |
||
| 131 | $this->menu = $mu_menu; |
||
| 132 | $this->mu_menu = 1; |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | $frm_roles = FrmAppHelper::frm_capabilities( 'pro' ); |
||
| 137 | foreach ( $frm_roles as $frm_role => $frm_role_description ) { |
||
| 138 | if ( ! isset( $this->$frm_role ) ) { |
||
| 139 | $this->$frm_role = 'administrator'; |
||
| 140 | } |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | public function fill_with_defaults( $params = array() ) { |
||
| 145 | $settings = $this->default_options(); |
||
| 146 | |||
| 147 | foreach ( $settings as $setting => $default ) { |
||
| 148 | if ( isset( $params[ 'frm_' . $setting ] ) ) { |
||
| 149 | $this->{$setting} = $params[ 'frm_' . $setting ]; |
||
| 150 | } elseif ( ! isset( $this->{$setting} ) ) { |
||
| 151 | $this->{$setting} = $default; |
||
| 152 | } |
||
| 153 | |||
| 154 | if ( $setting == 'menu' && empty( $this->{$setting} ) ) { |
||
| 155 | $this->{$setting} = $default; |
||
| 156 | } |
||
| 157 | |||
| 158 | unset( $setting, $default ); |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | private function fill_recaptcha_settings() { |
||
| 163 | $privkey = ''; |
||
| 164 | $re_lang = ''; |
||
| 165 | |||
| 166 | if ( ! isset( $this->pubkey ) ) { |
||
| 167 | // get the options from the database |
||
| 168 | $recaptcha_opt = is_multisite() ? get_site_option( 'recaptcha' ) : get_option( 'recaptcha' ); |
||
| 169 | $this->pubkey = isset( $recaptcha_opt['pubkey'] ) ? $recaptcha_opt['pubkey'] : ''; |
||
| 170 | $privkey = isset( $recaptcha_opt['privkey'] ) ? $recaptcha_opt['privkey'] : $privkey; |
||
| 171 | $re_lang = isset( $recaptcha_opt['re_lang'] ) ? $recaptcha_opt['re_lang'] : $re_lang; |
||
| 172 | } |
||
| 173 | |||
| 174 | if ( ! isset( $this->re_msg ) || empty( $this->re_msg ) ) { |
||
| 175 | $this->re_msg = __( 'The reCAPTCHA was not entered correctly', 'formidable' ); |
||
| 176 | } |
||
| 177 | |||
| 178 | if ( ! isset( $this->privkey ) ) { |
||
| 179 | $this->privkey = $privkey; |
||
| 180 | } |
||
| 181 | |||
| 182 | if ( ! isset( $this->re_lang ) ) { |
||
| 183 | $this->re_lang = $re_lang; |
||
| 184 | } |
||
| 185 | |||
| 186 | if ( ! isset( $this->re_type ) ) { |
||
| 187 | $this->re_type = ''; |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Get values that may be shown on the front-end without an override in the form settings. |
||
| 193 | * |
||
| 194 | * @since 3.06.01 |
||
| 195 | */ |
||
| 196 | public function translatable_strings() { |
||
| 197 | return array( |
||
| 198 | 'invalid_msg', |
||
| 199 | 'failed_msg', |
||
| 200 | 'login_msg', |
||
| 201 | ); |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Allow strings to be filtered when a specific form may be displaying them. |
||
| 206 | * |
||
| 207 | * @since 3.06.01 |
||
| 208 | */ |
||
| 209 | public function maybe_filter_for_form( $args ) { |
||
| 210 | if ( isset( $args['current_form'] ) && is_numeric( $args['current_form'] ) ) { |
||
| 211 | $this->current_form = $args['current_form']; |
||
| 212 | foreach ( $this->translatable_strings() as $string ) { |
||
| 213 | $this->{$string} = apply_filters( 'frm_global_setting', $this->{$string}, $string, $this ); |
||
| 214 | $this->{$string} = apply_filters( 'frm_global_' . $string, $this->{$string}, $this ); |
||
| 215 | } |
||
| 216 | } |
||
| 217 | } |
||
| 218 | |||
| 219 | public function validate( $params, $errors ) { |
||
| 220 | return apply_filters( 'frm_validate_settings', $errors, $params ); |
||
| 221 | } |
||
| 222 | |||
| 223 | public function update( $params ) { |
||
| 224 | $this->fill_with_defaults( $params ); |
||
| 225 | $this->update_settings( $params ); |
||
| 226 | |||
| 227 | if ( $this->mu_menu ) { |
||
| 228 | update_site_option( 'frm_admin_menu_name', $this->menu ); |
||
| 229 | } elseif ( current_user_can( 'administrator' ) ) { |
||
| 230 | update_site_option( 'frm_admin_menu_name', false ); |
||
| 231 | } |
||
| 232 | |||
| 233 | $this->update_roles( $params ); |
||
| 234 | |||
| 235 | do_action( 'frm_update_settings', $params ); |
||
| 236 | |||
| 237 | if ( function_exists( 'get_filesystem_method' ) ) { |
||
| 238 | // save styling settings in case fallback setting changes |
||
| 239 | $frm_style = new FrmStyle(); |
||
| 240 | $frm_style->update( 'default' ); |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | private function update_settings( $params ) { |
||
| 245 | $this->pubkey = trim( $params['frm_pubkey'] ); |
||
| 246 | $this->privkey = $params['frm_privkey']; |
||
| 247 | $this->re_type = $params['frm_re_type']; |
||
| 248 | $this->re_lang = $params['frm_re_lang']; |
||
| 249 | |||
| 250 | $this->load_style = $params['frm_load_style']; |
||
| 251 | |||
| 252 | $checkboxes = array( 'mu_menu', 're_multi', 'use_html', 'jquery_css', 'accordion_js', 'fade_form', 'old_css', 'no_ips', 'tracking', 'admin_bar' ); |
||
| 253 | foreach ( $checkboxes as $set ) { |
||
| 254 | $this->$set = isset( $params[ 'frm_' . $set ] ) ? $params[ 'frm_' . $set ] : 0; |
||
| 255 | } |
||
| 256 | } |
||
| 257 | |||
| 258 | private function update_roles( $params ) { |
||
| 280 | |||
| 281 | public function store() { |
||
| 291 | } |
||
| 292 |