| Total Complexity | 56 |
| Total Lines | 256 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
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.
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 | |||
| 13 | public $success_msg; |
||
| 14 | public $blank_msg; |
||
| 15 | public $unique_msg; |
||
| 16 | public $invalid_msg; |
||
| 17 | public $failed_msg; |
||
| 18 | public $submit_value; |
||
| 19 | public $login_msg; |
||
| 20 | public $admin_permission; |
||
| 21 | |||
| 22 | public $email_to; |
||
| 23 | public $load_style; |
||
| 24 | public $custom_style; |
||
| 25 | |||
| 26 | public $pubkey; |
||
| 27 | public $privkey; |
||
| 28 | public $re_lang; |
||
| 29 | public $re_type; |
||
| 30 | public $re_msg; |
||
| 31 | public $re_multi; |
||
| 32 | |||
| 33 | public $no_ips; |
||
| 34 | |||
| 35 | public function __construct() { |
||
| 36 | if ( ! defined('ABSPATH') ) { |
||
| 37 | die('You are not allowed to call this page directly.'); |
||
|
|
|||
| 38 | } |
||
| 39 | |||
| 40 | $settings = get_transient($this->option_name); |
||
| 41 | |||
| 42 | if ( ! is_object($settings) ) { |
||
| 43 | $settings = $this->translate_settings($settings); |
||
| 44 | } |
||
| 45 | |||
| 46 | foreach ( $settings as $setting_name => $setting ) { |
||
| 47 | $this->{$setting_name} = $setting; |
||
| 48 | unset($setting_name, $setting); |
||
| 49 | } |
||
| 50 | |||
| 51 | $this->set_default_options(); |
||
| 52 | } |
||
| 53 | |||
| 54 | private function translate_settings( $settings ) { |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @return array |
||
| 80 | */ |
||
| 81 | public function default_options() { |
||
| 82 | return array( |
||
| 83 | 'menu' => apply_filters( 'frm_default_menu', 'Formidable' ), |
||
| 84 | 'mu_menu' => 0, |
||
| 85 | 'use_html' => true, |
||
| 86 | 'jquery_css' => false, |
||
| 87 | 'accordion_js' => false, |
||
| 88 | 'fade_form' => false, |
||
| 89 | 'old_css' => true, |
||
| 90 | |||
| 91 | 're_multi' => 0, |
||
| 92 | |||
| 93 | 'success_msg' => __( 'Your responses were successfully submitted. Thank you!', 'formidable' ), |
||
| 94 | 'blank_msg' => __( 'This field cannot be blank.', 'formidable' ), |
||
| 95 | 'unique_msg' => __( 'This value must be unique.', 'formidable' ), |
||
| 96 | 'invalid_msg' => __( 'There was a problem with your submission. Errors are marked below.', 'formidable' ), |
||
| 97 | 'failed_msg' => __( 'We\'re sorry. It looks like you\'ve already submitted that.', 'formidable' ), |
||
| 98 | 'submit_value' => __( 'Submit', 'formidable' ), |
||
| 99 | 'login_msg' => __( 'You do not have permission to view this form.', 'formidable' ), |
||
| 100 | 'admin_permission' => __( 'You do not have permission to do that', 'formidable' ), |
||
| 101 | |||
| 102 | 'email_to' => '[admin_email]', |
||
| 103 | 'no_ips' => 0, |
||
| 104 | ); |
||
| 105 | } |
||
| 106 | |||
| 107 | private function set_default_options() { |
||
| 108 | $this->fill_recaptcha_settings(); |
||
| 109 | |||
| 110 | if ( ! isset($this->load_style) ) { |
||
| 111 | if ( ! isset($this->custom_style) ) { |
||
| 112 | $this->custom_style = true; |
||
| 113 | } |
||
| 114 | |||
| 115 | $this->load_style = 'all'; |
||
| 116 | } |
||
| 117 | |||
| 118 | $this->fill_with_defaults(); |
||
| 119 | |||
| 120 | if ( is_multisite() && is_admin() ) { |
||
| 121 | $mu_menu = get_site_option('frm_admin_menu_name'); |
||
| 122 | if ( $mu_menu && ! empty($mu_menu) ) { |
||
| 123 | $this->menu = $mu_menu; |
||
| 124 | $this->mu_menu = 1; |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | $frm_roles = FrmAppHelper::frm_capabilities('pro'); |
||
| 129 | foreach ( $frm_roles as $frm_role => $frm_role_description ) { |
||
| 130 | if ( ! isset($this->$frm_role) ) { |
||
| 131 | $this->$frm_role = 'administrator'; |
||
| 132 | } |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | public function fill_with_defaults( $params = array() ) { |
||
| 137 | $settings = $this->default_options(); |
||
| 138 | |||
| 139 | foreach ( $settings as $setting => $default ) { |
||
| 140 | if ( isset( $params[ 'frm_' . $setting ] ) ) { |
||
| 141 | $this->{$setting} = $params[ 'frm_' . $setting ]; |
||
| 142 | } else if ( ! isset($this->{$setting}) ) { |
||
| 143 | $this->{$setting} = $default; |
||
| 144 | } |
||
| 145 | |||
| 146 | if ( $setting == 'menu' && empty( $this->{$setting} ) ) { |
||
| 147 | $this->{$setting} = $default; |
||
| 148 | } |
||
| 149 | |||
| 150 | unset($setting, $default); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | private function fill_recaptcha_settings() { |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | public function validate( $params, $errors ) { |
||
| 185 | } |
||
| 186 | |||
| 187 | public function update( $params ) { |
||
| 188 | $this->fill_with_defaults($params); |
||
| 189 | $this->update_settings($params); |
||
| 190 | |||
| 191 | if ( $this->mu_menu ) { |
||
| 192 | update_site_option('frm_admin_menu_name', $this->menu); |
||
| 193 | } else if ( current_user_can('administrator') ) { |
||
| 194 | update_site_option('frm_admin_menu_name', false); |
||
| 195 | } |
||
| 196 | |||
| 197 | $this->update_roles($params); |
||
| 198 | |||
| 199 | do_action( 'frm_update_settings', $params ); |
||
| 200 | |||
| 201 | if ( function_exists( 'get_filesystem_method' ) ) { |
||
| 202 | // save styling settings in case fallback setting changes |
||
| 203 | $frm_style = new FrmStyle(); |
||
| 204 | $frm_style->update( 'default' ); |
||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | private function update_settings( $params ) { |
||
| 225 | } |
||
| 226 | |||
| 227 | private function update_roles( $params ) { |
||
| 228 | global $wp_roles; |
||
| 229 | |||
| 230 | $frm_roles = FrmAppHelper::frm_capabilities(); |
||
| 231 | $roles = get_editable_roles(); |
||
| 232 | foreach ( $frm_roles as $frm_role => $frm_role_description ) { |
||
| 233 | $this->$frm_role = (array) ( isset( $params[ $frm_role ] ) ? $params[ $frm_role ] : 'administrator' ); |
||
| 234 | |||
| 235 | // Make sure administrators always have permissions |
||
| 236 | if ( ! in_array( 'administrator', $this->$frm_role ) ) { |
||
| 237 | array_push( $this->$frm_role, 'administrator' ); |
||
| 238 | } |
||
| 239 | |||
| 240 | foreach ( $roles as $role => $details ) { |
||
| 241 | if ( in_array($role, $this->$frm_role) ) { |
||
| 242 | $wp_roles->add_cap( $role, $frm_role ); |
||
| 243 | } else { |
||
| 244 | $wp_roles->remove_cap( $role, $frm_role ); |
||
| 245 | } |
||
| 246 | } |
||
| 247 | } |
||
| 248 | } |
||
| 249 | |||
| 250 | public function store() { |
||
| 259 | } |
||
| 260 | } |
||
| 261 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.