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

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