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 = ''; $row_html .= '' . esc_html__( 'Disable User Account', 'admin-site-enhancements' ) . ''; $row_html .= ''; $row_html .= ''; // Inject before user-profile.js snapshots the form (avoids false "Leave site?" warnings). wp_enqueue_script( 'user-profile' ); wp_localize_script( 'user-profile', 'asenhaDisableUserAccountProfile', array( 'rowHtml' => $row_html, ) ); wp_add_inline_script( 'user-profile', $this->get_profile_row_injection_script(), 'before' ); } /** * Inline JS to place the disable-account row before user-profile.js captures form state. * * @since 8.8.0 * * @return string */ private function get_profile_row_injection_script() { return <<<'JS' ( function ( $ ) { 'use strict'; $( function () { if ( typeof asenhaDisableUserAccountProfile === 'undefined' || ! asenhaDisableUserAccountProfile.rowHtml ) { return; } var $table = $( 'table.form-table' ) .filter( function () { return $( this ).find( '#password' ).length > 0; } ) .first(); if ( ! $table.length ) { return; } var $rows = $( $.parseHTML( asenhaDisableUserAccountProfile.rowHtml, document, true ) ); var $sessions = $table.find( 'tr.user-sessions-wrap' ).first(); if ( $sessions.length ) { $sessions.before( $rows ); return; } var $reset = $table.find( 'tr.user-generate-reset-link-wrap' ).first(); if ( $reset.length ) { $reset.after( $rows ); return; } var $pwWeak = $table.find( 'tr.pw-weak' ).first(); if ( $pwWeak.length ) { $pwWeak.after( $rows ); } } ); } )( jQuery ); JS; } /** * Save the profile checkbox on user update. * * @since 8.7.4 * * @param int $user_id User ID being saved. */ public function save_profile_checkbox( $user_id ) { $user_id = (int) $user_id; if ( $user_id <= 0 ) { return; } if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), 'update-user_' . $user_id ) ) { return; } if ( get_current_user_id() === $user_id ) { return; } if ( ! current_user_can( 'edit_users' ) ) { return; } if ( ! $this->current_user_may_toggle_user( $user_id ) ) { return; } if ( isset( $_POST['asenha_user_account_disabled'] ) && '1' === sanitize_text_field( wp_unslash( $_POST['asenha_user_account_disabled'] ) ) ) { update_user_meta( $user_id, self::META_KEY, '1' ); } else { delete_user_meta( $user_id, self::META_KEY ); } } }