Completed
Push — master ( a55afc...799fa9 )
by Stephanie
04:11
created

FrmSettings::fill_recaptcha_settings()   D

Complexity

Conditions 10
Paths 136

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 24
rs 4.9032
cc 10
eloc 14
nc 136
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
class FrmSettings{
0 ignored issues
show
Coding Style introduced by
Class name "" is not in camel caps format
Loading history...
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
30
    public function __construct() {
31
        if ( ! defined('ABSPATH') ) {
32
            die('You are not allowed to call this page directly.');
33
        }
34
35
        $settings = get_transient($this->option_name);
36
37
        if ( ! is_object($settings) ) {
38
            $settings = $this->translate_settings($settings);
39
        }
40
41
        foreach ( $settings as $setting_name => $setting ) {
42
            $this->{$setting_name} = $setting;
43
            unset($setting_name, $setting);
44
        }
45
46
        $this->set_default_options();
47
    }
48
49
	private function translate_settings( $settings ) {
50
        if ( $settings ) { //workaround for W3 total cache conflict
51
            return unserialize(serialize($settings));
52
        }
53
54
        $settings = get_option($this->option_name);
55
        if ( is_object($settings) ) {
56
            set_transient($this->option_name, $settings);
57
            return $settings;
58
        }
59
60
        // If unserializing didn't work
61
        if ( $settings ) { //workaround for W3 total cache conflict
62
            $settings = unserialize(serialize($settings));
63
        } else {
64
            $settings = $this;
65
        }
66
67
        update_option($this->option_name, $settings);
68
        set_transient($this->option_name, $settings);
69
70
        return $settings;
71
    }
72
73
    /**
74
     * @return array
75
     */
76
	public function default_options() {
77
        return array(
78
            'menu'      => apply_filters( 'frm_default_menu', __( 'Forms', 'formidable' ) ),
79
            'mu_menu'   => 0,
80
            'preview_page_id' => 0,
81
            'use_html'  => true,
82
            'jquery_css' => false,
83
            'accordion_js' => false,
84
85
            'success_msg' => __( 'Your responses were successfully submitted. Thank you!', 'formidable' ),
86
            'blank_msg' => __( 'This field cannot be blank.', 'formidable' ),
87
            'unique_msg' => __( 'This value must be unique.', 'formidable' ),
88
            'invalid_msg' => __( 'There was a problem with your submission. Errors are marked below.', 'formidable' ),
89
            'failed_msg' => __( 'We\'re sorry. It looks like you\'ve  already submitted that.', 'formidable' ),
90
            'submit_value' => __( 'Submit', 'formidable' ),
91
            'login_msg' => __( 'You do not have permission to view this form.', 'formidable' ),
92
            'admin_permission' => __( 'You do not have permission to do that', 'formidable' ),
93
94
            'email_to' => '[admin_email]',
95
        );
96
    }
97
98
	private function set_default_options() {
99
        $this->fill_recaptcha_settings();
100
101
        if ( ! isset($this->load_style) ) {
102
            if ( ! isset($this->custom_style) ) {
103
                $this->custom_style = true;
104
            }
105
106
            $this->load_style = 'all';
107
        }
108
109
        $this->fill_with_defaults();
110
111
        if ( is_multisite() && is_admin() ) {
112
            $mu_menu = get_site_option('frm_admin_menu_name');
113
            if ( $mu_menu && ! empty($mu_menu) ) {
114
                $this->menu = $mu_menu;
115
                $this->mu_menu = 1;
116
            }
117
        }
118
119
        $frm_roles = FrmAppHelper::frm_capabilities('pro');
120
        foreach ( $frm_roles as $frm_role => $frm_role_description ) {
121
            if ( ! isset($this->$frm_role) ) {
122
                $this->$frm_role = 'administrator';
123
            }
124
        }
125
    }
126
127
	public function fill_with_defaults( $params = array() ) {
128
        $settings = $this->default_options();
129
130
        foreach ( $settings as $setting => $default ) {
131
			if ( isset( $params[ 'frm_' . $setting ] ) ) {
132
				$this->{$setting} = $params[ 'frm_' . $setting ];
133
            } else if ( ! isset($this->{$setting}) ) {
134
                $this->{$setting} = $default;
135
            }
136
137
			if ( $setting == 'menu' && empty( $this->{$setting} ) ) {
138
				$this->{$setting} = $default;
139
			}
140
141
            unset($setting, $default);
142
        }
143
    }
144
145
    private function fill_recaptcha_settings() {
146
        $privkey = '';
147
        $re_lang = 'en';
148
149
        if ( ! isset($this->pubkey) ) {
150
            // get the options from the database
151
            $recaptcha_opt = is_multisite() ? get_site_option('recaptcha') : get_option('recaptcha');
152
            $this->pubkey = isset($recaptcha_opt['pubkey']) ? $recaptcha_opt['pubkey'] : '';
153
            $privkey = isset($recaptcha_opt['privkey']) ? $recaptcha_opt['privkey'] : $privkey;
154
            $re_lang = isset($recaptcha_opt['re_lang']) ? $recaptcha_opt['re_lang'] : $re_lang;
155
        }
156
157
        if ( ! isset($this->re_msg) || empty($this->re_msg) ) {
158
            $this->re_msg = __( 'The reCAPTCHA was not entered correctly', 'formidable' );
159
        }
160
161
        if ( ! isset($this->privkey) ) {
162
            $this->privkey = $privkey;
163
        }
164
165
        if ( ! isset($this->re_lang) ) {
166
            $this->re_lang = $re_lang;
167
        }
168
    }
169
170
    public function validate( $params, $errors ) {
171
        $errors = apply_filters( 'frm_validate_settings', $errors, $params );
172
        return $errors;
173
    }
174
175
	public function update( $params ) {
176
        $this->fill_with_defaults($params);
177
        $this->update_settings($params);
178
179
        if ( $this->mu_menu ) {
180
            update_site_option('frm_admin_menu_name', $this->menu);
181
        } else if ( current_user_can('administrator') ) {
182
            update_site_option('frm_admin_menu_name', false);
183
        }
184
185
        $this->update_roles($params);
186
187
        do_action( 'frm_update_settings', $params );
188
    }
189
190
	private function update_settings( $params ) {
191
        $this->mu_menu = isset($params['frm_mu_menu']) ? $params['frm_mu_menu'] : 0;
192
193
        $this->pubkey = trim($params['frm_pubkey']);
194
        $this->privkey = $params['frm_privkey'];
195
        $this->re_lang = $params['frm_re_lang'];
196
197
        $this->load_style = $params['frm_load_style'];
198
        $this->preview_page_id = (int) $params['frm-preview-page-id'];
199
200
        $this->use_html = isset($params['frm_use_html']) ? $params['frm_use_html'] : 0;
201
        //$this->custom_style = isset($params['frm_custom_style']) ? $params['frm_custom_style'] : 0;
202
		$this->jquery_css = isset( $params['frm_jquery_css'] ) ? absint( $params['frm_jquery_css'] ) : 0;
203
		$this->accordion_js = isset( $params['frm_accordion_js'] ) ? absint( $params['frm_accordion_js'] ) : 0;
204
    }
205
206
	private function update_roles( $params ) {
207
        //update roles
208
        global $wp_roles;
209
210
        $frm_roles = FrmAppHelper::frm_capabilities();
211
        $roles = get_editable_roles();
212
        foreach ( $frm_roles as $frm_role => $frm_role_description ) {
213
            $this->$frm_role = (array) ( isset( $params[ $frm_role ] ) ? $params[ $frm_role ] : 'administrator' );
214
215
            if ( count($this->$frm_role) === 1 ) {
216
                $set_role = reset($this->$frm_role);
217
                switch ( $set_role ) {
218
                    case 'subscriber':
219
                        array_push($this->$frm_role, 'contributor');
220
                    case 'contributor':
221
                        array_push($this->$frm_role, 'author');
222
                    case 'author':
223
                        array_push($this->$frm_role, 'editor');
224
                    case 'editor':
225
                        array_push($this->$frm_role, 'administrator');
226
                }
227
                unset($set_role);
228
            }
229
230
            foreach ( $roles as $role => $details ) {
231
                if ( in_array($role, $this->$frm_role) ) {
232
    			    $wp_roles->add_cap( $role, $frm_role );
233
    			} else {
234
    			    $wp_roles->remove_cap( $role, $frm_role );
235
    			}
236
    			unset($role, $details);
237
    		}
238
		}
239
    }
240
241
	public function store() {
242
        // Save the posted value in the database
243
244
        update_option('frm_options', $this);
245
246
        delete_transient('frm_options');
247
        set_transient('frm_options', $this);
248
249
        do_action( 'frm_store_settings' );
250
    }
251
}
252