| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: WP Last Login |
| 4 |
* Plugin URI: http://en.wp.obenland.it/wp-last-login/#utm_source=wordpress&utm_medium=plugin&utm_campaign=wp-last-login |
| 5 |
* Description: Displays the date of the last login in user lists. |
| 6 |
* Version: 4 |
| 7 |
* Author: Konstantin Obenland |
| 8 |
* Author URI: http://en.wp.obenland.it/#utm_source=wordpress&utm_medium=plugin&utm_campaign=wp-last-login |
| 9 |
* Text Domain: wp-last-login |
| 10 |
* Domain Path: /lang |
| 11 |
* License: GPLv2 |
| 12 |
* |
| 13 |
* @package WP Last Login |
| 14 |
*/ |
| 15 |
|
| 16 |
if ( ! class_exists( 'Obenland_Wp_Plugins_V4' ) ) { |
| 17 |
require_once 'obenland-wp-plugins.php'; |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Class Obenland_Wp_Last_Login. |
| 22 |
*/ |
| 23 |
class Obenland_Wp_Last_Login extends Obenland_Wp_Plugins_V4 { |
| 24 |
|
| 25 |
/** |
| 26 |
* Constructor. |
| 27 |
* |
| 28 |
* @author Konstantin Obenland |
| 29 |
* @since 1.0 - 23.01.2012 |
| 30 |
* @access public |
| 31 |
*/ |
| 32 |
public function __construct() { |
| 33 |
parent::__construct( array( |
| 34 |
'textdomain' => 'wp-last-login', |
| 35 |
'plugin_path' => __FILE__, |
| 36 |
'donate_link_id' => 'K32M878XHREQC', |
| 37 |
) ); |
| 38 |
|
| 39 |
load_plugin_textdomain( 'wp-last-login', false, 'wp-last-login/lang' ); |
| 40 |
|
| 41 |
$this->hook( 'wp_login' ); |
| 42 |
$this->hook( 'user_register' ); |
| 43 |
|
| 44 |
/** |
| 45 |
* Programmers: |
| 46 |
* To limit this information to certain user roles, add a filter to |
| 47 |
* 'wpll_current_user_can' and check for user permissions, returning |
| 48 |
* true or false! |
| 49 |
* |
| 50 |
* Example: |
| 51 |
* |
| 52 |
* function prefix_wpll_visibility( $bool ) { |
| 53 |
* return current_user_can( 'manage_options' ); // Only for Admins |
| 54 |
* } |
| 55 |
* add_filter( 'wpll_current_user_can', 'prefix_wpll_visibility' ); |
| 56 |
*/ |
| 57 |
if ( is_admin() && apply_filters( 'wpll_current_user_can', true ) ) { |
| 58 |
|
| 59 |
$this->hook( 'manage_site-users-network_columns', 'add_column', 1 ); |
| 60 |
$this->hook( 'manage_users_columns', 'add_column', 1 ); |
| 61 |
$this->hook( 'wpmu_users_columns', 'add_column', 1 ); |
| 62 |
$this->hook( 'admin_print_styles-users.php', 'column_style' ); |
| 63 |
$this->hook( 'admin_print_styles-site-users.php', 'column_style' ); |
| 64 |
$this->hook( 'manage_users_custom_column' ); |
| 65 |
$this->hook( 'manage_users_sortable_columns', 'add_sortable' ); |
| 66 |
$this->hook( 'manage_users-network_sortable_columns', 'add_sortable' ); |
| 67 |
$this->hook( 'pre_get_users' ); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Update the login timestamp. |
| 73 |
* |
| 74 |
* @author Konstantin Obenland |
| 75 |
* @since 1.0 - 23.01.2012 |
| 76 |
* @access public |
| 77 |
* |
| 78 |
* @param string $user_login The user's login name. |
| 79 |
* |
| 80 |
* @return void |
| 81 |
*/ |
| 82 |
public function wp_login( $user_login ) { |
| 83 |
$user = get_user_by( 'login', $user_login ); |
| 84 |
update_user_meta( $user->ID, $this->textdomain, time() ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Set default data for new users. |
| 89 |
* |
| 90 |
* @author Konstantin Obenland |
| 91 |
* @since 3 - 12.09.2019 |
| 92 |
* @access public |
| 93 |
* |
| 94 |
* @param int $user_id The user ID. |
| 95 |
*/ |
| 96 |
public function user_register( $user_id ) { |
| 97 |
update_user_meta( $user_id, $this->textdomain, 0 ); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Adds the last login column to the network admin user list. |
| 102 |
* |
| 103 |
* @author Konstantin Obenland |
| 104 |
* @since 1.0 - 23.01.2012 |
| 105 |
* @access public |
| 106 |
* |
| 107 |
* @param array $cols The default columns. |
| 108 |
* |
| 109 |
* @return array |
| 110 |
*/ |
| 111 |
public function add_column( $cols ) { |
| 112 |
$cols[ $this->textdomain ] = __( 'Last Login', 'wp-last-login' ); |
| 113 |
return $cols; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Adds the last login column to the network admin user list. |
| 118 |
* |
| 119 |
* @author Konstantin Obenland |
| 120 |
* @since 1.0 - 23.01.2012 |
| 121 |
* @access public |
| 122 |
* |
| 123 |
* @param string $value Value of the custom column. |
| 124 |
* @param string $column_name The name of the column. |
| 125 |
* @param int $user_id The user's id. |
| 126 |
* |
| 127 |
* @return string |
| 128 |
*/ |
| 129 |
public function manage_users_custom_column( $value, $column_name, $user_id ) { |
| 130 |
if ( $this->textdomain === $column_name ) { |
| 131 |
$value = __( 'Never.', 'wp-last-login' ); |
| 132 |
$last_login = (int) get_user_meta( $user_id, $this->textdomain, true ); |
| 133 |
|
| 134 |
if ( $last_login ) { |
| 135 |
$format = apply_filters( 'wpll_date_format', get_option( 'date_format' ) ); |
| 136 |
$value = get_date_from_gmt( date( 'Y-m-d H:i:s', $last_login ), $format ); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
return $value; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Register the column as sortable. |
| 145 |
* |
| 146 |
* @author Konstantin Obenland |
| 147 |
* @since 1.2.0 - 11.12.2012 |
| 148 |
* @access public |
| 149 |
* |
| 150 |
* @param array $columns User table columns. |
| 151 |
* |
| 152 |
* @return array |
| 153 |
*/ |
| 154 |
public function add_sortable( $columns ) { |
| 155 |
$columns[ $this->textdomain ] = $this->textdomain; |
| 156 |
|
| 157 |
return $columns; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Handle ordering by last login. |
| 162 |
* |
| 163 |
* @since 1.2.0 - 11.12.2012 |
| 164 |
* @access public |
| 165 |
* |
| 166 |
* @param WP_User_Query $user_query Request arguments. |
| 167 |
* |
| 168 |
* @return WP_User_Query |
| 169 |
*/ |
| 170 |
public function pre_get_users( $user_query ) { |
| 171 |
if ( isset( $user_query->query_vars['orderby'] ) && $this->textdomain === $user_query->query_vars['orderby'] ) { |
| 172 |
$user_query->query_vars = array_merge( $user_query->query_vars, array( |
| 173 |
// phpcs:ignore WordPress.VIP.SlowDBQuery.slow_db_query_meta_key |
| 174 |
'meta_key' => $this->textdomain, |
| 175 |
'orderby' => 'meta_value_num', |
| 176 |
) ); |
| 177 |
} |
| 178 |
|
| 179 |
return $user_query; |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Defines the width of the column |
| 184 |
* |
| 185 |
* @author Konstantin Obenland |
| 186 |
* @since 1.0 - 23.01.2012 |
| 187 |
* @access public |
| 188 |
* |
| 189 |
* @return void |
| 190 |
*/ |
| 191 |
public function column_style() { |
| 192 |
?> |
| 193 |
<style type="text/css"> |
| 194 |
.column-wp-last-login { width: 12%; } |
| 195 |
</style> |
| 196 |
<?php |
| 197 |
} |
| 198 |
|
| 199 |
} // End of class Obenland_Wp_Last_Login. |
| 200 |
|
| 201 |
|
| 202 |
new Obenland_Wp_Last_Login(); |
| 203 |
|
| 204 |
/** |
| 205 |
* Sets a default meta value for all users. |
| 206 |
* |
| 207 |
* Allows sorting users by last login to work, even though some might not have |
| 208 |
* recorded login time. |
| 209 |
* |
| 210 |
* @see https://wordpress.org/support/topic/wp-40-sorting-by-date-doesnt-work |
| 211 |
*/ |
| 212 |
function wpll_activate() { |
| 213 |
$user_ids = get_users( array( |
| 214 |
'blog_id' => '', |
| 215 |
'fields' => 'ID', |
| 216 |
) ); |
| 217 |
|
| 218 |
foreach ( $user_ids as $user_id ) { |
| 219 |
update_user_meta( $user_id, 'wp-last-login', 0 ); |
| 220 |
} |
| 221 |
} |
| 222 |
register_activation_hook( __FILE__, 'wpll_activate' ); |
| 223 |
|