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

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