| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 3 |
|
| 4 |
/* handle field output */ |
| 5 |
function wppb_display_name_handler( $output, $form_location, $field, $user_id, $field_check_errors, $request_data ){ |
| 6 |
$item_title = apply_filters( 'wppb_'.$form_location.'_display-name_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 |
if ( $form_location == 'edit_profile' ){ |
| 10 |
$error_mark = ( ( $field['required'] == 'Yes' ) ? '<span class="wppb-required" title="'.wppb_required_field_error($field["field-title"]).'">*</span>' : '' ); |
| 11 |
|
| 12 |
if ( array_key_exists( $field['id'], $field_check_errors ) ) |
| 13 |
$error_mark = '<img src="'.WPPB_PLUGIN_URL.'assets/images/pencil_delete.png" title="'.wppb_required_field_error($field["field-title"]).'"/>'; |
| 14 |
|
| 15 |
/* |
| 16 |
* Create the options for the display_name drop-down |
| 17 |
* They are created same as in user-edit.php of the WordPress core |
| 18 |
*/ |
| 19 |
$user_data = get_userdata( $user_id ); |
| 20 |
$public_display = array(); |
| 21 |
$public_display['display_nickname'] = $user_data->nickname; |
| 22 |
$public_display['display_username'] = $user_data->user_login; |
| 23 |
|
| 24 |
if ( !empty($user_data->first_name) ) |
| 25 |
$public_display['display_firstname'] = $user_data->first_name; |
| 26 |
|
| 27 |
if ( !empty($user_data->last_name) ) |
| 28 |
$public_display['display_lastname'] = $user_data->last_name; |
| 29 |
|
| 30 |
if ( !empty($user_data->first_name) && !empty($user_data->last_name) ) { |
| 31 |
$public_display['display_firstlast'] = $user_data->first_name . ' ' . $user_data->last_name; |
| 32 |
$public_display['display_lastfirst'] = $user_data->last_name . ' ' . $user_data->first_name; |
| 33 |
} |
| 34 |
|
| 35 |
if ( !in_array( $user_data->display_name, $public_display ) ) // Only add this if it isn't duplicated elsewhere |
| 36 |
$public_display = array( 'display_displayname' => $user_data->display_name ) + $public_display; |
| 37 |
|
| 38 |
$public_display = array_map( 'trim', $public_display ); |
| 39 |
$public_display = array_unique( $public_display ); |
| 40 |
|
| 41 |
$public_display = apply_filters( 'wppb_display_name_options', $public_display ); |
| 42 |
|
| 43 |
$output = '<label for="display_name">'.$item_title.$error_mark.'</label>'; |
| 44 |
$output .= '<select class="default_field_display-name '. apply_filters( 'wppb_fields_extra_css_class', '', $field ) .'" name="display_name" id="display-name">'; |
| 45 |
|
| 46 |
foreach( $public_display as $display_name_option ) { |
| 47 |
$output .= '<option ' . selected( $user_data->display_name, $display_name_option, false ) . '>' . $display_name_option . '</option>'; |
| 48 |
} |
| 49 |
|
| 50 |
$output .= '</select>'; |
| 51 |
|
| 52 |
if( !empty( $item_description ) ) |
| 53 |
$output .= '<span class="wppb-description-delimiter">'. $item_description .'</span>'; |
| 54 |
|
| 55 |
} |
| 56 |
|
| 57 |
return apply_filters( 'wppb_'.$form_location.'_display-name', $output, $form_location, $field, $user_id, $field_check_errors, $request_data ); |
| 58 |
} |
| 59 |
add_filter( 'wppb_output_form_field_default-display-name-publicly-as', 'wppb_display_name_handler', 10, 6 ); |
| 60 |
|
| 61 |
|
| 62 |
/* handle field validation */ |
| 63 |
function wppb_check_display_name_value( $message, $field, $request_data, $form_location ){ |
| 64 |
if( $form_location != 'register' ){ |
| 65 |
if ($field['required'] == 'Yes') { |
| 66 |
if ((isset($request_data['display_name']) && (trim($request_data['display_name']) == '')) || !isset($request_data['display_name'])) { |
| 67 |
return wppb_required_field_error($field["field-title"]); |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
return $message; |
| 73 |
} |
| 74 |
add_filter( 'wppb_check_form_field_default-display-name-publicly-as', 'wppb_check_display_name_value', 10, 4 ); |
| 75 |
|
| 76 |
|
| 77 |
/* handle field save */ |
| 78 |
function wppb_userdata_add_display_name( $userdata, $global_request, $form_args ){ |
| 79 |
if( wppb_field_exists_in_form( 'Default - Display name publicly as', $form_args ) ) { |
| 80 |
if (isset($global_request['display_name'])) |
| 81 |
$userdata['display_name'] = trim(sanitize_text_field($global_request['display_name'])); |
| 82 |
} |
| 83 |
|
| 84 |
return $userdata; |
| 85 |
} |
| 86 |
add_filter( 'wppb_build_userdata', 'wppb_userdata_add_display_name', 10, 3 ); |