Completed
Push — master ( 537290...572154 )
by Stephanie
02:56
created

FrmSettings::store()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 10
rs 9.9332
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
	public $current_form = 0;
35
36
	public function __construct( $args = array() ) {
37
		if ( ! defined( 'ABSPATH' ) ) {
38
			die( 'You are not allowed to call this page directly.' );
39
		}
40
41
		$settings = get_transient( $this->option_name );
42
43
		if ( ! is_object( $settings ) ) {
44
			$settings = $this->translate_settings( $settings );
45
		}
46
47
        foreach ( $settings as $setting_name => $setting ) {
48
            $this->{$setting_name} = $setting;
49
			unset( $setting_name, $setting );
50
        }
51
52
        $this->set_default_options();
53
54
		$this->maybe_filter_for_form( $args );
55
    }
56
57
	private function translate_settings( $settings ) {
58
        if ( $settings ) { //workaround for W3 total cache conflict
59
			return unserialize( serialize( $settings ) );
60
        }
61
62
		$settings = get_option( $this->option_name );
63
		if ( is_object( $settings ) ) {
64
			set_transient( $this->option_name, $settings );
65
			return $settings;
66
		}
67
68
        // If unserializing didn't work
69
        if ( $settings ) { //workaround for W3 total cache conflict
70
			$settings = unserialize( serialize( $settings ) );
71
        } else {
72
            $settings = $this;
73
        }
74
75
		update_option( $this->option_name, $settings );
76
		set_transient( $this->option_name, $settings );
77
78
        return $settings;
79
    }
80
81
    /**
82
     * @return array
83
     */
84
	public function default_options() {
85
        return array(
86
            'menu'      => apply_filters( 'frm_default_menu', 'Formidable' ),
87
            'mu_menu'   => 0,
88
            'use_html'  => true,
89
            'jquery_css' => false,
90
            'accordion_js' => false,
91
			'fade_form' => false,
92
			'old_css'   => true,
93
94
			're_multi'  => 0,
95
96
            'success_msg' => __( 'Your responses were successfully submitted. Thank you!', 'formidable' ),
97
            'blank_msg' => __( 'This field cannot be blank.', 'formidable' ),
98
            'unique_msg' => __( 'This value must be unique.', 'formidable' ),
99
            'invalid_msg' => __( 'There was a problem with your submission. Errors are marked below.', 'formidable' ),
100
            'failed_msg' => __( 'We\'re sorry. It looks like you\'ve already submitted that.', 'formidable' ),
101
            'submit_value' => __( 'Submit', 'formidable' ),
102
            'login_msg' => __( 'You do not have permission to view this form.', 'formidable' ),
103
            'admin_permission' => __( 'You do not have permission to do that', 'formidable' ),
104
105
            'email_to' => '[admin_email]',
106
			'no_ips'   => 0,
107
        );
108
    }
109
110
	private function set_default_options() {
111
        $this->fill_recaptcha_settings();
112
113
		if ( ! isset( $this->load_style ) ) {
114
			if ( ! isset( $this->custom_style ) ) {
115
                $this->custom_style = true;
116
            }
117
118
            $this->load_style = 'all';
119
        }
120
121
        $this->fill_with_defaults();
122
123
        if ( is_multisite() && is_admin() ) {
124
			$mu_menu = get_site_option( 'frm_admin_menu_name' );
125
			if ( $mu_menu && ! empty( $mu_menu ) ) {
126
                $this->menu = $mu_menu;
127
                $this->mu_menu = 1;
128
            }
129
        }
130
131
		$frm_roles = FrmAppHelper::frm_capabilities( 'pro' );
132
		foreach ( $frm_roles as $frm_role => $frm_role_description ) {
133
			if ( ! isset( $this->$frm_role ) ) {
134
                $this->$frm_role = 'administrator';
135
            }
136
        }
137
    }
138
139
	public function fill_with_defaults( $params = array() ) {
140
        $settings = $this->default_options();
141
142
        foreach ( $settings as $setting => $default ) {
143
			if ( isset( $params[ 'frm_' . $setting ] ) ) {
144
				$this->{$setting} = $params[ 'frm_' . $setting ];
145
			} elseif ( ! isset( $this->{$setting} ) ) {
146
                $this->{$setting} = $default;
147
            }
148
149
			if ( $setting == 'menu' && empty( $this->{$setting} ) ) {
150
				$this->{$setting} = $default;
151
			}
152
153
			unset( $setting, $default );
154
        }
155
    }
156
157
    private function fill_recaptcha_settings() {
158
        $privkey = '';
159
		$re_lang = '';
160
161
		if ( ! isset( $this->pubkey ) ) {
162
			// get the options from the database
163
			$recaptcha_opt = is_multisite() ? get_site_option( 'recaptcha' ) : get_option( 'recaptcha' );
164
			$this->pubkey = isset( $recaptcha_opt['pubkey'] ) ? $recaptcha_opt['pubkey'] : '';
165
			$privkey = isset( $recaptcha_opt['privkey'] ) ? $recaptcha_opt['privkey'] : $privkey;
166
			$re_lang = isset( $recaptcha_opt['re_lang'] ) ? $recaptcha_opt['re_lang'] : $re_lang;
167
		}
168
169
		if ( ! isset( $this->re_msg ) || empty( $this->re_msg ) ) {
170
            $this->re_msg = __( 'The reCAPTCHA was not entered correctly', 'formidable' );
171
        }
172
173
		if ( ! isset( $this->privkey ) ) {
174
            $this->privkey = $privkey;
175
        }
176
177
		if ( ! isset( $this->re_lang ) ) {
178
            $this->re_lang = $re_lang;
179
        }
180
181
		if ( ! isset( $this->re_type ) ) {
182
			$this->re_type = '';
183
		}
184
    }
185
186
	/**
187
	 * Get values that may be shown on the front-end without an override in the form settings.
188
	 *
189
	 * @since 3.06.01
190
	 */
191
	public function translatable_strings() {
192
		return array(
193
			'invalid_msg',
194
			'failed_msg',
195
			'login_msg'
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
196
		);
197
	}
198
199
	/**
200
	 * Allow strings to be filtered when a specific form may be displaying them.
201
	 *
202
	 * @since 3.06.01
203
	 */
204
	public function maybe_filter_for_form( $args ) {
205
		if ( isset( $args['current_form'] ) && is_numeric( $args['current_form'] ) ) {
206
			$this->current_form = $args['current_form'];
207
			foreach ( $this->translatable_strings() as $string ) {
208
				$this->{$string} = apply_filters( 'frm_global_setting', $this->{$string}, $string, $this );
209
				$this->{$string} = apply_filters( 'frm_global_' . $string, $this->{$string}, $this );
210
			}
211
		}
212
	}
213
214
    public function validate( $params, $errors ) {
215
        return apply_filters( 'frm_validate_settings', $errors, $params );
216
    }
217
218
	public function update( $params ) {
219
		$this->fill_with_defaults( $params );
220
		$this->update_settings( $params );
221
222
		if ( $this->mu_menu ) {
223
			update_site_option( 'frm_admin_menu_name', $this->menu );
224
		} elseif ( current_user_can( 'administrator' ) ) {
225
			update_site_option( 'frm_admin_menu_name', false );
226
		}
227
228
		$this->update_roles( $params );
229
230
        do_action( 'frm_update_settings', $params );
231
232
		if ( function_exists( 'get_filesystem_method' ) ) {
233
			// save styling settings in case fallback setting changes
234
			$frm_style = new FrmStyle();
235
			$frm_style->update( 'default' );
236
		}
237
    }
238
239
	private function update_settings( $params ) {
240
		$this->mu_menu = isset( $params['frm_mu_menu'] ) ? $params['frm_mu_menu'] : 0;
241
242
		$this->pubkey = trim( $params['frm_pubkey'] );
243
        $this->privkey = $params['frm_privkey'];
244
		$this->re_type = $params['frm_re_type'];
245
        $this->re_lang = $params['frm_re_lang'];
246
		$this->re_multi = isset( $params['frm_re_multi'] ) ? $params['frm_re_multi'] : 0;
247
248
        $this->load_style = $params['frm_load_style'];
249
250
		$this->use_html = isset( $params['frm_use_html'] ) ? $params['frm_use_html'] : 0;
251
		$this->jquery_css = isset( $params['frm_jquery_css'] ) ? absint( $params['frm_jquery_css'] ) : 0;
252
		$this->accordion_js = isset( $params['frm_accordion_js'] ) ? absint( $params['frm_accordion_js'] ) : 0;
253
		$this->fade_form = isset( $params['frm_fade_form'] ) ? absint( $params['frm_fade_form'] ) : 0;
254
		$this->old_css   = isset( $params['frm_old_css'] ) ? absint( $params['frm_old_css'] ) : 0;
255
		$this->no_ips = isset( $params['frm_no_ips'] ) ? absint( $params['frm_no_ips'] ) : 0;
256
    }
257
258
	private function update_roles( $params ) {
259
        global $wp_roles;
260
261
        $frm_roles = FrmAppHelper::frm_capabilities();
262
        $roles = get_editable_roles();
263
        foreach ( $frm_roles as $frm_role => $frm_role_description ) {
264
            $this->$frm_role = (array) ( isset( $params[ $frm_role ] ) ? $params[ $frm_role ] : 'administrator' );
265
266
            // Make sure administrators always have permissions
267
            if ( ! in_array( 'administrator', $this->$frm_role ) ) {
268
				array_push( $this->$frm_role, 'administrator' );
269
            }
270
271
            foreach ( $roles as $role => $details ) {
272
				if ( in_array( $role, $this->$frm_role ) ) {
273
    			    $wp_roles->add_cap( $role, $frm_role );
274
    			} else {
275
    			    $wp_roles->remove_cap( $role, $frm_role );
276
    			}
277
    		}
278
		}
279
    }
280
281
	public function store() {
282
		// Save the posted value in the database
283
284
		update_option( 'frm_options', $this );
285
286
		delete_transient( 'frm_options' );
287
		set_transient( 'frm_options', $this );
288
289
		do_action( 'frm_store_settings' );
290
	}
291
}
292