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

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