Completed
Push — master ( feb49b...2e7061 )
by Stephanie
06:17 queued 03:03
created

FrmSettings::translatable_strings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
class FrmSettings {
4
	public $option_name = 'frm_options';
5
	public $menu;
6
	public $mu_menu;
7
	public $use_html;
8
	public $jquery_css;
9
	public $accordion_js;
10
	public $fade_form;
11
	public $old_css;
12
13
	public $success_msg;
14
	public $blank_msg;
15
	public $unique_msg;
16
	public $invalid_msg;
17
	public $failed_msg;
18
	public $submit_value;
19
	public $login_msg;
20
	public $admin_permission;
21
22
	public $email_to;
23
	public $load_style;
24
	public $custom_style;
25
26
	public $pubkey;
27
	public $privkey;
28
	public $re_lang;
29
	public $re_type;
30
	public $re_msg;
31
	public $re_multi;
32
33
	public $no_ips;
34
	public $current_form = 0;
35
	public $tracking;
36
37
	public function __construct( $args = array() ) {
38
		if ( ! defined( 'ABSPATH' ) ) {
39
			die( 'You are not allowed to call this page directly.' );
40
		}
41
42
		$settings = get_transient( $this->option_name );
43
44
		if ( ! is_object( $settings ) ) {
45
			$settings = $this->translate_settings( $settings );
46
		}
47
48
		foreach ( $settings as $setting_name => $setting ) {
49
			$this->{$setting_name} = $setting;
50
			unset( $setting_name, $setting );
51
		}
52
53
		$this->set_default_options();
54
55
		$this->maybe_filter_for_form( $args );
56
	}
57
58
	private function translate_settings( $settings ) {
59
		if ( $settings ) { //workaround for W3 total cache conflict
60
			return unserialize( serialize( $settings ) );
61
		}
62
63
		$settings = get_option( $this->option_name );
64
		if ( is_object( $settings ) ) {
65
			set_transient( $this->option_name, $settings );
66
67
			return $settings;
68
		}
69
70
		// If unserializing didn't work
71
		if ( $settings ) { //workaround for W3 total cache conflict
72
			$settings = unserialize( serialize( $settings ) );
73
		} else {
74
			$settings = $this;
75
		}
76
77
		update_option( $this->option_name, $settings );
78
		set_transient( $this->option_name, $settings );
79
80
		return $settings;
81
	}
82
83
	/**
84
	 * @return array
85
	 */
86
	public function default_options() {
87
		return array(
88
			'menu'         => apply_filters( 'frm_default_menu', 'Formidable' ),
89
			'mu_menu'      => 0,
90
			'use_html'     => true,
91
			'jquery_css'   => false,
92
			'accordion_js' => false,
93
			'fade_form'    => false,
94
			'old_css'      => true,
95
96
			're_multi' => 1,
97
98
			'success_msg'      => __( 'Your responses were successfully submitted. Thank you!', 'formidable' ),
99
			'blank_msg'        => __( 'This field cannot be blank.', 'formidable' ),
100
			'unique_msg'       => __( 'This value must be unique.', 'formidable' ),
101
			'invalid_msg'      => __( 'There was a problem with your submission. Errors are marked below.', 'formidable' ),
102
			'failed_msg'       => __( 'We\'re sorry. It looks like you\'ve already submitted that.', 'formidable' ),
103
			'submit_value'     => __( 'Submit', 'formidable' ),
104
			'login_msg'        => __( 'You do not have permission to view this form.', 'formidable' ),
105
			'admin_permission' => __( 'You do not have permission to do that', 'formidable' ),
106
107
			'email_to' => '[admin_email]',
108
			'no_ips'   => 0,
109
			'tracking' => FrmAppHelper::pro_is_installed(),
110
		);
111
	}
112
113
	private function set_default_options() {
114
		$this->fill_recaptcha_settings();
115
116
		if ( ! isset( $this->load_style ) ) {
117
			if ( ! isset( $this->custom_style ) ) {
118
				$this->custom_style = true;
119
			}
120
121
			$this->load_style = 'all';
122
		}
123
124
		$this->fill_with_defaults();
125
126
		if ( is_multisite() && is_admin() ) {
127
			$mu_menu = get_site_option( 'frm_admin_menu_name' );
128
			if ( $mu_menu && ! empty( $mu_menu ) ) {
129
				$this->menu    = $mu_menu;
130
				$this->mu_menu = 1;
131
			}
132
		}
133
134
		$frm_roles = FrmAppHelper::frm_capabilities( 'pro' );
135
		foreach ( $frm_roles as $frm_role => $frm_role_description ) {
136
			if ( ! isset( $this->$frm_role ) ) {
137
				$this->$frm_role = 'administrator';
138
			}
139
		}
140
	}
141
142
	public function fill_with_defaults( $params = array() ) {
143
		$settings = $this->default_options();
144
145
		// Use grids and fade in as default for new installs.
146
		if ( isset( $params['frm_tracking'] ) ) {
147
			$settings['old_css']   = false;
148
			$settings['fade_form'] = true;
149
		}
150
151
		foreach ( $settings as $setting => $default ) {
152
			if ( isset( $params[ 'frm_' . $setting ] ) ) {
153
				$this->{$setting} = $params[ 'frm_' . $setting ];
154
			} elseif ( ! isset( $this->{$setting} ) ) {
155
				$this->{$setting} = $default;
156
			}
157
158
			if ( $setting == 'menu' && empty( $this->{$setting} ) ) {
159
				$this->{$setting} = $default;
160
			}
161
162
			unset( $setting, $default );
163
		}
164
	}
165
166
	private function fill_recaptcha_settings() {
167
		$privkey = '';
168
		$re_lang = '';
169
170
		if ( ! isset( $this->pubkey ) ) {
171
			// get the options from the database
172
			$recaptcha_opt = is_multisite() ? get_site_option( 'recaptcha' ) : get_option( 'recaptcha' );
173
			$this->pubkey  = isset( $recaptcha_opt['pubkey'] ) ? $recaptcha_opt['pubkey'] : '';
174
			$privkey       = isset( $recaptcha_opt['privkey'] ) ? $recaptcha_opt['privkey'] : $privkey;
175
			$re_lang       = isset( $recaptcha_opt['re_lang'] ) ? $recaptcha_opt['re_lang'] : $re_lang;
176
		}
177
178
		if ( ! isset( $this->re_msg ) || empty( $this->re_msg ) ) {
179
			$this->re_msg = __( 'The reCAPTCHA was not entered correctly', 'formidable' );
180
		}
181
182
		if ( ! isset( $this->privkey ) ) {
183
			$this->privkey = $privkey;
184
		}
185
186
		if ( ! isset( $this->re_lang ) ) {
187
			$this->re_lang = $re_lang;
188
		}
189
190
		if ( ! isset( $this->re_type ) ) {
191
			$this->re_type = '';
192
		}
193
	}
194
195
	/**
196
	 * Get values that may be shown on the front-end without an override in the form settings.
197
	 *
198
	 * @since 3.06.01
199
	 */
200
	public function translatable_strings() {
201
		return array(
202
			'invalid_msg',
203
			'failed_msg',
204
			'login_msg',
205
		);
206
	}
207
208
	/**
209
	 * Allow strings to be filtered when a specific form may be displaying them.
210
	 *
211
	 * @since 3.06.01
212
	 */
213
	public function maybe_filter_for_form( $args ) {
214
		if ( isset( $args['current_form'] ) && is_numeric( $args['current_form'] ) ) {
215
			$this->current_form = $args['current_form'];
216
			foreach ( $this->translatable_strings() as $string ) {
217
				$this->{$string} = apply_filters( 'frm_global_setting', $this->{$string}, $string, $this );
218
				$this->{$string} = apply_filters( 'frm_global_' . $string, $this->{$string}, $this );
219
			}
220
		}
221
	}
222
223
	public function validate( $params, $errors ) {
224
		return apply_filters( 'frm_validate_settings', $errors, $params );
225
	}
226
227
	public function update( $params ) {
228
		$this->fill_with_defaults( $params );
229
		$this->update_settings( $params );
230
231
		if ( $this->mu_menu ) {
232
			update_site_option( 'frm_admin_menu_name', $this->menu );
233
		} elseif ( current_user_can( 'administrator' ) ) {
234
			update_site_option( 'frm_admin_menu_name', false );
235
		}
236
237
		$this->update_roles( $params );
238
239
		do_action( 'frm_update_settings', $params );
240
241
		if ( function_exists( 'get_filesystem_method' ) ) {
242
			// save styling settings in case fallback setting changes
243
			$frm_style = new FrmStyle();
244
			$frm_style->update( 'default' );
245
		}
246
	}
247
248
	private function update_settings( $params ) {
249
		$this->pubkey   = trim( $params['frm_pubkey'] );
250
		$this->privkey  = $params['frm_privkey'];
251
		$this->re_type  = $params['frm_re_type'];
252
		$this->re_lang  = $params['frm_re_lang'];
253
254
		$this->load_style = $params['frm_load_style'];
255
256
		$checkboxes = array( 'mu_menu', 're_multi', 'use_html', 'jquery_css', 'accordion_js', 'fade_form', 'old_css', 'no_ips', 'tracking' );
257
		foreach ( $checkboxes as $set ) {
258
			$this->$set = isset( $params[ 'frm_' . $set ] ) ? $params[ 'frm_' . $set ] : 0;
259
		}
260
	}
261
262
	private function update_roles( $params ) {
263
		global $wp_roles;
264
265
		$frm_roles = FrmAppHelper::frm_capabilities();
266
		$roles     = get_editable_roles();
267
		foreach ( $frm_roles as $frm_role => $frm_role_description ) {
268
			$this->$frm_role = (array) ( isset( $params[ $frm_role ] ) ? $params[ $frm_role ] : 'administrator' );
269
270
			// Make sure administrators always have permissions
271
			if ( ! in_array( 'administrator', $this->$frm_role ) ) {
272
				array_push( $this->$frm_role, 'administrator' );
273
			}
274
275
			foreach ( $roles as $role => $details ) {
276
				if ( in_array( $role, $this->$frm_role ) ) {
277
					$wp_roles->add_cap( $role, $frm_role );
278
				} else {
279
					$wp_roles->remove_cap( $role, $frm_role );
280
				}
281
			}
282
		}
283
	}
284
285
	public function store() {
286
		// Save the posted value in the database
287
288
		update_option( 'frm_options', $this );
289
290
		delete_transient( 'frm_options' );
291
		set_transient( 'frm_options', $this );
292
293
		do_action( 'frm_store_settings' );
294
	}
295
}
296