self::PAGE_SLUG, 'user_id' => $user_id, '_wpnonce' => wp_create_nonce( 'aiou-update-username-' . $user_id ), ), admin_url( 'users.php' ) ); } /** * AJAX: type-ahead results for the "Choose a user" box. * * @return void */ public function ajax_search(): void { UserPicker::respond( self::picker(), self::CAP ); } /** * The "Choose a user" box's context (see Admin\UserPicker). * * @return array */ private static function picker(): array { return array( 'action' => 'aiou_update_username_search', 'nonce' => 'aiou-update-username-search', 'page' => self::PAGE_SLUG, 'url' => array( self::class, 'form_url' ), ); } /** * Add the "Update username" row action. * * @param string[] $actions Existing row actions. * @param WP_User $user The listed user. * @return string[] */ public function row_action( array $actions, WP_User $user ): array { if ( ! current_user_can( 'edit_user', $user->ID ) ) { return $actions; } $url = self::form_url( (int) $user->ID ); $actions['aiou_update_username'] = sprintf( '%s', esc_url( $url ), esc_html__( 'Update username', 'all-in-one-utilities' ) ); return $actions; } /** * Register the (hidden, linked-to-only) Users subpage. * * @return void */ public function register_page(): void { add_users_page( __( 'Update Username', 'all-in-one-utilities' ), __( 'Update Username', 'all-in-one-utilities' ), self::CAP, self::PAGE_SLUG, array( $this, 'render_page' ) ); } /** * Render the update-username form. * * @return void */ public function render_page(): void { if ( ! current_user_can( self::CAP ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'all-in-one-utilities' ), '', array( 'response' => 403 ) ); } $user_id = isset( $_GET['user_id'] ) ? absint( $_GET['user_id'] ) : 0; // Opened from the menu, with no user yet: let the admin find one. if ( ! $user_id ) { $this->render_picker(); return; } if ( ! current_user_can( 'edit_user', $user_id ) ) { wp_die( esc_html__( 'Invalid user.', 'all-in-one-utilities' ), '', array( 'response' => 404 ) ); } check_admin_referer( 'aiou-update-username-' . $user_id ); $user = get_userdata( $user_id ); if ( ! $user ) { wp_die( esc_html__( 'User not found.', 'all-in-one-utilities' ), '', array( 'response' => 404 ) ); } $notify_default = aiou_get_setting( 'users', 'username_updater', 'notify_by_default', 0 ); $error = get_transient( 'aiou_username_updater_error_' . get_current_user_id() ); $success = get_transient( 'aiou_username_updater_success_' . get_current_user_id() ); delete_transient( 'aiou_username_updater_error_' . get_current_user_id() ); delete_transient( 'aiou_username_updater_success_' . get_current_user_id() ); PageLayout::start( array( 'title' => __( 'Update Username', 'all-in-one-utilities' ), 'owner' => 'free', 'back_url' => admin_url( 'users.php?page=' . self::PAGE_SLUG ), 'back_label' => __( 'Choose another user', 'all-in-one-utilities' ), 'description' => __( 'Change the username this person logs in with. WordPress itself never lets you edit a username after the account is created.', 'all-in-one-utilities' ), 'tips' => array( __( 'The user keeps their password, email, role, posts and everything else -- only the login name changes.', 'all-in-one-utilities' ), __( 'From now on they log in with the new username. Their author page address changes to match it.', 'all-in-one-utilities' ), __( 'The new username must be valid and available. If it isn\'t, you\'ll see a few suggestions instead.', 'all-in-one-utilities' ), ), ) ); ?>

display_name ) ); ?>
__( 'Update Username', 'all-in-one-utilities' ), 'owner' => 'free', 'description' => __( 'Change the username someone logs in with. WordPress itself never lets you edit a username after the account is created.', 'all-in-one-utilities' ), 'tips' => array( __( 'Start typing a first or last name, username or email address, then pick the person from the list.', 'all-in-one-utilities' ), __( 'You can also hover over anyone in Users → All Users and click "Update username".', 'all-in-one-utilities' ), __( 'Only people you are allowed to edit are listed.', 'all-in-one-utilities' ), ), ) ); PageLayout::section_start( __( 'Choose a user', 'all-in-one-utilities' ), __( 'Search by name, username or email address.', 'all-in-one-utilities' ) ); UserPicker::render( self::picker() ); PageLayout::section_end(); PageLayout::end(); } /** * Process the update-username form submission. * * @return void */ public function handle_submission(): void { if ( ! isset( $_POST['aiou_update_username'] ) || ! isset( $_POST['aiou_username_nonce'] ) ) { return; } $user_id = isset( $_POST['user_id'] ) ? absint( $_POST['user_id'] ) : 0; if ( ! $user_id || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['aiou_username_nonce'] ) ), 'aiou-update-username-submit-' . $user_id ) ) { wp_die( esc_html__( 'Security check failed. Please try again.', 'all-in-one-utilities' ), '', array( 'response' => 403 ) ); } if ( ! current_user_can( 'edit_user', $user_id ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'all-in-one-utilities' ), '', array( 'response' => 403 ) ); } // Back to this user's form. Not wp_nonce_url(): its "&" would // drop the user id and land on the "Choose a user" step instead. $redirect = self::form_url( $user_id ); $user = get_userdata( $user_id ); if ( ! $user ) { wp_safe_redirect( $redirect ); exit; } $raw_username = isset( $_POST['new_username'] ) ? sanitize_text_field( wp_unslash( $_POST['new_username'] ) ) : ''; $new_username = sanitize_user( $raw_username, true ); $error = Validator::validate( $new_username, $user ); if ( $error ) { $this->notice( 'error', $error ); wp_safe_redirect( $redirect ); exit; } $notify = ! empty( $_POST['notify_user'] ); $result = Service::apply( $user_id, $new_username, $notify, true ); $this->notice( $result['success'] ? 'success' : 'error', $result['message'] ); wp_safe_redirect( $redirect ); exit; } /** * Store a one-time admin notice for the current user. * * @param string $type 'success' or 'error'. * @param string $message Notice text. * @return void */ private function notice( string $type, string $message ): void { set_transient( 'aiou_username_updater_' . $type . '_' . get_current_user_id(), $message, MINUTE_IN_SECONDS ); } }