Completed
Push — master ( 34a52e...661e23 )
by Stephanie
04:47
created

FrmSettings::update_settings()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 11
nc 32
nop 1
dl 0
loc 16
rs 8.8571
c 1
b 0
f 0
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
	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 ) {
194
        $this->mu_menu = isset($params['frm_mu_menu']) ? $params['frm_mu_menu'] : 0;
195
196
        $this->pubkey = trim($params['frm_pubkey']);
197
        $this->privkey = $params['frm_privkey'];
198
        $this->re_lang = $params['frm_re_lang'];
199
		$this->re_multi = isset( $params['frm_re_multi'] ) ? $params['frm_re_multi'] : 0;
200
201
        $this->load_style = $params['frm_load_style'];
202
        $this->preview_page_id = (int) $params['frm-preview-page-id'];
203
204
        $this->use_html = isset($params['frm_use_html']) ? $params['frm_use_html'] : 0;
205
        //$this->custom_style = isset($params['frm_custom_style']) ? $params['frm_custom_style'] : 0;
206
		$this->jquery_css = isset( $params['frm_jquery_css'] ) ? absint( $params['frm_jquery_css'] ) : 0;
207
		$this->accordion_js = isset( $params['frm_accordion_js'] ) ? absint( $params['frm_accordion_js'] ) : 0;
208
    }
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
218
            // Make sure administrators always have permissions
219
            if ( ! in_array( 'administrator', $this->$frm_role ) ) {
220
				array_push( $this->$frm_role, 'administrator' );
221
            }
222
223
            foreach ( $roles as $role => $details ) {
224
                if ( in_array($role, $this->$frm_role) ) {
225
    			    $wp_roles->add_cap( $role, $frm_role );
226
    			} else {
227
    			    $wp_roles->remove_cap( $role, $frm_role );
228
    			}
229
    		}
230
		}
231
    }
232
233
	public function store() {
234
        // Save the posted value in the database
235
236
        update_option('frm_options', $this);
237
238
        delete_transient('frm_options');
239
        set_transient('frm_options', $this);
240
241
        do_action( 'frm_store_settings' );
242
    }
243
}
244