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

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

594 lines 18.4 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 extras.form_type = 'register' 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 extras.form_id = %d AND fields.form_id = %s ORDER BY extras.sort_order ASC", array( 'account', $form_id, $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 } elseif ( isset( $field->placeholder_value ) && ! empty( $field->placeholder_value ) ) {
188 $placeholder = __( $field->placeholder_value, 'userswp' );
189 } else {
190 $placeholder = uwp_get_form_label( $field );
191 }
192
193 if ( isset( $field->is_required ) && ! empty( $field->is_required ) ) {
194 $placeholder .= ' *';
195 }
196
197 return apply_filters( 'uwp_get_field_placeholder', stripslashes( $placeholder ), $field );
198 }
199
200 /**
201 * Returns description for field.
202 *
203 * @param object $field Field info.
204 * @param string $default Default value.
205 *
206 * @return string Label.
207 */
208 function uwp_get_field_description( $field, $default = '' ) {
209
210 if ( isset( $field->help_text ) && ! empty( $field->help_text ) ) {
211 $desc = __( $field->help_text, 'userswp' );
212 } else {
213 $desc = $default;
214 }
215
216 return apply_filters( 'uwp_get_field_description', stripslashes( $desc ), $field );
217 }
218
219 /**
220 * Gets the custom field info for given key.
221 *
222 * @param string $htmlvar_name Custom field key.
223 * @param string $form_type Form type.
224 *
225 * @return object Field info.
226 * @package userswp
227 *
228 * @since 1.0.0
229 */
230 function uwp_get_custom_field_info( $htmlvar_name, $form_type = 'account' ) {
231 global $wpdb, $custom_field_info;
232 $table_name = uwp_get_table_prefix() . 'uwp_form_fields';
233 $cache_key = $htmlvar_name . '_' . $form_type;
234
235 // Return cached field data.
236 if ( isset( $custom_field_info[ $cache_key ] ) ) {
237 return $custom_field_info[ $cache_key ];
238 }
239
240 if ( $form_type ) {
241 $field = $wpdb->get_row(
242 $wpdb->prepare(
243 'SELECT * FROM ' . $table_name . ' WHERE htmlvar_name = %s AND form_type = %s',
244 array(
245 $htmlvar_name,
246 $form_type,
247 )
248 )
249 );
250 } else {
251 $field = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM ' . $table_name . ' WHERE htmlvar_name = %s', array( $htmlvar_name ) ) );
252 }
253
254 // Cache the field data.
255 $custom_field_info[ $cache_key ] = $field;
256
257 return $field;
258 }
259
260 function uwp_resend_activation_mail( $user_id ) {
261
262 if ( ! $user_id ) {
263 return false;
264 }
265
266 if ( 'email_unconfirmed' == get_user_meta( $user_id, 'uwp_mod', true ) ) {
267 $user_data = get_userdata( $user_id );
268
269 $activation_link = uwp_get_activation_link( $user_id );
270
271 if ( $activation_link ) {
272
273 $message = __( 'To activate your account, visit the following address:', 'userswp' ) . "\r\n\r\n";
274
275 $message .= "<a href='" . esc_url( $activation_link ) . "' target='_blank'>" . esc_url( $activation_link ) . '</a>' . "\r\n";
276
277 $activate_message = '<p><b>' . __( 'Please activate your account :', 'userswp' ) . '</b></p><p>' . $message . '</p>';
278
279 $activate_message = apply_filters( 'uwp_activation_mail_message', $activate_message, $user_id );
280
281 $email_vars = array(
282 'user_id' => $user_id,
283 'login_details' => $activate_message,
284 'activation_link' => $activation_link,
285 );
286
287 $send_result = UsersWP_Mails::send( $user_data->user_email, 'registration_activate', $email_vars );
288
289 return $send_result;
290 }
291 }
292
293 return true;
294 }
295
296 /**
297 * Check font awesome icon or not.
298 *
299 * @param string $icon Font awesome icon.
300 * @return bool True if font awesome icon.
301 */
302 function uwp_is_fa_icon( $icon ) {
303 $return = false;
304 if ( $icon != '' ) {
305 $fa_icon = trim( $icon );
306 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 ) {
307 $return = true;
308 }
309 }
310 return apply_filters( 'uwp_is_fa_icon', $return, $icon );
311 }
312
313 /**
314 * Check icon url.
315 *
316 * @param string $icon Icon url.
317 * @return bool True if icon url.
318 */
319 function uwp_is_icon_url( $icon ) {
320 $return = false;
321 if ( $icon != '' ) {
322 $icon = trim( $icon );
323 if ( strpos( $icon, 'http://' ) === 0 || strpos( $icon, 'https://' ) === 0 ) {
324 $return = true;
325 }
326 }
327 return apply_filters( 'uwp_is_icon_url', $return, $icon );
328 }
329
330 /**
331 * Get the field icon.
332 *
333 * @since 1.0.12
334 * @package userswp
335 *
336 * @param string $value Field icon value.
337 * @return string Field icon element.
338 */
339 function uwp_get_field_icon( $value ) {
340 $field_icon = $value;
341
342 if ( ! empty( $value ) ) {
343 if ( strpos( $value, 'http' ) === 0 ) {
344 $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>';
345 } else {
346 $field_icon = '<i class="uwp_field_icon ' . esc_attr( $value ) . '"></i>';
347 }
348 }
349
350 return apply_filters( 'uwp_get_field_icon', $field_icon, $value );
351 }
352
353 function uwp_get_registration_form_actions() {
354 $registration_options = array(
355 'auto_approve' => __( 'Auto Approve', 'userswp' ),
356 'auto_approve_login' => __( 'Auto Approve + Auto Login', 'userswp' ),
357 'require_email_activation' => __( 'Require Email Activation', 'userswp' ),
358 );
359
360 return apply_filters( 'uwp_registration_status_options', $registration_options );
361 }
362
363 function uwp_get_register_forms_dropdown_options( $include_forms = array() ) {
364
365 $get_register_form = uwp_get_option( 'multiple_registration_forms' );
366
367 $register_forms = array();
368
369 if ( ! empty( $get_register_form ) && is_array( $get_register_form ) ) {
370 foreach ( $get_register_form as $key => $register_form ) {
371 $form_id = ! empty( $register_form['id'] ) ? $register_form['id'] : '';
372 if ( ! empty( $form_id ) && $form_id > 0 ) {
373 $register_forms[ $form_id ] = ! empty( $register_form['title'] ) ? $register_form['title'] : '';
374 }
375 }
376 }
377
378 if ( ! empty( $register_forms ) && isset( $include_forms ) && count( $include_forms ) > 0 ) {
379 foreach ( $register_forms as $form_id => $title ) {
380 if ( ! in_array( $form_id, $include_forms ) ) {
381 unset( $register_forms[ $form_id ] );
382 }
383 }
384 }
385
386 return $register_forms;
387 }
388
389 function uwp_get_register_form_by( $form_id, $type = 'title' ) {
390
391 $form_title = '';
392 if ( ! empty( $form_id ) && $form_id > 0 ) {
393 $get_register_form = uwp_get_option( 'multiple_registration_forms' );
394
395 if ( ! empty( $get_register_form ) && is_array( $get_register_form ) ) {
396
397 foreach ( $get_register_form as $key => $register_form ) {
398
399 if ( ! empty( $register_form['id'] ) && $register_form['id'] == $form_id ) {
400
401 $form_title = ! empty( $register_form[ $type ] ) ? $register_form[ $type ] : '';
402 }
403 }
404 }
405 }
406
407 return $form_title;
408 }
409
410 function uwp_get_form_id_by_type( $form_type ) {
411 $form_id = 1;
412 if ( isset( $form_type ) && ! empty( $form_type ) ) {
413 $get_register_form = uwp_get_option( 'multiple_registration_forms' );
414
415 if ( ! empty( $get_register_form ) && is_array( $get_register_form ) ) {
416
417 foreach ( $get_register_form as $key => $register_form ) {
418
419 if ( ! empty( $register_form['title'] ) && $form_type == sanitize_title_with_dashes( $register_form['title'] ) ) {
420
421 $form_id = $register_form['id'];
422 }
423 }
424 }
425 }
426
427 return $form_id;
428 }
429
430 function uwp_get_next_register_form_id() {
431
432 global $wpdb;
433
434 $extras_table_name = uwp_get_table_prefix() . 'uwp_form_extras';
435 $fields = $wpdb->get_results( "SELECT MAX(`form_id`) AS last_added FROM `$extras_table_name`" );
436 $last_added = ! empty( $fields['0']->last_added ) ? (int) $fields['0']->last_added : 1;
437
438 $register_forms = uwp_get_option( 'multiple_registration_forms' );
439 $register_form_ids = array();
440 if ( ! empty( $register_forms ) && is_array( $register_forms ) ) {
441 foreach ( $register_forms as $key => $register_form ) {
442 if ( ! empty( $register_form['id'] ) ) {
443 $register_form_ids[] = $register_form['id'];
444 }
445 }
446 }
447
448 $form_max_id = ! empty( $register_form_ids ) ? max( $register_form_ids ) : 0;
449
450 $next_form = max( $last_added, $form_max_id );
451 $next_form = ! empty( $next_form ) ? $next_form + 1 : 0;
452
453 return ! empty( $next_form ) ? $next_form : 1;
454 }
455
456 function uwp_get_register_form_id( $user_id ) {
457
458 if ( empty( $user_id ) ) {
459 return 1;
460 }
461
462 $form_id = get_user_meta( $user_id, '_uwp_register_form_id', true );
463
464 return ! empty( $form_id ) ? (int) $form_id : 1;
465 }
466
467 /**
468 * Get user regsitration form by form ID.
469 *
470 * @param int $form_id The ID of the registration form.
471 * @return array|false The user type array if found, false otherwise.
472 */
473 function uwp_get_user_register_form( int $form_id ) {
474 $register_forms = (array) UsersWP_User_Types::get_register_forms();
475
476 foreach ( $register_forms as $register_form ) {
477 if ( isset( $register_form['id'] ) && (int) $register_form['id'] === (int) $form_id ) {
478 return $register_form;
479 }
480 }
481
482 return false;
483 }
484
485 function uwp_get_register_fields_htmlvar( $form_id = 1 ) {
486
487 $fields = get_register_form_fields( $form_id );
488
489 $html_var = array();
490
491 if ( ! empty( $fields ) && is_array( $fields ) ) {
492
493 foreach ( $fields as $key => $field ) {
494
495 if ( ! empty( $field->htmlvar_name ) ) {
496
497 $html_var[] = $field->htmlvar_name;
498 }
499 }
500 }
501
502 return $html_var;
503 }
504
505 function uwp_get_unique_custom_fields( $fields, $key = 'htmlvar_name' ) {
506 if ( empty( $fields ) ) {
507 return array();
508 }
509
510 $temp_array = array();
511 $i = 0;
512 $key_array = array();
513
514 foreach ( $fields as $field ) {
515 if ( is_object( $field ) ) {
516 $val = $field->$key;
517 } else {
518 $val = $field[ $key ];
519 }
520
521 if ( ! in_array( $val, $key_array ) ) {
522 $key_array[ $i ] = $val;
523 $temp_array[ $val ] = $field;
524 }
525 ++$i;
526 }
527
528 return $temp_array;
529 }
530
531 function uwp_get_default_form_data() {
532 $fields = array();$i = 1;
533 $user_role = get_option( 'default_role' );
534
535 $account_fields = UsersWP_Activator::uwp_default_custom_fields_account();
536 if ( ! empty( $account_fields ) ) {
537 foreach ( $account_fields as $account_field ) {
538 $fields[ $account_field['htmlvar_name'] ] = $i;
539 ++$i;
540 }
541 }
542
543 $reg_fields = get_register_form_fields();
544
545 if ( ! empty( $reg_fields ) && is_array( $reg_fields ) ) {
546 foreach ( $reg_fields as $key => $field ) {
547 if ( isset( $field->htmlvar_name ) && ! empty( $field->htmlvar_name ) && $field->htmlvar_name == 'user_role' && ! empty( $field->option_values ) ) {
548 $user_role = $field->option_values;
549 $obj = new UsersWP_Form_Builder();
550 $obj->admin_form_field_delete( $field->id, true, 1 );
551 }
552 }
553 }
554
555 $data = array(
556 array(
557 'id' => 1,
558 'title' => __( 'Default', 'userswp' ),
559 'user_role' => $user_role,
560 'reg_action' => uwp_get_option( 'uwp_registration_action', 'auto_approve' ),
561 'redirect_to' => (int) uwp_get_option( 'register_redirect_to', - 1 ),
562 'custom_url' => uwp_get_option( 'register_redirect_custom_url', home_url() ),
563 'gdpr_page' => (int) uwp_get_option( 'register_gdpr_page', false ),
564 'tos_page' => (int) uwp_get_option( 'register_terms_page', false ),
565 'fields' => $fields,
566 ),
567 );
568
569 return $data;
570 }
571
572 function uwp_get_upload_image_size( $type = 'avatar' ) {
573
574 if ( $type == 'avatar' ) {
575 $width = uwp_get_option( 'profile_avatar_width', 150 );
576 $width = empty( $width ) ? 150 : $width;
577 $value['width'] = apply_filters( 'uwp_avatar_image_width', $width );
578
579 $height = uwp_get_option( 'profile_avatar_height', 150 );
580 $height = empty( $height ) ? 150 : $height;
581 $value['height'] = apply_filters( 'uwp_avatar_image_height', $height );
582 } else {
583 $width = uwp_get_option( 'profile_banner_width', 1000 );
584 $width = empty( $width ) ? 1000 : $width;
585 $value['width'] = apply_filters( 'uwp_banner_image_width', $width );
586
587 $height = uwp_get_option( 'profile_banner_height', 300 );
588 $height = empty( $height ) ? 300 : $height;
589 $value['height'] = apply_filters( 'uwp_banner_image_height', $height );
590 }
591
592 return $value;
593 }
594