PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / 1.0.1
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin v1.0.1
1.1.10 1.1.9 1.1.8 1.1.7 1.1.6 1.1.5 1.1.4 1.1.3 1.1.2 1.1.1 1.1.0 1.0.1 1.0.0 0.9.8 0.9.7 0.9.6 0.9.4 0.9.5 0.9.3 0.9.2 0.9.1 0.9.0 0.8.9 0.8.8 0.8.7 All 34 releases
desktop-mode / includes / users-window / login-tracker.php

login-tracker.php in OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin 1.0.1, at includes/users-window/login-tracker.php

75 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * OpenStation — Native Users Window: last-login tracker.
4 *
5 * Hooks `wp_login` to record the timestamp of every successful
6 * login as `_desktop_mode_last_login_at` user meta (a UTC unix
7 * timestamp, stored as integer). The Users window's REST field
8 * surfaces this back to the table for the Last login column.
9 *
10 * The meta key is intentionally underscore-prefixed (private) so
11 * it doesn't show up in user-meta-management UIs but still allows
12 * `update_user_meta` access.
13 *
14 * Plugins that want to read the value should use:
15 *
16 * $ts = (int) get_user_meta( $user_id, '_desktop_mode_last_login_at', true );
17 *
18 * @package OpenStation
19 */
20
21 defined( 'ABSPATH' ) || exit;
22
23 /**
24 * The user meta key carrying the last successful login's UTC unix
25 * timestamp. Public surface — exposed so other plugins can read /
26 * sort by it.
27 *
28 * The VALUE keeps its pre-rebrand spelling on purpose: it is a
29 * persisted or externally-visible identifier, so renaming it would
30 * orphan data already written by live installs (or break a live
31 * URL). The mismatch between this constant's name and its value is
32 * deliberate — it is NOT a half-finished rename.
33 */
34 const OPENSTATION_LAST_LOGIN_META_KEY = '_desktop_mode_last_login_at';
35
36 /**
37 * Record the login timestamp on every `wp_login` action.
38 *
39 * `wp_login` fires AFTER credentials have been validated and the
40 * cookie set, so any cap-eligible user reaching this point is
41 * legitimately logged in. We record server time (`time()`), which
42 * is always UTC by convention in PHP — match it on the read side.
43 *
44 * @param string $user_login Login of the user (unused).
45 * @param WP_User $user The user object.
46 */
47 function openstation_users_window_record_login( $user_login, $user = null ) {
48 $user_id = 0;
49 if ( $user instanceof WP_User ) {
50 $user_id = (int) $user->ID;
51 } elseif ( is_string( $user_login ) && '' !== $user_login ) {
52 $by = get_user_by( 'login', $user_login );
53 if ( $by instanceof WP_User ) {
54 $user_id = (int) $by->ID;
55 }
56 }
57
58 if ( $user_id <= 0 ) {
59 return;
60 }
61
62 update_user_meta( $user_id, OPENSTATION_LAST_LOGIN_META_KEY, time() );
63
64 /**
65 * Fires after the last-login meta has been written. Lets plugins
66 * piggy-back on the same hook to update their own last-seen
67 * tracking without duplicating the `wp_login` listener.
68 *
69 * @param int $user_id User id whose login was recorded.
70 * @param int $timestamp Unix timestamp written.
71 */
72 do_action( 'openstation_users_window_login_recorded', $user_id, time() );
73 }
74 add_action( 'wp_login', 'openstation_users_window_record_login', 10, 2 );
75