-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a594f43
commit 006ac0e
Showing
2 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
const LAST_SEEN_UPDATE_USER_META_CACHE_TTL = 60; // Store last seen once per minute to avoid too much write DB operations | ||
const LAST_SEEN_UPDATE_USER_META_CACHE_KEY_PREFIX = 'vip_last_seen_update_user_meta_cache_key'; | ||
|
||
add_action( 'set_current_user', 'update_user_last_seen', 10, 0 ); | ||
add_filter ('manage_users_columns', 'users_columns' ) ; | ||
add_filter ('manage_users_custom_column', 'users_custom_column', 10, 3 ) ; | ||
|
||
function update_user_last_seen() { | ||
global $current_user; | ||
|
||
$cache_key = LAST_SEEN_UPDATE_USER_META_CACHE_KEY_PREFIX . $current_user->ID; | ||
|
||
try { | ||
if ( wp_cache_get( $cache_key ) ) { | ||
return; | ||
} | ||
|
||
update_user_meta( $current_user->ID, 'vip_last_seen', time() ); | ||
|
||
wp_cache_set( $cache_key, true, null, LAST_SEEN_UPDATE_USER_META_CACHE_TTL ); | ||
} catch ( \Exception $e ) { | ||
trigger_error( | ||
sprintf( 'failed to update user last seen meta', esc_html( $e->getMessage() ) ), | ||
E_USER_WARNING | ||
); | ||
} | ||
} | ||
|
||
function users_columns ($cols) { | ||
$cols['last_seen'] = __( 'Last seen' ) ; | ||
return $cols; | ||
} | ||
|
||
function users_custom_column ( $default, $column_name, $user_id ) { | ||
if ( 'last_seen' == $column_name ) { | ||
$last_seen_timestamp = get_user_meta( $user_id, 'vip_last_seen', true ); | ||
|
||
if ( $last_seen_timestamp ) { | ||
$formatted_date = sprintf( | ||
__( '%1$s at %2$s' ), | ||
date_i18n( get_option('date_format'), $last_seen_timestamp ), | ||
date_i18n( get_option('time_format'), $last_seen_timestamp ) | ||
); | ||
|
||
if ( defined( 'VIP_CONSIDER_USERS_INACTIVE_AFTER_DAYS' ) ) { | ||
$diff = ( new DateTime( sprintf('@%s', $last_seen_timestamp ) ) )->diff( new DateTime() ); | ||
|
||
if ( $diff->days >= constant( 'VIP_CONSIDER_USERS_INACTIVE_AFTER_DAYS' ) ) { | ||
return sprintf( '<span class="wp-ui-text-notification">%s</span>', esc_html__( $formatted_date ) ); | ||
} | ||
} | ||
|
||
return sprintf( '<span>%s</span>', esc_html__( $formatted_date ) ); | ||
} | ||
} | ||
|
||
return $default; | ||
} |