| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPAdminify\Inc\DashboardWidgets; |
| 4 |
// no direct access allowed |
| 5 |
if (!defined('ABSPATH')) exit; |
| 6 |
|
| 7 |
/** |
| 8 |
* User Activities Dashboard Widget |
| 9 |
* |
| 10 |
* @return void |
| 11 |
*/ |
| 12 |
/** |
| 13 |
* WPAdminify |
| 14 |
* |
| 15 |
* @author Jewel Theme <support@jeweltheme.com> |
| 16 |
*/ |
| 17 |
|
| 18 |
class Adminify_UserActivities |
| 19 |
{ |
| 20 |
public function __construct() |
| 21 |
{ |
| 22 |
add_action('wp_dashboard_setup', [$this, 'jltwp_adminify_user_activities']); |
| 23 |
add_action('auth_cookie_expired', [$this, 'jltwp_adminify_auth_cookie_expired'], 10, 1); |
| 24 |
add_action('admin_init', [$this, 'jltwp_adminify_first_login_status'], 1); |
| 25 |
add_action('wp_login', [$this, 'jltwp_adminify_last_user_login'], 10, 2); |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
/** |
| 30 |
* Label: User Activities |
| 31 |
* |
| 32 |
* @return void |
| 33 |
*/ |
| 34 |
public function jltwp_adminify_user_activities() |
| 35 |
{ |
| 36 |
wp_add_dashboard_widget( |
| 37 |
'jltwp_adminify_dash_user_activities', |
| 38 |
esc_html__('User Activities - Adminify', WP_ADMINIFY_TD), |
| 39 |
[$this, 'jltwp_adminify_user_activities_details'] |
| 40 |
); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Cookie Expired |
| 45 |
*/ |
| 46 |
public function jltwp_adminify_auth_cookie_expired($user) |
| 47 |
{ |
| 48 |
|
| 49 |
$user = get_user_by('login', $user['username']); |
| 50 |
|
| 51 |
if ($user) { |
| 52 |
|
| 53 |
// Update user last logout date/time |
| 54 |
update_user_meta($user->ID, '_last_logout', $this->jltwp_adminify_set_date_time()); |
| 55 |
|
| 56 |
// Update user login status to logged out |
| 57 |
update_user_meta($user->ID, '_logged_in', 0); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Set Date Time |
| 63 |
*/ |
| 64 |
public function jltwp_adminify_set_date_time() |
| 65 |
{ |
| 66 |
|
| 67 |
// Update user last login date/time |
| 68 |
$timezone_string = get_option('timezone_string'); |
| 69 |
if (!empty($timezone_string)) { |
| 70 |
date_default_timezone_set($timezone_string); // --> Set default timezone by wp settings |
| 71 |
} |
| 72 |
|
| 73 |
// Get date / time in wp format |
| 74 |
$date = date_i18n(get_option('date_format'), strtotime(date('Y-m-d', current_time('timestamp', 1)))); |
| 75 |
$time = date_i18n(get_option('time_format'), strtotime(date('H:i:s', current_time('timestamp', 1)))); |
| 76 |
|
| 77 |
return $date . ' ' . $time; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* First login status |
| 82 |
* |
| 83 |
* @return void |
| 84 |
*/ |
| 85 |
public function jltwp_adminify_first_login_status() |
| 86 |
{ |
| 87 |
// Get current logged in user ID |
| 88 |
$current_user = wp_get_current_user(); |
| 89 |
|
| 90 |
// Check if user login status is false |
| 91 |
if (get_user_meta($current_user->ID, '_logged_in', true)) { |
| 92 |
// update user login status to logged in |
| 93 |
update_user_meta($current_user->ID, '_logged_in', 1); |
| 94 |
} |
| 95 |
|
| 96 |
// Check if user last login is empty |
| 97 |
if (empty(get_user_meta($current_user->ID, '_last_login', true))) { |
| 98 |
// Update user last login date/time |
| 99 |
update_user_meta($current_user->ID, '_last_login', $this->jltwp_adminify_set_date_time()); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Last Login Status |
| 105 |
*/ |
| 106 |
public function jltwp_adminify_last_user_login($user) |
| 107 |
{ |
| 108 |
// Get current logged in user ID |
| 109 |
$current_user = get_user_by('login', $user); |
| 110 |
|
| 111 |
update_user_meta($current_user->ID, '_last_login', $this->jltwp_adminify_set_date_time()); |
| 112 |
|
| 113 |
// Update user login status to logged in |
| 114 |
update_user_meta($current_user->ID, '_logged_in', 1); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Last Login Status |
| 119 |
*/ |
| 120 |
public function jltwp_adminify_current_logged_in_user($current_user) |
| 121 |
{ |
| 122 |
// Get current logged in user ID |
| 123 |
$current_user = wp_get_current_user(); |
| 124 |
|
| 125 |
// Update user last logout date/time |
| 126 |
update_user_meta($current_user->ID, '_last_logout', $this->jltwp_adminify_set_date_time()); |
| 127 |
|
| 128 |
// Update user login status to logged out |
| 129 |
update_user_meta($current_user->ID, '_logged_in', 0); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* User Activity lists |
| 134 |
* |
| 135 |
* @return void |
| 136 |
*/ |
| 137 |
public function jltwp_adminify_get_logged_in_users() |
| 138 |
{ |
| 139 |
|
| 140 |
$user_list = array(); |
| 141 |
|
| 142 |
$blog_id = '1'; |
| 143 |
|
| 144 |
// Check for multisite |
| 145 |
if (is_multisite()) { |
| 146 |
$blog_id = get_current_blog_id(); |
| 147 |
$options = get_blog_option($blog_id, 'wp_admin_theme_settings_options'); |
| 148 |
} |
| 149 |
|
| 150 |
// Build the user count query |
| 151 |
$user_count_args = array( |
| 152 |
'blog_id' => $blog_id, |
| 153 |
'number' => 999999, |
| 154 |
); |
| 155 |
|
| 156 |
$user_count_query = new \WP_User_Query($user_count_args); |
| 157 |
$user_count = $user_count_query->get_results(); |
| 158 |
|
| 159 |
// Count the number of users found in the query |
| 160 |
$total_users = isset($user_count) ? count($user_count) : 1; |
| 161 |
|
| 162 |
// Grab the current page number and set to 1 if no page number is set |
| 163 |
$page = isset($_GET['p']) ? $_GET['p'] : 1; |
| 164 |
|
| 165 |
// Limit users to show per page |
| 166 |
$users_per_page = 5; |
| 167 |
|
| 168 |
// Calculate the total number of pages |
| 169 |
$total_pages = 1; |
| 170 |
$offset = $users_per_page * ($page - 1); |
| 171 |
$total_pages = ceil($total_users / $users_per_page); |
| 172 |
|
| 173 |
// Build the user query |
| 174 |
$args = array( |
| 175 |
'blog_id' => $blog_id, |
| 176 |
'orderby' => 'login', |
| 177 |
'order' => 'ASC', |
| 178 |
'number' => $users_per_page, |
| 179 |
'offset' => $offset, |
| 180 |
); |
| 181 |
|
| 182 |
// Create the WP_User_Query object |
| 183 |
$wp_user_query = new \WP_User_Query($args); |
| 184 |
|
| 185 |
// Get the user results |
| 186 |
$users = $wp_user_query->get_results(); |
| 187 |
|
| 188 |
foreach ($users as $current_user) { |
| 189 |
|
| 190 |
// check last login date/time is false |
| 191 |
$getLastLogin = get_user_meta($current_user->ID, '_last_login', true); |
| 192 |
|
| 193 |
$last_user_login = $getLastLogin; |
| 194 |
if (empty($getLastLogin)) { |
| 195 |
$last_user_login = esc_html__('N/A', WP_ADMINIFY_TD); |
| 196 |
} |
| 197 |
|
| 198 |
// check last logout date/time is false |
| 199 |
$getLastLogout = get_user_meta($current_user->ID, '_last_logout', true); |
| 200 |
|
| 201 |
$last_user_logout = $getLastLogout; |
| 202 |
if (empty($getLastLogout)) { |
| 203 |
$last_user_logout = esc_html__('N/A', WP_ADMINIFY_TD); |
| 204 |
} |
| 205 |
|
| 206 |
// check if user login status is false |
| 207 |
$is_logged_in = '0'; |
| 208 |
if (get_user_meta($current_user->ID, '_logged_in', true)) { |
| 209 |
// check user is logged in |
| 210 |
$get_user_logged_in_status = get_user_meta($current_user->ID); |
| 211 |
$is_logged_in = $get_user_logged_in_status['_logged_in'][0]; |
| 212 |
} |
| 213 |
|
| 214 |
// get logged in status |
| 215 |
if ($is_logged_in != '1') { |
| 216 |
$login_status = '<span class="user-status-text logged-out">' . esc_html__('logged out', WP_ADMINIFY_TD) . '</span>'; |
| 217 |
if (is_rtl()) { |
| 218 |
$login_status = '<span class="user-status-text logged-out">' . esc_html__('logged out', WP_ADMINIFY_TD) . '</span>' . esc_html__('is', WP_ADMINIFY_TD); |
| 219 |
} |
| 220 |
} else { |
| 221 |
$login_status = '<span class="user-status-text logged-in">' . esc_html__('logged in', WP_ADMINIFY_TD) . '</span>'; |
| 222 |
if (is_rtl()) { |
| 223 |
$login_status = '<span class="user-status-text logged-in">' . esc_html__('logged in', WP_ADMINIFY_TD) . '</span>' . esc_html__('is', WP_ADMINIFY_TD); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
// Logged In/Logout Status Symbol |
| 229 |
if ($is_logged_in) { |
| 230 |
$status_class = 'logged-in'; |
| 231 |
} else { |
| 232 |
$status_class = 'logged-out'; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
// show logged in status |
| 237 |
if (is_rtl()) { |
| 238 |
$user_list['users'][] = '<tr> |
| 239 |
<td> |
| 240 |
<div class="media"> |
| 241 |
<div class="wp-adminify-user-avatar media-left is-pulled-left image"> |
| 242 |
' . get_avatar($current_user->user_email, 36, '', '', array('class' => 'is-rounded')) . ' |
| 243 |
<div class="user-status ' . $status_class . '"></div> |
| 244 |
</div> |
| 245 |
|
| 246 |
<div class="media-content"> |
| 247 |
<h5 class="wp-adminify-user-name">' . esc_html($current_user->display_name) . '</h5> |
| 248 |
' . $login_status . ' |
| 249 |
</div> |
| 250 |
</div> |
| 251 |
</td> |
| 252 |
|
| 253 |
<td> |
| 254 |
<div class="wp-adminify-user-time-tracker"> |
| 255 |
<time datetime="' . $last_user_login . '"><span>' . $last_user_login . '</span></time> |
| 256 |
</div> |
| 257 |
</td> |
| 258 |
<td> |
| 259 |
<div class="wp-adminify-user-time-tracker"> |
| 260 |
<time datetime="' . $last_user_logout . '"><span>' . $last_user_logout . '</span></time> |
| 261 |
</div> |
| 262 |
</td> |
| 263 |
</tr>'; |
| 264 |
} else { |
| 265 |
$user_list['users'][] = '<tr> |
| 266 |
<td> |
| 267 |
<div class="media"> |
| 268 |
<div class="wp-adminify-user-avatar media-left is-pulled-left image"> |
| 269 |
' . get_avatar($current_user->user_email, 36, '', '', array('class' => 'is-rounded')) . ' |
| 270 |
<div class="user-status ' . $status_class . '"></div> |
| 271 |
</div> |
| 272 |
|
| 273 |
<div class="media-content"> |
| 274 |
<h5 class="wp-adminify-user-name">' . esc_html($current_user->display_name) . '</h5> |
| 275 |
' . $login_status . ' |
| 276 |
</div> |
| 277 |
</div> |
| 278 |
</td> |
| 279 |
|
| 280 |
<td> |
| 281 |
<div class="wp-adminify-user-time-tracker"> |
| 282 |
<time datetime="' . $last_user_login . '"><span class="is-pulled-left">' . $last_user_login . '</span></time> |
| 283 |
</div> |
| 284 |
</td> |
| 285 |
<td> |
| 286 |
<div class="wp-adminify-user-time-tracker"> |
| 287 |
<time datetime="' . $last_user_logout . '"><span class="is-pulled-left">' . $last_user_logout . '</span></time> |
| 288 |
</div> |
| 289 |
</td> |
| 290 |
</tr>'; |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
// Grab the current query parameters |
| 295 |
$query_string = $_SERVER['QUERY_STRING']; |
| 296 |
|
| 297 |
// If in the admin, your base should be the admin URL + your page |
| 298 |
$base = admin_url('index.php') . '?' . remove_query_arg('p', $query_string) . '%_%'; |
| 299 |
|
| 300 |
$user_list['pagination'] = paginate_links(array( |
| 301 |
'base' => $base, // the base URL, including query arg |
| 302 |
'format' => '&p=%#%', // this defines the query parameter that will be used, in this case "p" |
| 303 |
'total' => $total_pages, // the total number of pages we have |
| 304 |
'current' => $page, // the current page |
| 305 |
'prev_text' => '‹', |
| 306 |
'next_text' => '›', |
| 307 |
'show_all' => false, |
| 308 |
'before_page_number' => '', |
| 309 |
'after_page_number' => '', |
| 310 |
)); |
| 311 |
|
| 312 |
return $user_list; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
/** |
| 317 |
* Dashboard Widgets: Logged in Users |
| 318 |
* |
| 319 |
* @return void |
| 320 |
*/ |
| 321 |
public function jltwp_adminify_user_activities_details() |
| 322 |
{ ?> |
| 323 |
|
| 324 |
<div class="wp-adminify-dash-users-activity"> |
| 325 |
<table class="wp-adminify-dash-user-activity-table"> |
| 326 |
<tr> |
| 327 |
<th><?php echo esc_html__('User', WP_ADMINIFY_TD); ?></th> |
| 328 |
<th><?php echo esc_html__('Last Login', WP_ADMINIFY_TD); ?></th> |
| 329 |
<th><?php echo esc_html__('Last Logout', WP_ADMINIFY_TD); ?></th> |
| 330 |
</tr> |
| 331 |
<?php |
| 332 |
$users = isset($this->jltwp_adminify_get_logged_in_users()['users']) ? $this->jltwp_adminify_get_logged_in_users()['users'] : false; |
| 333 |
foreach ($users as $current_user) { |
| 334 |
echo $current_user; |
| 335 |
} |
| 336 |
?> |
| 337 |
|
| 338 |
|
| 339 |
<?php |
| 340 |
$pagination = isset($this->jltwp_adminify_get_logged_in_users()['pagination']) ? $this->jltwp_adminify_get_logged_in_users()['pagination'] : false; |
| 341 |
if ($pagination) { ?> |
| 342 |
<div class="user-pagination"> |
| 343 |
<?php echo $pagination; ?> |
| 344 |
</div> |
| 345 |
<?php } |
| 346 |
?> |
| 347 |
|
| 348 |
</table> |
| 349 |
</div> |
| 350 |
<?php |
| 351 |
|
| 352 |
} |
| 353 |
} |
| 354 |
|