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