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

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