webizito_active_user_shortcode(); } public function webizito_enqueue_script() { wp_enqueue_style( 'style-css', plugin_dir_url( __FILE__ ) . 'assets/css/style.css' ); } public function webi_enqueue_custom_scripts() { wp_enqueue_script('webi-plugin-script', plugin_dir_url(__FILE__) . 'assets/js/custom.js', array('jquery'), rand(1,9999), true); } //Update user online status public function webizito_users_status_init(){ $logged_in_users = get_transient('users_status'); $user = wp_get_current_user(); if ( !isset($logged_in_users[$user->ID]['last']) || $logged_in_users[$user->ID]['last'] <= time()-50 ){ $logged_in_users[$user->ID] = array( 'id' => $user->ID, 'username' => $user->user_login, 'last' => time(), ); set_transient('users_status', $logged_in_users, 50); } } //Check if a user has been online in the last 5 minutes public function webizito_is_user_online($id){ $logged_in_users = get_transient('users_status'); return isset($logged_in_users[$id]['last']) && $logged_in_users[$id]['last'] > time()-50; } //Check when a user was last online. public function webizito_user_last_online($id){ $logged_in_users = get_transient('users_status'); if ( isset($logged_in_users[$id]['last']) ){ return $logged_in_users[$id]['last']; } else { return false; } } //Add columns to user listings public function webizito_user_columns_head($defaults){ $defaults['status'] = 'User Online Status'; return $defaults; } //Display Status in Users Page public function webizito_user_columns_content($value='', $column_name, $id){ if ( $column_name == 'status' ){ if ( $this->webizito_is_user_online($id) ){ return ''; } else if($this->webizito_user_last_online($id)){ return ( $this->webizito_user_last_online($id) ) ? '
Last Seen:
' . date('M j, Y @ g:ia', $this->webizito_user_last_online($id)) . '
' : ''; }else{ return ''; } } } //Active Users Metabox public function webizito_active_users_metabox(){ global $wp_meta_boxes; wp_add_dashboard_widget('webizito_active_users', 'Active Users', array($this, 'webizito_active_user_dashboard')); } public function webizito_active_user_dashboard( $post, $callback_args ){ $user_count = count_users(); $users_plural = ( $user_count['total_users'] == 1 )? 'User' : 'Users'; echo '
' . $user_count['total_users'] . ' ' . $users_plural . ' (' . $this->webizito_online_users('count') . ' currently active)
Rate our plugin  ★★★★★
'; } //Active User Shortcode public function webizito_active_user_shortcode(){ add_shortcode('webi_active_user', array($this, 'webizito_active_user')); } public function webizito_active_user(){ ob_start(); if(is_user_logged_in()){ $user_count = count_users(); $users_plural = ( $user_count['total_users'] == 1 ) ? 'User' : 'Users'; echo '
Currently Active Users: (' . $this->webizito_online_users('count') . ')
'; } return ob_get_clean(); } //Display Active User in Admin Bar public function webizito_admin_bar_link() { global $wp_admin_bar; if ( !is_super_admin() || !is_admin_bar_showing() ) return; $wp_admin_bar->add_menu( array( 'id' => 'webi_user_link', 'title' => '' . __( 'Active Users (' . $this->webizito_online_users('count') . ')') .'', 'href' => esc_url( admin_url( 'users.php' ) ) ) ); } //Get a count of online users, or an array of online user IDs public function webizito_online_users($return='count'){ $logged_in_users = get_transient('users_status'); //If no users are online if ( empty($logged_in_users) ){ return ( $return == 'count' )? 0 : false; } $user_online_count = 0; $online_users = array(); foreach ( $logged_in_users as $user ){ if ( !empty($user['username']) && isset($user['last']) && $user['last'] > time()-50 ){ $online_users[] = $user; $user_online_count++; } } return ( $return == 'count' )? $user_online_count : $online_users; } public function webizito_modify_user_view( $views ) { $logged_in_users = get_transient('users_status'); $user = wp_get_current_user(); $logged_in_users[$user->ID] = array( 'id' => $user->ID, 'username' => $user->user_login, 'last' => time(), ); $view = 'User Online ('.$this->webizito_online_users('count').')'; $views['status'] = $view; return $views; } public function webizito_support_and_faq_links( $links_array, $plugin_file_name, $plugin_data, $status ) { if ( strpos( $plugin_file_name, basename(__FILE__) ) ) { // You can still use `array_unshift()` to add links at the beginning. $links_array[] = 'Support'; $links_array[] = 'Docs'; $links_array[] = 'Rate our plugin ★★★★★'; } return $links_array; } public function webizito_plugin_by_link( $links ){ $url = 'https://webizito.com/'; $links[] = '' . __( 'Donate', 'wp-online-active-users' ) . ''; $_link = '' . __( 'By Webizito', 'wp-online-active-users' ) . ''; $links[] = $_link; return $links; } public function webizito_display_notice() { echo '
'; echo '

Enjoying our Wp online active users plugin? Please consider leaving us a review here. Or Support with a small donation here. We would greatly appreciate it!

'; echo '
'; } public function webizito_delete_transient() { delete_transient( 'users_status' ); } } } $myPlugin = new webi_active_user(); if ( ! class_exists( 'Webi_Custom_Widget' ) ) { class Webi_Custom_Widget extends WP_Widget { // Constructor public function __construct() { parent::__construct( 'webi_custom_widget', __( 'WP Online Active User', 'text_domain' ), array( 'description' => __( 'Display Online Active Users.', 'text_domain' ), ) ); } // Front-end display public function widget( $args, $instance ) { echo $args['before_widget']; // Title if ( ! empty( $instance['title'] ) ) { echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title']; } // Widget content if ( class_exists( 'webi_active_user' ) ) { $webi_plugin = new webi_active_user(); $active_users_count = $webi_plugin->webizito_online_users( 'count' ); echo '
'; echo '

Online Active Users: ' . $active_users_count . '

'; echo '
'; } else { echo '

Webi Active User plugin not found.

'; } echo $args['after_widget']; } // Back-end widget form public function form( $instance ) { $title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'Active Users', 'text_domain' ); ?>