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

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