Plugin Directory

Changeset 3299122


Ignore:
Timestamp:
05/23/2025 05:26:21 AM (10 months ago)
Author:
hardikhuptechdev
Message:

Version 2.0.0: Added CSV export and reset login data features

Location:
last-login-info/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • last-login-info/trunk/last-login-info.php

    r3298796 r3299122  
    44 * Plugin Name: Last Login Info
    55 * Description: Adds a column in the Users screen to show the last login date of each user.
    6  * Version: 1.1.0
     6 * Version: 2.0.0
    77 * Author: Hardik
    88 * Author URI: https://profiles.wordpress.org/hardikhuptechdev/
     
    4444    $roles = wp_roles()->roles;
    4545    $settings = lastlogininfo_get_settings();
     46
    4647?>
    4748    <div class="wrap">
     
    7677            <?php submit_button(); ?>
    7778        </form>
     79
     80        <hr>
     81
     82        <form method="post">
     83            <?php wp_nonce_field('lastlogininfo_export_csv', 'lastlogininfo_nonce'); ?>
     84            <input type="submit" name="lastlogininfo_export" class="button button-primary" value="<?php esc_attr_e('Export Last Login to CSV', 'last-login-info'); ?>">
     85            <input type="submit" name="lastlogininfo_reset" class="button button-secondary" onclick="return confirm('Are you sure you want to delete all last login records?');" value="<?php esc_attr_e('Reset Login Data', 'last-login-info'); ?>">
     86        </form>
    7887    </div>
    7988<?php
     
    8695}
    8796
    88 // Record last login
     97// Handle Export and Reset
     98add_action('admin_init', 'lastlogininfo_handle_export_or_reset');
     99function lastlogininfo_handle_export_or_reset()
     100{
     101    if (! current_user_can('manage_options') || ! isset($_POST['lastlogininfo_nonce'])) {
     102        return;
     103    }
     104
     105    if (! wp_verify_nonce($_POST['lastlogininfo_nonce'], 'lastlogininfo_export_csv')) {
     106        return;
     107    }
     108
     109    if (isset($_POST['lastlogininfo_export'])) {
     110        $users = get_users();
     111        header('Content-Type: text/csv');
     112        header('Content-Disposition: attachment; filename="last-login-export.csv"');
     113        $output = fopen('php://output', 'w');
     114        fputcsv($output, ['Username', 'Email', 'Roles', 'Last Login']);
     115
     116        foreach ($users as $user) {
     117            $timestamp = get_user_meta($user->ID, 'last_login', true);
     118            $login = $timestamp ? date_i18n(get_option('date_format') . ' ' . get_option('time_format'), $timestamp) : 'Never';
     119            fputcsv($output, [$user->user_login, $user->user_email, implode(', ', $user->roles), $login]);
     120        }
     121
     122        fclose($output);
     123        exit;
     124    }
     125
     126    if (isset($_POST['lastlogininfo_reset'])) {
     127        $users = get_users();
     128        foreach ($users as $user) {
     129            delete_user_meta($user->ID, 'last_login');
     130        }
     131        wp_safe_redirect(admin_url('options-general.php?page=last-login-info&reset=1'));
     132        exit;
     133    }
     134}
     135
     136// Record login
    89137add_action('wp_login', 'lastlogininfo_record_last_login', 10, 2);
    90138function lastlogininfo_record_last_login($user_login, $user)
    91139{
    92140    $settings = lastlogininfo_get_settings();
    93     $excluded_roles = $settings['excluded_roles'];
    94141    foreach ($user->roles as $role) {
    95         if (in_array($role, $excluded_roles, true)) {
     142        if (in_array($role, $settings['excluded_roles'], true)) {
    96143            return;
    97144        }
    98145    }
    99 
    100146    update_user_meta($user->ID, 'last_login', current_time('timestamp'));
    101147}
    102148
    103 // Add column
     149// Admin column
    104150add_filter('manage_users_columns', 'lastlogininfo_add_last_login_column');
    105151function lastlogininfo_add_last_login_column($columns)
     
    109155}
    110156
    111 // Display column
    112157add_filter('manage_users_custom_column', 'lastlogininfo_show_last_login', 10, 3);
    113158function lastlogininfo_show_last_login($output, $column_name, $user_id)
    114159{
    115160    if ('last_login' === $column_name) {
     161        $user = get_userdata($user_id);
     162        $settings = lastlogininfo_get_settings();
     163        foreach ($user->roles as $role) {
     164            if (in_array($role, $settings['excluded_roles'], true)) {
     165                return __('Excluded', 'last-login-info');
     166            }
     167        }
     168
    116169        $timestamp = get_user_meta($user_id, 'last_login', true);
    117170        if ($timestamp) {
    118             $settings = lastlogininfo_get_settings();
    119171            if ($settings['date_format'] === 'full') {
    120172                return date_i18n(get_option('date_format') . ' ' . get_option('time_format'), $timestamp);
    121173            } else {
    122174                $time_diff = human_time_diff($timestamp, current_time('timestamp'));
    123                 // translators: %s is a human-readable time difference (e.g., "2 days")
    124175                return sprintf(__('%s ago', 'last-login-info'), $time_diff);
    125176            }
  • last-login-info/trunk/readme.txt

    r3298796 r3299122  
    55Tested up to: 6.8
    66Requires PHP: 7.2
    7 Stable tag: 1.1.0
     7Stable tag: 2.0.0
    88License: GPLv2 or later
    99License URI: https://www.gnu.org/licenses/gpl-2.0.html
    1010
    11 Displays the last login timestamp of each user in the WordPress admin Users table, with a settings page for customization.
     11Displays the last login timestamp of each user in the WordPress admin Users table, with tools to export and manage login data.
    1212
    1313== Description ==
     
    1717This plugin adds a "Last Login" column to the Users table in the WordPress admin. It tracks and displays each user's last login time, with either human-readable formatting (e.g., "2 days ago") or full date/timestamp. The column is sortable, making it easier to find inactive users.
    1818
    19 A simple settings page is available under **Settings → Last Login Info**, where you can choose the date display format and exclude specific user roles from being tracked.
     19The settings page under **Settings → Last Login Info** allows you to customize the date format, exclude roles, export data to CSV, or reset all login records.
    2020
    2121== Features ==
     
    2525* Choose between human-readable or full timestamp display.
    2626* Exclude specific roles from being tracked (e.g., Administrator).
     27* Export all user logins to CSV.
     28* Reset all login tracking data.
    2729* Sortable column by last login time.
    2830* Lightweight, no bloat.
     
    33352. Activate the plugin through the 'Plugins' screen in WordPress.
    34363. Go to "Users" in the WordPress admin — you’ll see a new "Last Login" column.
    35 4. Visit **Settings → Last Login Info** to configure display options.
     374. Visit **Settings → Last Login Info** to configure display and export options.
    3638
    3739== Frequently Asked Questions ==
     
    4749= Can I reset the login data? =
    4850
    49 Not yet. This may be added in a future update. You can currently remove the `last_login` user meta manually via custom code or plugins like WP-CLI or Advanced Custom Fields.
     51Yes, go to Settings → Last Login Info and click the "Reset Login Data" button.
    5052
    51 == Screenshots ==
     53= Can I export login data? =
    5254
    53 1. Last Login column in the Users admin table.
    54 2. Settings page with display and role options.
     55Yes, the plugin provides a CSV export of all users and their last login details.
    5556
    5657== Changelog ==
     58
     59= 2.0.0 =
     60* Added a Reset Login Data button to clear all last login data for users.
     61* Added a CSV Export feature to download usernames, emails, roles, and last login times.
    5762
    5863= 1.1.0 =
     
    6671== Upgrade Notice ==
    6772
    68 = 1.1.0 =
    69 Adds a settings page with date format selection and role exclusion features.
    70 
    71 = 1.0.0 =
    72 Initial release of Last Login Info.
     73= 2.0.0 =
     74Major update: Reset and Export tools added to the settings page.
Note: See TracChangeset for help on using the changeset viewer.