PluginProbe
UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP / 1.2.73
UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP v1.2.73
1.2.74 1.2.73 1.2.72 1.2.71 1.2.70 1.2.69 1.2.68 1.2.67 1.2.66 1.2.65 1.2.64 1.2.63 trunk 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 All 174 releases
← All changes | includes/class-validation.php +137 -117 1.0.211.2.73 View file →
@@ -17,13 +17,13 @@
17 17 *
18 18 * @param array $data Submitted form data
19 19 * @param string $type Form type.
20 20 * @param array|bool $fields Fields applicable for validation.
21 + * @param string $extra_where Extra where query.
21 22 *
22 - * @return array|mixed|void|WP_Error Validated form data.
23 + * @return array|mixed|WP_Error Validated form data.
23 24 */
24 - public function validate_fields($data, $type, $fields = false) {
25 -
25 + public function validate_fields( $data, $type, $fields = false, $extra_where = '' ) {
26 26 $errors = new WP_Error();
27 27
28 28 $errors = apply_filters('uwp_validate_fields_before', $errors, $data, $type);
29 29
@@ -31,23 +31,22 @@
31 31 if (!empty($error_code)) {
32 32 return $errors;
33 33 }
34 34
35 -
36 35 if (!$fields) {
37 36 global $wpdb;
38 37 $table_name = uwp_get_table_prefix() . 'uwp_form_fields';
39 38 if ($type == 'register') {
40 - if (isset($data["uwp_role_id"])) {
41 - $role_id = (int) strip_tags(esc_sql($data["uwp_role_id"]));
42 - } else {
43 - $role_id = 0;
44 - }
45 - $fields = get_register_validate_form_fields($role_id);
39 + if ( isset( $data['uwp_register_form_id'] ) && ! empty( $data['uwp_register_form_id'] ) ) {
40 + $form_id = (int) $data['uwp_register_form_id'];
41 + } else {
42 + $form_id = 1;
43 + }
44 + $fields = get_register_validate_form_fields($form_id);
46 45 } elseif ($type == 'change') {
47 46 $fields = get_change_validate_form_fields();
48 47 } elseif ($type == 'account') {
49 - $fields = $wpdb->get_results($wpdb->prepare("SELECT * FROM " . $table_name . " WHERE form_type = %s AND field_type != 'fieldset' AND field_type != 'file' AND is_active = '1' AND is_register_only_field = '0' AND htmlvar_name != 'uwp_account_password' ORDER BY sort_order ASC", array('account')));
48 + $fields = get_account_form_fields( $extra_where );
50 49 } else {
51 50 $fields = $wpdb->get_results($wpdb->prepare("SELECT * FROM " . $table_name . " WHERE form_type = %s AND field_type != 'fieldset' AND field_type != 'file' AND is_active = '1' ORDER BY sort_order ASC", array($type)));
52 51 }
53 52 }
@@ -53,28 +52,35 @@
53 52 }
54 53
55 54 $validated_data = array();
56 55
57 - $email_field = uwp_get_custom_field_info('uwp_account_email');
56 + $email_field = uwp_get_custom_field_info('email','account');
58 57 $email_extra = array();
59 58 if (isset($email_field->extra_fields) && $email_field->extra_fields != '') {
60 59 $email_extra = unserialize($email_field->extra_fields);
61 60 }
61 +
62 62 $enable_confirm_email_field = isset($email_extra['confirm_email']) ? $email_extra['confirm_email'] : '0';
63 63
64 - $password_field = uwp_get_custom_field_info('uwp_account_password');
65 - $enable_password = $password_field->is_active;
64 + $password_field = uwp_get_custom_field_info('password','account');
65 + $enable_password = isset($data['password']) && !empty($password_field) && $password_field->is_active ? 1 : 0;
66 66 $password_extra = array();
67 - if (isset($password_field->extra_fields) && $password_field->extra_fields != '') {
67 + if (!empty($password_field) && isset($password_field->extra_fields) && $password_field->extra_fields != '') {
68 68 $password_extra = unserialize($password_field->extra_fields);
69 69 }
70 +
70 71 $enable_confirm_password_field = isset($password_extra['confirm_password']) ? $password_extra['confirm_password'] : '0';
71 72
72 73 $enable_old_password = uwp_get_option('change_enable_old_password', false);
74 + $user_id = get_current_user_id();
75 + if($user_id && 1 == get_user_meta($user_id, 'is_uwp_social_login_no_password', true)){
76 + $enable_old_password = 0;
77 + }
73 78
74 79 if ($type == 'account' || $type == 'change') {
75 80 if (!is_user_logged_in()) {
76 81 $errors->add('not_logged_in', __('<strong>Error</strong>: Permission denied.', 'userswp'));
82 + return $errors;
77 83 }
78 84 }
79 85
80 86 if (!empty($fields)) {
@@ -87,46 +93,22 @@
87 93
88 94 if ($type == 'register') {
89 95
90 96 if ($enable_password != '1') {
91 - if ( ($field->htmlvar_name == 'uwp_account_password') OR ($field->htmlvar_name == 'uwp_account_confirm_password') ) {
97 + if ( ($field->htmlvar_name == 'password') OR ($field->htmlvar_name == 'confirm_password') ) {
92 98 continue;
93 99 }
94 100 }
95 101
96 102 if ($enable_confirm_email_field != '1') {
97 - if ( $field->htmlvar_name == 'uwp_account_confirm_email' ) {
103 + if ( $field->htmlvar_name == 'confirm_email' ) {
98 104 continue;
99 105 }
100 106 }
101 107 }
102 108
103 -
104 - if (!isset($data[$field->htmlvar_name]) && $field->is_required == 1) {
105 - if (is_admin()) {
106 - //do nothing since admin edit fields can be empty
107 - } else {
108 - if ($field->required_msg) {
109 - $errors->add('empty_'.$field->htmlvar_name, __('<strong>Error</strong>: '.$field->site_title.' '.$field->required_msg, 'userswp'));
110 - } else {
111 - $errors->add('empty_'.$field->htmlvar_name, __('<strong>Error</strong>: '.$field->site_title.' cannot be empty.', 'userswp'));
112 - }
113 - }
114 - }
115 -
116 - $error_code = $errors->get_error_code();
117 - if (!empty($error_code)) {
118 - return $errors;
119 - }
120 -
121 -
122 109 $value = isset($data[$field->htmlvar_name]) ? $data[$field->htmlvar_name] : '';
123 110 $sanitized_value = $value;
124 -
125 - if ($field->field_type == 'password') {
126 - continue;
127 - }
128 -
129 111 $sanitized = false;
130 112
131 113 // sanitize our default fields
132 114 switch($field->htmlvar_name) {
@@ -131,9 +113,9 @@
131 113 // sanitize our default fields
132 114 switch($field->htmlvar_name) {
133 115
134 116 case 'uwp_register_username':
135 - case 'uwp_account_username':
117 + case 'username':
136 118 case 'uwp_login_username':
137 119 case 'uwp_reset_username':
138 120 $sanitized_value = sanitize_user($value);
139 121 $sanitized = true;
@@ -140,10 +122,10 @@
140 122 break;
141 123
142 124 case 'uwp_register_first_name':
143 125 case 'uwp_register_last_name':
144 - case 'uwp_account_first_name':
145 - case 'uwp_account_last_name':
126 + case 'first_name':
127 + case 'last_name':
146 128 $sanitized_value = sanitize_text_field($value);
147 129 $sanitized = true;
148 130 break;
149 131
@@ -148,10 +130,15 @@
148 130 break;
149 131
150 132 case 'uwp_register_email':
151 133 case 'uwp_forgot_email':
152 - case 'uwp_account_email':
153 - $sanitized_value = sanitize_email($value);
134 + case 'email':
135 + case 'confirm_email':
136 + if ($type == 'forgot' && $field->htmlvar_name == 'email') {
137 + $sanitized_value = is_email($value) ? sanitize_email($value) : sanitize_user($value);
138 + } else {
139 + $sanitized_value = sanitize_email($value);
140 + }
154 141 $sanitized = true;
155 142 break;
156 143
157 144 }
@@ -194,60 +181,117 @@
194 181 $sanitized_value = strtotime($date_value);
195 182 }
196 183 break;
197 184
185 + case 'editor':
186 + $sanitized_value = wp_kses_post( strip_shortcodes( $value ) );
187 + break;
188 +
189 + case 'url':
190 + $sanitized_value = sanitize_url( wp_unslash( $value ) );
191 + break;
192 +
193 + case 'file':
194 + $sanitized_value = sanitize_text_field( $value );
195 +
196 + // Validate the file path.
197 + if ( $sanitized_value && validate_file( $sanitized_value ) !== 0 ) {
198 + $sanitized_value = '';
199 + }
200 + break;
201 +
198 202 default:
199 203 $sanitized_value = sanitize_text_field($value);
200 -
201 204 }
202 205 }
203 206
204 -
205 - if ($field->is_required == 1 && $sanitized_value == '') {
206 - if (is_admin()) {
207 + if ($field->is_required == 1 && $sanitized_value == '' && $field->field_type != 'file') {
208 + if (isset($GLOBALS['current_screen']) && !is_customize_preview()) {
207 209 //do nothing since admin edit fields can be empty
208 210 } else {
209 211 if ($field->required_msg) {
210 - $errors->add('empty_'.$field->htmlvar_name, __('<strong>Error</strong>: '.$field->site_title.' '.$field->required_msg, 'userswp'));
212 + $errors->add('empty_'.$field->htmlvar_name, sprintf(__('<strong>Error</strong>: %s %s', 'userswp'), $field->site_title, $field->required_msg));
213 + return $errors;
211 214 } else {
212 - $errors->add('empty_'.$field->htmlvar_name, __('<strong>Error</strong>: '.$field->site_title.' cannot be empty.', 'userswp'));
215 + $errors->add('empty_'.$field->htmlvar_name, sprintf(__('<strong>Error</strong>: %s cannot be empty.', 'userswp'), $field->site_title));
216 + return $errors;
213 217 }
214 218 }
215 219 }
216 220
217 - if ($field->field_type == 'email' && !empty($sanitized_value) && !is_email($sanitized_value)) {
221 + if ($type == 'forgot' && $field->htmlvar_name == 'email') {
222 + if (!empty($sanitized_value) && !is_email($sanitized_value) && !validate_username($sanitized_value)) {
223 + $incorrect_username_or_email_error_msg = apply_filters('uwp_incorrect_username_or_email_error_msg', __('<strong>Error</strong>: Please enter a valid username or email address.', 'userswp'));
224 + $errors->add('invalid_username_or_email', $incorrect_username_or_email_error_msg);
225 + return $errors;
226 + }
227 + } elseif ($field->field_type == 'email' && !empty($sanitized_value) && !is_email($sanitized_value)) {
218 228 $incorrect_email_error_msg = apply_filters('uwp_incorrect_email_error_msg', __('<strong>Error</strong>: The email address isn&#8217;t correct.', 'userswp'));
219 229 $errors->add('invalid_email', $incorrect_email_error_msg);
230 + return $errors;
220 231 }
221 232
222 233 //register email
223 - if ($type == 'register' && $field->htmlvar_name == 'uwp_account_email' && email_exists($sanitized_value)) {
234 + if ($type == 'register' && $field->htmlvar_name == 'email' && email_exists($sanitized_value)) {
224 235 $errors->add('email_exists', __('<strong>Error</strong>: This email is already registered, please choose another one.', 'userswp'));
236 + return $errors;
225 237 }
226 238
227 239 //forgot email
228 240 if ($field->htmlvar_name == 'uwp_forgot_email' && !email_exists($sanitized_value)) {
229 241 $errors->add('email_exists', __('<strong>Error</strong>: This email doesn\'t exists.', 'userswp'));
242 + return $errors;
230 243 }
231 244
232 245 $incorrect_username_error_msg = apply_filters('uwp_incorrect_username_error_msg', __('<strong>Error</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.', 'userswp'));
233 246
234 247 // Check the username for register
235 - if ($field->htmlvar_name == 'uwp_account_username') {
236 - if (!is_admin()) {
237 - if (!validate_username($sanitized_value)) {
238 - $errors->add('invalid_username', $incorrect_username_error_msg);
239 - }
240 - if (username_exists($sanitized_value)) {
241 - $errors->add('username_exists', __('<strong>Error</strong>: This username is already registered. Please choose another one.', 'userswp'));
242 - }
248 + if ('register' == $type && $field->htmlvar_name == 'username') {
249 + if (!empty($sanitized_value) && !validate_username($sanitized_value)) {
250 + $errors->add('invalid_username', $incorrect_username_error_msg);
251 + return $errors;
243 252 }
253 + if (username_exists($sanitized_value)) {
254 + $errors->add('username_exists', __('<strong>Error</strong>: This username is already registered. Please choose another one.', 'userswp'));
255 + return $errors;
256 + }
257 + $username_length = uwp_get_option( 'register_username_length', 4);
258 + $username_length_max = uwp_get_option( 'register_username_length_max', 20);
259 +
260 + if(!empty($sanitized_value) && (strlen($sanitized_value) < $username_length || strlen($sanitized_value) > $username_length_max)) {
261 + $errors->add('username_length', sprintf(__('<strong>Error</strong>: Username must be between %s and %s characters.', 'userswp'), $username_length, $username_length_max));
262 + return $errors;
263 + }
244 264 }
245 265
266 + // check for the TOS and GDPR validation.
267 + if ('register' == $type && ($field->htmlvar_name == 'register_gdpr' || $field->htmlvar_name == 'register_tos' )) {
268 +
269 + if($field->htmlvar_name == 'register_gdpr'){
270 + $msg = __('You must read and accept our GDPR policy.', 'userswp');
271 + $is_page = uwp_get_option('register_gdpr_page', false);
272 + } else {
273 + $msg = __('You must accept our terms and conditions.', 'userswp');
274 + $is_page = uwp_get_option('register_terms_page', false);
275 + }
276 +
277 + if(isset($sanitized_value) && 1 != $sanitized_value && $is_page){
278 +
279 + if ($field->required_msg) {
280 + $errors->add('empty_'.$field->htmlvar_name, __($field->required_msg, 'userswp'));
281 + return $errors;
282 + } else {
283 + $errors->add('empty_'.$field->htmlvar_name, $msg);
284 + return $errors;
285 + }
286 + }
287 + }
288 +
246 289 // Check the username for login
247 - if ($field->htmlvar_name == 'uwp_login_username') {
248 - if (!validate_username($sanitized_value)) {
290 + if ($type != 'account' && $field->htmlvar_name == 'username') {
291 + if (!empty($sanitized_value) && !is_email($sanitized_value) && !validate_username($sanitized_value)) {
249 292 $errors->add('invalid_username', $incorrect_username_error_msg);
293 + return $errors;
250 294 }
251 295 }
252 296
253 297
@@ -260,46 +304,24 @@
260 304 if (!empty($error_code)) {
261 305 return $errors;
262 306 }
263 307
264 - if ($type == 'login') {
265 - $password_type = 'login';
266 - } elseif ($type == 'reset') {
267 - $password_type = 'reset';
268 - } elseif ($type == 'change') {
269 - $password_type = 'change';
270 - } else {
271 - $password_type = 'account';
272 - }
273 -
274 - if (($type == 'change' && $enable_old_password == '1')) {
275 - //check old password
276 - if( empty( $data['uwp_'.$password_type.'_old_password'] ) ) {
308 + if ( $type == 'change' && $enable_old_password == '1' ) {
309 + $old_pass = isset($data['old_password']) ? $data['old_password'] : "";
310 + //check old password
311 + if( empty( $old_pass ) ) {
277 312 $errors->add( 'empty_password', __( '<strong>Error</strong>: Please enter your old password', 'userswp' ) );
278 - }
279 -
280 - $error_code = $errors->get_error_code();
281 - if (!empty($error_code)) {
282 313 return $errors;
283 314 }
284 315
285 - $pass = $data['uwp_'.$password_type.'_old_password'];
286 316 $user = get_user_by( 'id', get_current_user_id() );
287 - if ( !wp_check_password( $pass, $user->data->user_pass, $user->ID) ) {
317 + if ( !wp_check_password( $old_pass, $user->data->user_pass, $user->ID) ) {
288 318 $errors->add( 'invalid_password', __( '<strong>Error</strong>: Incorrect old password', 'userswp' ) );
289 - }
290 -
291 - $error_code = $errors->get_error_code();
292 - if (!empty($error_code)) {
293 319 return $errors;
294 320 }
295 321
296 - if( $data['uwp_'.$password_type.'_old_password'] == $data['uwp_'.$password_type.'_password'] ) {
297 - $errors->add( 'invalid_password', __( '<strong>Error</strong>: Old password and new password are same', 'userswp' ) );
298 - }
299 -
300 - $error_code = $errors->get_error_code();
301 - if (!empty($error_code)) {
322 + if( $old_pass == $data['password'] ) {
323 + $errors->add( 'invalid_password', __( '<strong>Error</strong>: The old password and the new password are the same', 'userswp' ) );
302 324 return $errors;
303 325 }
304 326
305 327 }
@@ -305,32 +327,20 @@
305 327 }
306 328
307 329 if (($type == 'register' && $enable_confirm_email_field == '1')) {
308 330 //check confirm email
309 - if( empty( $data['uwp_account_email'] ) ) {
331 + if( empty( $data['email'] ) ) {
310 332 $errors->add( 'empty_email', __( '<strong>Error</strong>: Please enter your Email', 'userswp' ) );
311 - }
312 -
313 - $error_code = $errors->get_error_code();
314 - if (!empty($error_code)) {
315 333 return $errors;
316 334 }
317 335
318 - if( !isset($data['uwp_account_confirm_email']) || empty( $data['uwp_account_confirm_email'] ) ) {
336 + if( !isset($data['confirm_email']) || empty( $data['confirm_email'] ) ) {
319 337 $errors->add( 'empty_confirm_email', __( '<strong>Error</strong>: Please fill Confirm Email field', 'userswp' ) );
320 - }
321 -
322 - $error_code = $errors->get_error_code();
323 - if (!empty($error_code)) {
324 338 return $errors;
325 339 }
326 340
327 - if( $data['uwp_account_email'] != $data['uwp_account_confirm_email'] ) {
341 + if( $data['email'] != $data['confirm_email'] ) {
328 342 $errors->add( 'email_mismatch', __( '<strong>Error</strong>: Email and Confirm email not match', 'userswp' ) );
329 - }
330 -
331 - $error_code = $errors->get_error_code();
332 - if (!empty($error_code)) {
333 343 return $errors;
334 344 }
335 345
336 346 }
@@ -336,17 +346,27 @@
336 346 }
337 347
338 348 if ($type == 'change' || $type == 'reset' || $type == 'login' || ($type == 'register' && $enable_password == '1')) {
339 349 //check password
340 - if( empty( $data['uwp_'.$password_type.'_password'] ) ) {
350 + if( empty( $data['password'] ) ) {
341 351 $errors->add( 'empty_password', __( 'Please enter a password', 'userswp' ) );
342 352 }
343 353
344 - if ($type != 'login' && strlen($data['uwp_'.$password_type.'_password']) < 7) {
345 - $errors->add('pass_match', __('ERROR: Password must be 7 characters or more.', 'userswp'));
346 - }
354 + $password_min_length = uwp_get_option( 'register_password_min_length');
355 + $password_min_length = !empty($password_min_length) ? (int)$password_min_length : 8;
347 356
348 - $validated_data['password'] = $data['uwp_'.$password_type.'_password'];
357 + $password_max_length = uwp_get_option( 'register_password_max_length');
358 + $password_max_length = !empty($password_max_length) ? (int)$password_max_length : 15;
359 +
360 + if ($type != 'login' && (strlen($data['password']) < $password_min_length || strlen($data['password']) > $password_max_length )) {
361 + if(strlen($data['password']) > $password_max_length) {
362 + $errors->add('pass_match', sprintf(__('<strong>Error</strong>: Password must be %s characters or less.', 'userswp'), $password_max_length));
363 + } else{
364 + $errors->add('pass_match', sprintf(__('<strong>Error</strong>: Password must be %s characters or more.', 'userswp'), $password_min_length));
365 + }
366 + }
367 +
368 + $validated_data['password'] = isset($data['password']) ? $data['password'] : '';
349 369 }
350 370
351 371 $error_code = $errors->get_error_code();
352 372 if (!empty($error_code)) {
@@ -355,16 +375,16 @@
355 375
356 376 if (($type == 'register' && $enable_password == '1') || $type == 'reset' || $type == 'change') {
357 377
358 378 if (($type == 'register' && $enable_confirm_password_field != '1')) {
359 - $validated_data['password'] = $data['uwp_'.$password_type.'_password'];
379 + $validated_data['password'] = $data['password'];
360 380 } else {
361 381 //check password
362 - if ($data['uwp_'.$password_type.'_password'] != $data['uwp_'.$password_type.'_confirm_password']) {
363 - $errors->add('pass_match', __('ERROR: Passwords do not match.', 'userswp'));
382 + if ($data['password'] != $data['confirm_password']) {
383 + $errors->add('pass_match', __('<strong>Error</strong>: Passwords do not match.', 'userswp'));
364 384 }
365 385
366 - $validated_data['password'] = $data['uwp_'.$password_type.'_password'];
386 + $validated_data['password'] = isset($data['password']) ? $data['password'] : '';
367 387 }
368 388 }
369 389
370 390