| 1 |
<?php |
| 2 |
|
| 3 |
namespace change_username; |
| 4 |
|
| 5 |
/** |
| 6 |
* Enqueue assets on the "user edit" page for user swith the `edit_users` capability. |
| 7 |
*/ |
| 8 |
function enqueue_assets() |
| 9 |
{ |
| 10 |
global $pagenow; |
| 11 |
|
| 12 |
if (! in_array($pagenow, ['profile.php', 'user-edit.php'], true)) { |
| 13 |
return; |
| 14 |
} |
| 15 |
|
| 16 |
if (! current_user_can('edit_users')) { |
| 17 |
return; |
| 18 |
} |
| 19 |
|
| 20 |
wp_enqueue_script('change-username', plugins_url('/assets/js/script.min.js', CHANGE_USERNAME_FILE), [], CHANGE_USERNAME_VERSION, ['strategy' => 'defer', 'in_footer' => true ]); |
| 21 |
wp_localize_script('change-username', 'change_username', [ |
| 22 |
'nonce' => wp_create_nonce('change_username'), |
| 23 |
'ajaxurl' => admin_url('admin-ajax.php'), |
| 24 |
]); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Handles the AJAX request for changing a username. |
| 29 |
*/ |
| 30 |
function ajax_handler() |
| 31 |
{ |
| 32 |
$response = [ |
| 33 |
'success' => false, |
| 34 |
'new_nonce' => wp_create_nonce('change_username') |
| 35 |
]; |
| 36 |
|
| 37 |
// check capability |
| 38 |
if (! current_user_can('edit_users')) { |
| 39 |
$response['message'] = esc_html__('You do not have the required capability to do that.', 'change-username'); |
| 40 |
wp_send_json($response); |
| 41 |
} |
| 42 |
|
| 43 |
// validate nonce |
| 44 |
check_ajax_referer('change_username'); |
| 45 |
|
| 46 |
// validate request |
| 47 |
if (empty($_POST['new_username']) || empty($_POST['current_username'])) { |
| 48 |
$response['message'] = 'Invalid request.'; |
| 49 |
wp_send_json($response); |
| 50 |
} |
| 51 |
|
| 52 |
$new_username = trim(strip_tags(wp_unslash($_POST['new_username']))); |
| 53 |
$old_username = trim(strip_tags(wp_unslash($_POST['current_username']))); |
| 54 |
|
| 55 |
// old username should be provided by the script |
| 56 |
// if it doesn't exist, someone is tampering with the request values |
| 57 |
if (!username_exists($old_username)) { |
| 58 |
$response['message'] = 'Invalid request.'; |
| 59 |
wp_send_json($response); |
| 60 |
} |
| 61 |
|
| 62 |
if ($new_username != $old_username) { |
| 63 |
if (mb_strlen($new_username) < 3 || mb_strlen($new_username) > 60) { |
| 64 |
$response['message'] = esc_html__('Username must be between 3 and 60 characters long.', 'change-username'); |
| 65 |
wp_send_json($response); |
| 66 |
} |
| 67 |
|
| 68 |
if (! validate_username($new_username)) { |
| 69 |
$response['message'] = esc_html__('This username is invalid because it uses illegal characters. Please enter a valid username.', 'change-username'); |
| 70 |
wp_send_json($response); |
| 71 |
} |
| 72 |
|
| 73 |
// check if username is not in list of illegal logins |
| 74 |
/** This filter is documented in wp-includes/user.php */ |
| 75 |
$illegal_user_logins = array_map('strtolower', (array) apply_filters('illegal_user_logins', [])); |
| 76 |
if (in_array(strtolower($new_username), $illegal_user_logins, true)) { |
| 77 |
$response['message'] = esc_html__('Sorry, that username is not allowed.', 'change-username'); |
| 78 |
wp_send_json($response); |
| 79 |
} |
| 80 |
|
| 81 |
// check if new username is in use already |
| 82 |
if (username_exists($new_username)) { |
| 83 |
$response['message'] = \sprintf(esc_html__('%1$s is already in use.', 'change-username'), $new_username); |
| 84 |
wp_send_json($response); |
| 85 |
} |
| 86 |
|
| 87 |
// change the username |
| 88 |
change_username($old_username, $new_username); |
| 89 |
} |
| 90 |
|
| 91 |
// success response |
| 92 |
$response['success'] = true; |
| 93 |
$response['message'] = \sprintf(esc_html__('Username successfully changed to %1$s.', 'change-username'), $new_username); |
| 94 |
wp_send_json($response); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* @param string $old_username |
| 99 |
* @param string $new_username |
| 100 |
* @return boolean |
| 101 |
*/ |
| 102 |
function change_username($old_username, $new_username) |
| 103 |
{ |
| 104 |
global $wpdb; |
| 105 |
|
| 106 |
// do nothing if old username does not exist. |
| 107 |
$user_id = username_exists($old_username); |
| 108 |
if (! $user_id) { |
| 109 |
return false; |
| 110 |
} |
| 111 |
|
| 112 |
// change username |
| 113 |
$wpdb->query($wpdb->prepare("UPDATE $wpdb->users SET user_login = %s WHERE user_login = %s", $new_username, $old_username)); |
| 114 |
|
| 115 |
// change nicename if needed |
| 116 |
$wpdb->query($wpdb->prepare("UPDATE $wpdb->users SET user_nicename = %s WHERE user_login = %s AND user_nicename = %s", $new_username, $new_username, $old_username)); |
| 117 |
|
| 118 |
// change display name if needed |
| 119 |
$wpdb->query($wpdb->prepare("UPDATE $wpdb->users SET display_name = %s WHERE user_login = %s AND display_name = %s", $new_username, $new_username, $old_username)); |
| 120 |
|
| 121 |
// when on multisite, check if old username is in the `site_admins` options array. if so, replace with new username to retain superadmin rights. |
| 122 |
if (is_multisite()) { |
| 123 |
$super_admins = (array) get_site_option('site_admins', ['admin']); |
| 124 |
$array_key = array_search($old_username, $super_admins, true); |
| 125 |
if ($array_key !== false) { |
| 126 |
$super_admins[$array_key] = $new_username; |
| 127 |
update_site_option('site_admins', $super_admins); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Fires right after a username is changed. |
| 133 |
* |
| 134 |
* @param string $old_username |
| 135 |
* @param string $new_username |
| 136 |
*/ |
| 137 |
do_action('change_username.username_changed', $old_username, $new_username); |
| 138 |
return true; |
| 139 |
} |
| 140 |
|