PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.0.1
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.0.1
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / classes / models / FrmSettings.php

FrmSettings.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.0.1, at classes/models/FrmSettings.php

365 lines 10.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 die( 'You are not allowed to call this page directly.' );
4 }
5
6 class FrmSettings {
7 public $option_name = 'frm_options';
8 public $menu;
9 public $mu_menu;
10 public $use_html;
11 public $jquery_css;
12 public $accordion_js;
13 public $fade_form;
14 public $old_css;
15 public $admin_bar;
16
17 public $success_msg;
18 public $blank_msg;
19 public $unique_msg;
20 public $invalid_msg;
21 public $failed_msg;
22 public $submit_value;
23 public $login_msg;
24 public $admin_permission;
25
26 public $email_to;
27 public $load_style;
28 public $custom_style;
29
30 public $active_captcha;
31 public $hcaptcha_pubkey;
32 public $hcaptcha_privkey;
33 public $pubkey;
34 public $privkey;
35 public $re_lang;
36 public $re_type;
37 public $re_msg;
38 public $re_multi;
39
40 public $no_ips;
41 public $current_form = 0;
42 public $tracking;
43
44 /**
45 * @since 6.0
46 *
47 * @var string|false|null
48 */
49 public $custom_css;
50
51 public function __construct( $args = array() ) {
52 if ( ! defined( 'ABSPATH' ) ) {
53 die( 'You are not allowed to call this page directly.' );
54 }
55
56 $settings = get_transient( $this->option_name );
57
58 if ( ! is_object( $settings ) ) {
59 $settings = $this->translate_settings( $settings );
60 }
61
62 foreach ( $settings as $setting_name => $setting ) {
63 $this->{$setting_name} = $setting;
64 unset( $setting_name, $setting );
65 }
66
67 $this->set_default_options();
68
69 $this->maybe_filter_for_form( $args );
70 }
71
72 private function translate_settings( $settings ) {
73 if ( $settings ) { //workaround for W3 total cache conflict
74 return unserialize( serialize( $settings ) );
75 }
76
77 $settings = get_option( $this->option_name );
78 if ( is_object( $settings ) ) {
79 set_transient( $this->option_name, $settings );
80
81 return $settings;
82 }
83
84 // If unserializing didn't work
85 if ( $settings ) { //workaround for W3 total cache conflict
86 $settings = unserialize( serialize( $settings ) );
87 } else {
88 $settings = $this;
89 }
90
91 update_option( $this->option_name, $settings );
92 set_transient( $this->option_name, $settings );
93
94 return $settings;
95 }
96
97 /**
98 * @return array
99 */
100 public function default_options() {
101 return array(
102 'menu' => apply_filters( 'frm_default_menu', 'Formidable' ),
103 'mu_menu' => 0,
104 'use_html' => true,
105 'jquery_css' => false,
106 'accordion_js' => false,
107 'fade_form' => false,
108 'old_css' => false,
109 'admin_bar' => false,
110
111 're_multi' => 1,
112
113 'success_msg' => __( 'Your responses were successfully submitted. Thank you!', 'formidable' ),
114 'blank_msg' => __( 'This field cannot be blank.', 'formidable' ),
115 'unique_msg' => __( 'This value must be unique.', 'formidable' ),
116 'invalid_msg' => __( 'There was a problem with your submission. Errors are marked below.', 'formidable' ),
117 'failed_msg' => __( 'We\'re sorry. It looks like you\'ve already submitted that.', 'formidable' ),
118 'submit_value' => __( 'Submit', 'formidable' ),
119 'login_msg' => __( 'You do not have permission to view this form.', 'formidable' ),
120 'admin_permission' => __( 'You do not have permission to do that', 'formidable' ),
121
122 'email_to' => '[admin_email]',
123 'no_ips' => 0,
124 'tracking' => FrmAppHelper::pro_is_installed(),
125
126 'custom_css' => false,
127 );
128 }
129
130 private function set_default_options() {
131 $this->fill_captcha_settings();
132
133 if ( ! isset( $this->load_style ) ) {
134 if ( ! isset( $this->custom_style ) ) {
135 $this->custom_style = true;
136 }
137
138 $this->load_style = 'all';
139 }
140
141 $this->fill_with_defaults();
142
143 if ( is_multisite() && is_admin() ) {
144 $mu_menu = get_site_option( 'frm_admin_menu_name' );
145 if ( $mu_menu && ! empty( $mu_menu ) ) {
146 $this->menu = $mu_menu;
147 $this->mu_menu = 1;
148 }
149 }
150
151 $frm_roles = FrmAppHelper::frm_capabilities( 'pro' );
152 foreach ( $frm_roles as $frm_role => $frm_role_description ) {
153 if ( ! isset( $this->$frm_role ) ) {
154 $this->$frm_role = 'administrator';
155 }
156 }
157 }
158
159 /**
160 * @param array $params
161 * @return void
162 */
163 public function fill_with_defaults( $params = array() ) {
164 $settings = $this->default_options();
165 $filter_html = ! FrmAppHelper::allow_unfiltered_html();
166
167 if ( $filter_html ) {
168 $filter_keys = array( 'failed_msg', 'blank_msg', 'invalid_msg', 'admin_permission', 'unique_msg', 'success_msg', 'submit_value', 'login_msg', 'menu' );
169 if ( ! empty( $params['additional_filter_keys'] ) ) {
170 $filter_keys = array_merge( $filter_keys, $params['additional_filter_keys'] );
171 }
172 } else {
173 $filter_keys = array();
174 }
175
176 foreach ( $settings as $setting => $default ) {
177 if ( isset( $params[ 'frm_' . $setting ] ) ) {
178 $this->{$setting} = $params[ 'frm_' . $setting ];
179 } elseif ( ! isset( $this->{$setting} ) ) {
180 $this->{$setting} = $default;
181 }
182
183 if ( $setting === 'menu' && empty( $this->{$setting} ) ) {
184 $this->{$setting} = $default;
185 }
186
187 $this->{$setting} = $this->maybe_sanitize_global_setting( $this->{$setting}, $setting, $filter_keys );
188 unset( $setting, $default );
189 }
190 }
191
192 /**
193 * Handle sanitizing for a target global setting key.
194 *
195 * @since 6.0
196 *
197 * @param mixed $value The unsanitized global setting value.
198 * @param string $key The key of the global setting being saved.
199 * @param array $filter_keys These keys that are filtered with kses.
200 * @return mixed
201 */
202 private function maybe_sanitize_global_setting( $value, $key, $filter_keys ) {
203 if ( 'custom_css' === $key ) {
204 if ( false === $value ) {
205 // Avoid changing the false default value to an empty string.
206 return $value;
207 }
208 return sanitize_textarea_field( $value );
209 }
210
211 if ( in_array( $key, $filter_keys, true ) ) {
212 return FrmAppHelper::kses( $value, 'all' );
213 }
214
215 return $value;
216 }
217
218 private function fill_captcha_settings() {
219 if ( ! isset( $this->active_captcha ) ) {
220 $this->active_captcha = 'recaptcha';
221 }
222
223 $privkey = '';
224 $re_lang = '';
225
226 if ( ! isset( $this->hcaptcha_privkey ) ) {
227 $this->hcaptcha_privkey = '';
228 }
229
230 if ( ! isset( $this->pubkey ) ) {
231 // get the options from the database
232 $recaptcha_opt = is_multisite() ? get_site_option( 'recaptcha' ) : get_option( 'recaptcha' );
233 $this->pubkey = isset( $recaptcha_opt['pubkey'] ) ? $recaptcha_opt['pubkey'] : '';
234 $privkey = isset( $recaptcha_opt['privkey'] ) ? $recaptcha_opt['privkey'] : $privkey;
235 $re_lang = isset( $recaptcha_opt['re_lang'] ) ? $recaptcha_opt['re_lang'] : $re_lang;
236 }
237
238 if ( ! isset( $this->re_msg ) || empty( $this->re_msg ) ) {
239 $this->re_msg = __( 'The CAPTCHA was not entered correctly', 'formidable' );
240 }
241
242 if ( ! isset( $this->privkey ) ) {
243 $this->privkey = $privkey;
244 }
245
246 if ( ! isset( $this->re_lang ) ) {
247 $this->re_lang = $re_lang;
248 }
249
250 if ( ! isset( $this->re_type ) ) {
251 $this->re_type = '';
252 }
253
254 if ( ! isset( $this->re_threshold ) ) {
255 $this->re_threshold = .5;
256 }
257 }
258
259 /**
260 * Get values that may be shown on the front-end without an override in the form settings.
261 *
262 * @since 3.06.01
263 */
264 public function translatable_strings() {
265 return array(
266 'invalid_msg',
267 'admin_permission',
268 'failed_msg',
269 'login_msg',
270 );
271 }
272
273 /**
274 * Allow strings to be filtered when a specific form may be displaying them.
275 *
276 * @since 3.06.01
277 */
278 public function maybe_filter_for_form( $args ) {
279 if ( isset( $args['current_form'] ) && is_numeric( $args['current_form'] ) ) {
280 $this->current_form = $args['current_form'];
281 foreach ( $this->translatable_strings() as $string ) {
282 $this->{$string} = apply_filters( 'frm_global_setting', $this->{$string}, $string, $this );
283 $this->{$string} = apply_filters( 'frm_global_' . $string, $this->{$string}, $this );
284 }
285 }
286 }
287
288 public function validate( $params, $errors ) {
289 return apply_filters( 'frm_validate_settings', $errors, $params );
290 }
291
292 public function update( $params ) {
293 $this->fill_with_defaults( $params );
294 $this->update_settings( $params );
295
296 if ( $this->mu_menu ) {
297 update_site_option( 'frm_admin_menu_name', $this->menu );
298 } elseif ( current_user_can( 'administrator' ) ) {
299 update_site_option( 'frm_admin_menu_name', false );
300 }
301
302 $this->update_roles( $params );
303
304 do_action( 'frm_update_settings', $params );
305
306 if ( function_exists( 'get_filesystem_method' ) ) {
307 // Save styling settings in case fallback setting changes.
308 $frm_style = new FrmStyle();
309 $frm_style->update( 'default' );
310 }
311 }
312
313 private function update_settings( $params ) {
314 $this->active_captcha = $params['frm_active_captcha'];
315 $this->hcaptcha_pubkey = trim( $params['frm_hcaptcha_pubkey'] );
316 $this->hcaptcha_privkey = $params['frm_hcaptcha_privkey'];
317 $this->pubkey = trim( $params['frm_pubkey'] );
318 $this->privkey = $params['frm_privkey'];
319 $this->re_type = $params['frm_re_type'];
320 $this->re_lang = $params['frm_re_lang'];
321 $this->re_threshold = floatval( $params['frm_re_threshold'] );
322 $this->load_style = $params['frm_load_style'];
323 $this->custom_css = $params['frm_custom_css'];
324
325 $checkboxes = array( 'mu_menu', 're_multi', 'use_html', 'jquery_css', 'accordion_js', 'fade_form', 'no_ips', 'tracking', 'admin_bar' );
326 foreach ( $checkboxes as $set ) {
327 $this->$set = isset( $params[ 'frm_' . $set ] ) ? $params[ 'frm_' . $set ] : 0;
328 }
329 }
330
331 private function update_roles( $params ) {
332 global $wp_roles;
333
334 $frm_roles = FrmAppHelper::frm_capabilities();
335 $roles = get_editable_roles();
336 foreach ( $frm_roles as $frm_role => $frm_role_description ) {
337 $this->$frm_role = (array) ( isset( $params[ $frm_role ] ) ? $params[ $frm_role ] : 'administrator' );
338
339 // Make sure administrators always have permissions
340 if ( ! in_array( 'administrator', $this->$frm_role ) ) {
341 array_push( $this->$frm_role, 'administrator' );
342 }
343
344 foreach ( $roles as $role => $details ) {
345 if ( in_array( $role, $this->$frm_role ) ) {
346 $wp_roles->add_cap( $role, $frm_role );
347 } else {
348 $wp_roles->remove_cap( $role, $frm_role );
349 }
350 }
351 }
352 }
353
354 public function store() {
355 // Save the posted value in the database
356
357 update_option( 'frm_options', $this );
358
359 delete_transient( 'frm_options' );
360 set_transient( 'frm_options', $this );
361
362 do_action( 'frm_store_settings' );
363 }
364 }
365