Completed
Push — master ( f34f00...328911 )
by Stephanie
02:55
created

FrmSettings::update_settings()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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