update()`, * cache invalidation, optional notification email, and the * `aiou_username_updated` action. Callers are responsible for their own * capability checks, nonce verification, and calling `Validator::validate()` * first — this class assumes the change has already been authorized and * validated. */ final class Service { /** * Apply a username change. * * @param int $user_id User ID. * @param string $new_username Already-validated new username. * @param bool $notify Whether to email the user about the change. * @param bool $asked Whether an admin asked for the email on this * change (the "Notify user" box), which sends it * even while it's switched off in Settings → Emails. * @return array{success:bool,message:string} */ public static function apply( int $user_id, string $new_username, bool $notify, bool $asked = false ): array { $user = get_userdata( $user_id ); if ( ! $user ) { return array( 'success' => false, 'message' => __( 'User not found.', 'all-in-one-utilities' ), ); } global $wpdb; $updated = $wpdb->update( $wpdb->users, array( 'user_login' => $new_username, 'user_nicename' => sanitize_title( $new_username ), ), array( 'ID' => $user_id ), array( '%s', '%s' ), array( '%d' ) ); clean_user_cache( $user_id ); if ( false === $updated ) { return array( 'success' => false, 'message' => __( 'The database update failed. Please try again.', 'all-in-one-utilities' ), ); } $old_username = $user->user_login; // Switching the "Username Updated" email off in Settings → Emails stops // the automatic sends (e.g. Pro's approvals) -- not a failure worth // reporting. An admin ticking "Notify user" still gets it sent. $email = aiou()->emails()->get( UsernameUpdatedEmail::ID ); $notify = $notify && ( $asked || ! $email || $email->is_enabled() ); $mail_sent = $notify && self::notify_user( $user_id, $old_username, $new_username, $asked ); if ( $notify && ! $mail_sent ) { $message = __( 'Username updated, but the notification email could not be sent.', 'all-in-one-utilities' ); } elseif ( $notify ) { $message = __( 'Username updated and the user was notified by email.', 'all-in-one-utilities' ); } else { $message = __( 'Username updated.', 'all-in-one-utilities' ); } /** * Fires after a username has been changed. * * @since 2.0.0 * * @param int $user_id The user ID. * @param string $old_username Previous username. * @param string $new_username New username. */ do_action( 'aiou_username_updated', $user_id, $old_username, $new_username ); return array( 'success' => true, 'message' => $message, ); } /** * Email a user about their username change. * * @param int $user_id User ID. * @param string $old_username Previous username. * @param string $new_username New username. * @param bool $even_if_off Send while switched off in Settings → Emails. * @return bool */ public static function notify_user( int $user_id, string $old_username, string $new_username, bool $even_if_off = false ): bool { $user = get_userdata( $user_id ); $email = aiou()->emails()->get( UsernameUpdatedEmail::ID ); if ( ! $user || ! $email instanceof UsernameUpdatedEmail ) { return false; } return $email->send_to( $user, $old_username, $new_username, $even_if_off ); } }