Skip to content

Instantly share code, notes, and snippets.

@elhardoum
Last active April 14, 2016 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elhardoum/1ee43783d732160681136971c1277ce5 to your computer and use it in GitHub Desktop.
Save elhardoum/1ee43783d732160681136971c1277ce5 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Users Table Last Seen
Plugin URI: http://samelh.com
Description: updates users last seen status each time they browse the front-end and adds last seen to users table with user last seen time difference
Author: Samuel Elh
Version: 0.1
Author URI: http://samelh.com
*/
// hook into wp to run internally in the front-end
add_action('wp', 'se_update_user_last_seen');
/**
* Update user last seen with current time int
*
* @param $user_id int user ID to update (optional)
* @return null
*/
function se_update_user_last_seen( $user_id = 0 ) {
if( ! is_user_logged_in() )
return; // user is logged out
if( ! $user_id ) // get current user ID
$user_id = wp_get_current_user()->ID;
// update user meta
update_user_meta( $user_id, 'se_user_last_seen', time() );
}
// register the new column
add_filter( 'manage_users_columns', 'se_filter_users_columns' );
// append our new column to the existing ones
function se_filter_users_columns( $columns ) {
$columns['last_seen'] = 'Last seen';
return $columns;
}
// filter columns content to set ours
add_filter( 'manage_users_custom_column', 'se_append_users_column_data', 10, 3 );
/**
* Set last_seen column content with the user last seen data
*
* @param $column_value str the content of the selected column
* @param $column_name str the name of the column
* @param $user_id int the selected user ID
* @return str column value
*/
function se_append_users_column_data( $column_value, $column_name, $user_id ) {
// getting the meta from the database
$meta = (int) get_user_meta( $user_id, 'se_user_last_seen', TRUE );
$diff = $meta > 0 ? se_user_last_seen_diff( $meta ) . ' ago' : 'n/a';
if( 'last_seen' == $column_name )
return $diff;
return $column_value;
}
/**
* Get the date difference based on a specific time integer
*
* @param $targetDate int the date to target (time integer)
* @param $nowStr str when the difference is less than a second (very recent)
* @return str difference
*/
function se_user_last_seen_diff( $targetDate, $nowStr = 'a moment' ) {
$targetDate = new DateTime( date("Y-m-d H:i:s", $targetDate) );
$now = new DateTime( date("Y-m-d H:i:s", time()) );
$delta = $now->diff($targetDate);
$str = '';
$quantities = array(
'year' => $delta->y,
'month' => $delta->m,
'day' => $delta->d,
'hour' => $delta->h,
'minute' => $delta->i,
'second' => $delta->s
);
foreach($quantities as $unit => $value) {
if($value == 0) continue;
if($value != 1) {
$unit .= 's';
}
$str .= $value . ' ' . $unit;
$str .= ', ';
break;
}
return $str == '' ? $nowStr : substr($str, 0, -2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment