Completed
Push — master ( 56ab45...14b462 )
by Stephanie
03:20
created

FrmSettings::fill_with_defaults()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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