is_user_disabled( $user->ID ) ) { self::$pending_disabled_login_redirect_user_id = (int) $user->ID; return new \WP_Error( self::ERROR_CODE, __( 'ERROR: Your account has been disabled.', 'admin-site-enhancements' ) ); } return $user; } /** * Log out immediately if the logged-in account is disabled (stale cookie/session). * * @since 8.7.4 */ public function maybe_force_logout_disabled_user() { if ( ! is_user_logged_in() ) { return; } $user_id = get_current_user_id(); if ( ! $this->is_user_disabled( $user_id ) ) { return; } wp_logout(); wp_safe_redirect( wp_login_url() ); exit; } /** * Hide Application Passwords for disabled users. * * @since 8.7.4 * * @param bool $available Whether App Passwords are available for the user. * @param mixed $user \WP_User|int|null. * @return bool */ public function filter_application_passwords_for_user( $available, $user ) { $user_id = 0; if ( $user instanceof \WP_User ) { $user_id = (int) $user->ID; } elseif ( is_numeric( $user ) ) { $user_id = (int) $user; } if ( $user_id > 0 && $this->is_user_disabled( $user_id ) ) { return false; } return $available; } /** * Append Status column (last). * * @since 8.7.4 * * @param string[] $columns Column headers. * @return string[] */ public function add_status_column( $columns ) { $columns['asenha_user_account_status'] = __( 'Status', 'admin-site-enhancements' ); return $columns; } /** * Output Status cell content. * * @since 8.7.4 * * @param string $output Output. * @param string $column_name Column key. * @param int $user_id User ID. * @return string */ public function render_status_column( $output, $column_name, $user_id ) { if ( 'asenha_user_account_status' !== $column_name ) { return $output; } if ( $this->is_user_disabled( $user_id ) ) { return esc_html__( 'Disabled', 'admin-site-enhancements' ); } return ''; } /** * Row actions: Disable / Enable. * * @since 8.7.4 * * @param string[] $actions Row actions. * @param \WP_User $user User object. * @return string[] */ public function add_user_row_actions( $actions, $user ) { if ( ! $user instanceof \WP_User ) { return $actions; } if ( ! current_user_can( 'list_users' ) ) { return $actions; } if ( ! $this->current_user_may_toggle_user( $user->ID ) ) { return $actions; } $disabled = $this->is_user_disabled( $user->ID ); if ( $disabled ) { /* translators: verb: enable user login */ $label = __( 'Enable', 'admin-site-enhancements' ); $set_disabled = 0; } else { /* translators: verb: disable user login */ $label = __( 'Disable', 'admin-site-enhancements' ); $set_disabled = 1; } $actions['asenha_disable_user_account'] = sprintf( '%s', (int) $user->ID, (int) $set_disabled, esc_html( $label ) ); return $actions; } /** * AJAX: set or clear disabled meta for a user. * * @since 8.7.4 */ public function ajax_toggle_disabled() { check_ajax_referer( 'asenha_user_account_toggle', 'nonce' ); if ( ! current_user_can( 'list_users' ) ) { wp_send_json_error( array( 'message' => __( 'Sorry, you are not allowed to do this.', 'admin-site-enhancements' ) ), 403 ); } $user_id = isset( $_POST['user_id'] ) ? absint( $_POST['user_id'] ) : 0; $set_disabled = isset( $_POST['set_disabled'] ) ? (bool) filter_var( wp_unslash( $_POST['set_disabled'] ), FILTER_VALIDATE_BOOLEAN ) : false; if ( $user_id <= 0 || ! $this->current_user_may_toggle_user( $user_id ) ) { wp_send_json_error( array( 'message' => __( 'Sorry, you are not allowed to modify this user.', 'admin-site-enhancements' ) ), 403 ); } if ( $set_disabled ) { update_user_meta( $user_id, self::META_KEY, '1' ); } else { delete_user_meta( $user_id, self::META_KEY ); } $is_disabled = $this->is_user_disabled( $user_id ); if ( $is_disabled ) { $action_label = __( 'Enable', 'admin-site-enhancements' ); } else { $action_label = __( 'Disable', 'admin-site-enhancements' ); } wp_send_json_success( array( 'is_disabled' => $is_disabled, 'status_html' => $is_disabled ? esc_html__( 'Disabled', 'admin-site-enhancements' ) : '', 'action_label' => $action_label, 'next_set_disabled' => $is_disabled ? 0 : 1, ) ); } /** * Styles for Users list Status column. * * @since 8.7.4 */ public function print_users_list_column_style() { ?> is_user_disabled( $user_id ); $row_html = '