← All changes
|
front-end/default-fields/display-name/display-name.php
+86
-51
2.0.2
→
4.0.3
View file →
| @@ -1,51 +1,86 @@ | ||
| 1 | -<?php | |
| 2 | -/* handle field output */ | |
| 3 | -function wppb_display_name_handler( $output, $form_location, $field, $user_id, $field_check_errors, $request_data ){ | |
| 4 | - $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'] ) ); | |
| 5 | - $item_description = wppb_icl_t( 'plugin profile-builder-pro', 'default_field_'.$field['id'].'_description_translation', $field['description'] ); | |
| 6 | - $input_value = ''; | |
| 7 | - if( $form_location == 'edit_profile' ) | |
| 8 | - $input_value = get_the_author_meta( 'display_name', $user_id ); | |
| 9 | - | |
| 10 | - if ( trim( $input_value ) == '' ) | |
| 11 | - $input_value = $field['default-value']; | |
| 12 | - | |
| 13 | - $input_value = ( isset( $request_data['display-name'] ) ? trim( $request_data['display-name'] ) : $input_value ); | |
| 14 | - | |
| 15 | - if ( $form_location != 'back_end' ){ | |
| 16 | - $error_mark = ( ( $field['required'] == 'Yes' ) ? '<span class="wppb-required" title="'.wppb_required_field_error($field["field-title"]).'">*</span>' : '' ); | |
| 17 | - | |
| 18 | - if ( array_key_exists( $field['id'], $field_check_errors ) ) | |
| 19 | - $error_mark = '<img src="'.WPPB_PLUGIN_URL.'assets/images/pencil_delete.png" title="'.wppb_required_field_error($field["field-title"]).'"/>'; | |
| 20 | - | |
| 21 | - $output = ' | |
| 22 | - <label for="display-name">'.$item_title.$error_mark.'</label> | |
| 23 | - <input class="text-input default_field_display-name" name="display-name" maxlength="'. apply_filters( 'wppb_maximum_character_length', 70 ) .'" type="text" id="display-name" value="'. esc_attr( wp_unslash( $input_value ) ) .'" />'; | |
| 24 | - if( !empty( $item_description ) ) | |
| 25 | - $output .= '<span class="wppb-description-delimiter">'. $item_description .'</span>'; | |
| 26 | - | |
| 27 | - } | |
| 28 | - | |
| 29 | - return apply_filters( 'wppb_'.$form_location.'_display-name', $output, $form_location, $field, $user_id, $field_check_errors, $request_data ); | |
| 30 | -} | |
| 31 | -add_filter( 'wppb_output_form_field_default-display-name-publicly-as', 'wppb_display_name_handler', 10, 6 ); | |
| 32 | - | |
| 33 | - | |
| 34 | -/* handle field validation */ | |
| 35 | -function wppb_check_display_name_value( $message, $field, $request_data, $form_location ){ | |
| 36 | - if ( ( isset( $request_data['display-name'] ) && ( trim( $request_data['display-name'] ) == '' ) ) && ( $field['required'] == 'Yes' ) ) | |
| 37 | - return wppb_required_field_error($field["field-title"]); | |
| 38 | - | |
| 39 | - return $message; | |
| 40 | -} | |
| 41 | -add_filter( 'wppb_check_form_field_default-display-name-publicly-as', 'wppb_check_display_name_value', 10, 4 ); | |
| 42 | - | |
| 43 | - | |
| 44 | -/* handle field save */ | |
| 45 | -function wppb_userdata_add_display_name( $userdata, $global_request ){ | |
| 46 | - if ( isset( $global_request['display-name'] ) ) | |
| 47 | - $userdata['display-name'] = trim( sanitize_text_field( $global_request['display-name'] ) ); | |
| 48 | - | |
| 49 | - return $userdata; | |
| 50 | -} | |
| 51 | -add_filter( 'wppb_build_userdata', 'wppb_userdata_add_display_name', 10, 2 ); | |
| 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 ); | |