| 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.4 |
| 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('wp_loaded', array($this,'webizito_enqueue_script')); |
| 30 |
add_action('admin_enqueue_scripts', array($this,'webi_enqueue_custom_scripts')); |
| 31 |
add_action('admin_init', array($this, 'webizito_users_status_init')); |
| 32 |
add_action('wp_dashboard_setup', array($this, 'webizito_active_users_metabox')); |
| 33 |
add_filter('manage_users_columns', array($this, 'webizito_user_columns_head')); |
| 34 |
add_action('manage_users_custom_column', array($this, 'webizito_user_columns_content'), 10, 10); |
| 35 |
add_filter('views_users', array($this, 'webizito_modify_user_view' )); |
| 36 |
add_action('admin_bar_menu', array($this, 'webizito_admin_bar_link'),999); |
| 37 |
add_filter('plugin_row_meta', array($this, 'webizito_support_and_faq_links'), 10, 4 ); |
| 38 |
add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), array($this, 'webizito_plugin_by_link'), 10, 2 ); |
| 39 |
add_action('admin_notices', array($this,'webizito_display_notice')); |
| 40 |
register_deactivation_hook( __FILE__, array($this,'webizito_display_notice' )); |
| 41 |
register_deactivation_hook( __FILE__, array($this,'webizito_delete_transient' )); |
| 42 |
|
| 43 |
$this->webizito_active_user_shortcode(); |
| 44 |
} |
| 45 |
|
| 46 |
public function webizito_enqueue_script() { |
| 47 |
wp_enqueue_style( 'style-css', plugin_dir_url( __FILE__ ) . 'assets/css/style.css' ); |
| 48 |
} |
| 49 |
|
| 50 |
public function webi_enqueue_custom_scripts() { |
| 51 |
wp_enqueue_script('webi-plugin-script', plugin_dir_url(__FILE__) . 'assets/js/custom.js', array('jquery'), rand(1,9999), true); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
//Update user online status |
| 56 |
public function webizito_users_status_init(){ |
| 57 |
$logged_in_users = get_transient('users_status'); |
| 58 |
$user = wp_get_current_user(); |
| 59 |
|
| 60 |
if ( !isset($logged_in_users[$user->ID]['last']) || $logged_in_users[$user->ID]['last'] <= time()-50 ){ |
| 61 |
$logged_in_users[$user->ID] = array( |
| 62 |
'id' => $user->ID, |
| 63 |
'username' => $user->user_login, |
| 64 |
'last' => time(), |
| 65 |
); |
| 66 |
set_transient('users_status', $logged_in_users, 50); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
//Check if a user has been online in the last 5 minutes |
| 71 |
public function webizito_is_user_online($id){ |
| 72 |
$logged_in_users = get_transient('users_status'); |
| 73 |
return isset($logged_in_users[$id]['last']) && $logged_in_users[$id]['last'] > time()-50; |
| 74 |
} |
| 75 |
|
| 76 |
//Check when a user was last online. |
| 77 |
public function webizito_user_last_online($id){ |
| 78 |
$logged_in_users = get_transient('users_status'); |
| 79 |
if ( isset($logged_in_users[$id]['last']) ){ |
| 80 |
return $logged_in_users[$id]['last']; |
| 81 |
} else { |
| 82 |
return false; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
//Add columns to user listings |
| 87 |
public function webizito_user_columns_head($defaults){ |
| 88 |
$defaults['status'] = 'User Online Status'; |
| 89 |
return $defaults; |
| 90 |
} |
| 91 |
|
| 92 |
//Display Status in Users Page |
| 93 |
public function webizito_user_columns_content($value='', $column_name, $id){ |
| 94 |
if ( $column_name == 'status' ){ |
| 95 |
if ( $this->webizito_is_user_online($id) ){ |
| 96 |
return '<span class="online-logged-in">●</span>'; |
| 97 |
} else if($this->webizito_user_last_online($id)){ |
| 98 |
return ( $this->webizito_user_last_online($id) ) ? ' <span class="offline-dot">●</span><br /><small>Last Seen: <br /><em>' . date('M j, Y @ g:ia', $this->webizito_user_last_online($id)) . '</em></small>' : ''; |
| 99 |
}else{ |
| 100 |
return '<span class="offline-dot">●</span>'; |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
//Active Users Metabox |
| 107 |
public function webizito_active_users_metabox(){ |
| 108 |
global $wp_meta_boxes; |
| 109 |
wp_add_dashboard_widget('webizito_active_users', 'Active Users', array($this, 'webizito_active_user_dashboard')); |
| 110 |
} |
| 111 |
|
| 112 |
public function webizito_active_user_dashboard( $post, $callback_args ){ |
| 113 |
$user_count = count_users(); |
| 114 |
$users_plural = ( $user_count['total_users'] == 1 )? 'User' : 'Users'; |
| 115 |
echo '<div><a href="users.php">' . $user_count['total_users'] . ' ' . $users_plural . '</a> <small>(' . $this->webizito_online_users('count') . ' currently active)</small> |
| 116 |
<br /> |
| 117 |
<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;">� |
| 118 |
� |
| 119 |
� |
| 120 |
� |
| 121 |
� |
| 122 |
</span></a></strong></div>'; |
| 123 |
} |
| 124 |
|
| 125 |
//Active User Shortcode |
| 126 |
public function webizito_active_user_shortcode(){ |
| 127 |
add_shortcode('webi_active_user', array($this, 'webizito_active_user')); |
| 128 |
} |
| 129 |
|
| 130 |
public function webizito_active_user(){ |
| 131 |
ob_start(); |
| 132 |
if(is_user_logged_in()){ |
| 133 |
$user_count = count_users(); |
| 134 |
$users_plural = ( $user_count['total_users'] == 1 ) ? 'User' : 'Users'; |
| 135 |
echo '<div class="webi-active-users"> Currently Active Users: <small>(' . $this->webizito_online_users('count') . ')</small></div>'; |
| 136 |
} |
| 137 |
return ob_get_clean(); |
| 138 |
} |
| 139 |
|
| 140 |
//Display Active User in Admin Bar |
| 141 |
public function webizito_admin_bar_link() { |
| 142 |
global $wp_admin_bar; |
| 143 |
if ( !is_super_admin() || !is_admin_bar_showing() ) |
| 144 |
return; |
| 145 |
$wp_admin_bar->add_menu( array( |
| 146 |
'id' => 'webi_user_link', |
| 147 |
'title' => '<span class="ab-icon online-logged-in">●</span><span class="ab-label">' . __( 'Active Users (' . $this->webizito_online_users('count') . ')') .'</span>', |
| 148 |
'href' => esc_url( admin_url( 'users.php' ) ) |
| 149 |
) ); |
| 150 |
} |
| 151 |
|
| 152 |
//Get a count of online users, or an array of online user IDs |
| 153 |
public function webizito_online_users($return='count'){ |
| 154 |
$logged_in_users = get_transient('users_status'); |
| 155 |
|
| 156 |
//If no users are online |
| 157 |
if ( empty($logged_in_users) ){ |
| 158 |
return ( $return == 'count' )? 0 : false; |
| 159 |
} |
| 160 |
|
| 161 |
$user_online_count = 0; |
| 162 |
$online_users = array(); |
| 163 |
foreach ( $logged_in_users as $user ){ |
| 164 |
if ( !empty($user['username']) && isset($user['last']) && $user['last'] > time()-50 ){ |
| 165 |
$online_users[] = $user; |
| 166 |
$user_online_count++; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
return ( $return == 'count' )? $user_online_count : $online_users; |
| 171 |
|
| 172 |
} |
| 173 |
|
| 174 |
public function webizito_modify_user_view( $views ) { |
| 175 |
|
| 176 |
$logged_in_users = get_transient('users_status'); |
| 177 |
$user = wp_get_current_user(); |
| 178 |
|
| 179 |
$logged_in_users[$user->ID] = array( |
| 180 |
'id' => $user->ID, |
| 181 |
'username' => $user->user_login, |
| 182 |
'last' => time(), |
| 183 |
); |
| 184 |
|
| 185 |
$view = '<a href=' . admin_url('users.php') . '>User Online <span class="count">('.$this->webizito_online_users('count').')</span></a>'; |
| 186 |
|
| 187 |
$views['status'] = $view; |
| 188 |
return $views; |
| 189 |
} |
| 190 |
|
| 191 |
public function webizito_support_and_faq_links( $links_array, $plugin_file_name, $plugin_data, $status ) { |
| 192 |
if ( strpos( $plugin_file_name, basename(__FILE__) ) ) { |
| 193 |
|
| 194 |
// You can still use `array_unshift()` to add links at the beginning. |
| 195 |
$links_array[] = '<a href="https://wordpress.org/support/plugin/online-active-users/" target="_blank">Support</a>'; |
| 196 |
$links_array[] = '<a href="https://webizito.com/wp-online-active-users/" target="_blank">Docs</a>'; |
| 197 |
$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;">� |
| 198 |
� |
| 199 |
� |
| 200 |
� |
| 201 |
� |
| 202 |
</span></a></strong>'; |
| 203 |
} |
| 204 |
return $links_array; |
| 205 |
} |
| 206 |
|
| 207 |
public function webizito_plugin_by_link( $links ){ |
| 208 |
$url = 'https://webizito.com/'; |
| 209 |
$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>'; |
| 210 |
$_link = '<a href="'.$url.'" target="_blank">' . __( 'By <span>Webizito</span>', 'wp-online-active-users' ) . '</a>'; |
| 211 |
$links[] = $_link; |
| 212 |
return $links; |
| 213 |
} |
| 214 |
|
| 215 |
public function webizito_display_notice() { |
| 216 |
echo '<div class="notice notice-success is-dismissible wp-online-active-users-notice" id="wp-online-active-users-notice">'; |
| 217 |
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>'; |
| 218 |
echo '</div>'; |
| 219 |
} |
| 220 |
|
| 221 |
public function webizito_delete_transient() { |
| 222 |
delete_transient( 'users_status' ); |
| 223 |
} |
| 224 |
|
| 225 |
} |
| 226 |
} |
| 227 |
$myPlugin = new webi_active_user(); |