| 1 |
<?php |
| 2 |
// phpcs:ignoreFile WordPress.Files.FileName.InvalidClassFileName -- Main plugin bootstrap file; must keep the plugin-slug filename for WordPress to read its header. |
| 3 |
/** |
| 4 |
* Plugin Name: Online Active Users |
| 5 |
* Plugin Title: Online Active Users Plugin |
| 6 |
* Plugin URI: https://wordpress.org/plugins/online-active-users/ |
| 7 |
* Description: Monitor and display real-time online users and last seen status on your WordPress site with Online Active Users plugin. |
| 8 |
* Tags: online users, active users, user tracking, user status, user activity |
| 9 |
* Version: 3.4.4 |
| 10 |
* Requires at least: 6.3 |
| 11 |
* Requires PHP: 8.0 |
| 12 |
* Author: Webizito |
| 13 |
* Author URI: http://webizito.com/ |
| 14 |
* Contributors: valani9099 |
| 15 |
* License: GPLv3 or later |
| 16 |
* License URI: https://www.gnu.org/licenses/gpl-3.0.html |
| 17 |
* Text Domain: online-active-users |
| 18 |
* Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=APRNBJUZHRP7G |
| 19 |
* |
| 20 |
* @package Online_Active_Users |
| 21 |
*/ |
| 22 |
|
| 23 |
// Exit if accessed directly. |
| 24 |
if ( ! defined( 'ABSPATH' ) ) { |
| 25 |
exit; |
| 26 |
} |
| 27 |
|
| 28 |
if ( ! defined( 'WPOAU_PLUGIN_DIR' ) ) { |
| 29 |
define( 'WPOAU_PLUGIN_DIR', __DIR__ ); |
| 30 |
} |
| 31 |
|
| 32 |
if ( ! defined( 'WPOAU_PLUGIN_FILE' ) ) { |
| 33 |
define( 'WPOAU_PLUGIN_FILE', __FILE__ ); |
| 34 |
} |
| 35 |
|
| 36 |
if ( ! defined( 'WPOAU_VERSION' ) ) { |
| 37 |
define( 'WPOAU_VERSION', '3.4.3' ); |
| 38 |
} |
| 39 |
|
| 40 |
// class-wpoau-active-users.php replaces the old inc/webi-functions.php (removed): both defined the |
| 41 |
// same Wpoau_Active_Users class, and this one has the escaping/i18n/WPCS fixes applied. |
| 42 |
require_once WPOAU_PLUGIN_DIR . '/inc/class-wpoau-active-users.php'; |
| 43 |
require_once WPOAU_PLUGIN_DIR . '/class-webi-custom-widget.php'; |
| 44 |
|
| 45 |
if ( ! class_exists( 'Webi_Active_User' ) ) { |
| 46 |
|
| 47 |
/** |
| 48 |
* Main plugin bootstrap: wires up hooks for tracking and displaying online users. |
| 49 |
*/ |
| 50 |
class Webi_Active_User { |
| 51 |
|
| 52 |
/** |
| 53 |
* Active users tracking helper instance. |
| 54 |
* |
| 55 |
* @var Wpoau_Active_Users |
| 56 |
*/ |
| 57 |
public $wpoau; |
| 58 |
|
| 59 |
/** |
| 60 |
* Register all plugin hooks. |
| 61 |
*/ |
| 62 |
public function __construct() { |
| 63 |
|
| 64 |
$this->wpoau = new Wpoau_Active_Users( $this ); |
| 65 |
$GLOBALS['wpoau_users'] = $this->wpoau; |
| 66 |
|
| 67 |
register_activation_hook( WPOAU_PLUGIN_FILE, array( $this->wpoau, 'wpoau_users_status_init' ) ); |
| 68 |
add_action( 'init', array( $this->wpoau, 'wpoau_users_status_init' ) ); |
| 69 |
add_action( 'init', array( $this->wpoau, 'webi_track_user_activity' ) ); |
| 70 |
add_action( 'clear_auth_cookie', array( $this, 'wpoau_user_logout' ) ); |
| 71 |
add_action( 'wp_loaded', array( $this, 'wpoau_enqueue_script' ) ); |
| 72 |
add_action( 'admin_enqueue_scripts', array( $this, 'webi_enqueue_custom_scripts' ) ); |
| 73 |
add_action( 'admin_init', array( $this->wpoau, 'wpoau_users_status_init' ) ); |
| 74 |
add_action( 'wp_dashboard_setup', array( $this->wpoau, 'wpoau_active_users_metabox' ) ); |
| 75 |
add_filter( 'manage_users_columns', array( $this->wpoau, 'wpoau_user_columns_head' ) ); |
| 76 |
add_filter( 'manage_users_custom_column', array( $this->wpoau, 'wpoau_user_columns_content' ), 10, 3 ); |
| 77 |
add_filter( 'views_users', array( $this, 'wpoau_modify_user_view' ) ); |
| 78 |
add_action( 'admin_bar_menu', array( $this->wpoau, 'wpoau_admin_bar_link' ), 999 ); |
| 79 |
add_filter( 'plugin_row_meta', array( $this, 'wpoau_support_and_faq_links' ), 10, 4 ); |
| 80 |
add_action( 'admin_menu', array( $this, 'wpoau_add_admin_submenu' ) ); |
| 81 |
add_filter( 'plugin_action_links_' . plugin_basename( WPOAU_PLUGIN_FILE ), array( $this, 'wpoau_plugin_by_link' ), 10, 1 ); |
| 82 |
add_action( 'admin_notices', array( $this, 'wpoau_display_notice' ) ); |
| 83 |
register_deactivation_hook( WPOAU_PLUGIN_FILE, array( $this, 'wpoau_display_notice' ) ); |
| 84 |
register_deactivation_hook( WPOAU_PLUGIN_FILE, array( $this, 'wpoau_delete_transient' ) ); |
| 85 |
|
| 86 |
$this->wpoau->wpoau_active_user_shortcode(); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Enqueue the front-end plugin stylesheet. |
| 91 |
*/ |
| 92 |
public function wpoau_enqueue_script() { |
| 93 |
wp_enqueue_style( 'style-css', plugin_dir_url( WPOAU_PLUGIN_FILE ) . 'assets/css/style.css', array(), WPOAU_VERSION ); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Enqueue admin scripts and localize the last-seen timestamp formatting. |
| 98 |
*/ |
| 99 |
public function webi_enqueue_custom_scripts() { |
| 100 |
wp_enqueue_script( 'webi-plugin-script', plugin_dir_url( WPOAU_PLUGIN_FILE ) . 'assets/js/custom.js', array( 'jquery' ), WPOAU_VERSION, true ); |
| 101 |
|
| 102 |
wp_add_inline_script( |
| 103 |
'webi-plugin-script', |
| 104 |
" |
| 105 |
jQuery(document).ready(function($) { |
| 106 |
$('.webizito-last-seen').each(function() { |
| 107 |
var timestamp = $(this).data('timestamp'); |
| 108 |
if (timestamp) { |
| 109 |
var date = new Date(timestamp * 1000); |
| 110 |
$(this).text(date.toLocaleString()); |
| 111 |
} |
| 112 |
}); |
| 113 |
}); |
| 114 |
" |
| 115 |
); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Remove a user from the online list when they log out. |
| 120 |
*/ |
| 121 |
public function wpoau_user_logout() { |
| 122 |
if ( ! is_user_logged_in() ) { |
| 123 |
return; // Prevent errors if already logged out. |
| 124 |
} |
| 125 |
|
| 126 |
$user_id = get_current_user_id(); |
| 127 |
if ( ! $user_id ) { |
| 128 |
return; |
| 129 |
} |
| 130 |
|
| 131 |
$logged_in_users = get_transient( 'users_status' ); |
| 132 |
|
| 133 |
if ( isset( $logged_in_users[ $user_id ] ) ) { |
| 134 |
unset( $logged_in_users[ $user_id ] ); |
| 135 |
set_transient( 'users_status', $logged_in_users, 60 ); |
| 136 |
} |
| 137 |
|
| 138 |
update_user_meta( $user_id, 'last_seen', time() ); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Get a count of online users, or an array of online user records. |
| 143 |
* |
| 144 |
* @param string $type Either 'count' for the online user count, or anything else for the full array. |
| 145 |
* @return int|array |
| 146 |
*/ |
| 147 |
public function wpoau_online_users( $type = 'count' ) { |
| 148 |
$logged_in_users = get_transient( 'users_status' ); |
| 149 |
|
| 150 |
// If no users are online. |
| 151 |
if ( empty( $logged_in_users ) ) { |
| 152 |
return ( 'count' === $type ) ? 0 : false; |
| 153 |
} |
| 154 |
|
| 155 |
$user_online_count = 0; |
| 156 |
$online_users = array(); |
| 157 |
foreach ( $logged_in_users as $user ) { |
| 158 |
if ( ! empty( $user['username'] ) && isset( $user['last'] ) && $user['last'] > time() - 50 ) { |
| 159 |
$online_users[] = $user; |
| 160 |
++$user_online_count; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
return ( 'count' === $type ) ? $user_online_count : $online_users; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Register the "Online Active Users" submenu under Users. |
| 169 |
*/ |
| 170 |
public function wpoau_add_admin_submenu() { |
| 171 |
add_users_page( |
| 172 |
__( 'Online Active Users', 'online-active-users' ), |
| 173 |
__( 'Online Active Users', 'online-active-users' ), |
| 174 |
'list_users', |
| 175 |
'wpoau-active-users', |
| 176 |
array( $this, 'wpoau_active_users_page' ) |
| 177 |
); |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Render the "Online Active Users" admin submenu page. |
| 182 |
*/ |
| 183 |
public function wpoau_active_users_page() { |
| 184 |
echo '<div class="wrap"><h1>' . esc_html__( 'Online Users Lists', 'online-active-users' ) . '</h1>'; |
| 185 |
|
| 186 |
$users = $this->wpoau_online_users( 'array' ); |
| 187 |
|
| 188 |
if ( empty( $users ) ) { |
| 189 |
echo '<p>' . esc_html__( 'No users are online right now.', 'online-active-users' ) . '</p></div>'; |
| 190 |
return; |
| 191 |
} |
| 192 |
|
| 193 |
require_once WPOAU_PLUGIN_DIR . '/inc/user-list-table.php'; |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Add an "Online" view/count to the Users list table view links. |
| 198 |
* |
| 199 |
* @param array $views Existing view links. |
| 200 |
* @return array |
| 201 |
*/ |
| 202 |
public function wpoau_modify_user_view( $views ) { |
| 203 |
|
| 204 |
$logged_in_users = get_transient( 'users_status' ); |
| 205 |
$user = wp_get_current_user(); |
| 206 |
|
| 207 |
$logged_in_users[ $user->ID ] = array( |
| 208 |
'id' => $user->ID, |
| 209 |
'username' => $user->user_login, |
| 210 |
'last' => time(), |
| 211 |
); |
| 212 |
|
| 213 |
$view = '<a href="' . esc_url( admin_url( 'users.php?page=wpoau-active-users' ) ) . '">' . sprintf( |
| 214 |
/* translators: %s: HTML span showing the count of currently online users, e.g. "(3)". */ |
| 215 |
esc_html__( 'User Online %s', 'online-active-users' ), |
| 216 |
'<span class="count">(' . absint( $this->wpoau_online_users( 'count' ) ) . ')</span>' |
| 217 |
) . '</a>'; |
| 218 |
|
| 219 |
$views['status'] = $view; |
| 220 |
return $views; |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Add Support and Docs links to the plugin's row meta on the Plugins page. |
| 225 |
* |
| 226 |
* @param array $links_array Existing plugin row meta links. |
| 227 |
* @param string $plugin_file_name Path to the plugin file relative to the plugins directory. |
| 228 |
* @param array $plugin_data Unused. Required by the plugin_row_meta filter signature. |
| 229 |
* @param string $status Unused. Required by the plugin_row_meta filter signature. |
| 230 |
* @return array |
| 231 |
*/ |
| 232 |
public function wpoau_support_and_faq_links( $links_array, $plugin_file_name, $plugin_data, $status ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed |
| 233 |
if ( strpos( $plugin_file_name, basename( WPOAU_PLUGIN_FILE ) ) ) { |
| 234 |
|
| 235 |
// You can still use `array_unshift()` to add links at the beginning. |
| 236 |
$links_array[] = '<a href="' . esc_url( 'https://wordpress.org/support/plugin/online-active-users/' ) . '" target="_blank">' . esc_html__( 'Support', 'online-active-users' ) . '</a>'; |
| 237 |
$links_array[] = '<a href="' . esc_url( 'https://webizito.com/wp-online-active-users/' ) . '" target="_blank">' . esc_html__( 'Docs', 'online-active-users' ) . '</a>'; |
| 238 |
$links_array[] = '<strong><a href="' . esc_url( 'https://wordpress.org/support/plugin/online-active-users/reviews/?rate=5#new-post' ) . '" target="_blank">' . esc_html__( 'Rate our plugin', 'online-active-users' ) . ' <span style="color:#ffb900;font-size: 18px;position:relative;top:0.1em;">� |
| 239 |
� |
| 240 |
� |
| 241 |
� |
| 242 |
� |
| 243 |
</span></a></strong>'; |
| 244 |
} |
| 245 |
return $links_array; |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Add "Donate" and "By Webizito" links to the plugin's action links. |
| 250 |
* |
| 251 |
* @param array $links Existing plugin action links. |
| 252 |
* @return array |
| 253 |
*/ |
| 254 |
public function wpoau_plugin_by_link( $links ) { |
| 255 |
$links[] = '<a href="' . esc_url( 'https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=APRNBJUZHRP7G' ) . '" target="_blank"><span>' . esc_html__( 'Donate', 'online-active-users' ) . '</span></a>'; |
| 256 |
return $links; |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Display a review/donation admin notice. |
| 261 |
*/ |
| 262 |
public function wpoau_display_notice() { |
| 263 |
$message = sprintf( |
| 264 |
/* translators: 1: opening review link tag, 2: closing link tag, 3: opening donation link tag. */ |
| 265 |
esc_html__( 'Enjoying our WP Online Active Users plugin? Please consider leaving us a review %1$shere%2$s. Or support with a small donation %3$shere%2$s. We would greatly appreciate it!', 'online-active-users' ), |
| 266 |
'<a href="' . esc_url( 'https://wordpress.org/support/plugin/online-active-users/reviews/?rate=5#new-post' ) . '" target="_blank">', |
| 267 |
'</a>', |
| 268 |
'<a href="' . esc_url( 'https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=APRNBJUZHRP7G' ) . '" target="_blank">' |
| 269 |
); |
| 270 |
|
| 271 |
echo '<div class="notice notice-success is-dismissible wp-online-active-users-notice" id="wp-online-active-users-notice">'; |
| 272 |
echo '<p>' . wp_kses_post( $message ) . '</p>'; |
| 273 |
echo '</div>'; |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* Delete the plugin's online-users transient on deactivation. |
| 278 |
*/ |
| 279 |
public function wpoau_delete_transient() { |
| 280 |
delete_transient( 'users_status' ); |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
$wpoau_plugin = new Webi_Active_User(); |
| 286 |
|
| 287 |
// Register the custom widget. |
| 288 |
add_action( |
| 289 |
'widgets_init', |
| 290 |
function () { |
| 291 |
register_widget( 'Webi_Custom_Widget' ); |
| 292 |
} |
| 293 |
); |
| 294 |
|