Plugin Directory

Changeset 330945


Ignore:
Timestamp:
01/10/2011 07:33:23 PM (15 years ago)
Author:
AdamBackstrom
Message:

Show text "Online now!" if the user is currently online.

Location:
wp-whos-online/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • wp-whos-online/trunk/wp-whos-online.js

    r144379 r330945  
    1919        toggleUpdates();
    2020        updateRecents();
     21    };
     22
     23    // from http://snippets.dzone.com/posts/show/5925
     24    function formatDate(formatDate, formatString) {
     25        if(formatDate instanceof Date) {
     26            var months = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
     27            var yyyy = formatDate.getFullYear();
     28            var yy = yyyy.toString().substring(2);
     29            var m = formatDate.getMonth();
     30            var mm = m < 10 ? "0" + m : m;
     31            var mmm = months[m];
     32            var d = formatDate.getDate();
     33            var dd = d < 10 ? "0" + d : d;
     34
     35            var h = formatDate.getHours();
     36            var hh = h < 10 ? "0" + h : h;
     37            var n = formatDate.getMinutes();
     38            var nn = n < 10 ? "0" + n : n;
     39            var s = formatDate.getSeconds();
     40            var ss = s < 10 ? "0" + s : s;
     41
     42            formatString = formatString.replace(/yyyy/i, yyyy);
     43            formatString = formatString.replace(/yy/i, yy);
     44            formatString = formatString.replace(/mmm/i, mmm);
     45            formatString = formatString.replace(/mm/i, mm);
     46            formatString = formatString.replace(/m/i, m);
     47            formatString = formatString.replace(/dd/i, dd);
     48            formatString = formatString.replace(/d/i, d);
     49            formatString = formatString.replace(/hh/i, hh);
     50            formatString = formatString.replace(/h/i, h);
     51            formatString = formatString.replace(/nn/i, nn);
     52            formatString = formatString.replace(/n/i, n);
     53            formatString = formatString.replace(/ss/i, ss);
     54            formatString = formatString.replace(/s/i, s);
     55
     56            return formatString;
     57        } else {
     58            return "";
     59        }
    2160    }
    2261
     
    2463        var now = Math.round(new Date().getTime()/1000.0);
    2564
     65        var fresh = 120; // 2 minutes
    2666        var old = 600; // 10 minutes
    2767        var ancient = 7200; // 2 hours
     
    3272
    3373            var last = $o.data('wpwhosonline_timestamp');
    34             if( typeof last == 'undefined' ) {
    35                 last = Date.parse($o.text()) / 1000;
     74
     75            // last will be undefined the first time through
     76            if( last == null ) {
     77                var theText = $o.text();
     78
     79                if( theText == 'Online now!' ) {
     80                    last = now;
     81                } else {
     82                    last = Date.parse($o.text()) / 1000;
     83                }
    3684            }
    3785
     
    4492            } else {
    4593                oclass = "active";
     94
     95                // no longer fresh; remove "Online now!' text
     96                if(since > fresh && $o.text() == 'Online now!' ) {
     97                    var theDate = new Date(last * 1000);
     98                    $o.text( formatDate(theDate, 'dd mmm yyyy HH:MM:ss') );
     99                    console.log( $o.text() );
     100                }
    46101            }
    47102
  • wp-whos-online/trunk/wp-whos-online.php

    r144379 r330945  
    5757    }
    5858
     59    $now = time();
     60
    5961    $latest = 0;
    6062    foreach($authors as $author) {
     
    6365
    6466        $author->wpwhosonline_unix = (int)$author->wpwhosonline;
    65         $author->wpwhosonline = strftime( '%d %b %Y %H:%M:%S %Z', $author->wpwhosonline );
     67        if( $now - $author->wpwhosonline_unix < 120 ) {
     68            $author->wpwhosonline = 'Online now!';
     69        } else {
     70            $author->wpwhosonline = strftime( '%d %b %Y %H:%M:%S %Z', $author->wpwhosonline );
     71        }
    6672    }
    6773
     
    190196
    191197        if ( $wpwhosonline ) {
     198            $now = time();
     199
    192200            $wpwhosonline_time = get_usermeta( $author->ID, 'wpwhosonline_timestamp' );
    193201            if( $wpwhosonline_time ) {
    194                 $wpwhosonline_time = strftime( '%d %b %Y %H:%M:%S %Z', $wpwhosonline_time );
     202                if( $now - $wpwhosonline_time < 120 ) {
     203                    $wpwhosonline_time = 'Online now!';
     204                } else {
     205                    $wpwhosonline_time = strftime( '%d %b %Y %H:%M:%S %Z', $wpwhosonline_time );
     206                }
    195207            } else {
    196208                $wpwhosonline_time = '';
Note: See TracChangeset for help on using the changeset viewer.