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