PluginProbe
UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP / 1.2.16
UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP v1.2.16
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 1.0.23 All 172 releases
userswp / includes / helpers / forms.php

forms.php in UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP 1.2.16, at includes/helpers/forms.php

572 lines 17.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Returns the register form fields.
4 *
5 * @return array Form fields.
6 * @package userswp
7 *
8 * @since 1.0.0
9 */
10 function get_register_form_fields($form_id = 1) {
11 global $wpdb;
12 $table_name = uwp_get_table_prefix() . 'uwp_form_fields';
13 $extras_table_name = uwp_get_table_prefix() . 'uwp_form_extras';
14 $fields = $wpdb->get_results($wpdb->prepare("SELECT fields.* FROM " . $table_name . " fields JOIN " . $extras_table_name . " extras ON extras.site_htmlvar_name = fields.htmlvar_name WHERE fields.form_type = %s AND fields.is_active = '1' AND fields.for_admin_use != '1' AND fields.is_register_field = '1' AND extras.form_type = 'register' AND extras.form_id=%d AND fields.form_id=%d ORDER BY extras.sort_order ASC", array('account', $form_id, $form_id)));
15 $fields = apply_filters('uwp_get_register_form_fields', $fields, $form_id);
16 return $fields;
17 }
18
19 function check_register_form_field( $var ) {
20 if ( ! $var ) {
21 return false;
22 }
23
24 global $wpdb;
25 $extras_table_name = uwp_get_table_prefix() . 'uwp_form_extras';
26
27 $fields = $wpdb->get_results(
28 $wpdb->prepare(
29 "select * from " . $extras_table_name . " where form_type = %s AND site_htmlvar_name = %s order by sort_order asc",
30 array( 'register', $var )
31 )
32 );
33
34 $fields = apply_filters( 'uwp_check_register_form_field', $fields );
35
36 return $fields;
37 }
38
39 /**
40 * Returns the register form validate-able fields.
41 *
42 * @param int $form_id Form ID Default 1.
43 *
44 * @return array Validate-able fields.
45 * @since 1.0.0
46 * @package userswp
47 *
48 */
49 function get_register_validate_form_fields( $form_id = 1 ) {
50 global $wpdb;
51 $table_name = uwp_get_table_prefix() . 'uwp_form_fields';
52 $extras_table_name = uwp_get_table_prefix() . 'uwp_form_extras';
53 $fields = $wpdb->get_results( $wpdb->prepare( "SELECT fields.* FROM " . $table_name . " fields JOIN " . $extras_table_name . " extras ON extras.site_htmlvar_name = fields.htmlvar_name WHERE fields.form_type = %s AND fields.field_type != 'fieldset' AND fields.field_type != 'file' AND fields.is_active = '1' AND fields.for_admin_use != '1' AND fields.is_register_field = '1' AND fields.form_id = %s ORDER BY extras.sort_order ASC", array( 'account', $form_id ) ) );
54 $fields = apply_filters( 'uwp_get_register_validate_form_fields', $fields, $form_id );
55
56 return $fields;
57 }
58
59 /**
60 * Returns the change password form validate-able fields.
61 *
62 * @return array Validate-able fields
63 * @package userswp
64 *
65 * @since 1.0.0
66 */
67 function get_change_validate_form_fields() {
68 global $wpdb;
69 $table_name = uwp_get_table_prefix() . 'uwp_form_fields';
70 $enable_old_password = uwp_get_option( 'change_enable_old_password', false );
71 $user_id = get_current_user_id();
72 if($user_id && 1 == get_user_meta($user_id, 'is_uwp_social_login_no_password', true)){
73 $enable_old_password = 0;
74 }
75 if ( $enable_old_password == '1' ) {
76 $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 for_admin_use != '1' ORDER BY sort_order ASC", array( 'change' ) ) );
77 } else {
78 $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 for_admin_use != '1' AND htmlvar_name != 'old_password' ORDER BY sort_order ASC", array( 'change' ) ) );
79 }
80
81 return $fields;
82 }
83
84 /**
85 * Returns the account form fields.
86 *
87 * @param string $extra_where Extra where query.
88 *
89 * @return array Form fields.
90 * @since 1.0.0
91 * @package userswp
92 *
93 */
94 function get_account_form_fields( $extra_where = '' ) {
95 global $wpdb;
96
97 $form_id = uwp_get_register_form_id( get_current_user_id() );
98 $table_name = uwp_get_table_prefix() . 'uwp_form_fields';
99 $include_admin_use = apply_filters( 'uwp_account_include_admin_use_only_fields', false );
100 if ( $include_admin_use ) {
101 $fields = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM " . $table_name . " WHERE form_type = %s AND is_active = '1' AND is_register_only_field = '0' AND htmlvar_name != 'password' AND form_id = %s" . $extra_where . " ORDER BY sort_order ASC", array( 'account', $form_id ) ) );
102 } else {
103 $fields = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM " . $table_name . " WHERE form_type = %s AND is_active = '1' AND for_admin_use != '1' AND is_register_only_field = '0' AND htmlvar_name != 'password' AND form_id = %s" . $extra_where . " ORDER BY sort_order ASC", array( 'account', $form_id ) ) );
104 }
105
106 return $fields;
107 }
108
109 /**
110 * Returns the change password form fields.
111 *
112 * @return array Form fields.
113 * @package userswp
114 *
115 * @since 1.0.0
116 */
117 function get_change_form_fields() {
118 global $wpdb;
119 $table_name = uwp_get_table_prefix() . 'uwp_form_fields';
120 $enable_old_password = uwp_get_option( 'change_enable_old_password', false );
121 $user_id = get_current_user_id();
122 if($user_id && 1 == get_user_meta($user_id, 'is_uwp_social_login_no_password', true)){
123 $enable_old_password = 0;
124 }
125 if ( $enable_old_password == '1' ) {
126 $fields = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM " . $table_name . " WHERE form_type = %s AND is_active = '1' AND for_admin_use != '1' ORDER BY sort_order ASC", array( 'change' ) ) );
127 } else {
128 $fields = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM " . $table_name . " WHERE form_type = %s AND is_active = '1' AND for_admin_use != '1' AND htmlvar_name != 'old_password' ORDER BY sort_order ASC", array( 'change' ) ) );
129 }
130
131 return $fields;
132 }
133
134 /**
135 * Validates the submitted form data.
136 *
137 * @param array $data Submitted form data
138 * @param string $type Form type.
139 * @param array|bool $fields Fields applicable for validation.
140 *
141 * @return array|mixed|WP_Error Validated form data.
142 * @since 1.0.0
143 * @package userswp
144 *
145 */
146 function uwp_validate_fields( $data, $type, $fields = false ) {
147 $validation = new UsersWP_Validation();
148
149 return $validation->validate_fields( $data, $type, $fields );
150 }
151
152 /**
153 * Returns form field label. If empty returns the field title.
154 *
155 * @param object $field Field info.
156 *
157 * @return string Label.
158 * @since 1.0.0
159 * @package userswp
160 *
161 */
162 function uwp_get_form_label( $field ) {
163 if ( isset( $field->form_label ) && ! empty( $field->form_label ) ) {
164 $label = __( $field->form_label, 'userswp' );
165 } else {
166 $label = __( $field->site_title, 'userswp' );
167 }
168
169 return apply_filters( 'uwp_get_form_label', stripslashes( esc_attr( $label ) ), $field );
170 }
171
172 /**
173 * Returns placeholder for field.
174 *
175 * @param object $field Field info.
176 *
177 * @return string Label.
178 */
179 function uwp_get_field_placeholder( $field ) {
180
181 if ( isset( $field->field_type ) && in_array( $field->field_type, array( 'select', 'multiselect' ) ) ) {
182 if ( isset( $field->placeholder_value ) && ! empty( $field->placeholder_value ) ) {
183 $placeholder = __( $field->placeholder_value, 'userswp' );
184 } else {
185 $placeholder = wp_sprintf( __( 'Choose %s&hellip;', 'userswp' ), uwp_get_form_label( $field ) );
186 }
187 } else {
188 if ( isset( $field->placeholder_value ) && ! empty( $field->placeholder_value ) ) {
189 $placeholder = __( $field->placeholder_value, 'userswp' );
190 } else {
191 $placeholder = uwp_get_form_label( $field );
192 }
193 }
194
195 if ( isset( $field->is_required ) && ! empty( $field->is_required ) ) {
196 $placeholder .= ' *';
197 }
198
199 return apply_filters( 'uwp_get_field_placeholder', stripslashes( $placeholder ), $field );
200 }
201
202 /**
203 * Returns description for field.
204 *
205 * @param object $field Field info.
206 * @param string $default Default value.
207 *
208 * @return string Label.
209 */
210 function uwp_get_field_description( $field, $default = '' ) {
211
212 if ( isset( $field->help_text ) && ! empty( $field->help_text ) ) {
213 $desc = __( $field->help_text, 'userswp' );
214 } else {
215 $desc = $default;
216 }
217
218 return apply_filters( 'uwp_get_field_description', stripslashes( $desc ), $field );
219 }
220
221 /**
222 * Gets the custom field info for given key.
223 *
224 * @param string $htmlvar_name Custom field key.
225 * @param string $form_type Form type.
226 *
227 * @return object Field info.
228 * @package userswp
229 *
230 * @since 1.0.0
231 */
232 function uwp_get_custom_field_info( $htmlvar_name, $form_type = 'account' ) {
233 global $wpdb, $custom_field_info;
234 $table_name = uwp_get_table_prefix() . 'uwp_form_fields';
235 $cache_key = $htmlvar_name . '_' . $form_type;
236
237 // Return cached field data.
238 if ( isset( $custom_field_info[ $cache_key ] ) ) {
239 return $custom_field_info[ $cache_key ];
240 }
241
242 if ( $form_type ) {
243 $field = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM " . $table_name . " WHERE htmlvar_name = %s AND form_type = %s", array(
244 $htmlvar_name,
245 $form_type
246 ) ) );
247 } else {
248 $field = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM " . $table_name . " WHERE htmlvar_name = %s", array( $htmlvar_name ) ) );
249 }
250
251 // Cache the field data.
252 $custom_field_info[ $cache_key ] = $field;
253
254 return $field;
255 }
256
257 function uwp_resend_activation_mail($user_id) {
258
259 if ( ! $user_id ) {
260 return false;
261 }
262
263 if ( 'email_unconfirmed' == get_user_meta( $user_id, 'uwp_mod', true ) ) {
264 $user_data = get_userdata( $user_id );
265
266 $activation_link = uwp_get_activation_link( $user_id );
267
268 if ( $activation_link ) {
269
270 $message = __( 'To activate your account, visit the following address:', 'userswp' ) . "\r\n\r\n";
271
272 $message .= "<a href='" . esc_url( $activation_link ) . "' target='_blank'>" . esc_url( $activation_link ) . "</a>" . "\r\n";
273
274 $activate_message = '<p><b>' . __( 'Please activate your account :', 'userswp' ) . '</b></p><p>' . $message . '</p>';
275
276 $activate_message = apply_filters( 'uwp_activation_mail_message', $activate_message, $user_id );
277
278 $email_vars = array(
279 'user_id' => $user_id,
280 'login_details' => $activate_message,
281 'activation_link' => $activation_link,
282 );
283
284 $send_result = UsersWP_Mails::send( $user_data->user_email, 'registration_activate', $email_vars );
285
286 return $send_result;
287 }
288 }
289
290 return true;
291 }
292
293 /**
294 * Check font awesome icon or not.
295 *
296 * @param string $icon Font awesome icon.
297 * @return bool True if font awesome icon.
298 */
299 function uwp_is_fa_icon( $icon ) {
300 $return = false;
301 if ( $icon != '' ) {
302 $fa_icon = trim( $icon );
303 if ( strpos( $fa_icon, 'fa fa-' ) === 0 || strpos( $fa_icon, 'fas fa-' ) === 0 || strpos( $fa_icon, 'far fa-' ) === 0 || strpos( $fa_icon, 'fab fa-' ) === 0 || strpos( $fa_icon, 'fa-' ) === 0 ) {
304 $return = true;
305 }
306 }
307 return apply_filters( 'uwp_is_fa_icon', $return, $icon );
308 }
309
310 /**
311 * Check icon url.
312 *
313 * @param string $icon Icon url.
314 * @return bool True if icon url.
315 */
316 function uwp_is_icon_url( $icon ) {
317 $return = false;
318 if ( $icon != '' ) {
319 $icon = trim( $icon );
320 if ( strpos( $icon, 'http://' ) === 0 || strpos( $icon, 'https://' ) === 0 ) {
321 $return = true;
322 }
323 }
324 return apply_filters( 'uwp_is_icon_url', $return, $icon );
325 }
326
327 /**
328 * Get the field icon.
329 *
330 * @since 1.0.12
331 * @package userswp
332 *
333 * @param string $value Field icon value.
334 * @return string Field icon element.
335 */
336 function uwp_get_field_icon( $value ) {
337 $field_icon = $value;
338
339 if ( ! empty( $value ) ) {
340 if (strpos($value, 'http') === 0) {
341 $field_icon = '<span class="uwp_field_icon" style="background: url(' . esc_url( $value ) . ') no-repeat left center;padding-left:14px;background-size:100% auto;margin-right:5px"></span>';
342 } else {
343 $field_icon = '<i class="uwp_field_icon ' . esc_attr( $value ) . '"></i>';
344 }
345 }
346
347 return apply_filters( 'uwp_get_field_icon', $field_icon, $value );
348 }
349
350 function uwp_get_registration_form_actions(){
351 $registration_options = array(
352 'auto_approve' => __('Auto Approve', 'userswp'),
353 'auto_approve_login' => __('Auto Approve + Auto Login', 'userswp'),
354 'require_email_activation' => __('Require Email Activation', 'userswp'),
355 );
356
357 return apply_filters('uwp_registration_status_options', $registration_options);
358 }
359
360 function uwp_get_register_forms_dropdown_options($include_forms = array()) {
361
362 $get_register_form = uwp_get_option( 'multiple_registration_forms' );
363
364 $register_forms = array();
365
366 if ( ! empty( $get_register_form ) && is_array( $get_register_form ) ) {
367 foreach ( $get_register_form as $key => $register_form ) {
368 $form_id = ! empty( $register_form['id'] ) ? $register_form['id'] : '';
369 if ( ! empty( $form_id ) && $form_id > 0 ) {
370 $register_forms[ $form_id ] = ! empty( $register_form['title'] ) ? $register_form['title'] : '';
371 }
372 }
373 }
374
375 if(!empty($register_forms) && isset($include_forms) && count($include_forms) > 0){
376 foreach($register_forms as $form_id => $title){
377 if(!in_array($form_id, $include_forms)){
378 unset($register_forms[$form_id]);
379 }
380 }
381 }
382
383 return $register_forms;
384 }
385
386 function uwp_get_register_form_by( $form_id, $type = 'title' ) {
387
388 $form_title = '';
389 if ( ! empty( $form_id ) && $form_id > 0 ) {
390 $get_register_form = uwp_get_option( 'multiple_registration_forms' );
391
392 if ( ! empty( $get_register_form ) && is_array( $get_register_form ) ) {
393
394 foreach ( $get_register_form as $key => $register_form ) {
395
396 if ( ! empty( $register_form['id'] ) && $register_form['id'] == $form_id ) {
397
398 $form_title = ! empty( $register_form[$type] ) ? $register_form[$type] : '';
399 }
400 }
401 }
402 }
403
404 return $form_title;
405 }
406
407 function uwp_get_form_id_by_type($form_type){
408 $form_id = 1;
409 if ( isset($form_type) && ! empty( $form_type ) ) {
410 $get_register_form = uwp_get_option( 'multiple_registration_forms' );
411
412 if ( ! empty( $get_register_form ) && is_array( $get_register_form ) ) {
413
414 foreach ( $get_register_form as $key => $register_form ) {
415
416 if ( ! empty( $register_form['title'] ) && $form_type == sanitize_title_with_dashes($register_form['title']) ) {
417
418 $form_id = $register_form['id'];
419 }
420 }
421 }
422 }
423
424 return $form_id;
425 }
426
427 function uwp_get_next_register_form_id() {
428
429 global $wpdb;
430
431 $extras_table_name = uwp_get_table_prefix() . 'uwp_form_extras';
432 $fields = $wpdb->get_results( "SELECT MAX(`form_id`) AS last_added FROM `$extras_table_name`" );
433 $last_added = ! empty( $fields['0']->last_added ) ? (int) $fields['0']->last_added : 1;
434
435 $register_forms = uwp_get_option( 'multiple_registration_forms' );
436 $register_form_ids = array();
437 if ( ! empty( $register_forms ) && is_array( $register_forms ) ) {
438 foreach ( $register_forms as $key => $register_form ) {
439 if ( ! empty( $register_form['id'] ) ) {
440 $register_form_ids[] = $register_form['id'];
441 }
442 }
443 }
444
445 $form_max_id = ! empty( $register_form_ids ) ? max( $register_form_ids ) : 0;
446
447 $next_form = max( $last_added, $form_max_id );
448 $next_form = ! empty( $next_form ) ? $next_form + 1 : 0;
449
450 return ! empty( $next_form ) ? $next_form : 1;
451 }
452
453 function uwp_get_register_form_id( $user_id ) {
454
455 if ( empty( $user_id ) ) {
456 return 1;
457 }
458
459 $form_id = get_user_meta( $user_id, '_uwp_register_form_id', true );
460
461 return ! empty( $form_id ) ? (int) $form_id : 1;
462 }
463
464 function uwp_get_register_fields_htmlvar( $form_id = 1 ) {
465
466 $fields = get_register_form_fields( $form_id );
467
468 $html_var = array();
469
470 if ( ! empty( $fields ) && is_array( $fields ) ) {
471
472 foreach ( $fields as $key => $field ) {
473
474 if ( ! empty( $field->htmlvar_name ) ) {
475
476 $html_var[] = $field->htmlvar_name;
477 }
478 }
479 }
480
481 return $html_var;
482 }
483
484 function uwp_get_unique_custom_fields($fields, $key = 'htmlvar_name'){
485 if(empty($fields)){
486 return array();
487 }
488
489 $temp_array = array();
490 $i = 0;
491 $key_array = array();
492
493 foreach($fields as $field) {
494 if(is_object($field)){
495 $val = $field->$key;
496 } else {
497 $val = $field[$key];
498 }
499
500 if (!in_array($val, $key_array)) {
501 $key_array[$i] = $val;
502 $temp_array[$val] = $field;
503 }
504 $i++;
505 }
506
507 return $temp_array;
508 }
509
510 function uwp_get_default_form_data(){
511 $fields = array();$i = 1;
512 $user_role = get_option('default_role');
513
514 $account_fields = UsersWP_Activator::uwp_default_custom_fields_account();
515 if(!empty($account_fields)){
516 foreach ($account_fields as $account_field){
517 $fields[$account_field['htmlvar_name']] = $i;
518 $i++;
519 }
520 }
521
522 $reg_fields = get_register_form_fields();
523
524 if ( ! empty( $reg_fields ) && is_array( $reg_fields ) ) {
525 foreach ( $reg_fields as $key => $field ) {
526 if ( isset( $field->htmlvar_name ) && ! empty( $field->htmlvar_name ) && $field->htmlvar_name == 'user_role' && ! empty( $field->option_values ) ) {
527 $user_role = $field->option_values;
528 $obj = new UsersWP_Form_Builder();
529 $obj->admin_form_field_delete( $field->id, true, 1 );
530 }
531 }
532 }
533
534 $data = array(
535 array(
536 'id' => 1,
537 'title' => __('Default','userswp'),
538 'user_role' => $user_role,
539 'reg_action' => uwp_get_option( 'uwp_registration_action', 'auto_approve' ),
540 'redirect_to' => (int) uwp_get_option( 'register_redirect_to', - 1 ),
541 'custom_url' => uwp_get_option( 'register_redirect_custom_url', home_url() ),
542 'gdpr_page' => (int) uwp_get_option('register_gdpr_page', false),
543 'tos_page' => (int) uwp_get_option('register_terms_page', false),
544 'fields' => $fields
545 )
546 );
547
548 return $data;
549 }
550
551 function uwp_get_upload_image_size($type = "avatar"){
552
553 if ( $type == 'avatar' ) {
554 $width = uwp_get_option( 'profile_avatar_width', 150 );
555 $width = empty($width) ? 150 : $width;
556 $value['width'] = apply_filters( 'uwp_avatar_image_width', $width );
557
558 $height = uwp_get_option( 'profile_avatar_height', 150 );
559 $height = empty($height) ? 150 : $height;
560 $value['height'] = apply_filters( 'uwp_avatar_image_height', $height );
561 } else {
562 $width = uwp_get_option( 'profile_banner_width', 1000 );
563 $width = empty($width) ? 1000 : $width;
564 $value['width'] = apply_filters( 'uwp_banner_image_width', $width );
565
566 $height = uwp_get_option( 'profile_banner_height', 300 );
567 $height = empty($height) ? 300 : $height;
568 $value['height'] = apply_filters( 'uwp_banner_image_height', $height );
569 }
570
571 return $value;
572 }