Passed
Push — master ( ec16eb...0357f3 )
by Stephanie
03:05
created

FrmSettings::update_settings()   C

Complexity

Conditions 9
Paths 256

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 13
nc 256
nop 1
dl 0
loc 17
rs 6.4615
c 0
b 0
f 0
1
<?php
2
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.');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
38
        }
39
40
        $settings = get_transient($this->option_name);
0 ignored issues
show
Bug introduced by
The function get_transient was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
        $settings = /** @scrutinizer ignore-call */ get_transient($this->option_name);
Loading history...
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 ) {
55
        if ( $settings ) { //workaround for W3 total cache conflict
56
            return unserialize(serialize($settings));
57
        }
58
59
        $settings = get_option($this->option_name);
0 ignored issues
show
Bug introduced by
The function get_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
        $settings = /** @scrutinizer ignore-call */ get_option($this->option_name);
Loading history...
60
        if ( is_object($settings) ) {
61
            set_transient($this->option_name, $settings);
0 ignored issues
show
Bug introduced by
The function set_transient was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
            /** @scrutinizer ignore-call */ 
62
            set_transient($this->option_name, $settings);
Loading history...
62
            return $settings;
63
        }
64
65
        // If unserializing didn't work
66
        if ( $settings ) { //workaround for W3 total cache conflict
67
            $settings = unserialize(serialize($settings));
68
        } else {
69
            $settings = $this;
70
        }
71
72
        update_option($this->option_name, $settings);
0 ignored issues
show
Bug introduced by
The function update_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
        /** @scrutinizer ignore-call */ 
73
        update_option($this->option_name, $settings);
Loading history...
73
        set_transient($this->option_name, $settings);
74
75
        return $settings;
76
    }
77
78
    /**
79
     * @return array
80
     */
81
	public function default_options() {
82
        return array(
83
            'menu'      => apply_filters( 'frm_default_menu', 'Formidable' ),
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

83
            'menu'      => /** @scrutinizer ignore-call */ apply_filters( 'frm_default_menu', 'Formidable' ),
Loading history...
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' ),
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

93
            'success_msg' => /** @scrutinizer ignore-call */ __( 'Your responses were successfully submitted. Thank you!', 'formidable' ),
Loading history...
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() ) {
0 ignored issues
show
Bug introduced by
The function is_multisite was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

120
        if ( /** @scrutinizer ignore-call */ is_multisite() && is_admin() ) {
Loading history...
Bug introduced by
The function is_admin was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

120
        if ( is_multisite() && /** @scrutinizer ignore-call */ is_admin() ) {
Loading history...
121
            $mu_menu = get_site_option('frm_admin_menu_name');
0 ignored issues
show
Bug introduced by
The function get_site_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

121
            $mu_menu = /** @scrutinizer ignore-call */ get_site_option('frm_admin_menu_name');
Loading history...
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() {
155
        $privkey = '';
156
		$re_lang = '';
157
158
        if ( ! isset($this->pubkey) ) {
159
            // get the options from the database
160
            $recaptcha_opt = is_multisite() ? get_site_option('recaptcha') : get_option('recaptcha');
0 ignored issues
show
Bug introduced by
The function is_multisite was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

160
            $recaptcha_opt = /** @scrutinizer ignore-call */ is_multisite() ? get_site_option('recaptcha') : get_option('recaptcha');
Loading history...
Bug introduced by
The function get_site_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

160
            $recaptcha_opt = is_multisite() ? /** @scrutinizer ignore-call */ get_site_option('recaptcha') : get_option('recaptcha');
Loading history...
Bug introduced by
The function get_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

160
            $recaptcha_opt = is_multisite() ? get_site_option('recaptcha') : /** @scrutinizer ignore-call */ get_option('recaptcha');
Loading history...
161
            $this->pubkey = isset($recaptcha_opt['pubkey']) ? $recaptcha_opt['pubkey'] : '';
162
            $privkey = isset($recaptcha_opt['privkey']) ? $recaptcha_opt['privkey'] : $privkey;
163
            $re_lang = isset($recaptcha_opt['re_lang']) ? $recaptcha_opt['re_lang'] : $re_lang;
164
        }
165
166
        if ( ! isset($this->re_msg) || empty($this->re_msg) ) {
167
            $this->re_msg = __( 'The reCAPTCHA was not entered correctly', 'formidable' );
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

167
            $this->re_msg = /** @scrutinizer ignore-call */ __( 'The reCAPTCHA was not entered correctly', 'formidable' );
Loading history...
168
        }
169
170
        if ( ! isset($this->privkey) ) {
171
            $this->privkey = $privkey;
172
        }
173
174
        if ( ! isset($this->re_lang) ) {
175
            $this->re_lang = $re_lang;
176
        }
177
178
		if ( ! isset( $this->re_type ) ) {
179
			$this->re_type = '';
180
		}
181
    }
182
183
    public function validate( $params, $errors ) {
184
        return apply_filters( 'frm_validate_settings', $errors, $params );
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

184
        return /** @scrutinizer ignore-call */ apply_filters( 'frm_validate_settings', $errors, $params );
Loading history...
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);
0 ignored issues
show
Bug introduced by
The function update_site_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

192
            /** @scrutinizer ignore-call */ 
193
            update_site_option('frm_admin_menu_name', $this->menu);
Loading history...
193
        } else if ( current_user_can('administrator') ) {
0 ignored issues
show
Bug introduced by
The function current_user_can was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

193
        } else if ( /** @scrutinizer ignore-call */ current_user_can('administrator') ) {
Loading history...
194
            update_site_option('frm_admin_menu_name', false);
195
        }
196
197
        $this->update_roles($params);
198
199
        do_action( 'frm_update_settings', $params );
0 ignored issues
show
Bug introduced by
The function do_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

199
        /** @scrutinizer ignore-call */ 
200
        do_action( 'frm_update_settings', $params );
Loading history...
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 ) {
209
        $this->mu_menu = isset($params['frm_mu_menu']) ? $params['frm_mu_menu'] : 0;
210
211
        $this->pubkey = trim($params['frm_pubkey']);
212
        $this->privkey = $params['frm_privkey'];
213
		$this->re_type = $params['frm_re_type'];
214
        $this->re_lang = $params['frm_re_lang'];
215
		$this->re_multi = isset( $params['frm_re_multi'] ) ? $params['frm_re_multi'] : 0;
216
217
        $this->load_style = $params['frm_load_style'];
218
219
        $this->use_html = isset($params['frm_use_html']) ? $params['frm_use_html'] : 0;
220
		$this->jquery_css = isset( $params['frm_jquery_css'] ) ? absint( $params['frm_jquery_css'] ) : 0;
0 ignored issues
show
Bug introduced by
The function absint was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

220
		$this->jquery_css = isset( $params['frm_jquery_css'] ) ? /** @scrutinizer ignore-call */ absint( $params['frm_jquery_css'] ) : 0;
Loading history...
221
		$this->accordion_js = isset( $params['frm_accordion_js'] ) ? absint( $params['frm_accordion_js'] ) : 0;
222
		$this->fade_form = isset( $params['frm_fade_form'] ) ? absint( $params['frm_fade_form'] ) : 0;
223
		$this->old_css   = isset( $params['frm_old_css'] ) ? absint( $params['frm_old_css'] ) : 0;
224
		$this->no_ips = isset( $params['frm_no_ips'] ) ? absint( $params['frm_no_ips'] ) : 0;
225
    }
226
227
	private function update_roles( $params ) {
228
        global $wp_roles;
229
230
        $frm_roles = FrmAppHelper::frm_capabilities();
231
        $roles = get_editable_roles();
0 ignored issues
show
Bug introduced by
The function get_editable_roles was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

231
        $roles = /** @scrutinizer ignore-call */ get_editable_roles();
Loading history...
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() {
251
        // Save the posted value in the database
252
253
        update_option('frm_options', $this);
0 ignored issues
show
Bug introduced by
The function update_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

253
        /** @scrutinizer ignore-call */ 
254
        update_option('frm_options', $this);
Loading history...
254
255
        delete_transient('frm_options');
0 ignored issues
show
Bug introduced by
The function delete_transient was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

255
        /** @scrutinizer ignore-call */ 
256
        delete_transient('frm_options');
Loading history...
256
        set_transient('frm_options', $this);
0 ignored issues
show
Bug introduced by
The function set_transient was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

256
        /** @scrutinizer ignore-call */ 
257
        set_transient('frm_options', $this);
Loading history...
257
258
        do_action( 'frm_store_settings' );
0 ignored issues
show
Bug introduced by
The function do_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

258
        /** @scrutinizer ignore-call */ 
259
        do_action( 'frm_store_settings' );
Loading history...
259
    }
260
}
261