| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Online Active Users |
| 4 |
* Plugin Title: Online Active Users Plugin |
| 5 |
* Plugin URI: https://wordpress.org/plugins/online-active-users/ |
| 6 |
* Description: WordPress Online Active Users plugin enables you to display how many users are currently online active and display user last seen on your Users page in the WordPress admin. |
| 7 |
* Tags: wp-online-active-users, users, active-users, online-user, available-users |
| 8 |
* Version: 2.7 |
| 9 |
* Author: Webizito |
| 10 |
* Author URI: http://webizito.com/ |
| 11 |
* Contributors: valani9099 |
| 12 |
* License: GPLv3 or later |
| 13 |
* License URI: https://www.gnu.org/licenses/gpl-3.0.html |
| 14 |
* Text Domain: wp-online-active-users |
| 15 |
* Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=APRNBJUZHRP7G |
| 16 |
*/ |
| 17 |
|
| 18 |
// Exit if accessed directly |
| 19 |
if ( ! defined( 'ABSPATH' ) ) { |
| 20 |
exit; |
| 21 |
} |
| 22 |
|
| 23 |
if ( ! class_exists( 'webi_active_user' ) ) { |
| 24 |
class webi_active_user { |
| 25 |
public function __construct(){ |
| 26 |
|
| 27 |
register_activation_hook( __FILE__, array($this, 'webizito_users_status_init' )); |
| 28 |
add_action('init', array($this, 'webizito_users_status_init')); |
| 29 |
add_action('init', array($this, 'webi_track_user_activity')); |
| 30 |
add_action('clear_auth_cookie', array($this, 'webizito_user_logout')); |
| 31 |
add_action('wp_loaded', array($this,'webizito_enqueue_script')); |
| 32 |
add_action('admin_enqueue_scripts', array($this,'webi_enqueue_custom_scripts')); |
| 33 |
add_action('admin_init', array($this, 'webizito_users_status_init')); |
| 34 |
add_action('wp_dashboard_setup', array($this, 'webizito_active_users_metabox')); |
| 35 |
add_filter('manage_users_columns', array($this, 'webizito_user_columns_head')); |
| 36 |
add_action('manage_users_custom_column', array($this, 'webizito_user_columns_content'), 10, 10); |
| 37 |
add_filter('views_users', array($this, 'webizito_modify_user_view' )); |
| 38 |
add_action('admin_bar_menu', array($this, 'webizito_admin_bar_link'),999); |
| 39 |
add_filter('plugin_row_meta', array($this, 'webizito_support_and_faq_links'), 10, 4 ); |
| 40 |
add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), array($this, 'webizito_plugin_by_link'), 10, 2 ); |
| 41 |
add_action('admin_notices', array($this,'webizito_display_notice')); |
| 42 |
register_deactivation_hook( __FILE__, array($this,'webizito_display_notice' )); |
| 43 |
register_deactivation_hook( __FILE__, array($this,'webizito_delete_transient' )); |
| 44 |
|
| 45 |
$this->webizito_active_user_shortcode(); |
| 46 |
} |
| 47 |
|
| 48 |
public function webizito_enqueue_script() { |
| 49 |
wp_enqueue_style( 'style-css', plugin_dir_url( __FILE__ ) . 'assets/css/style.css' ); |
| 50 |
} |
| 51 |
|
| 52 |
public function webi_enqueue_custom_scripts() { |
| 53 |
wp_enqueue_script('webi-plugin-script', plugin_dir_url(__FILE__) . 'assets/js/custom.js', array('jquery'), rand(1,9999), true); |
| 54 |
|
| 55 |
wp_add_inline_script('webi-plugin-script', " |
| 56 |
jQuery(document).ready(function($) { |
| 57 |
$('.webizito-last-seen').each(function() { |
| 58 |
var timestamp = $(this).data('timestamp'); |
| 59 |
if (timestamp) { |
| 60 |
var date = new Date(timestamp * 1000); |
| 61 |
$(this).text(date.toLocaleString()); |
| 62 |
} |
| 63 |
}); |
| 64 |
}); |
| 65 |
"); |
| 66 |
} |
| 67 |
|
| 68 |
//Update user online status |
| 69 |
public function webizito_user_login($user_login, $user) { |
| 70 |
$logged_in_users = get_transient('users_status'); |
| 71 |
|
| 72 |
if (!is_array($logged_in_users)) { |
| 73 |
$logged_in_users = array(); |
| 74 |
} |
| 75 |
|
| 76 |
$logged_in_users[$user->ID] = array( |
| 77 |
'id' => $user->ID, |
| 78 |
'username' => $user->user_login, |
| 79 |
'last' => time(), |
| 80 |
); |
| 81 |
|
| 82 |
set_transient('users_status', $logged_in_users, 60); |
| 83 |
update_user_meta($user->ID, 'last_seen', time()); |
| 84 |
} |
| 85 |
|
| 86 |
public function webizito_users_status_init() { |
| 87 |
$logged_in_users = get_transient('users_status'); |
| 88 |
|
| 89 |
if (!is_array($logged_in_users)) { |
| 90 |
$logged_in_users = array(); |
| 91 |
} |
| 92 |
|
| 93 |
$user = wp_get_current_user(); |
| 94 |
if ($user->ID) { |
| 95 |
// Update user's last seen time |
| 96 |
$logged_in_users[$user->ID] = array( |
| 97 |
'id' => $user->ID, |
| 98 |
'username' => $user->user_login, |
| 99 |
'last' => time(), |
| 100 |
); |
| 101 |
|
| 102 |
// Store without expiration, keep refreshing via heartbeat |
| 103 |
set_transient('users_status', $logged_in_users); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
// Remove user from the online list when they log out |
| 108 |
public function webizito_user_logout() { |
| 109 |
if (!is_user_logged_in()) return; // Prevent errors if already logged out |
| 110 |
|
| 111 |
$user_id = get_current_user_id(); |
| 112 |
if (!$user_id) return; |
| 113 |
|
| 114 |
$logged_in_users = get_transient('users_status'); |
| 115 |
|
| 116 |
if (isset($logged_in_users[$user_id])) { |
| 117 |
unset($logged_in_users[$user_id]); |
| 118 |
set_transient('users_status', $logged_in_users, 60); |
| 119 |
} |
| 120 |
|
| 121 |
update_user_meta($user_id, 'last_seen', time()); |
| 122 |
} |
| 123 |
|
| 124 |
//Check if a user has been online in the last 5 minutes |
| 125 |
public function webizito_is_user_online($id){ |
| 126 |
$logged_in_users = get_transient('users_status'); |
| 127 |
|
| 128 |
// User is online if found in transient and last activity is within 50 seconds |
| 129 |
if (isset($logged_in_users[$id]) && $logged_in_users[$id]['last'] > time() - 50) { |
| 130 |
return true; |
| 131 |
} |
| 132 |
|
| 133 |
// Fallback: Check user meta if transient fails |
| 134 |
$last_seen = get_user_meta($id, 'last_seen', true); |
| 135 |
return ($last_seen && $last_seen > time() - 50); |
| 136 |
} |
| 137 |
|
| 138 |
//Check when a user was last online. |
| 139 |
public function webizito_user_last_online($id){ |
| 140 |
$logged_in_users = get_transient('users_status') ?: array(); |
| 141 |
return isset($logged_in_users[$id]['last']) ? $logged_in_users[$id]['last'] : false; |
| 142 |
} |
| 143 |
|
| 144 |
// Always update last seen |
| 145 |
public function webi_track_user_activity() { |
| 146 |
if (is_user_logged_in()) { |
| 147 |
$user_id = get_current_user_id(); |
| 148 |
update_user_meta($user_id, 'last_seen', time()); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
//Add columns to user listings |
| 153 |
public function webizito_user_columns_head($defaults){ |
| 154 |
$defaults['status'] = 'User Online Status'; |
| 155 |
return $defaults; |
| 156 |
} |
| 157 |
|
| 158 |
//Display Status in Users Page |
| 159 |
public function webizito_user_columns_content($value = '', $column_name, $id) { |
| 160 |
if ($column_name == 'status') { |
| 161 |
if ($this->webizito_is_user_online($id)) { |
| 162 |
return '<span class="online-logged-in">●</span> <br /><small><em>Online Now</em></small>'; |
| 163 |
} else { |
| 164 |
$last_seen = get_user_meta($id, 'last_seen', true); |
| 165 |
|
| 166 |
if (!$last_seen) { |
| 167 |
$last_seen_text = "<small><em>Never Logged In</em></small>"; |
| 168 |
return '<span class="never-dot">●</span> <br />' . $last_seen_text; |
| 169 |
} else { |
| 170 |
$last_seen_text = "<small>Last Seen: <br /><em class='webizito-last-seen' data-timestamp='{$last_seen}'>" . date('M j, Y @ g:ia', $last_seen) . "</em></small>"; |
| 171 |
return '<span class="offline-dot">●</span> <br />' . $last_seen_text; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
//Active Users Metabox |
| 181 |
public function webizito_active_users_metabox(){ |
| 182 |
global $wp_meta_boxes; |
| 183 |
wp_add_dashboard_widget('webizito_active_users', 'Active Users', array($this, 'webizito_active_user_dashboard')); |
| 184 |
} |
| 185 |
|
| 186 |
public function webizito_active_user_dashboard( $post, $callback_args ){ |
| 187 |
$user_count = count_users(); |
| 188 |
$users_plural = ( $user_count['total_users'] == 1 )? 'User' : 'Users'; |
| 189 |
echo '<div><a href="users.php">' . $user_count['total_users'] . ' ' . $users_plural . '</a> <small>(' . $this->webizito_online_users('count') . ' currently active)</small> |
| 190 |
<br /> |
| 191 |
<strong><a href="https://wordpress.org/support/plugin/online-active-users/reviews/?rate=5#new-post" target="_blank">Rate our plugin <span style="color:#ffb900;font-size: 18px;position:relative;top:0.1em;">� |
| 192 |
� |
| 193 |
� |
| 194 |
� |
| 195 |
� |
| 196 |
</span></a></strong></div>'; |
| 197 |
} |
| 198 |
|
| 199 |
//Active User Shortcode |
| 200 |
public function webizito_active_user_shortcode(){ |
| 201 |
add_shortcode('webi_active_user', array($this, 'webizito_active_user')); |
| 202 |
} |
| 203 |
|
| 204 |
public function webizito_active_user(){ |
| 205 |
ob_start(); |
| 206 |
if(is_user_logged_in()){ |
| 207 |
$user_count = count_users(); |
| 208 |
$users_plural = ( $user_count['total_users'] == 1 ) ? 'User' : 'Users'; |
| 209 |
echo '<div class="webi-active-users"> Currently Active Users: <small>(' . $this->webizito_online_users('count') . ')</small></div>'; |
| 210 |
} |
| 211 |
return ob_get_clean(); |
| 212 |
} |
| 213 |
|
| 214 |
//Display Active User in Admin Bar |
| 215 |
public function webizito_admin_bar_link() { |
| 216 |
global $wp_admin_bar; |
| 217 |
if ( !is_super_admin() || !is_admin_bar_showing() ) |
| 218 |
return; |
| 219 |
$wp_admin_bar->add_menu( array( |
| 220 |
'id' => 'webi_user_link', |
| 221 |
'title' => '<span class="ab-icon online-logged-in">●</span><span class="ab-label">' . __( 'Active Users (' . $this->webizito_online_users('count') . ')') .'</span>', |
| 222 |
'href' => esc_url( admin_url( 'users.php' ) ) |
| 223 |
) ); |
| 224 |
} |
| 225 |
|
| 226 |
//Get a count of online users, or an array of online user IDs |
| 227 |
public function webizito_online_users($return='count'){ |
| 228 |
$logged_in_users = get_transient('users_status'); |
| 229 |
|
| 230 |
//If no users are online |
| 231 |
if ( empty($logged_in_users) ){ |
| 232 |
return ( $return == 'count' )? 0 : false; |
| 233 |
} |
| 234 |
|
| 235 |
$user_online_count = 0; |
| 236 |
$online_users = array(); |
| 237 |
foreach ( $logged_in_users as $user ){ |
| 238 |
if ( !empty($user['username']) && isset($user['last']) && $user['last'] > time()-50 ){ |
| 239 |
$online_users[] = $user; |
| 240 |
$user_online_count++; |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
return ( $return == 'count' )? $user_online_count : $online_users; |
| 245 |
|
| 246 |
} |
| 247 |
|
| 248 |
public function webizito_modify_user_view( $views ) { |
| 249 |
|
| 250 |
$logged_in_users = get_transient('users_status'); |
| 251 |
$user = wp_get_current_user(); |
| 252 |
|
| 253 |
$logged_in_users[$user->ID] = array( |
| 254 |
'id' => $user->ID, |
| 255 |
'username' => $user->user_login, |
| 256 |
'last' => time(), |
| 257 |
); |
| 258 |
|
| 259 |
$view = '<a href=' . admin_url('users.php') . '>User Online <span class="count">('.$this->webizito_online_users('count').')</span></a>'; |
| 260 |
|
| 261 |
$views['status'] = $view; |
| 262 |
return $views; |
| 263 |
} |
| 264 |
|
| 265 |
public function webizito_support_and_faq_links( $links_array, $plugin_file_name, $plugin_data, $status ) { |
| 266 |
if ( strpos( $plugin_file_name, basename(__FILE__) ) ) { |
| 267 |
|
| 268 |
// You can still use `array_unshift()` to add links at the beginning. |
| 269 |
$links_array[] = '<a href="https://wordpress.org/support/plugin/online-active-users/" target="_blank">Support</a>'; |
| 270 |
$links_array[] = '<a href="https://webizito.com/wp-online-active-users/" target="_blank">Docs</a>'; |
| 271 |
$links_array[] = '<strong><a href="https://wordpress.org/support/plugin/online-active-users/reviews/?rate=5#new-post" target="_blank">Rate our plugin <span style="color:#ffb900;font-size: 18px;position:relative;top:0.1em;">� |
| 272 |
� |
| 273 |
� |
| 274 |
� |
| 275 |
� |
| 276 |
</span></a></strong>'; |
| 277 |
} |
| 278 |
return $links_array; |
| 279 |
} |
| 280 |
|
| 281 |
public function webizito_plugin_by_link( $links ){ |
| 282 |
$url = 'https://webizito.com/'; |
| 283 |
$links[] = '<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=APRNBJUZHRP7G" target="_blank">' . __( '<span style="font-weight: bold;">Donate</span>', 'wp-online-active-users' ) . '</a>'; |
| 284 |
$_link = '<a href="'.$url.'" target="_blank">' . __( 'By <span>Webizito</span>', 'wp-online-active-users' ) . '</a>'; |
| 285 |
$links[] = $_link; |
| 286 |
return $links; |
| 287 |
} |
| 288 |
|
| 289 |
public function webizito_display_notice() { |
| 290 |
echo '<div class="notice notice-success is-dismissible wp-online-active-users-notice" id="wp-online-active-users-notice">'; |
| 291 |
echo '<p>Enjoying our Wp online active users plugin? Please consider leaving us a review <a href="https://wordpress.org/support/plugin/online-active-users/reviews/?rate=5#new-post" target="_blank">here</a>. Or Support with a small donation <a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=APRNBJUZHRP7G" target="_blank">here</a>. We would greatly appreciate it!</p>'; |
| 292 |
echo '</div>'; |
| 293 |
} |
| 294 |
|
| 295 |
public function webizito_delete_transient() { |
| 296 |
delete_transient( 'users_status' ); |
| 297 |
} |
| 298 |
|
| 299 |
} |
| 300 |
} |
| 301 |
$myPlugin = new webi_active_user(); |
| 302 |
|
| 303 |
if ( ! class_exists( 'Webi_Custom_Widget' ) ) { |
| 304 |
class Webi_Custom_Widget extends WP_Widget { |
| 305 |
|
| 306 |
// Constructor |
| 307 |
public function __construct() { |
| 308 |
parent::__construct( |
| 309 |
'webi_custom_widget', |
| 310 |
__( 'WP Online Active User', 'text_domain' ), |
| 311 |
array( 'description' => __( 'Display Online Active Users.', 'text_domain' ), ) |
| 312 |
); |
| 313 |
} |
| 314 |
|
| 315 |
// Front-end display |
| 316 |
public function widget( $args, $instance ) { |
| 317 |
echo $args['before_widget']; |
| 318 |
|
| 319 |
// Title |
| 320 |
if ( ! empty( $instance['title'] ) ) { |
| 321 |
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title']; |
| 322 |
} |
| 323 |
|
| 324 |
// Widget content |
| 325 |
if ( class_exists( 'webi_active_user' ) ) { |
| 326 |
$webi_plugin = new webi_active_user(); |
| 327 |
$active_users_count = $webi_plugin->webizito_online_users( 'count' ); |
| 328 |
echo '<div class="webi-widget-content">'; |
| 329 |
echo '<p>Online Active Users: <strong>' . $active_users_count . '</strong></p>'; |
| 330 |
echo '</div>'; |
| 331 |
} else { |
| 332 |
echo '<p>Webi Active User plugin not found.</p>'; |
| 333 |
} |
| 334 |
|
| 335 |
echo $args['after_widget']; |
| 336 |
} |
| 337 |
|
| 338 |
// Back-end widget form |
| 339 |
public function form( $instance ) { |
| 340 |
$title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'Active Users', 'text_domain' ); |
| 341 |
?> |
| 342 |
<p> |
| 343 |
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_attr_e( 'Title:', 'text_domain' ); ?></label> |
| 344 |
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>"> |
| 345 |
</p> |
| 346 |
<?php |
| 347 |
} |
| 348 |
|
| 349 |
// Save widget settings |
| 350 |
public function update( $new_instance, $old_instance ) { |
| 351 |
$instance = array(); |
| 352 |
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : ''; |
| 353 |
return $instance; |
| 354 |
} |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
// Register the custom widget |
| 359 |
add_action( 'widgets_init', function() { |
| 360 |
register_widget( 'Webi_Custom_Widget' ); |
| 361 |
} ); |
| 362 |
|