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

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