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

479 lines 12.8 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 // Use false by default. We show a warning when this is unset. Once global settings have been saved, this gets saved.
163 'custom_header_ip' => false,
164 'tracking' => FrmAppHelper::pro_is_installed(),
165 // Only enable this by default for the main site.
166 'summary_emails' => get_current_blog_id() === get_main_site_id(),
167 'summary_emails_recipients' => '[admin_email]',
168
169 // Normally custom CSS is a string. A false value is used when nothing has been set.
170 // When it is false, we try to use the old custom_key value from the default style's post_content array.
171 'custom_css' => false,
172 );
173 }
174
175 /**
176 * @return void
177 */
178 private function set_default_options() {
179 $this->fill_captcha_settings();
180
181 if ( ! isset( $this->load_style ) ) {
182 if ( ! isset( $this->custom_style ) ) {
183 $this->custom_style = true;
184 }
185
186 $this->load_style = 'all';
187 }
188
189 $this->fill_with_defaults();
190
191 if ( is_multisite() && is_admin() ) {
192 $mu_menu = get_site_option( 'frm_admin_menu_name' );
193 if ( $mu_menu && ! empty( $mu_menu ) ) {
194 $this->menu = $mu_menu;
195 $this->mu_menu = 1;
196 }
197 }
198
199 $frm_roles = FrmAppHelper::frm_capabilities( 'pro' );
200 foreach ( $frm_roles as $frm_role => $frm_role_description ) {
201 if ( ! isset( $this->$frm_role ) ) {
202 $this->$frm_role = 'administrator';
203 }
204 }
205
206 if ( ! isset( $this->default_email ) ) {
207 $this->default_email = get_option( 'admin_email' );
208 }
209
210 if ( ! isset( $this->currency ) ) {
211 $this->currency = 'USD';
212 }
213 }
214
215 /**
216 * @param array $params
217 * @return void
218 */
219 public function fill_with_defaults( $params = array() ) {
220 $settings = $this->default_options();
221 $filter_html = ! FrmAppHelper::allow_unfiltered_html();
222
223 if ( $filter_html ) {
224 $filter_keys = array( 'failed_msg', 'blank_msg', 'invalid_msg', 'admin_permission', 'unique_msg', 'success_msg', 'submit_value', 'login_msg', 'menu' );
225 if ( ! empty( $params['additional_filter_keys'] ) ) {
226 $filter_keys = array_merge( $filter_keys, $params['additional_filter_keys'] );
227 }
228 } else {
229 $filter_keys = array();
230 }
231
232 foreach ( $settings as $setting => $default ) {
233 if ( isset( $params[ 'frm_' . $setting ] ) ) {
234 $this->{$setting} = $params[ 'frm_' . $setting ];
235 } elseif ( ! isset( $this->{$setting} ) ) {
236 $this->{$setting} = $default;
237 }
238
239 if ( $setting === 'menu' && empty( $this->{$setting} ) ) {
240 $this->{$setting} = $default;
241 }
242
243 $this->{$setting} = $this->maybe_sanitize_global_setting( $this->{$setting}, $setting, $filter_keys );
244 unset( $setting, $default );
245 }
246 }
247
248 /**
249 * Handle sanitizing for a target global setting key.
250 *
251 * @since 6.0
252 *
253 * @param mixed $value The unsanitized global setting value.
254 * @param string $key The key of the global setting being saved.
255 * @param array $filter_keys These keys that are filtered with kses.
256 * @return mixed
257 */
258 private function maybe_sanitize_global_setting( $value, $key, $filter_keys ) {
259 if ( 'custom_css' === $key ) {
260 if ( false === $value ) {
261 // Avoid changing the false default value to an empty string.
262 return $value;
263 }
264 return sanitize_textarea_field( $value );
265 }
266
267 if ( in_array( $key, $filter_keys, true ) ) {
268 return FrmAppHelper::kses( $value, 'all' );
269 }
270
271 return $value;
272 }
273
274 /**
275 * @return void
276 */
277 private function fill_captcha_settings() {
278 if ( ! isset( $this->active_captcha ) ) {
279 $this->active_captcha = 'recaptcha';
280 }
281
282 $privkey = '';
283 $re_lang = '';
284
285 if ( ! isset( $this->hcaptcha_privkey ) ) {
286 $this->hcaptcha_privkey = '';
287 }
288
289 if ( ! isset( $this->turnstile_privkey ) ) {
290 $this->turnstile_privkey = '';
291 }
292
293 if ( ! isset( $this->pubkey ) ) {
294 // Get the options from the database.
295 $recaptcha_opt = is_multisite() ? get_site_option( 'recaptcha' ) : get_option( 'recaptcha' );
296 $this->pubkey = isset( $recaptcha_opt['pubkey'] ) ? $recaptcha_opt['pubkey'] : '';
297 $privkey = isset( $recaptcha_opt['privkey'] ) ? $recaptcha_opt['privkey'] : $privkey;
298 $re_lang = isset( $recaptcha_opt['re_lang'] ) ? $recaptcha_opt['re_lang'] : $re_lang;
299 }
300
301 if ( empty( $this->re_msg ) ) {
302 $this->re_msg = __( 'The CAPTCHA was not entered correctly', 'formidable' );
303 }
304
305 if ( ! isset( $this->privkey ) ) {
306 $this->privkey = $privkey;
307 }
308
309 if ( ! isset( $this->re_lang ) ) {
310 $this->re_lang = $re_lang;
311 }
312
313 if ( ! isset( $this->re_type ) ) {
314 $this->re_type = '';
315 }
316
317 if ( ! isset( $this->re_threshold ) ) {
318 $this->re_threshold = .5;
319 }
320 }
321
322 /**
323 * Get values that may be shown on the front-end without an override in the form settings.
324 *
325 * @since 3.06.01
326 *
327 * @return string[]
328 */
329 public function translatable_strings() {
330 return array(
331 'invalid_msg',
332 'admin_permission',
333 'failed_msg',
334 'login_msg',
335 );
336 }
337
338 /**
339 * Allow strings to be filtered when a specific form may be displaying them.
340 *
341 * @since 3.06.01
342 *
343 * @return void
344 */
345 public function maybe_filter_for_form( $args ) {
346 if ( isset( $args['current_form'] ) && is_numeric( $args['current_form'] ) ) {
347 $this->current_form = $args['current_form'];
348 foreach ( $this->translatable_strings() as $string ) {
349 $this->{$string} = apply_filters( 'frm_global_setting', $this->{$string}, $string, $this );
350 $this->{$string} = apply_filters( 'frm_global_' . $string, $this->{$string}, $this );
351 }
352 }
353 }
354
355 /**
356 * @param array $params
357 * @param array $errors
358 */
359 public function validate( $params, $errors ) {
360 return apply_filters( 'frm_validate_settings', $errors, $params );
361 }
362
363 /**
364 * @param array $params
365 *
366 * @return void
367 */
368 public function update( $params ) {
369 $this->fill_with_defaults( $params );
370 $this->update_settings( $params );
371
372 if ( $this->mu_menu ) {
373 update_site_option( 'frm_admin_menu_name', $this->menu );
374 } elseif ( current_user_can( 'administrator' ) ) {
375 update_site_option( 'frm_admin_menu_name', false );
376 }
377
378 $this->update_roles( $params );
379
380 do_action( 'frm_update_settings', $params );
381
382 if ( function_exists( 'get_filesystem_method' ) ) {
383 // Save styling settings in case fallback setting changes.
384 $frm_style = new FrmStyle();
385 $frm_style->update( 'default' );
386 }
387 }
388
389 /**
390 * @param array $params
391 * @return void
392 */
393 private function update_settings( $params ) {
394 $this->active_captcha = $params['frm_active_captcha'];
395 $this->pubkey = trim( $params['frm_pubkey'] );
396 $this->privkey = trim( $params['frm_privkey'] );
397 $this->re_type = $params['frm_re_type'];
398 $this->re_lang = $params['frm_re_lang'];
399 $this->re_threshold = floatval( $params['frm_re_threshold'] );
400 $this->hcaptcha_pubkey = trim( $params['frm_hcaptcha_pubkey'] );
401 $this->hcaptcha_privkey = trim( $params['frm_hcaptcha_privkey'] );
402 $this->turnstile_pubkey = trim( $params['frm_turnstile_pubkey'] );
403 $this->turnstile_privkey = trim( $params['frm_turnstile_privkey'] );
404 $this->load_style = $params['frm_load_style'];
405 $this->custom_css = $params['frm_custom_css'];
406 $this->default_email = $params['frm_default_email'];
407 $this->from_email = $params['frm_from_email'];
408 $this->currency = $params['frm_currency'];
409
410 $checkboxes = array( 'mu_menu', 're_multi', 'use_html', 'jquery_css', 'accordion_js', 'fade_form', 'no_ips', 'custom_header_ip', 'tracking', 'admin_bar', 'summary_emails' );
411 foreach ( $checkboxes as $set ) {
412 $this->$set = isset( $params[ 'frm_' . $set ] ) ? absint( $params[ 'frm_' . $set ] ) : 0;
413 }
414 }
415
416 /**
417 * @return void
418 */
419 private function update_roles( $params ) {
420 global $wp_roles;
421
422 $frm_roles = FrmAppHelper::frm_capabilities();
423 $roles = get_editable_roles();
424 foreach ( $frm_roles as $frm_role => $frm_role_description ) {
425 $this->$frm_role = (array) ( isset( $params[ $frm_role ] ) ? $params[ $frm_role ] : 'administrator' );
426
427 // Make sure administrators always have permissions
428 if ( ! in_array( 'administrator', $this->$frm_role, true ) ) {
429 array_push( $this->$frm_role, 'administrator' );
430 }
431
432 foreach ( $roles as $role => $details ) {
433 if ( in_array( $role, $this->$frm_role ) ) {
434 $wp_roles->add_cap( $role, $frm_role );
435 } else {
436 $wp_roles->remove_cap( $role, $frm_role );
437 }
438 }
439 }
440 }
441
442 /**
443 * Updates a single setting with specified sanitization.
444 *
445 * @since 6.9
446 *
447 * @param string $key The setting key to update.
448 * @param mixed $value The new value for the setting.
449 * @param string $sanitize The name of the sanitization function to apply to the new value.
450 * @return bool True on success, false on failure.
451 */
452 public function update_setting( $key, $value, $sanitize ) {
453 if ( ! property_exists( $this, $key ) || ! is_callable( $sanitize ) ) {
454 // Setting does not exist or sanitization function name is not callable.
455 return false;
456 }
457
458 // Update the property value.
459 FrmAppHelper::sanitize_value( $sanitize, $value );
460 $this->{$key} = $value;
461
462 return true;
463 }
464
465 /**
466 * @return void
467 */
468 public function store() {
469 // Save the posted value in the database
470
471 update_option( 'frm_options', $this );
472
473 delete_transient( 'frm_options' );
474 set_transient( 'frm_options', $this );
475
476 do_action( 'frm_store_settings' );
477 }
478 }
479