PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / 0.9.5
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin v0.9.5
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 0.9.5, at includes/users-window/login-tracker.php

76 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Desktop Mode — 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 WPDesktopMode
19 * @since 0.8.1
20 */
21
22 defined( 'ABSPATH' ) || exit;
23
24 /**
25 * The user meta key carrying the last successful login's UTC unix
26 * timestamp. Public surface — exposed so other plugins can read /
27 * sort by it.
28 *
29 * @since 0.8.1
30 */
31 const DESKTOP_MODE_LAST_LOGIN_META_KEY = '_desktop_mode_last_login_at';
32
33 /**
34 * Record the login timestamp on every `wp_login` action.
35 *
36 * `wp_login` fires AFTER credentials have been validated and the
37 * cookie set, so any cap-eligible user reaching this point is
38 * legitimately logged in. We record server time (`time()`), which
39 * is always UTC by convention in PHP — match it on the read side.
40 *
41 * @since 0.8.1
42 *
43 * @param string $user_login Login of the user (unused).
44 * @param WP_User $user The user object.
45 */
46 function desktop_mode_users_window_record_login( $user_login, $user = null ) {
47 $user_id = 0;
48 if ( $user instanceof WP_User ) {
49 $user_id = (int) $user->ID;
50 } elseif ( is_string( $user_login ) && '' !== $user_login ) {
51 $by = get_user_by( 'login', $user_login );
52 if ( $by instanceof WP_User ) {
53 $user_id = (int) $by->ID;
54 }
55 }
56
57 if ( $user_id <= 0 ) {
58 return;
59 }
60
61 update_user_meta( $user_id, DESKTOP_MODE_LAST_LOGIN_META_KEY, time() );
62
63 /**
64 * Fires after the last-login meta has been written. Lets plugins
65 * piggy-back on the same hook to update their own last-seen
66 * tracking without duplicating the `wp_login` listener.
67 *
68 * @since 0.8.1
69 *
70 * @param int $user_id User id whose login was recorded.
71 * @param int $timestamp Unix timestamp written.
72 */
73 do_action( 'desktop_mode_users_window_login_recorded', $user_id, time() );
74 }
75 add_action( 'wp_login', 'desktop_mode_users_window_record_login', 10, 2 );
76