-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtemplate-tags.php
291 lines (220 loc) · 8.7 KB
/
template-tags.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
### Function: Display UserOnline
function users_online() {
echo get_users_online();
}
function get_users_online() {
$template = UserOnline_Core::$options->templates['useronline'];
$template = str_ireplace( '%PAGE_URL%', UserOnline_Core::$options->url, $template );
$template = str_ireplace( '%MOSTONLINE_COUNT%', get_most_users_online(), $template );
$template = str_ireplace( '%MOSTONLINE_DATE%', get_most_users_online_date(), $template );
return UserOnline_Template::format_count( get_users_online_count(), 'user', $template );
}
### Function: Display UserOnline Count
function users_online_count() {
echo number_format_i18n( get_useronline_count() );
}
function get_users_online_count() {
return UserOnline_Core::get_user_online_count();
}
### Function: Display Max UserOnline
function most_users_online() {
echo number_format_i18n( get_most_users_online() );
}
function get_most_users_online() {
return intval( UserOnline_Core::$most->count );
}
### Function: Display Max UserOnline Date
function most_users_online_date() {
echo get_most_users_online_date();
}
function get_most_users_online_date() {
return UserOnline_Template::format_date( UserOnline_Core::$most->date );
}
### Function: Display Users Browsing The Site
function users_browsing_site() {
echo get_users_browsing_site();
}
function get_users_browsing_site() {
return UserOnline_Template::compact_list( 'site' );
}
### Function: Display Users Browsing The ( Current ) Page
function users_browsing_page( $page_url = '' ) {
echo get_users_browsing_page( $page_url );
}
function get_users_browsing_page( $page_url = '' ) {
return UserOnline_Template::compact_list( 'page', 'html', $page_url );
}
### Function: UserOnline Page
function users_online_page() {
global $wpdb;
$usersonline = $wpdb->get_results( "SELECT * FROM $wpdb->useronline ORDER BY timestamp DESC" );
$user_buckets = array();
foreach ( $usersonline as $useronline )
$user_buckets[$useronline->user_type][] = $useronline;
$user_buckets = apply_filters( 'useronline_buckets', $user_buckets );
$counts = UserOnline_Template::get_counts( $user_buckets );
$nicetexts = array();
foreach ( array( 'user', 'member', 'guest', 'bot' ) as $user_type )
$nicetexts[$user_type] = UserOnline_Template::format_count( $counts[$user_type], $user_type );
$text = _n(
'There is <strong>%s</strong> online now: <strong>%s</strong>, <strong>%s</strong> and <strong>%s</strong>.',
'There are a total of <strong>%s</strong> online now: <strong>%s</strong>, <strong>%s</strong> and <strong>%s</strong>.',
$counts['user'], 'wp-useronline'
);
$output =
html( 'div id="useronline-details"',
html( 'p', vsprintf( $text, $nicetexts ) )
.html( 'p', UserOnline_Template::format_most_users() )
.UserOnline_Template::detailed_list( $counts, $user_buckets, $nicetexts )
);
return apply_filters( 'useronline_page', $output );
}
### Function Check If User Is Online
function is_user_online( $user_id ) {
global $wpdb;
return (bool) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( * ) FROM $wpdb->useronline WHERE user_id = %d LIMIT 1", $user_id ) );
}
function get_useronline_( $output, $type = 'site' ) {
return UserOnline_Template::compact_list( $type, $output );
}
class UserOnline_Template {
private static $cache = array();
static function compact_list( $type, $output = 'html', $page_url = '') {
UserOnline_Core::$add_script = true;
if ( !isset( self::$cache[$type] ) ) {
global $wpdb;
if ( 'site' == $type ) {
$where = '';
} elseif ( 'page' == $type ) {
if ( empty($page_url) )
$page_url = $_SERVER['REQUEST_URI'];
$where = $wpdb->prepare( 'WHERE page_url = %s', $page_url );
}
self::$cache[$type . $page_url] = $wpdb->get_results( "SELECT * FROM $wpdb->useronline $where ORDER BY timestamp DESC" );
}
$users = self::$cache[$type . $page_url];
if ( 'list' == $output )
return $users;
$buckets = array();
foreach ( $users as $user )
$buckets[$user->user_type][] = $user;
if ( 'buckets' == $output )
return $buckets;
$counts = self::get_counts( $buckets );
if ( 'counts' == $output )
return $counts;
// Template - Naming Conventions
$naming = UserOnline_Core::$options->naming;
// Template - User(s) Browsing Site
$template = UserOnline_Core::$options->templates["browsing$type"];
// Nice Text For Users
$output = self::format_count( $counts['user'], 'user', $template['text'] );
// Print Member Name
$temp_member = '';
$members = @$buckets['member'];
if ( $members ) {
$temp_member = array();
foreach ( $members as $member )
$temp_member[] = self::format_name( $member );
$temp_member = implode( $template['separators']['members'], $temp_member );
}
$output = str_ireplace( '%MEMBER_NAMES%', $temp_member, $output );
// Counts
foreach ( array( 'member', 'guest', 'bot' ) as $user_type ) {
if ( $counts[$user_type] > 1 )
$number = str_ireplace( '%COUNT%', number_format_i18n( $counts[$user_type] ), $naming[$user_type . 's'] );
elseif ( $counts[$user_type] == 1 )
$number = $naming[$user_type];
else
$number = '';
$output = str_ireplace( "%{$user_type}S%", $number, $output );
}
// SEPARATORs
$separator = ( $counts['member'] && $counts['guest'] ) ? $template['separators']['guests'] : '';
$output = str_ireplace( '%GUESTS_SEPARATOR%', $separator, $output );
$separator = ( ( $counts['guest'] || $counts['member'] ) && $counts['bot'] ) ? $template['separators']['bots'] : '';
$output = str_ireplace( '%BOTS_SEPARATOR%', $separator, $output );
return $output;
}
static function detailed_list( $counts, $user_buckets, $nicetexts ) {
UserOnline_Core::$add_script = true;
if ( $counts['user'] == 0 )
return html( 'h2', __( 'No one is online now.', 'wp-useronline' ) );
$_on = __( 'on', 'wp-useronline' );
$_url = __( 'url', 'wp-useronline' );
$_referral = __( 'referral', 'wp-useronline' );
$output = '';
foreach ( array( 'member', 'guest', 'bot' ) as $user_type ) {
if ( !$counts[$user_type] )
continue;
$count = $counts[$user_type];
$users = $user_buckets[$user_type];
$nicetext = $nicetexts[$user_type];
$output .= html( 'h2', $nicetext . ' ' . __( 'Online Now', 'wp-useronline' ) );
$i=1;
foreach ( $users as $user ) {
$nr = number_format_i18n( $i++ );
$name = self::format_name( $user );
$user_ip = self::format_ip( $user );
$date = self::format_date( $user->timestamp, true );
if ( current_user_can( 'edit_users' ) || false === strpos( $user->page_url, 'wp-admin' ) ) {
$page_title = esc_html( $user->page_title );
$current_link = self::format_link( $user->page_url, $_url );
$referral_link = self::format_link( $user->referral, $_referral );
}
$output .= apply_filters("useronline_custom_template", "<p><strong>#$nr - $name</strong> $user_ip $_on $date<br/>$page_title $current_link $referral_link</p>\n", $nr, $user);
}
}
return $output;
}
static function format_link($url, $title) {
if ( !empty($url) )
return '[' . html_link( $url, $title ) . ']';
return '';
}
static function format_ip( $user ) {
$ip = esc_attr( $user->user_ip );
if ( current_user_can( 'edit_users' ) && !empty( $ip ) && $ip != 'unknown' ) {
return
html( 'span', array('dir' => 'ltr'),
html( 'a', array(
'href' => esc_url( 'http://whois.domaintools.com/' . $ip ),
'title' => esc_attr( $user->user_agent )
), $ip )
);
}
}
static function format_date( $date, $mysql = false ) {
if ( $mysql )
return mysql2date( sprintf( __( '%s @ %s', 'wp-useronline' ), get_option( 'date_format' ), get_option( 'time_format' ) ), $date, true );
return date_i18n( sprintf( __( '%s @ %s', 'wp-useronline' ), get_option( 'date_format' ), get_option( 'time_format' ) ), $date );
}
static function format_name( $user ) {
return apply_filters( 'useronline_display_user', esc_html( $user->user_name ), $user );
}
static function format_count( $count, $user_type, $template = false ) {
$i = ( $count == 1 ) ? '' : 's';
$string = UserOnline_Core::$options->naming[$user_type . $i];
$output = str_ireplace( '%COUNT%', number_format_i18n( $count ), $string );
if ( false === $template )
return $output;
return str_ireplace( '%USERS%', $output, $template );
}
static function format_most_users() {
return sprintf( __( 'Most users ever online were <strong>%s</strong>, on <strong>%s</strong>', 'wp-useronline' ),
number_format_i18n( get_most_users_online() ),
get_most_users_online_date()
);
}
static function get_counts( $buckets ) {
$counts = array();
$total = 0;
foreach ( array( 'member', 'guest', 'bot' ) as $user_type ) {
$count = isset( $buckets[$user_type] ) ? count( @$buckets[$user_type] ) : 0;
$total += $counts[$user_type] = $count;
}
$counts['user'] = $total;
return $counts;
}
}