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

478 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 $from_email;
88 public $currency;
89
90 /**
91 * @since 6.0
92 *
93 * @var false|string|null
94 */
95 public $custom_css;
96
97 public function __construct( $args = array() ) {
98 if ( ! defined( 'ABSPATH' ) ) {
99 die( 'You are not allowed to call this page directly.' );
100 }
101
102 $settings = get_option( $this->option_name );
103
104 if ( ! is_object( $settings ) ) {
105 $settings = $this->translate_settings( $settings );
106 }
107
108 foreach ( $settings as $setting_name => $setting ) {
109 $this->{$setting_name} = $setting;
110 unset( $setting_name, $setting );
111 }
112
113 $this->set_default_options();
114
115 $this->maybe_filter_for_form( $args );
116 }
117
118 private function translate_settings( $settings ) {
119 if ( $settings ) {
120 // Workaround for W3 total cache conflict.
121 return unserialize( serialize( $settings ) );
122 }
123
124 // If unserializing didn't work.
125 $settings = $this;
126
127 update_option( $this->option_name, $settings, 'yes' );
128
129 return $settings;
130 }
131
132 /**
133 * @return array
134 */
135 public function default_options() {
136 return array(
137 'menu' => apply_filters( 'frm_default_menu', 'Formidable' ),
138 'mu_menu' => 0,
139 'use_html' => true,
140 'jquery_css' => false,
141 'accordion_js' => false,
142 'fade_form' => false,
143 'old_css' => false,
144 'admin_bar' => false,
145
146 're_multi' => 1,
147
148 'success_msg' => __( 'Your responses were successfully submitted. Thank you!', 'formidable' ),
149 // translators: %s: [field_name] shortcode.
150 'blank_msg' => sprintf( __( '%s cannot be blank.', 'formidable' ), '[field_name]' ),
151 // translators: %s: [field_name] shortcode.
152 'unique_msg' => sprintf( __( '%s must be unique.', 'formidable' ), '[field_name]' ),
153 'invalid_msg' => __( 'There was a problem with your submission. Errors are marked below.', 'formidable' ),
154 'failed_msg' => __( 'We\'re sorry. It looks like you\'ve already submitted that.', 'formidable' ),
155 'submit_value' => __( 'Submit', 'formidable' ),
156 'login_msg' => __( 'You do not have permission to view this form.', 'formidable' ),
157 'admin_permission' => __( 'You do not have permission to do that', 'formidable' ),
158 'new_tab_msg' => __( 'The page has been opened in a new tab.', 'formidable' ),
159
160 'email_to' => '[admin_email]',
161 'no_ips' => 0,
162 'custom_header_ip' => 0,
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->from_email = $params['frm_from_email'];
407 $this->currency = $params['frm_currency'];
408
409 $checkboxes = array( 'mu_menu', 're_multi', 'use_html', 'jquery_css', 'accordion_js', 'fade_form', 'no_ips', 'custom_header_ip', 'tracking', 'admin_bar', 'summary_emails' );
410 foreach ( $checkboxes as $set ) {
411 $this->$set = isset( $params[ 'frm_' . $set ] ) ? absint( $params[ 'frm_' . $set ] ) : 0;
412 }
413 }
414
415 /**
416 * @return void
417 */
418 private function update_roles( $params ) {
419 global $wp_roles;
420
421 $frm_roles = FrmAppHelper::frm_capabilities();
422 $roles = get_editable_roles();
423 foreach ( $frm_roles as $frm_role => $frm_role_description ) {
424 $this->$frm_role = (array) ( isset( $params[ $frm_role ] ) ? $params[ $frm_role ] : 'administrator' );
425
426 // Make sure administrators always have permissions
427 if ( ! in_array( 'administrator', $this->$frm_role, true ) ) {
428 array_push( $this->$frm_role, 'administrator' );
429 }
430
431 foreach ( $roles as $role => $details ) {
432 if ( in_array( $role, $this->$frm_role ) ) {
433 $wp_roles->add_cap( $role, $frm_role );
434 } else {
435 $wp_roles->remove_cap( $role, $frm_role );
436 }
437 }
438 }
439 }
440
441 /**
442 * Updates a single setting with specified sanitization.
443 *
444 * @since 6.9
445 *
446 * @param string $key The setting key to update.
447 * @param mixed $value The new value for the setting.
448 * @param string $sanitize The name of the sanitization function to apply to the new value.
449 * @return bool True on success, false on failure.
450 */
451 public function update_setting( $key, $value, $sanitize ) {
452 if ( ! property_exists( $this, $key ) || ! is_callable( $sanitize ) ) {
453 // Setting does not exist or sanitization function name is not callable.
454 return false;
455 }
456
457 // Update the property value.
458 FrmAppHelper::sanitize_value( $sanitize, $value );
459 $this->{$key} = $value;
460
461 return true;
462 }
463
464 /**
465 * @return void
466 */
467 public function store() {
468 // Save the posted value in the database
469
470 update_option( 'frm_options', $this );
471
472 delete_transient( 'frm_options' );
473 set_transient( 'frm_options', $this );
474
475 do_action( 'frm_store_settings' );
476 }
477 }
478