$uid, 'user_email' => $email ) ); remove_filter( 'send_email_change_email', '__return_false' ); if ( is_wp_error( $result ) ) { return $result; } delete_user_meta( $uid, '_new_email' ); $messages[] = __( 'Your own account email was changed too.', 'ai-guardwall' ); } Softvolt_Ai_Bodyguard_Logger::log( 'notice', 'tool', implode( ' ', $messages ) ); return array( 'message' => implode( ' ', $messages ) ); } /** * Rename a user (WordPress itself refuses to change user_login). * * @since 1.0.0 * @param int $user_id User. * @param string $new_login New username. * @return array|WP_Error */ public static function change_username( $user_id, $new_login ) { global $wpdb; $user = get_userdata( (int) $user_id ); if ( ! $user ) { return new WP_Error( 'svabg_user', __( 'That user does not exist.', 'ai-guardwall' ) ); } if ( ! current_user_can( 'edit_user', $user->ID ) ) { return new WP_Error( 'svabg_cap', __( 'You are not allowed to edit this user.', 'ai-guardwall' ) ); } $new_login = sanitize_user( trim( (string) $new_login ), true ); if ( '' === $new_login || ! validate_username( $new_login ) ) { return new WP_Error( 'svabg_username', __( 'That username is not valid. Use letters, numbers, dots, dashes and underscores.', 'ai-guardwall' ) ); } if ( strlen( $new_login ) > 60 ) { return new WP_Error( 'svabg_username', __( 'Usernames can be up to 60 characters.', 'ai-guardwall' ) ); } if ( strtolower( $new_login ) === strtolower( $user->user_login ) ) { return new WP_Error( 'svabg_username', __( 'That is already the username.', 'ai-guardwall' ) ); } if ( username_exists( $new_login ) ) { return new WP_Error( 'svabg_username', __( 'That username is taken.', 'ai-guardwall' ) ); } $blocked = array_map( 'strtolower', Softvolt_Ai_Bodyguard_Settings::list_to_array( Softvolt_Ai_Bodyguard_Settings::get( 'login_block_usernames', '' ) ) ); if ( in_array( strtolower( $new_login ), $blocked, true ) ) { return new WP_Error( 'svabg_username', __( 'That name is on the attack-username list; choose something less guessable.', 'ai-guardwall' ) ); } $old = $user->user_login; $data = array( 'user_login' => $new_login ); if ( $user->user_nicename === sanitize_title( $old ) || $user->user_nicename === strtolower( $old ) ) { $data['user_nicename'] = wp_unique_user_nicename( sanitize_title( $new_login ), $user->ID, $new_login ); } if ( $user->display_name === $old ) { $data['display_name'] = $new_login; } $ok = $wpdb->update( $wpdb->users, $data, array( 'ID' => $user->ID ) ); if ( false === $ok ) { return new WP_Error( 'svabg_db', __( 'The username could not be changed.', 'ai-guardwall' ) ); } clean_user_cache( $user->ID ); if ( get_current_user_id() === (int) $user->ID ) { // The login cookie carries the username, so refresh it before it stops validating. wp_set_auth_cookie( $user->ID, true, is_ssl() ); } Softvolt_Ai_Bodyguard_Logger::log( 'notice', 'tool', sprintf( /* translators: 1: old, 2: new login */ __( 'Username changed from "%1$s" to "%2$s".', 'ai-guardwall' ), $old, $new_login ), array(), $user->ID ); return array( 'message' => sprintf( /* translators: 1: old, 2: new login */ __( 'Done. "%1$s" is now "%2$s". Use the new name to log in from now on.', 'ai-guardwall' ), $old, $new_login ) ); } /** * Fresh security keys and salts (logs everyone out). * * @since 1.0.0 * @return array|WP_Error */ public static function rotate_salts() { $result = Softvolt_Ai_Bodyguard_Wpconfig::rotate_salts(); if ( is_wp_error( $result ) ) { return $result; } return array( 'message' => __( 'New keys were written to wp-config.php. Every user, including you, is logged out and must log in again.', 'ai-guardwall' ), 'relogin' => true ); } /** * Administrators for the rename tool. */ public static function users_for_rename() { $out = array(); foreach ( get_users( array( 'role__in' => array( 'administrator', 'editor', 'shop_manager' ), 'number' => 200, 'fields' => array( 'ID', 'user_login', 'user_email' ) ) ) as $u ) { $out[] = array( 'id' => (int) $u->ID, 'login' => $u->user_login, 'email' => $u->user_email ); } return $out; } }