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.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 1.0.22 All 173 releases
← All changes | includes/class-validation.php +141 -120 1.0.131.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,53 +31,56 @@
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' 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 }
54 53
55 -
56 54 $validated_data = array();
57 55
58 -
59 -
60 - $email_field = uwp_get_custom_field_info('uwp_account_email');
56 + $email_field = uwp_get_custom_field_info('email','account');
61 57 $email_extra = array();
62 58 if (isset($email_field->extra_fields) && $email_field->extra_fields != '') {
63 59 $email_extra = unserialize($email_field->extra_fields);
64 60 }
61 +
65 62 $enable_confirm_email_field = isset($email_extra['confirm_email']) ? $email_extra['confirm_email'] : '0';
66 63
67 - $password_field = uwp_get_custom_field_info('uwp_account_password');
68 - $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;
69 66 $password_extra = array();
70 - if (isset($password_field->extra_fields) && $password_field->extra_fields != '') {
67 + if (!empty($password_field) && isset($password_field->extra_fields) && $password_field->extra_fields != '') {
71 68 $password_extra = unserialize($password_field->extra_fields);
72 69 }
70 +
73 71 $enable_confirm_password_field = isset($password_extra['confirm_password']) ? $password_extra['confirm_password'] : '0';
74 72
75 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 + }
76 78
77 79 if ($type == 'account' || $type == 'change') {
78 80 if (!is_user_logged_in()) {
79 81 $errors->add('not_logged_in', __('<strong>Error</strong>: Permission denied.', 'userswp'));
82 + return $errors;
80 83 }
81 84 }
82 85
83 86 if (!empty($fields)) {
@@ -90,46 +93,22 @@
90 93
91 94 if ($type == 'register') {
92 95
93 96 if ($enable_password != '1') {
94 - 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') ) {
95 98 continue;
96 99 }
97 100 }
98 101
99 102 if ($enable_confirm_email_field != '1') {
100 - if ( $field->htmlvar_name == 'uwp_account_confirm_email' ) {
103 + if ( $field->htmlvar_name == 'confirm_email' ) {
101 104 continue;
102 105 }
103 106 }
104 107 }
105 108
106 -
107 - if (!isset($data[$field->htmlvar_name]) && $field->is_required == 1) {
108 - if (is_admin()) {
109 - //do nothing since admin edit fields can be empty
110 - } else {
111 - if ($field->required_msg) {
112 - $errors->add('empty_'.$field->htmlvar_name, __('<strong>Error</strong>: '.$field->site_title.' '.$field->required_msg, 'userswp'));
113 - } else {
114 - $errors->add('empty_'.$field->htmlvar_name, __('<strong>Error</strong>: '.$field->site_title.' cannot be empty.', 'userswp'));
115 - }
116 - }
117 - }
118 -
119 - $error_code = $errors->get_error_code();
120 - if (!empty($error_code)) {
121 - return $errors;
122 - }
123 -
124 -
125 109 $value = isset($data[$field->htmlvar_name]) ? $data[$field->htmlvar_name] : '';
126 110 $sanitized_value = $value;
127 -
128 - if ($field->field_type == 'password') {
129 - continue;
130 - }
131 -
132 111 $sanitized = false;
133 112
134 113 // sanitize our default fields
135 114 switch($field->htmlvar_name) {
@@ -134,9 +113,9 @@
134 113 // sanitize our default fields
135 114 switch($field->htmlvar_name) {
136 115
137 116 case 'uwp_register_username':
138 - case 'uwp_account_username':
117 + case 'username':
139 118 case 'uwp_login_username':
140 119 case 'uwp_reset_username':
141 120 $sanitized_value = sanitize_user($value);
142 121 $sanitized = true;
@@ -143,10 +122,10 @@
143 122 break;
144 123
145 124 case 'uwp_register_first_name':
146 125 case 'uwp_register_last_name':
147 - case 'uwp_account_first_name':
148 - case 'uwp_account_last_name':
126 + case 'first_name':
127 + case 'last_name':
149 128 $sanitized_value = sanitize_text_field($value);
150 129 $sanitized = true;
151 130 break;
152 131
@@ -151,10 +130,15 @@
151 130 break;
152 131
153 132 case 'uwp_register_email':
154 133 case 'uwp_forgot_email':
155 - case 'uwp_account_email':
156 - $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 + }
157 141 $sanitized = true;
158 142 break;
159 143
160 144 }
@@ -170,8 +154,12 @@
170 154 case 'checkbox':
171 155 $sanitized_value = sanitize_text_field($value);
172 156 break;
173 157
158 + case 'textarea':
159 + $sanitized_value = sanitize_textarea_field($value);
160 + break;
161 +
174 162 case 'email':
175 163 $sanitized_value = sanitize_email($value);
176 164 break;
177 165
@@ -193,60 +181,117 @@
193 181 $sanitized_value = strtotime($date_value);
194 182 }
195 183 break;
196 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 +
197 202 default:
198 203 $sanitized_value = sanitize_text_field($value);
199 -
200 204 }
201 205 }
202 206
203 -
204 - if ($field->is_required == 1 && $sanitized_value == '') {
205 - if (is_admin()) {
207 + if ($field->is_required == 1 && $sanitized_value == '' && $field->field_type != 'file') {
208 + if (isset($GLOBALS['current_screen']) && !is_customize_preview()) {
206 209 //do nothing since admin edit fields can be empty
207 210 } else {
208 211 if ($field->required_msg) {
209 - $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;
210 214 } else {
211 - $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;
212 217 }
213 218 }
214 219 }
215 220
216 - 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)) {
217 228 $incorrect_email_error_msg = apply_filters('uwp_incorrect_email_error_msg', __('<strong>Error</strong>: The email address isn&#8217;t correct.', 'userswp'));
218 229 $errors->add('invalid_email', $incorrect_email_error_msg);
230 + return $errors;
219 231 }
220 232
221 233 //register email
222 - 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)) {
223 235 $errors->add('email_exists', __('<strong>Error</strong>: This email is already registered, please choose another one.', 'userswp'));
236 + return $errors;
224 237 }
225 238
226 239 //forgot email
227 240 if ($field->htmlvar_name == 'uwp_forgot_email' && !email_exists($sanitized_value)) {
228 241 $errors->add('email_exists', __('<strong>Error</strong>: This email doesn\'t exists.', 'userswp'));
242 + return $errors;
229 243 }
230 244
231 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'));
232 246
233 247 // Check the username for register
234 - if ($field->htmlvar_name == 'uwp_account_username') {
235 - if (!is_admin()) {
236 - if (!validate_username($sanitized_value)) {
237 - $errors->add('invalid_username', $incorrect_username_error_msg);
238 - }
239 - if (username_exists($sanitized_value)) {
240 - $errors->add('username_exists', __('<strong>Error</strong>: This username is already registered. Please choose another one.', 'userswp'));
241 - }
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;
242 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 + }
243 264 }
244 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 +
245 289 // Check the username for login
246 - if ($field->htmlvar_name == 'uwp_login_username') {
247 - 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)) {
248 292 $errors->add('invalid_username', $incorrect_username_error_msg);
293 + return $errors;
249 294 }
250 295 }
251 296
252 297
@@ -259,46 +304,24 @@
259 304 if (!empty($error_code)) {
260 305 return $errors;
261 306 }
262 307
263 - if ($type == 'login') {
264 - $password_type = 'login';
265 - } elseif ($type == 'reset') {
266 - $password_type = 'reset';
267 - } elseif ($type == 'change') {
268 - $password_type = 'change';
269 - } else {
270 - $password_type = 'account';
271 - }
272 -
273 - if (($type == 'change' && $enable_old_password == '1')) {
274 - //check old password
275 - 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 ) ) {
276 312 $errors->add( 'empty_password', __( '<strong>Error</strong>: Please enter your old password', 'userswp' ) );
277 - }
278 -
279 - $error_code = $errors->get_error_code();
280 - if (!empty($error_code)) {
281 313 return $errors;
282 314 }
283 315
284 - $pass = $data['uwp_'.$password_type.'_old_password'];
285 316 $user = get_user_by( 'id', get_current_user_id() );
286 - if ( !wp_check_password( $pass, $user->data->user_pass, $user->ID) ) {
317 + if ( !wp_check_password( $old_pass, $user->data->user_pass, $user->ID) ) {
287 318 $errors->add( 'invalid_password', __( '<strong>Error</strong>: Incorrect old password', 'userswp' ) );
288 - }
289 -
290 - $error_code = $errors->get_error_code();
291 - if (!empty($error_code)) {
292 319 return $errors;
293 320 }
294 321
295 - if( $data['uwp_'.$password_type.'_old_password'] == $data['uwp_'.$password_type.'_password'] ) {
296 - $errors->add( 'invalid_password', __( '<strong>Error</strong>: Old password and new password are same', 'userswp' ) );
297 - }
298 -
299 - $error_code = $errors->get_error_code();
300 - 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' ) );
301 324 return $errors;
302 325 }
303 326
304 327 }
@@ -304,32 +327,20 @@
304 327 }
305 328
306 329 if (($type == 'register' && $enable_confirm_email_field == '1')) {
307 330 //check confirm email
308 - if( empty( $data['uwp_account_email'] ) ) {
331 + if( empty( $data['email'] ) ) {
309 332 $errors->add( 'empty_email', __( '<strong>Error</strong>: Please enter your Email', 'userswp' ) );
310 - }
311 -
312 - $error_code = $errors->get_error_code();
313 - if (!empty($error_code)) {
314 333 return $errors;
315 334 }
316 335
317 - if( !isset($data['uwp_account_confirm_email']) || empty( $data['uwp_account_confirm_email'] ) ) {
336 + if( !isset($data['confirm_email']) || empty( $data['confirm_email'] ) ) {
318 337 $errors->add( 'empty_confirm_email', __( '<strong>Error</strong>: Please fill Confirm Email field', 'userswp' ) );
319 - }
320 -
321 - $error_code = $errors->get_error_code();
322 - if (!empty($error_code)) {
323 338 return $errors;
324 339 }
325 340
326 - if( $data['uwp_account_email'] != $data['uwp_account_confirm_email'] ) {
341 + if( $data['email'] != $data['confirm_email'] ) {
327 342 $errors->add( 'email_mismatch', __( '<strong>Error</strong>: Email and Confirm email not match', 'userswp' ) );
328 - }
329 -
330 - $error_code = $errors->get_error_code();
331 - if (!empty($error_code)) {
332 343 return $errors;
333 344 }
334 345
335 346 }
@@ -335,17 +346,27 @@
335 346 }
336 347
337 348 if ($type == 'change' || $type == 'reset' || $type == 'login' || ($type == 'register' && $enable_password == '1')) {
338 349 //check password
339 - if( empty( $data['uwp_'.$password_type.'_password'] ) ) {
350 + if( empty( $data['password'] ) ) {
340 351 $errors->add( 'empty_password', __( 'Please enter a password', 'userswp' ) );
341 352 }
342 353
343 - if ($type != 'login' && strlen($data['uwp_'.$password_type.'_password']) < 7) {
344 - $errors->add('pass_match', __('ERROR: Password must be 7 characters or more.', 'userswp'));
345 - }
354 + $password_min_length = uwp_get_option( 'register_password_min_length');
355 + $password_min_length = !empty($password_min_length) ? (int)$password_min_length : 8;
346 356
347 - $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'] : '';
348 369 }
349 370
350 371 $error_code = $errors->get_error_code();
351 372 if (!empty($error_code)) {
@@ -354,16 +375,16 @@
354 375
355 376 if (($type == 'register' && $enable_password == '1') || $type == 'reset' || $type == 'change') {
356 377
357 378 if (($type == 'register' && $enable_confirm_password_field != '1')) {
358 - $validated_data['password'] = $data['uwp_'.$password_type.'_password'];
379 + $validated_data['password'] = $data['password'];
359 380 } else {
360 381 //check password
361 - if ($data['uwp_'.$password_type.'_password'] != $data['uwp_'.$password_type.'_confirm_password']) {
362 - $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'));
363 384 }
364 385
365 - $validated_data['password'] = $data['uwp_'.$password_type.'_password'];
386 + $validated_data['password'] = isset($data['password']) ? $data['password'] : '';
366 387 }
367 388 }
368 389
369 390