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

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