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

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