| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 3 |
|
| 4 |
/* handle field output */ |
| 5 |
function wppb_username_handler( $output, $form_location, $field, $user_id, $field_check_errors, $request_data ){ |
| 6 |
$item_title = apply_filters( 'wppb_'.$form_location.'_username_item_title', wppb_icl_t( 'plugin profile-builder-pro', 'default_field_'.$field['id'].'_title_translation', $field['field-title'], true ) ); |
| 7 |
$item_description = wppb_icl_t( 'plugin profile-builder-pro', 'default_field_'.$field['id'].'_description_translation', $field['description'], true ); |
| 8 |
|
| 9 |
$input_value = ( ( $form_location == 'edit_profile' ) ? get_the_author_meta( 'user_login', $user_id ) : '' ); |
| 10 |
|
| 11 |
$input_value = ( ( trim( $input_value ) == '' ) ? $field['default-value'] : $input_value ); |
| 12 |
|
| 13 |
$input_value = ( isset( $request_data['username'] ) ? trim( $request_data['username'] ) : $input_value ); |
| 14 |
$input_value = apply_filters( 'wppb_form_username_field_value', $input_value, $field, $form_location ); |
| 15 |
|
| 16 |
if ( $form_location != 'back_end' ){ |
| 17 |
$error_mark = ( ( $field['required'] == 'Yes' ) ? '<span class="wppb-required" title="'.wppb_required_field_error($field["field-title"]).'">*</span>' : '' ); |
| 18 |
|
| 19 |
if ( array_key_exists( $field['id'], $field_check_errors ) ) |
| 20 |
$error_mark = '<img src="'.WPPB_PLUGIN_URL.'assets/images/pencil_delete.png" title="'.wppb_required_field_error($field["field-title"]).'"/>'; |
| 21 |
|
| 22 |
$readonly = ( ( $form_location == 'edit_profile' ) ? ' disabled="disabled"' : '' ); |
| 23 |
|
| 24 |
$extra_attr = apply_filters( 'wppb_extra_attribute', '', $field, $form_location ); |
| 25 |
|
| 26 |
$output = ' |
| 27 |
<label for="username">'.$item_title.$error_mark.'</label> |
| 28 |
<input class="text-input default_field_username '. apply_filters( 'wppb_fields_extra_css_class', '', $field ) .'" name="username" maxlength="'. apply_filters( 'wppb_maximum_character_length', 60, $field ) .'" type="text" id="username" value="'. esc_attr( $input_value ) .'" '.$readonly.' '. $extra_attr .'/>'; |
| 29 |
if( !empty( $item_description ) ) |
| 30 |
$output .= '<span class="wppb-description-delimiter">'.$item_description.'</span>'; |
| 31 |
} |
| 32 |
|
| 33 |
return apply_filters( 'wppb_'.$form_location.'_username', $output, $form_location, $field, $user_id, $field_check_errors, $request_data ); |
| 34 |
} |
| 35 |
add_filter( 'wppb_output_form_field_default-username', 'wppb_username_handler', 10, 6 ); |
| 36 |
|
| 37 |
|
| 38 |
/* handle field validation */ |
| 39 |
function wppb_check_username_value( $message, $field, $request_data, $form_location ){ |
| 40 |
global $wpdb; |
| 41 |
|
| 42 |
if( $field['required'] == 'Yes' ){ |
| 43 |
if( ( isset( $request_data['username'] ) && ( trim( $request_data['username'] ) == '' ) ) || ( $form_location == 'register' && !isset( $request_data['username'] ) ) ){ |
| 44 |
return wppb_required_field_error($field["field-title"]); |
| 45 |
} |
| 46 |
|
| 47 |
} |
| 48 |
|
| 49 |
if( !empty( $request_data['username'] ) ){ |
| 50 |
if( $form_location == 'register' ) { |
| 51 |
if( username_exists($request_data['username'] ) ){ |
| 52 |
return __('This username already exists.', 'profile-builder') . '<br/>' . __('Please try a different one!', 'profile-builder'); |
| 53 |
} |
| 54 |
if (!validate_username($request_data['username'])) { |
| 55 |
return __('This username is invalid because it uses illegal characters.', 'profile-builder') . '<br/>' . __('Please enter a valid username.', 'profile-builder'); |
| 56 |
} |
| 57 |
// WordPress core rejects usernames longer than 60 characters in wp_insert_user(). |
| 58 |
if ( mb_strlen( sanitize_user( trim( $request_data['username'] ) ) ) > 60 ) { |
| 59 |
return __( 'This username is too long. It must be 60 characters or fewer.', 'profile-builder' ); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
$wppb_generalSettings = get_option('wppb_general_settings'); |
| 64 |
if ( isset($wppb_generalSettings['emailConfirmation']) && $wppb_generalSettings['emailConfirmation'] == 'yes' ){ |
| 65 |
|
| 66 |
if( is_multisite() && $request_data['username'] != preg_replace( '/\s+/', '', $request_data['username'] ) ){ |
| 67 |
return __( 'This username is invalid because it uses illegal characters.', 'profile-builder' ) .'<br/>'. __( 'Please enter a valid username.', 'profile-builder' ); |
| 68 |
} |
| 69 |
|
| 70 |
// Only pending signup rows should reserve a username during Email Confirmation |
| 71 |
// - activated rows can remain in the signups table after confirmation, so they must not block a new registration |
| 72 |
// - this keeps the username reservation check aligned with the email reservation check, which also only considers pending signups |
| 73 |
$userSignup = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM ".$wpdb->prefix."signups WHERE user_login = %s AND active = 0", $request_data['username'] ) ); |
| 74 |
if ( !empty( $userSignup ) ){ |
| 75 |
return __( 'This username is already reserved to be used soon.', 'profile-builder') .'<br/>'. __( 'Please try a different one!', 'profile-builder' ); |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
return $message; |
| 81 |
} |
| 82 |
add_filter( 'wppb_check_form_field_default-username', 'wppb_check_username_value', 10, 4 ); |
| 83 |
|
| 84 |
|
| 85 |
/* handle field save */ |
| 86 |
function wppb_userdata_add_username( $userdata, $global_request, $form_args ){ |
| 87 |
if( wppb_field_exists_in_form( 'Default - Username', $form_args ) ) { |
| 88 |
if (isset($global_request['username'])) |
| 89 |
$userdata['user_login'] = sanitize_user(trim($global_request['username'])); |
| 90 |
} |
| 91 |
|
| 92 |
return $userdata; |
| 93 |
} |
| 94 |
add_filter( 'wppb_build_userdata', 'wppb_userdata_add_username', 10, 3 ); |