| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Force First and Last Name as Display Name |
| 4 |
Description: Force the user field display_name to be set as the user's first and last name. |
| 5 |
Version: 1.2.2 |
| 6 |
Author: Yoohoo Plugins |
| 7 |
License: GPLv2 or Later |
| 8 |
Text Domain: force-first-last |
| 9 |
Domain Path: /languages |
| 10 |
*/ |
| 11 |
define( 'FFL_BASENAME', plugin_basename( __FILE__ ) ); |
| 12 |
|
| 13 |
/** |
| 14 |
* Load text domain |
| 15 |
*/ |
| 16 |
function ffl_load_plugin_text_domain() { |
| 17 |
load_plugin_textdomain( 'force-first-last', false, basename( dirname( __FILE__ ) ) . '/languages' ); |
| 18 |
} |
| 19 |
add_action( 'plugins_loaded', 'ffl_load_plugin_text_domain' ); |
| 20 |
|
| 21 |
/** |
| 22 |
* Generate the Display Name |
| 23 |
* |
| 24 |
*/ |
| 25 |
function ffl_generate_display_name( $first_name, $last_name ) { |
| 26 |
if ( empty( $first_name ) || empty( $last_name ) ) { |
| 27 |
return; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Adjust the display name order to be first last or last first depending on location. |
| 32 |
* |
| 33 |
* @param $display_name_order array An array of name parts. |
| 34 |
* @return $display_name A formatted display name. |
| 35 |
* |
| 36 |
*/ |
| 37 |
$display_name_order = apply_filters( 'ffl_display_name_order', array( $first_name, $last_name ), $first_name, $last_name ); |
| 38 |
$display_name = trim( $display_name_order[0] . ' ' . $display_name_order[1] ); |
| 39 |
return $display_name; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Hide Display Name field on profile page. |
| 44 |
* |
| 45 |
*/ |
| 46 |
function ffl_show_user_profile( $user ) { |
| 47 |
$hide_display_name = apply_filters( 'pmpro_ffl_hide_display_name_profile', true, $user ); |
| 48 |
|
| 49 |
if( $hide_display_name ){ |
| 50 |
?> |
| 51 |
<script> |
| 52 |
jQuery(document).ready(function() { |
| 53 |
jQuery('#display_name').parent().parent().hide(); |
| 54 |
}); |
| 55 |
</script> |
| 56 |
<?php |
| 57 |
} |
| 58 |
} |
| 59 |
add_action( 'show_user_profile', 'ffl_show_user_profile' ); |
| 60 |
add_action( 'edit_user_profile', 'ffl_show_user_profile' ); |
| 61 |
|
| 62 |
/** |
| 63 |
* Fix first last on profile saves. |
| 64 |
* |
| 65 |
*/ |
| 66 |
function ffl_save_extra_profile_fields( $user_id ) { |
| 67 |
if ( ! current_user_can( 'edit_user', $user_id ) ) { |
| 68 |
return false; |
| 69 |
} |
| 70 |
|
| 71 |
if ( isset( $_POST['first_name'] ) && isset( $_POST['last_name'] ) ) { |
| 72 |
$first_name = sanitize_text_field( trim( $_POST['first_name'] ) ); |
| 73 |
$last_name = sanitize_text_field( trim( $_POST['last_name'] ) ); |
| 74 |
$display_name = ffl_generate_display_name( $first_name, $last_name ); |
| 75 |
} else { |
| 76 |
$info = get_userdata( $user_id ); |
| 77 |
$display_name = ffl_generate_display_name( $info->first_name, $info->last_name ); |
| 78 |
if ( ! $display_name ) { |
| 79 |
$display_name = $info->user_login; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
$_POST['display_name'] = $display_name; |
| 84 |
|
| 85 |
$args = array( |
| 86 |
'ID' => $user_id, |
| 87 |
'display_name' => $display_name |
| 88 |
); |
| 89 |
wp_update_user( $args ); |
| 90 |
} |
| 91 |
add_action( 'personal_options_update', 'ffl_save_extra_profile_fields' ); |
| 92 |
add_action( 'edit_user_profile_update', 'ffl_save_extra_profile_fields' ); |
| 93 |
add_action( 'pmpro_personal_options_update', 'ffl_save_extra_profile_fields' ); |
| 94 |
|
| 95 |
/** |
| 96 |
* Removes the "Display Name" field on the Paid Memberships Pro frontned profile. |
| 97 |
* |
| 98 |
*/ |
| 99 |
function ffl_pmpro_member_profile_edit_user_object_fields( $user_fields ) { |
| 100 |
unset( $user_fields['display_name'] ); |
| 101 |
return $user_fields; |
| 102 |
} |
| 103 |
add_filter( 'pmpro_member_profile_edit_user_object_fields', 'ffl_pmpro_member_profile_edit_user_object_fields' ); |
| 104 |
|
| 105 |
/** |
| 106 |
* Fix first last on register. |
| 107 |
* |
| 108 |
*/ |
| 109 |
function ffl_fix_user_display_name( $user_id ) { |
| 110 |
// Get the user's first and last name. |
| 111 |
$info = get_userdata( $user_id ); |
| 112 |
$display_name = ffl_generate_display_name( $info->first_name, $info->last_name ); |
| 113 |
if ( ! $display_name ) { |
| 114 |
$display_name = $info->user_login; |
| 115 |
} |
| 116 |
|
| 117 |
$args = array( |
| 118 |
'ID' => $user_id, |
| 119 |
'display_name' => $display_name |
| 120 |
); |
| 121 |
wp_update_user( $args ); |
| 122 |
} |
| 123 |
add_action( 'user_register', 'ffl_fix_user_display_name', 20 ); |
| 124 |
|
| 125 |
/** |
| 126 |
* Add Settings Page |
| 127 |
* |
| 128 |
*/ |
| 129 |
function ffl_settings_menu_item() { |
| 130 |
add_options_page('Force First Last', __( 'Force First Last', 'force-first-last' ), 'manage_options', 'ffl_settings', 'ffl_settings_page'); |
| 131 |
} |
| 132 |
add_action( 'admin_menu', 'ffl_settings_menu_item', 20 ); |
| 133 |
|
| 134 |
/** |
| 135 |
* Settings Page Content |
| 136 |
* |
| 137 |
*/ |
| 138 |
function ffl_settings_page() { ?> |
| 139 |
<style> |
| 140 |
/* Admin Header */ |
| 141 |
.ffl_admin { |
| 142 |
padding: 20px; |
| 143 |
} |
| 144 |
</style> |
| 145 |
<div class="wrap"> |
| 146 |
<div class="ffl_admin"> |
| 147 |
<h1 class="wp-heading-inline"><?php esc_html_e( 'Force First and Last Name', 'force-first-last') ;?></h1> |
| 148 |
<?php if ( ! empty($_REQUEST['updateusers']) && current_user_can( 'manage_options' ) ) { |
| 149 |
// Check the nonce. |
| 150 |
check_admin_referer( 'ffl_update_users', 'ffl_update_users_nonce' ); |
| 151 |
|
| 152 |
global $wpdb; |
| 153 |
$user_ids = $wpdb->get_col("SELECT ID FROM $wpdb->users"); |
| 154 |
|
| 155 |
foreach ( $user_ids as $user_id ) { |
| 156 |
ffl_fix_user_display_name($user_id); |
| 157 |
set_time_limit(30); |
| 158 |
} ?> |
| 159 |
<div class="notice inline updated"><p><?php printf( __( '%s users updated', 'force-first-last' ), count( $user_ids ) ); ?></p></div> |
| 160 |
<?php } ?> |
| 161 |
<p><?php |
| 162 |
$allowed_ffl_admin_text_strings_html = array ( |
| 163 |
'a' => array ( |
| 164 |
'href' => array(), |
| 165 |
'target' => array(), |
| 166 |
'title' => array(), |
| 167 |
), |
| 168 |
'strong' => array(), |
| 169 |
'em' => array(), |
| 170 |
); |
| 171 |
echo wp_kses( __( 'The <em>Force First and Last Name as Display Name</em> plugin will only fix display names at registration or when a profile is updated. If you just activated this plugin, please click on the button below to update the display names of your existing users.', 'force-first-last' ), $allowed_ffl_admin_text_strings_html ); ?> |
| 172 |
</p> |
| 173 |
<form method="post" action=""> |
| 174 |
<input type="hidden" name="updateusers" value="1" /> |
| 175 |
<?php wp_nonce_field( 'ffl_update_users', 'ffl_update_users_nonce' ); ?> |
| 176 |
<input type="submit" class="button button-hero button-primary" value="<?php esc_attr_e( 'Update Existing Users', 'force-first-last' ); ?>" /> |
| 177 |
</form> |
| 178 |
<p><?php echo wp_kses( __( '<strong>WARNING:</strong> This process may take a long time for sites with many users or hosted on a slow server. <strong>Running this script may hang up or cause other issues with your site.</strong> Use at your own risk.', 'force-first-last' ), $allowed_ffl_admin_text_strings_html ); ?></p> |
| 179 |
</div> |
| 180 |
</div> <!-- end ffl_admin --> |
| 181 |
<?php |
| 182 |
} |
| 183 |
|