PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.12
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.12
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.12, at classes/models/FrmSettings.php

477 lines 12.7 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 #[\AllowDynamicProperties]
7 class FrmSettings {
8 public $option_name = 'frm_options';
9 public $menu;
10 public $mu_menu;
11 public $use_html;
12 public $jquery_css;
13 public $accordion_js;
14 public $fade_form;
15 public $old_css;
16 public $admin_bar;
17
18 public $success_msg;
19 public $blank_msg;
20 public $unique_msg;
21 public $invalid_msg;
22 public $failed_msg;
23 public $submit_value;
24 public $login_msg;
25 public $admin_permission;
26
27 public $email_to;
28 public $load_style;
29 public $custom_style;
30
31 public $active_captcha;
32
33 /**
34 * Settings for reCAPTCHA.
35 */
36
37 /**
38 * @var string|null
39 */
40 public $pubkey;
41
42 /**
43 * @var string|null
44 */
45 public $privkey;
46 public $re_lang;
47 public $re_type;
48 public $re_msg;
49 public $re_multi;
50
51 /**
52 * Settings for hCaptcha.
53 */
54
55 /**
56 * @var string
57 */
58 public $hcaptcha_pubkey;
59
60 /**
61 * @var string|null
62 */
63 public $hcaptcha_privkey;
64
65 /**
66 * Settings for Turnstile.
67 */
68
69 /**
70 * @var string
71 */
72 public $turnstile_pubkey;
73
74 /**
75 * @var string|null
76 */
77 public $turnstile_privkey;
78
79 public $no_ips;
80 public $custom_header_ip;
81 public $current_form = 0;
82 public $tracking;
83 public $summary_emails;
84 public $summary_emails_recipients;
85
86 public $default_email;
87 public $currency;
88
89 /**
90 * @since 6.0
91 *
92 * @var false|string|null
93 */
94 public $custom_css;
95
96 public function __construct( $args = array() ) {
97 if ( ! defined( 'ABSPATH' ) ) {
98 die( 'You are not allowed to call this page directly.' );
99 }
100
101 $settings = get_option( $this->option_name );
102
103 if ( ! is_object( $settings ) ) {
104 $settings = $this->translate_settings( $settings );
105 }
106
107 foreach ( $settings as $setting_name => $setting ) {
108 $this->{$setting_name} = $setting;
109 unset( $setting_name, $setting );
110 }
111
112 $this->set_default_options();
113
114 $this->maybe_filter_for_form( $args );
115 }
116
117 private function translate_settings( $settings ) {
118 if ( $settings ) {
119 // Workaround for W3 total cache conflict.
120 return unserialize( serialize( $settings ) );
121 }
122
123 // If unserializing didn't work.
124 $settings = $this;
125
126 update_option( $this->option_name, $settings, 'yes' );
127
128 return $settings;
129 }
130
131 /**
132 * @return array
133 */
134 public function default_options() {
135 return array(
136 'menu' => apply_filters( 'frm_default_menu', 'Formidable' ),
137 'mu_menu' => 0,
138 'use_html' => true,
139 'jquery_css' => false,
140 'accordion_js' => false,
141 'fade_form' => false,
142 'old_css' => false,
143 'admin_bar' => false,
144
145 're_multi' => 1,
146
147 'success_msg' => __( 'Your responses were successfully submitted. Thank you!', 'formidable' ),
148 // translators: %s: [field_name] shortcode.
149 'blank_msg' => sprintf( __( '%s cannot be blank.', 'formidable' ), '[field_name]' ),
150 // translators: %s: [field_name] shortcode.
151 'unique_msg' => sprintf( __( '%s must be unique.', 'formidable' ), '[field_name]' ),
152 'invalid_msg' => __( 'There was a problem with your submission. Errors are marked below.', 'formidable' ),
153 'failed_msg' => __( 'We\'re sorry. It looks like you\'ve already submitted that.', 'formidable' ),
154 'submit_value' => __( 'Submit', 'formidable' ),
155 'login_msg' => __( 'You do not have permission to view this form.', 'formidable' ),
156 'admin_permission' => __( 'You do not have permission to do that', 'formidable' ),
157 'new_tab_msg' => __( 'The page has been opened in a new tab.', 'formidable' ),
158
159 'email_to' => '[admin_email]',
160 'no_ips' => 0,
161 // Use false by default. We show a warning when this is unset. Once global settings have been saved, this gets saved.
162 'custom_header_ip' => false,
163 'tracking' => FrmAppHelper::pro_is_installed(),
164 // Only enable this by default for the main site.
165 'summary_emails' => get_current_blog_id() === get_main_site_id(),
166 'summary_emails_recipients' => '[admin_email]',
167
168 // Normally custom CSS is a string. A false value is used when nothing has been set.
169 // When it is false, we try to use the old custom_key value from the default style's post_content array.
170 'custom_css' => false,
171 );
172 }
173
174 /**
175 * @return void
176 */
177 private function set_default_options() {
178 $this->fill_captcha_settings();
179
180 if ( ! isset( $this->load_style ) ) {
181 if ( ! isset( $this->custom_style ) ) {
182 $this->custom_style = true;
183 }
184
185 $this->load_style = 'all';
186 }
187
188 $this->fill_with_defaults();
189
190 if ( is_multisite() && is_admin() ) {
191 $mu_menu = get_site_option( 'frm_admin_menu_name' );
192 if ( $mu_menu && ! empty( $mu_menu ) ) {
193 $this->menu = $mu_menu;
194 $this->mu_menu = 1;
195 }
196 }
197
198 $frm_roles = FrmAppHelper::frm_capabilities( 'pro' );
199 foreach ( $frm_roles as $frm_role => $frm_role_description ) {
200 if ( ! isset( $this->$frm_role ) ) {
201 $this->$frm_role = 'administrator';
202 }
203 }
204
205 if ( ! isset( $this->default_email ) ) {
206 $this->default_email = get_option( 'admin_email' );
207 }
208
209 if ( ! isset( $this->currency ) ) {
210 $this->currency = 'USD';
211 }
212 }
213
214 /**
215 * @param array $params
216 * @return void
217 */
218 public function fill_with_defaults( $params = array() ) {
219 $settings = $this->default_options();
220 $filter_html = ! FrmAppHelper::allow_unfiltered_html();
221
222 if ( $filter_html ) {
223 $filter_keys = array( 'failed_msg', 'blank_msg', 'invalid_msg', 'admin_permission', 'unique_msg', 'success_msg', 'submit_value', 'login_msg', 'menu' );
224 if ( ! empty( $params['additional_filter_keys'] ) ) {
225 $filter_keys = array_merge( $filter_keys, $params['additional_filter_keys'] );
226 }
227 } else {
228 $filter_keys = array();
229 }
230
231 foreach ( $settings as $setting => $default ) {
232 if ( isset( $params[ 'frm_' . $setting ] ) ) {
233 $this->{$setting} = $params[ 'frm_' . $setting ];
234 } elseif ( ! isset( $this->{$setting} ) ) {
235 $this->{$setting} = $default;
236 }
237
238 if ( $setting === 'menu' && empty( $this->{$setting} ) ) {
239 $this->{$setting} = $default;
240 }
241
242 $this->{$setting} = $this->maybe_sanitize_global_setting( $this->{$setting}, $setting, $filter_keys );
243 unset( $setting, $default );
244 }
245 }
246
247 /**
248 * Handle sanitizing for a target global setting key.
249 *
250 * @since 6.0
251 *
252 * @param mixed $value The unsanitized global setting value.
253 * @param string $key The key of the global setting being saved.
254 * @param array $filter_keys These keys that are filtered with kses.
255 * @return mixed
256 */
257 private function maybe_sanitize_global_setting( $value, $key, $filter_keys ) {
258 if ( 'custom_css' === $key ) {
259 if ( false === $value ) {
260 // Avoid changing the false default value to an empty string.
261 return $value;
262 }
263 return sanitize_textarea_field( $value );
264 }
265
266 if ( in_array( $key, $filter_keys, true ) ) {
267 return FrmAppHelper::kses( $value, 'all' );
268 }
269
270 return $value;
271 }
272
273 /**
274 * @return void
275 */
276 private function fill_captcha_settings() {
277 if ( ! isset( $this->active_captcha ) ) {
278 $this->active_captcha = 'recaptcha';
279 }
280
281 $privkey = '';
282 $re_lang = '';
283
284 if ( ! isset( $this->hcaptcha_privkey ) ) {
285 $this->hcaptcha_privkey = '';
286 }
287
288 if ( ! isset( $this->turnstile_privkey ) ) {
289 $this->turnstile_privkey = '';
290 }
291
292 if ( ! isset( $this->pubkey ) ) {
293 // Get the options from the database.
294 $recaptcha_opt = is_multisite() ? get_site_option( 'recaptcha' ) : get_option( 'recaptcha' );
295 $this->pubkey = isset( $recaptcha_opt['pubkey'] ) ? $recaptcha_opt['pubkey'] : '';
296 $privkey = isset( $recaptcha_opt['privkey'] ) ? $recaptcha_opt['privkey'] : $privkey;
297 $re_lang = isset( $recaptcha_opt['re_lang'] ) ? $recaptcha_opt['re_lang'] : $re_lang;
298 }
299
300 if ( empty( $this->re_msg ) ) {
301 $this->re_msg = __( 'The CAPTCHA was not entered correctly', 'formidable' );
302 }
303
304 if ( ! isset( $this->privkey ) ) {
305 $this->privkey = $privkey;
306 }
307
308 if ( ! isset( $this->re_lang ) ) {
309 $this->re_lang = $re_lang;
310 }
311
312 if ( ! isset( $this->re_type ) ) {
313 $this->re_type = '';
314 }
315
316 if ( ! isset( $this->re_threshold ) ) {
317 $this->re_threshold = .5;
318 }
319 }
320
321 /**
322 * Get values that may be shown on the front-end without an override in the form settings.
323 *
324 * @since 3.06.01
325 *
326 * @return string[]
327 */
328 public function translatable_strings() {
329 return array(
330 'invalid_msg',
331 'admin_permission',
332 'failed_msg',
333 'login_msg',
334 );
335 }
336
337 /**
338 * Allow strings to be filtered when a specific form may be displaying them.
339 *
340 * @since 3.06.01
341 *
342 * @return void
343 */
344 public function maybe_filter_for_form( $args ) {
345 if ( isset( $args['current_form'] ) && is_numeric( $args['current_form'] ) ) {
346 $this->current_form = $args['current_form'];
347 foreach ( $this->translatable_strings() as $string ) {
348 $this->{$string} = apply_filters( 'frm_global_setting', $this->{$string}, $string, $this );
349 $this->{$string} = apply_filters( 'frm_global_' . $string, $this->{$string}, $this );
350 }
351 }
352 }
353
354 /**
355 * @param array $params
356 * @param array $errors
357 */
358 public function validate( $params, $errors ) {
359 return apply_filters( 'frm_validate_settings', $errors, $params );
360 }
361
362 /**
363 * @param array $params
364 *
365 * @return void
366 */
367 public function update( $params ) {
368 $this->fill_with_defaults( $params );
369 $this->update_settings( $params );
370
371 if ( $this->mu_menu ) {
372 update_site_option( 'frm_admin_menu_name', $this->menu );
373 } elseif ( current_user_can( 'administrator' ) ) {
374 update_site_option( 'frm_admin_menu_name', false );
375 }
376
377 $this->update_roles( $params );
378
379 do_action( 'frm_update_settings', $params );
380
381 if ( function_exists( 'get_filesystem_method' ) ) {
382 // Save styling settings in case fallback setting changes.
383 $frm_style = new FrmStyle();
384 $frm_style->update( 'default' );
385 }
386 }
387
388 /**
389 * @param array $params
390 * @return void
391 */
392 private function update_settings( $params ) {
393 $this->active_captcha = $params['frm_active_captcha'];
394 $this->pubkey = trim( $params['frm_pubkey'] );
395 $this->privkey = trim( $params['frm_privkey'] );
396 $this->re_type = $params['frm_re_type'];
397 $this->re_lang = $params['frm_re_lang'];
398 $this->re_threshold = floatval( $params['frm_re_threshold'] );
399 $this->hcaptcha_pubkey = trim( $params['frm_hcaptcha_pubkey'] );
400 $this->hcaptcha_privkey = trim( $params['frm_hcaptcha_privkey'] );
401 $this->turnstile_pubkey = trim( $params['frm_turnstile_pubkey'] );
402 $this->turnstile_privkey = trim( $params['frm_turnstile_privkey'] );
403 $this->load_style = $params['frm_load_style'];
404 $this->custom_css = $params['frm_custom_css'];
405 $this->default_email = $params['frm_default_email'];
406 $this->currency = $params['frm_currency'];
407
408 $checkboxes = array( 'mu_menu', 're_multi', 'use_html', 'jquery_css', 'accordion_js', 'fade_form', 'no_ips', 'custom_header_ip', 'tracking', 'admin_bar', 'summary_emails' );
409 foreach ( $checkboxes as $set ) {
410 $this->$set = isset( $params[ 'frm_' . $set ] ) ? absint( $params[ 'frm_' . $set ] ) : 0;
411 }
412 }
413
414 /**
415 * @return void
416 */
417 private function update_roles( $params ) {
418 global $wp_roles;
419
420 $frm_roles = FrmAppHelper::frm_capabilities();
421 $roles = get_editable_roles();
422 foreach ( $frm_roles as $frm_role => $frm_role_description ) {
423 $this->$frm_role = (array) ( isset( $params[ $frm_role ] ) ? $params[ $frm_role ] : 'administrator' );
424
425 // Make sure administrators always have permissions
426 if ( ! in_array( 'administrator', $this->$frm_role, true ) ) {
427 array_push( $this->$frm_role, 'administrator' );
428 }
429
430 foreach ( $roles as $role => $details ) {
431 if ( in_array( $role, $this->$frm_role ) ) {
432 $wp_roles->add_cap( $role, $frm_role );
433 } else {
434 $wp_roles->remove_cap( $role, $frm_role );
435 }
436 }
437 }
438 }
439
440 /**
441 * Updates a single setting with specified sanitization.
442 *
443 * @since 6.9
444 *
445 * @param string $key The setting key to update.
446 * @param mixed $value The new value for the setting.
447 * @param string $sanitize The name of the sanitization function to apply to the new value.
448 * @return bool True on success, false on failure.
449 */
450 public function update_setting( $key, $value, $sanitize ) {
451 if ( ! property_exists( $this, $key ) || ! is_callable( $sanitize ) ) {
452 // Setting does not exist or sanitization function name is not callable.
453 return false;
454 }
455
456 // Update the property value.
457 FrmAppHelper::sanitize_value( $sanitize, $value );
458 $this->{$key} = $value;
459
460 return true;
461 }
462
463 /**
464 * @return void
465 */
466 public function store() {
467 // Save the posted value in the database
468
469 update_option( 'frm_options', $this );
470
471 delete_transient( 'frm_options' );
472 set_transient( 'frm_options', $this );
473
474 do_action( 'frm_store_settings' );
475 }
476 }
477