$idle ) { delete_user_meta( $uid, self::ACTIVITY_META ); Logger::log( 'idle_logout', sprintf( 'User "%s" logged out after inactivity.', wp_get_current_user()->user_login ), Logger::SEVERITY_INFO, [ 'user_id' => $uid ] ); wp_logout(); if ( ! wp_doing_ajax() && ! ( defined( 'REST_REQUEST' ) && REST_REQUEST ) && ! headers_sent() ) { wp_safe_redirect( wp_login_url() ); exit; } return; } // Throttle activity writes to once per minute. if ( ! $last || ( $now - $last ) > MINUTE_IN_SECONDS ) { update_user_meta( $uid, self::ACTIVITY_META, $now ); } } /** * Active sessions for a user, normalised for display. * * @param int $user_id * @return array */ public static function list_for( $user_id ) { $manager = \WP_Session_Tokens::get_instance( $user_id ); $sessions = $manager->get_all(); $current = wp_get_session_token(); $out = []; foreach ( $sessions as $s ) { $out[] = [ 'ip' => $s['ip'] ?? '', 'ua' => $s['ua'] ?? '', 'login' => isset( $s['login'] ) ? gmdate( 'Y-m-d H:i', (int) $s['login'] ) : '', 'expiration' => isset( $s['expiration'] ) ? gmdate( 'Y-m-d H:i', (int) $s['expiration'] ) : '', // We can flag the current session by matching token hash isn't exposed; approximate by IP+UA. 'is_current' => ( ( $s['ip'] ?? '' ) === Helper::get_ip_address() ), ]; } return $out; } }