| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: When Last Login |
| 4 |
Plugin URI: https://whenlastlogin.com |
| 5 |
Description: See when a user logs into your WordPress site. |
| 6 |
Version: 1.2.3 |
| 7 |
Author: Yoohoo Plugins |
| 8 |
Author URI: https://yoohooplugins.com |
| 9 |
Text Domain: when-last-login |
| 10 |
Domain Path: /languages |
| 11 |
*/ |
| 12 |
|
| 13 |
use geertw\IpAnonymizer\IpAnonymizer; |
| 14 |
|
| 15 |
define( 'WLL_VER', '1.2.3' ); |
| 16 |
|
| 17 |
class When_Last_Login { |
| 18 |
|
| 19 |
/** Refers to a single instance of this class. */ |
| 20 |
private static $instance = null; |
| 21 |
|
| 22 |
/** |
| 23 |
* Initializes the plugin by setting localization, filters, and administration functions. |
| 24 |
*/ |
| 25 |
private function __construct() { |
| 26 |
|
| 27 |
define( 'WLL_BASENAME', plugin_basename( __FILE__ ) ); |
| 28 |
define( 'WLL_DIR_PATH', plugin_dir_path( __FILE__ ) ); |
| 29 |
define( 'WLL_PLUGIN', WP_PLUGIN_URL . '/when-last-login' ); |
| 30 |
|
| 31 |
$settings = get_option( 'wll_settings' ); |
| 32 |
|
| 33 |
include WLL_DIR_PATH . '/includes/lib/IpAnonymizer.php'; |
| 34 |
include WLL_DIR_PATH . '/includes/privacy-policy.php'; |
| 35 |
|
| 36 |
add_action( 'admin_init', array( $this, 'admin_init' ) ); |
| 37 |
add_action( 'plugins_loaded', array( $this, 'text_domain' ) ); |
| 38 |
add_action( 'admin_enqueue_scripts', array( $this, 'load_js_for_notice' ) ); |
| 39 |
|
| 40 |
//Create the custom meta upon login |
| 41 |
add_action( 'wp_login', array( $this, 'last_login'), 10, 2 ); |
| 42 |
add_action( 'user_register', array( $this, 'wll_user_register' ), 10, 1 ); |
| 43 |
add_action( 'two_factor_user_authenticated', array( $this, 'two_factor_user_authenticated' ), 10, 2 ); |
| 44 |
|
| 45 |
//Admin actions |
| 46 |
add_action( 'wp_dashboard_setup', array( $this, 'admin_dashboard_widget' ) ); |
| 47 |
add_action( 'admin_notices', array( $this, 'update_notice' ) ); |
| 48 |
|
| 49 |
add_action( 'wp_ajax_wll_hide_subscription_notice', array( $this, 'wll_hide_subscription_notice' ) ); |
| 50 |
|
| 51 |
//Setting up columns. |
| 52 |
add_filter( 'manage_users_columns', array( $this, 'column_header'), 10, 1 ); |
| 53 |
add_action( 'manage_users_custom_column', array( $this, 'column_data'), 15, 3 ); |
| 54 |
add_filter( 'manage_users_sortable_columns', array( $this, 'column_sortable' ) ); |
| 55 |
add_action( 'pre_get_users', array( $this, 'sort_by_login_date') ); |
| 56 |
|
| 57 |
//Integration for Paid Memberships Pro |
| 58 |
//TODO: Improve integration with Member List and Paid Memberships Pro |
| 59 |
add_action( 'pmpro_memberslist_extra_cols_header', array( $this, 'pmpro_memberlist_add_header' ) ); |
| 60 |
add_action( 'pmpro_memberslist_extra_cols_body', array( $this, 'pmpro_memberlist_add_column_data' ) ); |
| 61 |
add_action( 'init', array( $this, 'login_record_cp' ) ); |
| 62 |
|
| 63 |
add_action( 'admin_menu', array( $this, 'wll_settings_page' ), 9 ); |
| 64 |
add_action( 'admin_head', array( $this, 'wll_settings_page_head' ) ); |
| 65 |
add_action( 'admin_init', array( $this, 'wll_automatically_remove_logs' ) ); |
| 66 |
|
| 67 |
add_filter( 'plugin_row_meta', array( $this, 'wll_plugin_row_meta' ), 10, 2 ); |
| 68 |
add_filter( 'plugin_action_links_' . WLL_BASENAME, array( $this, 'wll_plugin_action_links' ), 10, 2 ); |
| 69 |
|
| 70 |
add_filter( 'manage_wll_records_posts_columns' , array( $this, 'wll_records_columns'), 10, 1 ); |
| 71 |
add_action( 'manage_wll_records_posts_custom_column' , array( $this, 'wll_records_column_contents' ), 10, 2 ); |
| 72 |
|
| 73 |
/** |
| 74 |
* Multisite support |
| 75 |
*/ |
| 76 |
add_action( 'wp_network_dashboard_setup', array( $this, 'admin_dashboard_widget' ) ); |
| 77 |
add_filter( 'wpmu_users_columns', array( $this, 'column_header'), 10, 1 ); |
| 78 |
add_action( 'wpmu_users_custom_column', array( $this, 'column_data'), 15, 3 ); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Creates or returns an instance of this class. |
| 83 |
* |
| 84 |
* @return When_Last_Login A single instance of this class. |
| 85 |
*/ |
| 86 |
public static function get_instance() { |
| 87 |
if ( null == self::$instance ) { |
| 88 |
self::$instance = new self; |
| 89 |
} |
| 90 |
return self::$instance; |
| 91 |
} // end get_instance; |
| 92 |
|
| 93 |
/** |
| 94 |
* When Last plugin functions. |
| 95 |
*/ |
| 96 |
public static function admin_init(){ |
| 97 |
//init function |
| 98 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
do_action( 'wll_upgrade_check' ); |
| 103 |
|
| 104 |
$current_version = floatval( get_option( 'wll_current_version' ) ); |
| 105 |
|
| 106 |
// Clean up stuff for version 1.0 |
| 107 |
if( $current_version < 1.0 || empty( $current_version ) ) { |
| 108 |
|
| 109 |
global $wpdb; |
| 110 |
|
| 111 |
$delete_table = $wpdb->prefix . 'wll_login_attempts' ; |
| 112 |
$sql = "DROP TABLE IF EXISTS `$delete_table`"; |
| 113 |
$wpdb->query( $sql ); |
| 114 |
|
| 115 |
delete_transient( 'when_last_login_add_ons_page' ); |
| 116 |
|
| 117 |
// on upgrade remove the notice save. |
| 118 |
delete_option( 'wll_notice_hide' ); |
| 119 |
delete_option( 'wll_notice_hide_1' ); |
| 120 |
delete_option( 'wll_notice_hide_2' ); |
| 121 |
|
| 122 |
// update version number to 1.0 |
| 123 |
update_option( 'wll_current_version', 1.2 ); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
public static function text_domain(){ |
| 128 |
load_plugin_textdomain( 'when-last-login', false, dirname( 'WLL_BASE_NAME' ) . '/languages' ); |
| 129 |
} |
| 130 |
|
| 131 |
public static function update_notice(){ |
| 132 |
|
| 133 |
if( get_option( 'wll_notice_hide' ) != '1' && ( isset( $_REQUEST['page'] ) && $_REQUEST['page'] == 'when-last-login-settings' ) ){ |
| 134 |
?> |
| 135 |
<div class="notice notice-success wll-update-notice-newsletter is-dismissible" > |
| 136 |
<h3><?php _e('Thank you for using When Last Login', 'when-last-login'); ?></h3> |
| 137 |
<p><?php _e( sprintf( 'Please consider leaving an honest review for When Last Login by visiting %s', '<a href="'. esc_url( 'https://wordpress.org/support/plugin/when-last-login/reviews/#new-post' ) . '" target="_blank">this link</a>' ), 'when-last-login' ); ?></p> |
| 138 |
</div> |
| 139 |
<?php |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
public function wll_hide_subscription_notice(){ |
| 144 |
if ( ! wp_verify_nonce( $_REQUEST['nonce'], 'wll_hide_notice_nonce' ) ) { |
| 145 |
wp_die( __( 'Nonce is invalid', 'pmpro-pdf-invoices' ) ); |
| 146 |
} |
| 147 |
update_option( 'wll_notice_hide', '1' ); |
| 148 |
} |
| 149 |
|
| 150 |
public static function load_js_for_notice(){ |
| 151 |
if( get_option( 'wll_notice_hide' ) !== '1'){ |
| 152 |
wp_enqueue_script( 'wll_notice_update', plugins_url( 'js/notice-update.js', __FILE__ ), array( 'jquery' ), '1.0', false ); |
| 153 |
|
| 154 |
wp_localize_script( 'wll_notice_update', 'wll_notice_update', array( |
| 155 |
'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 156 |
'nonce' => wp_create_nonce( 'wll_hide_notice_nonce' ) |
| 157 |
) ); |
| 158 |
} |
| 159 |
if( isset( $_GET['page'] ) && $_GET['page'] == 'when-last-login-settings' ){ |
| 160 |
wp_enqueue_style( 'wll_admin_settings_styles', plugins_url( '/css/admin.css', __FILE__ ) ); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Track the login process for the Two Factor Authentication plugin. |
| 166 |
* |
| 167 |
* @since TBD |
| 168 |
* |
| 169 |
* @param WP_User $user |
| 170 |
* @param Two_Factor $two_factor (this is unused.) |
| 171 |
*/ |
| 172 |
public static function two_factor_user_authenticated( $user, $two_factor ) { |
| 173 |
// Let's call last_login function to record the login. |
| 174 |
When_Last_Login::last_login( $user->user_login, $user ); |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Track the user's login timestamp and "All Time Record". |
| 179 |
* |
| 180 |
* @param string $user_login The username that is logging in. |
| 181 |
* @param WP_User $user The WordPress user object. |
| 182 |
* @return void |
| 183 |
*/ |
| 184 |
public static function last_login( $user_login, $user ) { |
| 185 |
|
| 186 |
global $show_login_records; |
| 187 |
|
| 188 |
$record_login = apply_filters( 'wll_record_login', true, $user, $user_login ); |
| 189 |
|
| 190 |
// If filter isn't true, don't record login at all! |
| 191 |
if ( ! $record_login ) { |
| 192 |
return; |
| 193 |
} |
| 194 |
|
| 195 |
//get/update user meta 'when_last_login' on login and add time() to it. |
| 196 |
update_user_meta( $user->ID, 'when_last_login', time() ); |
| 197 |
|
| 198 |
//get and update user meta 'when_last_login_count' on login for # of login counts. Thanks to Jarryd Long (@jarrydlong) for the assistance |
| 199 |
$wll_count = get_user_meta( $user->ID, 'when_last_login_count', true ); |
| 200 |
|
| 201 |
if( $wll_count === false ){ |
| 202 |
update_user_meta($user->ID, 'when_last_login_count', 1); |
| 203 |
} else { |
| 204 |
$wll_new_value = intval($wll_count); |
| 205 |
$wll_new_value = $wll_new_value + 1; |
| 206 |
|
| 207 |
update_user_meta($user->ID, 'when_last_login_count', $wll_new_value); |
| 208 |
} |
| 209 |
|
| 210 |
if( $show_login_records == true ){ |
| 211 |
$args = array( |
| 212 |
'post_title' => $user->data->display_name . __( ' has logged in at ', 'when-last-login' ) . date( 'Y-m-d H:i:s', current_time( 'timestamp' ) ), |
| 213 |
'post_status' => 'publish', |
| 214 |
'post_author' => $user->ID, |
| 215 |
'post_type' => 'wll_records' |
| 216 |
); |
| 217 |
|
| 218 |
$post_id = wp_insert_post( $args ); |
| 219 |
|
| 220 |
} |
| 221 |
|
| 222 |
$wll_settings = get_option( 'wll_settings' ); |
| 223 |
|
| 224 |
if( isset( $wll_settings['record_ip_address'] ) && intval( $wll_settings['record_ip_address'] ) == 1 ){ |
| 225 |
|
| 226 |
// call function to anonymize here. |
| 227 |
$ip = When_Last_Login::wll_get_user_ip_address(); |
| 228 |
|
| 229 |
if ( ! empty( $post_id ) ) { |
| 230 |
update_post_meta( $post_id, 'wll_user_ip_address', $ip ); |
| 231 |
} |
| 232 |
|
| 233 |
update_user_meta( $user->ID, 'wll_user_ip_address', $ip ); |
| 234 |
} |
| 235 |
|
| 236 |
do_action( 'wll_logged_in_action', array( 'login_count' => $wll_new_value, 'user' => $user ), $wll_settings ); |
| 237 |
|
| 238 |
} |
| 239 |
|
| 240 |
public function wll_user_register( $user_id ){ |
| 241 |
|
| 242 |
$wll_settings = get_option( 'wll_settings' ); |
| 243 |
|
| 244 |
if( isset( $wll_settings['record_ip_address'] ) && $wll_settings['record_ip_address'] == 1 ){ |
| 245 |
|
| 246 |
$ip = When_Last_Login::wll_get_user_ip_address(); |
| 247 |
update_user_meta( $user_id, 'wll_user_ip_address', $ip ); |
| 248 |
|
| 249 |
} |
| 250 |
|
| 251 |
do_action( 'wll_register_action', $user_id, $wll_settings ); |
| 252 |
|
| 253 |
} |
| 254 |
|
| 255 |
public static function login_record_cp(){ |
| 256 |
|
| 257 |
global $show_login_records; |
| 258 |
|
| 259 |
$settings = get_option( 'wll_settings' ); |
| 260 |
|
| 261 |
$show = (!empty($settings['show_all_login_records']) AND $settings['show_all_login_records'] === 1); |
| 262 |
|
| 263 |
$show_login_records = apply_filters( 'when_last_login_show_records_table', $show ); |
| 264 |
|
| 265 |
if( $show_login_records != true ){ |
| 266 |
return; |
| 267 |
} |
| 268 |
|
| 269 |
$labels = array( |
| 270 |
'name' => __( 'Login Records', 'when-last-login' ), |
| 271 |
'singular_name' => __( 'Login Record', 'when-last-login' ), |
| 272 |
'menu_name' => __( 'Login Records', 'when-last-login' ), |
| 273 |
'name_admin_bar' => __( 'Login Record', 'when-last-login' ), |
| 274 |
'add_new' => __( 'Add New', 'when-last-login' ), |
| 275 |
'add_new_item' => __( 'Add New Login Record', 'when-last-login' ), |
| 276 |
'new_item' => __( 'New Login Record', 'when-last-login' ), |
| 277 |
'edit_item' => __( 'Edit Login Record', 'when-last-login' ), |
| 278 |
'view_item' => __( 'View Login Record', 'when-last-login' ), |
| 279 |
'all_items' => __( 'All Login Records', 'when-last-login' ), |
| 280 |
'search_items' => __( 'Search Login Records', 'when-last-login' ), |
| 281 |
'parent_item_colon' => __( 'Parent Login Records:', 'when-last-login' ), |
| 282 |
'not_found' => __( 'No login records found.', 'when-last-login' ), |
| 283 |
'not_found_in_trash' => __( 'No login records found in Trash.', 'when-last-login' ) |
| 284 |
); |
| 285 |
|
| 286 |
$args = array( |
| 287 |
'labels' => $labels, |
| 288 |
'description' => __( 'Description.', 'when-last-login' ), |
| 289 |
'public' => false, |
| 290 |
'publicly_queryable' => false, |
| 291 |
'show_ui' => true, |
| 292 |
'show_in_menu' => 'when-last-login-settings', |
| 293 |
'query_var' => true, |
| 294 |
'rewrite' => array( 'slug' => 'when-last-login-records' ), |
| 295 |
'capability_type' => 'post', |
| 296 |
'has_archive' => true, |
| 297 |
'hierarchical' => false, |
| 298 |
'menu_position' => null, |
| 299 |
'supports' => array( 'title', 'author' ), |
| 300 |
'capabilities' => array( |
| 301 |
'create_posts' => false, |
| 302 |
), |
| 303 |
'map_meta_cap' => true, |
| 304 |
); |
| 305 |
|
| 306 |
register_post_type( 'wll_records', $args ); |
| 307 |
} |
| 308 |
|
| 309 |
/** |
| 310 |
* Setup admin backend to display custom meta box for login count for admins |
| 311 |
*/ |
| 312 |
public static function admin_dashboard_widget(){ |
| 313 |
|
| 314 |
global $show_widget; |
| 315 |
|
| 316 |
$show_widget = apply_filters( 'when_last_login_show_admin_widget', true ); |
| 317 |
//only show for administrators |
| 318 |
if( current_user_can( 'manage_options' ) && $show_widget ){ |
| 319 |
wp_add_dashboard_widget( 'when_last_login_top_users', __( 'Most Frequent Logins', 'when-last-login' ), array( 'When_Last_Login', 'admin_dashboard_widget_display' ) ); |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
public static function admin_dashboard_widget_display(){ |
| 324 |
|
| 325 |
global $show_widget, $show_login_records; |
| 326 |
|
| 327 |
if( $show_widget != true ){ |
| 328 |
return; |
| 329 |
} |
| 330 |
|
| 331 |
if( is_network_admin() ){ |
| 332 |
|
| 333 |
$sites = get_sites(); |
| 334 |
|
| 335 |
if( is_array( $sites ) ){ |
| 336 |
|
| 337 |
foreach( $sites as $site ){ |
| 338 |
|
| 339 |
$blog_id = $site->blog_id; |
| 340 |
$blog_details = get_blog_details( $blog_id ); |
| 341 |
|
| 342 |
?><table width="100%" text-align="center" class='wp-list-table striped widefat'> |
| 343 |
<tr> |
| 344 |
<th colspan='4' style='text-align: center;'><strong><?php echo esc_html( $blog_details->blogname ) .' (<a href="'. esc_url( $blog_details->siteurl ).'" target="_BLANK">'. esc_html( $blog_details->siteurl ) . ')</a>'; ?></strong></th> |
| 345 |
</tr> |
| 346 |
<?php |
| 347 |
|
| 348 |
$user_query = new WP_User_Query( array( 'meta_key' => 'when_last_login_count', 'meta_value' => 0, 'meta_compare' => '!=', 'order' => 'DESC', 'orderby' => 'meta_value_num', 'number' => apply_filters( 'wll_top_widget_user_count', 3 ), 'blog_id' => $blog_id, 'role__not_in' => array( 'administrator' ) ) ); |
| 349 |
|
| 350 |
$topusers = $user_query->get_results(); |
| 351 |
|
| 352 |
if( $topusers ){ |
| 353 |
?> |
| 354 |
<tr> |
| 355 |
<th><strong>#</strong></th> |
| 356 |
<th><strong><?php esc_html_e( 'Users', 'when-last-login' ); ?></strong></th> |
| 357 |
<th><strong><?php esc_html_e( 'Login Count', 'when-last-login' ); ?></strong></th> |
| 358 |
<th><strong><?php esc_html_e( 'Last Logged In', 'when-last-login' ); ?></strong></th> |
| 359 |
</tr> |
| 360 |
<?php |
| 361 |
|
| 362 |
$count = 1; |
| 363 |
|
| 364 |
foreach($topusers as $wllusers){ |
| 365 |
echo '<tr><td>' . intval( $count ) . '</td>'; |
| 366 |
echo '<td>' . esc_html( $wllusers->display_name ) . '</td>'; |
| 367 |
echo '<td>' . get_user_meta( $wllusers->ID, 'when_last_login_count', true ) . '</td>'; |
| 368 |
echo '<td>' . date_i18n( 'Y-m-d H:i:s', get_user_meta( $wllusers->ID, 'when_last_login', true ) ) . '</td></tr>'; |
| 369 |
$count++; |
| 370 |
} |
| 371 |
|
| 372 |
} else { |
| 373 |
|
| 374 |
echo '<tr><td colspan="4">'. esc_html__('No data yet', 'when-last-login').'</td></tr>'; |
| 375 |
|
| 376 |
} |
| 377 |
|
| 378 |
?></table><br/><?php |
| 379 |
|
| 380 |
} |
| 381 |
|
| 382 |
?> |
| 383 |
|
| 384 |
<a href="<?php echo admin_url( 'users.php?orderby=when_last_login&order=desc' ); ?>"><?php _e( 'View All Users', 'when-last-login' ); ?></a> |
| 385 |
|
| 386 |
<?php if( $show_login_records == true ){ ?> |
| 387 |
<a style="float:right" href="<?php echo admin_url( 'edit.php?post_type=wll_records' ); ?>"><?php _e( 'View Login Records', 'when-last-login' ); } //end the if filter check here ?></a> |
| 388 |
<?php |
| 389 |
|
| 390 |
} |
| 391 |
|
| 392 |
} else { |
| 393 |
|
| 394 |
?><table width="100%" text-align="center" class='wp-list-table striped widefat'> |
| 395 |
|
| 396 |
<?php |
| 397 |
|
| 398 |
$user_query = new WP_User_Query( array( 'meta_key' => 'when_last_login_count', 'meta_value' => 0, 'meta_compare' => '!=', 'order' => 'DESC', 'orderby' => 'meta_value_num', 'number' => apply_filters( 'wll_top_widget_user_count', 3 ), 'role__not_in' => array( 'administrator' ) ) ); |
| 399 |
|
| 400 |
$topusers = $user_query->get_results(); |
| 401 |
|
| 402 |
if( $topusers ){ |
| 403 |
?> |
| 404 |
<tr> |
| 405 |
<th><strong>#</strong></th> |
| 406 |
<th><strong><?php esc_html_e( 'Users', 'when-last-login' ); ?></strong></th> |
| 407 |
<th><strong><?php esc_html_e( 'Login Count', 'when-last-login' ); ?></strong></th> |
| 408 |
<th><strong><?php esc_html_e( 'Last Logged In', 'when-last-login' ); ?></strong></th> |
| 409 |
</tr> |
| 410 |
<?php |
| 411 |
|
| 412 |
$count = 1; |
| 413 |
|
| 414 |
foreach($topusers as $wllusers){ |
| 415 |
echo '<tr><td>' . intval( $count ) . '</td>'; |
| 416 |
echo '<td>' . $wllusers->display_name . '</td>'; |
| 417 |
echo '<td>' . get_user_meta( $wllusers->ID, 'when_last_login_count', true ) . '</td>'; |
| 418 |
echo '<td>' . date_i18n( 'Y-m-d H:i:s', get_user_meta( $wllusers->ID, 'when_last_login', true ) ) . '</td></tr>'; |
| 419 |
$count++; |
| 420 |
} |
| 421 |
|
| 422 |
} else { |
| 423 |
|
| 424 |
echo '<tr><td colspan="4">'. esc_html__( 'No data yet', 'when-last-login' ).'</td></tr>'; |
| 425 |
|
| 426 |
} |
| 427 |
|
| 428 |
?></table><br/><?php |
| 429 |
|
| 430 |
?> |
| 431 |
|
| 432 |
<a href="<?php echo admin_url( 'users.php?orderby=when_last_login&order=desc' ); ?>"><?php esc_html_e( 'View All Users', 'when-last-login' ); ?></a> |
| 433 |
|
| 434 |
<?php if( $show_login_records == true ){ ?> |
| 435 |
<a style="float:right" href="<?php echo admin_url( 'edit.php?post_type=wll_records' ); ?>"><?php esc_html_e( 'View Login Records', 'when-last-login' ); } //end the if filter check here ?></a> |
| 436 |
<?php |
| 437 |
|
| 438 |
} |
| 439 |
|
| 440 |
} |
| 441 |
|
| 442 |
/** |
| 443 |
* Setup Column and data for users page with sortable |
| 444 |
*/ |
| 445 |
public static function column_header( $column ){ |
| 446 |
$settings = get_option( 'wll_settings' ); |
| 447 |
|
| 448 |
$column['when_last_login'] = esc_html__( 'Last Login', 'when-last-login' ); |
| 449 |
|
| 450 |
if ( ! empty( $settings['record_ip_address'] ) ) { |
| 451 |
$column['when_last_login_ip_address'] = esc_html__( 'IP Address', 'when-last-login' ); |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
return $column; |
| 456 |
} |
| 457 |
|
| 458 |
public static function column_data( $value, $column_name, $id ){ |
| 459 |
|
| 460 |
$settings = get_option( 'wll_settings' ); |
| 461 |
|
| 462 |
if ( $column_name == 'when_last_login' ){ |
| 463 |
|
| 464 |
$when_last_login_meta = get_the_author_meta( 'when_last_login', $id ); |
| 465 |
|
| 466 |
if( ! empty( $when_last_login_meta ) ){ |
| 467 |
return human_time_diff( $when_last_login_meta ); |
| 468 |
} else { |
| 469 |
if( get_the_author_meta( 'when_last_login', $id ) === 0 ){ |
| 470 |
return esc_html__( 'Never', 'when-last-login' ); |
| 471 |
} else { |
| 472 |
update_user_meta( $id, 'when_last_login', 0 ); |
| 473 |
return esc_html__( 'Never', 'when-last-login' ); |
| 474 |
} |
| 475 |
} |
| 476 |
} else if( $column_name == 'when_last_login_ip_address' ){ |
| 477 |
|
| 478 |
$when_last_login_ip_address = get_user_meta( $id, 'wll_user_ip_address', true ); |
| 479 |
|
| 480 |
if ( $when_last_login_ip_address && $when_last_login_ip_address != "" && $settings['record_ip_address'] != "") { |
| 481 |
return "<a href='http://www.ip-adress.com/ip_tracer/". esc_attr( $when_last_login_ip_address ) ."' target='_BLANK' title='".__( 'Lookup', 'when-last-login' )."'>" . esc_html( $when_last_login_ip_address ) . "</a>"; |
| 482 |
} else { |
| 483 |
return esc_html__( 'IP Address Not Recorded', 'when-last-login' ); |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
} |
| 488 |
return $value; |
| 489 |
} |
| 490 |
|
| 491 |
public static function column_sortable( $columns ){ |
| 492 |
$columns['when_last_login'] = 'when_last_login'; |
| 493 |
return $columns; |
| 494 |
} |
| 495 |
|
| 496 |
public static function sort_by_login_date( $query ) { |
| 497 |
if ( 'when_last_login' == $query->get( 'orderby' ) ) { |
| 498 |
$query->set( 'orderby', 'meta_value_num' ); |
| 499 |
$query->set( 'meta_key', 'when_last_login' ); |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
/* |
| 504 |
* Support for Paid Memberships Pro |
| 505 |
* TODO: use existing PMPro usermeta if installed |
| 506 |
*/ |
| 507 |
public static function pmpro_memberlist_add_header( $users ){ |
| 508 |
if( !defined( 'PMPRO_VERSION' ) ){ |
| 509 |
return; |
| 510 |
} |
| 511 |
?> |
| 512 |
<th><?php esc_html_e( 'Last Login', 'when-last-login' );?></th> |
| 513 |
<?php |
| 514 |
|
| 515 |
} |
| 516 |
|
| 517 |
public static function pmpro_memberlist_add_column_data( $users ){ |
| 518 |
if( !defined( 'PMPRO_VERSION' ) ){ |
| 519 |
return; |
| 520 |
} |
| 521 |
?> |
| 522 |
<td> |
| 523 |
<?php |
| 524 |
if( ! empty( $users->when_last_login ) ){ |
| 525 |
echo human_time_diff( $users->when_last_login ); |
| 526 |
}else{ |
| 527 |
return esc_html_e( 'Never', 'when-last-login' ); |
| 528 |
} |
| 529 |
?> |
| 530 |
</td> |
| 531 |
<?php |
| 532 |
} |
| 533 |
|
| 534 |
public function wll_settings_page(){ |
| 535 |
|
| 536 |
add_menu_page( __('When Last Login', 'when-last-login'), esc_html__('When Last Login', 'when-last-login'), 'manage_options', 'when-last-login-settings', array( $this, 'wll_settings_callback' ), 'dashicons-visibility'); |
| 537 |
|
| 538 |
add_submenu_page( 'when-last-login-settings', esc_html__('Settings', 'when-last-login'), __('Settings', 'when-last-login'), 'manage_options', 'when-last-login-settings', array( $this, 'wll_settings_callback' ) ); |
| 539 |
|
| 540 |
add_submenu_page( 'when-last-login-settings', esc_html__('Extensions', 'when-last-login'), __('Extensions', 'when-last-login'), 'manage_options', 'admin.php?page=when-last-login-settings&tab=add-ons' ); |
| 541 |
|
| 542 |
do_action( 'wll_settings_admin_menu_item' ); |
| 543 |
|
| 544 |
} |
| 545 |
|
| 546 |
public function wll_settings_callback(){ |
| 547 |
|
| 548 |
include WLL_DIR_PATH . '/includes/settings.php'; |
| 549 |
|
| 550 |
} |
| 551 |
|
| 552 |
public function wll_settings_page_head(){ |
| 553 |
|
| 554 |
$wll_settings = array(); |
| 555 |
|
| 556 |
if( isset( $_POST['wll_save_settings'] ) ){ |
| 557 |
|
| 558 |
if( wp_verify_nonce( $_POST['_nonce'], 'wll_settings_nonce' ) ) { |
| 559 |
|
| 560 |
$wll_settings['user_access'] = isset( $_POST['wll_login_record_user_access'] ) ? sanitize_text_field( $_POST['wll_login_record_user_access'] ) : ""; |
| 561 |
$wll_settings['record_ip_address'] = isset( $_POST['wll_record_user_ip_address'] ) && sanitize_text_field( $_POST['wll_record_user_ip_address'] ) == '1' ? 1 : 0; |
| 562 |
$wll_settings['show_all_login_records'] = isset( $_POST['wll_all_login_records'] ) && sanitize_text_field( $_POST['wll_all_login_records'] ) == '1' ? 1 : 0; |
| 563 |
|
| 564 |
$wll_settings = apply_filters( 'wll_settings_filter', $wll_settings ); |
| 565 |
|
| 566 |
if ( update_option( 'wll_settings', $wll_settings ) ) { |
| 567 |
//show admin notice here. |
| 568 |
add_action( 'admin_notices', array( $this, 'wll_admin_notices' ) ); |
| 569 |
} |
| 570 |
} else { |
| 571 |
die( 'nonce not valid' ); |
| 572 |
} |
| 573 |
|
| 574 |
} |
| 575 |
|
| 576 |
} |
| 577 |
|
| 578 |
public function wll_admin_notices() { |
| 579 |
?> |
| 580 |
<div class="notice notice-success is-dismissible"> |
| 581 |
<p><?php esc_html_e( 'Settings saved successfully.', 'when-last-login' ); ?></p> |
| 582 |
</div> |
| 583 |
<?php |
| 584 |
} |
| 585 |
|
| 586 |
public function wll_remove_records_notice__success() { |
| 587 |
?> |
| 588 |
<div class="notice notice-success is-dismissible"> |
| 589 |
<p><?php esc_html_e( 'Records have been removed successfully.', 'when-last-login' ); ?></p> |
| 590 |
</div> |
| 591 |
<?php |
| 592 |
} |
| 593 |
|
| 594 |
public function wll_remove_records_notice__warning() { |
| 595 |
?> |
| 596 |
<div class="notice notice-warning is-dismissible"> |
| 597 |
<p><?php esc_html_e( 'No old records to remove.', 'when-last-login' ); ?></p> |
| 598 |
</div> |
| 599 |
<?php |
| 600 |
} |
| 601 |
|
| 602 |
/** |
| 603 |
* Function to remove logs automatically older than 3 months. |
| 604 |
* @since 1.0.0 |
| 605 |
*/ |
| 606 |
public function wll_automatically_remove_logs() { |
| 607 |
global $pagenow, $wpdb; |
| 608 |
|
| 609 |
// Bail if there is not ?page=xxx parameter |
| 610 |
if ( empty( $_GET['page'] ) ) { |
| 611 |
return; |
| 612 |
} |
| 613 |
|
| 614 |
// Bail if not on our settings page |
| 615 |
if ( 'admin.php' == $pagenow && 'when-last-login-settings' != $_GET['page'] ) { |
| 616 |
return; |
| 617 |
} |
| 618 |
|
| 619 |
$sql = "DELETE p, pm FROM $wpdb->posts p LEFT JOIN $wpdb->postmeta pm ON pm.post_id = p.ID WHERE p.post_type = 'wll_records'"; |
| 620 |
|
| 621 |
if ( isset( $_REQUEST['remove_all_wll_records'] ) ) { |
| 622 |
|
| 623 |
$nonce = $_REQUEST['wll_remove_all_records_nonce']; |
| 624 |
if ( wp_verify_nonce( $nonce, 'wll_remove_all_records_nonce' ) ) { |
| 625 |
|
| 626 |
if ( $wpdb->query( $sql ) > 0 ) { |
| 627 |
add_action( 'admin_notices', array( $this, 'wll_remove_records_notice__success' ) ); |
| 628 |
} else { |
| 629 |
add_action( 'admin_notices', array( $this, 'wll_remove_records_notice__warning' ) ); |
| 630 |
} |
| 631 |
} else { |
| 632 |
die( 'nonce not valid.' ); |
| 633 |
} |
| 634 |
} |
| 635 |
|
| 636 |
if ( isset( $_REQUEST['remove_wll_records'] ) ) { |
| 637 |
|
| 638 |
$nonce = $_REQUEST['wll_remove_records_nonce']; |
| 639 |
if ( wp_verify_nonce( $nonce, 'wll_remove_records_nonce' ) ) { |
| 640 |
|
| 641 |
$date = apply_filters( 'wll_automatically_remove_logs_date', date( 'Y-m-d', strtotime( '-3 months' ) ) ); |
| 642 |
|
| 643 |
$sql .= " AND p.post_date <= '$date'"; |
| 644 |
|
| 645 |
if ( $wpdb->query( $sql ) > 0 ) { |
| 646 |
add_action( 'admin_notices', array( $this, 'wll_remove_records_notice__success' ) ); |
| 647 |
} else { |
| 648 |
add_action( 'admin_notices', array( $this, 'wll_remove_records_notice__warning' ) ); |
| 649 |
} |
| 650 |
} else { |
| 651 |
die( 'nonce not valid.' ); |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
if ( isset( $_REQUEST['remove_wll_ip_addresses'] ) ) { |
| 656 |
|
| 657 |
$nonce = $_REQUEST['wll_remove_ip_nonce']; |
| 658 |
if ( wp_verify_nonce( $nonce, 'wll_remove_ip_nonce' ) ) { |
| 659 |
|
| 660 |
$sql = "DELETE FROM $wpdb->usermeta WHERE meta_key = 'wll_user_ip_address'"; |
| 661 |
|
| 662 |
if ( $wpdb->query( $sql ) > 0 ) { |
| 663 |
add_action( 'admin_notices', array( $this, 'wll_remove_records_notice__success' ) ); |
| 664 |
} else { |
| 665 |
add_action( 'admin_notices', array( $this, 'wll_remove_records_notice__warning' ) ); |
| 666 |
} |
| 667 |
} else { |
| 668 |
die( 'nonce not valid.' ); |
| 669 |
} |
| 670 |
} |
| 671 |
} |
| 672 |
|
| 673 |
|
| 674 |
public function wll_records_columns( $columns ){ |
| 675 |
|
| 676 |
return array_merge( $columns, array( 'wll-ip-address' => __( 'IP Address', 'when-last-login' ) ) ); |
| 677 |
|
| 678 |
} |
| 679 |
|
| 680 |
public function wll_records_column_contents( $column, $post_id ){ |
| 681 |
|
| 682 |
switch ( $column ) { |
| 683 |
case 'wll-ip-address': |
| 684 |
$ip_address = get_post_meta( $post_id, 'wll_user_ip_address', true ); |
| 685 |
if ( ! empty( $ip_address ) && $ip_address != "" ) { |
| 686 |
echo "<a href='http://www.ip-adress.com/ip_tracer/". esc_attr( $ip_address ) ."' target='_BLANK' title='".__( 'Lookup', 'when-last-login' )."'>" . esc_html( $ip_address ) . "</a>"; |
| 687 |
} else { |
| 688 |
esc_html_e( 'IP Address Not Recorded', 'when-last-login' ); |
| 689 |
} |
| 690 |
break; |
| 691 |
|
| 692 |
} |
| 693 |
} |
| 694 |
|
| 695 |
public function wll_plugin_action_links( $links ) { |
| 696 |
$new_links = array( |
| 697 |
'<a href="' . admin_url('admin.php?page=when-last-login-settings') . '" title="' . esc_attr( __( 'View Settings', 'when-last-login' ) ) . '">' . __( 'Settings', 'when-last-login' ) . '</a>' |
| 698 |
); |
| 699 |
|
| 700 |
$new_links = apply_filters( 'wll_plugin_action_links', $new_links ); |
| 701 |
|
| 702 |
return array_merge( $new_links, $links ); |
| 703 |
} |
| 704 |
|
| 705 |
public function wll_plugin_row_meta( $links, $file ) { |
| 706 |
if ( strpos( $file, 'when-last-login.php' ) !== false ) { |
| 707 |
$new_links = array( |
| 708 |
'<a href="' . admin_url('admin.php?page=when-last-login-settings') . '" title="' . esc_attr( __( 'View Settings', 'when-last-login' ) ) . '">' . __( 'Settings', 'when-last-login' ) . '</a>', |
| 709 |
'<a href="' . esc_url( 'https://yoohooplugins.com/?s=when+last+login' ) . '" title="' . esc_attr__( 'View Documentation', 'when-last-login' ) . '">' . esc_html__( 'Docs', 'when-last-login' ) . '</a>', |
| 710 |
'<a href="' . esc_url( 'https://yoohooplugins.com/support/' ) . '" title="' . esc_attr__( 'Visit Customer Support Forum', 'when-last-login' ) . '">' . esc_html__( 'Support', 'when-last-login' ) . '</a>', |
| 711 |
); |
| 712 |
|
| 713 |
$new_links = apply_filters( 'wll_plugin_row_meta', $new_links ); |
| 714 |
$links = array_merge( $links, $new_links ); |
| 715 |
} |
| 716 |
return $links; |
| 717 |
} |
| 718 |
|
| 719 |
public static function wll_get_user_ip_address(){ |
| 720 |
|
| 721 |
if( !empty( $_SERVER['HTTP_CLIENT_IP'] ) ){ |
| 722 |
$ip = $_SERVER['HTTP_CLIENT_IP']; |
| 723 |
} else if ( !empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ){ |
| 724 |
$ip = $_SERVER['HTTP_X_FORWARDED_FOR']; |
| 725 |
} else { |
| 726 |
$ip = $_SERVER['REMOTE_ADDR']; |
| 727 |
} |
| 728 |
|
| 729 |
$ip = apply_filters( 'wll_user_ip_address', $ip ); |
| 730 |
|
| 731 |
if ( apply_filters( 'wll_force_anon_ip', false ) ) { |
| 732 |
return $ip; |
| 733 |
} else { |
| 734 |
return IpAnonymizer::anonymizeIp( $ip ); |
| 735 |
} |
| 736 |
|
| 737 |
return IpAnonymizer::anonymizeIp( $ip ); |
| 738 |
} |
| 739 |
|
| 740 |
} // end class |
| 741 |
When_Last_Login::get_instance(); |
| 742 |
|