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

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