-
Notifications
You must be signed in to change notification settings - Fork 2
/
wp-whos-online.php
242 lines (192 loc) · 6.93 KB
/
wp-whos-online.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
/*
Plugin Name: Who's Online
Plugin URI: http://wordpress.org/extend/plugins/wp-whos-online/
Description: Sidebar widget to log when a user was last online
Version: 0.7.3
Author: Annika Backstrom
Author URI: https://sixohthree.com/
License: GPL2
Text Domain: wp-whos-online
Domain Path: /languages/
*/
/* Copyright 2011 Annika Backstrom <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class WP_Whos_Online_Widget extends WP_Widget {
public function __construct() {
parent::__construct(
'widget_wpwhosonline', "Who's Online", __("Who's reading your P2 blog right now? Keep track.", 'wp-whos-online')
);
}
public function widget( $args, $instance ) {
echo $args['before_widget'] . $args['before_title'] . __("Users", 'wp-whos-online') . $args['after_title'];
echo '<ul class="wpwhosonline-list">';
wpwhosonline_list_authors();
echo '</ul>';
echo $args['after_widget'];
}
public static function register() {
register_widget( __CLASS__ );
}
public function form( $instance ) {
}
public function update( $new_instance, $old_instance ) {
return $new_instance;
}
}
function wpwhosonline_enqueue() {
add_action( 'wp_head', 'wpwhosonline_pageoptions_js', 20 );
wp_enqueue_script( 'wpwhosonline', plugins_url('wp-whos-online.js', __FILE__), array('jquery'), 1 );
wp_enqueue_style( 'wpwhosonline_css', plugins_url('wp-whos-online.css', __FILE__), null, 1 );
}
add_action('wp_enqueue_scripts', 'wpwhosonline_enqueue');
// our own ajax call
add_action( 'wp_ajax_wpwhosonline_ajax_update', 'wpwhosonline_ajax_update' );
// hook into p2 ajax calls, if they're there
add_action( 'wp_ajax_prologue_latest_posts', 'wpwhosonline_update' );
add_action( 'wp_ajax_prologue_latest_comments', 'wpwhosonline_update' );
add_action( 'plugins_loaded', 'wpwhosonline_textdomain', 99 );
/**
* Load the plugin's textdomain.
*/
function wpwhosonline_textdomain() {
load_plugin_textdomain( 'wp-whos-online', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Update a user's "last online" timestamp.
*/
function wpwhosonline_update() {
if( !is_user_logged_in() )
return null;
global $user_ID;
update_user_meta( $user_ID, 'wpwhosonline_timestamp', time() );
}//end wpwhosonline_update
add_action('template_redirect', 'wpwhosonline_update');
/**
* Echo json listing all authors who have had their "last online" timestamp updated
* since the client's last update.
*/
function wpwhosonline_ajax_update() {
global $wpdb;
// update timestamp of user who is checking
wpwhosonline_update();
$load_time = strtotime($_GET['load_time'] . ' GMT');
$users = wpwhosonline_recents( "meta_value=$load_time" );
if( count($users) == 0 ) {
die( '0' );
}
$now = time();
$latest = 0;
$return = array();
foreach($users as $user) {
$row = array();
$last_online_ts = get_user_meta( $user->ID, 'wpwhosonline_timestamp', true );
if( $last_online_ts > $latest )
$latest = $last_online_ts;
$row['user_id'] = $user->ID;
$row['html'] = wpwhosonline_user( $last_online_ts, $user );
$row['timestamp'] = $last_online_ts;
$return[] = $row;
}
echo json_encode( array('users' => $return, 'latestupdate' => gmdate('Y-m-d H:i:s', $latest)) );
exit;
}
function wpwhosonline_pageoptions_js() {
global $page_options;
?><script type='text/javascript'>
// <![CDATA[
var wpwhosonline = {
'ajaxUrl': "<?php echo esc_js( admin_url('admin-ajax.php') ); ?>",
'wpwhosonlineLoadTime': "<?php echo gmdate( 'Y-m-d H:i:s' ); ?>",
'getwpwhosonlineUpdate': '0',
'isFirstFrontPage': "<?php echo is_home(); ?>"
};
// ]]>
</script><?php
}
function wpwhosonline_usersort( $a, $b ) {
$ts_a = get_user_meta( $a->ID, 'wpwhosonline_timestamp', true );
$ts_b = get_user_meta( $b->ID, 'wpwhosonline_timestamp', true );
if( $ts_a == $ts_b ) {
return 0;
}
return ($ts_a < $ts_b) ? 1 : -1;
}
function wpwhosonline_recents( $args = array() ) {
$args = wp_parse_args( $args, array(
'meta_key' => 'wpwhosonline_timestamp',
'meta_value' => time() - 604800, // 1 week
'meta_compare' => '>',
'count_total' => false,
));
$users = get_users( $args );
foreach( $users as $user ) {
// grab all these values, or you'll anger usort by modifying
// an array mid-execution.
get_user_meta( $user->ID, 'wpwhosonline_timestamp', true );
}
usort( $users, 'wpwhosonline_usersort' );
return $users;
}
function wpwhosonline_list_authors() {
$users = wpwhosonline_recents();
$html = '';
foreach( $users as $user ) {
$last_online_ts = get_user_meta( $user->ID, 'wpwhosonline_timestamp', true );
$item = wpwhosonline_user( $last_online_ts, $user );
$class = wpwhosonline_class( $last_online_ts );
$item = '<li id="wpwhosonline-' . $user->ID . '" class="wpwhosonline-row ' . $class . '" data-wpwhosonline="' .
esc_attr( $last_online_ts ) . '">' . $item . '</li>';
$html .= $item;
}
echo $html;
}
/**
* Return HTML for a single blog user for the widget.
*
* @uses apply_filters() Calls 'wpwhosonline_author_link' on the author link element
* @return string HTML for the user row
*/
function wpwhosonline_user( $last_online_ts, $user ) {
$avatar = get_avatar( $user->user_email, 32 );
$name = $user->display_name;
$link = '<a href="' . get_author_posts_url( $user->ID, $user->user_nicename ) . '" title="' . esc_attr( sprintf(__("Posts by %s"), $user->display_name) ) . '">' . $name . '</a>';
$link = apply_filters( 'wpwhosonline_author_link', $link, $user );
// this should always exist; we queried using this meta
if ( ! $last_online_ts ) {
return;
}
$now = time();
if ( $now - $last_online_ts < 120 ) {
$last_online = __('Online now!', 'wp-whos-online');
} else {
$last_online = sprintf( __('%s ago', 'wp-whos-online'), human_time_diff( $now, $last_online_ts ) );
}
$last_online_title = date_i18n( get_option('date_format') . ' ' . get_option('time_format'), $last_online_ts );
if ( $last_online ) {
$last_online_title = sprintf( __( 'Last online: %s', 'wp-whos-online' ), $last_online_title );
$last_online = '<span title="' . esc_attr( $last_online_title ) . '">' . $last_online . '</a>';
}
return $avatar . $link . '<br>' . $last_online;
}
function wpwhosonline_class( $lastonline ) {
$diff = time() - $lastonline;
if( $diff > 7200 ) {
return 'wpwhosonline-ancient';
} elseif( $diff > 600 ) {
return 'wpwhosonline-recent';
} else {
return 'wpwhosonline-active';
}
}
add_action( 'widgets_init', 'WP_Whos_Online_Widget::register' );