PluginProbe
WP Last Login / 7
WP Last Login v7
trunk 1.0 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 1.3.0 1.4.0 2 3 4 5 6 7
wp-last-login / wp-last-login.php

wp-last-login.php in WP Last Login 7, at wp-last-login.php

192 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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: 7
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 /**
17 * Sets a default meta value for all users.
18 *
19 * Allows sorting users by last login to work, even though some might not have
20 * recorded login time.
21 *
22 * @see https://wordpress.org/support/topic/wp-40-sorting-by-date-doesnt-work
23 */
24 function wpll_activate() {
25 $user_ids = get_users(
26 array(
27 'blog_id' => '',
28 'fields' => 'ID',
29 )
30 );
31
32 foreach ( $user_ids as $user_id ) {
33 add_user_meta( $user_id, 'wp-last-login', 0 );
34 }
35 }
36 register_activation_hook( __FILE__, 'wpll_activate' );
37
38 /**
39 * Loads the plugin's translated strings.
40 */
41 function wpll_load_textdomain() {
42 load_plugin_textdomain( 'wp-last-login', false, 'wp-last-login/lang' );
43 }
44 add_action( 'init', 'wpll_load_textdomain' );
45
46 /**
47 * Update the login timestamp.
48 *
49 * @param string $user_login The user's login name.
50 */
51 function wpll_wp_login( $user_login ) {
52 $user = get_user_by( 'login', $user_login );
53 update_user_meta( $user->ID, 'wp-last-login', time() );
54 }
55 add_action( 'wp_login', 'wpll_wp_login' );
56
57 /**
58 * Update the login timestamp for two-factor authentication.
59 *
60 * The Two Factor plugin halts execution during the wp_login hook and the above
61 * callback is never fired.
62 * This callback is fired after the user has been authenticated with 2FA.
63 *
64 * @see https://github.com/WordPress/two-factor/blob/2b0d9bc964f9d9f7b86eac4eb0c4dbfb43c25c2c/class-two-factor-core.php#L567.
65 *
66 * @param WP_User $user The user object.
67 */
68 function wpll_two_factor_user_authenticated( $user ) {
69 update_user_meta( $user->ID, 'wp-last-login', time() );
70 }
71 add_action( 'two_factor_user_authenticated', 'wpll_two_factor_user_authenticated' );
72
73 /**
74 * Set default data for new users.
75 *
76 * @param int $user_id The user ID.
77 */
78 function wpll_user_register( $user_id ) {
79 update_user_meta( $user_id, 'wp-last-login', 0 );
80 }
81 add_action( 'user_register', 'wpll_user_register' );
82
83 /**
84 * Programmers:
85 * To limit this information to certain user roles, add a filter to
86 * 'wpll_current_user_can' and check for user permissions, returning
87 * true or false!
88 *
89 * Example:
90 *
91 * function prefix_wpll_visibility( $bool ) {
92 * return current_user_can( 'manage_options' ); // Only for Admins
93 * }
94 * add_filter( 'wpll_current_user_can', 'prefix_wpll_visibility' );
95 */
96 if ( ! apply_filters( 'wpll_current_user_can', true ) ) {
97 return;
98 }
99
100 /**
101 * Adds the last login column to the network admin user list.
102 *
103 * @param array $cols The default columns.
104 * @return array
105 */
106 function wpll_add_column( $cols ) {
107 $cols['wp-last-login'] = __( 'Last Login', 'wp-last-login' );
108
109 return $cols;
110 }
111 add_filter( 'manage_site-users-network_columns', 'wpll_add_column', 1 );
112 add_filter( 'manage_users_columns', 'wpll_add_column', 1 );
113 add_filter( 'wpmu_users_columns', 'wpll_add_column', 1 );
114
115 /**
116 * Defines the width of the column.
117 */
118 function wpll_column_style() {
119 echo '<style>.column-wp-last-login { width: 14%; }</style>';
120 }
121 add_filter( 'admin_print_styles-users.php', 'wpll_column_style' );
122 add_filter( 'admin_print_styles-site-users.php', 'wpll_column_style' );
123
124 /**
125 * Adds the last login column to the network admin user list.
126 *
127 * @param string $value Value of the custom column.
128 * @param string $column_name The name of the column.
129 * @param int $user_id The user's id.
130 * @return string
131 */
132 function wpll_manage_users_custom_column( $value, $column_name, $user_id ) {
133 if ( 'wp-last-login' === $column_name ) {
134 $value = __( 'Never.', 'wp-last-login' );
135 $last_login = (int) get_user_meta( $user_id, 'wp-last-login', true );
136
137 if ( $last_login ) {
138 /**
139 * Date format to use with last login date.
140 *
141 * @param string $format Date format. Default: `date_format` option value.
142 */
143 $format = apply_filters( 'wpll_date_format', get_option( 'date_format' ) );
144 $last_login = get_date_from_gmt( gmdate( 'Y-m-d H:i:s', $last_login ), 'U' );
145 $value = sprintf(
146 '<time title="%1$s" datetime="%2$s">%3$s</time>',
147 esc_attr( date_i18n( get_option( 'time_format' ), $last_login ) ),
148 esc_attr( gmdate( 'c', $last_login ) ),
149 esc_html( date_i18n( $format, $last_login ) )
150 );
151 }
152 }
153
154 return $value;
155 }
156 add_filter( 'manage_users_custom_column', 'wpll_manage_users_custom_column', 10, 3 );
157
158 /**
159 * Register the column as sortable.
160 *
161 * @param array $columns User table columns.
162 * @return array
163 */
164 function wpll_add_sortable( $columns ) {
165 $columns['wp-last-login'] = 'wp-last-login';
166
167 return $columns;
168 }
169 add_filter( 'manage_users_sortable_columns', 'wpll_add_sortable' );
170 add_filter( 'manage_users-network_sortable_columns', 'wpll_add_sortable' );
171
172 /**
173 * Handle ordering by last login.
174 *
175 * @param WP_User_Query $user_query Request arguments.
176 * @return WP_User_Query
177 */
178 function wpll_pre_get_users( $user_query ) {
179 if ( isset( $user_query->query_vars['orderby'] ) && 'wp-last-login' === $user_query->query_vars['orderby'] ) {
180 $user_query->query_vars = array_merge(
181 $user_query->query_vars,
182 array(
183 'meta_key' => 'wp-last-login', //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
184 'orderby' => 'meta_value_num',
185 )
186 );
187 }
188
189 return $user_query;
190 }
191 add_filter( 'pre_get_users', 'wpll_pre_get_users' );
192